mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 06:52:00 -05: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) {
|
||||
const times = Math.floor(Cast.toNumber(args.TIMES));
|
||||
const times = Math.round(Cast.toNumber(args.TIMES));
|
||||
// Initialize loop
|
||||
if (typeof util.stackFrame.loopCounter === 'undefined') {
|
||||
util.stackFrame.loopCounter = times;
|
||||
|
|
|
@ -31,6 +31,33 @@ test('repeat', t => {
|
|||
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 => {
|
||||
const rt = new Runtime();
|
||||
const c = new Control(rt);
|
||||
|
|
Loading…
Reference in a new issue