Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions blocks/procedures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,21 +726,21 @@ const PROCEDURES_MUTATORARGUMENT = {
if (sourceBlock.isInFlyout) {
return varName;
}

const model = outerWs.getVariable(varName, '');
const variableMap = outerWs.getVariableMap();
const model = variableMap.getVariable(varName, '');
if (model && model.getName() !== varName) {
// Rename the variable (case change)
outerWs.renameVariableById(model.getId(), varName);
variableMap.renameVariable(model, varName);
}
if (!model) {
if (this.editingInteractively) {
if (!this.editingVariable) {
this.editingVariable = outerWs.createVariable(varName, '');
this.editingVariable = variableMap.createVariable(varName, '');
} else {
outerWs.renameVariableById(this.editingVariable.getId(), varName);
variableMap.renameVariable(this.editingVariable, varName);
}
} else {
outerWs.createVariable(varName, '');
variableMap.createVariable(varName, '');
}
}
return varName;
Expand Down
8 changes: 5 additions & 3 deletions tests/mocha/block_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,7 @@ suite('Blocks', function () {
],
},
]);
this.variableMap = this.workspace.getVariableMap();
});
teardown(function () {
eventUtils.enable();
Expand Down Expand Up @@ -2426,13 +2427,14 @@ suite('Blocks', function () {
assertCollapsed(blockA);
});
});

suite('Renaming Vars', function () {
test('Simple Rename', function () {
const blockA = createRenderedBlock(this.workspace, 'variable_block');

blockA.setCollapsed(true);
const variable = this.workspace.getVariable('x', '');
this.workspace.renameVariableById(variable.getId(), 'y');
this.variableMap.renameVariable(variable, 'y');

this.clock.runAll();
assertCollapsed(blockA, 'y');
Expand All @@ -2441,8 +2443,8 @@ suite('Blocks', function () {
const blockA = createRenderedBlock(this.workspace, 'variable_block');

blockA.setCollapsed(true);
const variable = this.workspace.createVariable('y');
this.workspace.renameVariableById(variable.getId(), 'X');
const variable = this.variableMap.createVariable('y');
this.variableMap.renameVariable(variable, 'X');

this.clock.runAll();
assertCollapsed(blockA, 'X');
Expand Down
13 changes: 7 additions & 6 deletions tests/mocha/blocks/procedures_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ suite('Procedures', function () {
'preCreatedTypedVarId',
);
defineRowBlock();
this.variableMap = this.workspace.getVariableMap();
});

teardown(function () {
Expand Down Expand Up @@ -453,7 +454,7 @@ suite('Procedures', function () {
mutatorIcon.setBubbleVisible(false);

const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'new name');
this.variableMap.renameVariable(variable, 'new name');

assert.isNotNull(
defBlock.getField('PARAMS'),
Expand All @@ -480,7 +481,7 @@ suite('Procedures', function () {
this.clock.runAll();

const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'new name');
this.variableMap.renameVariable(variable, 'new name');

assert.equal(
paramBlock.getFieldValue('NAME'),
Expand All @@ -506,7 +507,7 @@ suite('Procedures', function () {
mutatorIcon.setBubbleVisible(false);

const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'new name');
this.variableMap.renameVariable(variable, 'new name');

assert.isNotNull(
callBlock.getInput('ARG0'),
Expand Down Expand Up @@ -535,7 +536,7 @@ suite('Procedures', function () {
mutatorIcon.setBubbleVisible(false);

const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar');
this.variableMap.renameVariable(variable, 'preCreatedVar');

assert.isNotNull(
defBlock.getField('PARAMS'),
Expand All @@ -562,7 +563,7 @@ suite('Procedures', function () {
this.clock.runAll();

const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar');
this.variableMap.renameVariable(variable, 'preCreatedVar');

assert.equal(
paramBlock.getFieldValue('NAME'),
Expand All @@ -588,7 +589,7 @@ suite('Procedures', function () {
mutatorIcon.setBubbleVisible(false);

const variable = this.workspace.getVariable('param1', '');
this.workspace.renameVariableById(variable.getId(), 'preCreatedVar');
this.variableMap.renameVariable(variable, 'preCreatedVar');

assert.isNotNull(
callBlock.getInput('ARG0'),
Expand Down
26 changes: 13 additions & 13 deletions tests/mocha/blocks/variables_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ suite('Variables', function () {
],
},
]);
this.workspace.createVariable('foo', 'type1', '1');
this.workspace.createVariable('bar', 'type1', '2');
this.workspace.createVariable('baz', 'type1', '3');
this.variableMap = this.workspace.getVariableMap();
this.variableMap.createVariable('foo', 'type1', '1');
this.variableMap.createVariable('bar', 'type1', '2');
this.variableMap.createVariable('baz', 'type1', '3');
});

teardown(function () {
Expand Down Expand Up @@ -116,12 +117,11 @@ suite('Variables', function () {
);
});
});

suite('getVariable', function () {
test('By ID', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable(this.workspace, 'id1');
const result2 = Blockly.Variables.getVariable(this.workspace, 'id2');
const result3 = Blockly.Variables.getVariable(this.workspace, 'id3');
Expand All @@ -132,9 +132,9 @@ suite('Variables', function () {
});

test('By name and type', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable(
this.workspace,
null,
Expand All @@ -161,9 +161,9 @@ suite('Variables', function () {
});

test('Bad ID with name and type fallback', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
const var1 = this.variableMap.createVariable('name1', 'type1', 'id1');
const var2 = this.variableMap.createVariable('name2', 'type1', 'id2');
const var3 = this.variableMap.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable(
this.workspace,
'badId',
Expand Down
8 changes: 5 additions & 3 deletions tests/mocha/field_variable_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,15 @@ suite('Variable Fields', function () {
this.variableField = this.variableBlock.getField('VAR');
});
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.getValue(), 'id1');
});
test('Rename & Get New ID', function () {
this.workspace.createVariable('name2', null, 'id2');
this.workspace.renameVariableById('id1', 'name2');
const variableMap = this.workspace.getVariableMap();
variableMap.createVariable('name2', null, 'id2');
variableMap.renameVariable(variableMap.getVariableById('id1'), 'name2');
assert.equal(this.variableField.getText(), 'name2');
assert.equal(this.variableField.getValue(), 'id2');
});
Expand Down
5 changes: 3 additions & 2 deletions tests/mocha/jso_serialization_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ suite('JSO Serialization', function () {
defineStatementBlock();

createGenUidStubWithReturns(new Array(10).fill().map((_, i) => 'id' + i));
this.variableMap = this.workspace.getVariableMap();
});

teardown(function () {
Expand Down Expand Up @@ -834,7 +835,7 @@ suite('JSO Serialization', function () {

suite('Variables', function () {
test('Without type', function () {
this.workspace.createVariable('testVar', '', 'testId');
this.variableMap.createVariable('testVar', '', 'testId');
const jso = Blockly.serialization.workspaces.save(this.workspace);
const variable = jso['variables'][0];
assertProperty(variable, 'name', 'testVar');
Expand All @@ -843,7 +844,7 @@ suite('JSO Serialization', 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 variable = jso['variables'][0];
assertProperty(variable, 'name', 'testVar');
Expand Down
Loading