mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-23 04:31:25 -04:00
Lint and fix failing tests
This commit is contained in:
parent
c9ce6776aa
commit
f785117e1d
8 changed files with 38 additions and 19 deletions
src
test
|
@ -276,7 +276,7 @@ const parseScratchObject = function (object, runtime, topLevel) {
|
||||||
/**
|
/**
|
||||||
* Top-level handler. Parse provided JSON,
|
* Top-level handler. Parse provided JSON,
|
||||||
* and process the top-level object (the stage object).
|
* and process the top-level object (the stage object).
|
||||||
* @param {!string} json SB2-format JSON to load.
|
* @param {!object} json SB2-format JSON to load.
|
||||||
* @param {!Runtime} runtime Runtime object to load all structures into.
|
* @param {!Runtime} runtime Runtime object to load all structures into.
|
||||||
* @param {boolean=} optForceSprite If set, treat as sprite (Sprite2).
|
* @param {boolean=} optForceSprite If set, treat as sprite (Sprite2).
|
||||||
* @return {?Promise} Promise that resolves to the loaded targets when ready.
|
* @return {?Promise} Promise that resolves to the loaded targets when ready.
|
||||||
|
|
|
@ -16,7 +16,7 @@ const loadSound = require('../import/load-sound.js');
|
||||||
/**
|
/**
|
||||||
* Serializes the specified VM runtime.
|
* Serializes the specified VM runtime.
|
||||||
* @param {!Runtime} runtime VM runtime instance to be serialized.
|
* @param {!Runtime} runtime VM runtime instance to be serialized.
|
||||||
* @return {string} Serialized runtime instance.
|
* @return {object} Serialized runtime instance.
|
||||||
*/
|
*/
|
||||||
const serialize = function (runtime) {
|
const serialize = function (runtime) {
|
||||||
// Fetch targets
|
// Fetch targets
|
||||||
|
@ -150,11 +150,12 @@ const parseScratchObject = function (object, runtime) {
|
||||||
/**
|
/**
|
||||||
* Deserializes the specified representation of a VM runtime and loads it into
|
* Deserializes the specified representation of a VM runtime and loads it into
|
||||||
* the provided runtime instance.
|
* the provided runtime instance.
|
||||||
* @param {string} json Stringified JSON representation of a VM runtime.
|
* @param {object} json JSON representation of a VM runtime.
|
||||||
* @param {Runtime} runtime Runtime instance
|
* @param {Runtime} runtime Runtime instance
|
||||||
|
* @returns {Promise} Promise that resolves to the list of targets after the project is deserialized
|
||||||
*/
|
*/
|
||||||
const deserialize = function (json, runtime) {
|
const deserialize = function (json, runtime) {
|
||||||
return Promise.all(json.targets.map(target => parseScratchObject(target, runtime)));
|
return Promise.all((json.targets || []).map(target => parseScratchObject(target, runtime)));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -763,14 +763,21 @@ class RenderedTarget extends Target {
|
||||||
this.dragging = false;
|
this.dragging = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to clean out data from each asset when serializing to JSON
|
||||||
|
* @param {Array<object>} assetList list of costumes or sounds
|
||||||
|
* @returns {Array<object>} list with raw data removed from each asset
|
||||||
|
*/
|
||||||
assetListToJSON (assetList) {
|
assetListToJSON (assetList) {
|
||||||
const exclude = ['data', 'url'];
|
const exclude = ['data', 'url'];
|
||||||
return assetList.map(asset =>
|
return assetList.map(asset => {
|
||||||
Object.keys(asset).reduce((rAsset, prop) => {
|
if (typeof asset !== 'object') return asset;
|
||||||
|
return Object.keys(asset).reduce((rAsset, prop) => {
|
||||||
if (exclude.indexOf(prop) === -1) rAsset[prop] = asset[prop];
|
if (exclude.indexOf(prop) === -1) rAsset[prop] = asset[prop];
|
||||||
return rAsset;
|
return rAsset;
|
||||||
}, {})
|
}, {});
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -231,7 +231,12 @@ class VirtualMachine extends EventEmitter {
|
||||||
* @returns {Promise} Promise that resolves after the sprite is added
|
* @returns {Promise} Promise that resolves after the sprite is added
|
||||||
*/
|
*/
|
||||||
addSprite2 (json) {
|
addSprite2 (json) {
|
||||||
// Select new sprite.
|
// Validate & parse
|
||||||
|
if (typeof json !== 'string') return;
|
||||||
|
json = JSON.parse(json);
|
||||||
|
if (typeof json !== 'object') return;
|
||||||
|
|
||||||
|
// Select new sprite.
|
||||||
return sb2.deserialize(json, this.runtime, true).then(targets => {
|
return sb2.deserialize(json, this.runtime, true).then(targets => {
|
||||||
this.runtime.targets.push(targets[0]);
|
this.runtime.targets.push(targets[0]);
|
||||||
this.editingTarget = targets[0];
|
this.editingTarget = targets[0];
|
||||||
|
|
1
test/fixtures/demo.json
vendored
Normal file
1
test/fixtures/demo.json
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -8,7 +8,7 @@ const runtime = require('../../src/engine/runtime');
|
||||||
const sb2 = require('../../src/serialization/sb2');
|
const sb2 = require('../../src/serialization/sb2');
|
||||||
|
|
||||||
test('spec', t => {
|
test('spec', t => {
|
||||||
t.type(sb2, 'function');
|
t.type(sb2.deserialize, 'function');
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -16,13 +16,15 @@ test('default', t => {
|
||||||
// Get SB2 JSON (string)
|
// Get SB2 JSON (string)
|
||||||
const uri = path.resolve(__dirname, '../fixtures/default.sb2');
|
const uri = path.resolve(__dirname, '../fixtures/default.sb2');
|
||||||
const file = extract(uri);
|
const file = extract(uri);
|
||||||
|
const json = JSON.parse(file);
|
||||||
|
|
||||||
// Create runtime instance & load SB2 into it
|
// Create runtime instance & load SB2 into it
|
||||||
const rt = new runtime();
|
const rt = new runtime();
|
||||||
attachTestStorage(rt);
|
attachTestStorage(rt);
|
||||||
sb2(file, rt).then(targets => {
|
sb2.deserialize(json, rt).then(targets => {
|
||||||
// Test
|
// Test
|
||||||
t.type(file, 'string');
|
t.type(file, 'string');
|
||||||
|
t.type(json, 'object');
|
||||||
t.type(rt, 'object');
|
t.type(rt, 'object');
|
||||||
t.type(targets, 'object');
|
t.type(targets, 'object');
|
||||||
|
|
||||||
|
|
|
@ -16,14 +16,14 @@ test('default', t => {
|
||||||
// Get SB2 JSON (string)
|
// Get SB2 JSON (string)
|
||||||
const uri = path.resolve(__dirname, '../fixtures/default.sb2');
|
const uri = path.resolve(__dirname, '../fixtures/default.sb2');
|
||||||
const file = extract(uri);
|
const file = extract(uri);
|
||||||
|
const json = JSON.parse(file);
|
||||||
|
|
||||||
// Create runtime instance & load SB2 into it
|
// Create runtime instance & load SB2 into it
|
||||||
const rt = new Runtime();
|
const rt = new Runtime();
|
||||||
sb2.deserialize(file, rt).then(targets => {
|
sb2.deserialize(json, rt).then(targets => {
|
||||||
console.dir(targets);
|
|
||||||
|
|
||||||
// Test
|
// Test
|
||||||
t.type(file, 'string');
|
t.type(file, 'string');
|
||||||
|
t.type(json, 'object');
|
||||||
t.type(rt, 'object');
|
t.type(rt, 'object');
|
||||||
t.type(targets, 'object');
|
t.type(targets, 'object');
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,22 @@
|
||||||
const test = require('tap').test;
|
const test = require('tap').test;
|
||||||
const VirtualMachine = require('../../src/index');
|
const VirtualMachine = require('../../src/index');
|
||||||
const sb3 = require('../../src/serialization/sb3');
|
const sb3 = require('../../src/serialization/sb3');
|
||||||
|
const demoSb3 = require('../fixtures/demo.json');
|
||||||
|
|
||||||
test('serialize', t => {
|
test('serialize', t => {
|
||||||
const vm = new VirtualMachine();
|
const vm = new VirtualMachine();
|
||||||
vm.fromJSON(JSON.stringify(require('../fixtures/demo.json')));
|
vm.fromJSON(JSON.stringify(demoSb3));
|
||||||
const result = sb3.serialize(vm.runtime);
|
const result = sb3.serialize(vm.runtime);
|
||||||
console.dir(JSON.stringify(result));
|
|
||||||
// @todo Analyze
|
// @todo Analyze
|
||||||
|
t.type(JSON.stringify(result), 'string');
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('deserialize', t => {
|
test('deserialize', t => {
|
||||||
const vm = new VirtualMachine();
|
const vm = new VirtualMachine();
|
||||||
const result = sb3.deserialize('', vm.runtime);
|
sb3.deserialize('', vm.runtime).then(targets => {
|
||||||
// @todo Analyize
|
// @todo Analyize
|
||||||
t.end();
|
t.type(targets, 'object');
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue