Use round() instead of floor() in repeat, add unit test

This commit is contained in:
Florrie 2018-08-14 20:27:18 -03:00
parent f9814a5f88
commit 5af79a086b
2 changed files with 28 additions and 1 deletions

View file

@ -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;

View file

@ -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);