mirror of
https://github.com/google/blockly.git
synced 2026-01-06 08:30:13 +01:00
* refactor(generators): Move lang.js -> lang/lang_gernator.js
Move the LangGenerator definitions into their respective
subdirectories and add a _generator suffix to their filenames,
i.e. generators/javascript.js becomes
generators/javascript/javascript_generator.js.
This is to keep related code together and allow the `lang/all.js`
entrypoints to be moved to the top level generators/ directory.
No goog module IDs were changed, so playground and test code
that accesses this modules by filename does not need to be modified.
* refactor(generators) Move lang/all.js -> lang.js
- Move the entrypoints in generators/*/all.js to correspondingly-named
files in generators/ instead—i.e., generators/javascript/all.js
becomes generators/javascript.js.
- Update build_tasks.js accordingly.
* fix(generators): Add missing exports for LuaGenerator, PhpGenerator
These were inadvertently omitted from #7161 and #7162, respectively.
* refactor(generators): Make block generator modules side-effect free
- Move declaration of <lang>Generator instance from
generators/<lang>/<lang>_generator.js to generators/<lang>.js.
- Move .addReservedWords() calls from generators/<lang>/*.js to
generators/<lang>.js
- Modify generators/<lang>/*.js to export block generator functions
individually, rather than installing on <lang>Generator instance.
- Modify generators/<lang>.js to import and install block generator
functions on <lang>Generator instance.
* fix(tests): Fix tests broken by restructuring of generators
Where these tests needed block generator functions preinstalled
they should have been importing the Blockly.<Lang>.all module.
Where they do not need the provided block generator functions
they can now create their own empty <Lang>Generator instances.
* chore: Update renamings file
- Fix a malformation in previous entries that was not detected by
the renaming file validator test.
- Add entries describing the work done in this and related recent
PRs.
* fix: Correct minor errors in PR #7173
- Fix a search-and-replace error in renamings.json5
- Fix an incorrect-but-usable import in generator_test.js
287 lines
8.4 KiB
JavaScript
287 lines
8.4 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2020 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
goog.declareModuleId('Blockly.test.fieldMultiline');
|
|
|
|
import * as Blockly from '../../build/src/core/blockly.js';
|
|
import {
|
|
assertFieldValue,
|
|
runConstructorSuiteTests,
|
|
runFromJsonSuiteTests,
|
|
runSetValueTests,
|
|
} from './test_helpers/fields.js';
|
|
import {
|
|
createTestBlock,
|
|
defineRowBlock,
|
|
} from './test_helpers/block_definitions.js';
|
|
import {
|
|
sharedTestSetup,
|
|
sharedTestTeardown,
|
|
workspaceTeardown,
|
|
} from './test_helpers/setup_teardown.js';
|
|
import {runCodeGenerationTestSuites} from './test_helpers/code_generation.js';
|
|
const {dartGenerator} = goog.require('Blockly.Dart.all');
|
|
const {javascriptGenerator} = goog.require('Blockly.JavaScript.all');
|
|
const {luaGenerator} = goog.require('Blockly.Lua.all');
|
|
const {phpGenerator} = goog.require('Blockly.PHP.all');
|
|
const {pythonGenerator} = goog.require('Blockly.Python.all');
|
|
|
|
suite('Multiline Input Fields', function () {
|
|
setup(function () {
|
|
sharedTestSetup.call(this);
|
|
});
|
|
teardown(function () {
|
|
sharedTestTeardown.call(this);
|
|
});
|
|
/**
|
|
* Configuration for field tests with invalid values.
|
|
* @type {!Array<!FieldCreationTestCase>}
|
|
*/
|
|
const invalidValueTestCases = [
|
|
{title: 'Undefined', value: undefined},
|
|
{title: 'Null', value: null},
|
|
];
|
|
/**
|
|
* Configuration for field tests with valid values.
|
|
* @type {!Array<!FieldCreationTestCase>}
|
|
*/
|
|
const validValueTestCases = [
|
|
{title: 'Empty string', value: '', expectedValue: ''},
|
|
{title: 'String no newline', value: 'value', expectedValue: 'value'},
|
|
{
|
|
title: 'String with newline',
|
|
value: 'bark bark\n bark bark bark\n bark bar bark bark\n',
|
|
expectedValue: 'bark bark\n bark bark bark\n bark bar bark bark\n',
|
|
},
|
|
{title: 'Boolean true', value: true, expectedValue: 'true'},
|
|
{title: 'Boolean false', value: false, expectedValue: 'false'},
|
|
{title: 'Number (Truthy)', value: 1, expectedValue: '1'},
|
|
{title: 'Number (Falsy)', value: 0, expectedValue: '0'},
|
|
{title: 'NaN', value: NaN, expectedValue: 'NaN'},
|
|
];
|
|
const addArgsAndJson = function (testCase) {
|
|
testCase.args = [testCase.value];
|
|
testCase.json = {'text': testCase.value};
|
|
};
|
|
invalidValueTestCases.forEach(addArgsAndJson);
|
|
validValueTestCases.forEach(addArgsAndJson);
|
|
|
|
/**
|
|
* The expected default value for the field being tested.
|
|
* @type {*}
|
|
*/
|
|
const defaultFieldValue = '';
|
|
/**
|
|
* Asserts that the field property values are set to default.
|
|
* @param {!Blockly.FieldMultilineInput} field The field to check.
|
|
*/
|
|
const assertFieldDefault = function (field) {
|
|
assertFieldValue(field, defaultFieldValue);
|
|
};
|
|
/**
|
|
* Asserts that the field properties are correct based on the test case.
|
|
* @param {!Blockly.FieldMultilineInput} field The field to check.
|
|
* @param {!FieldValueTestCase} testCase The test case.
|
|
*/
|
|
const validTestCaseAssertField = function (field, testCase) {
|
|
assertFieldValue(field, testCase.expectedValue);
|
|
};
|
|
|
|
runConstructorSuiteTests(
|
|
Blockly.FieldMultilineInput,
|
|
validValueTestCases,
|
|
invalidValueTestCases,
|
|
validTestCaseAssertField,
|
|
assertFieldDefault
|
|
);
|
|
|
|
runFromJsonSuiteTests(
|
|
Blockly.FieldMultilineInput,
|
|
validValueTestCases,
|
|
invalidValueTestCases,
|
|
validTestCaseAssertField,
|
|
assertFieldDefault
|
|
);
|
|
|
|
suite('setValue', function () {
|
|
suite('Empty -> New Value', function () {
|
|
setup(function () {
|
|
this.field = new Blockly.FieldMultilineInput();
|
|
});
|
|
runSetValueTests(
|
|
validValueTestCases,
|
|
invalidValueTestCases,
|
|
defaultFieldValue
|
|
);
|
|
test('With source block', function () {
|
|
this.field.setSourceBlock(createTestBlock());
|
|
this.field.setValue('value');
|
|
assertFieldValue(this.field, 'value');
|
|
});
|
|
});
|
|
suite('Value -> New Value', function () {
|
|
const initialValue = 'oldValue';
|
|
setup(function () {
|
|
this.field = new Blockly.FieldMultilineInput(initialValue);
|
|
});
|
|
runSetValueTests(
|
|
validValueTestCases,
|
|
invalidValueTestCases,
|
|
initialValue
|
|
);
|
|
test('With source block', function () {
|
|
this.field.setSourceBlock(createTestBlock());
|
|
this.field.setValue('value');
|
|
assertFieldValue(this.field, 'value');
|
|
});
|
|
});
|
|
});
|
|
|
|
suite('blockToCode', function () {
|
|
setup(function () {
|
|
this.workspace = new Blockly.Workspace();
|
|
});
|
|
const createBlockFn = (value) => {
|
|
return (workspace) => {
|
|
const block = workspace.newBlock('text_multiline');
|
|
const textField = block.getField('TEXT');
|
|
textField.setValue(value);
|
|
return block;
|
|
};
|
|
};
|
|
|
|
/**
|
|
* Test suites for code generation tests.s
|
|
* @type {Array<CodeGenerationTestSuite>}
|
|
*/
|
|
const testSuites = [
|
|
{
|
|
title: 'Dart',
|
|
generator: dartGenerator,
|
|
testCases: [
|
|
{
|
|
title: 'Empty string',
|
|
expectedCode: "''",
|
|
createBlock: createBlockFn(''),
|
|
},
|
|
{
|
|
title: 'String with newline',
|
|
expectedCode:
|
|
"'bark bark' + '\\n' + \n' bark bark bark' + '\\n' + \n' bark bar bark bark' + '\\n' + \n''",
|
|
createBlock: createBlockFn(
|
|
'bark bark\n bark bark bark\n bark bar bark bark\n'
|
|
),
|
|
},
|
|
],
|
|
},
|
|
{
|
|
title: 'JavaScript',
|
|
generator: javascriptGenerator,
|
|
testCases: [
|
|
{
|
|
title: 'Empty string',
|
|
expectedCode: "''",
|
|
createBlock: createBlockFn(''),
|
|
},
|
|
{
|
|
title: 'String with newline',
|
|
expectedCode:
|
|
"'bark bark' + '\\n' +\n' bark bark bark' + '\\n' +\n' bark bar bark bark' + '\\n' +\n''",
|
|
createBlock: createBlockFn(
|
|
'bark bark\n bark bark bark\n bark bar bark bark\n'
|
|
),
|
|
},
|
|
],
|
|
},
|
|
{
|
|
title: 'Lua',
|
|
generator: luaGenerator,
|
|
testCases: [
|
|
{
|
|
title: 'Empty string',
|
|
expectedCode: "''",
|
|
createBlock: createBlockFn(''),
|
|
},
|
|
{
|
|
title: 'String with newline',
|
|
expectedCode:
|
|
"'bark bark' .. '\\n' ..\n' bark bark bark' .. '\\n' ..\n' bark bar bark bark' .. '\\n' ..\n''",
|
|
createBlock: createBlockFn(
|
|
'bark bark\n bark bark bark\n bark bar bark bark\n'
|
|
),
|
|
},
|
|
],
|
|
},
|
|
{
|
|
title: 'PHP',
|
|
generator: phpGenerator,
|
|
testCases: [
|
|
{
|
|
title: 'Empty string',
|
|
expectedCode: "''",
|
|
createBlock: createBlockFn(''),
|
|
},
|
|
{
|
|
title: 'String with newline',
|
|
expectedCode:
|
|
"'bark bark' . \"\\n\" .\n' bark bark bark' . \"\\n\" .\n' bark bar bark bark' . \"\\n\" .\n''",
|
|
createBlock: createBlockFn(
|
|
'bark bark\n bark bark bark\n bark bar bark bark\n'
|
|
),
|
|
},
|
|
],
|
|
},
|
|
{
|
|
title: 'Python',
|
|
generator: pythonGenerator,
|
|
testCases: [
|
|
{
|
|
title: 'Empty string',
|
|
expectedCode: "''",
|
|
createBlock: createBlockFn(''),
|
|
},
|
|
{
|
|
title: 'String with newline',
|
|
expectedCode:
|
|
"'bark bark' + '\\n' + \n' bark bark bark' + '\\n' + \n' bark bar bark bark' + '\\n' + \n''",
|
|
createBlock: createBlockFn(
|
|
'bark bark\n bark bark bark\n bark bar bark bark\n'
|
|
),
|
|
},
|
|
],
|
|
},
|
|
];
|
|
runCodeGenerationTestSuites(testSuites);
|
|
});
|
|
|
|
suite('Serialization', function () {
|
|
setup(function () {
|
|
this.workspace = new Blockly.Workspace();
|
|
defineRowBlock();
|
|
|
|
this.assertValue = (value) => {
|
|
const block = this.workspace.newBlock('row_block');
|
|
const field = new Blockly.FieldMultilineInput(value);
|
|
block.getInput('INPUT').appendField(field, 'MULTILINE');
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
chai.assert.deepEqual(jso['fields'], {'MULTILINE': value});
|
|
};
|
|
});
|
|
|
|
teardown(function () {
|
|
workspaceTeardown.call(this, this.workspace);
|
|
});
|
|
|
|
test('Single line', function () {
|
|
this.assertValue('this is a single line');
|
|
});
|
|
|
|
test('Multiple lines', function () {
|
|
this.assertValue('this\nis\n multiple\n lines');
|
|
});
|
|
});
|
|
});
|