Merge pull request #1158 from rachel-fenichel/bugfix/1127

Hack to fix 1127 while we consider strict connection types
This commit is contained in:
Rachel Fenichel 2017-10-20 08:31:43 -07:00 committed by GitHub
commit 63ab0baf3b
2 changed files with 71 additions and 0 deletions

View file

@ -63,6 +63,8 @@ Blockly.Connection.REASON_TARGET_NULL = 3;
Blockly.Connection.REASON_CHECKS_FAILED = 4;
Blockly.Connection.REASON_DIFFERENT_WORKSPACES = 5;
Blockly.Connection.REASON_SHADOW_PARENT = 6;
// Fixes #1127, but may be the wrong solution.
Blockly.Connection.REASON_CUSTOM_PROCEDURE = 7;
/**
* Connection this connection connects to. Null if not connected.
@ -292,9 +294,11 @@ Blockly.Connection.prototype.canConnectWithReason_ = function(target) {
if (this.isSuperior()) {
var blockA = this.sourceBlock_;
var blockB = target.getSourceBlock();
var superiorConn = this;
} else {
var blockB = this.sourceBlock_;
var blockA = target.getSourceBlock();
var superiorConn = target;
}
if (blockA && blockA == blockB) {
return Blockly.Connection.REASON_SELF_CONNECTION;
@ -306,6 +310,12 @@ Blockly.Connection.prototype.canConnectWithReason_ = function(target) {
return Blockly.Connection.REASON_CHECKS_FAILED;
} else if (blockA.isShadow() && !blockB.isShadow()) {
return Blockly.Connection.REASON_SHADOW_PARENT;
} else if (blockA.type == 'procedures_defnoreturn' &&
blockB.type != 'procedures_callnoreturn_internal' &&
superiorConn == blockA.getInput('custom_block').connection ) {
// Hack to fix #1127: Fail attempts to connect to the custom_block input
// on a defnoreturn block, unless the connecting block is a specific type.
return Blockly.Connection.REASON_CUSTOM_PROCEDURE;
}
return Blockly.Connection.CAN_CONNECT;
};
@ -336,6 +346,8 @@ Blockly.Connection.prototype.checkConnection_ = function(target) {
throw msg;
case Blockly.Connection.REASON_SHADOW_PARENT:
throw 'Connecting non-shadow to shadow block.';
case Blockly.Connection.REASON_CUSTOM_PROCEDURE:
throw 'Trying to replace a shadow on a custom procedure definition.';
default:
throw 'Unknown connection failure: this should never happen!';
}

View file

@ -343,3 +343,62 @@ function testCheckConnection_Okay() {
connectionTest_tearDown();
}
function test_canConnectWithReason_Procedures_WrongBlockType() {
var sharedWorkspace = {};
var one = helper_createConnection(0, 0, Blockly.NEXT_STATEMENT);
one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
one.sourceBlock_.type = 'procedures_defnoreturn';
// Make one be the connection on its source block's input.
one.sourceBlock_.getInput = function() {
return {
connection: one
};
};
var two = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
// Fail because two's source block is the wrong type.
two.sourceBlock_.type = 'wrong_type';
assertEquals(Blockly.Connection.REASON_CUSTOM_PROCEDURE,
one.canConnectWithReason_(two));
}
function test_canConnectWithReason_Procedures_Pass() {
var sharedWorkspace = {};
var one = helper_createConnection(0, 0, Blockly.NEXT_STATEMENT);
one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
one.sourceBlock_.type = 'procedures_defnoreturn';
// Make one be the connection on its source block's input.
one.sourceBlock_.getInput = function() {
return {
connection: one
};
};
var two = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
two.sourceBlock_.type = 'procedures_callnoreturn_internal';
assertEquals(Blockly.Connection.CAN_CONNECT,
one.canConnectWithReason_(two));
}
function test_canConnectWithReason_Procedures_NextConnection() {
var sharedWorkspace = {};
var one = helper_createConnection(0, 0, Blockly.NEXT_STATEMENT);
one.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
one.sourceBlock_.type = 'procedures_defnoreturn';
// One is the next connection, not an input connection
one.sourceBlock_.nextConnection = one;
one.sourceBlock_.getInput = function() {
return {
connection: null
};
};
var two = helper_createConnection(0, 0, Blockly.PREVIOUS_STATEMENT);
two.sourceBlock_ = helper_makeSourceBlock(sharedWorkspace);
// It should be okay, even if two's source block has the wrong type, because
// it's not trying to connect to the input.
two.sourceBlock_.type = 'wrong_type';
assertEquals(Blockly.Connection.CAN_CONNECT,
one.canConnectWithReason_(two));
}