Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / core / test / unit / spec / lang / Array.js
index 6318656..0e0e14a 100644 (file)
@@ -1,3 +1,17 @@
+/*
+
+This file is part of Ext JS 4
+
+Copyright (c) 2011 Sencha Inc
+
+Contact:  http://www.sencha.com/contact
+
+GNU General Public License Usage
+This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
+
+If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
+
+*/
 describe("Ext.Array", function() {
     var array;
 
@@ -442,13 +456,13 @@ describe("Ext.Array", function() {
 
         it("should add items if the filter function returns true", function(){
             expect(filter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(val){
-                return val % 2 == 0;
+                return val % 2 === 0;
             })).toEqual([2, 4, 6, 8, 10]);
         });
 
         it("should add items if the filter function returns a truthy value", function(){
             expect(filter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], function(val){
-                if (val % 2 == 0) {
+                if (val % 2 === 0) {
                     return 1;
                 }
             })).toEqual([2, 4, 6, 8, 10]);
@@ -626,7 +640,7 @@ describe("Ext.Array", function() {
             it("should iterate arguments", function(){
                 var test, values = [], fn = function(){
                     test = each(arguments, function(val){
-                        values.push(val)
+                        values.push(val);
                     });
                 };
                 fn(1, 2, 3);
@@ -925,7 +939,7 @@ describe("Ext.Array", function() {
             });
 
             it("with comparisonFn", function() {
-                expect(Ext.Array.min([1,2,3,4,5,6], function(a, b) { return a < b ? 1 : -1 })).toEqual(6);
+                expect(Ext.Array.min([1,2,3,4,5,6], function(a, b) { return a < b ? 1 : -1; })).toEqual(6);
             });
         });
     });
@@ -937,7 +951,7 @@ describe("Ext.Array", function() {
             });
 
             it("with comparisonFn", function() {
-                expect(Ext.Array.max([1,2,3,4,5,6], function(a, b) { return a < b ? 1 : -1 })).toEqual(1);
+                expect(Ext.Array.max([1,2,3,4,5,6], function(a, b) { return a < b ? 1 : -1; })).toEqual(1);
             });
         });
     });
@@ -953,4 +967,192 @@ describe("Ext.Array", function() {
             expect(Ext.Array.mean([1,2,3,4,5,6])).toEqual(3.5);
         });
     });
