From 2f88a40f9bfa956927887f779c326c667ce64049 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Fri, 2 Aug 2019 10:45:54 -0700 Subject: [PATCH] Added block disposal tests. --- tests/mocha/block_test.js | 418 +++++++++++++++++++++++++------------- 1 file changed, 275 insertions(+), 143 deletions(-) diff --git a/tests/mocha/block_test.js b/tests/mocha/block_test.js index 8a173ad53..f9f97c32b 100644 --- a/tests/mocha/block_test.js +++ b/tests/mocha/block_test.js @@ -19,25 +19,14 @@ */ suite('Blocks', function() { - suite('Unplug', function() { - function assertUnpluggedNoheal(blocks) { - // A has nothing connected to it. - assertEquals(0, blocks.A.getChildren().length); - // B and C are still connected. - assertEquals(blocks.B, blocks.C.getParent()); - // B is the top of its stack. - assertNull(blocks.B.getParent()); - } - function assertUnpluggedHealed(blocks) { - // A and C are connected. - assertEquals(1, blocks.A.getChildren().length); - assertEquals(blocks.A, blocks.C.getParent()); - // B has nothing connected to it. - assertEquals(0, blocks.B.getChildren().length); - // B is the top of its stack. - assertNull(blocks.B.getParent()); - } + setup(function() { + this.workspace = new Blockly.Workspace(); + }); + teardown(function() { + this.workspace.dispose(); + }); + suite('Connection Management', function() { setup(function() { Blockly.defineBlocksWithJsonArray([{ "type": "stack_block", @@ -56,142 +45,285 @@ suite('Blocks', function() { ], "output": null }]); - - this.workspace = new Blockly.Workspace(); }); teardown(function() { delete Blockly.Blocks['stack_block']; delete Blockly.Blocks['row_block']; - - this.workspace.dispose(); }); - suite('Row', function() { - setup(function() { - var blockA = this.workspace.newBlock('row_block'); - var blockB = this.workspace.newBlock('row_block'); - var blockC = this.workspace.newBlock('row_block'); - - blockA.inputList[0].connection.connect(blockB.outputConnection); - blockB.inputList[0].connection.connect(blockC.outputConnection); - - assertEquals(blockB, blockC.getParent()); - - this.blocks = { - A: blockA, - B: blockB, - C: blockC - }; - }); - - test('Don\'t heal', function() { - this.blocks.B.unplug(false); - assertUnpluggedNoheal(this.blocks); - }); - test('Heal', function() { - this.blocks.B.unplug(true); - // Each block has only one input, and the types work. - assertUnpluggedHealed(this.blocks); - }); - test('Heal with bad checks', function() { - var blocks = this.blocks; - - // A and C can't connect, but both can connect to B. - blocks.A.inputList[0].connection.setCheck('type1'); - blocks.C.outputConnection.setCheck('type2'); - - // Each block has only one input, but the types don't work. - blocks.B.unplug(true); - assertUnpluggedNoheal(blocks); - }); - test('A has multiple inputs', function() { - var blocks = this.blocks; - // Add extra input to parent - blocks.A.appendValueInput("INPUT").setCheck(null); - blocks.B.unplug(true); - assertUnpluggedHealed(blocks); - }); - test('B has multiple inputs', function() { - var blocks = this.blocks; - // Add extra input to middle block - blocks.B.appendValueInput("INPUT").setCheck(null); - blocks.B.unplug(true); - assertUnpluggedHealed(blocks); - }); - test('C has multiple inputs', function() { - var blocks = this.blocks; - // Add extra input to child block - blocks.C.appendValueInput("INPUT").setCheck(null); - // Child block input count doesn't matter. - blocks.B.unplug(true); - assertUnpluggedHealed(blocks); - }); - test('C is Shadow', function() { - var blocks = this.blocks; - blocks.C.setShadow(true); - blocks.B.unplug(true); - // Even though we're asking to heal, it will appear as if it has not - // healed because shadows always stay with the parent. - assertUnpluggedNoheal(blocks); - }); - }); - suite('Stack', function() { - setup(function() { - var blockA = this.workspace.newBlock('stack_block'); - var blockB = this.workspace.newBlock('stack_block'); - var blockC = this.workspace.newBlock('stack_block'); - - blockA.nextConnection.connect(blockB.previousConnection); - blockB.nextConnection.connect(blockC.previousConnection); - - assertEquals(blockB, blockC.getParent()); - - this.blocks = { - A: blockA, - B: blockB, - C: blockC - }; - }); - - test('Don\'t heal', function() { - this.blocks.B.unplug(); - assertUnpluggedNoheal(this.blocks); - }); - test('Heal', function() { - this.blocks.B.unplug(true); - assertUnpluggedHealed(this.blocks); - }); - test('Heal with bad checks', function() { - var blocks = this.blocks; - // A and C can't connect, but both can connect to B. - blocks.A.nextConnection.setCheck('type1'); - blocks.C.previousConnection.setCheck('type2'); - - // The types don't work. - blocks.B.unplug(true); - - // Stack blocks unplug before checking whether the types match. - // TODO (#1994): Check types before unplugging. + suite('Unplug', function() { + function assertUnpluggedNoheal(blocks) { // A has nothing connected to it. assertEquals(0, blocks.A.getChildren().length); - // B has nothing connected to it. - assertEquals(0, blocks.B.getChildren().length); - // C has nothing connected to it. - assertEquals(0, blocks.C.getChildren().length); - // A is the top of its stack. - assertNull(blocks.A.getParent()); + // B and C are still connected. + assertEquals(blocks.B, blocks.C.getParent()); // B is the top of its stack. assertNull(blocks.B.getParent()); - // C is the top of its stack. - assertNull(blocks.C.getParent()); + } + function assertUnpluggedHealed(blocks) { + // A and C are connected. + assertEquals(1, blocks.A.getChildren().length); + assertEquals(blocks.A, blocks.C.getParent()); + // B has nothing connected to it. + assertEquals(0, blocks.B.getChildren().length); + // B is the top of its stack. + assertNull(blocks.B.getParent()); + } + + suite('Row', function() { + setup(function() { + var blockA = this.workspace.newBlock('row_block'); + var blockB = this.workspace.newBlock('row_block'); + var blockC = this.workspace.newBlock('row_block'); + + blockA.inputList[0].connection.connect(blockB.outputConnection); + blockB.inputList[0].connection.connect(blockC.outputConnection); + + assertEquals(blockB, blockC.getParent()); + + this.blocks = { + A: blockA, + B: blockB, + C: blockC + }; + }); + + test('Don\'t heal', function() { + this.blocks.B.unplug(false); + assertUnpluggedNoheal(this.blocks); + }); + test('Heal', function() { + this.blocks.B.unplug(true); + // Each block has only one input, and the types work. + assertUnpluggedHealed(this.blocks); + }); + test('Heal with bad checks', function() { + var blocks = this.blocks; + + // A and C can't connect, but both can connect to B. + blocks.A.inputList[0].connection.setCheck('type1'); + blocks.C.outputConnection.setCheck('type2'); + + // Each block has only one input, but the types don't work. + blocks.B.unplug(true); + assertUnpluggedNoheal(blocks); + }); + test('A has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to parent + blocks.A.appendValueInput("INPUT").setCheck(null); + blocks.B.unplug(true); + assertUnpluggedHealed(blocks); + }); + test('B has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to middle block + blocks.B.appendValueInput("INPUT").setCheck(null); + blocks.B.unplug(true); + assertUnpluggedHealed(blocks); + }); + test('C has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to child block + blocks.C.appendValueInput("INPUT").setCheck(null); + // Child block input count doesn't matter. + blocks.B.unplug(true); + assertUnpluggedHealed(blocks); + }); + test('C is Shadow', function() { + var blocks = this.blocks; + blocks.C.setShadow(true); + blocks.B.unplug(true); + // Even though we're asking to heal, it will appear as if it has not + // healed because shadows always stay with the parent. + assertUnpluggedNoheal(blocks); + }); }); - test('C is Shadow', function() { - var blocks = this.blocks; - blocks.C.setShadow(true); - blocks.B.unplug(true); - // Even though we're asking to heal, it will appear as if it has not - // healed because shadows always stay with the parent. - assertUnpluggedNoheal(blocks); + suite('Stack', function() { + setup(function() { + var blockA = this.workspace.newBlock('stack_block'); + var blockB = this.workspace.newBlock('stack_block'); + var blockC = this.workspace.newBlock('stack_block'); + + blockA.nextConnection.connect(blockB.previousConnection); + blockB.nextConnection.connect(blockC.previousConnection); + + assertEquals(blockB, blockC.getParent()); + + this.blocks = { + A: blockA, + B: blockB, + C: blockC + }; + }); + + test('Don\'t heal', function() { + this.blocks.B.unplug(); + assertUnpluggedNoheal(this.blocks); + }); + test('Heal', function() { + this.blocks.B.unplug(true); + assertUnpluggedHealed(this.blocks); + }); + test.skip('Heal with bad checks', function() { + var blocks = this.blocks; + // A and C can't connect, but both can connect to B. + blocks.A.nextConnection.setCheck('type1'); + blocks.C.previousConnection.setCheck('type2'); + + // The types don't work. + blocks.B.unplug(true); + + // TODO (#1994): Check types before unplugging. Currently + // everything disconnects, when C should stick with B + assertUnpluggedNoheal(); + }); + test('C is Shadow', function() { + var blocks = this.blocks; + blocks.C.setShadow(true); + blocks.B.unplug(true); + // Even though we're asking to heal, it will appear as if it has not + // healed because shadows always stay with the parent. + assertUnpluggedNoheal(blocks); + }); + }); + }); + suite('Dispose', function() { + function assertDisposedNoheal(blocks) { + chai.assert.isNotOk(blocks.A.disposed); + // A has nothing connected to it. + chai.assert.equal(0, blocks.A.getChildren().length); + // B is disposed. + chai.assert.isTrue(blocks.B.disposed); + // And C is disposed. + chai.assert.isTrue(blocks.C.disposed); + } + function assertDisposedHealed(blocks) { + chai.assert.isNotOk(blocks.A.disposed); + chai.assert.isNotOk(blocks.C.disposed); + // A and C are connected. + assertEquals(1, blocks.A.getChildren().length); + assertEquals(blocks.A, blocks.C.getParent()); + // B is disposed. + chai.assert.isTrue(blocks.B.disposed); + } + + suite('Row', function() { + setup(function() { + var blockA = this.workspace.newBlock('row_block'); + var blockB = this.workspace.newBlock('row_block'); + var blockC = this.workspace.newBlock('row_block'); + + blockA.inputList[0].connection.connect(blockB.outputConnection); + blockB.inputList[0].connection.connect(blockC.outputConnection); + + assertEquals(blockB, blockC.getParent()); + + this.blocks = { + A: blockA, + B: blockB, + C: blockC + }; + }); + + test('Don\'t heal', function() { + this.blocks.B.dispose(false); + assertDisposedNoheal(this.blocks); + }); + test('Heal', function() { + this.blocks.B.dispose(true); + // Each block has only one input, and the types work. + assertDisposedHealed(this.blocks); + }); + test('Heal with bad checks', function() { + var blocks = this.blocks; + + // A and C can't connect, but both can connect to B. + blocks.A.inputList[0].connection.setCheck('type1'); + blocks.C.outputConnection.setCheck('type2'); + + // Each block has only one input, but the types don't work. + blocks.B.dispose(true); + assertDisposedNoheal(blocks); + }); + test('A has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to parent + blocks.A.appendValueInput("INPUT").setCheck(null); + blocks.B.dispose(true); + assertDisposedHealed(blocks); + }); + test('B has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to middle block + blocks.B.appendValueInput("INPUT").setCheck(null); + blocks.B.dispose(true); + assertDisposedHealed(blocks); + }); + test('C has multiple inputs', function() { + var blocks = this.blocks; + // Add extra input to child block + blocks.C.appendValueInput("INPUT").setCheck(null); + // Child block input count doesn't matter. + blocks.B.dispose(true); + assertDisposedHealed(blocks); + }); + test('C is Shadow', function() { + var blocks = this.blocks; + blocks.C.setShadow(true); + blocks.B.dispose(true); + // Even though we're asking to heal, it will appear as if it has not + // healed because shadows always get destroyed. + assertDisposedNoheal(blocks); + }); + }); + suite('Stack', function() { + setup(function() { + var blockA = this.workspace.newBlock('stack_block'); + var blockB = this.workspace.newBlock('stack_block'); + var blockC = this.workspace.newBlock('stack_block'); + + blockA.nextConnection.connect(blockB.previousConnection); + blockB.nextConnection.connect(blockC.previousConnection); + + assertEquals(blockB, blockC.getParent()); + + this.blocks = { + A: blockA, + B: blockB, + C: blockC + }; + }); + + test('Don\'t heal', function() { + this.blocks.B.dispose(); + assertDisposedNoheal(this.blocks); + }); + test('Heal', function() { + this.blocks.B.dispose(true); + assertDisposedHealed(this.blocks); + }); + test.skip('Heal with bad checks', function() { + var blocks = this.blocks; + // A and C can't connect, but both can connect to B. + blocks.A.nextConnection.setCheck('type1'); + blocks.C.previousConnection.setCheck('type2'); + + // The types don't work. + blocks.B.dispose(true); + + // TODO (#1994): Check types before unplugging. Current C gets + // left behind when it should get disposed with B. + assertDisposedNoheal(blocks); + }); + test('C is Shadow', function() { + var blocks = this.blocks; + blocks.C.setShadow(true); + blocks.B.dispose(true); + // Even though we're asking to heal, it will appear as if it has not + // healed because shadows always get destroyed. + assertDisposedNoheal(blocks); + }); }); }); });