mirror of
https://github.com/google/blockly.git
synced 2026-01-08 01:20:12 +01:00
* 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.
504 lines
14 KiB
JavaScript
504 lines
14 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2019 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
import * as Blockly from '../../build/src/core/blockly.js';
|
|
import {
|
|
assertFieldValue,
|
|
runConstructorSuiteTests,
|
|
runFromJsonSuiteTests,
|
|
runSetValueTests,
|
|
} from './test_helpers/fields.js';
|
|
import {defineRowBlock} from './test_helpers/block_definitions.js';
|
|
import {
|
|
sharedTestSetup,
|
|
sharedTestTeardown,
|
|
workspaceTeardown,
|
|
} from './test_helpers/setup_teardown.js';
|
|
import {runTestCases} from './test_helpers/common.js';
|
|
|
|
suite('Number 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},
|
|
{title: 'NaN', value: NaN},
|
|
{title: 'Non-Parsable String', value: 'bad'},
|
|
];
|
|
/**
|
|
* Configuration for field tests with valid values.
|
|
* @type {!Array<!FieldCreationTestCase>}
|
|
*/
|
|
const validValueTestCases = [
|
|
{title: 'Integer', value: 1, expectedValue: 1},
|
|
{title: 'Float', value: 1.5, expectedValue: 1.5},
|
|
{title: 'Integer String', value: '1', expectedValue: 1},
|
|
{title: 'Float String', value: '1.5', expectedValue: 1.5},
|
|
{title: 'Infinity', value: Infinity, expectedValue: Infinity},
|
|
{title: 'Negative Infinity', value: -Infinity, expectedValue: -Infinity},
|
|
{title: 'Infinity String', value: 'Infinity', expectedValue: Infinity},
|
|
{
|
|
title: 'Negative Infinity String',
|
|
value: '-Infinity',
|
|
expectedValue: -Infinity,
|
|
},
|
|
];
|
|
const addArgsAndJson = function (testCase) {
|
|
testCase.args = Array(4).fill(testCase.value);
|
|
testCase.json = {
|
|
'value': testCase.value,
|
|
'min': testCase.value,
|
|
'max': testCase.value,
|
|
'precision': testCase.value,
|
|
};
|
|
};
|
|
invalidValueTestCases.forEach(addArgsAndJson);
|
|
validValueTestCases.forEach(addArgsAndJson);
|
|
|
|
/**
|
|
* The expected default value for the field being tested.
|
|
* @type {*}
|
|
*/
|
|
const defaultFieldValue = 0;
|
|
/**
|
|
* Asserts that the field property values are as expected.
|
|
* @param {!Blockly.FieldNumber} field The field to check.
|
|
* @param {!number} expectedMin The expected min.
|
|
* @param {!number} expectedMax The expected max.
|
|
* @param {!number} expectedPrecision The expected precision.
|
|
* @param {!number} expectedValue The expected value.
|
|
*/
|
|
function assertNumberField(
|
|
field,
|
|
expectedMin,
|
|
expectedMax,
|
|
expectedPrecision,
|
|
expectedValue,
|
|
) {
|
|
assertFieldValue(field, expectedValue);
|
|
chai.assert.equal(field.getMin(), expectedMin, 'Min');
|
|
chai.assert.equal(field.getMax(), expectedMax, 'Max');
|
|
chai.assert.equal(field.getPrecision(), expectedPrecision, 'Precision');
|
|
}
|
|
/**
|
|
* Asserts that the field property values are set to default.
|
|
* @param {!Blockly.FieldNumber} field The field to check.
|
|
*/
|
|
const assertFieldDefault = function (field) {
|
|
assertNumberField(field, -Infinity, Infinity, 0, defaultFieldValue);
|
|
};
|
|
/**
|
|
* Asserts that the field properties are correct based on the test case.
|
|
* @param {!Blockly.FieldNumber} field The field to check.
|
|
* @param {!FieldValueTestCase} testCase The test case.
|
|
*/
|
|
const validTestCaseAssertField = function (field, testCase) {
|
|
assertNumberField(
|
|
field,
|
|
testCase.expectedValue,
|
|
testCase.expectedValue,
|
|
testCase.expectedValue,
|
|
testCase.expectedValue,
|
|
);
|
|
};
|
|
|
|
runConstructorSuiteTests(
|
|
Blockly.FieldNumber,
|
|
validValueTestCases,
|
|
invalidValueTestCases,
|
|
validTestCaseAssertField,
|
|
assertFieldDefault,
|
|
);
|
|
|
|
runFromJsonSuiteTests(
|
|
Blockly.FieldNumber,
|
|
validValueTestCases,
|
|
invalidValueTestCases,
|
|
validTestCaseAssertField,
|
|
assertFieldDefault,
|
|
);
|
|
|
|
suite('setValue', function () {
|
|
suite('Empty -> New Value', function () {
|
|
setup(function () {
|
|
this.field = new Blockly.FieldNumber();
|
|
});
|
|
runSetValueTests(
|
|
validValueTestCases,
|
|
invalidValueTestCases,
|
|
defaultFieldValue,
|
|
);
|
|
});
|
|
suite('Value -> New Value', function () {
|
|
const initialValue = 1;
|
|
setup(function () {
|
|
this.field = new Blockly.FieldNumber(initialValue);
|
|
});
|
|
runSetValueTests(
|
|
validValueTestCases,
|
|
invalidValueTestCases,
|
|
initialValue,
|
|
);
|
|
});
|
|
suite('Constraints', function () {
|
|
const testCases = [
|
|
{title: 'Float', json: {}, value: 123.456, expectedValue: 123.456},
|
|
{
|
|
title: '0.01',
|
|
json: {precision: 0.01},
|
|
value: 123.456,
|
|
expectedValue: 123.46,
|
|
},
|
|
{
|
|
title: '1e-7',
|
|
json: {precision: 0.0000001},
|
|
value: 123.00000456,
|
|
expectedValue: 123.0000046,
|
|
},
|
|
{
|
|
title: '0.5',
|
|
json: {precision: 0.5},
|
|
value: 123.456,
|
|
expectedValue: 123.5,
|
|
},
|
|
{title: '1', json: {precision: 1}, value: 123.456, expectedValue: 123},
|
|
{
|
|
title: '1.5',
|
|
json: {precision: 1.5},
|
|
value: 123.456,
|
|
expectedValue: 123,
|
|
},
|
|
];
|
|
suite('Precision', function () {
|
|
runTestCases(testCases, function (testCase) {
|
|
return function () {
|
|
const field = Blockly.FieldNumber.fromJson(testCase.json);
|
|
field.setValue(testCase.value);
|
|
assertFieldValue(field, testCase.expectedValue);
|
|
};
|
|
});
|
|
test('Null', function () {
|
|
const field = Blockly.FieldNumber.fromJson({precision: null});
|
|
chai.assert.equal(field.getPrecision(), 0);
|
|
});
|
|
});
|
|
const setValueBoundsTestFn = function (testCase) {
|
|
return function () {
|
|
const field = Blockly.FieldNumber.fromJson(testCase.json);
|
|
testCase.values.forEach(function (value, i) {
|
|
field.setValue(value);
|
|
assertFieldValue(field, testCase.expectedValues[i]);
|
|
});
|
|
};
|
|
};
|
|
suite('Min', function () {
|
|
const testCases = [
|
|
{
|
|
title: '-10',
|
|
json: {min: -10},
|
|
values: [-20, 0, 20],
|
|
expectedValues: [-10, 0, 20],
|
|
},
|
|
{
|
|
title: '0',
|
|
json: {min: 0},
|
|
values: [-20, 0, 20],
|
|
expectedValues: [0, 0, 20],
|
|
},
|
|
{
|
|
title: '+10',
|
|
json: {min: 10},
|
|
values: [-20, 0, 20],
|
|
expectedValues: [10, 10, 20],
|
|
},
|
|
];
|
|
runTestCases(testCases, setValueBoundsTestFn);
|
|
test('Null', function () {
|
|
const field = Blockly.FieldNumber.fromJson({min: null});
|
|
chai.assert.equal(field.getMin(), -Infinity);
|
|
});
|
|
});
|
|
suite('Max', function () {
|
|
const testCases = [
|
|
{
|
|
title: '-10',
|
|
json: {max: -10},
|
|
values: [-20, 0, 20],
|
|
expectedValues: [-20, -10, -10],
|
|
},
|
|
{
|
|
title: '0',
|
|
json: {max: 0},
|
|
values: [-20, 0, 20],
|
|
expectedValues: [-20, 0, 0],
|
|
},
|
|
{
|
|
title: '+10',
|
|
json: {max: 10},
|
|
values: [-20, 0, 20],
|
|
expectedValues: [-20, 0, 10],
|
|
},
|
|
];
|
|
runTestCases(testCases, setValueBoundsTestFn);
|
|
test('Null', function () {
|
|
const field = Blockly.FieldNumber.fromJson({max: null});
|
|
chai.assert.equal(field.getMax(), Infinity);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
suite('Validators', function () {
|
|
setup(function () {
|
|
this.field = new Blockly.FieldNumber(1);
|
|
this.field.htmlInput_ = document.createElement('input');
|
|
this.field.htmlInput_.setAttribute('data-old-value', '1');
|
|
this.field.htmlInput_.setAttribute('data-untyped-default-value', '1');
|
|
this.stub = sinon.stub(this.field, 'resizeEditor_');
|
|
});
|
|
teardown(function () {
|
|
sinon.restore();
|
|
});
|
|
const testSuites = [
|
|
{
|
|
title: 'Null Validator',
|
|
validator: function () {
|
|
return null;
|
|
},
|
|
value: 2,
|
|
expectedValue: '1',
|
|
},
|
|
{
|
|
title: 'Force End with 6 Validator',
|
|
validator: function (newValue) {
|
|
return +String(newValue).replace(/.$/, '6');
|
|
},
|
|
value: 25,
|
|
expectedValue: 26,
|
|
},
|
|
{
|
|
title: 'Returns Undefined Validator',
|
|
validator: function () {},
|
|
value: 2,
|
|
expectedValue: 2,
|
|
},
|
|
];
|
|
testSuites.forEach(function (suiteInfo) {
|
|
suite(suiteInfo.title, function () {
|
|
setup(function () {
|
|
this.field.setValidator(suiteInfo.validator);
|
|
});
|
|
test('When Editing', function () {
|
|
this.field.isBeingEdited_ = true;
|
|
this.field.htmlInput_.value = String(suiteInfo.value);
|
|
this.field.onHtmlInputChange_(null);
|
|
assertFieldValue(
|
|
this.field,
|
|
suiteInfo.expectedValue,
|
|
String(suiteInfo.value),
|
|
);
|
|
});
|
|
test('When Not Editing', function () {
|
|
this.field.setValue(suiteInfo.value);
|
|
assertFieldValue(this.field, +suiteInfo.expectedValue);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
suite('Customizations', function () {
|
|
suite('Min', function () {
|
|
test('JS Constructor', function () {
|
|
const field = new Blockly.FieldNumber(0, -10);
|
|
assertNumberField(field, -10, Infinity, 0, 0);
|
|
});
|
|
test('JSON Definition', function () {
|
|
const field = Blockly.FieldNumber.fromJson({
|
|
min: -10,
|
|
});
|
|
assertNumberField(field, -10, Infinity, 0, 0);
|
|
});
|
|
test('Set Constraints', function () {
|
|
const field = new Blockly.FieldNumber();
|
|
field.setConstraints(-10);
|
|
assertNumberField(field, -10, Infinity, 0, 0);
|
|
});
|
|
test('Set Min', function () {
|
|
const field = new Blockly.FieldNumber();
|
|
field.setMin(-10);
|
|
assertNumberField(field, -10, Infinity, 0, 0);
|
|
});
|
|
test('JS Configuration - Simple', function () {
|
|
const field = new Blockly.FieldNumber(
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
{
|
|
min: -10,
|
|
},
|
|
);
|
|
assertNumberField(field, -10, Infinity, 0, 0);
|
|
});
|
|
test('JS Configuration - Ignore', function () {
|
|
const field = new Blockly.FieldNumber(
|
|
undefined,
|
|
-1,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
{
|
|
min: -10,
|
|
},
|
|
);
|
|
assertNumberField(field, -10, Infinity, 0, 0);
|
|
});
|
|
});
|
|
suite('Max', function () {
|
|
test('JS Constructor', function () {
|
|
const field = new Blockly.FieldNumber(0, undefined, 10);
|
|
assertNumberField(field, -Infinity, 10, 0, 0);
|
|
});
|
|
test('JSON Definition', function () {
|
|
const field = Blockly.FieldNumber.fromJson({
|
|
max: 10,
|
|
});
|
|
assertNumberField(field, -Infinity, 10, 0, 0);
|
|
});
|
|
test('Set Constraints', function () {
|
|
const field = new Blockly.FieldNumber();
|
|
field.setConstraints(undefined, 10);
|
|
assertNumberField(field, -Infinity, 10, 0, 0);
|
|
});
|
|
test('Set Max', function () {
|
|
const field = new Blockly.FieldNumber();
|
|
field.setMax(10);
|
|
assertNumberField(field, -Infinity, 10, 0, 0);
|
|
});
|
|
test('JS Configuration - Simple', function () {
|
|
const field = new Blockly.FieldNumber(
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
{
|
|
max: 10,
|
|
},
|
|
);
|
|
assertNumberField(field, -Infinity, 10, 0, 0);
|
|
});
|
|
test('JS Configuration - Ignore', function () {
|
|
const field = new Blockly.FieldNumber(
|
|
undefined,
|
|
undefined,
|
|
1,
|
|
undefined,
|
|
undefined,
|
|
{
|
|
max: 10,
|
|
},
|
|
);
|
|
assertNumberField(field, -Infinity, 10, 0, 0);
|
|
});
|
|
});
|
|
suite('Precision', function () {
|
|
test('JS Constructor', function () {
|
|
const field = new Blockly.FieldNumber(0, undefined, undefined, 1);
|
|
assertNumberField(field, -Infinity, Infinity, 1, 0);
|
|
});
|
|
test('JSON Definition', function () {
|
|
const field = Blockly.FieldNumber.fromJson({
|
|
precision: 1,
|
|
});
|
|
assertNumberField(field, -Infinity, Infinity, 1, 0);
|
|
});
|
|
test('Set Constraints', function () {
|
|
const field = new Blockly.FieldNumber();
|
|
field.setConstraints(undefined, undefined, 1);
|
|
assertNumberField(field, -Infinity, Infinity, 1, 0);
|
|
});
|
|
test('Set Precision', function () {
|
|
const field = new Blockly.FieldNumber();
|
|
field.setPrecision(1);
|
|
assertNumberField(field, -Infinity, Infinity, 1, 0);
|
|
});
|
|
test('JS Configuration - Simple', function () {
|
|
const field = new Blockly.FieldNumber(
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
{
|
|
precision: 1,
|
|
},
|
|
);
|
|
assertNumberField(field, -Infinity, Infinity, 1, 0);
|
|
});
|
|
test('JS Configuration - Ignore', function () {
|
|
const field = new Blockly.FieldNumber(
|
|
undefined,
|
|
undefined,
|
|
undefined,
|
|
0.5,
|
|
undefined,
|
|
{
|
|
precision: 1,
|
|
},
|
|
);
|
|
assertNumberField(field, -Infinity, Infinity, 1, 0);
|
|
});
|
|
});
|
|
});
|
|
|
|
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.FieldNumber(value);
|
|
block.getInput('INPUT').appendField(field, 'NUMBER');
|
|
const jso = Blockly.serialization.blocks.save(block);
|
|
chai.assert.deepEqual(jso['fields'], {'NUMBER': value});
|
|
};
|
|
});
|
|
|
|
teardown(function () {
|
|
workspaceTeardown.call(this, this.workspace);
|
|
});
|
|
|
|
test('Simple', function () {
|
|
this.assertValue(10);
|
|
});
|
|
|
|
test('Max precision small', function () {
|
|
this.assertValue(1.000000000000001);
|
|
});
|
|
|
|
test('Max precision large', function () {
|
|
this.assertValue(1000000000000001);
|
|
});
|
|
|
|
test('Smallest', function () {
|
|
this.assertValue(5e-324);
|
|
});
|
|
|
|
test('Largest', function () {
|
|
this.assertValue(1.7976931348623157e308);
|
|
});
|
|
});
|
|
});
|