diff --git a/core/block.js b/core/block.js index c7faf8525..0f4bf1cb0 100644 --- a/core/block.js +++ b/core/block.js @@ -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.'); diff --git a/tests/mocha/block_test.js b/tests/mocha/block_test.js index e9a2b9fbf..4d310cac2 100644 --- a/tests/mocha/block_test.js +++ b/tests/mocha/block_test.js @@ -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() {