Files
blockly/tests/mocha/field_multilineinput_test.js
Christopher Allen f947b3f4f6 refactor!: Remove remaining use of goog.module.declareLegacyNamespace. (#6254)
* fix(build): Minor corrections to build_tasks.js

  - Use TSC_OUTPUT_DIR to find goog/goog.js when suppressing warnings.
  - Remove unnecessary trailing semicolons.

* refactor(blocks): Remove declareLegacyNamespace

  Remove the call to goog.module.declareLegacyNamespace from
  Blockly.libraryBlocks.  This entails:

  - Changes to the UMD wrapper to be able to find the exports object.
  - Changes to tests/bootstrap_helper.js to save the exports object
    in the libraryBlocks global variable.
  - As a precaution, renaming the tests/compile/test_blocks.js module
    so that goog.provide does not touch Blockly or
    Blockly.libraryBlocks, which may not exist / be writable.

  * feat(build): Add support named exports from chunks

  We need to convert the generators to named exports.  For backwards
  compatibility we still want e.g. Blockly.JavaScript to point at
  the generator object when the chunk is loaded using a script tag.

  Modify chunkWrapper to honour a .reexportOnly property in the
  chunks table and generate suitable additional code in the UMD
  wrapper.

* refactor(generators): Migrate JavaScript generator to named export

  - Export the JavaScript generator object as javascriptGenerator
    from the Blockly.JavaScript module(generators/javascript.js).

  - Modify the Blockly.JavaScript.all module
    (generators/javascript/all.js) to reexport the exports from
    Blockly.JavaScript.

  - Update chunk configuration so the generator object remains
    available as Blockly.JavaScript when loading
    javascript_compressed.js via a <script> tag.

    (N.B. it is otherwise necessary to destructure the require
    / import.)

  - Modify bootstrap_helper.js to store that export as
    window.javascriptGenerator for use in test code.

  - Modify test code to use javascriptGenerator instead of
    Blockly.JavaScript.

  - Modify .eslintrc.json so that javascriptGenerator is allowed
    as a global in test/.  (Also restrict use of Blockly global
    to test/.)

  N.B. that demo code in demos/code/code.js uses <script> tag
  loading and so will continue to access Blockly.JavaScript.

* refactor(generators): Migrate Lua generator to named export

* refactor(generators): Migrate PHP generator to named export

* refactor(generators): Migrate Python generator to named export

* refactor(generators): Remove declareLegacyNamespace calls

  Remove the goog.module.declareLegacyNamespace calls from the
  generators.

  This turns out to have the unexpected side-effect of causing the
  compiler to rename the core/blockly.js exports object from
  $.Blockly to just Blockly in blockly_compressed.js - presumably
  because it no longer needs to be accessed in any subsequent chunk
  because they no longer add properties to it.  This requires
  some changes (mainly simplification) to the chunkWrapper function
  in build_tasks.js.

* refactor(core): Remove declareLegacyNamespace from blockly.js

  So easy to do _now_: just need to:

  - Make sure the UMD wrapper for the first chunk knows where the
    exports object is.
  - Use that same value to set the Blockly.VERSION @define.
  - Have bootstrap_helper.js set window.Blockly to the exports
    object.
  - Fix tests/compile/test_blocks.js to not assume a Blockly
    global variable, by converting it to a goog.module so we
    can use a named require.
2022-06-30 19:53:32 +01:00

192 lines
7.3 KiB
JavaScript

/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.module('Blockly.test.fieldMultiline');
const {assertFieldValue, runConstructorSuiteTests, runFromJsonSuiteTests, runSetValueTests} = goog.require('Blockly.test.helpers.fields');
const {createTestBlock, defineRowBlock} = goog.require('Blockly.test.helpers.blockDefinitions');
const {sharedTestSetup, sharedTestTeardown, workspaceTeardown} = goog.require('Blockly.test.helpers.setupTeardown');
const {runCodeGenerationTestSuites} = goog.require('Blockly.test.helpers.codeGeneration');
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');
});
});
});