Shared global cleanup (#4217)

* Refactor block type cleanup into shared cleanup.

* Use shared cleanup for block type cleanup.

* Remove overwriting of core Blockly block in test.

* Rename cleanup arrays for clarity

* Add blocks to cleanup array when defined in shared helpers

* Refactor logic for adding to shared cleanup.

* Add helper for defining block and adding block type to cleanup.

* Fix jsdocs

* Simplifying helpers for adding to cleanup.

* Add missing semicolons

* Update jsdoc for sharedTestSetup

* Use stub to wrap cleanup with defineBlocksWithJsonArray

* Remove unused helper from lint ignore
This commit is contained in:
Monica Kozbial
2020-09-09 15:51:34 -07:00
committed by GitHub
parent 6181dd37c7
commit 896c630a89
24 changed files with 104 additions and 191 deletions

View File

@@ -7,6 +7,8 @@
"globals": {
"chai": false,
"sinon": false,
"addBlockTypeToCleanup": true,
"addMessageToCleanup": true,
"assertArrayEquals": true,
"assertDeprecationWarningCall": true,
"assertEventEquals": true,

View File

@@ -91,9 +91,6 @@ suite('ASTNode', function() {
});
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['input_statement'];
delete Blockly.Blocks['field_input'];
delete Blockly.Blocks['value_input'];
});
suite('HelperFunctions', function() {
@@ -284,7 +281,6 @@ suite('ASTNode', function() {
"helpUrl": "",
"nextStatement": null
}]);
var noNextConnection = this.workspace.newBlock('top_connection');
var fieldAndInputs = this.workspace.newBlock('fields_and_input');
var twoFields = this.workspace.newBlock('two_fields');
@@ -308,16 +304,6 @@ suite('ASTNode', function() {
this.blocks.secondBlock = secondBlock;
this.blocks.outputNextBlock = outputNextBlock;
});
teardown(function() {
delete Blockly.Blocks['output_next'];
delete Blockly.Blocks['fields_and_input2'];
delete Blockly.Blocks['two_fields'];
delete Blockly.Blocks['fields_and_input'];
delete Blockly.Blocks['top_connection'];
delete Blockly.Blocks['start_block'];
delete Blockly.Blocks['dummy_input'];
delete Blockly.Blocks['dummy_inputValue'];
});
suite('Next', function() {
setup(function() {
this.singleBlockWorkspace = new Blockly.Workspace();

View File

@@ -45,10 +45,6 @@ suite('Blocks', function() {
});
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['empty_block'];
delete Blockly.Blocks['stack_block'];
delete Blockly.Blocks['row_block'];
delete Blockly.Blocks['statement_block'];
});
function createTestBlocks(workspace, isRow) {
@@ -328,9 +324,6 @@ suite('Blocks', function() {
},
]);
});
teardown(function() {
delete Blockly.Blocks['value_block'];
});
suite('Value', function() {
setup(function() {
@@ -1235,7 +1228,6 @@ suite('Blocks', function() {
teardown(function() {
Blockly.Events.enable();
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['variable_block'];
});
suite('Connecting and Disconnecting', function() {
test('Connect Block to Next', function() {

View File

@@ -14,7 +14,6 @@ suite('Comments', function() {
"args0": []
},
]);
this.workspace = Blockly.inject('blocklyDiv', {
comments: true,
scrollbars: true
@@ -27,7 +26,6 @@ suite('Comments', function() {
});
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['empty_block'];
});
suite('Visibility and Editability', function() {
setup(function() {

View File

@@ -74,8 +74,6 @@ suite('Cursor', function() {
});
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['input_statement'];
delete Blockly.Blocks['field_input'];
});
test('Next - From a Previous skip over next connection and block', function() {

View File

@@ -28,8 +28,6 @@ suite('Events', function() {
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['field_variable_test_block'];
delete Blockly.Blocks['simple_test_block'];
});
function createSimpleTestBlock(workspace) {

View File

@@ -8,15 +8,10 @@ suite('Extensions', function() {
setup(function() {
sharedTestSetup.call(this);
this.workspace = new Blockly.Workspace();
this.blockTypesCleanup_ = [];
this.extensionsCleanup_ = [];
});
teardown(function() {
sharedTestTeardown.call(this);
for (let i = 0; i < this.blockTypesCleanup_.length; i++) {
var blockType = this.blockTypesCleanup_[i];
delete Blockly.Blocks[blockType];
}
for (let i = 0; i < this.extensionsCleanup_.length; i++) {
var extension = this.extensionsCleanup_[i];
delete Blockly.Extensions.ALL_[extension];
@@ -26,7 +21,6 @@ suite('Extensions', function() {
test('Definition before and after block type', function() {
this.extensionsCleanup_.push('extensions_test_before');
this.extensionsCleanup_.push('extensions_test_after');
this.blockTypesCleanup_.push('extension_test_block');
chai.assert.isUndefined(Blockly.Extensions.ALL_['extensions_test_before']);
var beforeCallback = sinon.spy();
@@ -58,9 +52,6 @@ suite('Extensions', function() {
});
test('Parent tooltip when inline', function() {
this.blockTypesCleanup_.push('test_parent_tooltip_when_inline');
this.blockTypesCleanup_.push('test_parent');
var defaultTooltip = "defaultTooltip";
var parentTooltip = "parentTooltip";
Blockly.defineBlocksWithJsonArray([
@@ -114,7 +105,6 @@ suite('Extensions', function() {
suite('Mixin', function() {
test('Basic', function() {
this.extensionsCleanup_.push('mixin_test');
this.blockTypesCleanup_.push('test_block_mixin');
var testMixin = {
field: 'FIELD',
@@ -129,6 +119,7 @@ suite('Extensions', function() {
chai.assert.typeOf(Blockly.Extensions.ALL_['mixin_test'], 'function');
Blockly.defineBlocksWithJsonArray([{
"type": "test_block_mixin",
"message0": "test_block_mixin",
@@ -144,7 +135,6 @@ suite('Extensions', function() {
suite('Mutator', function() {
test('Basic', function() {
this.extensionsCleanup_.push('mutator_test');
this.blockTypesCleanup_.push('mutator_test_block');
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
@@ -182,7 +172,6 @@ suite('Extensions', function() {
test('With helper function', function() {
this.extensionsCleanup_.push('extensions_test');
this.blockTypesCleanup_.push('mutator_test_block');
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
@@ -214,7 +203,6 @@ suite('Extensions', function() {
test('No dialog', function() {
this.extensionsCleanup_.push('mutator_test');
this.blockTypesCleanup_.push('mutator_test_block');
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
@@ -249,8 +237,6 @@ suite('Extensions', function() {
suite('Error cases', function() {
test('Missing extension', function() {
this.blockTypesCleanup_.push('missing_extension_block');
Blockly.defineBlocksWithJsonArray([{
"type": "missing_extension_block",
"message0": "missing_extension_block",
@@ -266,7 +252,6 @@ suite('Extensions', function() {
test('Mixin overwrites local value', function() {
this.extensionsCleanup_.push('mixin_bad_inputList');
this.blockTypesCleanup_.push('test_block_bad_inputList');
var TEST_MIXIN_BAD_INPUTLIST = {
inputList: 'bad inputList' // Defined in constructor
@@ -291,7 +276,6 @@ suite('Extensions', function() {
test('Mixin overwrites prototype', function() {
this.extensionsCleanup_.push('mixin_bad_colour_');
this.blockTypesCleanup_.push('test_block_bad_colour');
var TEST_MIXIN_BAD_COLOUR = {
colour_: 'bad colour_' // Defined on prototype
@@ -316,7 +300,6 @@ suite('Extensions', function() {
test('Use mutator as extension', function() {
this.extensionsCleanup_.push('mutator_test');
this.blockTypesCleanup_.push('mutator_test_block');
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
@@ -348,7 +331,6 @@ suite('Extensions', function() {
test('Use mutator mixin as extension', function() {
this.extensionsCleanup_.push('mutator_test');
this.blockTypesCleanup_.push('mutator_test_block');
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",
@@ -380,7 +362,6 @@ suite('Extensions', function() {
test('Use extension as mutator', function() {
this.extensionsCleanup_.push('extensions_test');
this.blockTypesCleanup_.push('mutator_test_block');
Blockly.defineBlocksWithJsonArray([{
"type": "mutator_test_block",

View File

@@ -351,11 +351,9 @@ suite('Abstract Fields', function() {
});
suite('W/ Msg References', function() {
setup(function() {
addMessageToCleanup(this.sharedCleanup, 'TOOLTIP');
Blockly.Msg['TOOLTIP'] = 'test tooltip';
});
teardown(function() {
delete Blockly.Msg['TOOLTIP'];
});
test('JS Constructor', function() {
var field = new Blockly.Field('value', null, {
tooltip: '%{BKY_TOOLTIP}',
@@ -378,6 +376,7 @@ suite('Abstract Fields', function() {
workspaceTeardown.call(this, this.workspace);
});
test('Before Append', function() {
addBlockTypeToCleanup(this.sharedCleanup, 'tooltip');
Blockly.Blocks['tooltip'] = {
init: function() {
var field = new Blockly.FieldTextInput('default');
@@ -393,9 +392,9 @@ suite('Abstract Fields', function() {
).children[0], this.workspace);
var field = block.getField('TOOLTIP');
chai.assert.equal(field.getClickTarget_().tooltip, 'tooltip');
delete Blockly.Blocks['tooltip'];
});
test('After Append', function() {
addBlockTypeToCleanup(this.sharedCleanup, 'tooltip');
Blockly.Blocks['tooltip'] = {
init: function() {
var field = new Blockly.FieldTextInput('default');
@@ -411,9 +410,9 @@ suite('Abstract Fields', function() {
).children[0], this.workspace);
var field = block.getField('TOOLTIP');
chai.assert.equal(field.getClickTarget_().tooltip, 'tooltip');
delete Blockly.Blocks['tooltip'];
});
test('After Block Creation', function() {
addBlockTypeToCleanup(this.sharedCleanup, 'tooltip');
Blockly.Blocks['tooltip'] = {
init: function() {
var field = new Blockly.FieldTextInput('default');
@@ -429,9 +428,9 @@ suite('Abstract Fields', function() {
var field = block.getField('TOOLTIP');
field.setTooltip('tooltip');
chai.assert.equal(field.getClickTarget_().tooltip, 'tooltip');
delete Blockly.Blocks['tooltip'];
});
test('Dynamic Function', function() {
addBlockTypeToCleanup(this.sharedCleanup, 'tooltip');
Blockly.Blocks['tooltip'] = {
init: function() {
var field = new Blockly.FieldTextInput('default');
@@ -451,9 +450,9 @@ suite('Abstract Fields', function() {
).children[0], this.workspace);
var field = block.getField('TOOLTIP');
chai.assert.equal(field.getClickTarget_().tooltip, block.tooltipFunc);
delete Blockly.Blocks['tooltip'];
});
test('Element', function() {
addBlockTypeToCleanup(this.sharedCleanup, 'tooltip');
Blockly.Blocks['tooltip'] = {
init: function() {
var field = new Blockly.FieldTextInput('default');
@@ -472,9 +471,9 @@ suite('Abstract Fields', function() {
).children[0], this.workspace);
var field = block.getField('TOOLTIP');
chai.assert.equal(field.getClickTarget_().tooltip, block.element);
delete Blockly.Blocks['tooltip'];
});
test('Null', function() {
addBlockTypeToCleanup(this.sharedCleanup, 'tooltip');
Blockly.Blocks['tooltip'] = {
init: function() {
var field = new Blockly.FieldTextInput('default');
@@ -490,9 +489,9 @@ suite('Abstract Fields', function() {
).children[0], this.workspace);
var field = block.getField('TOOLTIP');
chai.assert.equal(field.getClickTarget_().tooltip, block);
delete Blockly.Blocks['tooltip'];
});
test('Undefined', function() {
addBlockTypeToCleanup(this.sharedCleanup, 'tooltip');
Blockly.Blocks['tooltip'] = {
init: function() {
var field = new Blockly.FieldTextInput('default');
@@ -507,7 +506,6 @@ suite('Abstract Fields', function() {
).children[0], this.workspace);
var field = block.getField('TOOLTIP');
chai.assert.equal(field.getClickTarget_().tooltip, block);
delete Blockly.Blocks['tooltip'];
});
});
});

View File

@@ -321,10 +321,6 @@ suite('Variable Fields', function() {
'field_variable_test_block');
this.variableField = this.variableBlock.getField('VAR');
});
teardown(function() {
this.variableBlock.dispose();
delete Blockly.Blocks['field_variable_test_block'];
});
test('Rename & Keep Old ID', function() {
this.workspace.renameVariableById('id1', 'name2');
chai.assert.equal(this.variableField.getText(), 'name2');

View File

@@ -27,7 +27,6 @@ suite('Flyout', function() {
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['basic_block'];
});
suite('createFlyoutInfo_', function() {

View File

@@ -78,11 +78,6 @@ suite('Generator', function() {
};
});
teardown(function() {
delete Blockly.Blocks['stack_block'];
delete Blockly.Blocks['row_block'];
});
var testCase = [
[Blockly.Dart, 'Dart'],
[Blockly.JavaScript, 'JavaScript'],
@@ -115,7 +110,7 @@ suite('Generator', function() {
suite('Nested block', function() {
setup(function() {
Blockly.defineBlocksWithJsonArray([ {
Blockly.defineBlocksWithJsonArray([{
"type": "test_loop_block",
"message0": "Repeat Loop",
"message1": "%1",
@@ -142,10 +137,6 @@ suite('Generator', function() {
};
});
teardown(function() {
delete Blockly.Blocks['test_loop_block'];
});
testCase.forEach(function(testCase) {
var generator = testCase[0];
var name = testCase[1];

View File

@@ -46,14 +46,13 @@ suite('Gesture', function() {
setup(function() {
sharedTestSetup.call(this);
defineBasicBlockWithField();
defineBasicBlockWithField(this.sharedCleanup);
var toolbox = document.getElementById('gesture-test-toolbox');
this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
});
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Block['test_field_block'];
});
test('Constructor', function() {

View File

@@ -7,13 +7,11 @@
suite('Inputs', function() {
setup(function() {
sharedTestSetup.call(this);
Blockly.defineBlocksWithJsonArray([
{
"type": "empty_block",
"message0": "",
"args0": []
},
]);
Blockly.defineBlocksWithJsonArray([{
"type": "empty_block",
"message0": "",
"args0": []
}]);
this.workspace = Blockly.inject('blocklyDiv');
this.block = Blockly.Xml.domToBlock(Blockly.Xml.textToDom(
@@ -32,7 +30,6 @@ suite('Inputs', function() {
});
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['empty_block'];
});
suite('Insert Field At', function() {
suite('Index Bounds', function() {

View File

@@ -41,9 +41,6 @@ suite('InsertionMarkers', function() {
});
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['stack_block'];
delete Blockly.Blocks['row_block'];
delete Blockly.Blocks['statement_block'];
});
suite('Code Generation', function() {
setup(function() {

View File

@@ -8,32 +8,22 @@ suite('JSON Block Definitions', function() {
setup(function() {
sharedTestSetup.call(this);
this.workspace_ = new Blockly.Workspace();
this.blockTypes_ = [];
this.messages_ = [];
});
teardown(function() {
sharedTestTeardown.call(this);
for (var i = 0, blockType; (blockType = this.blockTypes_[i]); i++) {
delete Blockly.Blocks[blockType];
}
for (var i = 0, message; (message = this.messages_[i]); i++) {
delete Blockly.Msg[message];
}
});
suite('defineBlocksWithJsonArray', function() {
test('Basic block', function() {
/** Ensure a block can be instantiated from a JSON definition. */
var BLOCK_TYPE = 'test_json_minimal';
this.blockTypes_.push(BLOCK_TYPE);
var workspace = this.workspace_;
var block;
assertNoWarnings(() => {
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE
}]);
block = new Blockly.Block(workspace, BLOCK_TYPE);
block = new Blockly.Block(this.workspace_, BLOCK_TYPE);
});
chai.assert.isNotNull(block);
@@ -43,8 +33,6 @@ suite('JSON Block Definitions', function() {
test('Null or undefined type id', function() {
var BLOCK_TYPE1 = 'test_json_before_bad_blocks';
var BLOCK_TYPE2 = 'test_json_after_bad_blocks';
this.blockTypes_.push(BLOCK_TYPE1);
this.blockTypes_.push(BLOCK_TYPE2);
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE1]);
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]);
@@ -67,8 +55,6 @@ suite('JSON Block Definitions', function() {
test('Null item', function() {
var BLOCK_TYPE1 = 'test_block_before_null';
var BLOCK_TYPE2 = 'test_block_after_null';
this.blockTypes_.push(BLOCK_TYPE1);
this.blockTypes_.push(BLOCK_TYPE2);
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE1]);
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]);
@@ -96,8 +82,6 @@ suite('JSON Block Definitions', function() {
test('Undefined item', function() {
var BLOCK_TYPE1 = 'test_block_before_undefined';
var BLOCK_TYPE2 = 'test_block_after_undefined';
this.blockTypes_.push(BLOCK_TYPE1);
this.blockTypes_.push(BLOCK_TYPE2);
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE1]);
chai.assert.isUndefined(Blockly.Blocks[BLOCK_TYPE2]);
@@ -123,7 +107,6 @@ suite('JSON Block Definitions', function() {
test('message0 creates input', function() {
var BLOCK_TYPE = 'test_json_message0';
this.blockTypes_.push(BLOCK_TYPE);
var MESSAGE0 = 'message0';
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE,
@@ -141,7 +124,6 @@ suite('JSON Block Definitions', function() {
test('message1 and message0 creates two inputs', function() {
/** Ensure message1 creates a new input. */
var BLOCK_TYPE = 'test_json_message1';
this.blockTypes_.push(BLOCK_TYPE);
var MESSAGE0 = 'message0';
var MESSAGE1 = 'message1';
Blockly.defineBlocksWithJsonArray([{
@@ -166,12 +148,11 @@ suite('JSON Block Definitions', function() {
test('Message string is dereferenced', function() {
var BLOCK_TYPE = 'test_json_message0_i18n';
this.blockTypes_.push(BLOCK_TYPE);
var MESSAGE0 = '%{BKY_MESSAGE}';
var MESSAGE = 'message';
addMessageToCleanup(this.sharedCleanup, 'MESSAGE');
Blockly.Msg['MESSAGE'] = MESSAGE;
this.messages_.push('MESSAGE');
Blockly.defineBlocksWithJsonArray([{
"type": BLOCK_TYPE,
"message0": MESSAGE0
@@ -187,7 +168,6 @@ suite('JSON Block Definitions', function() {
test('Dropdown', function() {
var BLOCK_TYPE = 'test_json_dropdown';
this.blockTypes_.push(BLOCK_TYPE);
var FIELD_NAME = 'FIELD_NAME';
var LABEL0 = 'LABEL0';
var VALUE0 = 'VALUE0';
@@ -226,11 +206,10 @@ suite('JSON Block Definitions', function() {
test('Dropdown with images', function() {
var BLOCK_TYPE = 'test_json_dropdown';
this.blockTypes_.push(BLOCK_TYPE);
var FIELD_NAME = 'FIELD_NAME';
var IMAGE1_ALT_TEXT = 'Localized message.';
addMessageToCleanup(this.sharedCleanup, 'ALT_TEXT');
Blockly.Msg['ALT_TEXT'] = IMAGE1_ALT_TEXT;
this.messages_.push('ALT_TEXT');
var IMAGE0 = {
'width': 12,
'height': 34,

View File

@@ -24,9 +24,9 @@ suite('Insert/Modify', function() {
'<block type="statement_block" id="statement_block_1" x="12" y="288"></block>' +
'<block type="statement_block" id="statement_block_2" x="12" y="288"></block>' +
'</xml>';
defineStackBlock();
defineRowBlock();
defineStatementBlock();
defineStackBlock(this.sharedCleanup);
defineRowBlock(this.sharedCleanup);
defineStatementBlock(this.sharedCleanup);
var toolbox = document.getElementById('toolbox-connections');
this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
@@ -44,9 +44,6 @@ suite('Insert/Modify', function() {
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['stack_block'];
delete Blockly.Blocks['row_block'];
delete Blockly.Blocks['statement_block'];
});
suite('Marked Connection', function() {

View File

@@ -55,7 +55,6 @@ suite('Navigation', function() {
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['basic_block'];
});
function testToolboxSelectMethodCalled(ws, mockEvent, keyCode, selectMethodName) {
@@ -137,7 +136,6 @@ suite('Navigation', function() {
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['basic_block'];
});
// Should be a no-op
@@ -228,7 +226,6 @@ suite('Navigation', function() {
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['basic_block'];
});
test('Previous', function() {
@@ -341,7 +338,6 @@ suite('Navigation', function() {
});
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['basic_block'];
});
test('Action does not exist', function() {
var block = this.workspace.getTopBlocks()[0];
@@ -452,7 +448,6 @@ suite('Navigation', function() {
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['field_block'];
});
test('Perform valid action for read only', function() {
@@ -506,7 +501,6 @@ suite('Navigation', function() {
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['basic_block'];
});
test('Insert from flyout with a valid connection marked', function() {
@@ -611,8 +605,6 @@ suite('Navigation', function() {
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['inline_block'];
delete Blockly.Blocks['basic_block'];
});
test('Connect cursor on previous into stack', function() {
@@ -682,7 +674,6 @@ suite('Navigation', function() {
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks['basic_block'];
});
test('Delete block - has parent ', function() {

View File

@@ -130,11 +130,66 @@ function createEventsFireStubFireImmediately_(clock) {
return stub;
}
/**
* Adds message to shared cleanup object so that it is cleaned from
* Blockly.Messages global in sharedTestTeardown.
* @param {!Object} sharedCleanupObj The shared cleanup object created in
* sharedTestSetup.
* @param {string} message The message to add to shared cleanup object.
*/
function addMessageToCleanup(sharedCleanupObj, message) {
sharedCleanupObj.messagesCleanup_.push(message);
}
/**
* Adds block type to shared cleanup object so that it is cleaned from
* Blockly.Blocks global in sharedTestTeardown.
* @param {!Object} sharedCleanupObj The shared cleanup object created in
* sharedTestSetup.
* @param {string} blockType The block type to add to shared cleanup object.
*/
function addBlockTypeToCleanup(sharedCleanupObj, blockType) {
sharedCleanupObj.blockTypesCleanup_.push(blockType);
}
/**
* Wraps Blockly.defineBlocksWithJsonArray using stub in order to keep track of
* block types passed in to method on shared cleanup object so they are cleaned
* from Blockly.Blocks global in sharedTestTeardown.
* @param {!Object} sharedCleanupObj The shared cleanup object created in
* sharedTestSetup.
* @private
*/
function wrapDefineBlocksWithJsonArrayWithCleanup_(sharedCleanupObj) {
var stub = sinon.stub(Blockly, 'defineBlocksWithJsonArray');
stub.callsFake(function(jsonArray) {
if (jsonArray) {
jsonArray.forEach((jsonBlock) => {
if (jsonBlock) {
addBlockTypeToCleanup(sharedCleanupObj, jsonBlock['type']);
}
});
}
// Calls original method.
stub.wrappedMethod.call(this, ...arguments);
});
}
/**
* Shared setup method that sets up fake timer for clock so that pending
* setTimeout calls can be cleared in test teardown along with other common
* stubs. Should be called in setup of outermost suite using
* sharedTestSetup.call(this).
* The sinon fake timer defined on this.clock_ should not be reset in tests to
* avoid causing issues with cleanup in sharedTestTeardown.
*
* Stubs created in this setup (unless disabled by options passed):
* - Blockly.Events.fire - this.eventsFireStub - wraps fire event to trigger
* fireNow_ call immediately, rather than on timeout
* - Blockly.defineBlocksWithJsonArray - thin wrapper that adds logic to keep
* track of block types defined so that they can be undefined in
* sharedTestTeardown and calls original method.
*
* @param {Object<string, boolean>} options Options to enable/disable setup
* of certain stubs.
*/
@@ -147,6 +202,14 @@ function sharedTestSetup(options = {}) {
// Stubs event firing unless passed option "fireEventsNow: false"
this.eventsFireStub = createEventsFireStubFireImmediately_(this.clock);
}
this.sharedCleanup = {
blockTypesCleanup_: [],
messagesCleanup_: []
};
this.blockTypesCleanup_ = this.sharedCleanup.blockTypesCleanup_;
this.messagesCleanup_ = this.sharedCleanup.messagesCleanup_;
wrapDefineBlocksWithJsonArrayWithCleanup_(this.sharedCleanup);
}
/**
@@ -187,6 +250,15 @@ function sharedTestTeardown() {
// Restore all stubbed methods.
this.sharedSetupSandbox_.restore();
sinon.restore();
var blockTypes = this.sharedCleanup.blockTypesCleanup_;
for (let i = 0; i < blockTypes.length; i++) {
delete Blockly.Blocks[blockTypes[i]];
}
var messages = this.sharedCleanup.messagesCleanup_;
for (let i = 0; i < messages.length; i++) {
delete Blockly.Msg[messages[i]];
}
}
}
@@ -397,7 +469,7 @@ function assertNthCallEventArgEquals(spy, n, instanceType, expectedProperties,
assertXmlProperties_(eventArg, xmlProperties);
}
function defineStackBlock() {
function defineStackBlock(sharedCleanupObj) {
Blockly.defineBlocksWithJsonArray([{
"type": "stack_block",
"message0": "",
@@ -406,7 +478,7 @@ function defineStackBlock() {
}]);
}
function defineRowBlock() {
function defineRowBlock(sharedCleanupObj) {
Blockly.defineBlocksWithJsonArray([{
"type": "row_block",
"message0": "%1",
@@ -420,7 +492,7 @@ function defineRowBlock() {
}]);
}
function defineStatementBlock() {
function defineStatementBlock(sharedCleanupObj) {
Blockly.defineBlocksWithJsonArray([{
"type": "statement_block",
"message0": "%1",
@@ -437,7 +509,7 @@ function defineStatementBlock() {
"helpUrl": ""
}]);
}
function defineBasicBlockWithField() {
function defineBasicBlockWithField(sharedCleanupObj) {
Blockly.defineBlocksWithJsonArray([{
"type": "test_field_block",
"message0": "%1",

View File

@@ -20,7 +20,7 @@ suite('Theme', function() {
Blockly.registry.typeMap_['theme'] = {};
});
function defineThemeTestBlocks() {
function defineThemeTestBlocks(sharedCleanupObj) {
Blockly.defineBlocksWithJsonArray([{
"type": "stack_block",
"message0": "",
@@ -40,12 +40,6 @@ suite('Theme', function() {
}]);
}
function undefineThemeTestBlocks() {
delete Blockly.Blocks['stack_block'];
delete Blockly.Blocks['row_block'];
}
function createBlockStyles() {
return {
"styleOne": {
@@ -123,7 +117,7 @@ suite('Theme', function() {
});
test('Set Theme', function() {
defineThemeTestBlocks();
defineThemeTestBlocks(this.sharedCleanup);
try {
var blockStyles = createBlockStyles();
var workspace = new Blockly.WorkspaceSvg(new Blockly.Options({}));
@@ -154,7 +148,6 @@ suite('Theme', function() {
workspace.id, null);
} finally {
workspaceTeardown.call(this, workspace);
undefineThemeTestBlocks();
}
});

View File

@@ -13,7 +13,6 @@ suite('Toolbox', function() {
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['row_block'];
});
suite('init', function() {

View File

@@ -26,7 +26,6 @@ suite('Variables', function() {
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['get_var_block'];
});
/**

View File

@@ -28,8 +28,6 @@ suite('WorkspaceSvg', function() {
teardown(function() {
sharedTestTeardown.call(this);
delete Blockly.Blocks['simple_test_block'];
delete Blockly.Blocks['test_val_in'];
});
test('dispose of WorkspaceSvg without dom throws no error', function() {
@@ -144,7 +142,6 @@ suite('WorkspaceSvg', function() {
// because it holds the variable map.
// Normally the main workspace disposes of the flyout workspace.
workspaceTeardown.call(this, this.targetWorkspace);
delete Blockly.Blocks['get_var_block'];
});
test('Trivial Flyout is True', function() {

View File

@@ -34,7 +34,6 @@ function testAWorkspace() {
});
teardown(function() {
delete Blockly.Blocks['get_var_block'];
// Clear Blockly.Event state.
Blockly.Events.setGroup(false);
Blockly.Events.disabled_ = 0;

View File

@@ -36,7 +36,6 @@ suite('XML', function() {
"args0": []
},
]);
this.blockTypes_ = ['empty_block'];
this.complexXmlText = [
'<xml xmlns="https://developers.google.com/blockly/xml">',
' <block type="controls_repeat_ext" inline="true" x="21" y="23">',
@@ -67,10 +66,6 @@ suite('XML', function() {
});
teardown(function() {
sharedTestTeardown.call(this);
for (var i = 0; i < this.blockTypes_.length; i++) {
delete Blockly.Blocks[this.blockTypes_[i]];
}
this.blockTypes_.length = 0;
});
suite('textToDom', function() {
test('Basic', function() {
@@ -99,7 +94,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_angle_test_block');
var block = new Blockly.Block(this.workspace,
'field_angle_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
@@ -117,7 +111,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_checkbox_test_block');
var block = new Blockly.Block(this.workspace,
'field_checkbox_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
@@ -135,7 +128,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_colour_test_block');
var block = new Blockly.Block(this.workspace,
'field_colour_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
@@ -166,7 +158,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_dropdown_test_block');
var block = new Blockly.Block(this.workspace,
'field_dropdown_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
@@ -187,7 +178,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_image_test_block');
var block = new Blockly.Block(this.workspace,
'field_image_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block);
@@ -205,7 +195,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_label_test_block');
var block = new Blockly.Block(this.workspace,
'field_label_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block);
@@ -223,7 +212,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_label_serializable_test_block');
var block = new Blockly.Block(this.workspace,
'field_label_serializable_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
@@ -241,7 +229,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_number_test_block');
var block = new Blockly.Block(this.workspace,
'field_number_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
@@ -259,7 +246,6 @@ suite('XML', function() {
}
],
}]);
this.blockTypes_.push('field_text_input_test_block');
var block = new Blockly.Block(this.workspace,
'field_text_input_test_block');
var resultFieldDom = Blockly.Xml.blockToDom(block).childNodes[0];
@@ -278,7 +264,6 @@ suite('XML', function() {
}
]
}]);
this.blockTypes_.push('field_variable_test_block');
});
test('Variable Trivial', function() {
this.workspace.createVariable('name1', '', 'id1');
@@ -404,7 +389,6 @@ suite('XML', function() {
}
]
}]);
this.blockTypes_.push('field_variable_test_block');
});
teardown(function() {
workspaceTeardown.call(this, this.workspace);
@@ -485,35 +469,7 @@ suite('XML', function() {
"name": "VALUE"
}
]
},
{
"type": "math_change",
"message0": "%1 %2",
"args0": [
{
"type": "field_variable",
"name": "VAR"
},
{
"type": "input_value",
"name": "DELTA",
"check": "Number"
}
]
},
{
"type": "math_number",
"message0": "%1",
"args0": [{
"type": "field_number",
"name": "NUM",
"value": 0
}],
"output": "Number"
}]);
Array.prototype.push.apply(
this.blockTypes_,
['variables_get', 'variables_set', 'math_change', 'math_number']);
});
teardown(function() {
workspaceTeardown.call(this, this.workspace);
@@ -680,7 +636,6 @@ suite('XML', function() {
}
]
}]);
this.blockTypes_.push('field_variable_test_block');
});
teardown(function() {
workspaceTeardown.call(this, this.workspace);
@@ -758,7 +713,8 @@ suite('XML', function() {
});
suite('appendDomToWorkspace', function() {
setup(function() {
Blockly.Blocks.test_block = {
addBlockTypeToCleanup(this.sharedCleanup,'test_block');
Blockly.Blocks['test_block'] = {
init: function() {
this.jsonInit({
message0: 'test',
@@ -769,7 +725,6 @@ suite('XML', function() {
});
teardown(function() {
workspaceTeardown.call(this, this.workspace);
delete Blockly.Blocks.test_block;
});
test('Headless', function() {
var dom = Blockly.Xml.textToDom(