Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apple502j committed Jun 30, 2020
1 parent 255d105 commit 882ed8b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/engine/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ class Blocks {
// Delete comments attached to the block.
if (block.comment) {
const editingTarget = this.runtime.getEditingTarget();
if (editingTarget.comments.hasOwnProperty(block.comment)) {
if (editingTarget && editingTarget.comments.hasOwnProperty(block.comment)) {
delete editingTarget.comments[block.comment];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/serialization/sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ const deserializeFields = function (fields) {
* work with pre-parsed deserialized blocks.
*
* @param {object} blocks Serialized SB3 "blocks" property of a target. Will be mutated.
* @return {object} input is modified and returned
* @return {object} object that has "blocks" and "oldToNew" attributes
*/
const deserializeBlocks = function (blocks) {
// oldToNew is used to fix comments attached to variable blocks: see #1893
Expand Down
Binary file added test/fixtures/variable-comment.sb3
Binary file not shown.
53 changes: 53 additions & 0 deletions test/integration/variable-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const path = require('path');
const test = require('tap').test;
const makeTestStorage = require('../fixtures/make-test-storage');
const readFileToBuffer = require('../fixtures/readProjectFile').readFileToBuffer;
const VirtualMachine = require('../../src/index');

const projectUri = path.resolve(__dirname, '../fixtures/variable-comment.sb3');
const project = readFileToBuffer(projectUri);

test('load an sb3 project with comments attached to variable blocks (#1893)', t => {
const vm = new VirtualMachine();
vm.attachStorage(makeTestStorage());

// Evaluate playground data and exit
vm.on('playgroundData', e => {
const threads = JSON.parse(e.threads);
t.equal(threads.length, 0);

const stage = vm.runtime.targets[0];
const stageComments = Object.values(stage.comments);

// Stage has 2 comments
t.equal(stageComments.length, 2);

// comment1 is atached to variable block
t.equal(stageComments[0].text, 'comment1');
const variableBlock = stage.blocks.getBlock(stageComments[0].blockId);
t.equal(variableBlock.opcode, 'data_variable');

// comment2 is atached to variable block
t.equal(stageComments[1].text, 'comment2');
const listBlock = stage.blocks.getBlock(stageComments[1].blockId);
t.equal(listBlock.opcode, 'data_listcontents');

t.end();
process.nextTick(process.exit);
});

// Start VM, load project, and run
t.doesNotThrow(() => {
vm.start();
vm.clear();
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(project).then(() => {
vm.greenFlag();
setTimeout(() => {
vm.getPlaygroundData();
vm.stopAll();
}, 100);
});
});
});
19 changes: 19 additions & 0 deletions test/unit/engine_blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,25 @@ test('delete inputs', t => {
t.end();
});

test('delete block with comment', t => {
const b = new Blocks(new Runtime());
const fakeTarget = {
comments: {
bar: {
blockId: 'foo'
}
}
};
b.runtime.getEditingTarget = () => fakeTarget;
b.createBlock({
id: 'foo',
comment: 'bar'
});
b.deleteBlock('foo');
t.notOk(fakeTarget.comments.hasOwnProperty('bar'));
t.end();
});

test('updateAssetName function updates name in sound field', t => {
const b = new Blocks(new Runtime());
b.createBlock({
Expand Down
13 changes: 13 additions & 0 deletions test/unit/sprites_rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ test('blocks get new id on duplicate', t => {
});
});

test('comments are duplicated when duplicating target', t => {
const r = new Runtime();
const s = new Sprite(null, r);
const rt = new RenderedTarget(s, r);
rt.createComment('commentid', null, 'testcomment', 0, 0, 100, 100, false);
t.ok(s.comments.hasOwnProperty('commentid'));
return rt.duplicate().then(duplicate => {
t.notOk(duplicate.comments.hasOwnProperty('commentid'));
t.ok(Object.keys(duplicate.comments).length === 1);
t.end();
});
});

test('direction', t => {
const r = new Runtime();
const s = new Sprite(null, r);
Expand Down

0 comments on commit 882ed8b

Please sign in to comment.