Files
blockly/tests/mocha/generator_test.js
Beka Westberg 96758fedd4 chore: convert mocha tests and test helpers to esmodules (#6333)
* chore: change goog.module to goog.declareModuleId

* chore: change test helper exports to esmod exports

* chore: change test helpers to use esmodule imports

* chore: convert imports of test helpers to esmodule imports

* chore: convert other imports in tests to esm imports

* fix: make imports use built files

* chore: add blockly imports to a bunch of tests

* fix: reference Blockly.Blocks instead of Blocks'

* fix: properly import generators

* chore: fix lint

* chore: cleanup from rebase

* chore: cleanup from rebase

* chore: fix blocks tests
2022-08-10 14:54:02 -07:00

157 lines
4.9 KiB
JavaScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.declareModuleId('Blockly.test.generator');
import * as Blockly from '../../build/src/core/blockly.js';
const {dartGenerator} = goog.require('Blockly.Dart');
const {javascriptGenerator} = goog.require('Blockly.JavaScript');
const {luaGenerator} = goog.require('Blockly.Lua');
const {phpGenerator} = goog.require('Blockly.PHP');
const {pythonGenerator} = goog.require('Blockly.Python');
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.Generator('INTERCAL');
});
test('Nothing', function() {
chai.assert.equal(this.generator.prefixLines('', ''), '');
});
test('One word', function() {
chai.assert.equal(this.generator.prefixLines('Hello', '@'), '@Hello');
});
test('One line', function() {
chai.assert.equal(this.generator.prefixLines('Hello\n', '12'), '12Hello\n');
});
test('Two lines', function() {
chai.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.row_block = function(_) {return 'row_block';};
generator.stack_block = function(_) {return 'stack_block';};
rowBlock.nextConnection.connect(stackBlock.previousConnection);
rowBlock.disabled = blockDisabled;
const code = generator.blockToCode(rowBlock, opt_thisOnly);
chai.assert.equal(code, expectedCode, opt_message);
};
});
const testCase = [
[dartGenerator, 'Dart'],
[javascriptGenerator, 'JavaScript'],
[luaGenerator, 'Lua'],
[phpGenerator, 'PHP'],
[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, false, true, 'row_block');
this.blockToCodeTest(
generator, false, 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, true, true, '');
this.blockToCodeTest(generator, true, 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.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);
chai.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');
});
});
});
});
});