mirror of
https://github.com/google/blockly.git
synced 2026-01-10 02:17:09 +01:00
refactor: make VariableMap implement IVariableMap. (#8395)
* refactor: make VariableMap implement IVariableMap. * chore: remove unused arrayUtils import. * chore: fix comment on variable map backing store. * chore: Added JSDoc to new VariableMap methods. * chore: Improve test descriptions.
This commit is contained in:
@@ -39,17 +39,17 @@ suite('Variable Map', function () {
|
||||
this.variableMap.createVariable('name1', 'type1', 'id1');
|
||||
|
||||
// Assert there is only one variable in the this.variableMap.
|
||||
let keys = Array.from(this.variableMap.variableMap.keys());
|
||||
let keys = this.variableMap.getTypes();
|
||||
assert.equal(keys.length, 1);
|
||||
let varMapLength = this.variableMap.variableMap.get(keys[0]).length;
|
||||
let varMapLength = this.variableMap.getVariablesOfType(keys[0]).length;
|
||||
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 = Array.from(this.variableMap.variableMap.keys());
|
||||
keys = this.variableMap.getTypes();
|
||||
assert.equal(keys.length, 1);
|
||||
varMapLength = this.variableMap.variableMap.get(keys[0]).length;
|
||||
varMapLength = this.variableMap.getVariablesOfType(keys[0]).length;
|
||||
assert.equal(varMapLength, 1);
|
||||
});
|
||||
|
||||
@@ -59,16 +59,16 @@ suite('Variable Map', function () {
|
||||
this.variableMap.createVariable('name1', 'type1', 'id1');
|
||||
|
||||
// Assert there is only one variable in the this.variableMap.
|
||||
let keys = Array.from(this.variableMap.variableMap.keys());
|
||||
let keys = this.variableMap.getTypes();
|
||||
assert.equal(keys.length, 1);
|
||||
const varMapLength = this.variableMap.variableMap.get(keys[0]).length;
|
||||
const varMapLength = this.variableMap.getVariablesOfType(keys[0]).length;
|
||||
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 = Array.from(this.variableMap.variableMap.keys());
|
||||
keys = this.variableMap.getTypes();
|
||||
assert.equal(keys.length, 2);
|
||||
});
|
||||
|
||||
@@ -246,6 +246,65 @@ suite('Variable Map', function () {
|
||||
});
|
||||
});
|
||||
|
||||
suite(
|
||||
'Using changeVariableType to change the type of a variable',
|
||||
function () {
|
||||
test('updates it to a new non-empty value', function () {
|
||||
const variable = this.variableMap.createVariable(
|
||||
'name1',
|
||||
'type1',
|
||||
'id1',
|
||||
);
|
||||
this.variableMap.changeVariableType(variable, 'type2');
|
||||
const oldTypeVariables = this.variableMap.getVariablesOfType('type1');
|
||||
const newTypeVariables = this.variableMap.getVariablesOfType('type2');
|
||||
assert.deepEqual(oldTypeVariables, []);
|
||||
assert.deepEqual(newTypeVariables, [variable]);
|
||||
assert.equal(variable.getType(), 'type2');
|
||||
});
|
||||
|
||||
test('updates it to a new empty value', function () {
|
||||
const variable = this.variableMap.createVariable(
|
||||
'name1',
|
||||
'type1',
|
||||
'id1',
|
||||
);
|
||||
this.variableMap.changeVariableType(variable, '');
|
||||
const oldTypeVariables = this.variableMap.getVariablesOfType('type1');
|
||||
const newTypeVariables = this.variableMap.getVariablesOfType('');
|
||||
assert.deepEqual(oldTypeVariables, []);
|
||||
assert.deepEqual(newTypeVariables, [variable]);
|
||||
assert.equal(variable.getType(), '');
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
suite('addVariable', function () {
|
||||
test('normally', function () {
|
||||
const variable = new Blockly.VariableModel(this.workspace, 'foo', 'int');
|
||||
assert.isNull(this.variableMap.getVariableById(variable.getId()));
|
||||
this.variableMap.addVariable(variable);
|
||||
assert.equal(
|
||||
this.variableMap.getVariableById(variable.getId()),
|
||||
variable,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
suite('getTypes', function () {
|
||||
test('when map is empty', function () {
|
||||
const types = this.variableMap.getTypes();
|
||||
assert.deepEqual(types, []);
|
||||
});
|
||||
|
||||
test('with various types', function () {
|
||||
this.variableMap.createVariable('name1', 'type1', 'id1');
|
||||
this.variableMap.createVariable('name2', '', 'id2');
|
||||
const types = this.variableMap.getTypes();
|
||||
assert.deepEqual(types, ['type1', '']);
|
||||
});
|
||||
});
|
||||
|
||||
suite('getAllVariables', function () {
|
||||
test('Trivial', function () {
|
||||
const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
|
||||
|
||||
Reference in New Issue
Block a user