mirror of
https://github.com/google/blockly.git
synced 2026-01-08 01:20:12 +01:00
* refactor(xml): Move textToDom to core/utils/xml.ts This function being in core/xml.ts was the cause for the last remaining circular import in core/ (between variables.ts and xml.ts). Moving it to utils/xml.ts makes sense anyway, since there is nothing Blockly-specific about this function. Fixes #6817. * fix(closure): Reenable goog.declareModuleId multiple-call check Reenable an assertion which check to make sure that goog.declareModuleId is not called more than once in a module (and which also catches circular imports amongst ES modules, which are not detected by closure-make-deps). * chore(tests,demos): Augo-migrate use of textToDom Testing the migration file entry by auto-migrating all uses of Blockly.Xml.textToDom to Blockly.utils.xml.textToDom. * chore(blocks): Manually migrate remaining use of textToDom Update the one remaining call to textToDom (in blocks/lists.ts) to the function's new location - also removing the last use of the Blockly.Xml / core/xml.ts) module from this file. * docs(xml): Remove unneeded @alias per comments on PR #6818 * fix(imports): Remove unused import
244 lines
6.7 KiB
JavaScript
244 lines
6.7 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2020 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
goog.declareModuleId('Blockly.test.helpers.procedures');
|
|
|
|
import {ConnectionType} from '../../../build/src/core/connection_type.js';
|
|
|
|
|
|
/**
|
|
* Asserts that the procedure definition or call block has the expected var
|
|
* models.
|
|
* @param {!Blockly.Block} block The procedure definition or call block to
|
|
* check.
|
|
* @param {!Array<string>} varIds An array of variable ids.
|
|
*/
|
|
function assertBlockVarModels(block, varIds) {
|
|
const expectedVarModels = [];
|
|
for (let i = 0; i < varIds.length; i++) {
|
|
expectedVarModels.push(block.workspace.getVariableById(varIds[i]));
|
|
}
|
|
chai.assert.sameDeepOrderedMembers(block.getVarModels(), expectedVarModels);
|
|
}
|
|
|
|
/**
|
|
* Asserts that the procedure call block has the expected arguments.
|
|
* @param {!Blockly.Block} callBlock The procedure definition block.
|
|
* @param {Array<string>=} args An array of argument names.
|
|
*/
|
|
function assertCallBlockArgsStructure(callBlock, args) {
|
|
// inputList also contains "TOPROW"
|
|
chai.assert.equal(callBlock.inputList.length - 1, args.length,
|
|
'call block has the expected number of args');
|
|
|
|
for (let i = 0; i < args.length; i++) {
|
|
const expectedName = args[i];
|
|
const callInput = callBlock.inputList[i + 1];
|
|
chai.assert.equal(callInput.type, ConnectionType.INPUT_VALUE);
|
|
chai.assert.equal(callInput.name, 'ARG' + i);
|
|
chai.assert.equal(callInput.fieldRow[0].getValue(), expectedName,
|
|
'Call block consts did not match expected.');
|
|
}
|
|
chai.assert.sameOrderedMembers(callBlock.getVars(), args);
|
|
}
|
|
|
|
/**
|
|
* Asserts that the procedure definition block has the expected inputs and
|
|
* fields.
|
|
* @param {!Blockly.Block} defBlock The procedure definition block.
|
|
* @param {boolean=} hasReturn If we expect the procedure def to have a return
|
|
* input or not.
|
|
* @param {Array<string>=} args An array of argument names.
|
|
* @param {Array<string>=} varIds An array of variable ids.
|
|
* @param {boolean=} hasStatements If we expect the procedure def to have a
|
|
* statement input or not.
|
|
*/
|
|
export function assertDefBlockStructure(defBlock, hasReturn = false,
|
|
args = [], varIds = [], hasStatements = true) {
|
|
if (hasStatements) {
|
|
chai.assert.isNotNull(defBlock.getInput('STACK'),
|
|
'Def block should have STACK input');
|
|
} else {
|
|
chai.assert.isNull(defBlock.getInput('STACK'),
|
|
'Def block should not have STACK input');
|
|
}
|
|
if (hasReturn) {
|
|
chai.assert.isNotNull(defBlock.getInput('RETURN'),
|
|
'Def block should have RETURN input');
|
|
} else {
|
|
chai.assert.isNull(defBlock.getInput('RETURN'),
|
|
'Def block should not have RETURN input');
|
|
}
|
|
if (args.length) {
|
|
chai.assert.include(defBlock.toString(), 'with',
|
|
'Def block string should include "with"');
|
|
} else {
|
|
chai.assert.notInclude(defBlock.toString(), 'with',
|
|
'Def block string should not include "with"');
|
|
}
|
|
|
|
chai.assert.sameOrderedMembers(defBlock.getVars(), args);
|
|
assertBlockVarModels(defBlock, varIds);
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
export function assertCallBlockStructure(
|
|
callBlock, args = [], varIds = [], name = undefined) {
|
|
if (args.length) {
|
|
chai.assert.include(callBlock.toString(), 'with');
|
|
} else {
|
|
chai.assert.notInclude(callBlock.toString(), 'with');
|
|
}
|
|
|
|
assertCallBlockArgsStructure(callBlock, args);
|
|
assertBlockVarModels(callBlock, varIds);
|
|
if (name !== undefined) {
|
|
chai.assert.equal(callBlock.getFieldValue('NAME'), name);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates procedure definition block using domToBlock call.
|
|
* @param {!Blockly.Workspace} workspace The Blockly workspace.
|
|
* @param {boolean=} hasReturn Whether the procedure definition should have
|
|
* return.
|
|
* @param {Array<string>=} args An array of argument names.
|
|
* @param {string=} name The name of the def block (defaults to 'proc name').
|
|
* @return {Blockly.Block} The created block.
|
|
*/
|
|
export function createProcDefBlock(
|
|
workspace, hasReturn = false, args = [], name = 'proc name') {
|
|
const type = hasReturn ?
|
|
'procedures_defreturn' : 'procedures_defnoreturn';
|
|
let xml = `<block type="${type}">`;
|
|
for (let i = 0; i < args.length; i ++) {
|
|
xml +=
|
|
` <mutation><arg name="${args[i]}"></arg></mutation>\n`;
|
|
}
|
|
xml +=
|
|
` <field name="NAME">${name}</field>` +
|
|
'</block>';
|
|
return Blockly.Xml.domToBlock(Blockly.utils.xml.textToDom(xml), workspace);
|
|
}
|
|
|
|
/**
|
|
* Creates procedure call block using domToBlock call.
|
|
* @param {!Blockly.Workspace} workspace The Blockly workspace.
|
|
* @param {boolean=} hasReturn Whether the corresponding procedure definition
|
|
* has return.
|
|
* @param {string=} name The name of the caller block (defaults to 'proc name').
|
|
* @return {Blockly.Block} The created block.
|
|
*/
|
|
export function createProcCallBlock(
|
|
workspace, hasReturn = false, name = 'proc name') {
|
|
const type = hasReturn ?
|
|
'procedures_callreturn' : 'procedures_callnoreturn';
|
|
return Blockly.Xml.domToBlock(Blockly.utils.xml.textToDom(
|
|
`<block type="${type}">` +
|
|
` <mutation name="${name}"/>` +
|
|
`</block>`
|
|
), workspace);
|
|
}
|
|
|
|
export class MockProcedureModel {
|
|
constructor() {
|
|
this.id = Blockly.utils.idGenerator.genUid();
|
|
this.name = '';
|
|
this.parameters = [];
|
|
this.returnTypes = null;
|
|
this.enabled = true;
|
|
}
|
|
|
|
setName(name) {
|
|
this.name = name;
|
|
return this;
|
|
}
|
|
|
|
insertParameter(parameterModel, index) {
|
|
this.parameters.splice(index, 0, parameterModel);
|
|
return this;
|
|
}
|
|
|
|
deleteParameter(index) {
|
|
this.parameters.splice(index, 1);
|
|
return this;
|
|
}
|
|
|
|
setReturnTypes(types) {
|
|
this.returnTypes = types;
|
|
return this;
|
|
}
|
|
|
|
setEnabled(enabled) {
|
|
this.enabled = enabled;
|
|
return this;
|
|
}
|
|
|
|
getId() {
|
|
return this.id;
|
|
}
|
|
|
|
getName() {
|
|
return this.name;
|
|
}
|
|
|
|
getParameter(index) {
|
|
return this.parameters[index];
|
|
}
|
|
|
|
getParameters() {
|
|
return [...this.parameters];
|
|
}
|
|
|
|
getReturnTypes() {
|
|
return this.returnTypes;
|
|
}
|
|
|
|
getEnabled() {
|
|
return this.enabled;
|
|
}
|
|
|
|
startPublishing() { }
|
|
|
|
stopPublishing() { }
|
|
}
|
|
|
|
export class MockParameterModel {
|
|
constructor(name) {
|
|
this.id = Blockly.utils.idGenerator.genUid();
|
|
this.name = name;
|
|
this.types = [];
|
|
}
|
|
|
|
setName(name) {
|
|
this.name = name;
|
|
return this;
|
|
}
|
|
|
|
setTypes(types) {
|
|
this.types = types;
|
|
return this;
|
|
}
|
|
|
|
getName() {
|
|
return this.name;
|
|
}
|
|
|
|
getTypes() {
|
|
return this.types;
|
|
}
|
|
|
|
getId() {
|
|
return this.id;
|
|
}
|
|
}
|