Added more setValue test coverage.

This commit is contained in:
Beka Westberg
2019-05-24 16:13:21 -07:00
parent 5c614d73c1
commit e6caa50131
10 changed files with 436 additions and 27 deletions

View File

@@ -673,7 +673,11 @@ Blockly.Field.prototype.setValue = function(newValue) {
return;
}
newValue = this.doClassValidation_(newValue);
var validatedValue = this.doClassValidation_(newValue);
// Class validators might accidentally forget to return, we'll ignore that.
if (validatedValue !== undefined) {
newValue = validatedValue;
}
if (newValue === null) {
doLogging && console.log('invalid, return');
this.doValueInvalid_();
@@ -686,8 +690,7 @@ Blockly.Field.prototype.setValue = function(newValue) {
var localValidator = this.getValidator();
if (localValidator) {
var validatedValue = localValidator.call(this, newValue);
// Sometimes local validators are used as change listeners (bad!) which
// means they might return undefined accidentally, so we'll just ignore that.
// Local validators might accidentally forget to return, we'll ignore that.
if (validatedValue !== undefined) {
newValue = validatedValue;
}

View File

@@ -206,6 +206,7 @@ suite ('Angle Fields', function() {
Blockly.FieldTextInput.htmlInput_.untypedDefaultValue_ = 1;
});
teardown(function() {
this.angleField.setValidator(null);
Blockly.FieldTextInput.htmlInput_ = null;
});
suite('Null Validator', function() {
@@ -244,5 +245,21 @@ suite ('Angle Fields', function() {
assertValue(this.angleField, 30);
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.angleField.setValidator(function() {});
});
test('When Editing', function() {
this.angleField.isBeingEdited_ = true;
Blockly.FieldTextInput.htmlInput_.value = '2';
this.angleField.onHtmlInputChange_(null);
assertValue(this.angleField, 2);
this.angleField.isBeingEdited_ = false;
});
test('When Not Editing', function() {
this.angleField.setValue(2);
assertValue(this.angleField, 2);
});
});
});
});

View File

