mirror of
https://github.com/google/blockly.git
synced 2026-01-10 10:27:08 +01:00
Add unit tests and code cleanup to utils (#1993)
* Add unit tests for new util functions * Utils functions shouldn’t be package, not private. * Use existing utility function for insertAfter
This commit is contained in:
@@ -214,7 +214,7 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface, workspac
|
||||
if (!options.hasCategories && options.languageTree) {
|
||||
// Add flyout as an <svg> that is a sibling of the workspace svg.
|
||||
var flyout = mainWorkspace.addFlyout_('svg');
|
||||
Blockly.utils.insertAfter_(flyout, svg);
|
||||
Blockly.utils.insertAfter(flyout, svg);
|
||||
}
|
||||
|
||||
// A null translation will also apply the correct initial scale.
|
||||
|
||||
@@ -57,7 +57,7 @@ Blockly.ScrollbarPair = function(workspace) {
|
||||
'class': 'blocklyScrollbarBackground'
|
||||
},
|
||||
null);
|
||||
Blockly.utils.insertAfter_(this.corner_, workspace.getBubbleCanvas());
|
||||
Blockly.utils.insertAfter(this.corner_, workspace.getBubbleCanvas());
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -625,7 +625,7 @@ Blockly.Scrollbar.prototype.createDom_ = function(opt_class) {
|
||||
'ry': radius
|
||||
},
|
||||
this.svgGroup_);
|
||||
Blockly.utils.insertAfter_(this.outerSvg_, this.workspace_.getParentSvg());
|
||||
Blockly.utils.insertAfter(this.outerSvg_, this.workspace_.getParentSvg());
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -191,9 +191,8 @@ Blockly.Toolbox.prototype.init = function() {
|
||||
this.flyout_ = new Blockly.VerticalFlyout(workspaceOptions);
|
||||
}
|
||||
// Insert the flyout after the workspace.
|
||||
var workspaceSvg = this.workspace_.getParentSvg();
|
||||
workspaceSvg.parentNode.insertBefore(this.flyout_.createDom('svg'),
|
||||
workspaceSvg.nextSibling);
|
||||
Blockly.utils.insertAfter(this.flyout_.createDom('svg'),
|
||||
this.workspace_.getParentSvg());
|
||||
this.flyout_.init(workspace);
|
||||
|
||||
this.config_['cleardotPath'] = workspace.options.pathToMedia + '1x1.gif';
|
||||
|
||||
@@ -831,9 +831,9 @@ Blockly.utils.is3dSupported = function() {
|
||||
* Contrast with node.insertBefore function.
|
||||
* @param {!Element} newNode New element to insert.
|
||||
* @param {!Element} refNode Existing element to precede new node.
|
||||
* @private
|
||||
* @package
|
||||
*/
|
||||
Blockly.utils.insertAfter_ = function(newNode, refNode) {
|
||||
Blockly.utils.insertAfter = function(newNode, refNode) {
|
||||
var siblingNode = refNode.nextSibling;
|
||||
var parentNode = refNode.parentNode;
|
||||
if (!parentNode) {
|
||||
@@ -906,6 +906,7 @@ Blockly.utils.getViewportBBox = function() {
|
||||
* @param {string} str The string to check.
|
||||
* @param {string} prefix A string to look for at the start of `str`.
|
||||
* @return {boolean} True if `str` begins with `prefix`.
|
||||
* @package
|
||||
*/
|
||||
Blockly.utils.startsWith = function(str, prefix) {
|
||||
return str.lastIndexOf(prefix, 0) == 0;
|
||||
@@ -917,6 +918,7 @@ Blockly.utils.startsWith = function(str, prefix) {
|
||||
* value.
|
||||
* @param {*} obj Object to remove.
|
||||
* @return {boolean} True if an element was removed.
|
||||
* @package
|
||||
*/
|
||||
Blockly.utils.arrayRemove = function(arr, obj) {
|
||||
var i = arr.indexOf(obj);
|
||||
@@ -932,6 +934,7 @@ Blockly.utils.arrayRemove = function(arr, obj) {
|
||||
* Copied from Closure's goog.math.toRadians.
|
||||
* @param {number} angleDegrees Angle in degrees.
|
||||
* @return {number} Angle in radians.
|
||||
* @package
|
||||
*/
|
||||
Blockly.utils.toRadians = function(angleDegrees) {
|
||||
return angleDegrees * Math.PI / 180;
|
||||
@@ -942,6 +945,7 @@ Blockly.utils.toRadians = function(angleDegrees) {
|
||||
* Copied from Closure's goog.math.toDegrees.
|
||||
* @param {number} angleRadians Angle in radians.
|
||||
* @return {number} Angle in degrees.
|
||||
* @package
|
||||
*/
|
||||
Blockly.utils.toDegrees = function(angleRadians) {
|
||||
return angleRadians * 180 / Math.PI;
|
||||
@@ -952,6 +956,7 @@ Blockly.utils.toDegrees = function(angleRadians) {
|
||||
* @param {!Node} parent The node that should contain the other node.
|
||||
* @param {!Node} descendant The node to test presence of.
|
||||
* @return {boolean} Whether the parent node contains the descendant node.
|
||||
* @package
|
||||
*/
|
||||
Blockly.utils.containsNode = function(parent, descendant) {
|
||||
return !!(parent.compareDocumentPosition(descendant) &
|
||||
|
||||
@@ -150,13 +150,13 @@ Blockly.WorkspaceDragSurfaceSvg.prototype.clearAndHide = function(newSurface) {
|
||||
// If there is a previous sibling, put the blockCanvas back right afterwards,
|
||||
// otherwise insert it as the first child node in newSurface.
|
||||
if (this.previousSibling_ != null) {
|
||||
Blockly.utils.insertAfter_(blockCanvas, this.previousSibling_);
|
||||
Blockly.utils.insertAfter(blockCanvas, this.previousSibling_);
|
||||
} else {
|
||||
newSurface.insertBefore(blockCanvas, newSurface.firstChild);
|
||||
}
|
||||
|
||||
// Reattach the bubble canvas after the blockCanvas.
|
||||
Blockly.utils.insertAfter_(bubbleCanvas, blockCanvas);
|
||||
Blockly.utils.insertAfter(bubbleCanvas, blockCanvas);
|
||||
// Hide the drag surface.
|
||||
this.SVG_.style.display = 'none';
|
||||
if (this.SVG_.childNodes.length) {
|
||||
|
||||
@@ -232,3 +232,42 @@ function test_replaceMessageReferences() {
|
||||
resultString = Blockly.utils.replaceMessageReferences('before %{bky_string_ref_with_subref} after');
|
||||
assertEquals('Message ref and subref dereferenced.', 'before test subref string after', resultString);
|
||||
}
|
||||
|
||||
function test_startsWith() {
|
||||
assertEquals('Does not start with', false, Blockly.utils.startsWith('123', '2'));
|
||||
assertEquals('Start with', true, Blockly.utils.startsWith('123', '12'));
|
||||
assertEquals('Start with empty string 1', true, Blockly.utils.startsWith('123', ''));
|
||||
assertEquals('Start with empty string 2', true, Blockly.utils.startsWith('', ''));
|
||||
}
|
||||
|
||||
function test_arrayRemove() {
|
||||
var arr = [1, 2, 3, 2];
|
||||
assertEquals('Remove Not found', false, Blockly.utils.arrayRemove(arr, 0));
|
||||
assertEquals('Remove Not found result', '1,2,3,2', arr.join(','));
|
||||
assertEquals('Remove item', true, Blockly.utils.arrayRemove(arr, 2));
|
||||
assertEquals('Remove item result', '1,3,2', arr.join(','));
|
||||
assertEquals('Remove item again', true, Blockly.utils.arrayRemove(arr, 2));
|
||||
assertEquals('Remove item again result', '1,3', arr.join(','));
|
||||
}
|
||||
|
||||
function test_toRadians() {
|
||||
var quarter = Math.PI / 2;
|
||||
assertEquals('-90', -quarter, Blockly.utils.toRadians(-90));
|
||||
assertEquals('0', 0, Blockly.utils.toRadians(0));
|
||||
assertEquals('90', quarter, Blockly.utils.toRadians(90));
|
||||
assertEquals('180', 2 * quarter, Blockly.utils.toRadians(180));
|
||||
assertEquals('270', 3 * quarter, Blockly.utils.toRadians(270));
|
||||
assertEquals('360', 4 * quarter, Blockly.utils.toRadians(360));
|
||||
assertEquals('450', 5 * quarter, Blockly.utils.toRadians(360 + 90));
|
||||
}
|
||||
|
||||
function test_toDegrees() {
|
||||
var quarter = Math.PI / 2;
|
||||
assertEquals('-90', -90, Blockly.utils.toDegrees(-quarter));
|
||||
assertEquals('0', 0, Blockly.utils.toDegrees(0));
|
||||
assertEquals('90', 90, Blockly.utils.toDegrees(quarter));
|
||||
assertEquals('180', 180, Blockly.utils.toDegrees(2 * quarter));
|
||||
assertEquals('270', 270, Blockly.utils.toDegrees(3 * quarter));
|
||||
assertEquals('360', 360, Blockly.utils.toDegrees(4 * quarter));
|
||||
assertEquals('450', 360 + 90, Blockly.utils.toDegrees(5 * quarter));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user