mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-08-28 22:30:40 -04:00
Adding unit tests for new lookupOrCreate funciton, fixing a bug that came up during testing (using lookupVariableById on a null target). Skipping integration tests that do not work right now because SB2 import of broadcast message blocks has not been implemented yet.
This commit is contained in:
parent
b674a0c047
commit
e5378d323d
6 changed files with 54 additions and 10 deletions
|
@ -12,7 +12,7 @@ test('#760 - broadcastAndWait', t => {
|
|||
id: 'broadcastAndWaitBlock',
|
||||
fields: {
|
||||
BROADCAST_OPTION: {
|
||||
id: 'BROADCAST_OPTION',
|
||||
id: 'testBroadcastID',
|
||||
value: 'message'
|
||||
}
|
||||
},
|
||||
|
@ -30,7 +30,7 @@ test('#760 - broadcastAndWait', t => {
|
|||
id: 'receiveMessageBlock',
|
||||
fields: {
|
||||
BROADCAST_OPTION: {
|
||||
id: 'BROADCAST_OPTION',
|
||||
id: 'testBroadcastID',
|
||||
value: 'message'
|
||||
}
|
||||
},
|
||||
|
@ -51,15 +51,17 @@ test('#760 - broadcastAndWait', t => {
|
|||
b.createBlock(broadcastAndWaitBlock);
|
||||
b.createBlock(receiveMessageBlock);
|
||||
const tgt = new Target(rt, b);
|
||||
tgt.isStage = true;
|
||||
rt.targets.push(tgt);
|
||||
|
||||
let th = rt._pushThread('broadcastAndWaitBlock', t);
|
||||
const util = new BlockUtility();
|
||||
util.sequencer = rt.sequencer;
|
||||
util.thread = th;
|
||||
util.runtime = rt;
|
||||
|
||||
// creates threads
|
||||
e.broadcastAndWait({BROADCAST_OPTION: 'message'}, util);
|
||||
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
||||
t.strictEqual(rt.threads.length, 2);
|
||||
t.strictEqual(rt.threads[1].topBlock, 'receiveMessageBlock');
|
||||
// yields when some thread is active
|
||||
|
@ -76,18 +78,18 @@ test('#760 - broadcastAndWait', t => {
|
|||
// restarts done threads that are in runtime threads
|
||||
th = rt._pushThread('broadcastAndWaitBlock', tgt);
|
||||
util.thread = th;
|
||||
e.broadcastAndWait({BROADCAST_OPTION: 'message'}, util);
|
||||
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
||||
t.strictEqual(rt.threads.length, 3);
|
||||
t.strictEqual(rt.threads[1].status, Thread.STATUS_RUNNING);
|
||||
t.strictEqual(th.status, Thread.STATUS_YIELD);
|
||||
// yields when some restarted thread is active
|
||||
th.status = Thread.STATUS_RUNNING;
|
||||
e.broadcastAndWait({BROADCAST_OPTION: 'message'}, util);
|
||||
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
||||
t.strictEqual(th.status, Thread.STATUS_YIELD);
|
||||
// does not yield once all threads are done
|
||||
th.status = Thread.STATUS_RUNNING;
|
||||
rt.threads[1].status = Thread.STATUS_DONE;
|
||||
e.broadcastAndWait({BROADCAST_OPTION: 'message'}, util);
|
||||
e.broadcastAndWait({BROADCAST_OPTION: {id: 'testBroadcastID', name: 'message'}}, util);
|
||||
t.strictEqual(th.status, Thread.STATUS_RUNNING);
|
||||
|
||||
t.end();
|
||||
|
|
|
@ -173,3 +173,32 @@ test('lookupOrCreateList returns list if one with given id exists', t => {
|
|||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('lookupOrCreateBroadcastMsg creates a var if one does not exist', t => {
|
||||
const target = new Target();
|
||||
const variables = target.variables;
|
||||
|
||||
t.equal(Object.keys(variables).length, 0);
|
||||
const broadcastVar = target.lookupOrCreateBroadcastMsg('foo', 'bar');
|
||||
t.equal(Object.keys(variables).length, 1);
|
||||
t.equal(broadcastVar.id, 'foo');
|
||||
t.equal(broadcastVar.name, 'bar');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('lookupOrCreateBroadcastMsg returns the var with given id if exists', t => {
|
||||
const target = new Target();
|
||||
const variables = target.variables;
|
||||
|
||||
t.equal(Object.keys(variables).length, 0);
|
||||
target.createVariable('foo', 'bar', Variable.BROADCAST_MESSAGE_TYPE);
|
||||
t.equal(Object.keys(variables).length, 1);
|
||||
|
||||
const broadcastMsg = target.lookupOrCreateBroadcastMsg('foo', 'bar');
|
||||
t.equal(Object.keys(variables).length, 1);
|
||||
t.equal(broadcastMsg.id, 'foo');
|
||||
t.equal(broadcastMsg.name, 'bar');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue