Files
blockly/tests/mocha/blocks/variables_test.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

311 lines
9.1 KiB
JavaScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
sharedTestSetup,
sharedTestTeardown,
} from '../test_helpers/setup_teardown.js';
import {nameUsedWithConflictingParam} from '../../../build/src/core/variables.js';
import {
MockParameterModelWithVar,
MockProcedureModel,
} from '../test_helpers/procedures.js';
suite('Variables', function () {
setup(function () {
sharedTestSetup.call(this);
this.workspace = new Blockly.Workspace();
Blockly.defineBlocksWithJsonArray([
{
'type': 'get_var_block',
'message0': '%1',
'args0': [
{
'type': 'field_variable',
'name': 'VAR',
'variableTypes': ['', 'type1', 'type2'],
},
],
},
]);
this.workspace.createVariable('foo', 'type1', '1');
this.workspace.createVariable('bar', 'type1', '2');
this.workspace.createVariable('baz', 'type1', '3');
});
teardown(function () {
sharedTestTeardown.call(this);
});
/**
* Create a test get_var_block.
* Will fail if get_var_block isn't defined.
* @param {!Blockly.Workspace} workspace The workspace on which to create the
* block.
* @param {!string} variableId The id of the variable to reference.
* @return {!Blockly.Block} The created block.
*/
function createTestVarBlock(workspace, variableId) {
// Turn off events to avoid testing XML at the same time.
Blockly.Events.disable();
const block = new Blockly.Block(workspace, 'get_var_block');
block.inputList[0].fieldRow[0].setValue(variableId);
Blockly.Events.enable();
return block;
}
suite('allUsedVarModels', function () {
test('All used', function () {
createTestVarBlock(this.workspace, '1');
createTestVarBlock(this.workspace, '2');
createTestVarBlock(this.workspace, '3');
const result = Blockly.Variables.allUsedVarModels(this.workspace);
chai.assert.equal(
result.length,
3,
'Expected three variables in the list of used variables',
);
});
test('Some unused', function () {
createTestVarBlock(this.workspace, '2');
const result = Blockly.Variables.allUsedVarModels(this.workspace);
chai.assert.equal(
result.length,
1,
'Expected one variable in the list of used variables',
);
chai.assert.equal(
result[0].getId(),
'2',
'Expected variable with ID 2 in the list of used variables',
);
});
test('Var used twice', function () {
createTestVarBlock(this.workspace, '2');
createTestVarBlock(this.workspace, '2');
const result = Blockly.Variables.allUsedVarModels(this.workspace);
// Using the same variable multiple times should not change the number of
// elements in the list.
chai.assert.equal(
result.length,
1,
'Expected one variable in the list of used variables',
);
chai.assert.equal(
result[0].getId(),
'2',
'Expected variable with ID 2 in the list of used variables',
);
});
test('All unused', function () {
const result = Blockly.Variables.allUsedVarModels(this.workspace);
chai.assert.equal(
result.length,
0,
'Expected no variables in the list of used variables',
);
});
});
suite('getVariable', function () {
test('By ID', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable(this.workspace, 'id1');
const result2 = Blockly.Variables.getVariable(this.workspace, 'id2');
const result3 = Blockly.Variables.getVariable(this.workspace, 'id3');
chai.assert.equal(var1, result1);
chai.assert.equal(var2, result2);
chai.assert.equal(var3, result3);
});
test('By name and type', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable(
this.workspace,
null,
'name1',
'type1',
);
const result2 = Blockly.Variables.getVariable(
this.workspace,
null,
'name2',
'type1',
);
const result3 = Blockly.Variables.getVariable(
this.workspace,
null,
'name3',
'type2',
);
// Searching by name + type is correct.
chai.assert.equal(var1, result1);
chai.assert.equal(var2, result2);
chai.assert.equal(var3, result3);
});
test('Bad ID with name and type fallback', function () {
const var1 = this.workspace.createVariable('name1', 'type1', 'id1');
const var2 = this.workspace.createVariable('name2', 'type1', 'id2');
const var3 = this.workspace.createVariable('name3', 'type2', 'id3');
const result1 = Blockly.Variables.getVariable(
this.workspace,
'badId',
'name1',
'type1',
);
const result2 = Blockly.Variables.getVariable(
this.workspace,
'badId',
'name2',
'type1',
);
const result3 = Blockly.Variables.getVariable(
this.workspace,
'badId',
'name3',
'type2',
);
// Searching by ID failed, but falling back onto name + type is correct.
chai.assert.equal(var1, result1);
chai.assert.equal(var2, result2);
chai.assert.equal(var3, result3);
});
});
suite('renaming variables creating conflicts', function () {
suite('renaming variables creating parameter conflicts', function () {
test('conflicts within legacy procedure blocks return the procedure name', function () {
Blockly.serialization.blocks.append(
{
'type': 'procedures_defnoreturn',
'extraState': {
'params': [
{
'name': 'x',
'id': '6l3P%Y!9EgA(Nh{E`Tl,',
},
{
'name': 'y',
'id': 'l1EtlJe%z_M[O-@uPAQ8',
},
],
},
'fields': {
'NAME': 'test name',
},
},
this.workspace,
);
chai.assert.equal(
'test name',
nameUsedWithConflictingParam('x', 'y', this.workspace),
'Expected the name of the procedure with the conflicting ' +
'param to be returned',
);
});
test(
'if no legacy block has the old var name, no procedure ' +
'name is returned',
function () {
Blockly.serialization.blocks.append(
{
'type': 'procedures_defnoreturn',
'extraState': {
'params': [
{
'name': 'definitely not x',
'id': '6l3P%Y!9EgA(Nh{E`Tl,',
},
{
'name': 'y',
'id': 'l1EtlJe%z_M[O-@uPAQ8',
},
],
},
'fields': {
'NAME': 'test name',
},
},
this.workspace,
);
chai.assert.isNull(
nameUsedWithConflictingParam('x', 'y', this.workspace),
'Expected there to be no conflict',
);
},
);
test('conflicts within procedure models return the procedure name', function () {
this.workspace
.getProcedureMap()
.add(
new MockProcedureModel('test name')
.insertParameter(
new MockParameterModelWithVar('x', this.workspace),
0,
)
.insertParameter(
new MockParameterModelWithVar('y', this.workspace),
0,
),
);
chai.assert.equal(
'test name',
nameUsedWithConflictingParam('x', 'y', this.workspace),
'Expected the name of the procedure with the conflicting ' +
'param to be returned',
);
});
test(
'if no procedure model has the old var, no procedure ' +
'name is returned',
function () {
this.workspace
.getProcedureMap()
.add(
new MockProcedureModel('test name')
.insertParameter(
new MockParameterModelWithVar(
'definitely not x',
this.workspace,
),
0,
)
.insertParameter(
new MockParameterModelWithVar('y', this.workspace),
0,
),
);
chai.assert.isNull(
nameUsedWithConflictingParam('x', 'y', this.workspace),
'Expected there to be no conflict',
);
},
);
});
});
});