mirror of
https://github.com/google/blockly.git
synced 2026-01-07 00:50:27 +01:00
Require field name to be string, test changes
* Now throws error for getField/getFieldValue/setFieldValue if provided name is not a string * Changed error to more specific TypeError * Type checking and error message moved up to getField * Tests added/modified to check that non-string types for field names produce type errors
This commit is contained in:
committed by
Christopher Allen
parent
62bb663b38
commit
672574b056
@@ -1024,9 +1024,15 @@ Blockly.Block.prototype.setOnChange = function(onchangeFn) {
|
||||
* @return {?Blockly.Field} Named field, or null if field does not exist.
|
||||
*/
|
||||
Blockly.Block.prototype.getField = function(name) {
|
||||
if (typeof name !== 'string') {
|
||||
throw TypeError('Blockly.Block.prototype.getField expects a string ' +
|
||||
'with the field name but received ' +
|
||||
(name === undefined ? 'nothing' : name + ' of type ' + typeof name) +
|
||||
' instead');
|
||||
}
|
||||
for (var i = 0, input; (input = this.inputList[i]); i++) {
|
||||
for (var j = 0, field; (field = input.fieldRow[j]); j++) {
|
||||
if (typeof field.name !== "undefined" && field.name == name) {
|
||||
if (field.name === name) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
@@ -1124,13 +1130,9 @@ Blockly.Block.prototype.getFieldValue = function(name) {
|
||||
/**
|
||||
* Sets the value of the given field for this block.
|
||||
* @param {*} newValue The value to set.
|
||||
* @param {!string} name The name of the field to set the value of.
|
||||
* @param {string} name The name of the field to set the value of.
|
||||
*/
|
||||
Blockly.Block.prototype.setFieldValue = function(newValue, name) {
|
||||
if (typeof name === "undefined") {
|
||||
throw Error("Call to Blockly.Block.prototype.setFieldValue without " +
|
||||
"required second argument of field name.");
|
||||
}
|
||||
var field = this.getField(name);
|
||||
if (!field) {
|
||||
throw Error('Field "' + name + '" not found.');
|
||||
|
||||
@@ -1112,22 +1112,52 @@ suite('Blocks', function() {
|
||||
});
|
||||
|
||||
test('Getting Field', function() {
|
||||
chai.assert.instanceOf(this.block.getField("TEXT"), Blockly.Field);
|
||||
chai.assert.instanceOf(this.block.getField('TEXT'), Blockly.Field);
|
||||
});
|
||||
test('Getting Field without Name', function() {
|
||||
chai.assert.isNull(this.block.getField());
|
||||
chai.assert.throws(this.block.getField.bind(this.block), TypeError);
|
||||
});
|
||||
test('Getting Value of Field without Name', function() {
|
||||
chai.assert.isNull(this.block.getFieldValue());
|
||||
chai.assert.throws(this.block.getFieldValue.bind(this.block), TypeError);
|
||||
});
|
||||
test('Getting Field with Wrong Type', function() {
|
||||
var testFunction = function() {
|
||||
return 'TEXT';
|
||||
};
|
||||
var inputs = [1, null, testFunction, {toString: testFunction}, ['TEXT']];
|
||||
for (var i = 0; i < inputs.length; i++) {
|
||||
chai.assert.throws(this.block.getField.bind(this.block, inputs[i]),
|
||||
TypeError);
|
||||
}
|
||||
});
|
||||
test('Getting Value of Field with Wrong Type', function() {
|
||||
var testFunction = function() {
|
||||
return 'TEXT';
|
||||
};
|
||||
var inputs = [1, null, testFunction, {toString: testFunction}, ['TEXT']];
|
||||
for (var i = 0; i < inputs.length; i++) {
|
||||
chai.assert.throws(
|
||||
this.block.getFieldValue.bind(this.block, inputs[i]), TypeError);
|
||||
}
|
||||
});
|
||||
test('Getting/Setting Field Value', function() {
|
||||
chai.assert.equal(this.block.getFieldValue("TEXT"), "test");
|
||||
this.block.setFieldValue("abc", "TEXT");
|
||||
chai.assert.equal(this.block.getFieldValue("TEXT"), "abc");
|
||||
chai.assert.equal(this.block.getFieldValue('TEXT'), 'test');
|
||||
this.block.setFieldValue('abc', 'TEXT');
|
||||
chai.assert.equal(this.block.getFieldValue('TEXT'), 'abc');
|
||||
});
|
||||
test('Setting Field without Name', function() {
|
||||
chai.assert.throws(this.block.setFieldValue.bind(this.block, 'test'));
|
||||
});
|
||||
test('Setting Field with Wrong Type', function() {
|
||||
var testFunction = function() {
|
||||
return 'TEXT';
|
||||
};
|
||||
var inputs = [1, null, testFunction, {toString: testFunction}, ['TEXT']];
|
||||
for (var i = 0; i < inputs.length; i++) {
|
||||
chai.assert.throws(this.block.setFieldValue.bind(this.block, 'test',
|
||||
inputs[i]), TypeError);
|
||||
}
|
||||
});
|
||||
});
|
||||
suite('Icon Management', function() {
|
||||
suite('Bubbles and Collapsing', function() {
|
||||
|
||||
Reference in New Issue
Block a user