Remove modernie from specs
[mootools.git] / Specs / Types / Array.js
blob115110ffb12b66ab05877ad3c3ef33c059c6df1f
1 /*
2 ---
3 name: Array
4 requires: ~
5 provides: ~
6 ...
7 */
9 (function(){
11 var getTestArray = function(){
12         var a = [0, 1, 2, 3];
13         delete a[1];
14         delete a[2];
15         return a;
19 describe("Array", function(){
21         // Array.flatten
23         it('should flatten a multidimensional array', function(){
24                 var arr = [1,2,3,[4,5,[6,7,[8]]], [[[[[9]]]]]];
25                 expect(arr.flatten()).toEqual([1,2,3,4,5,6,7,8,9]);
26         });
28         it('should flatten arguments', function(){
29                 var test = function(){
30                         return Array.flatten(arguments);
31                 };
32                 expect(test(1,2,3)).toEqual([1,2,3]);
33                 expect(test([1,2,3])).toEqual([1,2,3]);
34                 expect(test(1,2,[3])).toEqual([1,2,3]);
35         });
37         // Array.filter
39         it('should filter an array', function(){
40                 var array = [1,2,3,0,0,0];
41                 var arr = array.concat([false, null, 4]).filter(Type.isNumber);
42                 expect(arr).toEqual(array.concat(4));
43         });
45         it('filter should skip deleted elements', function(){
46                 var i = 0;
47                 getTestArray().filter(function(){
48                         i++;
49                         return true;
50                 });
52                 expect(i).toEqual(2);
53         });
55         // Array.clean
57         it('should clean an array from undefined and null values', function(){
58                 var array = [null, 1, 0, true, false, "foo", undefined];
59                 var arr = array.clean();
60                 expect(arr).toEqual([1, 0, true, false, "foo"]);
61         });
63         // Array.map
65         it('should return a mapping of an array', function(){
66                 var arr = [1,2,3,0,0,0].map(function(item){
67                         return (item + 1);
68                 });
70                 expect(arr).toEqual([2,3,4,1,1,1]);
71         });
73         it('map should skip deleted elements', function(){
74                 var i = 0;
75                 getTestArray().map(function(){
76                         return i++;
77                 });
79                 expect(i).toEqual(2);
80         });
82         // Array.every
84         it('should return true if every item matches the comparator, otherwise false', function(){
85                 expect([1,2,3,0,0,0].every(Type.isNumber)).toBeTruthy();
87                 expect(['1',2,3,0].every(Type.isNumber)).toBeFalsy();
88         });
90         it('every should skip deleted elements', function(){
91                 var i = 0;
92                 getTestArray().every(function(){
93                         i++;
94                         return true;
95                 });
97                 expect(i).toEqual(2);
98         });
100         // Array.some
102         it('should return true if some of the items in the array match the comparator, otherwise false', function(){
103                 expect(['1',2,3,0].some(Type.isNumber)).toBeTruthy();
105                 expect([1,2,3,0,0,0].map(String).some(Type.isNumber)).toBeFalsy();
106         });
108         it('some should skip deleted elements', function(){
109                 var i = 0;
110                 var a = getTestArray();
111                 delete a[0];
113                 // skips the first three elements
114                 a.some(function(value, index){
115                         i = index;
116                         return true;
117                 });
119                 expect(i).toEqual(3);
120         });
122         // Array.indexOf
124         it('should return the index of the item', function(){
125                 expect([1,2,3,0,0,0].indexOf(0)).toEqual(3);
126         });
128         it('should return -1 if the item is not found in the array', function(){
129                 expect([1,2,3,0,0,0].indexOf('not found')).toEqual(-1);
130         });
132         // Array.erase
134         it('should remove all items in the array that match the specified item', function(){
135                 var arr = [1,2,3,0,0,0].erase(0);
136                 expect(arr).toEqual([1,2,3]);
137         });
139         // Array.contains
141         it('should return true if the array contains the specified item', function(){
142                 expect([1,2,3,0,0,0].contains(0)).toBeTruthy();
143         });
145         it('should return false if the array does not contain the specified item', function(){
146                 expect([0,1,2].contains('not found')).toBeFalsy();
147         });
149         // Array.associate
151         it('should associate an array with a specified array', function(){
152                 var obj = [1,2,3,0,0,0].associate(['a', 'b', 'c', 'd']);
153                 expect(obj).toEqual({a:1, b:2, c:3, d:0});
154         });
156         // Array.append
158         it('should append to an array', function(){
159                 var a = [1,2,4];
160                 var b = [2,3,4,5];
161                 a.append(b);
162                 expect(a).toEqual([1,2,4,2,3,4,5]);
163                 expect(b).toEqual([2,3,4,5]);
164         });
166         var isType = function(type){
167                 return function(obj){
168                         return typeOf(obj) == type;
169                 };
170         };
172         // Array.link
173         it('should link an array items to a new object according to the specified matchers', function(){
174                 var el = document.createElement('div');
175                 var assoc2 = [100, 'Hello', {foo: 'bar'}, el, false].link({
176                         myNumber: isType('number'),
177                         myElement: isType('element'),
178                         myObject: isType('object'),
179                         myString: isType('string'),
180                         myBoolean: isType('boolean')
181                 });
183                 expect(assoc2).toEqual({
184                         myNumber: 100,
185                         myElement: el,
186                         myObject: {foo: 'bar'},
187                         myString: 'Hello',
188                         myBoolean: false
189                 });
190         });
192         //<1.2compat>
193         // Array.extend
194         it('should extend an array', function(){
195                 var a = [1,2,4];
196                 var b = [2,3,4,5];
197                 a.extend(b);
198                 expect(a).toEqual([1,2,4,2,3,4,5]);
199                 expect(b).toEqual([2,3,4,5]);
200         });
201         //</1.2compat>
203         it('should append an array', function(){
204                 var a = [1,2,4];
205                 var b = [2,3,4,5];
206                 a.append(b);
207                 expect(a).toEqual([1,2,4,2,3,4,5]);
208                 expect(b).toEqual([2,3,4,5]);
209         });
211         // Array.combine
213         it('should combine an array', function(){
214                 var arr = [1,2,3,4].combine([3,1,4,5,6,7]);
215                 expect(arr).toEqual([1,2,3,4,5,6,7]);
216         });
218         // Array.include
220         it('should include only new items', function(){
221                 var arr = [1,2,3,4].include(1).include(5);
222                 expect(arr).toEqual([1,2,3,4,5]);
223         });
225         // Array.getLast
227         it('should return the last item in the array', function(){
228                 expect([1,2,3,0,0,0].getLast()).toEqual(0);
229                 expect([3].getLast()).toEqual(3);
230         });
232         it('should return null if there are no items', function(){
233                 expect([].getLast()).toEqual(null);
234         });
236         // Array.empty
238         it('should empty the array', function(){
239                 var arr = [1,2,3,4].empty();
240                 expect(arr).toEqual([]);
241         });
245 describe("Array Color Methods", function(){
247         // Array.hexToRgb
249         it('should return null if the length of the array is not 3', function(){
250                 expect([].hexToRgb()).toBeNull();
251         });
253         it('should return a CSS rgb string', function(){
254                 expect(['0','0','0'].hexToRgb()).toEqual('rgb(0,0,0)');
255         });
257         it('should support shorthand hex', function(){
258                 expect(['c','c','c'].hexToRgb()).toEqual('rgb(204,204,204)');
259         });
261         it('should return an array with 16-based numbers when passed true', function(){
262                 expect(['ff','ff','ff'].hexToRgb(true)).toEqual([255,255,255]);
263         });
265         // Array.rgbToHex
267         it('should return null if the array does not have at least 3 times', function(){
268                 expect([0,1].rgbToHex()).toBeNull();
269         });
271         it('should return a css hexadecimal string', function(){
272                 expect(['255', '0', '0'].rgbToHex()).toEqual('#ff0000');
273                 expect([0,0,255].rgbToHex()).toEqual('#0000ff');
274         });
276         it('should return an array with hexadecimal string items', function(){
277                 expect([0,255,0].rgbToHex(true)).toEqual(['00', 'ff', '00']);
278         });
280         it('should return `transparent` if the fourth item is 0 and first param is not true', function(){
281                 expect([0,0,0,0].rgbToHex()).toEqual('transparent');
282         });
286 describe('Array.getRandom', function(){
288         it('should get a random element from an array', function(){
289                 var a = [1];
291                 expect(a.getRandom()).toEqual(1);
293                 a.push(2);
295                 // Let's try a few times
296                 expect(a).toContain(a.getRandom());
297                 expect(a).toContain(a.getRandom());
298                 expect(a).toContain(a.getRandom());
299                 expect(a).toContain(a.getRandom());
300         });
304 describe('Array.pick', function(){
306         it('should pick a value that is not null from the array', function(){
307                 expect([null, undefined, true, 1].pick()).toEqual(true);
308         });
310         it('should return null', function(){
311                 expect([].pick()).toBeNull();
312         });
316 describe('Array', function(){
318         describe('Array.map', function(){
320                 it('should return an array with the same length', function(){
321                         expect([1, 2, 3, undefined].map(function(v){
322                                 return v;
323                         }).length).toEqual(4);
324                 });
326                 it('shoud return an empty array when the thisArg does not has a length property', function(){
327                         expect([].map.call({}, function(){
328                                 return 1;
329                         })).toEqual([]);
330                 });
332         });
334         it('should accept thisArgs without length property', function(){
335                 var object = {}, fn = function(){};
336                 expect([].every.call(object, fn)).toBe(true);
337                 expect([].filter.call(object, fn)).toEqual([]);
338                 expect([].indexOf.call(object)).toEqual(-1);
339                 expect([].map.call(object, fn)).toEqual([]);
340                 expect([].some.call(object, fn)).toBe(false);
341         });
343         describe('Array.filter', function(){
345                 it('should return the original item, and not any mutations.', function(){
347                         var result = [0, 1, 2].filter(function(num, i, array){
348                                 if (num == 1){
349                                         array[i] = 'mutation';
350                                         return true;
351                                 }
352                         });
354                         expect(result[0]).toEqual(1);
355                 });
357         });
361 })();