Add test coverage for SB2 importer (#278)

This commit is contained in:
Andrew Sliwinski 2016-10-17 11:55:21 -04:00 committed by GitHub
parent 80fa2d5a58
commit 9cd4d96101
3 changed files with 518 additions and 0 deletions

71
test/fixtures/default.json vendored Normal file
View file

@ -0,0 +1,71 @@
{
"objName": "Stage",
"sounds": [{
"soundName": "pop",
"soundID": -1,
"md5": "83a9787d4cb6f3b7632b4ddfebf74367.wav",
"sampleCount": 258,
"rate": 11025,
"format": ""
}],
"costumes": [{
"costumeName": "backdrop1",
"baseLayerID": -1,
"baseLayerMD5": "739b5e2a2435f6e1ec2993791b423146.png",
"bitmapResolution": 1,
"rotationCenterX": 240,
"rotationCenterY": 180
}],
"currentCostumeIndex": 0,
"penLayerMD5": "5c81a336fab8be57adc039a8a2b33ca9.png",
"penLayerID": -1,
"tempoBPM": 60,
"videoAlpha": 0.5,
"children": [{
"objName": "Sprite1",
"sounds": [{
"soundName": "meow",
"soundID": -1,
"md5": "83c36d806dc92327b9e7049a565c6bff.wav",
"sampleCount": 18688,
"rate": 22050,
"format": ""
}],
"costumes": [{
"costumeName": "costume1",
"baseLayerID": -1,
"baseLayerMD5": "09dc888b0b7df19f70d81588ae73420e.svg",
"bitmapResolution": 1,
"rotationCenterX": 47,
"rotationCenterY": 55
},
{
"costumeName": "costume2",
"baseLayerID": -1,
"baseLayerMD5": "3696356a03a8d938318876a593572843.svg",
"bitmapResolution": 1,
"rotationCenterX": 47,
"rotationCenterY": 55
}],
"currentCostumeIndex": 0,
"scratchX": 0,
"scratchY": 0,
"scale": 1,
"direction": 90,
"rotationStyle": "normal",
"isDraggable": false,
"indexInLibrary": 1,
"visible": true,
"spriteInfo": {
}
}],
"info": {
"videoOn": false,
"userAgent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/50.0.2661.102 Safari\/537.36",
"swfVersion": "v446",
"scriptCount": 0,
"spriteCount": 1,
"hasCloudData": false,
"flashVersion": "MAC 21,0,0,242"
}
}

359
test/fixtures/demo.json vendored Normal file
View file

@ -0,0 +1,359 @@
{
"objName": "Stage",
"variables": [{
"name": "x",
"value": "1",
"isPersistent": false
},
{
"name": "y",
"value": "1",
"isPersistent": false
},
{
"name": "z",
"value": "1",
"isPersistent": false
},
{
"name": "d",
"value": "1",
"isPersistent": false
},
{
"name": "a",
"value": 4,
"isPersistent": false
}],
"lists": [{
"listName": "D# Minor Pentatonic",
"contents": ["78",
"75",
"73",
"75",
"70",
"78",
"73",
"75",
"75",
"78",
"75",
"73",
"75",
"70",
"75",
"78",
"73",
"75",
"78",
"75",
"73",
"75",
"70",
"73",
"68",
"70",
"66",
"68",
"63"],
"isPersistent": false,
"x": 5,
"y": 32,
"width": 125,
"height": 206,
"visible": true
}],
"scripts": [[52,
8,
[["whenIReceive", "start"],
["setVar:to:", "a", "1"],
["doRepeat",
["lineCountOfList:", "D# Minor Pentatonic"],
[["noteOn:duration:elapsed:from:", ["getLine:ofList:", ["readVariable", "a"], "D# Minor Pentatonic"], 0.5], ["changeVar:by:", "a", 1]]]]],
[53,
186,
[["whenIReceive", "start"],
["setVar:to:", "x", "1"],
["rest:elapsed:from:", 7.25],
["doRepeat",
["lineCountOfList:", "D# Minor Pentatonic"],
[["noteOn:duration:elapsed:from:", ["getLine:ofList:", ["readVariable", "x"], "D# Minor Pentatonic"], 0.25], ["changeVar:by:", "x", 1]]]]],
[48,
557,
[["whenIReceive", "start"],
["setVar:to:", "z", "1"],
["rest:elapsed:from:", 13],
["doRepeat",
["lineCountOfList:", "D# Minor Pentatonic"],
[["noteOn:duration:elapsed:from:", ["getLine:ofList:", ["readVariable", "z"], "D# Minor Pentatonic"], 0.0625], ["changeVar:by:", "z", 1]]]]],
[49,
368,
[["whenIReceive", "start"],
["setVar:to:", "y", "1"],
["rest:elapsed:from:", 11],
["doRepeat",
["lineCountOfList:", "D# Minor Pentatonic"],
[["noteOn:duration:elapsed:from:", ["getLine:ofList:", ["readVariable", "y"], "D# Minor Pentatonic"], 0.125], ["changeVar:by:", "y", 1]]]]],
[52,
745,
[["whenIReceive", "start"],
["setVar:to:", "d", "1"],
["rest:elapsed:from:", 13.5],
["doRepeat",
["lineCountOfList:", "D# Minor Pentatonic"],
[["noteOn:duration:elapsed:from:", ["getLine:ofList:", ["readVariable", "d"], "D# Minor Pentatonic"], 0.03125], ["changeVar:by:", "d", 1]]]]]],
"sounds": [{
"soundName": "pop",
"soundID": 0,
"md5": "83a9787d4cb6f3b7632b4ddfebf74367.wav",
"sampleCount": 258,
"rate": 11025,
"format": ""
}],
"costumes": [{
"costumeName": "backdrop1",
"baseLayerID": 4,
"baseLayerMD5": "b61b1077b0ea1931abee9dbbfa7903ff.png",
"bitmapResolution": 2,
"rotationCenterX": 480,
"rotationCenterY": 360
}],
"currentCostumeIndex": 0,
"penLayerMD5": "5c81a336fab8be57adc039a8a2b33ca9.png",
"penLayerID": 0,
"tempoBPM": 60,
"videoAlpha": 0.5,
"children": [{
"objName": "Indicator",
"scripts": [[247.85,
32.8,
[["procDef", "foo %n", ["bar"], [1], false],
["hide"],
["clearPenTrails"],
["penColor:", 5968094],
["say:", ["getParam", "bar", "r"]],
["stopScripts", "this script"]]],
[41, 36, [["whenGreenFlag"], ["call", "foo %n", 1]]]],
"sounds": [{
"soundName": "pop",
"soundID": 0,
"md5": "83a9787d4cb6f3b7632b4ddfebf74367.wav",
"sampleCount": 258,
"rate": 11025,
"format": ""
}],
"costumes": [{
"costumeName": "costume1",
"baseLayerID": 1,
"baseLayerMD5": "d36f6603ec293d2c2198d3ea05109fe0.png",
"bitmapResolution": 2,
"rotationCenterX": 0,
"rotationCenterY": 0
}],
"currentCostumeIndex": 0,
"scratchX": 22,
"scratchY": -26,
"scale": 1,
"direction": 90,
"rotationStyle": "normal",
"isDraggable": false,
"indexInLibrary": 3,
"visible": false,
"spriteInfo": {
}
},
{
"target": "Stage",
"cmd": "timer",
"param": null,
"color": 2926050,
"label": "timer",
"mode": 1,
"sliderMin": 0,
"sliderMax": 100,
"isDiscrete": true,
"x": 5,
"y": 5,
"visible": false
},
{
"target": "Stage",
"cmd": "getVar:",
"param": "x",
"color": 15629590,
"label": "x",
"mode": 1,
"sliderMin": 0,
"sliderMax": 100,
"isDiscrete": true,
"x": 5,
"y": 268,
"visible": true
},
{
"target": "Stage",
"cmd": "getVar:",
"param": "y",
"color": 15629590,
"label": "y",
"mode": 1,
"sliderMin": 0,
"sliderMax": 100,
"isDiscrete": true,
"x": 5,
"y": 295,
"visible": true
},
{
"target": "Stage",
"cmd": "getVar:",
"param": "z",
"color": 15629590,
"label": "z",
"mode": 1,
"sliderMin": 0,
"sliderMax": 100,
"isDiscrete": true,
"x": 78,
"y": 268,
"visible": true
},
{
"objName": "Play",
"scripts": [[32, 33, [["whenClicked"], ["broadcast:", "start"]]]],
"sounds": [{
"soundName": "pop",
"soundID": 0,
"md5": "83a9787d4cb6f3b7632b4ddfebf74367.wav",
"sampleCount": 258,
"rate": 11025,
"format": ""
}],
"costumes": [{
"costumeName": "costume1",
"baseLayerID": 2,
"baseLayerMD5": "30f811366ae3a53e6447932cc7f0212d.png",
"bitmapResolution": 2,
"rotationCenterX": 68,
"rotationCenterY": 115
}],
"currentCostumeIndex": 0,
"scratchX": 2,
"scratchY": -48,
"scale": 1,
"direction": 90,
"rotationStyle": "normal",
"isDraggable": false,
"indexInLibrary": 1,
"visible": true,
"spriteInfo": {
}
},
{
"target": "Stage",
"cmd": "getVar:",
"param": "d",
"color": 15629590,
"label": "d",
"mode": 1,
"sliderMin": 0,
"sliderMax": 100,
"isDiscrete": true,
"x": 5,
"y": 241,
"visible": true
},
{
"target": "Stage",
"cmd": "getVar:",
"param": "a",
"color": 15629590,
"label": "a",
"mode": 1,
"sliderMin": 0,
"sliderMax": 100,
"isDiscrete": true,
"x": 78,
"y": 241,
"visible": true
},
{
"objName": "Stop",
"scripts": [[45, 104, [["whenClicked"], ["stopScripts", "all"]]]],
"sounds": [{
"soundName": "pop",
"soundID": 0,
"md5": "83a9787d4cb6f3b7632b4ddfebf74367.wav",
"sampleCount": 258,
"rate": 11025,
"format": ""
}],
"costumes": [{
"costumeName": "costume1",
"baseLayerID": 3,
"baseLayerMD5": "3de406f265b8d664406adf7c70762514.png",
"bitmapResolution": 2,
"rotationCenterX": 68,
"rotationCenterY": 70
}],
"currentCostumeIndex": 0,
"scratchX": 121,
"scratchY": -33,
"scale": 1,
"direction": 90,
"rotationStyle": "normal",
"isDraggable": false,
"indexInLibrary": 2,
"visible": true,
"spriteInfo": {
}
},
{
"listName": "D# Minor Pentatonic",
"contents": ["78",
"75",
"73",
"75",
"70",
"78",
"73",
"75",
"75",
"78",
"75",
"73",
"75",
"70",
"75",
"78",
"73",
"75",
"78",
"75",
"73",
"75",
"70",
"73",
"68",
"70",
"66",
"68",
"63"],
"isPersistent": false,
"x": 5,
"y": 32,
"width": 125,
"height": 206,
"visible": true
}],
"info": {
"spriteCount": 3,
"projectID": "118381369",
"videoOn": false,
"hasCloudData": false,
"userAgent": "Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/53.0.2785.143 Safari\/537.36",
"scriptCount": 9,
"flashVersion": "MAC 23,0,0,185",
"swfVersion": "v450.1"
}
}

88
test/unit/import_sb2.js Normal file
View file

@ -0,0 +1,88 @@
var fs = require('fs');
var path = require('path');
var test = require('tap').test;
var clone = require('../../src/sprites/clone');
var runtime = require('../../src/engine/runtime');
var sb2 = require('../../src/import/sb2import');
test('spec', function (t) {
t.type(sb2, 'function');
t.end();
});
test('default', function (t) {
// Get SB2 JSON (string)
var uri = path.resolve(__dirname, '../fixtures/default.json');
var file = fs.readFileSync(uri, 'utf8');
// Create runtime instance & load SB2 into it
var rt = new runtime();
sb2(file, rt);
// Test
t.type(file, 'string');
t.type(rt, 'object');
t.type(rt.targets, 'object');
t.ok(rt.targets[0] instanceof clone);
t.type(rt.targets[0].id, 'string');
t.type(rt.targets[0].blocks, 'object');
t.type(rt.targets[0].variables, 'object');
t.type(rt.targets[0].lists, 'object');
t.equal(rt.targets[0].isOriginal, true);
t.equal(rt.targets[0].currentCostume, 0);
t.equal(rt.targets[0].isOriginal, true);
t.equal(rt.targets[0].isStage, true);
t.ok(rt.targets[1] instanceof clone);
t.type(rt.targets[1].id, 'string');
t.type(rt.targets[1].blocks, 'object');
t.type(rt.targets[1].variables, 'object');
t.type(rt.targets[1].lists, 'object');
t.equal(rt.targets[1].isOriginal, true);
t.equal(rt.targets[1].currentCostume, 0);
t.equal(rt.targets[1].isOriginal, true);
t.equal(rt.targets[1].isStage, false);
t.end();
});
test('demo', function (t) {
// Get SB2 JSON (string)
var uri = path.resolve(__dirname, '../fixtures/demo.json');
var file = fs.readFileSync(uri, 'utf8');
// Create runtime instance & load SB2 into it
var rt = new runtime();
sb2(file, rt);
// Test
t.type(file, 'string');
t.type(rt, 'object');
t.type(rt.targets, 'object');
t.ok(rt.targets[0] instanceof clone);
t.type(rt.targets[0].id, 'string');
t.type(rt.targets[0].blocks, 'object');
t.type(rt.targets[0].variables, 'object');
t.type(rt.targets[0].lists, 'object');
t.equal(rt.targets[0].isOriginal, true);
t.equal(rt.targets[0].currentCostume, 0);
t.equal(rt.targets[0].isOriginal, true);
t.equal(rt.targets[0].isStage, true);
t.ok(rt.targets[1] instanceof clone);
t.type(rt.targets[1].id, 'string');
t.type(rt.targets[1].blocks, 'object');
t.type(rt.targets[1].variables, 'object');
t.type(rt.targets[1].lists, 'object');
t.equal(rt.targets[1].isOriginal, true);
t.equal(rt.targets[1].currentCostume, 0);
t.equal(rt.targets[1].isOriginal, true);
t.equal(rt.targets[1].isStage, false);
t.end();
});