chore: fix isNameUsed not fully respecting the procedure map (#6833)

* chore: fix isNameUsed not fully respecting the procedure map

* chore: add tests that isNameUsed respects the procedure map
This commit is contained in:
Beka Westberg
2023-02-08 09:12:23 -08:00
committed by GitHub
parent d83dcfbe2e
commit 8386024e44
2 changed files with 37 additions and 24 deletions

View File

@@ -194,15 +194,19 @@ export function isNameUsed(
for (const block of workspace.getAllBlocks(false)) {
if (block === opt_exclude) continue;
if (isProcedureBlock(block) && block.isProcedureDef() &&
Names.equals(block.getProcedureModel().getName(), name)) {
return true;
}
if (isLegacyProcedureDefBlock(block) &&
Names.equals(block.getProcedureDef()[0], name)) {
return true;
}
}
const excludeModel = opt_exclude && isProcedureBlock(opt_exclude) ?
opt_exclude?.getProcedureModel() :
undefined;
for (const model of workspace.getProcedureMap().getProcedures()) {
if (model === excludeModel) continue;
if (Names.equals(model.getName(), name)) return true;
}
return false;
}

View File

@@ -7,7 +7,7 @@
goog.declareModuleId('Blockly.test.procedures');
import * as Blockly from '../../../build/src/core/blockly.js';
import {assertCallBlockStructure, assertDefBlockStructure, createProcDefBlock, createProcCallBlock} from '../test_helpers/procedures.js';
import {assertCallBlockStructure, assertDefBlockStructure, createProcDefBlock, createProcCallBlock, MockProcedureModel} from '../test_helpers/procedures.js';
import {runSerializationTestSuite} from '../test_helpers/serialization.js';
import {createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} from '../test_helpers/setup_teardown.js';
import {defineRowBlock} from '../test_helpers/block_definitions.js';
@@ -1083,10 +1083,35 @@ suite('Procedures', function() {
});
suite('isNameUsed', function() {
test('No Blocks', function() {
test('returns false if no blocks or models exists', function() {
chai.assert.isFalse(
Blockly.Procedures.isNameUsed('name1', this.workspace)
);
Blockly.Procedures.isNameUsed('proc name', this.workspace));
});
test('returns true if an associated block exists', function() {
createProcDefBlock(this.workspace, false, [], 'proc name');
chai.assert.isTrue(
Blockly.Procedures.isNameUsed('proc name', this.workspace));
});
test('return false if an associated block does not exist', function() {
createProcDefBlock(this.workspace, false, [], 'proc name');
chai.assert.isFalse(
Blockly.Procedures.isNameUsed('other proc name', this.workspace));
});
test('returns true if an associated procedure model exists', function() {
this.workspace.getProcedureMap()
.add(new MockProcedureModel().setName('proc name'));
chai.assert.isTrue(
Blockly.Procedures.isNameUsed('proc name', this.workspace));
});
test('returns false if an associated procedure model exists', function() {
this.workspace.getProcedureMap()
.add(new MockProcedureModel().setName('proc name'));
chai.assert.isFalse(
Blockly.Procedures.isNameUsed('other proc name', this.workspace));
});
});
@@ -1273,22 +1298,6 @@ suite('Procedures', function() {
assertCallBlockStructure(this.callBlock);
});
});
suite('isNameUsed', function() {
setup(function() {
this.defBlock = this.workspace.newBlock(testSuite.defType);
this.defBlock.setFieldValue('proc name', 'NAME');
this.callBlock = this.workspace.newBlock(testSuite.callType);
this.callBlock.setFieldValue('proc name', 'NAME');
});
test('True', function() {
chai.assert.isTrue(
Blockly.Procedures.isNameUsed('proc name', this.workspace));
});
test('False', function() {
chai.assert.isFalse(
Blockly.Procedures.isNameUsed('unused proc name', this.workspace));
});
});
suite('rename', function() {
setup(function() {
this.defBlock = Blockly.serialization.blocks.append({