Merge pull request #1842 from kchadha/fix-serialization-after-block-cleanup

Serialize x and y for topLevel blocks even if they are 0
This commit is contained in:
Karishma Chadha 2018-12-12 21:07:59 -05:00 committed by GitHub
commit ef2e9eb205
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 7 deletions

View file

@ -200,15 +200,13 @@ const serializeBlock = function (block) {
obj.parent = block.parent; obj.parent = block.parent;
obj.inputs = serializeInputs(block.inputs); obj.inputs = serializeInputs(block.inputs);
obj.fields = serializeFields(block.fields); obj.fields = serializeFields(block.fields);
obj.topLevel = block.topLevel ? block.topLevel : false;
obj.shadow = block.shadow; obj.shadow = block.shadow;
if (block.topLevel) { if (block.topLevel) {
if (block.x) { obj.topLevel = true;
obj.x = Math.round(block.x); obj.x = block.x ? Math.round(block.x) : 0;
} obj.y = block.y ? Math.round(block.y) : 0;
if (block.y) { } else {
obj.y = Math.round(block.y); obj.topLevel = false;
}
} }
if (block.mutation) { if (block.mutation) {
obj.mutation = block.mutation; obj.mutation = block.mutation;

BIN
test/fixtures/top-level-reporters.sb3 vendored Normal file

Binary file not shown.

View file

@ -8,6 +8,7 @@ const commentsSB2ProjectPath = path.resolve(__dirname, '../fixtures/comments.sb2
const commentsSB3ProjectPath = path.resolve(__dirname, '../fixtures/comments.sb3'); const commentsSB3ProjectPath = path.resolve(__dirname, '../fixtures/comments.sb3');
const commentsSB3NoDupeIds = path.resolve(__dirname, '../fixtures/comments_no_duplicate_id_serialization.sb3'); const commentsSB3NoDupeIds = path.resolve(__dirname, '../fixtures/comments_no_duplicate_id_serialization.sb3');
const variableReporterSB2ProjectPath = path.resolve(__dirname, '../fixtures/top-level-variable-reporter.sb2'); const variableReporterSB2ProjectPath = path.resolve(__dirname, '../fixtures/top-level-variable-reporter.sb2');
const topLevelReportersProjectPath = path.resolve(__dirname, '../fixtures/top-level-reporters.sb3');
const FakeRenderer = require('../fixtures/fake-renderer'); const FakeRenderer = require('../fixtures/fake-renderer');
test('serialize', t => { test('serialize', t => {
@ -198,6 +199,38 @@ test('serializeBlocks', t => {
}); });
}); });
test('serializeBlocks serializes x and y for topLevel blocks with x,y of 0,0', t => {
const vm = new VirtualMachine();
vm.loadProject(readFileToBuffer(topLevelReportersProjectPath))
.then(() => {
// Verify that there are 2 blocks and they are both top level
const blocks = vm.runtime.targets[1].blocks._blocks;
const blockIds = Object.keys(blocks);
t.equal(blockIds.length, 2);
const blocksArray = blockIds.map(key => blocks[key]);
t.equal(blocksArray.every(b => b.topLevel), true);
// Simulate cleaning up the blocks by resetting x and y positions to 0
blockIds.forEach(blockId => {
blocks[blockId].x = 0;
blocks[blockId].y = 0;
});
const result = sb3.serializeBlocks(blocks);
const serializedBlocks = result[0];
t.type(serializedBlocks, 'object');
const serializedBlockIds = Object.keys(serializedBlocks);
t.equal(serializedBlockIds.length, 2);
const firstBlock = serializedBlocks[serializedBlockIds[0]];
const secondBlock = serializedBlocks[serializedBlockIds[1]];
t.equal(firstBlock.x, 0);
t.equal(firstBlock.y, 0);
t.equal(secondBlock.x, 0);
t.equal(secondBlock.y, 0);
t.end();
});
});
test('deserializeBlocks', t => { test('deserializeBlocks', t => {
const vm = new VirtualMachine(); const vm = new VirtualMachine();
vm.loadProject(readFileToBuffer(commentsSB3ProjectPath)) vm.loadProject(readFileToBuffer(commentsSB3ProjectPath))