mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-23 12:38:58 -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,
|
||||
* 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 {boolean=} optForceSprite If set, treat as sprite (Sprite2).
|
||||
* @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.
|
||||
* @param {!Runtime} runtime VM runtime instance to be serialized.
|
||||
* @return {string} Serialized runtime instance.
|
||||
* @return {object} Serialized runtime instance.
|
||||
*/
|
||||
const serialize = function (runtime) {
|
||||
// Fetch targets
|
||||
|
@ -150,11 +150,12 @@ const parseScratchObject = function (object, runtime) {
|
|||
/**
|
||||
* Deserializes the specified representation of a VM runtime and loads it into
|
||||
* 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
|
||||
* @returns {Promise} Promise that resolves to the list of targets after the project is deserialized
|
||||
*/
|
||||
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 = {
|
||||
|
|
|
@ -763,14 +763,21 @@ class RenderedTarget extends Target {
|
|||
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) {
|
||||
const exclude = ['data', 'url'];
|
||||
return assetList.map(asset =>
|
||||
Object.keys(asset).reduce((rAsset, prop) => {
|
||||
return assetList.map(asset => {
|
||||
if (typeof asset !== 'object') return asset;
|
||||
return Object.keys(asset).reduce((rAsset, prop) => {
|
||||
if (exclude.indexOf(prop) === -1) rAsset[prop] = asset[prop];
|
||||
return rAsset;
|
||||
}, {})
|
||||
);
|
||||
}, {});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -231,6 +231,11 @@ class VirtualMachine extends EventEmitter {
|
|||
* @returns {Promise} Promise that resolves after the sprite is added
|
||||
*/
|
||||
addSprite2 (json) {
|
||||
// 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 => {
|
||||
this.runtime.targets.push(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');
|
||||
|
||||
test('spec', t => {
|
||||
t.type(sb2, 'function');
|
||||
t.type(sb2.deserialize, 'function');
|
||||
t.end();
|
||||
});
|
||||
|
||||
|
@ -16,13 +16,15 @@ test('default', t => {
|
|||
// Get SB2 JSON (string)
|
||||
const uri = path.resolve(__dirname, '../fixtures/default.sb2');
|
||||
const file = extract(uri);
|
||||
const json = JSON.parse(file);
|
||||
|
||||
// Create runtime instance & load SB2 into it
|
||||
const rt = new runtime();
|
||||
attachTestStorage(rt);
|
||||
sb2(file, rt).then(targets => {
|
||||
sb2.deserialize(json, rt).then(targets => {
|
||||
// Test
|
||||
t.type(file, 'string');
|
||||
t.type(json, 'object');
|
||||
t.type(rt, 'object');
|
||||
t.type(targets, 'object');
|
||||
|
||||
|
|
|
@ -16,14 +16,14 @@ test('default', t => {
|
|||
// Get SB2 JSON (string)
|
||||
const uri = path.resolve(__dirname, '../fixtures/default.sb2');
|
||||
const file = extract(uri);
|
||||
const json = JSON.parse(file);
|
||||
|
||||
// Create runtime instance & load SB2 into it
|
||||
const rt = new Runtime();
|
||||
sb2.deserialize(file, rt).then(targets => {
|
||||
console.dir(targets);
|
||||
|
||||
sb2.deserialize(json, rt).then(targets => {
|
||||
// Test
|
||||
t.type(file, 'string');
|
||||
t.type(json, 'object');
|
||||
t.type(rt, 'object');
|
||||
t.type(targets, 'object');
|
||||
|
||||
|
|
|
@ -1,19 +1,22 @@
|
|||
const test = require('tap').test;
|
||||
const VirtualMachine = require('../../src/index');
|
||||
const sb3 = require('../../src/serialization/sb3');
|
||||
const demoSb3 = require('../fixtures/demo.json');
|
||||
|
||||
test('serialize', t => {
|
||||
const vm = new VirtualMachine();
|
||||
vm.fromJSON(JSON.stringify(require('../fixtures/demo.json')));
|
||||
vm.fromJSON(JSON.stringify(demoSb3));
|
||||
const result = sb3.serialize(vm.runtime);
|
||||
console.dir(JSON.stringify(result));
|
||||
// @todo Analyze
|
||||
t.type(JSON.stringify(result), 'string');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('deserialize', t => {
|
||||
const vm = new VirtualMachine();
|
||||
const result = sb3.deserialize('', vm.runtime);
|
||||
sb3.deserialize('', vm.runtime).then(targets => {
|
||||
// @todo Analyize
|
||||
t.type(targets, 'object');
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue