mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 23:12:24 -05:00
840ffb5df0
Newer versions of `tap` run more asynchronously, so sometimes using `process.nextTick(process.exit)` to end a test would prevent the test from completing correctly. Removing all instances of `process.nextTick(process.exit)` put tests into three categories: * the test still worked correctly -- no fixup needed. * the test would hang because the VM's `_steppingInterval` was keeping Node alive. These tests call a new `quit()` method which ends the stepping interval. * the `load-extensions` test needed special attention because the "Video Sensing" extension starts its own loop using `setTimeout`. I added a `_stopLoop()` method on the extension and directly call that from the test. I'm not completely happy with this solution but anything more general would likely require a change to the extension spec, so I'm leaving that as a followup task.
79 lines
2.5 KiB
JavaScript
79 lines
2.5 KiB
JavaScript
const path = require('path');
|
|
const test = require('tap').test;
|
|
const makeTestStorage = require('../fixtures/make-test-storage');
|
|
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
|
|
|
|
const VirtualMachine = require('../../src/virtual-machine');
|
|
const RenderedTarget = require('../../src/sprites/rendered-target');
|
|
|
|
const projectUri = path.resolve(__dirname, '../fixtures/default.sb2');
|
|
const project = readFileToBuffer(projectUri);
|
|
|
|
const vm = new VirtualMachine();
|
|
|
|
test('spec', t => {
|
|
t.type(vm.addSprite, 'function');
|
|
t.end();
|
|
});
|
|
|
|
test('default cat', t => {
|
|
// Get default cat from .sprite2
|
|
const uri = path.resolve(__dirname, '../fixtures/example_sprite.sprite2');
|
|
const sprite = readFileToBuffer(uri);
|
|
|
|
vm.attachStorage(makeTestStorage());
|
|
|
|
// Evaluate playground data and exit
|
|
vm.on('playgroundData', e => {
|
|
const threads = JSON.parse(e.threads);
|
|
t.ok(threads.length === 0);
|
|
vm.quit();
|
|
t.end();
|
|
});
|
|
|
|
vm.start();
|
|
vm.clear();
|
|
vm.setCompatibilityMode(false);
|
|
vm.setTurboMode(false);
|
|
t.doesNotThrow(() => {
|
|
vm.loadProject(project).then(() => {
|
|
|
|
t.equal(vm.runtime.targets.length, 2); // stage and default sprite
|
|
|
|
// Add another sprite
|
|
vm.addSprite(sprite).then(() => {
|
|
const targets = vm.runtime.targets;
|
|
|
|
// Test
|
|
t.type(targets, 'object');
|
|
t.equal(targets.length, 3);
|
|
|
|
const newTarget = targets[2];
|
|
|
|
t.ok(newTarget instanceof RenderedTarget);
|
|
t.type(newTarget.id, 'string');
|
|
t.type(newTarget.blocks, 'object');
|
|
t.type(newTarget.variables, 'object');
|
|
const varIds = Object.keys(newTarget.variables);
|
|
t.type(varIds.length, 1);
|
|
const variable = newTarget.variables[varIds[0]];
|
|
t.equal(variable.name, 'foo');
|
|
t.equal(variable.value, 0);
|
|
|
|
t.equal(newTarget.isOriginal, true);
|
|
t.equal(newTarget.currentCostume, 0);
|
|
t.equal(newTarget.isOriginal, true);
|
|
t.equal(newTarget.isStage, false);
|
|
t.equal(newTarget.sprite.name, 'Apple');
|
|
|
|
vm.greenFlag();
|
|
|
|
setTimeout(() => {
|
|
t.equal(variable.value, 10);
|
|
vm.getPlaygroundData();
|
|
vm.stopAll();
|
|
}, 1000);
|
|
});
|
|
});
|
|
});
|
|
});
|