From 14feb64005fa33b172d165e1cae59a3f47e8a3a3 Mon Sep 17 00:00:00 2001 From: Tim Mickel <tim.mickel@gmail.com> Date: Thu, 8 Sep 2016 09:40:01 -0400 Subject: [PATCH] Create a new input if one doesn't exist (#148) * Create a new input if one doesn't exist * Add regression tests for move-into-empty and obscuring shadows via move --- src/engine/blocks.js | 12 ++++++--- test/unit/blocks.js | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index a3d949a5f..421087f5b 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -249,9 +249,15 @@ Blocks.prototype.moveBlock = function (e) { if (e.newInput !== undefined) { // Moved to the new parent's input. // Don't obscure the shadow block. - var newInput = this._blocks[e.newParent].inputs[e.newInput]; - newInput.name = e.newInput; - newInput.block = e.id; + var oldShadow = null; + if (this._blocks[e.newParent].inputs.hasOwnProperty(e.newInput)) { + oldShadow = this._blocks[e.newParent].inputs[e.newInput].shadow; + } + this._blocks[e.newParent].inputs[e.newInput] = { + name: e.newInput, + block: e.id, + shadow: oldShadow + }; } else { // Moved to the new parent's next connection. this._blocks[e.newParent].next = e.id; diff --git a/test/unit/blocks.js b/test/unit/blocks.js index 81fb0ad93..2c9f8f13e 100644 --- a/test/unit/blocks.js +++ b/test/unit/blocks.js @@ -291,6 +291,67 @@ test('move', function (t) { t.end(); }); +test('move into empty', function (t) { + var b = new Blocks(); + b.createBlock({ + id: 'foo', + opcode: 'TEST_BLOCK', + next: null, + fields: {}, + inputs: {}, + topLevel: true + }); + b.createBlock({ + id: 'bar', + opcode: 'TEST_BLOCK', + next: null, + fields: {}, + inputs: {}, + topLevel: true + }); + b.moveBlock({ + id: 'bar', + newInput: 'fooInput', + newParent: 'foo' + }); + t.equal(b._blocks['foo'].inputs['fooInput'].block, 'bar'); + t.end(); +}); + +test('move no obscure shadow', function (t) { + var b = new Blocks(); + b.createBlock({ + id: 'foo', + opcode: 'TEST_BLOCK', + next: null, + fields: {}, + inputs: { + 'fooInput': { + name: 'fooInput', + block: 'x', + shadow: 'y' + } + }, + topLevel: true + }); + b.createBlock({ + id: 'bar', + opcode: 'TEST_BLOCK', + next: null, + fields: {}, + inputs: {}, + topLevel: true + }); + b.moveBlock({ + id: 'bar', + newInput: 'fooInput', + newParent: 'foo' + }); + t.equal(b._blocks['foo'].inputs['fooInput'].block, 'bar'); + t.equal(b._blocks['foo'].inputs['fooInput'].shadow, 'y'); + t.end(); +}); + test('change', function (t) { var b = new Blocks(); b.createBlock({