mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-29 09:22:35 -05:00
130 lines
3 KiB
JavaScript
130 lines
3 KiB
JavaScript
const test = require('tap').test;
|
|
const Control = require('../../src/blocks/scratch3_control');
|
|
const Runtime = require('../../src/engine/runtime');
|
|
|
|
test('getPrimitives', t => {
|
|
const rt = new Runtime();
|
|
const c = new Control(rt);
|
|
t.type(c.getPrimitives(), 'object');
|
|
t.end();
|
|
});
|
|
|
|
test('repeat', 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++;
|
|
c.repeat({TIMES: repeat}, util);
|
|
}
|
|
};
|
|
|
|
// Execute test
|
|
c.repeat({TIMES: 10}, util);
|
|
t.strictEqual(util.stackFrame.loopCounter, -1);
|
|
t.strictEqual(i, repeat);
|
|
t.end();
|
|
});
|
|
|
|
test('repeatUntil', 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++;
|
|
c.repeatUntil({CONDITION: (i === repeat)}, util);
|
|
}
|
|
};
|
|
|
|
// Execute test
|
|
c.repeatUntil({CONDITION: (i === repeat)}, util);
|
|
t.strictEqual(i, repeat);
|
|
t.end();
|
|
});
|
|
|
|
test('forever', t => {
|
|
const rt = new Runtime();
|
|
const c = new Control(rt);
|
|
|
|
// Test harness (mocks `util`)
|
|
let i = 0;
|
|
const util = {
|
|
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();
|
|
});
|
|
|
|
test('if / ifElse', t => {
|
|
const rt = new Runtime();
|
|
const c = new Control(rt);
|
|
|
|
// Test harness (mocks `util`)
|
|
let i = 0;
|
|
const util = {
|
|
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();
|
|
});
|
|
|
|
test('stop', t => {
|
|
const rt = new Runtime();
|
|
const c = new Control(rt);
|
|
|
|
// Test harness (mocks `util`)
|
|
const state = {
|
|
stopAll: 0,
|
|
stopOtherTargetThreads: 0,
|
|
stopThisScript: 0
|
|
};
|
|
const util = {
|
|
stopAll: function () {
|
|
state.stopAll++;
|
|
},
|
|
stopOtherTargetThreads: function () {
|
|
state.stopOtherTargetThreads++;
|
|
},
|
|
stopThisScript: function () {
|
|
state.stopThisScript++;
|
|
}
|
|
};
|
|
|
|
// 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);
|
|
t.strictEqual(state.stopThisScript, 1);
|
|
t.end();
|
|
});
|