From 615235434820188c48fb27a0a242a9f03295bfc2 Mon Sep 17 00:00:00 2001 From: Monica Kozbial Date: Fri, 21 Aug 2020 14:45:02 -0700 Subject: [PATCH] Check contents of warnings messages in tests. (#4212) * Check contents of warnings messages in tests. --- tests/mocha/.eslintrc.json | 2 ++ tests/mocha/json_test.js | 21 +++++++-------------- tests/mocha/test_helpers.js | 25 +++++++++++++++++++++++++ tests/mocha/workspace_test.js | 14 ++++---------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/tests/mocha/.eslintrc.json b/tests/mocha/.eslintrc.json index bb10deb57..cfcc4d939 100644 --- a/tests/mocha/.eslintrc.json +++ b/tests/mocha/.eslintrc.json @@ -15,6 +15,8 @@ "assertNthCallEventArgEquals": true, "assertSingleDeprecationWarningCall": true, "assertVariableValues": true, + "assertNoWarnings": true, + "assertWarnings": true, "captureWarnings": true, "createDeprecationWarningStub": true, "createRenderedBlock": true, diff --git a/tests/mocha/json_test.js b/tests/mocha/json_test.js index acdf9af7c..8943ff7b8 100644 --- a/tests/mocha/json_test.js +++ b/tests/mocha/json_test.js @@ -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() { diff --git a/tests/mocha/test_helpers.js b/tests/mocha/test_helpers.js index 922f13b14..d28d60ee7 100644 --- a/tests/mocha/test_helpers.js +++ b/tests/mocha/test_helpers.js @@ -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} 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. diff --git a/tests/mocha/workspace_test.js b/tests/mocha/workspace_test.js index 0c3d47907..c215dd8a4 100644 --- a/tests/mocha/workspace_test.js +++ b/tests/mocha/workspace_test.js @@ -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');