From 10d1b914f526fb7a51be96584941cabeac532fad Mon Sep 17 00:00:00 2001 From: Rafal Fronczyk Date: Thu, 7 Nov 2024 13:04:58 +0100 Subject: [PATCH] Add test for incorrect costumes attribute in sb2 project --- test/fixtures/sb2/invalid-costumes.json | 23 +++++++++++++++++++++++ test/unit/sb2.js | 22 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 test/fixtures/sb2/invalid-costumes.json diff --git a/test/fixtures/sb2/invalid-costumes.json b/test/fixtures/sb2/invalid-costumes.json new file mode 100644 index 0000000..cc491c4 --- /dev/null +++ b/test/fixtures/sb2/invalid-costumes.json @@ -0,0 +1,23 @@ +{ + "objName": "Stage", + "sounds": [{ + "soundName": "pop", + "soundID": -1, + "md5": "83a9787d4cb6f3b7632b4ddfebf74367.wav", + "sampleCount": 258, + "rate": 11025, + "format": "" + }], + "costumes": { + "invalid": "This should be an array but is an object instead" + }, + "currentCostumeIndex": 0, + "penLayerMD5": "5c81a336fab8be57adc039a8a2b33ca9.png", + "penLayerID": -1, + "children": [], + "info": { + "videoOn": false, + "scriptCount": 0, + "spriteCount": 0 + } +} \ No newline at end of file diff --git a/test/unit/sb2.js b/test/unit/sb2.js index c3a9f2f..5a7aa2c 100644 --- a/test/unit/sb2.js +++ b/test/unit/sb2.js @@ -2,6 +2,9 @@ const fs = require('fs'); const path = require('path'); const test = require('tap').test; const analysis = require('../../lib/index'); +// using the sb2 directly to bypass scratch-parser and excersise +// logic targeting broken project files +const sb2 = require('../../lib/sb2'); const defaultObject = fs.readFileSync( path.resolve(__dirname, '../fixtures/sb2/default.json') @@ -13,6 +16,10 @@ const complexBinary = fs.readFileSync( path.resolve(__dirname, '../fixtures/sb2/complex.sb2') ); +const invalidCostumes = fs.readFileSync( + path.resolve(__dirname, '../fixtures/sb2/invalid-costumes.json') +); + test('default (object)', t => { analysis(defaultObject, (err, result) => { t.ok(typeof err === 'undefined' || err === null); @@ -284,3 +291,18 @@ test('complex (binary)', t => { t.end(); }); }); + +test('stage with invalid costumes', t => { + const project = JSON.parse(invalidCostumes); + + sb2(project, (err, result) => { + t.ok(typeof err === 'undefined' || err === null); + t.type(result, 'object'); + t.type(result.backdrops, 'object'); + t.equal(result.backdrops.count, 0); + t.same(result.backdrops.id, []); + t.same(result.backdrops.hash, []); + + t.end(); + }); +});