Skip to content

Commit

Permalink
fix: move test helpers from samples into core (google#5969)
Browse files Browse the repository at this point in the history
* fix: move core test helpers into new directory

* fix: add test helpers to core and convert to goog modules

* fix: change tests to use local helpers

* fix: change local tests to use chai asserts

* fix: skip field tests in serializer b/c test blocks are unavailable

* fix: rename some helper files

* fix: rename some helper modules

* fix: split block helpers into code gen and serialization

* fix: split block defs into new helper file

* fix: split warning helpers into new file

* fix: split user input helpers into new file

* fix: split event helpers into a new file

* fix: split variable helper into new file

* fix: move remaining test helpers to new setup-teardown file

* fix: rename setup and teardown module

* fix: cleanup from rebase

* fix: undo accidental rename

* fix: lint?

* fix: bad toolbox definitions namespace

* fix: fixup warning helpers

* fix: remove inclusion of dev-tools in mocha tests

* move to modules, but break mocha

* fix: run mocha as a module

* fix: lint
  • Loading branch information
BeksOmega authored Mar 4, 2022
1 parent 6841ccc commit 2edd228
Show file tree
Hide file tree
Showing 74 changed files with 1,713 additions and 985 deletions.
131 changes: 70 additions & 61 deletions tests/deps.mocha.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions tests/mocha/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
{
"parserOptions": {
"sourceType": "module"
},
"env": {
"browser": true,
"mocha": true
},
"globals": {
"chai": false,
"sinon": false,
"testHelpers": true
"sinon": false
},
"rules": {
"no-unused-vars": ["off"],
// Allow uncommented helper functions in tests.
"require-jsdoc": ["off"],
"prefer-rest-params": ["off"]
"prefer-rest-params": ["off"],
"no-invalid-this": ["off"]
},
"extends": "../../.eslintrc.json"
}
8 changes: 7 additions & 1 deletion tests/mocha/astnode_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
goog.module('Blockly.test.astNode');

const {ASTNode} = goog.require('Blockly.ASTNode');
const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('ASTNode', function() {
console.log('1/a');
setup(function() {
sharedTestSetup.call(this);
Blockly.defineBlocksWithJsonArray([{
Expand Down Expand Up @@ -100,7 +101,9 @@ suite('ASTNode', function() {
});

suite('HelperFunctions', function() {
console.log('2');
test('findNextForInput_', function() {
console.log('3');
const input = this.blocks.statementInput1.inputList[0];
const input2 = this.blocks.statementInput1.inputList[1];
const connection = input.connection;
Expand Down Expand Up @@ -169,6 +172,7 @@ suite('ASTNode', function() {
});

suite('NavigationFunctions', function() {
console.log('b');
setup(function() {
Blockly.defineBlocksWithJsonArray([{
"type": "top_connection",
Expand Down Expand Up @@ -319,7 +323,9 @@ suite('ASTNode', function() {
workspaceTeardown.call(this, this.singleBlockWorkspace);
});

console.log('c');
test('fromPreviousToBlock', function() {
console.log('d');
const prevConnection = this.blocks.statementInput1.previousConnection;
const node = ASTNode.createConnectionNode(prevConnection);
const nextNode = node.next();
Expand Down
3 changes: 2 additions & 1 deletion tests/mocha/block_change_event_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

goog.module('Blockly.test.blockChangeEvent');

const {defineMutatorBlocks, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');
const {defineMutatorBlocks} = goog.require('Blockly.test.helpers.blockDefinitions');


suite('Block Change Event', function() {
Expand Down
3 changes: 2 additions & 1 deletion tests/mocha/block_create_event_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

goog.module('Blockly.test.blockCreateEvent');

const {assertEventFired} = goog.require('Blockly.test.helpers.events');
const eventUtils = goog.require('Blockly.Events.utils');
const {assertEventFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Block Create Event', function() {
Expand Down
6 changes: 4 additions & 2 deletions tests/mocha/block_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

goog.module('Blockly.test.blocks');

const eventUtils = goog.require('Blockly.Events.utils');
const {Blocks} = goog.require('Blockly.blocks');
const {ConnectionType} = goog.require('Blockly.ConnectionType');
const {createDeprecationWarningStub, createRenderedBlock, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers');
const {createDeprecationWarningStub} = goog.require('Blockly.test.helpers.warnings');
const {createRenderedBlock} = goog.require('Blockly.test.helpers.blockDefinitions');
const eventUtils = goog.require('Blockly.Events.utils');
const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Blocks', function() {
Expand Down
3 changes: 2 additions & 1 deletion tests/mocha/comment_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

goog.module('Blockly.test.comments');

const {assertEventFired} = goog.require('Blockly.test.helpers.events');
const eventUtils = goog.require('Blockly.Events.utils');
const {assertEventFired, sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Comments', function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/connection_checker_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
goog.module('Blockly.test.connectionChecker');

const {ConnectionType} = goog.require('Blockly.ConnectionType');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Connection checker', function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/connection_db_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
goog.module('Blockly.test.connectionDb');

const {ConnectionType} = goog.require('Blockly.ConnectionType');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Connection Database', function() {
Expand Down
3 changes: 2 additions & 1 deletion tests/mocha/connection_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

goog.module('Blockly.test.connection');

const {assertSingleDeprecationWarningCall, createDeprecationWarningStub, createGenUidStubWithReturns, defineRowBlock, defineStatementBlock, defineStackBlock, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers');
const {createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');
const {defineRowBlock, defineStatementBlock, defineStackBlock} = goog.require('Blockly.test.helpers.blockDefinitions');


suite('Connection', function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/contextmenu_items_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

goog.module('Blockly.test.contextMenuItem');

const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Context Menu Items', function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/cursor_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

goog.module('Blockly.test.cursor');

const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');
const {ASTNode} = goog.require('Blockly.ASTNode');


Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/dropdowndiv_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

goog.module('Blockly.test.dropdown');

const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('DropDownDiv', function() {
Expand Down
6 changes: 4 additions & 2 deletions tests/mocha/event_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

goog.module('Blockly.test.event');

const eventUtils = goog.require('Blockly.Events.utils');
const {assertEventEquals, assertNthCallEventArgEquals, assertVariableValues, createFireChangeListenerSpy, createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers');
const {ASTNode} = goog.require('Blockly.ASTNode');
const {assertEventEquals, assertNthCallEventArgEquals, createFireChangeListenerSpy} = goog.require('Blockly.test.helpers.events');
const {assertVariableValues} = goog.require('Blockly.test.helpers.variables');
const {createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');
const eventUtils = goog.require('Blockly.Events.utils');
goog.require('Blockly.WorkspaceComment');


Expand Down
2 changes: 1 addition & 1 deletion tests/mocha/extensions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

goog.module('Blockly.test.extensions');

const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Extensions', function() {
Expand Down
24 changes: 13 additions & 11 deletions tests/mocha/field_angle_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

goog.module('Blockly.test.fieldAngle');

const {createTestBlock, defineRowBlock, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers');
const {assertFieldValue, runConstructorSuiteTests, runFromJsonSuiteTests, runSetValueTests} = goog.require('Blockly.test.helpers.fields');
const {createTestBlock, defineRowBlock} = goog.require('Blockly.test.helpers.blockDefinitions');
const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Angle Fields', function() {
Expand Down Expand Up @@ -60,22 +62,22 @@ suite('Angle Fields', function() {
* @param {FieldTemplate} field The field to check.
*/
const assertFieldDefault = function(field) {
testHelpers.assertFieldValue(field, defaultFieldValue);
assertFieldValue(field, defaultFieldValue);
};
/**
* Asserts that the field properties are correct based on the test case.
* @param {!Blockly.FieldAngle} field The field to check.
* @param {!FieldValueTestCase} testCase The test case.
*/
const validTestCaseAssertField = function(field, testCase) {
testHelpers.assertFieldValue(field, testCase.expectedValue);
assertFieldValue(field, testCase.expectedValue);
};

testHelpers.runConstructorSuiteTests(
runConstructorSuiteTests(
Blockly.FieldAngle, validValueTestCases, invalidValueTestCases,
validTestCaseAssertField, assertFieldDefault);

testHelpers.runFromJsonSuiteTests(
runFromJsonSuiteTests(
Blockly.FieldAngle, validValueTestCases, invalidValueTestCases,
validTestCaseAssertField, assertFieldDefault);

Expand All @@ -84,25 +86,25 @@ suite('Angle Fields', function() {
setup(function() {
this.field = new Blockly.FieldAngle();
});
testHelpers.runSetValueTests(
runSetValueTests(
validValueTestCases, invalidValueTestCases, defaultFieldValue);
test('With source block', function() {
this.field.setSourceBlock(createTestBlock());
this.field.setValue(2.5);
testHelpers.assertFieldValue(this.field, 2.5);
assertFieldValue(this.field, 2.5);
});
});
suite('Value -> New Value', function() {
const initialValue = 1;
setup(function() {
this.field = new Blockly.FieldAngle(initialValue);
});
testHelpers.runSetValueTests(
runSetValueTests(
validValueTestCases, invalidValueTestCases, initialValue);
test('With source block', function() {
this.field.setSourceBlock(createTestBlock());
this.field.setValue(2.5);
testHelpers.assertFieldValue(this.field, 2.5);
assertFieldValue(this.field, 2.5);
});
});
});
Expand Down Expand Up @@ -142,12 +144,12 @@ suite('Angle Fields', function() {
this.field.isBeingEdited_ = true;
this.field.htmlInput_.value = String(suiteInfo.value);
this.field.onHtmlInputChange_(null);
testHelpers.assertFieldValue(
assertFieldValue(
this.field, suiteInfo.expectedValue, String(suiteInfo.value));
});
test('When Not Editing', function() {
this.field.setValue(suiteInfo.value);
testHelpers.assertFieldValue(this.field, suiteInfo.expectedValue);
assertFieldValue(this.field, suiteInfo.expectedValue);
});
});
});
Expand Down
18 changes: 10 additions & 8 deletions tests/mocha/field_checkbox_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

goog.module('Blockly.test.fieldCheckbox');

const {defineRowBlock, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers');
const {assertFieldValue, runConstructorSuiteTests, runFromJsonSuiteTests, runSetValueTests} = goog.require('Blockly.test.helpers.fields');
const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');
const {defineRowBlock} = goog.require('Blockly.test.helpers.blockDefinitions');


suite('Checkbox Fields', function() {
Expand Down Expand Up @@ -61,7 +63,7 @@ suite('Checkbox Fields', function() {
* @param {!Blockly.FieldCheckbox} field The field to check.
*/
const assertFieldDefault = function(field) {
testHelpers.assertFieldValue(
assertFieldValue(
field, defaultFieldValue, defaultFieldValue.toLowerCase());
};
/**
Expand All @@ -70,15 +72,15 @@ suite('Checkbox Fields', function() {
* @param {!FieldValueTestCase} testCase The test case.
*/
const validTestCaseAssertField = function(field, testCase) {
testHelpers.assertFieldValue(
assertFieldValue(
field, testCase.expectedValue, testCase.expectedValue.toLowerCase());
};

testHelpers.runConstructorSuiteTests(
runConstructorSuiteTests(
Blockly.FieldCheckbox, validValueTestCases, invalidValueTestCases,
validTestCaseAssertField, assertFieldDefault);

testHelpers.runFromJsonSuiteTests(
runFromJsonSuiteTests(
Blockly.FieldCheckbox, validValueTestCases, invalidValueTestCases,
validTestCaseAssertField, assertFieldDefault);

Expand All @@ -87,14 +89,14 @@ suite('Checkbox Fields', function() {
setup(function() {
this.field = new Blockly.FieldCheckbox('TRUE');
});
testHelpers.runSetValueTests(
runSetValueTests(
validValueTestCases, invalidValueTestCases, 'TRUE', 'true');
});
suite('False -> New Value', function() {
setup(function() {
this.field = new Blockly.FieldCheckbox('FALSE');
});
testHelpers.runSetValueTests(
runSetValueTests(
validValueTestCases, invalidValueTestCases, 'FALSE', 'false');
});
});
Expand Down Expand Up @@ -131,7 +133,7 @@ suite('Checkbox Fields', function() {
});
test('New Value', function() {
this.field.setValue(suiteInfo.value);
testHelpers.assertFieldValue(
assertFieldValue(
this.field, suiteInfo.expectedValue,
String(suiteInfo.expectedValue).toLowerCase());
});
Expand Down
Loading

0 comments on commit 2edd228

Please sign in to comment.