Check contents of warnings messages in tests. (#4212)

* Check contents of warnings messages in tests.
This commit is contained in:
Monica Kozbial
2020-08-21 14:45:02 -07:00
committed by GitHub
parent 1b9af5413c
commit 6152354348
4 changed files with 38 additions and 24 deletions

View File

@@ -15,6 +15,8 @@
"assertNthCallEventArgEquals": true,
"assertSingleDeprecationWarningCall": true,
"assertVariableValues": true,
"assertNoWarnings": true,
"assertWarnings": true,
"captureWarnings": true,
"createDeprecationWarningStub": true,
"createRenderedBlock": true,

View File

@@ -29,7 +29,7 @@ suite('JSON Block Definitions', function() {
this.blockTypes_.push(BLOCK_TYPE);
var workspace = this.workspace_;
var block;
var warnings = captureWarnings(function() {
assertNoWarnings(() => {
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE
}]);
@@ -38,8 +38,6 @@ suite('JSON Block Definitions', function() {
chai.assert.isNotNull(block);
chai.assert.equal(BLOCK_TYPE, block.type);
chai.assert.equal(warnings.length, 0,
'Expecting no warnings when defining and creating a simple block.');
});
test('Null or undefined type id', function() {
@@ -52,21 +50,18 @@ suite('JSON Block Definitions', function() {
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]);
var blockTypeCount = Object.keys(Blockly.Blocks).length;
var warnings = captureWarnings(function() {
assertWarnings(() => {
Blockly.defineBlocksWithJsonArray([
{"type": BLOCK_TYPE1},
{"type": undefined},
{"type": null},
{"type": BLOCK_TYPE2}]);
});
}, [/missing a type attribute/, /missing a type attribute/]);
chai.assert.isNotNull(Blockly.Blocks[BLOCK_TYPE1],
'Block before bad blocks should be defined.');
chai.assert.isNotNull(Blockly.Blocks[BLOCK_TYPE2],
'Block after bad blocks should be defined.');
chai.assert.equal(Object.keys(Blockly.Blocks).length, blockTypeCount + 2);
chai.assert.equal(warnings.length, 2,
'Expecting 2 warnings, one for each bad block.');
});
test('Null item', function() {
@@ -79,7 +74,7 @@ suite('JSON Block Definitions', function() {
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]);
var blockTypeCount = Object.keys(Blockly.Blocks).length;
var warnings = captureWarnings(function() {
assertWarnings(() => {
Blockly.defineBlocksWithJsonArray([
{
"type": BLOCK_TYPE1,
@@ -90,13 +85,12 @@ suite('JSON Block Definitions', function() {
"type": BLOCK_TYPE2,
"message0": 'after'
}]);
});
}, /is null/);
chai.assert.isNotNull(Blockly.Blocks[BLOCK_TYPE1],
'Block before null in array should be defined.');
chai.assert.isNotNull(Blockly.Blocks[BLOCK_TYPE2],
'Block after null in array should be defined.');
chai.assert.equal(Object.keys(Blockly.Blocks).length, blockTypeCount + 2);
chai.assert.equal(warnings.length, 1, 'Expected 1 warning for the bad block.');
});
test('Undefined item', function() {
@@ -108,7 +102,7 @@ suite('JSON Block Definitions', function() {
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE1]);
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]);
var blockTypeCount = Object.keys(Blockly.Blocks).length;
var warnings = captureWarnings(function() {
assertWarnings(() => {
Blockly.defineBlocksWithJsonArray([
{
"type": BLOCK_TYPE1,
@@ -119,13 +113,12 @@ suite('JSON Block Definitions', function() {
"type": BLOCK_TYPE2,
"message0": 'after'
}]);
});
}, /is undefined/);
chai.assert.isNotNull(Blockly.Blocks[BLOCK_TYPE1],
'Block before undefined in array should be defined.');
chai.assert.isNotNull(Blockly.Blocks[BLOCK_TYPE2],
'Block after undefined in array should be defined.');
chai.assert.equal(Object.keys(Blockly.Blocks).length, blockTypeCount + 2);
chai.assert.equal( warnings.length, 1, 'Expected 1 warning for the bad block.');
});
test('message0 creates input', function() {

View File

@@ -39,6 +39,31 @@ function captureWarnings(innerFunc) {
return msgs;
}
/**
* Asserts that the given function logs the provided warning messages.
* @param {function} innerFunc The function to call.
* @param {Array<!RegExp>|!RegExp} messages A list of regex for the expected
* messages (in the expected order).
*/
function assertWarnings(innerFunc, messages) {
if (!Array.isArray(messages)) {
messages = [messages];
}
var warnings = captureWarnings(innerFunc);
chai.assert.lengthOf(warnings, messages.length);
messages.forEach((message, i) => {
chai.assert.match(warnings[i], message);
});
}
/**
* Asserts that the given function logs no warning messages.
* @param {function} innerFunc The function to call.
*/
function assertNoWarnings(innerFunc) {
assertWarnings(innerFunc, []);
}
/**
* Stubs Blockly.utils.deprecation.warn call.
* @return {!SinonStub} The created stub.

View File

@@ -778,12 +778,9 @@ function testAWorkspace() {
this.workspace.createVariable('name1', 'type1', 'id1');
this.workspace.deleteVariableById('id1');
var workspace = this.workspace;
var warnings = captureWarnings(function() {
assertWarnings(() => {
workspace.deleteVariableById('id1');
});
chai.assert.equal(warnings.length, 1,
'Expected 1 warning for second deleteVariableById call.');
}, [/Can't delete non-existent variable/]);
// Check the undoStack only recorded one delete event.
var undoStack = this.workspace.undoStack_;
chai.assert.equal(undoStack[undoStack.length - 1].type, 'var_delete');
@@ -807,12 +804,9 @@ function testAWorkspace() {
createVarBlocksNoEvents(this.workspace, ['id1']);
this.workspace.deleteVariableById('id1');
var workspace = this.workspace;
var warnings = captureWarnings(function() {
assertWarnings(() => {
workspace.deleteVariableById('id1');
});
chai.assert.equal(warnings.length, 1,
'Expected 1 warning for second deleteVariableById call.');
}, [/Can't delete non-existent variable/]);
// Check the undoStack only recorded one delete event.
var undoStack = this.workspace.undoStack_;
chai.assert.equal(undoStack[undoStack.length - 1].type, 'var_delete');