diff --git a/core/connection.js b/core/connection.js index 8914c56f9..19aab3724 100644 --- a/core/connection.js +++ b/core/connection.js @@ -248,10 +248,11 @@ Blockly.Connection.prototype.isConnected = function() { * @param {Blockly.Connection} target Connection to check compatibility with. * @return {number} Blockly.Connection.CAN_CONNECT if the connection is legal, * an error code otherwise. - * @deprecated July 2020 + * @deprecated July 2020. Use the workspace's connectionChecker. */ Blockly.Connection.prototype.canConnectWithReason = function(target) { - // TODO: deprecation warning with date, plus tests. + console.warn('Connection.prototype.canConnectWithReason was deprecated in ' + + 'July 2020 and will be deleted in July 2021.'); return this.getConnectionChecker().canConnectWithReason( this, target, false); }; @@ -262,11 +263,11 @@ Blockly.Connection.prototype.canConnectWithReason = function(target) { * @param {Blockly.Connection} target The connection to check compatibility * with. * @package - * @deprecated July 2020 + * @deprecated July 2020. Use the workspace's connectionChecker. */ Blockly.Connection.prototype.checkConnection = function(target) { -// TODO: Add deprecation warning notices *and* add tests to make sure these -// still work (for any blocks that use them). + console.warn('Connection.prototype.checkConnection was deprecated in ' + + 'July 2020 and will be deleted in July 2021.'); var checker = this.getConnectionChecker(); var reason = checker.canConnectWithReason(this, target, false); if (reason != Blockly.Connection.CAN_CONNECT) { @@ -288,9 +289,11 @@ Blockly.Connection.prototype.getConnectionChecker = function() { * Check if the two connections can be dragged to connect to each other. * @param {!Blockly.Connection} candidate A nearby connection to check. * @return {boolean} True if the connection is allowed, false otherwise. - * @deprecated July 2020 + * @deprecated July 2020. Use the workspace's connectionChecker. */ Blockly.Connection.prototype.isConnectionAllowed = function(candidate) { + console.warn('Connection.prototype.isConnectionAllowed was deprecated in ' + + 'July 2020 and will be deleted in July 2021.'); return this.getConnectionChecker().canConnect(this, candidate, true); }; diff --git a/tests/mocha/connection_checker_test.js b/tests/mocha/connection_checker_test.js new file mode 100644 index 000000000..82528107d --- /dev/null +++ b/tests/mocha/connection_checker_test.js @@ -0,0 +1,264 @@ +/** + * @license + * Copyright 2020 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +suite('Connection checker', function() { + suiteSetup(function() { + this.checker = new Blockly.ConnectionChecker(); + }); + suite('Safety checks', function() { + function assertReasonHelper(checker, one, two, reason) { + chai.assert.equal(checker.canConnectWithReason(one, two), reason); + // Order should not matter. + chai.assert.equal(checker.canConnectWithReason(two, one), reason); + } + + test('Target Null', function() { + var connection = new Blockly.Connection({}, Blockly.INPUT_VALUE); + assertReasonHelper( + this.checker, + connection, + null, + Blockly.Connection.REASON_TARGET_NULL); + }); + test('Target Self', function() { + var block = {workspace: 1}; + var connection1 = new Blockly.Connection(block, Blockly.INPUT_VALUE); + var connection2 = new Blockly.Connection(block, Blockly.OUTPUT_VALUE); + + assertReasonHelper( + this.checker, + connection1, + connection2, + Blockly.Connection.REASON_SELF_CONNECTION); + }); + test('Different Workspaces', function() { + var connection1 = new Blockly.Connection( + {workspace: 1}, Blockly.INPUT_VALUE); + var connection2 = new Blockly.Connection( + {workspace: 2}, Blockly.OUTPUT_VALUE); + + assertReasonHelper( + this.checker, + connection1, + connection2, + Blockly.Connection.REASON_DIFFERENT_WORKSPACES); + }); + suite('Types', function() { + setup(function() { + // We have to declare each separately so that the connections belong + // on different blocks. + var prevBlock = { isShadow: function() {}}; + var nextBlock = { isShadow: function() {}}; + var outBlock = { isShadow: function() {}}; + var inBlock = { isShadow: function() {}}; + this.previous = new Blockly.Connection( + prevBlock, Blockly.PREVIOUS_STATEMENT); + this.next = new Blockly.Connection( + nextBlock, Blockly.NEXT_STATEMENT); + this.output = new Blockly.Connection( + outBlock, Blockly.OUTPUT_VALUE); + this.input = new Blockly.Connection( + inBlock, Blockly.INPUT_VALUE); + }); + test('Previous, Next', function() { + assertReasonHelper( + this.checker, + this.previous, + this.next, + Blockly.Connection.CAN_CONNECT); + }); + test('Previous, Output', function() { + assertReasonHelper( + this.checker, + this.previous, + this.output, + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('Previous, Input', function() { + assertReasonHelper( + this.checker, + this.previous, + this.input, + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('Next, Previous', function() { + assertReasonHelper( + this.checker, + this.next, + this.previous, + Blockly.Connection.CAN_CONNECT); + }); + test('Next, Output', function() { + assertReasonHelper( + this.checker, + this.next, + this.output, + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('Next, Input', function() { + assertReasonHelper( + this.checker, + this.next, + this.input, + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('Output, Previous', function() { + assertReasonHelper( + this.checker, + this.previous, + this.output, + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('Output, Next', function() { + assertReasonHelper( + this.checker, + this.output, + this.next, + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('Output, Input', function() { + assertReasonHelper( + this.checker, + this.output, + this.input, + Blockly.Connection.CAN_CONNECT); + }); + test('Input, Previous', function() { + assertReasonHelper( + this.checker, + this.previous, + this.input, + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('Input, Next', function() { + assertReasonHelper( + this.checker, + this.input, + this.next, + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('Input, Output', function() { + assertReasonHelper( + this.checker, + this.input, + this.output, + Blockly.Connection.CAN_CONNECT); + }); + }); + suite('Shadows', function() { + test('Previous Shadow', function() { + var prevBlock = { isShadow: function() { return true; }}; + var nextBlock = { isShadow: function() { return false; }}; + var prev = new Blockly.Connection(prevBlock, Blockly.PREVIOUS_STATEMENT); + var next = new Blockly.Connection(nextBlock, Blockly.NEXT_STATEMENT); + + assertReasonHelper( + this.checker, + prev, + next, + Blockly.Connection.CAN_CONNECT); + }); + test('Next Shadow', function() { + var prevBlock = { isShadow: function() { return false; }}; + var nextBlock = { isShadow: function() { return true; }}; + var prev = new Blockly.Connection(prevBlock, Blockly.PREVIOUS_STATEMENT); + var next = new Blockly.Connection(nextBlock, Blockly.NEXT_STATEMENT); + + assertReasonHelper( + this.checker, + prev, + next, + Blockly.Connection.REASON_SHADOW_PARENT); + }); + test('Prev and Next Shadow', function() { + var prevBlock = { isShadow: function() { return true; }}; + var nextBlock = { isShadow: function() { return true; }}; + var prev = new Blockly.Connection(prevBlock, Blockly.PREVIOUS_STATEMENT); + var next = new Blockly.Connection(nextBlock, Blockly.NEXT_STATEMENT); + + assertReasonHelper( + this.checker, + prev, + next, + Blockly.Connection.CAN_CONNECT); + }); + test('Output Shadow', function() { + var outBlock = { isShadow: function() { return true; }}; + var inBlock = { isShadow: function() { return false; }}; + var outCon = new Blockly.Connection(outBlock, Blockly.OUTPUT_VALUE); + var inCon = new Blockly.Connection(inBlock, Blockly.INPUT_VALUE); + + assertReasonHelper( + this.checker, + outCon, + inCon, + Blockly.Connection.CAN_CONNECT); + }); + test('Input Shadow', function() { + var outBlock = { isShadow: function() { return false; }}; + var inBlock = { isShadow: function() { return true; }}; + var outCon = new Blockly.Connection(outBlock, Blockly.OUTPUT_VALUE); + var inCon = new Blockly.Connection(inBlock, Blockly.INPUT_VALUE); + + assertReasonHelper( + this.checker, + outCon, + inCon, + Blockly.Connection.REASON_SHADOW_PARENT); + }); + test('Output and Input Shadow', function() { + var outBlock = { isShadow: function() { return true; }}; + var inBlock = { isShadow: function() { return true; }}; + var outCon = new Blockly.Connection(outBlock, Blockly.OUTPUT_VALUE); + var inCon = new Blockly.Connection(inBlock, Blockly.INPUT_VALUE); + + assertReasonHelper( + this.checker, + outCon, + inCon, + Blockly.Connection.CAN_CONNECT); + }); + }); + }); + suite('Check Types', function() { + setup(function() { + this.con1 = new Blockly.Connection({}, Blockly.PREVIOUS_STATEMENT); + this.con2 = new Blockly.Connection({}, Blockly.NEXT_STATEMENT); + }); + function assertCheckTypes(checker, one, two) { + chai.assert.isTrue(checker.doTypeChecks(one, two)); + // Order should not matter. + chai.assert.isTrue(checker.doTypeChecks(one, two)); + } + test('No Types', function() { + assertCheckTypes(this.checker, this.con1, this.con2); + }); + test('Same Type', function() { + this.con1.setCheck('type1'); + this.con2.setCheck('type1'); + assertCheckTypes(this.checker, this.con1, this.con2); + }); + test('Same Types', function() { + this.con1.setCheck(['type1', 'type2']); + this.con2.setCheck(['type1', 'type2']); + assertCheckTypes(this.checker, this.con1, this.con2); + }); + test('Single Same Type', function() { + this.con1.setCheck(['type1', 'type2']); + this.con2.setCheck(['type1', 'type3']); + assertCheckTypes(this.checker, this.con1, this.con2); + }); + test('One Typed, One Promiscuous', function() { + this.con1.setCheck('type1'); + assertCheckTypes(this.checker, this.con1, this.con2); + }); + test('No Compatible Types', function() { + this.con1.setCheck('type1'); + this.con2.setCheck('type2'); + chai.assert.isFalse(this.checker.doTypeChecks(this.con1, this.con2)); + }); + }); +}); diff --git a/tests/mocha/connection_test.js b/tests/mocha/connection_test.js index 4f058126b..a55fcc34e 100644 --- a/tests/mocha/connection_test.js +++ b/tests/mocha/connection_test.js @@ -4,261 +4,36 @@ * SPDX-License-Identifier: Apache-2.0 */ -suite('Connection type checker', function() { +suite('Connection', function() { suiteSetup(function() { - this.checker = new Blockly.ConnectionChecker(); + this.workspace = = { + connectionChecker: new Blockly.ConnectionChecker() + }; + this.createConnection = function(type) { + var connection = new Blockly.Connection({workspace: this.workspace}, type); + return connection; + }; }); - suite('Safety checks', function() { - function assertReasonHelper(checker, one, two, reason) { - chai.assert.equal(checker.canConnectWithReason(one, two), reason); - // Order should not matter. - chai.assert.equal(checker.canConnectWithReason(two, one), reason); - } - - test('Target Null', function() { - var connection = new Blockly.Connection({}, Blockly.INPUT_VALUE); - assertReasonHelper( - this.checker, - connection, - null, - Blockly.Connection.REASON_TARGET_NULL); - }); - test('Target Self', function() { - var block = {workspace: 1}; - var connection1 = new Blockly.Connection(block, Blockly.INPUT_VALUE); - var connection2 = new Blockly.Connection(block, Blockly.OUTPUT_VALUE); - - assertReasonHelper( - this.checker, - connection1, - connection2, - Blockly.Connection.REASON_SELF_CONNECTION); - }); - test('Different Workspaces', function() { - var connection1 = new Blockly.Connection( - {workspace: 1}, Blockly.INPUT_VALUE); - var connection2 = new Blockly.Connection( - {workspace: 2}, Blockly.OUTPUT_VALUE); - - assertReasonHelper( - this.checker, - connection1, - connection2, - Blockly.Connection.REASON_DIFFERENT_WORKSPACES); - }); - suite('Types', function() { - setup(function() { - // We have to declare each separately so that the connections belong - // on different blocks. - var prevBlock = { isShadow: function() {}}; - var nextBlock = { isShadow: function() {}}; - var outBlock = { isShadow: function() {}}; - var inBlock = { isShadow: function() {}}; - this.previous = new Blockly.Connection( - prevBlock, Blockly.PREVIOUS_STATEMENT); - this.next = new Blockly.Connection( - nextBlock, Blockly.NEXT_STATEMENT); - this.output = new Blockly.Connection( - outBlock, Blockly.OUTPUT_VALUE); - this.input = new Blockly.Connection( - inBlock, Blockly.INPUT_VALUE); - }); - test('Previous, Next', function() { - assertReasonHelper( - this.checker, - this.previous, - this.next, - Blockly.Connection.CAN_CONNECT); - }); - test('Previous, Output', function() { - assertReasonHelper( - this.checker, - this.previous, - this.output, - Blockly.Connection.REASON_WRONG_TYPE); - }); - test('Previous, Input', function() { - assertReasonHelper( - this.checker, - this.previous, - this.input, - Blockly.Connection.REASON_WRONG_TYPE); - }); - test('Next, Previous', function() { - assertReasonHelper( - this.checker, - this.next, - this.previous, - Blockly.Connection.CAN_CONNECT); - }); - test('Next, Output', function() { - assertReasonHelper( - this.checker, - this.next, - this.output, - Blockly.Connection.REASON_WRONG_TYPE); - }); - test('Next, Input', function() { - assertReasonHelper( - this.checker, - this.next, - this.input, - Blockly.Connection.REASON_WRONG_TYPE); - }); - test('Output, Previous', function() { - assertReasonHelper( - this.checker, - this.previous, - this.output, - Blockly.Connection.REASON_WRONG_TYPE); - }); - test('Output, Next', function() { - assertReasonHelper( - this.checker, - this.output, - this.next, - Blockly.Connection.REASON_WRONG_TYPE); - }); - test('Output, Input', function() { - assertReasonHelper( - this.checker, - this.output, - this.input, - Blockly.Connection.CAN_CONNECT); - }); - test('Input, Previous', function() { - assertReasonHelper( - this.checker, - this.previous, - this.input, - Blockly.Connection.REASON_WRONG_TYPE); - }); - test('Input, Next', function() { - assertReasonHelper( - this.checker, - this.input, - this.next, - Blockly.Connection.REASON_WRONG_TYPE); - }); - test('Input, Output', function() { - assertReasonHelper( - this.checker, - this.input, - this.output, - Blockly.Connection.CAN_CONNECT); - }); - }); - suite('Shadows', function() { - test('Previous Shadow', function() { - var prevBlock = { isShadow: function() { return true; }}; - var nextBlock = { isShadow: function() { return false; }}; - var prev = new Blockly.Connection(prevBlock, Blockly.PREVIOUS_STATEMENT); - var next = new Blockly.Connection(nextBlock, Blockly.NEXT_STATEMENT); - - assertReasonHelper( - this.checker, - prev, - next, - Blockly.Connection.CAN_CONNECT); - }); - test('Next Shadow', function() { - var prevBlock = { isShadow: function() { return false; }}; - var nextBlock = { isShadow: function() { return true; }}; - var prev = new Blockly.Connection(prevBlock, Blockly.PREVIOUS_STATEMENT); - var next = new Blockly.Connection(nextBlock, Blockly.NEXT_STATEMENT); - - assertReasonHelper( - this.checker, - prev, - next, - Blockly.Connection.REASON_SHADOW_PARENT); - }); - test('Prev and Next Shadow', function() { - var prevBlock = { isShadow: function() { return true; }}; - var nextBlock = { isShadow: function() { return true; }}; - var prev = new Blockly.Connection(prevBlock, Blockly.PREVIOUS_STATEMENT); - var next = new Blockly.Connection(nextBlock, Blockly.NEXT_STATEMENT); - - assertReasonHelper( - this.checker, - prev, - next, - Blockly.Connection.CAN_CONNECT); - }); - test('Output Shadow', function() { - var outBlock = { isShadow: function() { return true; }}; - var inBlock = { isShadow: function() { return false; }}; - var outCon = new Blockly.Connection(outBlock, Blockly.OUTPUT_VALUE); - var inCon = new Blockly.Connection(inBlock, Blockly.INPUT_VALUE); - - assertReasonHelper( - this.checker, - outCon, - inCon, - Blockly.Connection.CAN_CONNECT); - }); - test('Input Shadow', function() { - var outBlock = { isShadow: function() { return false; }}; - var inBlock = { isShadow: function() { return true; }}; - var outCon = new Blockly.Connection(outBlock, Blockly.OUTPUT_VALUE); - var inCon = new Blockly.Connection(inBlock, Blockly.INPUT_VALUE); - - assertReasonHelper( - this.checker, - outCon, - inCon, - Blockly.Connection.REASON_SHADOW_PARENT); - }); - test('Output and Input Shadow', function() { - var outBlock = { isShadow: function() { return true; }}; - var inBlock = { isShadow: function() { return true; }}; - var outCon = new Blockly.Connection(outBlock, Blockly.OUTPUT_VALUE); - var inCon = new Blockly.Connection(inBlock, Blockly.INPUT_VALUE); - - assertReasonHelper( - this.checker, - outCon, - inCon, - Blockly.Connection.CAN_CONNECT); - }); - }); + test('canConnectWithReason passes', function() { + var conn1 = this.createConnection(Blockly.PREVIOUS_STATEMENT); + var conn2 = this.createConnection(Blockly.NEXT_STATEMENT); + chai.assert.equal(conn1.canConnectWithReason(conn2), + Blockly.Connection.CAN_CONNECT); }); - suite('Check Types', function() { - setup(function() { - this.con1 = new Blockly.Connection({}, Blockly.PREVIOUS_STATEMENT); - this.con2 = new Blockly.Connection({}, Blockly.NEXT_STATEMENT); - }); - function assertCheckTypes(checker, one, two) { - chai.assert.isTrue(checker.doTypeChecks(one, two)); - // Order should not matter. - chai.assert.isTrue(checker.doTypeChecks(one, two)); - } - test('No Types', function() { - assertCheckTypes(this.checker, this.con1, this.con2); - }); - test('Same Type', function() { - this.con1.setCheck('type1'); - this.con2.setCheck('type1'); - assertCheckTypes(this.checker, this.con1, this.con2); - }); - test('Same Types', function() { - this.con1.setCheck(['type1', 'type2']); - this.con2.setCheck(['type1', 'type2']); - assertCheckTypes(this.checker, this.con1, this.con2); - }); - test('Single Same Type', function() { - this.con1.setCheck(['type1', 'type2']); - this.con2.setCheck(['type1', 'type3']); - assertCheckTypes(this.checker, this.con1, this.con2); - }); - test('One Typed, One Promiscuous', function() { - this.con1.setCheck('type1'); - assertCheckTypes(this.checker, this.con1, this.con2); - }); - test('No Compatible Types', function() { - this.con1.setCheck('type1'); - this.con2.setCheck('type2'); - chai.assert.isFalse(this.checker.doTypeChecks(this.con1, this.con2)); - }); + test('canConnectWithReason fails', function() { + var conn1 = this.createConnection(Blockly.PREVIOUS_STATEMENT); + var conn2 = this.createConnection(Blockly.OUTPUT_VALUE); + chai.assert.equal(conn1.canConnectWithReason(conn2), + Blockly.Connection.REASON_WRONG_TYPE); + }); + test('checkConnection passes', function() { + var conn1 = this.createConnection(Blockly.PREVIOUS_STATEMENT); + var conn2 = this.createConnection(Blockly.NEXT_STATEMENT); + chai.assert.doesNotThrow(conn1.checkConnection(conn2)); + }); + test('checkConnection fails', function() { + var conn1 = this.createConnection(Blockly.PREVIOUS_STATEMENT); + var conn2 = this.createConnection(Blockly.OUTPUT_VALUE); + chai.assert.throws(conn1.checkConnection(conn2)); }); }); diff --git a/tests/mocha/index.html b/tests/mocha/index.html index 9c81f3889..e4391e0c7 100644 --- a/tests/mocha/index.html +++ b/tests/mocha/index.html @@ -40,6 +40,7 @@ +