+
+ 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]);
+ });
+ }
+ });