Files
blockly/tests/mocha/test_helpers/procedures.js
Maribeth Bottorff 88ff901a72 chore: use prettier instead of clang-format (#7014)
* chore: add and configure prettier

* chore: remove clang-format

* chore: remove clang-format config

* chore: lint additional ts files

* chore: fix lint errors in blocks

* chore: add prettier-ignore where needed

* chore: ignore js blocks when formatting

* chore: fix playground html syntax

* chore: fix yaml spacing from merge

* chore: convert text blocks to use arrow functions

* chore: format everything with prettier

* chore: fix lint unused imports in blocks
2023-05-10 16:01:39 -07:00

287 lines
7.1 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';
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;
}
}