jenkins-bot has submitted this change and it was merged.

Change subject: ve.utils: Cover insertIntoArray with unit test
......................................................................


ve.utils: Cover insertIntoArray with unit test

* Cover insertIntoArray() with tests.
* Move insertIntoArray() definition to below batchSplice().
* Write function documentation for insertIntoArray().

Change-Id: Ida065a4ab4252e0eabc2245ecdb8dbb49a56ef41
---
M src/ve.utils.js
M tests/ve.test.js
2 files changed, 38 insertions(+), 21 deletions(-)

Approvals:
  Esanders: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/ve.utils.js b/src/ve.utils.js
index b1ce515..34eb674 100644
--- a/src/ve.utils.js
+++ b/src/ve.utils.js
@@ -148,28 +148,26 @@
  *
  * Includes a replacement for broken implementation of 
Array.prototype.splice() found in Opera 12.
  *
- * @param {Array|ve.dm.BranchNode} arr Object supporting .splice() to remove 
from and insert into. Will be modified
+ * @param {Array|ve.dm.BranchNode} arr Target object (must have `splice` 
method, object will be modified)
  * @param {number} offset Offset in arr to splice at. This may NOT be 
negative, unlike the
- *  'index' parameter in Array#splice
+ *  'index' parameter in Array#splice.
  * @param {number} remove Number of elements to remove at the offset. May be 
zero
- * @param {Array} data Array of items to insert at the offset. May not be 
empty if remove=0
- * @returns {Array} Array of items removed
+ * @param {Array} data Array of items to insert at the offset. Must be 
non-empty if remove=0
+ * @return {Array} Array of items removed
  */
 ve.batchSplice = ( function () {
        var arraySplice;
 
-       // This yields 'true' on Opera 12.15.
-       function isSpliceBroken() {
+       // This yields 'false' on Opera 12.15.
+       function spliceWorks() {
                var n = 256,
                        a = [];
                a[n] = 'a';
-
                a.splice( n + 1, 0, 'b' );
-
-               return a[n] !== 'a';
+               return a[n] === 'a';
        }
 
-       if ( !isSpliceBroken() ) {
+       if ( spliceWorks() ) {
                arraySplice = Array.prototype.splice;
        } else {
                // Standard Array.prototype.splice() function implemented using 
.slice() and .push().
@@ -227,6 +225,20 @@
 }() );
 
 /**
+ * Insert one array into another.
+ *
+ * Shortcut for `ve.batchSplice( dst, offset, 0, src )`.
+ *
+ * @see #batchSplice
+ * @param {Array|ve.dm.BranchNode} arr Target object (must have `splice` 
method)
+ * @param {number} offset Offset in arr where items will be inserted
+ * @param {Array} data Items to insert at offset
+ */
+ve.insertIntoArray = function ( dst, offset, src ) {
+       ve.batchSplice( dst, offset, 0, src );
+};
+
+/**
  * Push one array into another.
  *
  * This is the equivalent of arr.push( d1, d2, d3, ... ) except that arguments 
are
@@ -250,17 +262,6 @@
                index += batchSize;
        }
        return length;
-};
-
-/**
- * Insert one array into another.
- *
- * This just a shortcut for `ve.batchSplice( dst, offset, 0, src )`.
- *
- * @see #batchSplice
- */
-ve.insertIntoArray = function ( dst, offset, src ) {
-       ve.batchSplice( dst, offset, 0, src );
 };
 
 /**
diff --git a/tests/ve.test.js b/tests/ve.test.js
index 79c7e3b..ab9916a 100644
--- a/tests/ve.test.js
+++ b/tests/ve.test.js
@@ -231,6 +231,22 @@
        assert.deepEqual( expected, actual, 'replacing 3 elements with 2100 
elements (array)' );
 } );
 
+QUnit.test( 'insertIntoArray', 3, function ( assert ) {
+       var target;
+
+       target = [ 'a', 'b', 'c' ];
+       ve.insertIntoArray( target, 0, [ 'x', 'y' ] );
+       assert.deepEqual( target, [ 'x', 'y', 'a', 'b', 'c' ], 'insert at 
start' );
+
+       target = [ 'a', 'b', 'c' ];
+       ve.insertIntoArray( target, 2, [ 'x', 'y' ] );
+       assert.deepEqual( target, [ 'a', 'b', 'x', 'y', 'c' ], 'insert into the 
middle' );
+
+       target = [ 'a', 'b', 'c' ];
+       ve.insertIntoArray( target, 10, [ 'x', 'y' ] );
+       assert.deepEqual( target, [ 'a', 'b', 'c', 'x', 'y' ], 'insert beyond 
end' );
+} );
+
 QUnit.test( 'createDocumentFromHtml', function ( assert ) {
        var key, doc, expectedHead, expectedBody,
                cases = [

-- 
To view, visit https://gerrit.wikimedia.org/r/189508
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ida065a4ab4252e0eabc2245ecdb8dbb49a56ef41
Gerrit-PatchSet: 2
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
Gerrit-Reviewer: Esanders <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to