refactor(VariableMap): Stop using deprecated wrapper methods (#9340)

* refactor(VariableMap): Stop using deprecated wrapper methods

* fix format

* fix: Apply review suggestions

Co-authored-by: Christopher Allen <cpcallen+github@gmail.com>

* fix: restore blank line

---------

Co-authored-by: Christopher Allen <cpcallen+github@gmail.com>
This commit is contained in:
Tasheena
2025-09-24 11:23:41 -04:00
committed by GitHub
parent bf0043d987
commit a481253bb4
8 changed files with 152 additions and 139 deletions

View File

@@ -726,21 +726,21 @@ const PROCEDURES_MUTATORARGUMENT = {
if (sourceBlock.isInFlyout) { if (sourceBlock.isInFlyout) {
return varName; return varName;
} }
const variableMap = outerWs.getVariableMap();
const model = outerWs.getVariable(varName, ''); const model = variableMap.getVariable(varName, '');
if (model && model.getName() !== varName) { if (model && model.getName() !== varName) {
// Rename the variable (case change) // Rename the variable (case change)
outerWs.renameVariableById(model.getId(), varName); variableMap.renameVariable(model, varName);
} }
if (!model) { if (!model) {
if (this.editingInteractively) { if (this.editingInteractively) {
if (!this.editingVariable) { if (!this.editingVariable) {
this.editingVariable = outerWs.createVariable(varName, ''); this.editingVariable = variableMap.createVariable(varName, '');
} else { } else {
outerWs.renameVariableById(this.editingVariable.getId(), varName); variableMap.renameVariable(this.editingVariable, varName);
} }
} else { } else {
outerWs.createVariable(varName, ''); variableMap.createVariable(varName, '');
} }
} }
return varName; return varName;

View File

@@ -2088,6 +2088,7 @@ suite('Blocks', function () {
], ],
}, },
]); ]);
this.variableMap = this.workspace.getVariableMap();
}); });
teardown(function () { teardown(function () {
eventUtils.enable(); eventUtils.enable();
@@ -2426,13 +2427,14 @@ suite('Blocks', function () {
assertCollapsed(blockA); assertCollapsed(blockA);
}); });
}); });
suite('Renaming Vars', function () { suite('Renaming Vars', function () {
test('Simple Rename', function () { test('Simple Rename', function () {
const blockA = createRenderedBlock(this.workspace, 'variable_block'); const blockA = createRenderedBlock(this.workspace, 'variable_block');
blockA.setCollapsed(true); blockA.setCollapsed(true);
const variable = this.workspace.getVariable('x', ''); const variable = this.workspace.getVariable('x', '');
this.workspace.renameVariableById(variable.getId(), 'y'); this.variableMap.renameVariable(variable, 'y');
this.clock.runAll(); this.clock.runAll();
assertCollapsed(blockA, 'y'); assertCollapsed(blockA, 'y');
@@ -2441,8 +2443,8 @@ suite('Blocks', function () {
const blockA = createRenderedBlock(this.workspace, 'variable_block'); const blockA = createRenderedBlock(this.workspace, 'variable_block');
blockA.setCollapsed(true); blockA.setCollapsed(true);
const variable = this.workspace.createVariable('y'); const variable = this.variableMap.createVariable('y');
this.workspace.renameVariableById(variable.getId(), 'X'); this.variableMap.renameVariable(variable, 'X');
this.clock.runAll(); this.clock.runAll();
assertCollapsed(blockA, 'X'); assertCollapsed(blockA, 'X');

View File

@@ -33,6 +33,7 @@ suite('Procedures', function () {
'preCreatedTypedVarId', 'preCreatedTypedVarId',
); );
defineRowBlock(); defineRowBlock();
this.variableMap = this.workspace.getVariableMap();
}); });
teardown(function () { teardown(function () {
@@ -453,7 +454,7 @@ suite('Procedures', function () {
mutatorIcon.setBubbleVisible(false); mutatorIcon.setBubbleVisible(false);
const variable = this.workspace.getVariable('param1', ''); const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'new name'); this.variableMap.renameVariable(variable, 'new name');
assert.isNotNull( assert.isNotNull(
defBlock.getField('PARAMS'), defBlock.getField('PARAMS'),
@@ -480,7 +481,7 @@ suite('Procedures', function () {
this.clock.runAll(); this.clock.runAll();
const variable = this.workspace.getVariable('param1', ''); const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'new name'); this.variableMap.renameVariable(variable, 'new name');
assert.equal( assert.equal(
paramBlock.getFieldValue('NAME'), paramBlock.getFieldValue('NAME'),
@@ -506,7 +507,7 @@ suite('Procedures', function () {
mutatorIcon.setBubbleVisible(false); mutatorIcon.setBubbleVisible(false);
const variable = this.workspace.getVariable('param1', ''); const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'new name'); this.variableMap.renameVariable(variable, 'new name');
assert.isNotNull( assert.isNotNull(
callBlock.getInput('ARG0'), callBlock.getInput('ARG0'),
@@ -535,7 +536,7 @@ suite('Procedures', function () {
mutatorIcon.setBubbleVisible(false); mutatorIcon.setBubbleVisible(false);
const variable = this.workspace.getVariable('param1', ''); const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar'); this.variableMap.renameVariable(variable, 'preCreatedVar');
assert.isNotNull( assert.isNotNull(
defBlock.getField('PARAMS'), defBlock.getField('PARAMS'),
@@ -562,7 +563,7 @@ suite('Procedures', function () {
this.clock.runAll(); this.clock.runAll();
const variable = this.workspace.getVariable('param1', ''); const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar'); this.variableMap.renameVariable(variable, 'preCreatedVar');
assert.equal( assert.equal(
paramBlock.getFieldValue('NAME'), paramBlock.getFieldValue('NAME'),
@@ -588,7 +589,7 @@ suite('Procedures', function () {
mutatorIcon.setBubbleVisible(false); mutatorIcon.setBubbleVisible(false);
const variable = this.workspace.getVariable('param1', ''); const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar'); this.variableMap.renameVariable(variable, 'preCreatedVar');
assert.isNotNull( assert.isNotNull(
callBlock.getInput('ARG0'), callBlock.getInput('ARG0'),

View File

@@ -32,9 +32,10 @@ suite('Variables', function () {
], ],
}, },
]); ]);
this.workspace.createVariable('foo', 'type1', '1'); this.variableMap = this.workspace.getVariableMap();
this.workspace.createVariable('bar', 'type1', '2'); this.variableMap.createVariable('foo', 'type1', '1');
this.workspace.createVariable('baz', 'type1', '3'); this.variableMap.createVariable('bar', 'type1', '2');
this.variableMap.createVariable('baz', 'type1', '3');
}); });
teardown(function () { teardown(function () {
@@ -116,12 +117,11 @@ suite('Variables', function () {
); );
}); });
}); });
suite('getVariable', function () { suite('getVariable', function () {
test('By ID', function () { test('By ID', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1'); const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2'); const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3'); const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable(this.workspace, 'id1'); const result1 = Blockly.Variables.getVariable(this.workspace, 'id1');
const result2 = Blockly.Variables.getVariable(this.workspace, 'id2'); const result2 = Blockly.Variables.getVariable(this.workspace, 'id2');
const result3 = Blockly.Variables.getVariable(this.workspace, 'id3'); const result3 = Blockly.Variables.getVariable(this.workspace, 'id3');
@@ -132,9 +132,9 @@ suite('Variables', function () {
}); });
test('By name and type', function () { test('By name and type', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1'); const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2'); const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3'); const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable( const result1 = Blockly.Variables.getVariable(
this.workspace, this.workspace,
null, null,
@@ -161,9 +161,9 @@ suite('Variables', function () {
}); });
test('Bad ID with name and type fallback', function () { test('Bad ID with name and type fallback', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1'); const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2'); const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3'); const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable( const result1 = Blockly.Variables.getVariable(
this.workspace, this.workspace,
'badId', 'badId',

View File

@@ -488,13 +488,15 @@ suite('Variable Fields', function () {
this.variableField = this.variableBlock.getField('VAR'); this.variableField = this.variableBlock.getField('VAR');
}); });
test('Rename & Keep Old ID', function () { test('Rename & Keep Old ID', function () {
this.workspace.renameVariableById('id1', 'name2'); const variableMap = this.workspace.getVariableMap();
variableMap.renameVariable(variableMap.getVariableById('id1'), 'name2');
assert.equal(this.variableField.getText(), 'name2'); assert.equal(this.variableField.getText(), 'name2');
assert.equal(this.variableField.getValue(), 'id1'); assert.equal(this.variableField.getValue(), 'id1');
}); });
test('Rename & Get New ID', function () { test('Rename & Get New ID', function () {
this.workspace.createVariable('name2', null, 'id2'); const variableMap = this.workspace.getVariableMap();
this.workspace.renameVariableById('id1', 'name2'); variableMap.createVariable('name2', null, 'id2');
variableMap.renameVariable(variableMap.getVariableById('id1'), 'name2');
assert.equal(this.variableField.getText(), 'name2'); assert.equal(this.variableField.getText(), 'name2');
assert.equal(this.variableField.getValue(), 'id2'); assert.equal(this.variableField.getValue(), 'id2');
}); });

View File

@@ -33,6 +33,7 @@ suite('JSO Serialization', function () {
defineStatementBlock(); defineStatementBlock();
createGenUidStubWithReturns(new Array(10).fill().map((_, i) => 'id' + i)); createGenUidStubWithReturns(new Array(10).fill().map((_, i) => 'id' + i));
this.variableMap = this.workspace.getVariableMap();
}); });
teardown(function () { teardown(function () {
@@ -834,7 +835,7 @@ suite('JSO Serialization', function () {
suite('Variables', function () { suite('Variables', function () {
test('Without type', function () { test('Without type', function () {
this.workspace.createVariable('testVar', '', 'testId'); this.variableMap.createVariable('testVar', '', 'testId');
const jso = Blockly.serialization.workspaces.save(this.workspace); const jso = Blockly.serialization.workspaces.save(this.workspace);
const variable = jso['variables'][0]; const variable = jso['variables'][0];
assertProperty(variable, 'name', 'testVar'); assertProperty(variable, 'name', 'testVar');
@@ -843,7 +844,7 @@ suite('JSO Serialization', function () {
}); });
test('With type', function () { test('With type', function () {
this.workspace.createVariable('testVar', 'testType', 'testId'); this.variableMap.createVariable('testVar', 'testType', 'testId');
const jso = Blockly.serialization.workspaces.save(this.workspace); const jso = Blockly.serialization.workspaces.save(this.workspace);
const variable = jso['variables'][0]; const variable = jso['variables'][0];
assertProperty(variable, 'name', 'testVar'); assertProperty(variable, 'name', 'testVar');

View File

@@ -25,6 +25,7 @@ export function testAWorkspace() {
], ],
}, },
]); ]);
this.variableMap = this.workspace.getVariableMap();
}); });
teardown(function () { teardown(function () {
@@ -68,13 +69,13 @@ export function testAWorkspace() {
suite('clear', function () { suite('clear', function () {
test('Trivial', function () { test('Trivial', function () {
sinon.stub(eventUtils.TEST_ONLY, 'setGroupInternal').returns(null); sinon.stub(eventUtils.TEST_ONLY, 'setGroupInternal').returns(null);
this.workspace.createVariable('name1', 'type1', 'id1'); this.variableMap.createVariable('name1', 'type1', 'id1');
this.workspace.createVariable('name2', 'type2', 'id2'); this.variableMap.createVariable('name2', 'type2', 'id2');
this.workspace.newBlock(''); this.workspace.newBlock('');
this.workspace.clear(); this.workspace.clear();
assert.equal(this.workspace.getTopBlocks(false).length, 0); assert.equal(this.workspace.getTopBlocks(false).length, 0);
const varMapLength = this.workspace.getVariableMap().variableMap.size; const varMapLength = this.variableMap.variableMap.size;
assert.equal(varMapLength, 0); assert.equal(varMapLength, 0);
}); });
@@ -84,7 +85,7 @@ export function testAWorkspace() {
this.workspace.clear(); this.workspace.clear();
assert.equal(this.workspace.getTopBlocks(false).length, 0); assert.equal(this.workspace.getTopBlocks(false).length, 0);
const varMapLength = this.workspace.getVariableMap().variableMap.size; const varMapLength = this.variableMap.variableMap.size;
assert.equal(varMapLength, 0); assert.equal(varMapLength, 0);
}); });
}); });
@@ -92,8 +93,8 @@ export function testAWorkspace() {
suite('deleteVariable', function () { suite('deleteVariable', function () {
setup(function () { setup(function () {
// Create two variables of different types. // Create two variables of different types.
this.var1 = this.workspace.createVariable('name1', 'type1', 'id1'); this.var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
this.var2 = this.workspace.createVariable('name2', 'type2', 'id2'); this.var2 = this.variableMap.createVariable('name2', 'type2', 'id2');
// Create blocks to refer to both of them. // Create blocks to refer to both of them.
createVarBlocksNoEvents(this.workspace, ['id1', 'id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id1', 'id2']);
}); });
@@ -101,12 +102,12 @@ export function testAWorkspace() {
test('deleteVariableById(id2) one usage', function () { test('deleteVariableById(id2) one usage', function () {
// Deleting variable one usage should not trigger confirm dialog. // Deleting variable one usage should not trigger confirm dialog.
const stub = sinon.stub(window, 'confirm').returns(true); const stub = sinon.stub(window, 'confirm').returns(true);
this.workspace.deleteVariableById('id2'); this.variableMap.deleteVariableById('id2');
sinon.assert.notCalled(stub); sinon.assert.notCalled(stub);
const variable = this.workspace.getVariableById('id2'); const variable = this.variableMap.getVariableById('id2');
assert.isNull(variable); assert.isNull(variable);
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertBlockVarModelName(this.workspace, 0, 'name1'); assertBlockVarModelName(this.workspace, 0, 'name1');
stub.restore(); stub.restore();
@@ -115,12 +116,12 @@ export function testAWorkspace() {
test('deleteVariableById(id1) multiple usages confirm', function () { test('deleteVariableById(id1) multiple usages confirm', function () {
// Deleting variable with multiple usages triggers confirm dialog. // Deleting variable with multiple usages triggers confirm dialog.
const stub = sinon.stub(window, 'confirm').returns(true); const stub = sinon.stub(window, 'confirm').returns(true);
this.workspace.deleteVariableById('id1'); this.variableMap.deleteVariableById('id1');
sinon.assert.calledOnce(stub); sinon.assert.calledOnce(stub);
const variable = this.workspace.getVariableById('id1'); const variable = this.variableMap.getVariableById('id1');
assert.isNull(variable); assert.isNull(variable);
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
assertBlockVarModelName(this.workspace, 0, 'name2'); assertBlockVarModelName(this.workspace, 0, 'name2');
stub.restore(); stub.restore();
@@ -129,11 +130,11 @@ export function testAWorkspace() {
test('deleteVariableById(id1) multiple usages cancel', function () { test('deleteVariableById(id1) multiple usages cancel', function () {
// Deleting variable with multiple usages triggers confirm dialog. // Deleting variable with multiple usages triggers confirm dialog.
const stub = sinon.stub(window, 'confirm').returns(false); const stub = sinon.stub(window, 'confirm').returns(false);
this.workspace.deleteVariableById('id1'); this.variableMap.deleteVariableById('id1');
sinon.assert.calledOnce(stub); sinon.assert.calledOnce(stub);
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
assertBlockVarModelName(this.workspace, 0, 'name1'); assertBlockVarModelName(this.workspace, 0, 'name1');
assertBlockVarModelName(this.workspace, 1, 'name1'); assertBlockVarModelName(this.workspace, 1, 'name1');
assertBlockVarModelName(this.workspace, 2, 'name2'); assertBlockVarModelName(this.workspace, 2, 'name2');
@@ -144,50 +145,50 @@ export function testAWorkspace() {
suite('renameVariableById', function () { suite('renameVariableById', function () {
setup(function () { setup(function () {
this.workspace.createVariable('name1', 'type1', 'id1'); this.variableMap.createVariable('name1', 'type1', 'id1');
}); });
test('No references rename to name2', function () { test('No references rename to name2', function () {
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
assertVariableValues(this.workspace, 'name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id1');
// Renaming should not have created a new variable. // Renaming should not have created a new variable.
assert.equal(this.workspace.getAllVariables().length, 1); assert.equal(this.variableMap.getAllVariables().length, 1);
}); });
test('Reference exists rename to name2', function () { test('Reference exists rename to name2', function () {
createVarBlocksNoEvents(this.workspace, ['id1']); createVarBlocksNoEvents(this.workspace, ['id1']);
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
assertVariableValues(this.workspace, 'name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id1');
// Renaming should not have created a new variable. // Renaming should not have created a new variable.
assert.equal(this.workspace.getAllVariables().length, 1); assert.equal(this.variableMap.getAllVariables().length, 1);
assertBlockVarModelName(this.workspace, 0, 'name2'); assertBlockVarModelName(this.workspace, 0, 'name2');
}); });
test('Reference exists different capitalization rename to Name1', function () { test('Reference exists different capitalization rename to Name1', function () {
createVarBlocksNoEvents(this.workspace, ['id1']); createVarBlocksNoEvents(this.workspace, ['id1']);
this.workspace.renameVariableById('id1', 'Name1'); this.variableMap.renameVariableById('id1', 'Name1');
assertVariableValues(this.workspace, 'Name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'Name1', 'type1', 'id1');
// Renaming should not have created a new variable. // Renaming should not have created a new variable.
assert.equal(this.workspace.getAllVariables().length, 1); assert.equal(this.variableMap.getAllVariables().length, 1);
assertBlockVarModelName(this.workspace, 0, 'Name1'); assertBlockVarModelName(this.workspace, 0, 'Name1');
}); });
suite('Two variables rename overlap', function () { suite('Two variables rename overlap', function () {
test('Same type rename variable with id1 to name2', function () { test('Same type rename variable with id1 to name2', function () {
this.workspace.createVariable('name2', 'type1', 'id2'); this.variableMap.createVariable('name2', 'type1', 'id2');
createVarBlocksNoEvents(this.workspace, ['id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id2']);
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
// The second variable should remain unchanged. // The second variable should remain unchanged.
assertVariableValues(this.workspace, 'name2', 'type1', 'id2'); assertVariableValues(this.workspace, 'name2', 'type1', 'id2');
// The first variable should have been deleted. // The first variable should have been deleted.
const variable = this.workspace.getVariableById('id1'); const variable = this.variableMap.getVariableById('id1');
assert.isNull(variable); assert.isNull(variable);
// There should only be one variable left. // There should only be one variable left.
assert.equal(this.workspace.getAllVariables().length, 1); assert.equal(this.variableMap.getAllVariables().length, 1);
// Both blocks should now reference variable with name2. // Both blocks should now reference variable with name2.
assertBlockVarModelName(this.workspace, 0, 'name2'); assertBlockVarModelName(this.workspace, 0, 'name2');
@@ -195,14 +196,14 @@ export function testAWorkspace() {
}); });
test('Different type rename variable with id1 to name2', function () { test('Different type rename variable with id1 to name2', function () {
this.workspace.createVariable('name2', 'type2', 'id2'); this.variableMap.createVariable('name2', 'type2', 'id2');
createVarBlocksNoEvents(this.workspace, ['id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id2']);
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
// Variables with different type are allowed to have the same name. // Variables with different type are allowed to have the same name.
assertVariableValues(this.workspace, 'name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
// Both blocks should now reference variable with name2. // Both blocks should now reference variable with name2.
assertBlockVarModelName(this.workspace, 0, 'name2'); assertBlockVarModelName(this.workspace, 0, 'name2');
@@ -210,18 +211,18 @@ export function testAWorkspace() {
}); });
test('Same type different capitalization rename variable with id1 to Name2', function () { test('Same type different capitalization rename variable with id1 to Name2', function () {
this.workspace.createVariable('name2', 'type1', 'id2'); this.variableMap.createVariable('name2', 'type1', 'id2');
createVarBlocksNoEvents(this.workspace, ['id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id2']);
this.workspace.renameVariableById('id1', 'Name2'); this.variableMap.renameVariableById('id1', 'Name2');
// The second variable should be updated. // The second variable should be updated.
assertVariableValues(this.workspace, 'Name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'Name2', 'type1', 'id2');
// The first variable should have been deleted. // The first variable should have been deleted.
const variable = this.workspace.getVariableById('id1'); const variable = this.variableMap.getVariableById('id1');
assert.isNull(variable); assert.isNull(variable);
// There should only be one variable left. // There should only be one variable left.
assert.equal(this.workspace.getAllVariables().length, 1); assert.equal(this.variableMap.getAllVariables().length, 1);
// Both blocks should now reference variable with Name2. // Both blocks should now reference variable with Name2.
assertBlockVarModelName(this.workspace, 0, 'Name2'); assertBlockVarModelName(this.workspace, 0, 'Name2');
@@ -229,15 +230,15 @@ export function testAWorkspace() {
}); });
test('Different type different capitalization rename variable with id1 to Name2', function () { test('Different type different capitalization rename variable with id1 to Name2', function () {
this.workspace.createVariable('name2', 'type2', 'id2'); this.variableMap.createVariable('name2', 'type2', 'id2');
createVarBlocksNoEvents(this.workspace, ['id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id2']);
this.workspace.renameVariableById('id1', 'Name2'); this.variableMap.renameVariableById('id1', 'Name2');
// Variables with different type are allowed to have the same name. // Variables with different type are allowed to have the same name.
assertVariableValues(this.workspace, 'Name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'Name2', 'type1', 'id1');
// Second variable should remain unchanged. // Second variable should remain unchanged.
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
// Only first block should use new capitalization. // Only first block should use new capitalization.
assertBlockVarModelName(this.workspace, 0, 'Name2'); assertBlockVarModelName(this.workspace, 0, 'Name2');
@@ -1477,180 +1478,184 @@ export function testAWorkspace() {
suite('renameVariableById', function () { suite('renameVariableById', function () {
setup(function () { setup(function () {
this.workspace.createVariable('name1', 'type1', 'id1'); this.variableMap.createVariable('name1', 'type1', 'id1');
}); });
test('Reference exists no usages rename to name2', function () { test('Reference exists no usages rename to name2', function () {
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id1');
}); });
test('Reference exists with usages rename to name2', function () { test('Reference exists with usages rename to name2', function () {
createVarBlocksNoEvents(this.workspace, ['id1']); createVarBlocksNoEvents(this.workspace, ['id1']);
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertBlockVarModelName(this.workspace, 0, 'name1'); assertBlockVarModelName(this.workspace, 0, 'name1');
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertBlockVarModelName(this.workspace, 0, 'name2'); assertBlockVarModelName(this.workspace, 0, 'name2');
assertVariableValues(this.workspace, 'name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id1');
}); });
test('Reference exists different capitalization no usages rename to Name1', function () { test('Reference exists different capitalization no usages rename to Name1', function () {
this.workspace.renameVariableById('id1', 'Name1'); this.variableMap.renameVariableById('id1', 'Name1');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'Name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'Name1', 'type1', 'id1');
}); });
test('Reference exists different capitalization with usages rename to Name1', function () { test('Reference exists different capitalization with usages rename to Name1', function () {
createVarBlocksNoEvents(this.workspace, ['id1']); createVarBlocksNoEvents(this.workspace, ['id1']);
this.workspace.renameVariableById('id1', 'Name1'); this.variableMap.renameVariableById('id1', 'Name1');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertBlockVarModelName(this.workspace, 0, 'name1'); assertBlockVarModelName(this.workspace, 0, 'name1');
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertBlockVarModelName(this.workspace, 0, 'Name1'); assertBlockVarModelName(this.workspace, 0, 'Name1');
assertVariableValues(this.workspace, 'Name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'Name1', 'type1', 'id1');
}); });
suite('Two variables rename overlap', function () { suite('Two variables rename overlap', function () {
test('Same type no usages rename variable with id1 to name2', function () { test('Same type no usages rename variable with id1 to name2', function () {
this.workspace.createVariable('name2', 'type1', 'id2'); this.variableMap.createVariable('name2', 'type1', 'id2');
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id2');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id2');
assert.isNull(this.workspace.getVariableById('id1')); assert.isNull(this.variableMap.getVariableById('id1'));
}); });
test('Same type with usages rename variable with id1 to name2', function () { test('Same type with usages rename variable with id1 to name2', function () {
this.workspace.createVariable('name2', 'type1', 'id2'); const variable = this.variableMap.createVariable(
'name2',
'type1',
'id2',
);
createVarBlocksNoEvents(this.workspace, ['id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id2']);
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertBlockVarModelName(this.workspace, 0, 'name1'); assertBlockVarModelName(this.workspace, 0, 'name1');
assertBlockVarModelName(this.workspace, 1, 'name2'); assertBlockVarModelName(this.workspace, 1, 'name2');
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id2');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id2');
assert.isNull(this.workspace.getVariableById('id1')); assert.isNull(this.variableMap.getVariableById('id1'));
}); });
test('Same type different capitalization no usages rename variable with id1 to Name2', function () { test('Same type different capitalization no usages rename variable with id1 to Name2', function () {
this.workspace.createVariable('name2', 'type1', 'id2'); this.variableMap.createVariable('name2', 'type1', 'id2');
this.workspace.renameVariableById('id1', 'Name2'); this.variableMap.renameVariableById('id1', 'Name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id2');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'Name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'Name2', 'type1', 'id2');
assert.isNull(this.workspace.getVariable('name1')); assert.isNull(this.variableMap.getVariable('name1'));
}); });
test('Same type different capitalization with usages rename variable with id1 to Name2', function () { test('Same type different capitalization with usages rename variable with id1 to Name2', function () {
this.workspace.createVariable('name2', 'type1', 'id2'); this.workspace.createVariable('name2', 'type1', 'id2');
createVarBlocksNoEvents(this.workspace, ['id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id2']);
this.workspace.renameVariableById('id1', 'Name2'); this.variableMap.renameVariableById('id1', 'Name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertBlockVarModelName(this.workspace, 0, 'name1'); assertBlockVarModelName(this.workspace, 0, 'name1');
assertBlockVarModelName(this.workspace, 1, 'name2'); assertBlockVarModelName(this.workspace, 1, 'name2');
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id2');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'Name2', 'type1', 'id2'); assertVariableValues(this.variableMap, 'Name2', 'type1', 'id2');
assert.isNull(this.workspace.getVariableById('id1')); assert.isNull(this.variableMap.getVariableById('id1'));
assertBlockVarModelName(this.workspace, 0, 'Name2'); assertBlockVarModelName(this.workspace, 0, 'Name2');
assertBlockVarModelName(this.workspace, 1, 'Name2'); assertBlockVarModelName(this.workspace, 1, 'Name2');
}); });
test('Different type no usages rename variable with id1 to name2', function () { test('Different type no usages rename variable with id1 to name2', function () {
this.workspace.createVariable('name2', 'type2', 'id2'); this.variableMap.createVariable('name2', 'type2', 'id2');
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
}); });
test('Different type with usages rename variable with id1 to name2', function () { test('Different type with usages rename variable with id1 to name2', function () {
this.workspace.createVariable('name2', 'type2', 'id2'); this.variableMap.createVariable('name2', 'type2', 'id2');
createVarBlocksNoEvents(this.workspace, ['id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id2']);
this.workspace.renameVariableById('id1', 'name2'); this.variableMap.renameVariableById('id1', 'name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
assertBlockVarModelName(this.workspace, 0, 'name1'); assertBlockVarModelName(this.workspace, 0, 'name1');
assertBlockVarModelName(this.workspace, 1, 'name2'); assertBlockVarModelName(this.workspace, 1, 'name2');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name2', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
assertBlockVarModelName(this.workspace, 0, 'name2'); assertBlockVarModelName(this.workspace, 0, 'name2');
assertBlockVarModelName(this.workspace, 1, 'name2'); assertBlockVarModelName(this.workspace, 1, 'name2');
}); });
test('Different type different capitalization no usages rename variable with id1 to Name2', function () { test('Different type different capitalization no usages rename variable with id1 to Name2', function () {
this.workspace.createVariable('name2', 'type2', 'id2'); this.variableMap.createVariable('name2', 'type2', 'id2');
this.workspace.renameVariableById('id1', 'Name2'); this.variableMap.renameVariableById('id1', 'Name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
@@ -1660,27 +1665,27 @@ export function testAWorkspace() {
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'Name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'Name2', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
}); });
test('Different type different capitalization with usages rename variable with id1 to Name2', function () { test('Different type different capitalization with usages rename variable with id1 to Name2', function () {
this.workspace.createVariable('name2', 'type2', 'id2'); this.variableMap.createVariable('name2', 'type2', 'id2');
createVarBlocksNoEvents(this.workspace, ['id1', 'id2']); createVarBlocksNoEvents(this.workspace, ['id1', 'id2']);
this.workspace.renameVariableById('id1', 'Name2'); this.variableMap.renameVariableById('id1', 'Name2');
this.clock.runAll(); this.clock.runAll();
this.workspace.undo(); this.workspace.undo();
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'name1', 'type1', 'id1'); assertVariableValues(this.variableMap, 'name1', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
assertBlockVarModelName(this.workspace, 0, 'name1'); assertBlockVarModelName(this.workspace, 0, 'name1');
assertBlockVarModelName(this.workspace, 1, 'name2'); assertBlockVarModelName(this.workspace, 1, 'name2');
this.workspace.undo(true); this.workspace.undo(true);
this.clock.runAll(); this.clock.runAll();
assertVariableValues(this.workspace, 'Name2', 'type1', 'id1'); assertVariableValues(this.variableMap, 'Name2', 'type1', 'id1');
assertVariableValues(this.workspace, 'name2', 'type2', 'id2'); assertVariableValues(this.variableMap, 'name2', 'type2', 'id2');
assertBlockVarModelName(this.workspace, 0, 'Name2'); assertBlockVarModelName(this.workspace, 0, 'Name2');
assertBlockVarModelName(this.workspace, 1, 'name2'); assertBlockVarModelName(this.workspace, 1, 'name2');
}); });

View File

@@ -119,6 +119,7 @@ suite('XML', function () {
suite('blockToDom', function () { suite('blockToDom', function () {
setup(function () { setup(function () {
this.workspace = new Blockly.Workspace(); this.workspace = new Blockly.Workspace();
this.variableMap = this.workspace.getVariableMap();
}); });
teardown(function () { teardown(function () {
workspaceTeardown.call(this, this.workspace); workspaceTeardown.call(this, this.workspace);
@@ -296,7 +297,7 @@ suite('XML', function () {
]); ]);
}); });
test('Variable Trivial', function () { test('Variable Trivial', function () {
this.workspace.createVariable('name1', '', 'id1'); this.variableMap.createVariable('name1', '', 'id1');
const block = new Blockly.Block( const block = new Blockly.Block(
this.workspace, this.workspace,
'field_variable_test_block', 'field_variable_test_block',
@@ -306,7 +307,7 @@ suite('XML', function () {
assertVariableDomField(resultFieldDom, 'VAR', null, 'id1', 'name1'); assertVariableDomField(resultFieldDom, 'VAR', null, 'id1', 'name1');
}); });
test('Variable Typed', function () { test('Variable Typed', function () {
this.workspace.createVariable('name1', 'string', 'id1'); this.variableMap.createVariable('name1', 'string', 'id1');
const block = new Blockly.Block( const block = new Blockly.Block(
this.workspace, this.workspace,
'field_variable_test_block', 'field_variable_test_block',
@@ -323,7 +324,7 @@ suite('XML', function () {
}); });
test('Variable Default Case', function () { test('Variable Default Case', function () {
createGenUidStubWithReturns('1'); createGenUidStubWithReturns('1');
this.workspace.createVariable('name1'); this.variableMap.createVariable('name1');
Blockly.Events.disable(); Blockly.Events.disable();
const block = new Blockly.Block( const block = new Blockly.Block(
@@ -439,13 +440,14 @@ suite('XML', function () {
], ],
}, },
]); ]);
this.variableMap = this.workspace.getVariableMap();
}); });
teardown(function () { teardown(function () {
workspaceTeardown.call(this, this.workspace); workspaceTeardown.call(this, this.workspace);
}); });
test('One Variable', function () { test('One Variable', function () {
createGenUidStubWithReturns('1'); createGenUidStubWithReturns('1');
this.workspace.createVariable('name1'); this.variableMap.createVariable('name1');
const resultDom = Blockly.Xml.variablesToDom( const resultDom = Blockly.Xml.variablesToDom(
this.workspace.getAllVariables(), this.workspace.getAllVariables(),
); );
@@ -456,8 +458,8 @@ suite('XML', function () {
assert.equal(resultVariableDom.getAttribute('id'), '1'); assert.equal(resultVariableDom.getAttribute('id'), '1');
}); });
test('Two Variable one block', function () { test('Two Variable one block', function () {
this.workspace.createVariable('name1', '', 'id1'); this.variableMap.createVariable('name1', '', 'id1');
this.workspace.createVariable('name2', 'type2', 'id2'); this.variableMap.createVariable('name2', 'type2', 'id2');
// If events are enabled during block construction, it will create a // If events are enabled during block construction, it will create a
// default variable. // default variable.
Blockly.Events.disable(); Blockly.Events.disable();