Converting variable map tests to mocha. (#3827)

* Converting variable map tests to mocha.

* Removing TODO updating chai syntax.
This commit is contained in:
Monica Kozbial
2020-04-16 17:47:17 -07:00
committed by GitHub
parent e838027629
commit a74cd0898c
7 changed files with 274 additions and 307 deletions

View File

@@ -19,7 +19,6 @@
<script src="test_utilities.js"></script>
<script src="generator_test.js"></script>
<script src="variable_map_test.js"></script>
<script src="widget_div_test.js"></script>
<!-- Disable workspace comment tests until we include them in our build. -->
<!-- <script src="workspace_comment_test.js"></script> -->

View File

@@ -1,284 +0,0 @@
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Tests for variable map.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
var variable_map;
var mockControl_;
var workspace;
function variableMapTest_setUp() {
workspace = new Blockly.Workspace();
variable_map = new Blockly.VariableMap(workspace);
}
function variableMapTest_tearDown() {
workspace.dispose();
if (mockControl_) {
mockControl_.restore();
}
variable_map = null;
}
function test_getVariable_ByNameAndType() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', 'type1', 'id1');
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
var var_3 = variable_map.createVariable('name3', 'type2', 'id3');
var result_1 = variable_map.getVariable('name1', 'type1');
var result_2 = variable_map.getVariable('name2', 'type1');
var result_3 = variable_map.getVariable('name3', 'type2');
// Searching by name + type is correct.
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
// Searching only by name defaults to the '' type.
assertNull(variable_map.getVariable('name1'));
assertNull(variable_map.getVariable('name2'));
assertNull(variable_map.getVariable('name3'));
variableMapTest_tearDown();
}
function test_getVariable_NotFound() {
variableMapTest_setUp();
var result = variable_map.getVariable('name1');
assertNull(result);
variableMapTest_tearDown();
}
function test_getVariableById_Trivial() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', 'type1', 'id1');
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
var var_3 = variable_map.createVariable('name3', 'type2', 'id3');
var result_1 = variable_map.getVariableById('id1');
var result_2 = variable_map.getVariableById('id2');
var result_3 = variable_map.getVariableById('id3');
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
variableMapTest_tearDown();
}
function test_getVariableById_NotFound() {
variableMapTest_setUp();
var result = variable_map.getVariableById('id1');
assertNull(result);
variableMapTest_tearDown();
}
function test_createVariableTrivial() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
checkVariableValues(variable_map, 'name1', 'type1', 'id1');
variableMapTest_tearDown();
}
function test_createVariableAlreadyExists() {
// Expect that when the variable already exists, the variableMap_ is unchanged.
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
// Assert there is only one variable in the variable_map.
var keys = Object.keys(variable_map.variableMap_);
assertEquals(1, keys.length);
var varMapLength = variable_map.variableMap_[keys[0]].length;
assertEquals(1, varMapLength);
variable_map.createVariable('name1', 'type1');
checkVariableValues(variable_map, 'name1', 'type1', 'id1');
// Check that the size of the variableMap_ did not change.
keys = Object.keys(variable_map.variableMap_);
assertEquals(1, keys.length);
varMapLength = variable_map.variableMap_[keys[0]].length;
assertEquals(1, varMapLength);
variableMapTest_tearDown();
}
function test_createVariableNameAlreadyExists() {
// Expect that when a variable with the same name but a different type already
// exists, the new variable is created.
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
// Assert there is only one variable in the variable_map.
var keys = Object.keys(variable_map.variableMap_);
assertEquals(1, keys.length);
var varMapLength = variable_map.variableMap_[keys[0]].length;
assertEquals(1, varMapLength);
variable_map.createVariable('name1', 'type2', 'id2');
checkVariableValues(variable_map, 'name1', 'type1', 'id1');
checkVariableValues(variable_map, 'name1', 'type2', 'id2');
// Check that the size of the variableMap_ did change.
keys = Object.keys(variable_map.variableMap_);
assertEquals(2, keys.length);
variableMapTest_tearDown();
}
function test_createVariableNullAndUndefinedType() {
variableMapTest_setUp();
variable_map.createVariable('name1', null, 'id1');
variable_map.createVariable('name2', undefined, 'id2');
checkVariableValues(variable_map, 'name1', '', 'id1');
checkVariableValues(variable_map, 'name2', '', 'id2');
variableMapTest_tearDown();
}
function test_createVariableNullId() {
variableMapTest_setUp();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1', '2']);
try {
variable_map.createVariable('name1', 'type1', null);
checkVariableValues(variable_map, 'name1', 'type1', '1');
} finally {
variableMapTest_tearDown();
}
}
function test_createVariableUndefinedId() {
variableMapTest_setUp();
mockControl_ = setUpMockMethod(Blockly.utils, 'genUid', null, ['1', '2']);
try {
variable_map.createVariable('name1', 'type1', undefined);
checkVariableValues(variable_map, 'name1', 'type1', '1');
} finally {
variableMapTest_tearDown();
}
}
function test_createVariableIdAlreadyExists() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
try {
variable_map.createVariable('name2', 'type2', 'id1');
fail();
} catch (e) {
// expected
}
variableMapTest_tearDown();
}
function test_createVariableMismatchedIdAndType() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
try {
variable_map.createVariable('name1', 'type2', 'id1');
fail();
} catch (e) {
// expected
}
try {
variable_map.createVariable('name1', 'type1', 'id2');
fail();
} catch (e) {
// expected
}
variableMapTest_tearDown();
}
function test_createVariableTwoSameTypes() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
variable_map.createVariable('name2', 'type1', 'id2');
checkVariableValues(variable_map, 'name1', 'type1', 'id1');
checkVariableValues(variable_map, 'name2', 'type1', 'id2');
variableMapTest_tearDown();
}
function test_getVariablesOfType_Trivial() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', 'type1', 'id1');
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
variable_map.createVariable('name3', 'type2', 'id3');
variable_map.createVariable('name4', 'type3', 'id4');
var result_array_1 = variable_map.getVariablesOfType('type1');
var result_array_2 = variable_map.getVariablesOfType('type5');
isEqualArrays([var_1, var_2], result_array_1);
isEqualArrays([], result_array_2);
variableMapTest_tearDown();
}
function test_getVariablesOfType_Null() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', '', 'id1');
var var_2 = variable_map.createVariable('name2', '', 'id2');
var var_3 = variable_map.createVariable('name3', '', 'id3');
variable_map.createVariable('name4', 'type1', 'id4');
var result_array = variable_map.getVariablesOfType(null);
isEqualArrays([var_1, var_2, var_3], result_array);
variableMapTest_tearDown();
}
function test_getVariablesOfType_EmptyString() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', null, 'id1');
var var_2 = variable_map.createVariable('name2', null, 'id2');
var result_array = variable_map.getVariablesOfType('');
isEqualArrays([var_1, var_2], result_array);
variableMapTest_tearDown();
}
function test_getVariablesOfType_Deleted() {
variableMapTest_setUp();
var variable = variable_map.createVariable('name1', null, 'id1');
variable_map.deleteVariable(variable);
var result_array = variable_map.getVariablesOfType('');
isEqualArrays([], result_array);
variableMapTest_tearDown();
}
function test_getVariablesOfType_DoesNotExist() {
variableMapTest_setUp();
var result_array = variable_map.getVariablesOfType('type1');
isEqualArrays([], result_array);
variableMapTest_tearDown();
}
function test_getVariableTypes_Trivial() {
variableMapTest_setUp();
variable_map.createVariable('name1', 'type1', 'id1');
variable_map.createVariable('name2', 'type1', 'id2');
variable_map.createVariable('name3', 'type2', 'id3');
variable_map.createVariable('name4', 'type3', 'id4');
var result_array = variable_map.getVariableTypes();
// The empty string is always an option.
isEqualArrays(['type1', 'type2', 'type3', ''], result_array);
variableMapTest_tearDown();
}
function test_getVariableTypes_None() {
variableMapTest_setUp();
// The empty string is always an option.
var result_array = variable_map.getVariableTypes();
isEqualArrays([''], result_array);
variableMapTest_tearDown();
}
function test_getAllVariables_Trivial() {
variableMapTest_setUp();
var var_1 = variable_map.createVariable('name1', 'type1', 'id1');
var var_2 = variable_map.createVariable('name2', 'type1', 'id2');
var var_3 = variable_map.createVariable('name3', 'type2', 'id3');
var result_array = variable_map.getAllVariables();
isEqualArrays([var_1, var_2, var_3], result_array);
variableMapTest_tearDown();
}
function test_getAllVariables_None() {
variableMapTest_setUp();
var result_array = variable_map.getAllVariables();
isEqualArrays([], result_array);
variableMapTest_tearDown();
}

