mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 15:02:06 -05:00
Add tests and update core example to handle stage being undefined.
This commit is contained in:
parent
0e710ba3d9
commit
30c9b7fd84
3 changed files with 68 additions and 17 deletions
|
@ -36,7 +36,8 @@ class Scratch3CoreExample {
|
||||||
* @returns {string} The name of the first target in the project.
|
* @returns {string} The name of the first target in the project.
|
||||||
*/
|
*/
|
||||||
exampleOpcode () {
|
exampleOpcode () {
|
||||||
return this.runtime.getTargetForStage().getName();
|
const stage = this.runtime.getTargetForStage();
|
||||||
|
return stage ? stage.getName() : 'no stage yet';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@ const Worker = require('tiny-worker');
|
||||||
const dispatch = require('../../src/dispatch/central-dispatch');
|
const dispatch = require('../../src/dispatch/central-dispatch');
|
||||||
const VirtualMachine = require('../../src/virtual-machine');
|
const VirtualMachine = require('../../src/virtual-machine');
|
||||||
|
|
||||||
|
const Sprite = require('../../src/sprites/sprite');
|
||||||
|
const RenderedTarget = require('../../src/sprites/rendered-target');
|
||||||
|
|
||||||
// By default Central Dispatch works with the Worker class built into the browser. Tell it to use TinyWorker instead.
|
// By default Central Dispatch works with the Worker class built into the browser. Tell it to use TinyWorker instead.
|
||||||
dispatch.workerClass = Worker;
|
dispatch.workerClass = Worker;
|
||||||
|
|
||||||
|
@ -52,7 +55,7 @@ test('internal extension', t => {
|
||||||
t.ok(extension.status.constructorCalled);
|
t.ok(extension.status.constructorCalled);
|
||||||
|
|
||||||
t.notOk(extension.status.getInfoCalled);
|
t.notOk(extension.status.getInfoCalled);
|
||||||
return vm.extensionManager._registerInternalExtension(extension).then(() => {
|
vm.extensionManager._registerInternalExtension(extension);
|
||||||
t.ok(extension.status.getInfoCalled);
|
t.ok(extension.status.getInfoCalled);
|
||||||
|
|
||||||
const func = vm.runtime.getOpcodeFunction('testInternalExtension_go');
|
const func = vm.runtime.getOpcodeFunction('testInternalExtension_go');
|
||||||
|
@ -70,5 +73,31 @@ test('internal extension', t => {
|
||||||
// Second menu is a dynamic menu and therefore should be a function.
|
// Second menu is a dynamic menu and therefore should be a function.
|
||||||
t.type(
|
t.type(
|
||||||
vm.runtime._blockInfo[0].menus[1].json.args0[0].options, 'function');
|
vm.runtime._blockInfo[0].menus[1].json.args0[0].options, 'function');
|
||||||
});
|
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('load sync', t => {
|
||||||
|
const vm = new VirtualMachine();
|
||||||
|
vm.extensionManager.loadExtensionIdSync('coreExample');
|
||||||
|
t.ok(vm.extensionManager.isExtensionLoaded('coreExample'));
|
||||||
|
|
||||||
|
t.equal(vm.runtime._blockInfo.length, 1);
|
||||||
|
t.equal(vm.runtime._blockInfo[0].blocks.length, 1);
|
||||||
|
t.type(vm.runtime._blockInfo[0].blocks[0].info, 'object');
|
||||||
|
t.equal(vm.runtime._blockInfo[0].blocks[0].info.opcode, 'exampleOpcode');
|
||||||
|
t.equal(vm.runtime._blockInfo[0].blocks[0].info.blockType, 'reporter');
|
||||||
|
|
||||||
|
// Test the opcode function
|
||||||
|
t.equal(vm.runtime._blockInfo[0].blocks[0].info.func(), 'no stage yet');
|
||||||
|
|
||||||
|
const sprite = new Sprite(null, vm.runtime);
|
||||||
|
sprite.name = 'Stage';
|
||||||
|
const stage = new RenderedTarget(sprite, vm.runtime);
|
||||||
|
stage.isStage = true;
|
||||||
|
vm.runtime.targets = [stage];
|
||||||
|
|
||||||
|
t.equal(vm.runtime._blockInfo[0].blocks[0].info.func(), 'Stage');
|
||||||
|
|
||||||
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
|
@ -62,3 +62,24 @@ test('remote', t => {
|
||||||
.then(() => runServiceTest('RemoteDispatchTest', t), e => t.fail(e))
|
.then(() => runServiceTest('RemoteDispatchTest', t), e => t.fail(e))
|
||||||
.then(() => dispatch._remoteCall(worker, 'dispatch', 'terminate'), e => t.fail(e));
|
.then(() => dispatch._remoteCall(worker, 'dispatch', 'terminate'), e => t.fail(e));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('local, sync', t => {
|
||||||
|
dispatch.setServiceSync('SyncDispatchTest', new DispatchTestService());
|
||||||
|
|
||||||
|
const a = dispatch.callSync('SyncDispatchTest', 'returnFortyTwo');
|
||||||
|
t.equal(a, 42);
|
||||||
|
|
||||||
|
const b = dispatch.callSync('SyncDispatchTest', 'doubleArgument', 9);
|
||||||
|
t.equal(b, 18);
|
||||||
|
|
||||||
|
const c = dispatch.callSync('SyncDispatchTest', 'doubleArgument', 123);
|
||||||
|
t.equal(c, 246);
|
||||||
|
|
||||||
|
try {
|
||||||
|
dispatch.callSync('SyncDispatchTest', 'throwException');
|
||||||
|
} catch (e) {
|
||||||
|
t.equal(e.message, 'This is a test exception thrown by DispatchTest');
|
||||||
|
}
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue