mirror of
https://github.com/google/blockly.git
synced 2026-01-08 17:40:09 +01:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user