View File

@@ -17,6 +17,7 @@
"assertUndefined": true,
"assertNotUndefined": true,
"assertNotNullNorUndefined": true,
"assertVariableValues": true,
"defineStackBlock": true,
"defineRowBlock": true,
"defineStatementBlock": true,

View File

@@ -228,21 +228,6 @@ suite('Events', function() {
this.variable = this.workspace.createVariable('name1', 'type1', 'id1');
});
/**
* Check if a variable with the given values exists.
* @param {Blockly.Workspace|Blockly.VariableMap} container The workspace or
* variableMap the checked variable belongs to.
* @param {!string} name The expected name of the variable.
* @param {!string} type The expected type of the variable.
* @param {!string} id The expected id of the variable.
*/
function checkVariableValues(container, name, type, id) {
var variable = container.getVariableById(id);
assertNotUndefined(variable);
assertEquals(name, variable.name);
assertEquals(type, variable.type);
assertEquals(id, variable.getId());
}
suite('Constructors', function() {
test('Var base', function() {
var event = new Blockly.Events.VarBase(this.variable);
@@ -334,7 +319,7 @@ suite('Events', function() {
var x = this.workspace.getVariableById('id2');
assertNull(x);
event.run(true);
checkVariableValues(this.workspace, 'name2', 'type2', 'id2');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2');
});
test('Var delete', function() {
@@ -348,7 +333,7 @@ suite('Events', function() {
var event = new Blockly.Events.VarRename(this.variable, 'name2');
event.run(true);
assertNull(this.workspace.getVariable('name1'));
checkVariableValues(this.workspace, 'name2', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type1', 'id1');
});
});
suite('Run Backward', function() {
@@ -364,14 +349,14 @@ suite('Events', function() {
var event = Blockly.Events.fromJson(json, this.workspace);
assertNull(this.workspace.getVariableById('id2'));
event.run(false);
checkVariableValues(this.workspace, 'name2', 'type2', 'id2');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2');
});
test('Var rename', function() {
var event = new Blockly.Events.VarRename(this.variable, 'name2');
event.run(false);
assertNull(this.workspace.getVariable('name2'));
checkVariableValues(this.workspace, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name1', 'type1', 'id1');
});
});
});

View File

@@ -59,8 +59,9 @@
<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="variable_map_test.js"></script>
<script src="variable_model_test.js"></script>
<script src="variables_test.js"></script>
<script src="xml_procedures_test.js"></script>
<script src="xml_test.js"></script>

View File

@@ -6,7 +6,7 @@
/* exported assertEquals, assertNotEquals, assertArrayEquals, assertTrue, assertFalse,
assertNull, assertNotNull, assertNotNullNorUndefined, assert,
isEqualArrays, assertUndefined, assertNotUndefined,
isEqualArrays, assertUndefined, assertNotUndefined, assertVariableValues,
defineRowBlock, defineStackBlock, defineStatementBlock, createTestBlock */
function _argumentsIncludeComments(expectedNumberOfNonCommentArgs, args) {
return args.length == expectedNumberOfNonCommentArgs + 1;
@@ -141,6 +141,22 @@ function assertArrayEquals() {
isEqualArrays(var1, var2);
}
/**
* Check if a variable with the given values exists.
* @param {Blockly.Workspace|Blockly.VariableMap} container The workspace or
* variableMap the checked variable belongs to.
* @param {!string} name The expected name of the variable.
* @param {!string} type The expected type of the variable.
* @param {!string} id The expected id of the variable.
*/
function assertVariableValues(container, name, type, id) {
var variable = container.getVariableById(id);
assertNotNull(variable);
assertEquals(name, variable.name);
assertEquals(type, variable.type);
assertEquals(id, variable.getId());
}
function defineStackBlock() {
Blockly.defineBlocksWithJsonArray([{
"type": "stack_block",
@@ -191,4 +207,3 @@ function createTestBlock() {
}
};
}

View File

@@ -0,0 +1,250 @@
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
suite('Variable Map', function() {
setup(function() {
this.workspace = new Blockly.Workspace();
this.variableMap = new Blockly.VariableMap(this.workspace);
});
teardown(function() {
this.workspace.dispose();
sinon.restore();
});
suite('createVariable', function() {
test('Trivial', function() {
this.variableMap.createVariable('name1', 'type1', 'id1');
assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
});
test('Already exists', function() {
// Expect that when the variable already exists, the variableMap_ is unchanged.
this.variableMap.createVariable('name1', 'type1', 'id1');
// Assert there is only one variable in the this.variableMap.
var keys = Object.keys(this.variableMap.variableMap_);
chai.assert.equal(keys.length, 1);
var varMapLength = this.variableMap.variableMap_[keys[0]].length;
chai.assert.equal(varMapLength, 1);
this.variableMap.createVariable('name1', 'type1');
assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
// Check that the size of the variableMap_ did not change.
keys = Object.keys(this.variableMap.variableMap_);
chai.assert.equal(keys.length, 1);
varMapLength = this.variableMap.variableMap_[keys[0]].length;
chai.assert.equal(varMapLength, 1);
});
test('Name already exists', function() {
// Expect that when a variable with the same name but a different type already
// exists, the new variable is created.
this.variableMap.createVariable('name1', 'type1', 'id1');
// Assert there is only one variable in the this.variableMap.
var keys = Object.keys(this.variableMap.variableMap_);
chai.assert.equal(keys.length, 1);
var varMapLength = this.variableMap.variableMap_[keys[0]].length;
chai.assert.equal(varMapLength, 1);
this.variableMap.createVariable('name1', 'type2', 'id2');
assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.variableMap, 'name1', 'type2', 'id2');
// Check that the size of the variableMap_ did change.
keys = Object.keys(this.variableMap.variableMap_);
chai.assert.equal(keys.length, 2);
});
test('Null type', function() {
this.variableMap.createVariable('name1', null, 'id1');
assertVariableValues(this.variableMap, 'name1', '', 'id1');
});
test('Undefined type', function() {
this.variableMap.createVariable('name2', undefined, 'id2');
assertVariableValues(this.variableMap, 'name2', '', 'id2');
});
test('Null id', function() {
sinon.stub(Blockly.utils, "genUid").returns('1');
this.variableMap.createVariable('name1', 'type1', null);
assertVariableValues(this.variableMap, 'name1', 'type1', '1');
});
test('Undefined id', function() {
sinon.stub(Blockly.utils, "genUid").returns('1');
this.variableMap.createVariable('name1', 'type1', undefined);
assertVariableValues(this.variableMap, 'name1', 'type1', '1');
});
test('Two variables same type', function() {
this.variableMap.createVariable('name1', 'type1', 'id1');
this.variableMap.createVariable('name2', 'type1', 'id2');
assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.variableMap, 'name2', 'type1', 'id2');
});
test('Two variables same name', function() {
this.variableMap.createVariable('name1', 'type1', 'id1');
this.variableMap.createVariable('name1', 'type2', 'id2');
assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.variableMap, 'name1', 'type2', 'id2');
});
suite('Error cases', function() {
test('Id already exists', function() {
this.variableMap.createVariable('name1', 'type1', 'id1');
var variableMap = this.variableMap;
chai.assert.throws(function() {
variableMap.createVariable('name2', 'type2', 'id1');
}, /"id1".*in use/);
assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
});
test('Mismatched id', function() {
this.variableMap.createVariable('name1', 'type1', 'id1');
var variableMap = this.variableMap;
chai.assert.throws(function() {
variableMap.createVariable('name1', 'type1', 'id2');
}, /"name1".*in use/);
assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
});
test('Mismatched type', function() {
this.variableMap.createVariable('name1', 'type1', 'id1');
var variableMap = this.variableMap;
chai.assert.throws(function() {
variableMap.createVariable('name1', 'type2', 'id1');
});
assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
chai.assert.isNull(this.variableMap.getVariableById('id2'));
});
});
});
suite('getVariable', function() {
test('By name and type', function() {
var var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
var var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
var var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
var result1 = this.variableMap.getVariable('name1', 'type1');
var result2 = this.variableMap.getVariable('name2', 'type1');
var result3 = this.variableMap.getVariable('name3', 'type2');
// Searching by name + type is correct.
chai.assert.equal(result1, var1);
chai.assert.equal(result2, var2);
chai.assert.equal(result3, var3);
// Searching only by name defaults to the '' type.
chai.assert.isNull(this.variableMap.getVariable('name1'));
chai.assert.isNull(this.variableMap.getVariable('name2'));
chai.assert.isNull(this.variableMap.getVariable('name3'));
});
test('Not found', function() {
var result = this.variableMap.getVariable('name1');
chai.assert.isNull(result);
});
});
suite('getVariableById', function() {
test('Trivial', function() {
var var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
var var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
var var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
var result1 = this.variableMap.getVariableById('id1');
var result2 = this.variableMap.getVariableById('id2');
var result3 = this.variableMap.getVariableById('id3');
chai.assert.equal(result1, var1);
chai.assert.equal(result2, var2);
chai.assert.equal(result3, var3);
});
test('Not found', function() {
var result = this.variableMap.getVariableById('id1');
chai.assert.isNull(result);
});
});
suite('getVariableTypes', function() {
test('Trivial', function() {
this.variableMap.createVariable('name1', 'type1', 'id1');
this.variableMap.createVariable('name2', 'type1', 'id2');
this.variableMap.createVariable('name3', 'type2', 'id3');
this.variableMap.createVariable('name4', 'type3', 'id4');
var resultArray = this.variableMap.getVariableTypes();
// The empty string is always an option.
assertArrayEquals(resultArray, ['type1', 'type2', 'type3', '']);
});
test('None', function() {
// The empty string is always an option.
var resultArray = this.variableMap.getVariableTypes();
assertArrayEquals(resultArray, ['']);
});
});
suite('getVariablesOfType', function() {
test('Trivial', function() {
var var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
var var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
this.variableMap.createVariable('name3', 'type2', 'id3');
this.variableMap.createVariable('name4', 'type3', 'id4');
var resultArray1 = this.variableMap.getVariablesOfType('type1');
var resultArray2 = this.variableMap.getVariablesOfType('type5');
assertArrayEquals(resultArray1, [var1, var2]);
assertArrayEquals(resultArray2, []);
});
test('Null', function() {
var var1 = this.variableMap.createVariable('name1', '', 'id1');
var var2 = this.variableMap.createVariable('name2', '', 'id2');
var var3 = this.variableMap.createVariable('name3', '', 'id3');
this.variableMap.createVariable('name4', 'type1', 'id4');
var resultArray = this.variableMap.getVariablesOfType(null);
assertArrayEquals(resultArray, [var1, var2, var3]);
});
test('Empty string', function() {
var var1 = this.variableMap.createVariable('name1', null, 'id1');
var var2 = this.variableMap.createVariable('name2', null, 'id2');
var resultArray = this.variableMap.getVariablesOfType('');
assertArrayEquals(resultArray, [var1, var2]);
});
test('Deleted', function() {
var variable = this.variableMap.createVariable('name1', null, 'id1');
this.variableMap.deleteVariable(variable);
var resultArray = this.variableMap.getVariablesOfType('');
assertArrayEquals(resultArray, []);
});
test('Does not exist', function() {
var resultArray = this.variableMap.getVariablesOfType('type1');
assertArrayEquals(resultArray, []);
});
});
suite('getAllVariables', function() {
test('Trivial', function() {
var var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
var var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
var var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
var resultArray = this.variableMap.getAllVariables();
assertArrayEquals(resultArray, [var1, var2, var3]);
});
test('None', function() {
var resultArray = this.variableMap.getAllVariables();
assertArrayEquals(resultArray, []);
});
});
});