Skip to content

Commit

Permalink
fix blocks test runner (#9881)
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll authored Feb 22, 2024
1 parent b0ddc97 commit d62312e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
4 changes: 3 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,8 @@ const karma = gulp.series(buildKarmaRunner, browserifyKarma, runKarma);
const buildBlocksTestRunner = () => compileTsProject("tests/blocks-test", "built/", true);
const browserifyBlocksTestRunner = () =>
exec('node node_modules/browserify/bin/cmd built/tests/blocks-test/blocksrunner.js -o built/tests/blocksrunner.js --debug');
const browserifyBlocksPrep = () =>
exec('node node_modules/browserify/bin/cmd built/tests/blocks-test/blockssetup.js -o built/tests/blockssetup.js --debug');

const testAll = gulp.series(
testdecompiler,
Expand Down Expand Up @@ -824,7 +826,7 @@ const buildAll = gulp.series(
browserifyAssetEditor,
gulp.parallel(semanticjs, copyJquery, copyWebapp, copySemanticFonts, copyMonaco),
buildBlocksTestRunner,
browserifyBlocksTestRunner,
gulp.parallel(browserifyBlocksTestRunner, browserifyBlocksPrep),
runUglify
);

Expand Down
57 changes: 56 additions & 1 deletion pxtblocks/plugins/math/fieldSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,62 @@ export class FieldSlider extends Blockly.FieldNumber {
this.setConstraints(min, max, precision);

this.step_ = parseFloat(step) || undefined;
};
}

// Mostly the same as FieldNumber, but doesn't constrain min and max
protected override doClassValidation_(newValue?: any): number {
if (newValue === null) {
return null;
}

// Clean up text.
newValue = `${newValue}`;
// TODO: Handle cases like 'ten', '1.203,14', etc.
// 'O' is sometimes mistaken for '0' by inexperienced users.
newValue = newValue.replace(/O/gi, '0');
// Strip out thousands separators.
newValue = newValue.replace(/,/g, '');
// Ignore case of 'Infinity'.
newValue = newValue.replace(/infinity/i, 'Infinity');

// Clean up number.
let n = Number(newValue || 0);
if (isNaN(n)) {
// Invalid number.
return null;
}

// We allow values outside of the range with sliders in pxt
// n = Math.min(Math.max(n, this.min_), this.max_);

// Round to nearest multiple of precision.
if (this.precision_ && isFinite(n)) {
n = Math.round(n / this.precision_) * this.precision_;
}

let precisionString = String(this.precision_);
if (precisionString.indexOf('e') !== -1) {
// String() is fast. But it turns .0000001 into '1e-7'.
// Use the much slower toLocaleString to access all the digits.
precisionString = this.precision_.toLocaleString('en-US', {
maximumFractionDigits: 20,
});
}
const decimalIndex = precisionString.indexOf('.');
let decimalPlaces: number;
if (decimalIndex === -1) {
// If the precision is 0 (float) allow any number of decimals,
// otherwise allow none.
decimalPlaces = this.precision_ ? 0 : null;
} else {
decimalPlaces = precisionString.length - decimalIndex - 1;
}
// Clean up floating point errors.
if (decimalPlaces !== null) {
n = Number(n.toFixed(decimalPlaces));
}
return n;
}


protected showEditor_(_e?: Event, quietInput?: boolean): void {
Expand Down
2 changes: 1 addition & 1 deletion tests/blocks-test/blocksrunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function testXmlAsync(blocksfile: string) {
fail(e.message);
}

const err = compareBlocklyTrees(xml, Blockly.Xml.workspaceToDom(workspace));
const err = compareBlocklyTrees(xml, pxtblockly.workspaceToDom(workspace));
if (err) {
fail(`XML mismatch (${err.reason}) ${err.chain} \n See https://makecode.com/develop/blockstests for more info`);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/blocks-test/blockssetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path="..\..\localtypings\pxteditor.d.ts" />
/// <reference path="..\..\built\pxtcompiler.d.ts" />

import * as Blockly from "blockly";
import * as pxtblockly from "../../pxtblocks";

pxt.blocks.requireBlockly = () => Blockly;
pxt.blocks.requirePxtBlockly = () => pxtblockly;
1 change: 1 addition & 0 deletions tests/blocks-test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module.exports = function(config) {
'node_modules/pxt-core/built/pxtblocks.js',
'node_modules/pxt-core/built/pxtcompiler.js',
'node_modules/pxt-core/built/pxteditor.js',
'node_modules/pxt-core/built/tests/blockssetup.js',
'built/target.js',
'built/fieldeditors.js',

Expand Down

0 comments on commit d62312e

Please sign in to comment.