From 376bed8c466c7ef6bc7cf5e92ce2ca869c7fc711 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Tue, 27 Nov 2018 12:56:53 -0800 Subject: [PATCH 1/8] First pass at creating mocha tests --- package.json | 3 +- tests/mocha/index.html | 26 ++++ tests/mocha/mocha.opts | 3 + tests/mocha/utils_test.js | 267 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 tests/mocha/index.html create mode 100644 tests/mocha/mocha.opts create mode 100644 tests/mocha/utils_test.js diff --git a/package.json b/package.json index 469be7a6c..abda73acb 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "scripts": { "prepare": "gulp blockly_javascript_en", "lint": "eslint .", - "test": "tests/run_all_tests.sh" + "test": "mocha --opts tests/mocha/mocha.opts tests/mocha/*.js" }, "license": "Apache-2.0", "private": true, @@ -34,6 +34,7 @@ "gulp-series": "^1.0.2", "gulp-shell": "^0.6.5", "jshint": "^2.9.7", + "mocha": "^5.2.0", "webdriverio": "^4.14.1" }, "jshintConfig": { diff --git a/tests/mocha/index.html b/tests/mocha/index.html new file mode 100644 index 000000000..9b21c559e --- /dev/null +++ b/tests/mocha/index.html @@ -0,0 +1,26 @@ + + + + + Mocha Tests for Blockly + + + + + + +
+ + + + + + + + diff --git a/tests/mocha/mocha.opts b/tests/mocha/mocha.opts new file mode 100644 index 000000000..b0986b141 --- /dev/null +++ b/tests/mocha/mocha.opts @@ -0,0 +1,3 @@ +--ui tdd +--file ../blockly_uncompressed.js +--reporter landing diff --git a/tests/mocha/utils_test.js b/tests/mocha/utils_test.js new file mode 100644 index 000000000..7eeaae09d --- /dev/null +++ b/tests/mocha/utils_test.js @@ -0,0 +1,267 @@ +// var assert = require('assert'); +// describe('Array', function() { +// describe('#indexOf()', function() { +// it('should return -1 when the value is not present', function() { +// assert.equal([1,2,3].indexOf(4), -1); +// }); +// }); +// }); + + +suite('Utils', function() { + test('genUid', function() { + var uuids = {}; + chai.assert.equal([1,2,3].indexOf(4), -1); + for (var i = 0; i < 1000; i++) { + var uuid = Blockly.utils.genUid(); + chai.assert.isTrue(uuid in uuids, 'UUID different: ' + uuid); + uuids[uuid] = true; + } + }); + + + // function test_addClass() { + // var p = document.createElement('p'); + // Blockly.utils.addClass(p, 'one'); + // assertEquals('Adding "one"', 'one', p.className); + // Blockly.utils.addClass(p, 'one'); + // assertEquals('Adding duplicate "one"', 'one', p.className); + // Blockly.utils.addClass(p, 'two'); + // assertEquals('Adding "two"', 'one two', p.className); + // Blockly.utils.addClass(p, 'two'); + // assertEquals('Adding duplicate "two"', 'one two', p.className); + // Blockly.utils.addClass(p, 'three'); + // assertEquals('Adding "three"', 'one two three', p.className); + // } + + // function test_hasClass() { + // var p = document.createElement('p'); + // p.className = ' one three two three '; + // assertTrue('Has "one"', Blockly.utils.hasClass(p, 'one')); + // assertTrue('Has "two"', Blockly.utils.hasClass(p, 'two')); + // assertTrue('Has "three"', Blockly.utils.hasClass(p, 'three')); + // assertFalse('Has no "four"', Blockly.utils.hasClass(p, 'four')); + // assertFalse('Has no "t"', Blockly.utils.hasClass(p, 't')); + // } + + // function test_removeClass() { + // var p = document.createElement('p'); + // p.className = ' one three two three '; + // Blockly.utils.removeClass(p, 'two'); + // assertEquals('Removing "two"', 'one three three', p.className); + // Blockly.utils.removeClass(p, 'four'); + // assertEquals('Removing "four"', 'one three three', p.className); + // Blockly.utils.removeClass(p, 'three'); + // assertEquals('Removing "three"', 'one', p.className); + // Blockly.utils.removeClass(p, 'ne'); + // assertEquals('Removing "ne"', 'one', p.className); + // Blockly.utils.removeClass(p, 'one'); + // assertEquals('Removing "one"', '', p.className); + // Blockly.utils.removeClass(p, 'zero'); + // assertEquals('Removing "zero"', '', p.className); + // } + + // function test_shortestStringLength() { + // var len = Blockly.utils.shortestStringLength('one,two,three,four,five'.split(',')); + // assertEquals('Length of "one"', 3, len); + // len = Blockly.utils.shortestStringLength('one,two,three,four,five,'.split(',')); + // assertEquals('Length of ""', 0, len); + // len = Blockly.utils.shortestStringLength(['Hello World']); + // assertEquals('List of one', 11, len); + // len = Blockly.utils.shortestStringLength([]); + // assertEquals('Empty list', 0, len); + // } + + // function test_commonWordPrefix() { + // var len = Blockly.utils.commonWordPrefix('one,two,three,four,five'.split(',')); + // assertEquals('No prefix', 0, len); + // len = Blockly.utils.commonWordPrefix('Xone,Xtwo,Xthree,Xfour,Xfive'.split(',')); + // assertEquals('No word prefix', 0, len); + // len = Blockly.utils.commonWordPrefix('abc de,abc de,abc de,abc de'.split(',')); + // assertEquals('Full equality', 6, len); + // len = Blockly.utils.commonWordPrefix('abc deX,abc deY'.split(',')); + // assertEquals('One word prefix', 4, len); + // len = Blockly.utils.commonWordPrefix('abc de,abc deY'.split(',')); + // assertEquals('Overflow no', 4, len); + // len = Blockly.utils.commonWordPrefix('abc de,abc de Y'.split(',')); + // assertEquals('Overflow yes', 6, len); + // len = Blockly.utils.commonWordPrefix(['Hello World']); + // assertEquals('List of one', 11, len); + // len = Blockly.utils.commonWordPrefix([]); + // assertEquals('Empty list', 0, len); + // len = Blockly.utils.commonWordPrefix('turn left,turn right'.split(',')); + // assertEquals('No prefix due to &nbsp;', 0, len); + // len = Blockly.utils.commonWordPrefix('turn\u00A0left,turn\u00A0right'.split(',')); + // assertEquals('No prefix due to \\u00A0', 0, len); + // } + + // function test_commonWordSuffix() { + // var len = Blockly.utils.commonWordSuffix('one,two,three,four,five'.split(',')); + // assertEquals('No prefix', 0, len); + // len = Blockly.utils.commonWordSuffix('oneX,twoX,threeX,fourX,fiveX'.split(',')); + // assertEquals('No word prefix', 0, len); + // len = Blockly.utils.commonWordSuffix('abc de,abc de,abc de,abc de'.split(',')); + // assertEquals('Full equality', 6, len); + // len = Blockly.utils.commonWordSuffix('Xabc de,Yabc de'.split(',')); + // assertEquals('One word prefix', 3, len); + // len = Blockly.utils.commonWordSuffix('abc de,Yabc de'.split(',')); + // assertEquals('Overflow no', 3, len); + // len = Blockly.utils.commonWordSuffix('abc de,Y abc de'.split(',')); + // assertEquals('Overflow yes', 6, len); + // len = Blockly.utils.commonWordSuffix(['Hello World']); + // assertEquals('List of one', 11, len); + // len = Blockly.utils.commonWordSuffix([]); + // assertEquals('Empty list', 0, len); + // } + + // function test_tokenizeInterpolation() { + // var tokens = Blockly.utils.tokenizeInterpolation(''); + // assertArrayEquals('Null interpolation', [], tokens); + + // tokens = Blockly.utils.tokenizeInterpolation('Hello'); + // assertArrayEquals('No interpolation', ['Hello'], tokens); + + // tokens = Blockly.utils.tokenizeInterpolation('Hello%World'); + // assertArrayEquals('Unescaped %.', ['Hello%World'], tokens); + + // tokens = Blockly.utils.tokenizeInterpolation('Hello%%World'); + // assertArrayEquals('Escaped %.', ['Hello%World'], tokens); + + // tokens = Blockly.utils.tokenizeInterpolation('Hello %1 World'); + // assertArrayEquals('Interpolation.', ['Hello ', 1, ' World'], tokens); + + // tokens = Blockly.utils.tokenizeInterpolation('%123Hello%456World%789'); + // assertArrayEquals('Interpolations.', [123, 'Hello', 456, 'World', 789], tokens); + + // tokens = Blockly.utils.tokenizeInterpolation('%%%x%%0%00%01%'); + // assertArrayEquals('Torture interpolations.', ['%%x%0', 0, 1, '%'], tokens); + + // Blockly.Msg = Blockly.Msg || {}; + + // Blockly.Msg.STRING_REF = 'test string'; + // tokens = Blockly.utils.tokenizeInterpolation('%{bky_string_ref}'); + // assertArrayEquals('String table reference, lowercase', ['test string'], tokens); + // tokens = Blockly.utils.tokenizeInterpolation('%{BKY_STRING_REF}'); + // assertArrayEquals('String table reference, uppercase', ['test string'], tokens); + + // Blockly.Msg.WITH_PARAM = 'before %1 after'; + // tokens = Blockly.utils.tokenizeInterpolation('%{bky_with_param}'); + // assertArrayEquals('String table reference, with parameter', ['before ', 1, ' after'], tokens); + + // Blockly.Msg.RECURSE = 'before %{bky_string_ref} after'; + // tokens = Blockly.utils.tokenizeInterpolation('%{bky_recurse}'); + // assertArrayEquals('String table reference, with subreference', ['before test string after'], tokens); + + // // Error cases... + // tokens = Blockly.utils.tokenizeInterpolation('%{bky_undefined}'); + // assertArrayEquals('Undefined string table reference', ['%{bky_undefined}'], tokens); + + // Blockly.Msg['1'] = 'Will not match'; + // tokens = Blockly.utils.tokenizeInterpolation('before %{1} after'); + // assertArrayEquals('Invalid initial digit in string table reference', ['before %{1} after'], tokens); + + // Blockly.Msg['TWO WORDS'] = 'Will not match'; + // tokens = Blockly.utils.tokenizeInterpolation('before %{two words} after'); + // assertArrayEquals('Invalid character in string table reference: space', ['before %{two words} after'], tokens); + + // Blockly.Msg['TWO-WORDS'] = 'Will not match'; + // tokens = Blockly.utils.tokenizeInterpolation('before %{two-words} after'); + // assertArrayEquals('Invalid character in string table reference: dash', ['before %{two-words} after'], tokens); + + // Blockly.Msg['TWO.WORDS'] = 'Will not match'; + // tokens = Blockly.utils.tokenizeInterpolation('before %{two.words} after'); + // assertArrayEquals('Invalid character in string table reference: period', ['before %{two.words} after'], tokens); + + // Blockly.Msg['AB&C'] = 'Will not match'; + // tokens = Blockly.utils.tokenizeInterpolation('before %{ab&c} after'); + // assertArrayEquals('Invalid character in string table reference: &', ['before %{ab&c} after'], tokens); + + // Blockly.Msg['UNCLOSED'] = 'Will not match'; + // tokens = Blockly.utils.tokenizeInterpolation('before %{unclosed'); + // assertArrayEquals('String table reference, with parameter', ['before %{unclosed'], tokens); + // } + + // function test_replaceMessageReferences() { + // Blockly.Msg = Blockly.Msg || {}; + // Blockly.Msg.STRING_REF = 'test string'; + // Blockly.Msg.SUBREF = 'subref'; + // Blockly.Msg.STRING_REF_WITH_ARG = 'test %1 string'; + // Blockly.Msg.STRING_REF_WITH_SUBREF = 'test %{bky_subref} string'; + + // var resultString = Blockly.utils.replaceMessageReferences(''); + // assertEquals('Empty string produces empty string', '', resultString); + + // resultString = Blockly.utils.replaceMessageReferences('%%'); + // assertEquals('Escaped %', '%', resultString); + // resultString = Blockly.utils.replaceMessageReferences('%%{bky_string_ref}'); + // assertEquals('Escaped %', '%{bky_string_ref}', resultString); + + // resultString = Blockly.utils.replaceMessageReferences('%a'); + // assertEquals('Unrecognized % escape code treated as literal', '%a', resultString); + + // resultString = Blockly.utils.replaceMessageReferences('%1'); + // assertEquals('Interpolation tokens ignored.', '%1', resultString); + // resultString = Blockly.utils.replaceMessageReferences('%1 %2'); + // assertEquals('Interpolation tokens ignored.', '%1 %2', resultString); + // resultString = Blockly.utils.replaceMessageReferences('before %1 after'); + // assertEquals('Interpolation tokens ignored.', 'before %1 after', resultString); + + // // Blockly.Msg.STRING_REF cases: + // resultString = Blockly.utils.replaceMessageReferences('%{bky_string_ref}'); + // assertEquals('Message ref dereferenced.', 'test string', resultString); + // resultString = Blockly.utils.replaceMessageReferences('before %{bky_string_ref} after'); + // assertEquals('Message ref dereferenced.', 'before test string after', resultString); + + // // Blockly.Msg.STRING_REF_WITH_ARG cases: + // resultString = Blockly.utils.replaceMessageReferences('%{bky_string_ref_with_arg}'); + // assertEquals('Message ref dereferenced with argument preserved.', 'test %1 string', resultString); + // resultString = Blockly.utils.replaceMessageReferences('before %{bky_string_ref_with_arg} after'); + // assertEquals('Message ref dereferenced with argument preserved.', 'before test %1 string after', resultString); + + // // Blockly.Msg.STRING_REF_WITH_SUBREF cases: + // resultString = Blockly.utils.replaceMessageReferences('%{bky_string_ref_with_subref}'); + // assertEquals('Message ref and subref dereferenced.', 'test subref string', resultString); + // resultString = Blockly.utils.replaceMessageReferences('before %{bky_string_ref_with_subref} after'); + // assertEquals('Message ref and subref dereferenced.', 'before test subref string after', resultString); + // } + + // function test_startsWith() { + // assertEquals('Does not start with', false, Blockly.utils.startsWith('123', '2')); + // assertEquals('Start with', true, Blockly.utils.startsWith('123', '12')); + // assertEquals('Start with empty string 1', true, Blockly.utils.startsWith('123', '')); + // assertEquals('Start with empty string 2', true, Blockly.utils.startsWith('', '')); + // } + + // function test_arrayRemove() { + // var arr = [1, 2, 3, 2]; + // assertEquals('Remove Not found', false, Blockly.utils.arrayRemove(arr, 0)); + // assertEquals('Remove Not found result', '1,2,3,2', arr.join(',')); + // assertEquals('Remove item', true, Blockly.utils.arrayRemove(arr, 2)); + // assertEquals('Remove item result', '1,3,2', arr.join(',')); + // assertEquals('Remove item again', true, Blockly.utils.arrayRemove(arr, 2)); + // assertEquals('Remove item again result', '1,3', arr.join(',')); + // } + + // function test_toRadians() { + // var quarter = Math.PI / 2; + // assertEquals('-90', -quarter, Blockly.utils.toRadians(-90)); + // assertEquals('0', 0, Blockly.utils.toRadians(0)); + // assertEquals('90', quarter, Blockly.utils.toRadians(90)); + // assertEquals('180', 2 * quarter, Blockly.utils.toRadians(180)); + // assertEquals('270', 3 * quarter, Blockly.utils.toRadians(270)); + // assertEquals('360', 4 * quarter, Blockly.utils.toRadians(360)); + // assertEquals('450', 5 * quarter, Blockly.utils.toRadians(360 + 90)); + // } + + // function test_toDegrees() { + // var quarter = Math.PI / 2; + // assertEquals('-90', -90, Blockly.utils.toDegrees(-quarter)); + // assertEquals('0', 0, Blockly.utils.toDegrees(0)); + // assertEquals('90', 90, Blockly.utils.toDegrees(quarter)); + // assertEquals('180', 180, Blockly.utils.toDegrees(2 * quarter)); + // assertEquals('270', 270, Blockly.utils.toDegrees(3 * quarter)); + // assertEquals('360', 360, Blockly.utils.toDegrees(4 * quarter)); + // assertEquals('450', 360 + 90, Blockly.utils.toDegrees(5 * quarter)); + // } + +}); From e27f58ada7b088391e44fb8e90c2d3bce11b0988 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Fri, 7 Dec 2018 16:18:14 -0800 Subject: [PATCH 2/8] Add jsunit->chai assert converters, and block tests --- tests/mocha/block_test.js | 182 ++++++++++++++++++++++++++++++++++++ tests/mocha/index.html | 2 + tests/mocha/test_helpers.js | 73 +++++++++++++++ tests/mocha/utils_test.js | 155 +++++++++++++++--------------- 4 files changed, 335 insertions(+), 77 deletions(-) create mode 100644 tests/mocha/block_test.js create mode 100644 tests/mocha/test_helpers.js diff --git a/tests/mocha/block_test.js b/tests/mocha/block_test.js new file mode 100644 index 000000000..6400bccd2 --- /dev/null +++ b/tests/mocha/block_test.js @@ -0,0 +1,182 @@ + +// Declare some globals to make eslint happier. +// TODO: make an eslint config that applies to this directory and put this +// configuration in that file, instead of inline. +/* global suite, test, chai, setup, teardown */ + +/* global assertFalse, assertTrue, assertNull, assertEquals */ + +suite('Blocks', function() { + + suite('Unplug', function() { + function assertUnpluggedNoheal(blocks) { + // A has nothing connected to it. + assertEquals(0, blocks.A.getChildren().length); + // B and C are still connected. + assertEquals(blocks.B, blocks.C.getParent()); + // B is the top of its stack. + assertNull(blocks.B.getParent()); + } + + function assertUnpluggedHealed(blocks) { + // A and C are connected. + assertEquals(1, blocks.A.getChildren().length); + assertEquals(blocks.A, blocks.C.getParent()); + // B has nothing connected to it. + assertEquals(0, blocks.B.getChildren().length); + // B is the top of its stack. + assertNull(blocks.B.getParent()); + } + + setup(function() { + Blockly.defineBlocksWithJsonArray([{ + "type": "stack_block", + "message0": "", + "previousStatement": null, + "nextStatement": null + }, + { + "type": "row_block", + "message0": "%1", + "args0": [ + { + "type": "input_value", + "name": "INPUT" + } + ], + "output": null + }]); + + this.workspace = new Blockly.Workspace(); + }); + + teardown(function() { + delete Blockly.Blocks['stack_block']; + delete Blockly.Blocks['row_block']; + + this.workspace.dispose(); + }); + + suite('Row', function() { + setup(function() { + var blockA = this.workspace.newBlock('row_block'); + var blockB = this.workspace.newBlock('row_block'); + var blockC = this.workspace.newBlock('row_block'); + + blockA.inputList[0].connection.connect(blockB.outputConnection); + blockB.inputList[0].connection.connect(blockC.outputConnection); + + assertEquals(blockB, blockC.getParent()); + + this.blocks = { + A: blockA, + B: blockB, + C: blockC + }; + }); + + test('Don\'t heal', function() { + this.blocks.B.unplug(false); + assertUnpluggedNoheal(this.blocks); + }); + + test('Heal', function() { + this.blocks.B.unplug(true); + // Each block has only one input, and the types work. + assertUnpluggedHealed(this.blocks); + }); + + test('Heal with bad checks', function() { + var blocks = this.blocks; + + // A and C can't connect, but both can connect to B. + blocks.A.inputList[0].connection.setCheck('type1'); + blocks.C.outputConnection.setCheck('type2'); + + // Each block has only one input, but the types don't work. + blocks.B.unplug(true); + assertUnpluggedNoheal(blocks); + }); + + test('Parent has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to parent + blocks.A.appendValueInput("INPUT").setCheck(null); + blocks.B.unplug(true); + assertUnpluggedHealed(blocks); + }); + + test('Middle block has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to middle block + blocks.B.appendValueInput("INPUT").setCheck(null); + blocks.B.unplug(true); + assertUnpluggedNoheal(blocks); + }); + + test('Child block has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to child block + blocks.C.appendValueInput("INPUT").setCheck(null); + // Child block input count doesn't matter. + blocks.B.unplug(true); + assertUnpluggedHealed(blocks); + }); + }); + + + suite('Stack', function() { + setup(function() { + var blockA = this.workspace.newBlock('stack_block'); + var blockB = this.workspace.newBlock('stack_block'); + var blockC = this.workspace.newBlock('stack_block'); + + blockA.nextConnection.connect(blockB.previousConnection); + blockB.nextConnection.connect(blockC.previousConnection); + + assertEquals(blockB, blockC.getParent()); + + this.blocks = { + A: blockA, + B: blockB, + C: blockC + }; + }); + + test('Don\'t heal', function() { + this.blocks.B.unplug(); + assertUnpluggedNoheal(this.blocks); + }); + + test('Heal', function() { + this.blocks.B.unplug(true); + assertUnpluggedHealed(this.blocks); + }); + + test('Heal with bad checks', function() { + var blocks = this.blocks; + // A and C can't connect, but both can connect to B. + blocks.A.nextConnection.setCheck('type1'); + blocks.C.previousConnection.setCheck('type2'); + + // The types don't work. + blocks.B.unplug(true); + + // Stack blocks unplug before checking whether the types match. + // TODO (#1994): Check types before unplugging. + // A has nothing connected to it. + assertEquals(0, blocks.A.getChildren().length); + // B has nothing connected to it. + assertEquals(0, blocks.B.getChildren().length); + // C has nothing connected to it. + assertEquals(0, blocks.C.getChildren().length); + // A is the top of its stack. + assertNull(blocks.A.getParent()); + // B is the top of its stack. + assertNull(blocks.B.getParent()); + // C is the top of its stack. + assertNull(blocks.C.getParent()); + }); + }); + }); +}); diff --git a/tests/mocha/index.html b/tests/mocha/index.html index 9b21c559e..c4258eebd 100644 --- a/tests/mocha/index.html +++ b/tests/mocha/index.html @@ -17,6 +17,8 @@ ui: 'tdd' }); + + + + + - +