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');
|
2019-01-29 15:30:45 -05:00
|
|
|
const Runtime = require('../../src/engine/runtime');
|
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');
|
2018-12-12 12:37:00 -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');
|
2018-12-12 12:37:00 -05:00
|
|
|
t.type(th.initParams, 'function');
|
2017-02-09 18:18:53 -05:00
|
|
|
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');
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
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');
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
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');
|
2019-07-22 12:59:48 -04:00
|
|
|
t.strictEquals(th.popStack(), undefined);
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
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);
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
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');
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
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);
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
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);
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
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-06-05 23:20:42 -04:00
|
|
|
t.strictEquals(th.justReported, 'value');
|
2018-04-10 16:29:51 -04:00
|
|
|
|
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);
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-09 18:18:53 -05:00
|
|
|
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');
|
2018-12-12 12:37:00 -05:00
|
|
|
th.initParams();
|
2017-02-09 18:18:53 -05:00
|
|
|
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');
|
2019-01-29 15:30:45 -05:00
|
|
|
const r = new Runtime();
|
|
|
|
const s = new Sprite(null, r);
|
|
|
|
const rt = new RenderedTarget(s, r);
|
2017-04-20 19:17:05 -04:00
|
|
|
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
|
|
|
|
};
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
rt.blocks.createBlock(block1);
|
|
|
|
rt.blocks.createBlock(block2);
|
|
|
|
rt.blocks.createBlock(block2);
|
|
|
|
th.target = rt;
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
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);
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('stopThisScript', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2019-01-29 15:30:45 -05:00
|
|
|
const r = new Runtime();
|
|
|
|
const s = new Sprite(null, r);
|
|
|
|
const rt = new RenderedTarget(s, r);
|
2017-04-20 19:17:05 -04:00
|
|
|
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
|
|
|
|
};
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
rt.blocks.createBlock(block1);
|
|
|
|
rt.blocks.createBlock(block2);
|
|
|
|
th.target = rt;
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
th.stopThisScript();
|
|
|
|
t.strictEquals(th.peekStack(), null);
|
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
t.strictEquals(th.peekStack(), 'arbitraryString');
|
|
|
|
th.stopThisScript();
|
2019-06-25 14:38:46 -04:00
|
|
|
t.strictEquals(th.peekStack(), null);
|
|
|
|
th.pushStack('arbitraryString');
|
|
|
|
th.pushStack('secondString');
|
|
|
|
th.stopThisScript();
|
2019-07-22 12:59:48 -04:00
|
|
|
t.strictEquals(th.peekStack(), 'secondString');
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('isRecursiveCall', t => {
|
|
|
|
const th = new Thread('arbitraryString');
|
2019-01-29 15:30:45 -05:00
|
|
|
const r = new Runtime();
|
|
|
|
const s = new Sprite(null, r);
|
|
|
|
const rt = new RenderedTarget(s, r);
|
2017-04-20 19:17:05 -04:00
|
|
|
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
|
|
|
|
};
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
rt.blocks.createBlock(block1);
|
|
|
|
rt.blocks.createBlock(block2);
|
|
|
|
th.target = rt;
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
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);
|
2018-12-12 12:37:00 -05:00
|
|
|
|
2017-02-10 13:06:39 -05:00
|
|
|
t.end();
|
|
|
|
});
|