Created separate file for VariableMap

This commit is contained in:
marisaleung 2017-05-03 13:42:07 -07:00
commit 12ea998155
6 changed files with 2112 additions and 1932 deletions

File diff suppressed because one or more lines are too long

223
core/variable_map.js Normal file
View file

@ -0,0 +1,223 @@
/**
* @license
* Visual Blocks Editor
*
* Copyright 2017 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Object representing a map of variables and their types.
* @author marisaleung@google.com (Marisa Leung)
*/
'use strict';
goog.provide('Blockly.VariableMap');
/**
* Class for a variable map. This contains a dictionary data structure with
* variable types as keys and lists of variables as values. The list of
* variables are the type indicated by the key.
* @constructor
*/
Blockly.VariableMap = function() {
/**
* @type {!Object<string, !Array.<Blockly.VariableModel>>}
* A map from variable type to list of variable names. The lists contain all
* of the named variables in the workspace, including variables
* that are not currently in use.
* @private
*/
this.variableMap_ = {};
};
/**
* Clear the variable map.
*/
Blockly.VariableMap.prototype.clear = function() {
this.variableMap_ = new Object(null);
};
/**
* Rename the given variable by updating its name in the variable map.
* TODO: #468
* @param {?Blockly.VariableModel} variable Variable to rename.
* @param {string} newName New variable name.
*/
Blockly.VariableMap.prototype.renameVariable = function(variable, newName) {
var newVariable = this.getVariable(newName);
var variableIndex = -1;
var newVariableIndex = -1;
var type = '';
if (variable || newVariable) {
type = (variable || newVariable).type;
}
var variableList = this.getVariablesOfType(type);
if (variable) {
variableIndex = variableList.indexOf(variable);
}
if (newVariable){ // see if I can get rid of newVariable dependency
newVariableIndex = variableList.indexOf(newVariable);
}
if (variableIndex == -1 && newVariableIndex == -1) {
this.createVariable(newName, '');
console.log('Tried to rename an non-existent variable.');
} else if (variableIndex == newVariableIndex ||
variableIndex != -1 && newVariableIndex == -1) {
// Only changing case, or renaming to a completely novel name.
this.variableMap_[type][variableIndex].name = newName;
} else if (variableIndex != -1 && newVariableIndex != -1) {
// Renaming one existing variable to another existing variable.
// The case might have changed, so we update the destination ID.
this.variableMap_[type][newVariableIndex].name = newName;
this.variableMap_[type].splice(variableIndex, 1);
}
};
/**
* Create a variable with a given name, optional type, and optional id.
* @param {!string} name The name of the variable. This must be unique across
* variables and procedures.
* @param {?string} opt_type The type of the variable like 'int' or 'string'.
* Does not need to be unique. Field_variable can filter variables based on
* their type. This will default to '' which is a specific type.
* @param {?string} opt_id The unique id of the variable. This will default to
* a UUID.
* @return {?Blockly.VariableModel} The newly created variable.
*/
Blockly.VariableMap.prototype.createVariable = function(name, opt_type, opt_id) {
var variable = this.getVariable(name);
if (variable) {
if (opt_type && variable.type != opt_type) {
throw Error('Variable "' + name + '" is already in use and its type is "'
+ variable.type + '" which conflicts with the passed in ' +
'type, "' + opt_type + '".');
}
if (opt_id && variable.getId() != opt_id) {
throw Error('Variable "' + name + '" is already in use and its id is "'
+ variable.getId() + '" which conflicts with the passed in ' +
'id, "' + opt_id + '".');
}
// The variable already exists and has the same id and type.
return variable;
}
if (opt_id && this.getVariableById(opt_id)) {
throw Error('Variable id, "' + opt_id + '", is already in use.');
}
opt_id = opt_id || Blockly.utils.genUid();
opt_type = opt_type || '';
variable = new Blockly.VariableModel(name, opt_type, opt_id);
// If opt_type is not a key, create a new list.
if (!this.variableMap_[opt_type]) {
this.variableMap_[opt_type] = [variable];
} else {
// Else append the variable to the preexisting list.
this.variableMap_[opt_type].push(variable);
}
return variable;
};
/**
* Delete a variable.
* @param {Blockly.VariableModel} variable Variable to delete.
*/
Blockly.VariableMap.prototype.deleteVariable = function(variable) {
var variableList = this.variableMap_[variable.type];
for (var i = 0, tempVar; tempVar = variableList[i]; i++) {
if (tempVar.getId() == variable.getId()) {
variableList.splice(i, 1);
return;
}
}
};
/**
* Find the variable by the given name and return it. Return null if it is not
* found.
* @param {!string} name The name to check for.
* @return {?Blockly.VariableModel} The variable with the given name, or null if
* it was not found.
*/
Blockly.VariableMap.prototype.getVariable = function(name) {
var keys = Object.keys(this.variableMap_);
for (var i = 0; i < keys.length; i++ ) {
var key = keys[i];
for (var j = 0, variable; variable = this.variableMap_[key][j]; j++) {
if (Blockly.Names.equals(variable.name, name)) {
return variable;
}
}
}
return null;
};
/**
* Find the variable by the given id and return it. Return null if it is not
* found.
* @param {!string} id The id to check for.
* @return {?Blockly.VariableModel} The variable with the given id.
*/
Blockly.VariableMap.prototype.getVariableById = function(id) {
var keys = Object.keys(this.variableMap_);
for (var i = 0; i < keys.length; i++ ) {
var key = keys[i];
for (var j = 0, variable; variable = this.variableMap_[key][j]; j++) {
if (variable.getId() == id) {
return variable;
}
}
}
return null;
};
/**
* Get a list containing all of the variables of a specified type. If type is
* null, return list of variables with empty string type.
* @param {?string} type Type of the variables to find.
* @return {Array.<Blockly.VariableModel>} The sought after variables of the
* passed in type. An empty array if none are found.
*/
Blockly.VariableMap.prototype.getVariablesOfType = function(type) {
type = type || '';
var variable_list = this.variableMap_[type];
if (variable_list) {
return variable_list;
}
return [];
};
/**
* Return all variable types.
* @return {!Array.<string>} List of variable types.
*/
Blockly.VariableMap.prototype.getVariableTypes = function() {
return Object.keys(this.variableMap_);
};
/**
* Return all variables of all types.
* @return {!Array.<Blockly.VariableModel>} List of variable models.
*/
Blockly.VariableMap.prototype.getAllVariables = function() {
var all_variables = [];
var keys = Object.keys(this.variableMap_);
for (var i = 0; i < keys.length; i++ ) {
all_variables = all_variables.concat(this.variableMap_[keys[i]]);
}
return all_variables;
};

