Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always rename caller to legal name #6014

Merged
merged 3 commits into from
Mar 21, 2022
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
9 changes: 4 additions & 5 deletions blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,11 +968,10 @@ const PROCEDURE_CALL_COMMON = {
block.appendChild(mutation);
const field = xmlUtils.createElement('field');
field.setAttribute('name', 'NAME');
let callName = this.getProcedureCall();
if (!callName) {
// Rename if name is empty string.
callName = Procedures.findLegalName('', this);
this.renameProcedure('', callName);
const callName = this.getProcedureCall();
const newName = Procedures.findLegalName(callName, this);
if (callName !== newName) {
this.renameProcedure(callName, newName);
}
field.appendChild(xmlUtils.createTextNode(callName));
block.appendChild(field);
Expand Down
29 changes: 16 additions & 13 deletions tests/mocha/procedures_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ goog.require('Blockly');
goog.require('Blockly.Msg');
const {assertCallBlockStructure, assertDefBlockStructure, createProcDefBlock, createProcCallBlock} = goog.require('Blockly.test.helpers.procedures');
const {runSerializationTestSuite} = goog.require('Blockly.test.helpers.serialization');
const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');
const {createGenUidStubWithReturns, sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');


suite('Procedures', function() {
Expand Down Expand Up @@ -294,8 +294,12 @@ suite('Procedures', function() {
});
});
suite('caller param mismatch', function() {
test.skip('callreturn with missing args', function() {
// TODO: How do we want it to behave in this situation?
setup(function() {
this.TEST_VAR_ID = 'test-id';
this.genUidStub = createGenUidStubWithReturns(this.TEST_VAR_ID);
});

test('callreturn with missing args', function() {
const defBlock = Blockly.Xml.domToBlock(Blockly.Xml.textToDom(`
<block type="procedures_defreturn">
<field name="NAME">do something</field>
Expand All @@ -310,10 +314,9 @@ suite('Procedures', function() {
'</block>'
), this.workspace);
assertDefBlockStructure(defBlock, true, ['x'], ['arg']);
assertCallBlockStructure(callBlock, ['x'], ['arg']);
assertCallBlockStructure(callBlock, [], [], 'do something2');
});
test.skip('callreturn with bad args', function() {
// TODO: How do we want it to behave in this situation?
test('callreturn with bad args', function() {
const defBlock = Blockly.Xml.domToBlock(Blockly.Xml.textToDom(`
<block type="procedures_defreturn">
<field name="NAME">do something</field>
Expand All @@ -330,10 +333,10 @@ suite('Procedures', function() {
</block>
`), this.workspace);
assertDefBlockStructure(defBlock, true, ['x'], ['arg']);
assertCallBlockStructure(callBlock, ['x'], ['arg']);
assertCallBlockStructure(
callBlock, ['y'], [this.TEST_VAR_ID], 'do something2');
});
test.skip('callnoreturn with missing args', function() {
// TODO: How do we want it to behave in this situation?
test('callnoreturn with missing args', function() {
const defBlock = Blockly.Xml.domToBlock(Blockly.Xml.textToDom(`
<block type="procedures_defnoreturn">
<field name="NAME">do something</field>
Expand All @@ -348,10 +351,9 @@ suite('Procedures', function() {
'</block>'
), this.workspace);
assertDefBlockStructure(defBlock, false, ['x'], ['arg']);
assertCallBlockStructure(callBlock, ['x'], ['arg']);
assertCallBlockStructure(callBlock, [], [], 'do something2');
});
test.skip('callnoreturn with bad args', function() {
// TODO: How do we want it to behave in this situation?
test('callnoreturn with bad args', function() {
const defBlock = Blockly.Xml.domToBlock(Blockly.Xml.textToDom(`
<block type="procedures_defnoreturn">
<field name="NAME">do something</field>
Expand All @@ -368,7 +370,8 @@ suite('Procedures', function() {
</block>
`), this.workspace);
assertDefBlockStructure(defBlock, false, ['x'], ['arg']);
assertCallBlockStructure(callBlock, ['x'], ['arg']);
assertCallBlockStructure(
callBlock, ['y'], [this.TEST_VAR_ID], 'do something2');
});
});
});
Expand Down
9 changes: 7 additions & 2 deletions tests/mocha/test_helpers/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ function assertDefBlockStructure(defBlock, hasReturn = false,
exports.assertDefBlockStructure = assertDefBlockStructure;

/**
* Asserts that the procedure definition block has the expected inputs and
* Asserts that the procedure call block has the expected inputs and
* fields.
* @param {!Blockly.Block} callBlock The procedure call block.
* @param {Array<string>=} args An array of argument names.
* @param {Array<string>=} varIds An array of variable ids.
* @param {string=} name The name we expect the caller to have.
*/
function assertCallBlockStructure(callBlock, args = [], varIds = []) {
function assertCallBlockStructure(
callBlock, args = [], varIds = [], name = undefined) {
if (args.length) {
chai.assert.include(callBlock.toString(), 'with');
} else {
Expand All @@ -100,6 +102,9 @@ function assertCallBlockStructure(callBlock, args = [], varIds = []) {

assertCallBlockArgsStructure(callBlock, args);
assertBlockVarModels(callBlock, varIds);
if (name !== undefined) {
chai.assert(callBlock.getFieldValue('NAME'), name);
}
}
exports.assertCallBlockStructure = assertCallBlockStructure;

Expand Down