Skip to content

Commit

Permalink
Update loop and conditional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Aug 23, 2023
1 parent 2adf5ce commit 4845bf8
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 73 deletions.
Binary file modified test/fixtures/tw-conditional.sb3
Binary file not shown.
Binary file modified test/fixtures/tw-loop.sb3
Binary file not shown.
210 changes: 137 additions & 73 deletions test/integration/tw_conditional_and_loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ const VirtualMachine = require('../../src/virtual-machine');
const Scratch = require('../../src/extension-support/tw-extension-api-common');

// Based on https://github.com/TurboWarp/scratch-vm/pull/141
class LoopsAndThings {
getInfo() {
class TestExtensionUsingReturn {
getInfo () {
return {
id: "loopsAndThings",
name: "Loops and things test",
id: 'loopsAndThings',
name: 'Loops and things test - return',
blocks: [
{
opcode: "conditional",
opcode: 'conditional',
blockType: Scratch.BlockType.CONDITIONAL,
text: "run branch [BRANCH] of",
text: 'run branch [BRANCH] of',
arguments: {
BRANCH: {
type: Scratch.ArgumentType.NUMBER,
Expand All @@ -24,21 +24,21 @@ class LoopsAndThings {
branchCount: 3
},
{
opcode: "loop",
opcode: 'loop',
blockType: Scratch.BlockType.LOOP,
text: "my repeat [TIMES]",
text: 'my repeat [TIMES]',
arguments: {
TIMES: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 10
}
},
}
},
'---',
{
opcode: "testPromise",
opcode: 'testPromise',
blockType: Scratch.BlockType.REPORTER,
text: "return [VALUE] in a Promise",
text: 'return [VALUE] in a Promise',
arguments: {
VALUE: {
type: Scratch.ArgumentType.STRING,
Expand All @@ -50,13 +50,13 @@ class LoopsAndThings {
};
}

conditional({BRANCH}, util) {
conditional ({BRANCH}) {
return Scratch.Cast.toNumber(BRANCH);
}

loop({TIMES}, util) {
loop ({TIMES}, util) {
const times = Math.round(Scratch.Cast.toNumber(TIMES));
if (typeof util.stackFrame.loopCounter === "undefined") {
if (typeof util.stackFrame.loopCounter === 'undefined') {
util.stackFrame.loopCounter = times;
}
util.stackFrame.loopCounter--;
Expand All @@ -65,69 +65,133 @@ class LoopsAndThings {
}
}

testPromise({VALUE}) {
testPromise ({VALUE}) {
return Promise.resolve(VALUE);
}
}

const compilerAndInterpreter = (name, callback) => {
test(`${name} - interpreted`, t => {
callback(t, {
enabled: false
});
});
test(`${name} - compiled`, t => {
callback(t, {
enabled: true
});
});
};

compilerAndInterpreter('CONDITIONAL', (t, co) => {
t.plan(1);

const vm = new VirtualMachine();
vm.setCompilerOptions(co);
vm.extensionManager.addBuiltinExtension('loopsAndThings', LoopsAndThings);
vm.runtime.on('COMPILE_ERROR', () => {
t.fail('Compile error');
});

vm.loadProject(fs.readFileSync(path.join(__dirname, '../fixtures/tw-conditional.sb3'))).then(() => {
let okayCount = 0;
vm.runtime.on('SAY', (target, type, text) => {
if (text === 'OK!') {
okayCount++;
} else if (text === 'end') {
vm.stop();
t.equal(okayCount, 5);
t.end();
}
});
class TestExtensionUsingStartBranch {
getInfo () {
return {
id: 'loopsAndThings',
name: 'Loops and things test - startBranch',
blocks: [
{
opcode: 'conditional',
blockType: Scratch.BlockType.CONDITIONAL,
text: 'run branch [BRANCH] of',
arguments: {
BRANCH: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 1
}
},
branchCount: 3
},
{
opcode: 'loop',
blockType: Scratch.BlockType.LOOP,
text: 'my repeat [TIMES]',
arguments: {
TIMES: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 10
}
}
},
'---',
{
opcode: 'testPromise',
blockType: Scratch.BlockType.REPORTER,
text: 'return [VALUE] in a Promise',
arguments: {
VALUE: {
type: Scratch.ArgumentType.STRING,
defaultValue: ''
}
}
}
]
};
}

conditional ({BRANCH}, util) {
util.startBranch(Scratch.Cast.toNumber(BRANCH), false);
}

loop ({TIMES}, util) {
const times = Math.round(Scratch.Cast.toNumber(TIMES));
if (typeof util.stackFrame.loopCounter === 'undefined') {
util.stackFrame.loopCounter = times;
}
util.stackFrame.loopCounter--;
if (util.stackFrame.loopCounter >= 0) {
util.startBranch(1, true);
}
}

testPromise ({VALUE}) {
return Promise.resolve(VALUE);
}
}

/* eslint-disable no-loop-func */

for (const Extension of [TestExtensionUsingReturn, TestExtensionUsingStartBranch]) {
for (const compilerEnabled of [false, true]) {
test(`CONDITIONAL - ${Extension.name} - ${compilerEnabled ? 'compiled' : 'interpreted'}`, t => {
t.plan(1);

const vm = new VirtualMachine();
vm.setCompilerOptions({
enabled: compilerEnabled
});
vm.extensionManager.addBuiltinExtension('loopsAndThings', Extension);
vm.runtime.on('COMPILE_ERROR', () => {
t.fail('Compile error');
});

vm.greenFlag();
vm.start();
});
});

compilerAndInterpreter('LOOP', (t, co) => {
t.plan(1);

const vm = new VirtualMachine();
vm.setCompilerOptions(co);
vm.extensionManager.addBuiltinExtension('loopsAndThings', LoopsAndThings);
vm.runtime.on('COMPILE_ERROR', () => {
t.fail('Compile error');
});

vm.loadProject(fs.readFileSync(path.join(__dirname, '../fixtures/tw-loop.sb3'))).then(() => {
vm.runtime.on('SAY', (target, type, text) => {
vm.stop();
t.equal(text, 'a 5 b 50 c 200');
t.end();
vm.loadProject(fs.readFileSync(path.join(__dirname, '../fixtures/tw-conditional.sb3'))).then(() => {
let okayCount = 0;
vm.runtime.on('SAY', (target, type, text) => {
if (text === 'OK!') {
okayCount++;
} else if (text === 'end') {
vm.stop();
t.equal(okayCount, 5);
t.end();
} else {
t.fail(`Unexpected text: ${text}`);
}
});

vm.greenFlag();
vm.start();
});
});

vm.greenFlag();
vm.start();
});
});
test(`LOOP - ${Extension.name} - ${compilerEnabled ? 'compiled' : 'interpreted'}`, t => {
t.plan(1);

const vm = new VirtualMachine();
vm.setCompilerOptions({
enabled: compilerEnabled
});
vm.extensionManager.addBuiltinExtension('loopsAndThings', Extension);
vm.runtime.on('COMPILE_ERROR', () => {
t.fail('Compile error');
});

vm.loadProject(fs.readFileSync(path.join(__dirname, '../fixtures/tw-loop.sb3'))).then(() => {
vm.runtime.on('SAY', (target, type, text) => {
vm.stop();
t.equal(text, 'a 3 b 12 c 48 frames 64');
t.end();
});

vm.greenFlag();
vm.start();
});
});
}
}

0 comments on commit 4845bf8

Please sign in to comment.