@@ -136,6 +136,9 @@ suite('Checkbox Fields', function() {
setup(function() {
this.checkboxField = new Blockly.FieldCheckbox(true);
});
teardown(function() {
this.checkboxField.setValidator(null);
});
suite('Null Validator', function() {
setup(function() {
this.checkboxField.setValidator(function() {
@@ -169,5 +172,14 @@ suite('Checkbox Fields', function() {
assertValue(this.checkboxField, 'FALSE', 'false');
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.checkboxField.setValidator(function() {});
});
test('New Value', function() {
this.checkboxField.setValue('FALSE');
assertValue(this.checkboxField, 'FALSE', 'false');
});
});
});
});

View File

@@ -184,6 +184,9 @@ suite ('Colour Fields', function() {
setup(function() {
this.colourField = new Blockly.FieldColour('#aaaaaa');
});
teardown(function() {
this.colourField.setValidator(null);
});
suite('Null Validator', function() {
setup(function() {
this.colourField.setValidator(function() {
@@ -206,5 +209,14 @@ suite ('Colour Fields', function() {
assertValue(this.colourField, '#ff0000', '#f00');
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.colourField.setValidator(function() {});
});
test('New Value', function() {
this.colourField.setValue('#000000');
assertValue(this.colourField, '#000000', '#000');
});
});
});
});

View File

@@ -153,6 +153,9 @@ suite ('Date Fields', function() {
setup(function() {
this.dateField = new Blockly.FieldDate('2020-02-20');
});
teardown(function() {
this.dateField.setValidator(null);
});
suite('Null Validator', function() {
setup(function() {
this.dateField.setValidator(function() {
@@ -175,5 +178,14 @@ suite ('Date Fields', function() {
assertValue(this.dateField, '3030-03-20');
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.dateField.setValidator(function() {});
});
test('New Value', function() {
this.dateField.setValue('3030-03-30');
assertValue(this.dateField, '3030-03-30');
});
});
});
});

View File

@@ -42,22 +42,28 @@ suite ('Dropdown Fields', function() {
});
});
test('Array Items not Arrays', function() {
console.log('You should see three console warnings after this message.');
var stub = sinon.stub(console, 'error');
chai.assert.throws(function() {
new Blockly.FieldDropdown([1, 2, 3]);
});
chai.assert(stub.calledThrice);
stub.restore();
});
test('Array Items with Invalid IDs', function() {
console.log('You should see three console warnings after this message.');
var stub = sinon.stub(console, 'error');
chai.assert.throws(function() {
new Blockly.FieldDropdown([['1', 1], ['2', 2], ['3', 3]]);
});
chai.assert(stub.calledThrice);
stub.restore();
});
test('Array Items with Invalid Content', function() {
console.log('You should see three console warnings after this message.');
var stub = sinon.stub(console, 'error');
chai.assert.throws(function() {
new Blockly.FieldDropdown([[1, '1'], [2, '2'], [3, '3']]);
});
chai.assert(stub.calledThrice);
stub.restore();
});
test('Text Dropdown', function() {
var dropdownField = new Blockly.FieldDropdown(
@@ -107,24 +113,30 @@ suite ('Dropdown Fields', function() {
});
});
test('Array Items not Arrays', function() {
console.log('You should see three console warnings after this message.');
var stub = sinon.stub(console, 'error');
chai.assert.throws(function() {
Blockly.FieldDropdown.fromJson({ options: [1, 2, 3] });
});
chai.assert(stub.calledThrice);
stub.restore();
});
test('Array Items with Invalid IDs', function() {
console.log('You should see three console warnings after this message.');
var stub = sinon.stub(console, 'error');
chai.assert.throws(function() {
Blockly.FieldDropdown.fromJson(
{ options:[['1', 1], ['2', 2], ['3', 3]] });
});
chai.assert(stub.calledThrice);
stub.restore();
});
test('Array Items with Invalid Content', function() {
console.log('You should see three console warnings after this message.');
var stub = sinon.stub(console, 'error');
chai.assert.throws(function() {
Blockly.FieldDropdown.fromJson(
{ options:[[1, '1'], [2, '2'], [3, '3']] });
});
chai.assert(stub.calledThrice);
stub.restore();
});
test('Text Dropdown', function() {
var dropdownField = Blockly.FieldDropdown.fromJson(
@@ -167,6 +179,9 @@ suite ('Dropdown Fields', function() {
["1a","1A"], ["1b","1B"], ["1c","1C"],
["2a","2A"], ["2b","2B"], ["2c","2C"]]);
});
teardown(function() {
this.dropdownField.setValidator(null);
});
suite('Null Validator', function() {
setup(function() {
this.dropdownField.setValidator(function() {
@@ -189,5 +204,14 @@ suite ('Dropdown Fields', function() {
assertValue(this.dropdownField, '1B', '1b');
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.dropdownField.setValidator(function() {});
});
test('New Value', function() {
this.dropdownField.setValue('1B');
assertValue(this.dropdownField, '1B', '1b');
});
});
});
});

View File

@@ -27,8 +27,8 @@ suite ('Number Fields', function() {
assertEquals(parseFloat(actualValue), expectedValue);
assertEquals(actualText, opt_expectedText);
}
function assertValueDefault(numberFieldField) {
assertValue(numberFieldField, 0);
function assertValueDefault(numberField) {
assertValue(numberField, 0);
}
function assertNumberField(numberField, expectedMin, expectedMax,
expectedPrecision, expectedValue) {
@@ -297,45 +297,65 @@ suite ('Number Fields', function() {
});
suite('Validators', function() {
setup(function() {
this.numberFieldField = new Blockly.FieldNumber(1);
this.numberField = new Blockly.FieldNumber(1);
Blockly.FieldTextInput.htmlInput_ = Object.create(null);
Blockly.FieldTextInput.htmlInput_.oldValue_ = '1';
Blockly.FieldTextInput.htmlInput_.untypedDefaultValue_ = 1;
});
teardown(function() {
this.numberField.setValidator(null);
Blockly.FieldTextInput.htmlInput_ = null;
});
suite('Null Validator', function() {
setup(function() {
this.numberFieldField.setValidator(function() {
this.numberField.setValidator(function() {
return null;
});
});
test('When Editing', function() {
this.numberFieldField.isBeingEdited_ = true;
this.numberField.isBeingEdited_ = true;
Blockly.FieldTextInput.htmlInput_.value = '2';
this.numberFieldField.onHtmlInputChange_(null);
assertValue(this.numberFieldField, 1, '2');
this.numberFieldField.isBeingEdited_ = false;
this.numberField.onHtmlInputChange_(null);
assertValue(this.numberField, 1, '2');
this.numberField.isBeingEdited_ = false;
});
test('When Not Editing', function() {
this.numberFieldField.setValue(2);
assertValue(this.numberFieldField, 1);
this.numberField.setValue(2);
assertValue(this.numberField, 1);
});
});
suite('Force End with 6 Validator', function() {
setup(function() {
this.numberFieldField.setValidator(function(newValue) {
this.numberField.setValidator(function(newValue) {
return String(newValue).replace(/.$/, "6");
});
});
test('When Editing', function() {
this.numberFieldField.isBeingEdited_ = true;
this.numberField.isBeingEdited_ = true;
Blockly.FieldTextInput.htmlInput_.value = '25';
this.numberFieldField.onHtmlInputChange_(null);
assertValue(this.numberFieldField, 26, '25');
this.numberFieldField.isBeingEdited_ = false;
this.numberField.onHtmlInputChange_(null);
assertValue(this.numberField, 26, '25');
this.numberField.isBeingEdited_ = false;
});
test('When Not Editing', function() {
this.numberFieldField.setValue(25);
assertValue(this.numberFieldField, 26);
this.numberField.setValue(25);
assertValue(this.numberField, 26);
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.numberField.setValidator(function() {});
});
test('When Editing', function() {
this.numberField.isBeingEdited_ = true;
Blockly.FieldTextInput.htmlInput_.value = '2';
this.numberField.onHtmlInputChange_(null);
assertValue(this.numberField, 2);
this.numberField.isBeingEdited_ = false;
});
test('When Not Editing', function() {
this.numberField.setValue(2);
assertValue(this.numberField, 2);
});
});
});

View File

@@ -49,8 +49,10 @@ suite ('Abstract Fields', function() {
test('Editable Default(true), Serializable Default(false)', function() {
// An old default field should be serialized.
var field = new FieldDefault();
console.log('You should receive a warning after this message');
var stub = sinon.stub(console, 'warn');
assertEquals(true, field.isSerializable());
chai.assert(stub.calledOnce);
stub.restore();
});
test('Editable False, Serializable Default(false)', function() {
// An old non-editable field should not be serialized.
@@ -70,4 +72,273 @@ suite ('Abstract Fields', function() {
assertEquals(true, field.isSerializable());
});
});
suite ('setValue', function() {
function addSpies(field) {
if (!this.isSpying) {
sinon.spy(field, 'doValueInvalid_');
sinon.spy(field, 'doValueUpdate_');
sinon.spy(field, 'forceRerender');
this.isSpying = true;
}
}
function removeSpies(field) {
if (this.isSpying) {
field.doValueInvalid_.restore();
field.doValueUpdate_.restore();
field.forceRerender.restore();
this.isSpying = false;
}
}
setup(function() {
this.field = new Blockly.Field();
this.field.isDirty_ = false;
this.cachedDoClassValidation = this.field.doClassValidation_;
this.cachedDoValueUpdate = this.field.doValueUpdate_;
this.cachedDoValueInvalid = this.field.doValueInvalid_;
});
teardown(function() {
removeSpies(this.field);
this.field.doClassValidation_ = this.cachedDoClassValidation;
this.field.doValueUpdate_ = this.cachedDoValueUpdate;
this.field.doValueInvalid_ = this.cachedDoValueInvalid;
this.field.setValidator(null);
});
test('Null', function() {
addSpies(this.field);
this.field.setValue(null);
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.notCalled);
chai.assert(this.field.forceRerender.notCalled);
});
test('No Validators, Dirty (Default)', function() {
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
chai.assert(this.field.forceRerender.calledOnce);
});
test('No Validators, Not Dirty', function() {
this.field.doValueUpdate_ = function(newValue) {
this.value_ = newValue;
this.isDirty_ = false;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
chai.assert(this.field.forceRerender.notCalled);
});
test('Class Validator Returns Invalid, Not Dirty (Default)', function() {
this.field.doClassValidation_ = function() {
return null;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.calledOnce);
chai.assert(this.field.doValueUpdate_.notCalled);
chai.assert(this.field.forceRerender.notCalled);
});
test('Class Validator Returns Invalid, Dirty', function() {
this.field.doClassValidation_ = function() {
return null;
};
this.field.doValueInvalid_ = function() {
this.isDirty_ = true;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.calledOnce);
chai.assert(this.field.doValueUpdate_.notCalled);
chai.assert(this.field.forceRerender.calledOnce);
});
test('Class Validator Returns Valid, Not Dirty', function() {
this.field.doClassValidation_ = function(newValue) {
return newValue;
};
this.field.doValueUpdate_ = function() {
this.isDirty_ = false;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
chai.assert(this.field.forceRerender.notCalled);
});
test('Class Validator Returns Valid, Dirty (Default)', function() {
this.field.doClassValidation_ = function(newValue) {
return newValue;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
chai.assert(this.field.forceRerender.calledOnce);
});
test('Local Validator Returns Invalid, Not Dirty (Default)', function() {
this.field.setValidator(function() {
return null;
});
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.calledOnce);
chai.assert(this.field.doValueUpdate_.notCalled);
chai.assert(this.field.forceRerender.notCalled);
});
test('Local Validator Returns Invalid, Dirty', function() {
this.field.setValidator(function() {
return null;
});
this.field.doValueInvalid_ = function() {
this.isDirty_ = true;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.calledOnce);
chai.assert(this.field.doValueUpdate_.notCalled);
chai.assert(this.field.forceRerender.calledOnce);
});
test('Local Validator Returns Valid, Not Dirty', function() {
this.field.setValidator(function(newValue) {
return newValue;
});
this.field.doValueUpdate_ = function() {
this.isDirty_ = false;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
chai.assert(this.field.forceRerender.notCalled);
});
test('Local Validator Returns Valid, Dirty (Default)', function() {
this.field.setValidator(function(newValue) {
return newValue;
});
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
chai.assert(this.field.forceRerender.calledOnce);
});
test('New Value Matches Old Value', function() {
this.field.setValue('value');
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.notCalled);
chai.assert(this.field.forceRerender.notCalled);
});
test('New Value (Class)Validates to Old Value', function() {
this.field.setValue('value');
this.field.doClassValidation_ = function() {
return 'value';
};
addSpies(this.field);
this.field.setValue('notValue');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.notCalled);
chai.assert(this.field.forceRerender.notCalled);
});
test('New Value (Local)Validates to Old Value', function() {
this.field.setValue('value');
this.field.setValidator(function() {
return 'value';
});
addSpies(this.field);
this.field.setValue('notValue');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.notCalled);
chai.assert(this.field.forceRerender.notCalled);
});
test('New Value (Class)Validates to not Old Value', function() {
this.field.setValue('value');
this.field.doClassValidation_ = function() {
return 'notValue';
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
});
test('New Value (Local)Validates to not Old Value', function() {
this.field.setValue('value');
this.field.setValidator(function() {
return 'notValue';
});
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
});
test('Class Validator Returns Null', function() {
this.field.doClassValidation_ = function() {
return null;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.calledOnce);
chai.assert(this.field.doValueUpdate_.notCalled);
});
test('Class Validator Returns Same', function() {
this.field.doClassValidation_ = function(newValue) {
return newValue;
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
});
test('Class Validator Returns Different', function() {
this.field.doClassValidation_ = function() {
return 'differentValue';
};
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
});
test('Class Validator Returns Undefined', function() {
this.field.doClassValidation_ = function() {};
addSpies(this.field);
this.field.setValue('value');
chai.assert.equal(this.field.getValue(), 'value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
});
test('Local Validator Returns Null', function() {
this.field.setValidator(function() {
return null;
});
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.calledOnce);
chai.assert(this.field.doValueUpdate_.notCalled);
});
test('Local Validator Returns Same', function() {
this.field.setValidator(function(newValue) {
return newValue;
});
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
});
test('Local Validator Returns Different', function() {
this.field.setValidator(function() {
return 'differentValue';
});
addSpies(this.field);
this.field.setValue('value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
});
test('Local Validator Returns Undefined', function() {
this.field.setValidator(function() {});
addSpies(this.field);
this.field.setValue('value');
chai.assert.equal(this.field.getValue(), 'value');
chai.assert(this.field.doValueInvalid_.notCalled);
chai.assert(this.field.doValueUpdate_.calledOnce);
});
});
});

View File

@@ -173,6 +173,10 @@ suite ('Text Input Fields', function() {
Blockly.FieldTextInput.htmlInput_.oldValue_ = 'value';
Blockly.FieldTextInput.htmlInput_.untypedDefaultValue_ = 'value';
});
teardown(function() {
this.textInputField.setValidator(null);
Blockly.FieldTextInput.htmlInput_ = null;
});
suite('Null Validator', function() {
setup(function() {
this.textInputField.setValidator(function() {
@@ -209,5 +213,21 @@ suite ('Text Input Fields', function() {
assertValue(this.textInputField, 'bbb');
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.textInputField.setValidator(function() {});
});
test('When Editing', function() {
this.textInputField.isBeingEdited_ = true;
Blockly.FieldTextInput.htmlInput_.value = 'newValue';
this.textInputField.onHtmlInputChange_(null);
assertValue(this.textInputField, 'newValue');
this.textInputField.isBeingEdited_ = false;
});
test('When Not Editing', function() {
this.textInputField.setValue('newValue');
assertValue(this.textInputField, 'newValue');
});
});
});
});

View File

@@ -136,8 +136,11 @@ suite('Variable Fields', function() {
test('Undefined', function() {
var variableField = createAndInitFieldConstructor(
this.workspace, 'name1');
var stub = sinon.stub(console, 'warn');
variableField.setValue(undefined);
assertValue(variableField, 'name1');
chai.assert(stub.calledOnce);
stub.restore();
});
test('New Variable ID', function() {
this.workspace.createVariable('name2', null, 'id2');
@@ -155,8 +158,11 @@ suite('Variable Fields', function() {
test('Variable Does not Exist', function() {
var variableField = createAndInitFieldConstructor(
this.workspace, 'name1');
var stub = sinon.stub(console, 'warn');
variableField.setValue('id1');
assertValue(variableField, 'name1');
chai.assert(stub.calledOnce);
stub.restore();
});
});
suite('Validators', function() {
@@ -166,6 +172,9 @@ suite('Variable Fields', function() {
this.workspace.createVariable('name3', null, 'id3');
this.variableField = createAndInitFieldConstructor(this.workspace, 'name1');
});
teardown(function() {
this.variableField.setValidator(null);
});
suite('Null Validator', function() {
setup(function() {
this.variableField.setValidator(function() {
@@ -190,6 +199,15 @@ suite('Variable Fields', function() {
assertValue(this.variableField, 'name2', 'id2');
});
});
suite('Returns Undefined Validator', function() {
setup(function() {
this.variableField.setValidator(function() {});
});
test('New Value', function() {
this.variableField.setValue('id2');
assertValue(this.variableField, 'name2', 'id2');
});
});
});
suite('Get variable types', function() {
setup(function() {