Skip to content

Commit b0d1e46

Browse files
committed
test(tests): fix up tests to accommodate new apis
1 parent b808e74 commit b0d1e46

File tree

10 files changed

+154
-64
lines changed

10 files changed

+154
-64
lines changed

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@ var validate = require('./lib/validate');
77
/**
88
* Unpacks, parses, validates, and analyzes Scratch projects. If successful,
99
* will return a valid Scratch project object with appended metadata.
10-
* @param {boolean} isSprite Whether this is a sprite (true) or whole project (false)
1110
* @param {Buffer | string} input Buffer or string representing project
11+
* @param {boolean} isSprite Whether this is a sprite (true) or whole project (false)
1212
* @param {Function} callback Returns error or project data
1313
*/
14-
module.exports = function (isSprite, input, callback) {
14+
module.exports = function (input, isSprite, callback) {
1515
// First unpack the input (need this outside of the async waterfall so that
1616
// unpackedProject can be refered to again)
17-
unpack(isSprite, input, function (err, unpackedProject) {
17+
unpack(input, isSprite, function (err, unpackedProject) {
1818
if (err) return callback(err);
1919

2020
async.waterfall([
2121
function (cb) {
2222
parse(unpackedProject[0], cb);
2323
},
24+
// TODO is there a better way to pass this arg
25+
// than partially applying this funciton?
2426
validate.bind(null, isSprite)
2527
], function (error, validatedInput) {
2628
// One more callback wrapper so that we can re-package everything

lib/sb2_definitions.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@
108108
"currentCostumeIndex"
109109
]
110110
},
111+
"stage_child": {
112+
"type": "object",
113+
"description": "A child of the stage, can be a sprite or a monitor"
114+
},
111115
"stage_object" : {
112116
"type": "object",
113117
"properties": {
@@ -175,7 +179,7 @@
175179
},
176180
"children": {
177181
"type": "array",
178-
"items": {"$ref": "#/definitions/sprite_object"}
182+
"items": {"$ref": "#/definitions/stage_child"}
179183
}
180184
},
181185
"required": [

lib/unpack.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ var unzip = require('./unzip');
44
* If input a buffer, transforms buffer into a UTF-8 string.
55
* If input is encoded in zip or gzip format, the input will be extracted and decoded.
66
* If input is a string, passes that string along to the given callback.
7+
* @param {Buffer | string} input Project data
78
* @param {boolean} isSprite Whether the input should be treated as
89
* a sprite (true) or a whole project (false)
9-
* @param {Buffer | string} input Project data
1010
* @param {Function} callback Error or stringified project data
1111
* @return {void}
1212
*/
13-
module.exports = function (isSprite, input, callback) {
13+
module.exports = function (input, isSprite, callback) {
1414
if (typeof input === 'string') {
1515
// Pass string to callback
1616
return callback(null, [input, null]);
@@ -48,5 +48,5 @@ module.exports = function (isSprite, input, callback) {
4848
// Return error if legacy encoding detected
4949
if (isLegacy) return callback('Parser only supports Scratch 2.X and above');
5050

51-
unzip(isSprite, input, isGZip, callback);
51+
unzip(input, isGZip, isSprite, callback);
5252
};

lib/unzip.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ var GZip = require('gzip-js');
33

44
/**
55
* Unpacks a zip or gzip file.
6-
* @param {boolean} isSprite Whether the input should be treated as
7-
* a sprite (true) or whole project (false)
86
* @param {string} input Zip file provided as a string
97
* @param {boolean} isGZip Whether the input is a GZip file, otherwise treat as zip
8+
* @param {boolean} isSprite Whether the input should be treated as
9+
* a sprite (true) or whole project (false)
1010
* @param {array} callback Array including both the project and zip archive
1111
* @return {void}
1212
*/
13-
module.exports = function (isSprite, input, isGZip, callback) {
13+
module.exports = function (input, isGZip, isSprite, callback) {
1414
var msg = 'Failed to unzip and extract project.json, with error: ';
1515
if (isGZip) {
1616
var unpackedProject = null;

test/integration/empty.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ var data = require('../fixtures/data');
44
var parser = require('../../index');
55

66
test('sb', function (t) {
7-
parser(data.empty.sb, function (err, res) {
7+
parser(data.empty.sb, false, function (err, res) {
88
t.type(err, 'string');
99
t.type(res, 'undefined');
1010
t.end();
1111
});
1212
});
1313

1414
test('sb2', function (t) {
15-
parser(data.empty.sb2, function (err, result) {
15+
parser(data.empty.sb2, false, function (err, result) {
1616
t.equal(err, null);
1717
t.equal(Array.isArray(result), true);
1818
var res = result[0];
@@ -25,7 +25,7 @@ test('sb2', function (t) {
2525
});
2626

2727
test('json', function (t) {
28-
parser(data.empty.json, function (err, result) {
28+
parser(data.empty.json, false, function (err, result) {
2929
t.equal(err, null);
3030
t.equal(Array.isArray(result), true);
3131
var res = result[0];
@@ -38,7 +38,7 @@ test('json', function (t) {
3838
});
3939

4040
test('json string', function (t) {
41-
parser(data.empty.json.toString('utf-8'), function (err, result) {
41+
parser(data.empty.json.toString('utf-8'), false, function (err, result) {
4242
t.equal(err, null);
4343
t.equal(Array.isArray(result), true);
4444
var res = result[0];
@@ -51,7 +51,7 @@ test('json string', function (t) {
5151
});
5252

5353
test('gzipped json', function (t) {
54-
parser(data.empty.gzipJson, function (err, result) {
54+
parser(data.empty.gzipJson, false, function (err, result) {
5555
t.equal(err, null);
5656
t.equal(Array.isArray(result), true);
5757
var res = result[0];

test/integration/example.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ var data = require('../fixtures/data');
44
var parser = require('../../index');
55

66
test('sb', function (t) {
7-
parser(data.example.sb, function (err, res) {
7+
parser(data.example.sb, false, function (err, res) {
88
t.type(err, 'string');
99
t.type(res, 'undefined');
1010
t.end();
1111
});
1212
});
1313

1414
test('sb2', function (t) {
15-
parser(data.example.sb2, function (err, result) {
15+
parser(data.example.sb2, false, function (err, result) {
1616
t.equal(err, null);
1717
t.equal(Array.isArray(result), true);
1818
var res = result[0];
@@ -25,7 +25,7 @@ test('sb2', function (t) {
2525
});
2626

2727
test('json', function (t) {
28-
parser(data.example.json, function (err, result) {
28+
parser(data.example.json, false, function (err, result) {
2929
t.equal(err, null);
3030
t.equal(Array.isArray(result), true);
3131
var res = result[0];
@@ -38,7 +38,7 @@ test('json', function (t) {
3838
});
3939

4040
test('json string', function (t) {
41-
parser(data.example.json.toString('utf-8'), function (err, result) {
41+
parser(data.example.json.toString('utf-8'), false, function (err, result) {
4242
t.equal(err, null);
4343
t.equal(Array.isArray(result), true);
4444
var res = result[0];
@@ -51,7 +51,7 @@ test('json string', function (t) {
5151
});
5252

5353
test('gzipped json', function (t) {
54-
parser(data.example.gzipJson, function (err, result) {
54+
parser(data.example.gzipJson, false, function (err, result) {
5555
t.equal(err, null);
5656
t.equal(Array.isArray(result), true);
5757
var res = result[0];

test/integration/stress.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test('sb', function (t) {
77
var set = data.sb;
88
t.plan(set.length * 2);
99
for (var i in data.sb) {
10-
parser(data.sb[i], function (err, res) {
10+
parser(data.sb[i], false, function (err, res) {
1111
t.type(err, 'string');
1212
t.type(res, 'undefined');
1313
});
@@ -18,7 +18,7 @@ test('sb2', function (t) {
1818
var set = data.sb2;
1919
t.plan(set.length * 5);
2020
for (var i in data.sb2) {
21-
parser(data.sb2[i], function (err, result) {
21+
parser(data.sb2[i], false, function (err, result) {
2222
t.equal(err, null);
2323
t.equal(Array.isArray(result), true);
2424
var res = result[0];
@@ -34,7 +34,7 @@ test('json', function (t) {
3434
var set = data.json;
3535
t.plan(set.length * 5);
3636
for (var i in data.json) {
37-
parser(data.json[i], function (err, result) {
37+
parser(data.json[i], false, function (err, result) {
3838
t.equal(err, null);
3939
t.equal(Array.isArray(result), true);
4040
var res = result[0];
@@ -50,7 +50,7 @@ test('json string', function (t) {
5050
var set = data.json;
5151
t.plan(set.length * 5);
5252
for (var i in data.json) {
53-
parser(data.json[i].toString('utf-8'), function (err, result) {
53+
parser(data.json[i].toString('utf-8'), false, function (err, result) {
5454
t.equal(err, null);
5555
t.equal(Array.isArray(result), true);
5656
var res = result[0];

test/unit/unpack.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test('spec', function (t) {
2121

2222
test('sb', function (t) {
2323
var buffer = new Buffer(fixtures.sb);
24-
unpack(buffer, function (err, res) {
24+
unpack(buffer, false, function (err, res) {
2525
t.type(err, 'string');
2626
t.type(res, 'undefined');
2727
t.end();
@@ -30,7 +30,7 @@ test('sb', function (t) {
3030

3131
test('sb2', function (t) {
3232
var buffer = new Buffer(fixtures.sb2);
33-
unpack(buffer, function (err, res) {
33+
unpack(buffer, false, function (err, res) {
3434
t.equal(err, null);
3535
t.equal(Array.isArray(res), true);
3636
t.type(res[0], 'string');
@@ -42,9 +42,18 @@ test('sb2', function (t) {
4242
});
4343
});
4444

45+
test('sb2 does not validate as sprite', function (t) {
46+
var buffer = new Buffer(fixtures.sb2);
47+
unpack(buffer, true, function (err, res) {
48+
t.type(err, 'string');
49+
t.type(res, 'undefined');
50+
t.end();
51+
});
52+
});
53+
4554
test('json', function (t) {
4655
var buffer = new Buffer(fixtures.json);
47-
unpack(buffer, function (err, res) {
56+
unpack(buffer, false, function (err, res) {
4857
t.equal(err, null);
4958
t.equal(Array.isArray(res), true);
5059
t.type(res[0], 'string');
@@ -58,7 +67,7 @@ test('json', function (t) {
5867

5968
test('json utf-8 string', function (t) {
6069
var buffer = new Buffer(fixtures.json);
61-
unpack(buffer.toString('utf-8'), function (err, res) {
70+
unpack(buffer.toString('utf-8'), false, function (err, res) {
6271
t.equal(err, null);
6372
t.equal(Array.isArray(res), true);
6473
t.type(res[0], 'string');
@@ -71,7 +80,16 @@ test('json utf-8 string', function (t) {
7180
});
7281

7382
test('invalid string', function (t) {
74-
unpack('this is not json', function (err, res) {
83+
unpack('this is not json', false, function (err, res) {
84+
t.equal(err, null);
85+
t.equal(Array.isArray(res), true);
86+
t.type(res[0], 'string');
87+
t.throws(function () {
88+
JSON.parse(res[0]);
89+
});
90+
t.equal(res[1], null);
91+
});
92+
unpack('this is not json', true, function (err, res) {
7593
t.equal(err, null);
7694
t.equal(Array.isArray(res), true);
7795
t.type(res[0], 'string');
@@ -85,23 +103,23 @@ test('invalid string', function (t) {
85103

86104
test('undefined', function (t) {
87105
var foo;
88-
unpack(foo, function (err, res) {
106+
unpack(false, foo, function (err, res) {
89107
t.type(err, 'string');
90108
t.type(res, 'undefined');
91109
t.end();
92110
});
93111
});
94112

95113
test('null', function (t) {
96-
unpack(null, function (err, obj) {
114+
unpack(false, null, function (err, obj) {
97115
t.type(err, 'string');
98116
t.type(obj, 'undefined');
99117
t.end();
100118
});
101119
});
102120

103121
test('object', function (t) {
104-
unpack({}, function (err, obj) {
122+
unpack(false, {}, function (err, obj) {
105123
t.type(err, 'string');
106124
t.type(obj, 'undefined');
107125
t.end();

0 commit comments

Comments
 (0)