2017-04-20 19:17:05 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const Control = require('../../src/blocks/scratch3_control');
|
|
|
|
const Runtime = require('../../src/engine/runtime');
|
2018-12-04 10:35:42 -05:00
|
|
|
const BlockUtility = require('../../src/engine/block-utility');
|
2016-10-24 14:38:24 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('getPrimitives', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
2016-10-24 14:38:24 -04:00
|
|
|
t.type(c.getPrimitives(), 'object');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('repeat', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
2016-10-24 14:38:24 -04:00
|
|
|
|
|
|
|
// Test harness (mocks `util`)
|
2017-04-20 19:17:05 -04:00
|
|
|
let i = 0;
|
|
|
|
const repeat = 10;
|
2017-08-26 13:14:26 -04:00
|
|
|
const util = {
|
2016-10-24 14:38:24 -04:00
|
|
|
stackFrame: Object.create(null),
|
|
|
|
startBranch: function () {
|
|
|
|
i++;
|
|
|
|
c.repeat({TIMES: repeat}, util);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute test
|
|
|
|
c.repeat({TIMES: 10}, util);
|
|
|
|
t.strictEqual(util.stackFrame.loopCounter, -1);
|
|
|
|
t.strictEqual(i, repeat);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-08-14 19:27:18 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('repeatUntil', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
2016-10-24 14:38:24 -04:00
|
|
|
|
|
|
|
// Test harness (mocks `util`)
|
2017-04-20 19:17:05 -04:00
|
|
|
let i = 0;
|
|
|
|
const repeat = 10;
|
2017-08-26 13:14:26 -04:00
|
|
|
const util = {
|
2016-10-24 14:38:24 -04:00
|
|
|
stackFrame: Object.create(null),
|
|
|
|
startBranch: function () {
|
|
|
|
i++;
|
|
|
|
c.repeatUntil({CONDITION: (i === repeat)}, util);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute test
|
|
|
|
c.repeatUntil({CONDITION: (i === repeat)}, util);
|
|
|
|
t.strictEqual(i, repeat);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-04-07 13:07:15 -04:00
|
|
|
test('repeatWhile', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
|
|
|
|
|
|
|
// Test harness (mocks `util`)
|
|
|
|
let i = 0;
|
|
|
|
const repeat = 10;
|
|
|
|
const util = {
|
|
|
|
stackFrame: Object.create(null),
|
|
|
|
startBranch: function () {
|
|
|
|
i++;
|
|
|
|
// Note !== instead of ===
|
|
|
|
c.repeatWhile({CONDITION: (i !== repeat)}, util);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute test
|
|
|
|
c.repeatWhile({CONDITION: (i !== repeat)}, util);
|
|
|
|
t.strictEqual(i, repeat);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2018-03-05 15:18:09 -05:00
|
|
|
test('forEach', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
|
|
|
|
|
|
|
const variableValues = [];
|
|
|
|
const variable = {value: 0};
|
2018-03-28 10:38:19 -04:00
|
|
|
let value;
|
2018-03-05 15:18:09 -05:00
|
|
|
const util = {
|
|
|
|
stackFrame: Object.create(null),
|
|
|
|
target: {
|
|
|
|
lookupOrCreateVariable: function () {
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
startBranch: function () {
|
|
|
|
variableValues.push(variable.value);
|
|
|
|
c.forEach({VARIABLE: {}, VALUE: value}, util);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// for each (variable) in "5"
|
|
|
|
// ..should yield variable values 1, 2, 3, 4, 5
|
|
|
|
util.stackFrame = Object.create(null);
|
|
|
|
variableValues.splice(0);
|
|
|
|
variable.value = 0;
|
|
|
|
value = '5';
|
|
|
|
c.forEach({VARIABLE: {}, VALUE: value}, util);
|
|
|
|
t.deepEqual(variableValues, [1, 2, 3, 4, 5]);
|
|
|
|
|
|
|
|
// for each (variable) in 4
|
|
|
|
// ..should yield variable values 1, 2, 3, 4
|
|
|
|
util.stackFrame = Object.create(null);
|
|
|
|
variableValues.splice(0);
|
|
|
|
variable.value = 0;
|
|
|
|
value = 4;
|
|
|
|
c.forEach({VARIABLE: {}, VALUE: value}, util);
|
|
|
|
t.deepEqual(variableValues, [1, 2, 3, 4]);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('forever', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
2016-10-24 14:38:24 -04:00
|
|
|
|
|
|
|
// Test harness (mocks `util`)
|
2017-04-20 19:17:05 -04:00
|
|
|
let i = 0;
|
|
|
|
const util = {
|
2016-10-24 14:38:24 -04:00
|
|
|
startBranch: function (branchNum, isLoop) {
|
|
|
|
i++;
|
|
|
|
t.strictEqual(branchNum, 1);
|
|
|
|
t.strictEqual(isLoop, true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute test
|
|
|
|
c.forever(null, util);
|
|
|
|
t.strictEqual(i, 1);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('if / ifElse', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
2016-10-24 14:38:24 -04:00
|
|
|
|
|
|
|
// Test harness (mocks `util`)
|
2017-04-20 19:17:05 -04:00
|
|
|
let i = 0;
|
|
|
|
const util = {
|
2016-10-24 14:38:24 -04:00
|
|
|
startBranch: function (branchNum) {
|
|
|
|
i += branchNum;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute test
|
|
|
|
c.if({CONDITION: true}, util);
|
|
|
|
t.strictEqual(i, 1);
|
|
|
|
c.if({CONDITION: false}, util);
|
|
|
|
t.strictEqual(i, 1);
|
|
|
|
c.ifElse({CONDITION: true}, util);
|
|
|
|
t.strictEqual(i, 2);
|
|
|
|
c.ifElse({CONDITION: false}, util);
|
|
|
|
t.strictEqual(i, 4);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('stop', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
2016-10-24 14:38:24 -04:00
|
|
|
|
|
|
|
// Test harness (mocks `util`)
|
2017-04-20 19:17:05 -04:00
|
|
|
const state = {
|
2016-10-24 14:38:24 -04:00
|
|
|
stopAll: 0,
|
|
|
|
stopOtherTargetThreads: 0,
|
2017-02-08 04:44:10 -05:00
|
|
|
stopThisScript: 0
|
2016-10-24 14:38:24 -04:00
|
|
|
};
|
2017-04-20 19:17:05 -04:00
|
|
|
const util = {
|
2016-10-24 14:38:24 -04:00
|
|
|
stopAll: function () {
|
|
|
|
state.stopAll++;
|
|
|
|
},
|
|
|
|
stopOtherTargetThreads: function () {
|
|
|
|
state.stopOtherTargetThreads++;
|
|
|
|
},
|
2017-02-08 04:44:10 -05:00
|
|
|
stopThisScript: function () {
|
|
|
|
state.stopThisScript++;
|
2016-10-24 14:38:24 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute test
|
|
|
|
c.stop({STOP_OPTION: 'all'}, util);
|
|
|
|
c.stop({STOP_OPTION: 'other scripts in sprite'}, util);
|
|
|
|
c.stop({STOP_OPTION: 'other scripts in stage'}, util);
|
|
|
|
c.stop({STOP_OPTION: 'this script'}, util);
|
|
|
|
t.strictEqual(state.stopAll, 1);
|
|
|
|
t.strictEqual(state.stopOtherTargetThreads, 2);
|
2017-02-08 04:44:10 -05:00
|
|
|
t.strictEqual(state.stopThisScript, 1);
|
2016-10-24 14:38:24 -04:00
|
|
|
t.end();
|
|
|
|
});
|
2018-04-07 15:02:49 -04:00
|
|
|
|
|
|
|
test('counter, incrCounter, clearCounter', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
|
|
|
|
|
|
|
// Default value
|
|
|
|
t.strictEqual(c.getCounter(), 0);
|
|
|
|
|
|
|
|
c.incrCounter();
|
|
|
|
c.incrCounter();
|
|
|
|
t.strictEqual(c.getCounter(), 2);
|
|
|
|
|
|
|
|
c.clearCounter();
|
|
|
|
t.strictEqual(c.getCounter(), 0);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
2018-04-30 11:36:15 -04:00
|
|
|
|
|
|
|
test('allAtOnce', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
|
|
|
|
|
|
|
// Test harness (mocks `util`)
|
|
|
|
let ran = false;
|
|
|
|
const util = {
|
|
|
|
startBranch: function () {
|
|
|
|
ran = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Execute test
|
|
|
|
c.allAtOnce({}, util);
|
|
|
|
t.true(ran);
|
|
|
|
t.end();
|
|
|
|
});
|
2018-12-04 10:35:42 -05:00
|
|
|
|
|
|
|
test('wait', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const c = new Control(rt);
|
|
|
|
const args = {DURATION: .01};
|
|
|
|
const waitTime = args.DURATION * 1000;
|
|
|
|
const startTest = Date.now();
|
2022-06-06 17:22:56 -04:00
|
|
|
const thresholdSmall = 1000 / 60; // only allow the wait to end one 60Hz frame early
|
|
|
|
const thresholdLarge = 1000 / 3; // be less picky about when the wait ends, in case CPU load makes the VM run slowly
|
2018-12-04 10:35:42 -05:00
|
|
|
let yields = 0;
|
|
|
|
const util = new BlockUtility();
|
|
|
|
const mockUtil = {
|
|
|
|
stackFrame: {},
|
|
|
|
yield: () => yields++,
|
|
|
|
stackTimerNeedsInit: util.stackTimerNeedsInit,
|
|
|
|
startStackTimer: util.startStackTimer,
|
|
|
|
stackTimerFinished: util.stackTimerFinished
|
|
|
|
};
|
|
|
|
|
|
|
|
c.wait(args, mockUtil);
|
|
|
|
t.equal(yields, 1, 'First wait block yielded');
|
|
|
|
|
|
|
|
// Spin the cpu until enough time passes
|
|
|
|
let timeElapsed = 0;
|
|
|
|
while (timeElapsed < waitTime) {
|
|
|
|
timeElapsed = mockUtil.stackFrame.timer.timeElapsed();
|
|
|
|
// In case util.timer is broken - have our own "exit"
|
2022-06-06 17:22:56 -04:00
|
|
|
if (Date.now() - startTest > timeElapsed + thresholdSmall) {
|
2018-12-04 10:35:42 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.wait(args, mockUtil);
|
|
|
|
t.equal(yields, 1, 'Second call after timeElapsed does not yield');
|
|
|
|
t.equal(waitTime, mockUtil.stackFrame.duration);
|
2022-06-06 17:22:56 -04:00
|
|
|
t.ok(timeElapsed >= (waitTime - thresholdSmall),
|
|
|
|
'Wait block ended too early: ${timeElapsed} < ${waitTime} - ${thresholdSmall}');
|
|
|
|
t.ok(timeElapsed <= (waitTime + thresholdLarge),
|
|
|
|
'Wait block ended too late: ${timeElapsed} > ${waitTime} + ${thresholdLarge}');
|
2018-12-04 10:35:42 -05:00
|
|
|
t.end();
|
|
|
|
});
|