2017-04-20 19:17:05 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const Thread = require('../../src/engine/thread');
|
|
|
|
const RenderedTarget = require('../../src/sprites/rendered-target');
|
|
|
|
const Sprite = require('../../src/sprites/sprite');
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('spec', t => {
|
2016-04-18 17:20:30 -04:00
|
|
|
t.type(Thread, 'function');
|
2017-02-09 18:18:53 -05:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
t.type(th, 'object');
|
|
|
|
t.ok(th instanceof Thread);
|
|
|
|
t.type(th.pushStack, 'function');
|
|
|
|
t.type(th.reuseStackForNextBlock, 'function');
|
|
|
|
t.type(th.popStack, 'function');
|
|
|
|
t.type(th.stopThisScript, 'function');
|
|
|
|
t.type(th.peekStack, 'function');
|
|
|
|
t.type(th.peekStackFrame, 'function');
|
|
|
|
t.type(th.peekParentStackFrame, 'function');
|
|
|
|
t.type(th.pushReportedValue, 'function');
|
|
|
|
t.type(th.pushParam, 'function');
|
|
|
|
t.type(th.peekStack, 'function');
|
|
|
|
t.type(th.getParam, 'function');
|
|
|
|
t.type(th.atStackTop, 'function');
|
|
|
|
t.type(th.goToNextBlock, 'function');
|
|
|
|
t.type(th.isRecursiveCall, 'function');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('pushStack', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('popStack', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
t.strictEquals(th.popStack(), 'arbitraryString');
|
|
|
|
t.strictEquals(th.popStack(), undefined);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('atStackTop', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
th.pushStack('secondString');
|
|
|
|
t.strictEquals(th.atStackTop(), false);
|
|
|
|
th.popStack();
|
|
|
|
t.strictEquals(th.atStackTop(), true);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('reuseStackForNextBlock', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
th.reuseStackForNextBlock('secondString');
|
|
|
|
t.strictEquals(th.popStack(), 'secondString');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('peekStackFrame', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
t.strictEquals(th.peekStackFrame().warpMode, false);
|
|
|
|
th.popStack();
|
|
|
|
t.strictEquals(th.peekStackFrame(), null);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('peekParentStackFrame', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
th.peekStackFrame().warpMode = true;
|
|
|
|
t.strictEquals(th.peekParentStackFrame(), null);
|
|
|
|
th.pushStack('secondString');
|
|
|
|
t.strictEquals(th.peekParentStackFrame().warpMode, true);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('pushReportedValue', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
th.pushStack('secondString');
|
|
|
|
th.pushReportedValue('value');
|
2018-01-24 14:34:02 -05:00
|
|
|
t.strictEquals(th.peekParentStackFrame().justReported, 'value');
|
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('peekStack', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
t.strictEquals(th.peekStack(), 'arbitraryString');
|
|
|
|
th.popStack();
|
|
|
|
t.strictEquals(th.peekStack(), null);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('PushGetParam', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2017-02-09 18:18:53 -05:00
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
th.pushParam('testParam', 'testValue');
|
|
|
|
t.strictEquals(th.peekStackFrame().params.testParam, 'testValue');
|
|
|
|
t.strictEquals(th.getParam('testParam'), 'testValue');
|
2017-12-06 11:21:00 -05:00
|
|
|
// Params outside of define stack always evaluate to null
|
|
|
|
t.strictEquals(th.getParam('nonExistentParam'), null);
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
t.end();
|
|
|
|
});
|
2017-02-10 13:06:39 -05:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('goToNextBlock', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
|
|
|
const s = new Sprite();
|
|
|
|
const rt = new RenderedTarget(s, null);
|
|
|
|
const block1 = {fields: Object,
|
2017-02-10 13:06:39 -05:00
|
|
|
id: 'arbitraryString',
|
|
|
|
inputs: Object,
|
|
|
|
STEPS: Object,
|
|
|
|
block: 'fakeBlock',
|
|
|
|
name: 'STEPS',
|
|
|
|
next: 'secondString',
|
|
|
|
opcode: 'motion_movesteps',
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
topLevel: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
2017-04-20 19:17:05 -04:00
|
|
|
const block2 = {fields: Object,
|
2017-02-10 13:06:39 -05:00
|
|
|
id: 'secondString',
|
|
|
|
inputs: Object,
|
|
|
|
STEPS: Object,
|
|
|
|
block: 'fakeBlock',
|
|
|
|
name: 'STEPS',
|
|
|
|
next: null,
|
2017-11-16 14:23:56 -05:00
|
|
|
opcode: 'procedures_call',
|
2017-02-10 13:06:39 -05:00
|
|
|
mutation: {proccode: 'fakeCode'},
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
topLevel: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
rt.blocks.createBlock(block1);
|
|
|
|
rt.blocks.createBlock(block2);
|
|
|
|
rt.blocks.createBlock(block2);
|
|
|
|
th.target = rt;
|
|
|
|
|
|
|
|
t.strictEquals(th.peekStack(), null);
|
|
|
|
th.pushStack('secondString');
|
|
|
|
t.strictEquals(th.peekStack(), 'secondString');
|
|
|
|
th.goToNextBlock();
|
|
|
|
t.strictEquals(th.peekStack(), null);
|
|
|
|
th.pushStack('secondString');
|
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
t.strictEquals(th.peekStack(), 'arbitraryString');
|
|
|
|
th.goToNextBlock();
|
|
|
|
t.strictEquals(th.peekStack(), 'secondString');
|
|
|
|
th.goToNextBlock();
|
|
|
|
t.strictEquals(th.peekStack(), null);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('stopThisScript', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
|
|
|
const s = new Sprite();
|
|
|
|
const rt = new RenderedTarget(s, null);
|
|
|
|
const block1 = {fields: Object,
|
2017-02-10 13:06:39 -05:00
|
|
|
id: 'arbitraryString',
|
|
|
|
inputs: Object,
|
|
|
|
STEPS: Object,
|
|
|
|
block: 'fakeBlock',
|
|
|
|
name: 'STEPS',
|
|
|
|
next: null,
|
|
|
|
opcode: 'motion_movesteps',
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
topLevel: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
2017-04-20 19:17:05 -04:00
|
|
|
const block2 = {fields: Object,
|
2017-02-10 13:06:39 -05:00
|
|
|
id: 'secondString',
|
|
|
|
inputs: Object,
|
|
|
|
STEPS: Object,
|
|
|
|
block: 'fakeBlock',
|
|
|
|
name: 'STEPS',
|
|
|
|
next: null,
|
2017-11-16 14:17:08 -05:00
|
|
|
opcode: 'procedures_call',
|
2017-02-10 13:06:39 -05:00
|
|
|
mutation: {proccode: 'fakeCode'},
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
topLevel: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
rt.blocks.createBlock(block1);
|
|
|
|
rt.blocks.createBlock(block2);
|
|
|
|
th.target = rt;
|
|
|
|
|
|
|
|
th.stopThisScript();
|
|
|
|
t.strictEquals(th.peekStack(), null);
|
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
t.strictEquals(th.peekStack(), 'arbitraryString');
|
|
|
|
th.stopThisScript();
|
|
|
|
t.strictEquals(th.peekStack(), null);
|
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
th.pushStack('secondString');
|
|
|
|
th.stopThisScript();
|
|
|
|
t.strictEquals(th.peekStack(), 'secondString');
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('isRecursiveCall', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
|
|
|
const s = new Sprite();
|
|
|
|
const rt = new RenderedTarget(s, null);
|
|
|
|
const block1 = {fields: Object,
|
2017-02-10 13:06:39 -05:00
|
|
|
id: 'arbitraryString',
|
|
|
|
inputs: Object,
|
|
|
|
STEPS: Object,
|
|
|
|
block: 'fakeBlock',
|
|
|
|
name: 'STEPS',
|
|
|
|
next: null,
|
|
|
|
opcode: 'motion_movesteps',
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
topLevel: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
2017-04-20 19:17:05 -04:00
|
|
|
const block2 = {fields: Object,
|
2017-02-10 13:06:39 -05:00
|
|
|
id: 'secondString',
|
|
|
|
inputs: Object,
|
|
|
|
STEPS: Object,
|
|
|
|
block: 'fakeBlock',
|
|
|
|
name: 'STEPS',
|
|
|
|
next: null,
|
2017-11-16 14:17:08 -05:00
|
|
|
opcode: 'procedures_call',
|
2017-02-10 13:06:39 -05:00
|
|
|
mutation: {proccode: 'fakeCode'},
|
|
|
|
parent: null,
|
|
|
|
shadow: false,
|
|
|
|
topLevel: true,
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
rt.blocks.createBlock(block1);
|
|
|
|
rt.blocks.createBlock(block2);
|
|
|
|
th.target = rt;
|
|
|
|
|
|
|
|
t.strictEquals(th.isRecursiveCall('fakeCode'), false);
|
|
|
|
th.pushStack('secondString');
|
|
|
|
t.strictEquals(th.isRecursiveCall('fakeCode'), false);
|
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
t.strictEquals(th.isRecursiveCall('fakeCode'), true);
|
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
t.strictEquals(th.isRecursiveCall('fakeCode'), true);
|
|
|
|
th.popStack();
|
|
|
|
t.strictEquals(th.isRecursiveCall('fakeCode'), true);
|
|
|
|
th.popStack();
|
|
|
|
t.strictEquals(th.isRecursiveCall('fakeCode'), false);
|
|
|
|
th.popStack();
|
|
|
|
t.strictEquals(th.isRecursiveCall('fakeCode'), false);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|