Fixed procedures not handling empty names. Renenabled/fixed tests.

This commit is contained in:
Beka Westberg
2019-07-08 09:23:16 -07:00
parent efde04b7dc
commit 87fbe0447b
3 changed files with 19 additions and 11 deletions

View File

@@ -92,6 +92,8 @@ Blockly.Procedures.procTupleComparator_ = function(ta, tb) {
/**
* Ensure two identically-named procedures don't exist.
* Take the proposed procedure name, and return a legal name i.e. one that
* is not empty and doesn't collide with other procedures.
* @param {string} name Proposed procedure name.
* @param {!Blockly.Block} block Block to disambiguate.
* @return {string} Non-colliding name.
@@ -101,6 +103,7 @@ Blockly.Procedures.findLegalName = function(name, block) {
// Flyouts can have multiple procedures called 'do something'.
return name;
}
name = name || Blockly.Msg['UNNAMED_KEY'];
while (!Blockly.Procedures.isLegalName_(name, block.workspace, block)) {
// Collision with another procedure.
var r = name.match(/^(.*?)(\d+)$/);
@@ -162,7 +165,6 @@ Blockly.Procedures.rename = function(name) {
// Strip leading and trailing whitespace. Beyond this, all names are legal.
name = name.trim();
// Ensure two identically-named procedures don't exist.
var legalName = Blockly.Procedures.findLegalName(name, this.getSourceBlock());
var oldName = this.getValue();
if (oldName != name && oldName != legalName) {

View File

@@ -71,6 +71,9 @@ Blockly.Msg.PROCEDURES_HUE = '290';
/// For more context, see
/// [[Translating:Blockly#infrequent_message_types]].\n{{Identical|Item}}
Blockly.Msg.VARIABLES_DEFAULT_NAME = 'item';
/// default name - A simple, default name for an unnamed function or
// variable. Preferably indicates that the key is unnamed.
Blockly.Msg.UNNAMED_KEY = 'unnamed';
/// button text - Button that sets a calendar to today's date.\n{{Identical|Today}}
Blockly.Msg.TODAY = 'Today';

View File

@@ -165,16 +165,15 @@ suite('Procedures', function() {
defInput.htmlInput_.value = '';
defInput.onHtmlInputChange_(null);
chai.assert.equal(this.defBlock.getFieldValue('NAME'), '');
chai.assert.equal(this.callBlock.getFieldValue('NAME'), '');
chai.assert.equal(
this.defBlock.getFieldValue('NAME'),
Blockly.Msg['UNNAMED_KEY']);
chai.assert.equal(
this.callBlock.getFieldValue('NAME'),
Blockly.Msg['UNNAMED_KEY']);
}, 'start name');
});
// TODO: Procedures do not handle having no name correctly. It is a
// problem with newly created procedure blocks having '' as the
// oldName, just like empty procedures. The renameProcedure function
// gets confused when a procedure's name is initially set. This is
// basically #503 again.
test.skip('Set Empty, and Create New', function() {
test('Set Empty, and Create New', function() {
this.callForAllTypes(function() {
var defInput = this.defBlock.getField('NAME');
defInput.htmlInput_ = Object.create(null);
@@ -185,8 +184,12 @@ suite('Procedures', function() {
defInput.onHtmlInputChange_(null);
var newDefBlock = new Blockly.Block(this.workspace, this.defType);
newDefBlock.setFieldValue('new name', 'NAME');
chai.assert.equal(this.defBlock.getFieldValue('NAME'), '');
chai.assert.equal(this.callBlock.getFieldValue('NAME'), '');
chai.assert.equal(
this.defBlock.getFieldValue('NAME'),
Blockly.Msg['UNNAMED_KEY']);
chai.assert.equal(
this.callBlock.getFieldValue('NAME'),
Blockly.Msg['UNNAMED_KEY']);
newDefBlock.dispose();
}, 'start name');