Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection tests and js style fixes #90

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/block_render_svg_horizontal.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ goog.provide('Blockly.BlockSvg.render');

goog.require('Blockly.BlockSvg');


// UI constants for rendering blocks.
/**
* Horizontal space between elements.
Expand Down
27 changes: 20 additions & 7 deletions core/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ Blockly.Connection.prototype.isSuperior = function() {
/**
* Checks whether the current connection can connect with the target
* connection.
* @param (Blockly.Connection) target Connection to check compatibility with.
* @return {int} Blockly.Connection.CAN_CONNECT if the connection is legal,
* @param {Blockly.Connection} target Connection to check compatibility with.
* @return {number} Blockly.Connection.CAN_CONNECT if the connection is legal,
* an error code otherwise.
* @private
*/
Expand All @@ -195,7 +195,7 @@ Blockly.Connection.prototype.canConnectWithReason_ = function(target) {
return Blockly.Connection.REASON_CHECKS_FAILED;
}
return Blockly.Connection.CAN_CONNECT;
}
};

/**
* Checks whether the current connection and target connection are compatible
Expand All @@ -218,10 +218,12 @@ Blockly.Connection.prototype.checkConnection_ = function(target) {
throw 'Source connection already connected.';
case Blockly.Connection.REASON_TARGET_NULL:
throw 'Target connection is null.';
case Blockly.Connection.REASON_CHECKS_FAILED:
throw 'Connection checks failed.';
default:
throw 'Unknown connection failure: this should never happen!';
}
}
};

/**
* Connect this connection to another connection.
Expand Down Expand Up @@ -311,8 +313,7 @@ Blockly.Connection.prototype.connect = function(otherConnection) {
}

// Establish the connections.
this.targetConnection = otherConnection;
otherConnection.targetConnection = this;
Blockly.Connection.connectReciprocally(this, otherConnection);

// Demote the inferior block so that one is a child of the superior one.
childBlock.setParent(parentBlock);
Expand All @@ -337,6 +338,19 @@ Blockly.Connection.prototype.connect = function(otherConnection) {
}
};

/**
* Update two connections to target each other.
* @param {Blockly.Connection} first The first connection to update.
* @param {Blockly.Connection} second The second conneciton to update.
*/
Blockly.Connection.connectReciprocally = function(first, second) {
if (!first || !second) {
throw 'Cannot connect null connections.';
}
first.targetConnection = second;
second.targetConnection = first;
};

/**
* Does the given block have one and only one connection point that will accept
* an orphaned block?
Expand Down Expand Up @@ -366,7 +380,6 @@ Blockly.Connection.singleConnection_ = function(block, orphanBlock) {
* connections that will accept the orphaned block. If at any point there
* are zero or multiple eligible connections, returns null. Otherwise
* returns the only input on the last block in the chain.
* <p/>
* Terminates early for shadow blocks.
* @param {!Blockly.Block} startBlack The block on which to start the search.
* @param {!Blockly.Block} orphanBlock The block that is looking for a home.
Expand Down
95 changes: 95 additions & 0 deletions tests/jsunit/connection_db_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @license
* Blockly Tests
*
* Copyright 2015 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

function verify_DB_(msg, expected, db) {
var equal = (expected.length == db.length);
if (equal) {
for (var x = 0; x < expected.length; x++) {
if (expected[x] != db[x]) {
equal = false;
break;
}
}
}
if (equal) {
assertTrue(msg, true);
} else {
assertEquals(msg, expected, db);
}
}

function test_DB_addConnection() {
var db = new Blockly.ConnectionDB();
var o2 = {y_: 2, sourceBlock_: {}};
db.addConnection_(o2);
verify_DB_('Adding connection #2', [o2], db);

var o4 = {y_: 4, sourceBlock_: {}};
db.addConnection_(o4);
verify_DB_('Adding connection #4', [o2, o4], db);

var o1 = {y_: 1, sourceBlock_: {}};
db.addConnection_(o1);
verify_DB_('Adding connection #1', [o1, o2, o4], db);

var o3a = {y_: 3, sourceBlock_: {}};
db.addConnection_(o3a);
verify_DB_('Adding connection #3a', [o1, o2, o3a, o4], db);

var o3b = {y_: 3, sourceBlock_: {}};
db.addConnection_(o3b);
verify_DB_('Adding connection #3b', [o1, o2, o3b, o3a, o4], db);
}

function test_DB_removeConnection() {
var db = new Blockly.ConnectionDB();
var o1 = {y_: 1, sourceBlock_: {}};
var o2 = {y_: 2, sourceBlock_: {}};
var o3a = {y_: 3, sourceBlock_: {}};
var o3b = {y_: 3, sourceBlock_: {}};
var o3c = {y_: 3, sourceBlock_: {}};
var o4 = {y_: 4, sourceBlock_: {}};
db.addConnection_(o1);
db.addConnection_(o2);
db.addConnection_(o3c);
db.addConnection_(o3b);
db.addConnection_(o3a);
db.addConnection_(o4);
verify_DB_('Adding connections 1-4', [o1, o2, o3a, o3b, o3c, o4], db);

db.removeConnection_(o2);
verify_DB_('Removing connection #2', [o1, o3a, o3b, o3c, o4], db);

db.removeConnection_(o4);
verify_DB_('Removing connection #4', [o1, o3a, o3b, o3c], db);

db.removeConnection_(o1);
verify_DB_('Removing connection #1', [o3a, o3b, o3c], db);

db.removeConnection_(o3a);
verify_DB_('Removing connection #3a', [o3b, o3c], db);

db.removeConnection_(o3c);
verify_DB_('Removing connection #3c', [o3b], db);

db.removeConnection_(o3b);
verify_DB_('Removing connection #3b', [], db);
}
Loading