View file

@ -76,13 +76,13 @@ Blockly.Workspace = function(opt_options) {
this.blockDB_ = Object.create(null);
/**
* @type {!Object<string, !Array.<Blockly.VariableModel>>}
* @type {!Blockly.VariableMap}
* A map from variable type to list of variable names. The lists contain all
* of the named variables in the workspace, including variables
* that are not currently in use.
* @private
*/
this.variableMap_ = Object.create(null);
this.variableMap_ = new Blockly.VariableMap();
};
/**
@ -195,7 +195,7 @@ Blockly.Workspace.prototype.clear = function() {
if (!existingGroup) {
Blockly.Events.setGroup(false);
}
this.variableMap_ = Object.create(null);
this.variableMap_.clear();
};
/**
@ -224,7 +224,7 @@ Blockly.Workspace.prototype.updateVariableStore = function(clear) {
}
}
if (clear) {
this.variableMap_ = Object.create(null);
this.variableMap_.clear();
}
// Update the list in place so that the flyout's references stay correct.
for (var i = 0, varDict; varDict = varList[i]; i++) {
@ -244,9 +244,6 @@ Blockly.Workspace.prototype.updateVariableStore = function(clear) {
Blockly.Workspace.prototype.renameVariableInternal_ = function(variable, newName) {
var newVariable = this.getVariable(newName);
var oldCase;
var variableIndex = -1;
var newVariableIndex = -1;
var type;
// If they are different types, throw an error.
if (variable && newVariable && variable.type != newVariable.type) {
@ -254,23 +251,9 @@ Blockly.Workspace.prototype.renameVariableInternal_ = function(variable, newName
'" and variable "' + newName + '" is type "' + newVariable.type +
'". Both must be the same type.');
}
if (variable || newVariable) {
type = (variable || newVariable).type;
}
else {
type = '';
}
var variableList = this.getVariablesOfType(type);
if (variable) {
variableIndex = variableList.indexOf(variable);
}
if (newVariable){
newVariableIndex = variableList.indexOf(newVariable);
}
// Find if newVariable case is different.
if (newVariableIndex != -1 && newVariable.name != newName) {
if (newVariable && newVariable.name != newName) {
oldCase = newVariable.name;
}
@ -285,20 +268,7 @@ Blockly.Workspace.prototype.renameVariableInternal_ = function(variable, newName
}
Blockly.Events.setGroup(false);
if (variableIndex == -1 && newVariableIndex == -1) {
this.createVariable(newName, '');
console.log('Tried to rename an non-existent variable.');
}
else if (variableIndex == newVariableIndex ||
variableIndex != -1 && newVariableIndex == -1) {
// Only changing case, or renaming to a completely novel name.
this.variableMap_[type][variableIndex].name = newName;
} else if (variableIndex != -1 && newVariableIndex != -1) {
// Renaming one existing variable to another existing variable.
// The case might have changed, so we update the destination ID.
this.variableMap_[type][newVariableIndex].name = newName;
this.variableMap_[type].splice(variableIndex, 1);
}
this.variableMap_.renameVariable(variable, newName);
};
@ -335,37 +305,10 @@ Blockly.Workspace.prototype.renameVariableById = function(id, newName) {
* their type. This will default to '' which is a specific type.
* @param {?string} opt_id The unique id of the variable. This will default to
* a UUID.
* @return {?Blockly.VariableModel} The newly created variable.
*/
Blockly.Workspace.prototype.createVariable = function(name, opt_type, opt_id) {
var variable = this.getVariable(name);
if (variable) {
if (opt_type && variable.type != opt_type) {
throw Error('Variable "' + name + '" is already in use and its type is "'
+ variable.type + '" which conflicts with the passed in ' +
'type, "' + opt_type + '".');
}
if (opt_id && variable.getId() != opt_id) {
throw Error('Variable "' + name + '" is already in use and its id is "'
+ variable.getId() + '" which conflicts with the passed in ' +
'id, "' + opt_id + '".');
}
return;
}
if (opt_id && this.getVariableById(opt_id)) {
throw Error('Variable id, "' + opt_id + '", is already in use.');
}
opt_id = opt_id || Blockly.utils.genUid();
opt_type = opt_type || '';
variable = new Blockly.VariableModel(name, opt_type, opt_id);
// If opt_type is not a key, create a new list.
if (!this.variableMap_[opt_type]) {
this.variableMap_[opt_type] = [variable];
}
// Else append the variable to the preexisting list.
else {
this.variableMap_[opt_type].push(variable);
}
return this.variableMap_.createVariable(name, opt_type, opt_id);
};
/**
@ -456,14 +399,7 @@ Blockly.Workspace.prototype.deleteVariableInternal_ = function(variable) {
}
Blockly.Events.setGroup(false);
var type = variable.type;
for (var i = 0, tempVar; tempVar = this.variableMap_[type][i]; i++) {
if (Blockly.Names.equals(tempVar.name, variable.name)) {
delete this.variableMap_[type][i];
this.variableMap_[type].splice(i, 1);
return;
}
}
this.variableMap_.deleteVariable(variable);
};
/**
@ -487,16 +423,7 @@ Blockly.Workspace.prototype.variableIndexOf = function(name) {
* @return {?Blockly.VariableModel} the variable with the given name.
*/
Blockly.Workspace.prototype.getVariable = function(name) {
var keys = Object.keys(this.variableMap_);
for (var i = 0; i < keys.length; i++ ) {
var key = keys[i];
for (var j = 0, variable; variable = this.variableMap_[key][j]; j++) {
if (Blockly.Names.equals(variable.name, name)) {
return variable;
}
}
}
return null;
return this.variableMap_.getVariable(name);
};
/**
@ -506,14 +433,7 @@ Blockly.Workspace.prototype.getVariable = function(name) {
* @return {?Blockly.VariableModel} The variable with the given id.
*/
Blockly.Workspace.prototype.getVariableById = function(id) {
for (var key of Object.keys(this.variableMap_)) {
for (var variable of this.variableMap_[key]) {
if (variable.getId() == id) {
return variable;
}
}
}
return null;
return this.variableMap_.getVariableById(id);
};
/**
@ -659,12 +579,7 @@ Blockly.Workspace.prototype.allInputsFilled = function(opt_shadowBlocksAreFilled
* passed in type. An empty array if none are found.
*/
Blockly.Workspace.prototype.getVariablesOfType = function(type) {
type = type || '';
var variable_list = this.variableMap_[type];
if (variable_list) {
return variable_list;
}
return [];
return this.variableMap_.getVariablesOfType(type);
};
/**
@ -672,7 +587,7 @@ Blockly.Workspace.prototype.getVariablesOfType = function(type) {
* @return {!Array.<string>} List of variable types.
*/
Blockly.Workspace.prototype.getVariableTypes = function() {
return Object.keys(this.variableMap_);
return this.variableMap_.getVariableTypes();
};
/**
@ -680,12 +595,7 @@ Blockly.Workspace.prototype.getVariableTypes = function() {
* @return {!Array.<Blockly.VariableModel>} List of variable models.
*/
Blockly.Workspace.prototype.getAllVariables = function() {
var all_variables = [];
var keys = Object.keys(this.variableMap_);
for (var i = 0; i < keys.length; i++ ) {
all_variables = all_variables.concat(this.variableMap_[keys[i]]);
}
return all_variables;
return this.variableMap_.getAllVariables();
};
/**

View file

@ -21,5 +21,6 @@
<script src="xml_test.js"></script>
<script src="json_test.js"></script>
<script src="variable_model_test.js"></script>
<script src="variable_map_test.js"></script>
</body>
</html>

View file

@ -0,0 +1,282 @@
/**
* @license
* Blockly Tests
*
* Copyright 2017 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
goog.require('goog.testing');
goog.require('goog.testing.MockControl');
var variable_map;
var mockControl_;
function variableMapTest_setUp() {
variable_map = new Blockly.VariableMap();
mockControl_ = new goog.testing.MockControl();
}
function variableMapTest_tearDown() {
mockControl_.$tearDown();
variable_map = null;
}
/**
* Check if a variable with the given values exists.
* @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 variableMapTest_checkVariableValues(name, type, id) {
var variable = variable_map.getVariable(name);
assertNotUndefined(variable);
assertEquals(name, variable.name);
assertEquals(type, variable.type);
assertEquals(id, variable.getId());
}
function test_getVariable_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.getVariable('name1');
var result_2 = variable_map.getVariable('name2');
var result_3 = variable_map.getVariable('name3');
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
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');
variableMapTest_checkVariableValues('name1', 'type1', 'id1')
variableMapTest_tearDown();
}
function test_createVariableAlreadyExists() {
// Expect that when the variable already exists, the variableMap_ is unchanged.
variableMapTest_setUp();
var var_1 = 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');
variableMapTest_checkVariableValues('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_createVariableNullAndUndefinedType() {
variableMapTest_setUp();
variable_map.createVariable('name1', null, 'id1');
variable_map.createVariable('name2', undefined, 'id2');
variableMapTest_checkVariableValues('name1', '', 'id1');
variableMapTest_checkVariableValues('name2', '', 'id2');
variableMapTest_tearDown();
}
function test_createVariableNullId() {
variableMapTest_setUp();
var mockGenUid = setUpMockMethod(Blockly.utils, 'genUid', null, '1');
try {
variable_map.createVariable('name1', 'type1', null);
mockGenUid.$verify();
variableMapTest_checkVariableValues('name1', 'type1', '1');
}
finally {
variableMapTest_tearDown();
}
}
function test_createVariableUndefinedId() {
variableMapTest_setUp();
var mockGenUid = setUpMockMethod(Blockly.utils, 'genUid', null, '1');
try {
variable_map.createVariable('name1', 'type1', undefined);
mockGenUid.$verify();
variableMapTest_checkVariableValues('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');
variableMapTest_checkVariableValues('name1', 'type1', 'id1');
variableMapTest_checkVariableValues('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');
this.isEqualArrays([var_1, var_2], result_array_1);
this.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);
this.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('');
this.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('');
this.isEqualArrays([], result_array);
variableMapTest_tearDown();
}
function test_getVariablesOfType_DoesNotExist() {
variableMapTest_setUp();
var result_array = variable_map.getVariablesOfType('type1');
this.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();
this.isEqualArrays(['type1', 'type2', 'type3'], result_array);
variableMapTest_tearDown();
}
function test_getVariableTypes_None() {
variableMapTest_setUp();
var result_array = variable_map.getVariableTypes();
this.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();
this.isEqualArrays([var_1, var_2, var_3], result_array);
variableMapTest_tearDown();
}
function test_getAllVariables_None() {
variableMapTest_setUp();
var result_array = variable_map.getAllVariables();
this.isEqualArrays([], result_array);
variableMapTest_tearDown();
}

View file

@ -89,7 +89,7 @@ function isEqualArrays(array1, array2) {
* @param {!string} type The expected type of the variable.
* @param {!string} id The expected id of the variable.
*/
function checkVariableValues(name, type, id) {
function workspaceTest_checkVariableValues(name, type, id) {
var variable = workspace.getVariable(name);
assertNotUndefined(variable);
assertEquals(name, variable.name);
@ -97,18 +97,6 @@ function checkVariableValues(name, type, id) {
assertEquals(id, variable.getId());
}
/**
* Create a variable with the specified parameters and return it.
* @param {!string} name The name of the variable.
* @param {!string} opt_type The type of the variable.
* @param {!string} opt_id The id of the variable.
* @return {!Blockly.VariableModel} The created variable.
*/
function createAndGetVariable(name, opt_type, opt_id) {
workspace.createVariable(name, opt_type, opt_id);
return workspace.getVariable(name);
}
/**
* Creates a controlled MethodMock. Set the expected return values. Set the
* method to replay.
@ -231,154 +219,9 @@ function test_getBlockById() {
}
}
function test_getVariable_Trivial() {
workspaceTest_setUp();
var var_1 = this.createAndGetVariable('name1', 'type1', 'id1');
var var_2 = this.createAndGetVariable('name2', 'type1', 'id2');
var var_3 = this.createAndGetVariable('name3', 'type2', 'id3');
var result_1 = workspace.getVariable('name1');
var result_2 = workspace.getVariable('name2');
var result_3 = workspace.getVariable('name3');
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
workspaceTest_tearDown();
}
function test_getVariable_NotFound() {
workspaceTest_setUp();
var result = workspace.getVariable('name1');
assertNull(result);
workspaceTest_tearDown();
}
function test_getVariableById_Trivial() {
workspaceTest_setUp();
var var_1 = this.createAndGetVariable('name1', 'type1', 'id1');
var var_2 = this.createAndGetVariable('name2', 'type1', 'id2');
var var_3 = this.createAndGetVariable('name3', 'type2', 'id3');
var result_1 = workspace.getVariableById('id1');
var result_2 = workspace.getVariableById('id2');
var result_3 = workspace.getVariableById('id3');
assertEquals(var_1, result_1);
assertEquals(var_2, result_2);
assertEquals(var_3, result_3);
workspaceTest_tearDown();
}
function test_getVariableById_NotFound() {
workspaceTest_setUp();
var result = workspace.getVariableById('id1');
assertNull(result);
workspaceTest_tearDown();
}
function test_createVariable_Trivial() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
checkVariableValues('name1', 'type1', 'id1')
workspaceTest_tearDown();
}
function test_createVariable_AlreadyExists() {
// Expect that when the variable already exists, the variableMap_ is unchanged.
workspaceTest_setUp();
var var_1 = this.createAndGetVariable('name1', 'type1', 'id1');
// Assert there is only one variable in the workspace.
var keys = Object.keys(workspace.variableMap_);
assertEquals(1, keys.length);
assertEquals(1, workspace.variableMap_[keys[0]].length);
workspace.createVariable('name1');
checkVariableValues('name1', 'type1', 'id1');
// Check that the size of the variableMap_ did not change.
assertEquals(1, keys.length);
var varMapLength = workspace.variableMap_[keys[0]].length;
assertEquals(1, varMapLength);
workspaceTest_tearDown();
}
function test_createVariable_NullAndUndefinedType() {
workspaceTest_setUp();
workspace.createVariable('name1', null, 'id1');
workspace.createVariable('name2', undefined, 'id2');
checkVariableValues('name1', '', 'id1');
checkVariableValues('name2', '', 'id2');
workspaceTest_tearDown();
}
function test_createVariable_NullId() {
workspaceTest_setUp();
var mockGenUid = setUpMockMethod(Blockly.utils, 'genUid', null, '1');
try {
workspace.createVariable('name1', 'type1', null);
mockGenUid.$verify();
checkVariableValues('name1', 'type1', '1');
}
finally {
workspaceTest_tearDown();
}
}
function test_createVariable_UndefinedId() {
workspaceTest_setUp();
var mockGenUid = setUpMockMethod(Blockly.utils, 'genUid', null, '1');
try {
workspace.createVariable('name1', 'type1', undefined);
mockGenUid.$verify();
checkVariableValues('name1', 'type1', '1');
}
finally {
workspaceTest_tearDown();
}
}
function test_createVariable_IdAlreadyExists() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
try {
workspace.createVariable('name2', 'type2', 'id1');
fail();
} catch (e) {
// expected
}
workspaceTest_tearDown();
}
function test_createVariable_MismatchedIdAndType() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
try {
workspace.createVariable('name1', 'type2', 'id1');
fail();
} catch (e) {
// expected
}
try {
workspace.createVariable('name1', 'type1', 'id2');
fail();
} catch (e) {
// expected
}
workspaceTest_tearDown();
}
function test_createVariable_TwoSameTypes() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type1', 'id2');
checkVariableValues('name1', 'type1', 'id1');
checkVariableValues('name2', 'type1', 'id2');
workspaceTest_tearDown();
}
function test_deleteVariable_InternalTrivial() {
workspaceTest_setUpWithMockBlocks()
var var_1 = createAndGetVariable('name1', 'type1', 'id1');
var var_1 = workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type2', 'id2');
createMockBlock('name1');
createMockBlock('name1');
@ -388,7 +231,7 @@ function test_deleteVariable_InternalTrivial() {
var variable = workspace.getVariable('name1');
var block_var_name = workspace.topBlocks_[0].getVars()[0];
assertNull(variable);
checkVariableValues('name2', 'type2', 'id2');
workspaceTest_checkVariableValues('name2', 'type2', 'id2');
assertEquals('name2', block_var_name);
workspaceTest_tearDownWithMockBlocks();
}
@ -405,8 +248,8 @@ function test_updateVariableStore_TrivialNoClear() {
try {
workspace.updateVariableStore();
mockAllUsedVariables.$verify();
checkVariableValues('name1', 'type1', 'id1');
checkVariableValues('name2', 'type2', 'id2');
workspaceTest_checkVariableValues('name1', 'type1', 'id1');
workspaceTest_checkVariableValues('name2', 'type2', 'id2');
}
finally {
workspaceTest_tearDown();
@ -421,7 +264,7 @@ function test_updateVariableStore_NameNotInvariableMap_NoClear() {
try {
workspace.updateVariableStore();
mockControl_.$verifyAll();
checkVariableValues('name1', '', '1');
workspaceTest_checkVariableValues('name1', '', '1');
}
finally {
workspaceTest_tearDown();
@ -438,8 +281,8 @@ function test_updateVariableStore_ClearAndAllInUse() {
try {
workspace.updateVariableStore(true);
mockAllUsedVariables.$verify();
checkVariableValues('name1', 'type1', 'id1');
checkVariableValues('name2', 'type2', 'id2');
workspaceTest_checkVariableValues('name1', 'type1', 'id1');
workspaceTest_checkVariableValues('name2', 'type2', 'id2');
}
finally {
workspaceTest_tearDown();
@ -456,7 +299,7 @@ function test_updateVariableStore_ClearAndOneInUse() {
try {
workspace.updateVariableStore(true);
mockAllUsedVariables.$verify();
checkVariableValues('name1', 'type1', 'id1');
workspaceTest_checkVariableValues('name1', 'type1', 'id1');
var variabe = workspace.getVariable('name2');
assertNull(variable);
}
@ -476,7 +319,7 @@ function test_addTopBlock_TrivialFlyoutIsTrue() {
try {
workspace.addTopBlock(block);
mockControl_.$verifyAll();
checkVariableValues('name1', '', '1');
workspaceTest_checkVariableValues('name1', '', '1');
}
finally {
workspaceTest_tearDownWithMockBlocks();
@ -496,7 +339,7 @@ function test_clear_Trivial() {
workspace.clear();
mockControl_.$verifyAll();
var topBlocks_length = workspace.topBlocks_.length;
var varMapLength = Object.keys(workspace.variableMap_).length;
var varMapLength = Object.keys(workspace.variableMap_.variableMap_).length;
assertEquals(0, topBlocks_length);
assertEquals(0, varMapLength);
}
@ -516,7 +359,7 @@ function test_clear_NoVariables() {
workspace.clear();
mockSetGroup.$verify();
var topBlocks_length = workspace.topBlocks_.length;
var varMapLength = Object.keys(workspace.variableMap_).length;
var varMapLength = Object.keys(workspace.variableMap_.variableMap_).length;
assertEquals(0, topBlocks_length);
assertEquals(0, varMapLength);
}
@ -541,7 +384,7 @@ function test_renameVariable_NoBlocks() {
try {
workspace.renameVariable(oldName, newName);
mockControl_.$verifyAll();
checkVariableValues('name2', '', '1');
workspaceTest_checkVariableValues('name2', '', '1');
var variable = workspace.getVariable(oldName);
assertNull(variable);
}
@ -557,7 +400,7 @@ function test_renameVariable_SameNameNoBlocks() {
workspace.createVariable(name, 'type1', 'id1');
workspace.renameVariable(name, name);
checkVariableValues(name, 'type1', 'id1');
workspaceTest_checkVariableValues(name, 'type1', 'id1');
workspaceTest_tearDownWithMockBlocks();
}
@ -570,7 +413,7 @@ function test_renameVariable_OnlyOldNameBlockExists() {
createMockBlock(oldName);
workspace.renameVariable(oldName, newName);
checkVariableValues(newName, 'type1', 'id1');
workspaceTest_checkVariableValues(newName, 'type1', 'id1');
var variable = workspace.getVariable(oldName);
var block_var_name = workspace.topBlocks_[0].getVars()[0];
assertNull(variable);
@ -590,7 +433,7 @@ function test_renameVariable_TwoVariablesSameType() {
createMockBlock(newName);
workspace.renameVariable(oldName, newName);
checkVariableValues(newName, 'type1', 'id2');
workspaceTest_checkVariableValues(newName, 'type1', 'id2');
var variable = workspace.getVariable(oldName);
var block_var_name_1 = workspace.topBlocks_[0].getVars()[0];
var block_var_name_2 = workspace.topBlocks_[1].getVars()[0];
@ -616,8 +459,8 @@ function test_renameVariable_TwoVariablesDifferentType() {
} catch (e) {
// expected
}
checkVariableValues(oldName, 'type1', 'id1');
checkVariableValues(newName, 'type2', 'id2');
workspaceTest_checkVariableValues(oldName, 'type1', 'id1');
workspaceTest_checkVariableValues(newName, 'type2', 'id2');
var block_var_name_1 = workspace.topBlocks_[0].getVars()[0];
var block_var_name_2 = workspace.topBlocks_[1].getVars()[0];
assertEquals(oldName, block_var_name_1);
@ -634,7 +477,7 @@ function test_renameVariable_OldCase() {
createMockBlock(oldCase);
workspace.renameVariable(oldCase, newName);
checkVariableValues(newName, 'type1', 'id1');
workspaceTest_checkVariableValues(newName, 'type1', 'id1');
var result_oldCase = workspace.getVariable(oldCase).name
assertNotEquals(oldCase, result_oldCase);
workspaceTest_tearDownWithMockBlocks();
@ -653,7 +496,7 @@ function test_renameVariable_TwoVariablesAndOldCase() {
workspace.renameVariable(oldName, newName);
checkVariableValues(newName, 'type1', 'id2');
workspaceTest_checkVariableValues(newName, 'type1', 'id2');
var variable = workspace.getVariable(oldName);
var result_oldCase = workspace.getVariable(oldCase).name;
var block_var_name_1 = workspace.topBlocks_[0].getVars()[0];
@ -679,7 +522,7 @@ function test_renameVariableById_TwoVariablesSameType() {
createMockBlock(newName);
workspace.renameVariableById('id1', newName);
checkVariableValues(newName, 'type1', 'id2');
workspaceTest_checkVariableValues(newName, 'type1', 'id2');
var variable = workspace.getVariable(oldName)
var block_var_name_1 = workspace.topBlocks_[0].getVars()[0];
var block_var_name_2 = workspace.topBlocks_[1].getVars()[0];
@ -697,7 +540,7 @@ function test_deleteVariable_Trivial() {
createMockBlock('name2');
workspace.deleteVariable('name1');
checkVariableValues('name2', 'type1', 'id2');
workspaceTest_checkVariableValues('name2', 'type1', 'id2');
var variable = workspace.getVariable('name1');
var block_var_name = workspace.topBlocks_[0].getVars()[0];
assertNull(variable);
@ -713,94 +556,10 @@ function test_deleteVariableById_Trivial() {
createMockBlock('name2');
workspace.deleteVariableById('id1');
checkVariableValues('name2', 'type1', 'id2');
workspaceTest_checkVariableValues('name2', 'type1', 'id2');
var variable = workspace.getVariable('name1');
var block_var_name = workspace.topBlocks_[0].getVars()[0];
assertNull(variable);
assertEquals('name2', block_var_name);
workspaceTest_tearDownWithMockBlocks();
}
function test_getVariablesOfType_Trivial() {
workspaceTest_setUp();
var var_1 = this.createAndGetVariable('name1', 'type1', 'id1');
var var_2 = this.createAndGetVariable('name2', 'type1', 'id2');
workspace.createVariable('name3', 'type2', 'id3');
workspace.createVariable('name4', 'type3', 'id4');
var result_array_1 = workspace.getVariablesOfType('type1');
var result_array_2 = workspace.getVariablesOfType('type5');
this.isEqualArrays([var_1, var_2], result_array_1);
this.isEqualArrays([], result_array_2);
workspaceTest_tearDown();
}
function test_getVariablesOfType_Null() {
workspaceTest_setUp();
var var_1 = this.createAndGetVariable('name1', '', 'id1');
var var_2 = this.createAndGetVariable('name2', '', 'id2');
var var_3 = this.createAndGetVariable('name3', '', 'id3');
workspace.createVariable('name4', 'type1', 'id4');
var result_array = workspace.getVariablesOfType(null);
this.isEqualArrays([var_1, var_2, var_3], result_array);
workspaceTest_tearDown();
}
function test_getVariablesOfType_EmptyString() {
workspaceTest_setUp();
var var_1 = this.createAndGetVariable('name1', null, 'id1');
var var_2 = this.createAndGetVariable('name2', null, 'id2');
var result_array = workspace.getVariablesOfType('');
this.isEqualArrays([var_1, var_2], result_array);
workspaceTest_tearDown();
}
function test_getVariablesOfType_Deleted() {
workspaceTest_setUp();
workspace.createVariable('name1', null, 'id1');
workspace.deleteVariable('name1');
var result_array = workspace.getVariablesOfType('');
this.isEqualArrays([], result_array);
workspaceTest_tearDown();
}
function test_getVariablesOfType_DoesNotExist() {
workspaceTest_setUp();
var result_array = workspace.getVariablesOfType('type1');
this.isEqualArrays([], result_array);
workspaceTest_tearDown();
}
function test_getVariableTypes_Trivial() {
workspaceTest_setUp();
workspace.createVariable('name1', 'type1', 'id1');
workspace.createVariable('name2', 'type1', 'id2');
workspace.createVariable('name3', 'type2', 'id3');
workspace.createVariable('name4', 'type3', 'id4');
var result_array = workspace.getVariableTypes();
this.isEqualArrays(['type1', 'type2', 'type3'], result_array);
workspaceTest_tearDown();
}
function test_getVariableTypes_None() {
workspaceTest_setUp();
var result_array = workspace.getVariableTypes();
this.isEqualArrays([], result_array);
workspaceTest_tearDown();
}
function test_getAllVariables_Trivial() {
workspaceTest_setUp();
var var_1 = this.createAndGetVariable('name1', 'type1', 'id1');
var var_2 = this.createAndGetVariable('name2', 'type1', 'id2');
var var_3 = this.createAndGetVariable('name3', 'type2', 'id3');
var result_array = workspace.getAllVariables();
this.isEqualArrays([var_1, var_2, var_3], result_array);
workspaceTest_tearDown();
}
function test_getAllVariables_None() {
workspaceTest_setUp();
var result_array = workspace.getAllVariables();
this.isEqualArrays([], result_array);
workspaceTest_tearDown();
}