Files
blockly/tests/mocha/test_helpers/procedures.js
Christopher Allen 6f20ac290d refactor(tests): Use import instead of goog.bootstrap to load Blockly in mocha tests (#7406)
* fix(build): Have buildShims clean up up after itself

  We need to create a build/package.json file to allow node.js to
  load build/src/core/blockly.js and the other chunk entry points
  as ES modules (it forcibly assumes .js means CJS even if one is
  trying to import, unless package.json says {"type": "module"}),
  but this interferes with scripts/migration/js2ts doing a
  require('build/deps.js'), which is _not_ an ES module.

  Specific error message was:

  /Users/cpcallen/src/blockly/scripts/migration/js2ts:56
  require(path.resolve(__dirname, '../../build/deps.js'));
  ^

  Error [ERR_REQUIRE_ESM]: require() of ES Module
  /Users/cpcallen/src/blockly/build/deps.js from /Users/cpcallen/src/blockly/scripts/migration/js2ts
  not supported.
  deps.js is treated as an ES module file as it is a .js file whose
  nearest parent package.json contains "type": "module" which
  declares all .js files in that package scope as ES modules.
  Instead rename deps.js to end in .cjs, change the requiring code
  to use dynamic import() which is available in all CommonJS
  modules, or change "type": "module" to "type": "commonjs" in
  /Users/cpcallen/src/blockly/build/package.json to treat all .js
  files as CommonJS (using .mjs for all ES modules instead).

      at Object.<anonymous> (/Users/cpcallen/src/blockly/scripts/migration/js2ts:56:1) {
    code: 'ERR_REQUIRE_ESM'
  }

* chore(tests): Reorder to put interesting script nearer top of file

* chore(tests): Add missing imports of closure/goog/goog.js

  These modules were depending on being loaded via the
  debug module loader, which cannot be used without first loading
  base.js as a script, and thereby defining goog.declareModuleId
  as a side effect—but if they are to be loaded via direct import
  statements then they need to actually import their own
  dependencies.

  This is a temporary measure as soon the goog.declareMouleId
  calls can themselves be deleted.

* refactor(tests): Use import instead of bootstrap to load Blockly

* chores(build): Stop generating deps.mocha.js

  This file was only needed by tests/mocha/index.html's use of
  the debug module loader (via bootstrap.js), which has now been
  removed.

* chore(tests): Remove unneeded goog.declareModuleId calls

  These were only needed because these modules were previously
  being loaded by goog.require and/or goog.bootstrap.

* chores(tests): Remove dead code

  We are fully committed to proper modules now.
2023-08-18 18:06:52 +01:00

286 lines
7.0 KiB
JavaScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {ConnectionType} from '../../../build/src/core/connection_type.js';
import {VariableModel} from '../../../build/src/core/variable_model.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(name = '') {
this.id = Blockly.utils.idGenerator.genUid();
this.name = 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;
}
}
export class MockParameterModelWithVar extends MockParameterModel {
constructor(name, workspace) {
super(name);
this.variable = new VariableModel(workspace, name);
}
getVariableModel() {
return this.variable;
}
}