+
+    function testReplace (replace) {
+        it('should remove items in the middle', function () {
+            var array = [0, 1, 2, 3, 4, 5, 6, 7];
+            replace(array, 2, 2);
+            expect(Ext.encode(array)).toEqual('[0,1,4,5,6,7]');
+        });
+        it('should insert items in the middle', function () {
+            var array = [0, 1, 2, 3, 4, 5, 6, 7];
+            replace(array, 2, 0, ['a','b']);
+            expect(Ext.encode(array)).toEqual('[0,1,"a","b",2,3,4,5,6,7]');
+        });
+        it('should replace in the middle with more items', function () {
+            var array = [0, 1, 2, 3, 4, 5, 6, 7];
+            replace(array, 2, 2, ['a','b', 'c', 'd']);
+            expect(Ext.encode(array)).toEqual('[0,1,"a","b","c","d",4,5,6,7]');
+        });
+        it('should replace in the middle with fewer items', function () {
+            var array = [0, 1, 2, 3, 4, 5, 6, 7];
+            replace(array, 2, 4, ['a','b']);
+            expect(Ext.encode(array)).toEqual('[0,1,"a","b",6,7]');
+        });
+        it('should delete at front', function () {
+            var array = [0, 1, 2, 3];
+            replace(array, 0, 2);
+            expect(Ext.encode(array)).toEqual('[2,3]');
+        });
+        it('should delete at tail', function () {
+            var array = [0, 1, 2, 3];
+            replace(array, 2, 2);
+            expect(Ext.encode(array)).toEqual('[0,1]');
+        });
+        it('should delete everything', function () {
+            var array = [0, 1, 2, 3];
+            replace(array, 0, 4);
+            expect(Ext.encode(array)).toEqual('[]');
+        });
+        it('should insert at front', function () {
+            var array = [0, 1];
+            replace(array, 0, 0, ['a','b','c','d','e']);
+            expect(Ext.encode(array)).toEqual('["a","b","c","d","e",0,1]');
+        });
+        it('should insert at tail', function () {
+            var array = [0, 1];
+            replace(array, array.length, 0, ['a','b','c','d','e']);
+            expect(Ext.encode(array)).toEqual('[0,1,"a","b","c","d","e"]');
+        });
+        it('should insert into empty array', function () {
+            var array = [];
+            replace(array, 0, 0, ['a','b','c','d','e']);
+            expect(Ext.encode(array)).toEqual('["a","b","c","d","e"]');
+        });
+        it('should replace at front', function () {
+            var array = [0, 1];
+            replace(array, 0, 1, ['a','b','c','d','e']);
+            expect(Ext.encode(array)).toEqual('["a","b","c","d","e",1]');
+        });
+        it('should replace at tail', function () {
+            var array = [0, 1];
+            replace(array, 1, 1, ['a','b','c','d','e']);
+            expect(Ext.encode(array)).toEqual('[0,"a","b","c","d","e"]');
+        });
+        it('should replace entire array', function () {
+            var array = [0, 1, 2, 3];
+            replace(array, 0, array.length, ['a','b','c','d','e']);
+            expect(Ext.encode(array)).toEqual('["a","b","c","d","e"]');
+        });
+        it('should handle negative index', function () {
+            var array = [0, 1, 2, 3];
+            replace(array, -2, 20); // should clip
+            expect(Ext.encode(array)).toEqual('[0,1]');
+        });
+        it('should work around the IE8 bug', function () {
+            // see http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/6e946d03-e09f-4b22-a4dd-cd5e276bf05a/
+            var array = [],
+                lengthBefore,
+                j = 20;
+
+            while (j--) {
+                array.push("A");
+            }
+
+            array.splice(15, 0, "F", "F", "F", "F", "F","F","F","F","F","F","F","F","F","F","F","F","F","F","F","F","F");
+            // the fact that this is an APPLY is not instrumental to reproducing this bug
+
+            lengthBefore = array.length; // = 41
+
+            // everything above should be exactly preserved including the true splice call.
+            // That way we have produced the Array Time Bomb... now see if it explodes!
+
+            replace(array, 13, 0, ["XXX"]); // add one element (this was the failure)
+
+            expect(array.length).toEqual(lengthBefore+1);
+        });
+    }
+
+    describe('replaceSim', function () {
+        // The _replace method is our corrected method for IE8, but we make it available (in
+        // debug builds) on all browsers to see that it works.
+        testReplace(Ext.Array._replaceSim);
+    });
+
+    describe('replaceNative', function () {
+        // and test the wrapper on other browsers
+        testReplace(Ext.Array.replace);
+    });
+
+    describe('splice', function () {
+        it('returns proper result array at the front', function () {
+            var ret = Ext.Array._spliceSim([1,2,3,4], 0, 2);
+            expect(Ext.encode(ret)).toEqual('[1,2]');
+        });
+        it('returns proper result array at the end', function () {
+            var ret = Ext.Array._spliceSim([1,2,3,4], 2, 2);
+            expect(Ext.encode(ret)).toEqual('[3,4]');
+        });
+        it('returns proper result array from the middle', function () {
+            var ret = Ext.Array._spliceSim([1,2,3,4], 1, 2);
+            expect(Ext.encode(ret)).toEqual('[2,3]');
+        });
+        it('return an empty array when nothing removed', function () {
+            var ret = Ext.Array._spliceSim([1,2,3,4], 1, 0);
+            expect(Ext.encode(ret)).toEqual('[]');
+        });
+    });
+
+    describe('slice', function(){
+        
+        var array;
+        
+        describe('with Array', function(){
+            beforeEach(function(){
+                array = [{0:0}, {1:1}, {2:2}, {3:3}];
+            });
+            tests();
+        });
+        
+        describe('with arguments', function(){
+            beforeEach(function(){
+                array = (function(){ return arguments; })({0:0}, {1:1}, {2:2}, {3:3});
+            });
+            tests();
+        });
+        
+        function tests(){
+            it('should shallow clone', function(){
+                var newArray = Ext.Array.slice(array, 0);
+                expect(newArray === array).toBe(false);
+                expect(newArray[0] === array[0]).toBe(true);
+            });
+            it('should not require a begin or end', function(){
+                var newArray = Ext.Array.slice(array);
+                expect(newArray === array).toBe(false);
+                expect(newArray[0]).toBe(array[0]);
+            });
+            it('should slice off the first item', function(){
+                var newArray = Ext.Array.slice(array, 1);
+                expect(newArray.length).toBe(3);
+                expect(newArray[0]).toBe(array[1]);
+                expect(newArray[2]).toBe(array[3]);
+            });
+            it('should ignore `end` if undefined', function(){
+                var newArray = Ext.Array.slice(array, 1, undefined);
+                expect(newArray.length).toBe(3);
+                expect(newArray[0]).toBe(array[1]);
+                expect(newArray[2]).toBe(array[3]);
+            });
+            it('should ignore `begin` if undefined', function(){
+                var newArray = Ext.Array.slice(array, undefined);
+                expect(newArray.length).toBe(4);
+                expect(newArray[0]).toBe(array[0]);
+                expect(newArray[3]).toBe(array[3]);
+            });
+            it('should ignore `begin` and `end` if undefined', function(){
+                var newArray = Ext.Array.slice(array, undefined, undefined);
+                expect(newArray.length).toBe(4);
+                expect(newArray[0]).toBe(array[0]);
+                expect(newArray[3]).toBe(array[3]);
+            });
+            it('should slice out the middle', function(){
+                var newArray = Ext.Array.slice(array, 1, -1);
+                expect(newArray.length).toBe(2);
+                expect(newArray[0]).toBe(array[1]);
+                expect(newArray[1]).toBe(array[2]);
+            });
+        }
+    });
 });
+