Converting variable tests to mocha. (#3828)

This commit is contained in:
Monica Kozbial
2020-04-16 14:18:12 -07:00
committed by GitHub
parent 9e98df9949
commit 9c400f2bf0
4 changed files with 143 additions and 137 deletions

View File

@@ -19,7 +19,6 @@
<script src="test_utilities.js"></script>
<script src="generator_test.js"></script>
<script src="variables_test.js"></script>
<script src="variable_map_test.js"></script>
<script src="variable_model_test.js"></script>
<script src="widget_div_test.js"></script>

View File

@@ -1,136 +0,0 @@
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Tests for variable utility functions in Blockly
* @author fenichel@google.com (Rachel Fenichel)
*/
'use strict';
function variablesTest_setUp() {
defineGetVarBlock();
var workspace = new Blockly.Workspace();
workspace.createVariable('foo', 'type1', '1');
workspace.createVariable('bar', 'type1', '2');
workspace.createVariable('baz', 'type1', '3');
return workspace;
}
function variablesTest_tearDown(workspace) {
undefineGetVarBlock();
workspace.dispose();
}
function buildVariablesTest(testImpl) {
return function() {
var workspace = variablesTest_setUp();
try {
testImpl(workspace);
} finally {
variablesTest_tearDown(workspace);
}
};
}
var test_allUsedVarModels = buildVariablesTest(
function(workspace) {
createMockVarBlock(workspace, '1');
createMockVarBlock(workspace, '2');
createMockVarBlock(workspace, '3');
var result = Blockly.Variables.allUsedVarModels(workspace);
assertEquals('Expected three variables in the list of used variables',
3, result.length);
}
);
var test_allUsedVarModels_someUnused = buildVariablesTest(
function(workspace) {
createMockVarBlock(workspace, '2');
var result = Blockly.Variables.allUsedVarModels(workspace);
assertEquals('Expected one variable in the list of used variables',
1, result.length);
assertEquals('Expected variable with ID 2 in the list of used variables',
'2', result[0].getId());
}
);
var test_allUsedVarModels_varUsedTwice = buildVariablesTest(
function(workspace) {
createMockVarBlock(workspace, '2');
createMockVarBlock(workspace, '2');
var result = Blockly.Variables.allUsedVarModels(workspace);
// Using the same variable multiple times should not change the number of
// elements in the list.
assertEquals('Expected one variable in the list of used variables',
1, result.length);
assertEquals('Expected variable with ID 2 in the list of used variables',
'2', result[0].getId());
}
);
var test_allUsedVarModels_allUnused = buildVariablesTest(
function(workspace) {
var result = Blockly.Variables.allUsedVarModels(workspace);
assertEquals('Expected no variables in the list of used variables',
0, result.length);
}
);
var test_getVariable_byId = buildVariablesTest(
function(workspace) {
var var_1 = workspace.createVariable('name1', 'type1', 'id1');
var var_2 = workspace.createVariable('name2', 'type1', 'id2');
var var_3 = workspace.createVariable('name3', 'type2', 'id3');
var result_1 = Blockly.Variables.getVariable(workspace, 'id1');
var result_2 = Blockly.Variables.getVariable(workspace, 'id2');
var result_3 = Blockly.Variables.getVariable(workspace, 'id3');
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
}
);
var test_getVariable_byNameAndType = buildVariablesTest(
function(workspace) {
var var_1 = workspace.createVariable('name1', 'type1', 'id1');
var var_2 = workspace.createVariable('name2', 'type1', 'id2');
var var_3 = workspace.createVariable('name3', 'type2', 'id3');
var result_1 =
Blockly.Variables.getVariable(workspace, null, 'name1', 'type1');
var result_2 =
Blockly.Variables.getVariable(workspace, null, 'name2', 'type1');
var result_3 =
Blockly.Variables.getVariable(workspace, null, 'name3', 'type2');
// Searching by name + type is correct.
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
}
);
var test_getVariable_byIdThenName = buildVariablesTest(
function(workspace) {
var var_1 = workspace.createVariable('name1', 'type1', 'id1');
var var_2 = workspace.createVariable('name2', 'type1', 'id2');
var var_3 = workspace.createVariable('name3', 'type2', 'id3');
var result_1 =
Blockly.Variables.getVariable(workspace, 'badId', 'name1', 'type1');
var result_2 =
Blockly.Variables.getVariable(workspace, 'badId', 'name2', 'type1');
var result_3 =
Blockly.Variables.getVariable(workspace, 'badId', 'name3', 'type2');
// Searching by ID failed, but falling back onto name + type is correct.
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
}
);

View File

@@ -59,6 +59,7 @@
<script src="theme_test.js"></script>
<script src="trashcan_test.js"></script>
<script src="utils_test.js"></script>
<script src="variables_test.js"></script>
<script src="xml_procedures_test.js"></script>
<script src="xml_test.js"></script>

View File

@@ -0,0 +1,142 @@
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
suite('Variables', function() {
setup(function() {
this.workspace = new Blockly.Workspace();
Blockly.defineBlocksWithJsonArray([{
"type": "get_var_block",
"message0": "%1",
"args0": [
{
"type": "field_variable",
"name": "VAR",
"variableTypes": ["", "type1", "type2"]
}
]
}]);
this.workspace.createVariable('foo', 'type1', '1');
this.workspace.createVariable('bar', 'type1', '2');
this.workspace.createVariable('baz', 'type1', '3');
});
teardown(function() {
delete Blockly.Blocks['get_var_block'];
this.workspace.dispose();
// Clear Blockly.Event state.
Blockly.Events.setGroup(false);
Blockly.Events.disabled_ = 0;
sinon.restore();
});
/**
* Create a test get_var_block.
* Will fail if get_var_block isn't defined.
* @param {!Blockly.Workspace} workspace The workspace on which to create the
* block.
* @param {!string} variable_id The id of the variable to reference.
* @return {!Blockly.Block} The created block.
*/
function createTestVarBlock(workspace, variable_id) {
// Turn off events to avoid testing XML at the same time.
Blockly.Events.disable();
var block = new Blockly.Block(workspace, 'get_var_block');
block.inputList[0].fieldRow[0].setValue(variable_id);
Blockly.Events.enable();
return block;
}
suite('allUsedVarModels', function() {
test('All used', function() {
createTestVarBlock(this.workspace, '1');
createTestVarBlock(this.workspace, '2');
createTestVarBlock(this.workspace, '3');
var result = Blockly.Variables.allUsedVarModels(this.workspace);
assertEquals('Expected three variables in the list of used variables',
3, result.length);
});
test('Some unused', function() {
createTestVarBlock(this.workspace, '2');
var result = Blockly.Variables.allUsedVarModels(this.workspace);
assertEquals('Expected one variable in the list of used variables',
1, result.length);
assertEquals('Expected variable with ID 2 in the list of used variables',
'2', result[0].getId());
});
test('Var used twice', function() {
createTestVarBlock(this.workspace, '2');
createTestVarBlock(this.workspace, '2');
var result = Blockly.Variables.allUsedVarModels(this.workspace);
// Using the same variable multiple times should not change the number of
// elements in the list.
assertEquals('Expected one variable in the list of used variables',
1, result.length);
assertEquals('Expected variable with ID 2 in the list of used variables',
'2', result[0].getId());
});
test('All unused', function() {
var result = Blockly.Variables.allUsedVarModels(this.workspace);
assertEquals('Expected no variables in the list of used variables',
0, result.length);
});
});
suite('getVariable', function() {
test('By id', function() {
var var1 = this.workspace.createVariable('name1', 'type1', 'id1');
var var2 = this.workspace.createVariable('name2', 'type1', 'id2');
var var3 = this.workspace.createVariable('name3', 'type2', 'id3');
var result1 = Blockly.Variables.getVariable(this.workspace, 'id1');
var result2 = Blockly.Variables.getVariable(this.workspace, 'id2');
var result3 = Blockly.Variables.getVariable(this.workspace, 'id3');
assertEquals(var1, result1);
assertEquals(var2, result2);
assertEquals(var3, result3);
});
test('By name and type', function() {
var var1 = this.workspace.createVariable('name1', 'type1', 'id1');
var var2 = this.workspace.createVariable('name2', 'type1', 'id2');
var var3 = this.workspace.createVariable('name3', 'type2', 'id3');
var result1 =
Blockly.Variables.getVariable(this.workspace, null, 'name1', 'type1');
var result2 =
Blockly.Variables.getVariable(this.workspace, null, 'name2', 'type1');
var result3 =
Blockly.Variables.getVariable(this.workspace, null, 'name3', 'type2');
// Searching by name + type is correct.
assertEquals(var1, result1);
assertEquals(var2, result2);
assertEquals(var3, result3);
});
test('Bad id with name and type fallback', function() {
var var1 = this.workspace.createVariable('name1', 'type1', 'id1');
var var2 = this.workspace.createVariable('name2', 'type1', 'id2');
var var3 = this.workspace.createVariable('name3', 'type2', 'id3');
var result1 =
Blockly.Variables.getVariable(this.workspace, 'badId', 'name1', 'type1');
var result2 =
Blockly.Variables.getVariable(this.workspace, 'badId', 'name2', 'type1');
var result3 =
Blockly.Variables.getVariable(this.workspace, 'badId', 'name3', 'type2');
// Searching by ID failed, but falling back onto name + type is correct.
assertEquals(var1, result1);
assertEquals(var2, result2);
assertEquals(var3, result3);
});
});
});