mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-03 09:04:40 -04:00
Use round() instead of floor() in repeat, add unit test
This commit is contained in:
parent
f9814a5f88
commit
5af79a086b
2 changed files with 28 additions and 1 deletions
|
@ -49,7 +49,7 @@ class Scratch3ControlBlocks {
|
||||||
}
|
}
|
||||||
|
|
||||||
repeat (args, util) {
|
repeat (args, util) {
|
||||||
const times = Math.floor(Cast.toNumber(args.TIMES));
|
const times = Math.round(Cast.toNumber(args.TIMES));
|
||||||
// Initialize loop
|
// Initialize loop
|
||||||
if (typeof util.stackFrame.loopCounter === 'undefined') {
|
if (typeof util.stackFrame.loopCounter === 'undefined') {
|
||||||
util.stackFrame.loopCounter = times;
|
util.stackFrame.loopCounter = times;
|
||||||
|
|
|
@ -31,6 +31,33 @@ test('repeat', t => {
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('repeat rounds with round()', t => {
|
||||||
|
const rt = new Runtime();
|
||||||
|
const c = new Control(rt);
|
||||||
|
|
||||||
|
const roundingTest = (inputForRepeat, expectedTimes) => {
|
||||||
|
// Test harness (mocks `util`)
|
||||||
|
let i = 0;
|
||||||
|
const util = {
|
||||||
|
stackFrame: Object.create(null),
|
||||||
|
startBranch: function () {
|
||||||
|
i++;
|
||||||
|
c.repeat({TIMES: inputForRepeat}, util);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Execute test
|
||||||
|
c.repeat({TIMES: inputForRepeat}, util);
|
||||||
|
t.strictEqual(i, expectedTimes);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Execute tests
|
||||||
|
roundingTest(3.2, 3);
|
||||||
|
roundingTest(3.7, 4);
|
||||||
|
roundingTest(3.5, 4);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
test('repeatUntil', t => {
|
test('repeatUntil', t => {
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
const c = new Control(rt);
|
const c = new Control(rt);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue