mirror of
https://github.com/google/blockly.git
synced 2026-01-09 18:10:08 +01:00
Removed duplicate block tests and renamed tests. (#3812)
* Removed duplicate block tests and renamed tests. * Refactor shared code and add comment about parent/middle/child.
This commit is contained in:
@@ -1,243 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2018 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @fileoverview Tests for Blockly.Block
|
||||
* @author fenichel@google.com (Rachel Fenichel)
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var workspace;
|
||||
var mockControl_;
|
||||
|
||||
function defineTestBlocks() {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "stack_block",
|
||||
"message0": "",
|
||||
"previousStatement": null,
|
||||
"nextStatement": null
|
||||
},
|
||||
{
|
||||
"type": "row_block",
|
||||
"message0": "%1",
|
||||
"args0": [
|
||||
{
|
||||
"type": "input_value",
|
||||
"name": "INPUT"
|
||||
}
|
||||
],
|
||||
"output": null
|
||||
}]);
|
||||
}
|
||||
|
||||
function undefineTestBlocks() {
|
||||
delete Blockly.Blocks['stack_block'];
|
||||
delete Blockly.Blocks['row_block'];
|
||||
}
|
||||
|
||||
function blockTest_setUp() {
|
||||
defineTestBlocks();
|
||||
workspace = new Blockly.Workspace();
|
||||
}
|
||||
|
||||
function blockTest_tearDown() {
|
||||
undefineTestBlocks();
|
||||
workspace.dispose();
|
||||
if (mockControl_) {
|
||||
mockControl_.restore();
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
function assertUnpluggedHealFailed(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);
|
||||
// B is the top of its stack.
|
||||
assertNull(blocks.B.getParent());
|
||||
// C is the top of its stack.
|
||||
assertNull(blocks.C.getParent());
|
||||
}
|
||||
|
||||
function setUpRowBlocks() {
|
||||
var blockA = workspace.newBlock('row_block');
|
||||
var blockB = workspace.newBlock('row_block');
|
||||
var blockC = workspace.newBlock('row_block');
|
||||
|
||||
blockA.inputList[0].connection.connect(blockB.outputConnection);
|
||||
blockB.inputList[0].connection.connect(blockC.outputConnection);
|
||||
|
||||
assertEquals(blockB, blockC.getParent());
|
||||
|
||||
return {
|
||||
A: blockA,
|
||||
B: blockB,
|
||||
C: blockC
|
||||
};
|
||||
}
|
||||
|
||||
function setUpStackBlocks() {
|
||||
var blockA = workspace.newBlock('stack_block');
|
||||
var blockB = workspace.newBlock('stack_block');
|
||||
var blockC = workspace.newBlock('stack_block');
|
||||
|
||||
blockA.nextConnection.connect(blockB.previousConnection);
|
||||
blockB.nextConnection.connect(blockC.previousConnection);
|
||||
|
||||
assertEquals(blockB, blockC.getParent());
|
||||
|
||||
return {
|
||||
A: blockA,
|
||||
B: blockB,
|
||||
C: blockC
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function test_block_stack_unplug_noheal() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpStackBlocks();
|
||||
blocks.B.unplug();
|
||||
assertUnpluggedNoheal(blocks);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
function test_block_stack_unplug_heal() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpStackBlocks();
|
||||
blocks.B.unplug(true);
|
||||
assertUnpluggedHealed(blocks);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
function test_block_stack_unplug_heal_bad_checks() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpStackBlocks();
|
||||
|
||||
// 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);
|
||||
|
||||
assertUnpluggedHealFailed(blocks);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
function test_block_row_unplug_noheal() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpRowBlocks();
|
||||
blocks.B.unplug(false);
|
||||
assertUnpluggedNoheal(blocks);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
function test_block_row_unplug_heal() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpRowBlocks();
|
||||
// Each block has only one input, and the types work.
|
||||
blocks.B.unplug(true);
|
||||
assertUnpluggedHealed(blocks);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
function test_block_row_unplug_heal_bad_checks() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpRowBlocks();
|
||||
|
||||
// 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);
|
||||
assertUnpluggedHealFailed(blocks);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
function test_block_row_unplug_multi_inputs_parent() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpRowBlocks();
|
||||
// Add extra input to parent
|
||||
blocks.A.appendValueInput('INPUT').setCheck(null);
|
||||
|
||||
// Parent block has multiple inputs.
|
||||
blocks.B.unplug(true);
|
||||
assertUnpluggedHealed(blocks);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
function test_block_row_unplug_multi_inputs_middle() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpRowBlocks();
|
||||
// Add extra input to middle block
|
||||
blocks.B.appendValueInput('INPUT').setCheck(null);
|
||||
|
||||
// Middle block has multiple inputs.
|
||||
blocks.B.unplug(true);
|
||||
assertUnpluggedHealed(blocks);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
function test_block_row_unplug_multi_inputs_child() {
|
||||
blockTest_setUp();
|
||||
try {
|
||||
var blocks = setUpRowBlocks();
|
||||
// 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);
|
||||
} finally {
|
||||
blockTest_tearDown();
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,6 @@
|
||||
<script src="../mocha/test_helpers.js"></script>
|
||||
|
||||
<script src="test_utilities.js"></script>
|
||||
<script src="block_test.js"></script>
|
||||
<script src="event_test.js"></script>
|
||||
<script src="extensions_test.js"></script>
|
||||
<script src="generator_test.js"></script>
|
||||
|
||||
@@ -49,6 +49,29 @@ suite('Blocks', function() {
|
||||
delete Blockly.Blocks['statement_block'];
|
||||
});
|
||||
|
||||
function createTestBlocks(workspace, isRow) {
|
||||
var blockType = isRow ? 'row_block' : 'stack_block';
|
||||
var blockA = workspace.newBlock(blockType);
|
||||
var blockB = workspace.newBlock(blockType);
|
||||
var blockC = workspace.newBlock(blockType);
|
||||
|
||||
if (isRow) {
|
||||
blockA.inputList[0].connection.connect(blockB.outputConnection);
|
||||
blockB.inputList[0].connection.connect(blockC.outputConnection);
|
||||
} else {
|
||||
blockA.nextConnection.connect(blockB.previousConnection);
|
||||
blockB.nextConnection.connect(blockC.previousConnection);
|
||||
}
|
||||
|
||||
assertEquals(blockB, blockC.getParent());
|
||||
|
||||
return {
|
||||
A: blockA, /* Parent */
|
||||
B: blockB, /* Middle */
|
||||
C: blockC /* Child */
|
||||
};
|
||||
}
|
||||
|
||||
suite('Unplug', function() {
|
||||
function assertUnpluggedNoheal(blocks) {
|
||||
// A has nothing connected to it.
|
||||
@@ -80,20 +103,7 @@ suite('Blocks', function() {
|
||||
|
||||
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
|
||||
};
|
||||
this.blocks = createTestBlocks(this.workspace, true);
|
||||
});
|
||||
|
||||
test('Don\'t heal', function() {
|
||||
@@ -116,21 +126,21 @@ suite('Blocks', function() {
|
||||
blocks.B.unplug(true);
|
||||
assertUnpluggedHealFailed(blocks);
|
||||
});
|
||||
test('A has multiple inputs', function() {
|
||||
test('Parent 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() {
|
||||
test('Middle 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() {
|
||||
test('Child has multiple inputs', function() {
|
||||
var blocks = this.blocks;
|
||||
// Add extra input to child block
|
||||
blocks.C.appendValueInput("INPUT").setCheck(null);
|
||||
@@ -138,7 +148,7 @@ suite('Blocks', function() {
|
||||
blocks.B.unplug(true);
|
||||
assertUnpluggedHealed(blocks);
|
||||
});
|
||||
test('C is Shadow', function() {
|
||||
test('Child is shadow', function() {
|
||||
var blocks = this.blocks;
|
||||
blocks.C.setShadow(true);
|
||||
blocks.B.unplug(true);
|
||||
@@ -149,20 +159,7 @@ suite('Blocks', function() {
|
||||
});
|
||||
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
|
||||
};
|
||||
this.blocks = createTestBlocks(this.workspace, false);
|
||||
});
|
||||
|
||||
test('Don\'t heal', function() {
|
||||
@@ -184,7 +181,7 @@ suite('Blocks', function() {
|
||||
|
||||
assertUnpluggedHealFailed(blocks);
|
||||
});
|
||||
test('C is Shadow', function() {
|
||||
test('Child is shadow', function() {
|
||||
var blocks = this.blocks;
|
||||
blocks.C.setShadow(true);
|
||||
blocks.B.unplug(true);
|
||||
@@ -226,20 +223,7 @@ suite('Blocks', function() {
|
||||
|
||||
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
|
||||
};
|
||||
this.blocks = createTestBlocks(this.workspace, true);
|
||||
});
|
||||
|
||||
test('Don\'t heal', function() {
|
||||
@@ -262,21 +246,21 @@ suite('Blocks', function() {
|
||||
blocks.B.dispose(true);
|
||||
assertDisposedHealFailed(blocks);
|
||||
});
|
||||
test('A has multiple inputs', function() {
|
||||
test('Parent 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() {
|
||||
test('Middle 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() {
|
||||
test('Child has multiple inputs', function() {
|
||||
var blocks = this.blocks;
|
||||
// Add extra input to child block
|
||||
blocks.C.appendValueInput("INPUT").setCheck(null);
|
||||
@@ -284,7 +268,7 @@ suite('Blocks', function() {
|
||||
blocks.B.dispose(true);
|
||||
assertDisposedHealed(blocks);
|
||||
});
|
||||
test('C is Shadow', function() {
|
||||
test('Child is shadow', function() {
|
||||
var blocks = this.blocks;
|
||||
blocks.C.setShadow(true);
|
||||
blocks.B.dispose(true);
|
||||
@@ -295,20 +279,7 @@ suite('Blocks', function() {
|
||||
});
|
||||
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
|
||||
};
|
||||
this.blocks = createTestBlocks(this.workspace, false);
|
||||
});
|
||||
|
||||
test('Don\'t heal', function() {
|
||||
@@ -330,7 +301,7 @@ suite('Blocks', function() {
|
||||
|
||||
assertDisposedHealFailed(blocks);
|
||||
});
|
||||
test('C is Shadow', function() {
|
||||
test('Child is shadow', function() {
|
||||
var blocks = this.blocks;
|
||||
blocks.C.setShadow(true);
|
||||
blocks.B.dispose(true);
|
||||
|
||||
Reference in New Issue
Block a user