Files
blockly/tests/mocha/generator_test.js
dependabot[bot] bfb5b1dd49 chore(deps): Bump chai from 4.3.10 to 5.1.1 (#8092)
* chore(deps): Bump chai from 4.3.10 to 5.1.1

  Bumps [chai](https://github.com/chaijs/chai) from 4.3.10 to 5.1.1.
  - [Release notes](https://github.com/chaijs/chai/releases)
  - [Changelog](https://github.com/chaijs/chai/blob/main/History.md)
  - [Commits](https://github.com/chaijs/chai/compare/v4.3.10...v5.1.1)

  ---
  updated-dependencies:
  - dependency-name: chai
    dependency-type: direct:development
    update-type: version-update:semver-major
  ...

  Signed-off-by: dependabot[bot] <support@github.com>

* fix(tests): Migrate all usage of chai to ESM (#8216)

* fix(tests): Migrate node tests from CJS to ESM

  This allows us to import (rather than require) chai, fixing failures
  caused by that package dropping suppport for CJS in chai v5.0.0.

* fix(tests): Have mocha tests directly import chai

  Previously they relied on obtaining it from the global scope, but it's
  better if imports are explicit.

* fix(tests): Remove broken load of chai as script

  Chai v5.0.0 no longer supports being loaded as a script, so this did
  nothing but emit an syntax error message on the console.

* fix(tests): Migrate browser tests from CJS to ESM

  This allows us to import (rather than require) chai, fixing failures
  caused by chai no longer supporting CJS.

* chore(tests): format

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Christopher Allen <cpcallen+git@google.com>
2024-06-17 16:48:21 +01:00

234 lines
6.7 KiB
JavaScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {assert} from '../../node_modules/chai/chai.js';
import * as Blockly from '../../build/src/core/blockly.js';
import {DartGenerator} from '../../build/src/generators/dart/dart_generator.js';
import {JavascriptGenerator} from '../../build/src/generators/javascript/javascript_generator.js';
import {LuaGenerator} from '../../build/src/generators/lua/lua_generator.js';
import {PhpGenerator} from '../../build/src/generators/php/php_generator.js';
import {PythonGenerator} from '../../build/src/generators/python/python_generator.js';
import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
suite('Generator', function () {
setup(function () {
sharedTestSetup.call(this);
this.workspace = new Blockly.Workspace();
});
teardown(function () {
sharedTestTeardown.call(this);
});
suite('prefix', function () {
setup(function () {
this.generator = new Blockly.CodeGenerator('INTERCAL');
});
test('Nothing', function () {
assert.equal(this.generator.prefixLines('', ''), '');
});
test('One word', function () {
assert.equal(this.generator.prefixLines('Hello', '@'), '@Hello');
});
test('One line', function () {
assert.equal(this.generator.prefixLines('Hello\n', '12'), '12Hello\n');
});
test('Two lines', function () {
assert.equal(
this.generator.prefixLines('Hello\nWorld\n', '***'),
'***Hello\n***World\n',
);
});
});
suite('blockToCode', function () {
setup(function () {
Blockly.defineBlocksWithJsonArray([
{
'type': 'stack_block',
'message0': '',
'previousStatement': null,
'nextStatement': null,
},
{
'type': 'row_block',
'message0': '%1',
'args0': [
{
'type': 'input_value',
'name': 'INPUT',
},
],
'output': null,
'nextStatement': null,
},
]);
const rowBlock = this.workspace.newBlock('row_block');
const stackBlock = this.workspace.newBlock('stack_block');
this.blockToCodeTest = function (
generator,
blockDisabled,
opt_thisOnly,
expectedCode,
opt_message,
) {
generator.forBlock['row_block'] = function (_) {
return 'row_block';
};
generator.forBlock['stack_block'] = function (_) {
return 'stack_block';
};
rowBlock.nextConnection.connect(stackBlock.previousConnection);
rowBlock.setDisabledReason(blockDisabled, 'test reason');
const code = generator.blockToCode(rowBlock, opt_thisOnly);
delete generator.forBlock['stack_block'];
delete generator.forBlock['row_block'];
assert.equal(code, expectedCode, opt_message);
};
});
const testCase = [
[new DartGenerator(), 'Dart'],
[new JavascriptGenerator(), 'JavaScript'],
[new LuaGenerator(), 'Lua'],
[new PhpGenerator(), 'PHP'],
[new PythonGenerator(), 'Python'],
];
suite('Trivial', function () {
testCase.forEach(function (testCase) {
const generator = testCase[0];
const name = testCase[1];
test(name, function () {
generator.init(this.workspace);
this.blockToCodeTest(
generator,
/* blockDisabled = */ false,
/* opt_thisOnly = */ true,
'row_block',
);
this.blockToCodeTest(
generator,
/* blockDisabled = */ false,
/* opt_thisOnly = */ false,
'row_blockstack_block',
'thisOnly=false',
);
});
});
});
suite('Disabled block', function () {
testCase.forEach(function (testCase) {
const generator = testCase[0];
const name = testCase[1];
test(name, function () {
this.blockToCodeTest(
generator,
/* blockDisabled = */ true,
/* opt_thisOnly = */ true,
'',
);
this.blockToCodeTest(
generator,
/* blockDisabled = */ true,
/* opt_thisOnly = */ false,
'stack_block',
'thisOnly=false',
);
});
});
});
suite('Nested block', function () {
setup(function () {
Blockly.defineBlocksWithJsonArray([
{
'type': 'test_loop_block',
'message0': 'Repeat Loop',
'message1': '%1',
'args1': [
{
'type': 'input_statement',
'name': 'DO',
},
],
'previousStatement': null,
'nextStatement': null,
},
]);
const blockA = this.workspace.newBlock('test_loop_block');
const blockB = this.workspace.newBlock('test_loop_block');
const blockC = this.workspace.newBlock('test_loop_block');
this.loopTest = function (
generator,
opt_thisOnly,
expectedCode,
opt_message,
) {
generator.forBlock['test_loop_block'] = function (block) {
return '{' + generator.statementToCode(block, 'DO') + '}';
};
blockA.getInput('DO').connection.connect(blockB.previousConnection);
blockA.nextConnection.connect(blockC.previousConnection);
const code = generator.blockToCode(blockA, opt_thisOnly);
assert.equal(code, expectedCode, opt_message);
};
});
testCase.forEach(function (testCase) {
const generator = testCase[0];
const name = testCase[1];
test(name, function () {
this.loopTest(generator, true, '{ {}}');
this.loopTest(generator, false, '{ {}}{}', 'thisOnly=false');
});
});
});
suite('Names', function () {
setup(function () {
class TestGenerator extends Blockly.CodeGenerator {
init() {
super.init();
this.nameDB_ = sinon.createStubInstance(Blockly.Names, {
getName: 'foo',
});
}
}
this.generator = new TestGenerator();
});
test('No nameDB_ initialized', function () {
assert.throws(() => {
this.generator.getVariableName('foo');
});
});
test('Get variable name', function () {
this.generator.init();
assert.equal(this.generator.getVariableName('foo'), 'foo');
});
test('Get procedure name', function () {
this.generator.init();
assert.equal(this.generator.getProcedureName('foo'), 'foo');
});
});
});
});