* Starting to work on the new specs for MooTools-Core
[mootools/dkf.git] / OldSpecs / Native / Array.js
blob37d28e6bb80ac4ed924486afe1efd758733f9b57
1 /*
2 Script: Array.js
3         Specs for Array.js
5 License:
6         MIT-style license.
7 */
9 describe("Array Methods", {
11         // Array.flatten
13         'should flatten a multidimensional array': function(){
14                 var arr = [1,2,3,[4,5,[6,7,[8]]], [[[[[9]]]]]];
15                 value_of(arr.flatten()).should_be([1,2,3,4,5,6,7,8,9]);
16         },
18         'should flatten arguments': function(){
19                 var test = function(){
20                         return Array.flatten(arguments);
21                 };
22                 value_of(test(1,2,3)).should_be([1,2,3]);
23                 value_of(test([1,2,3])).should_be([1,2,3]);
24                 value_of(test(1,2,[3])).should_be([1,2,3]);
25         },
27         // Array.filter
29         'should filter an array': function(){
30                 var array = [1,2,3,0,0,0];
31                 var arr = array.concat([false, null, 4]).filter(Number.type);
32                 value_of(arr).should_be(array.concat(4));
33         },
35         // Array.clean
37         'should clean an array from undefined and null values': function(){
38                 var array = [null, 1, 0, true, false, "foo", undefined];
39                 var arr = array.clean();
40                 value_of(arr).should_be([1, 0, true, false, "foo"]);
41         },
43         // Array.map
45         'should return a mapping of an array': function(){
46                 var arr = [1,2,3,0,0,0].map(function(item){
47                         return (item + 1);
48                 });
50                 value_of(arr).should_be([2,3,4,1,1,1]);
51         },
53         // Array.every
55         'should return true if every item matches the comparator, otherwise false': function(){
56                 value_of([1,2,3,0,0,0].every(Number.type)).should_be_true();
58                 value_of(['1',2,3,0].every(Number.type)).should_be_false();
59         },
61         // Array.some
63         'should return true if some of the items in the array match the comparator, otherwise false': function(){
64                 value_of(['1',2,3,0].some(Number.type)).should_be_true();
66                 value_of([1,2,3,0,0,0].map(String).some(Number.type)).should_be_false();
67         },
69         // Array.indexOf
71         'should return the index of the item': function(){
72                 value_of([1,2,3,0,0,0].indexOf(0)).should_be(3);
73         },
75         'should return -1 if the item is not found in the array': function(){
76                 value_of([1,2,3,0,0,0].indexOf('not found')).should_be(-1);
77         },
79         // Array.erase
81         'should remove all items in the array that match the specified item': function(){
82                 var arr = [1,2,3,0,0,0].erase(0);
83                 value_of(arr).should_be([1,2,3]);
84         },
86         // Array.contains
88         'should return true if the array contains the specified item': function(){
89                 value_of([1,2,3,0,0,0].contains(0)).should_be_true();
90         },
92         'should return false if the array does not contain the specified item': function(){
93                 value_of([0,1,2].contains('not found')).should_be_false();
94         },
96         // Array.associate
98         'should associate an array with a specified array': function(){
99                 var obj = [1,2,3,0,0,0].associate(['a', 'b', 'c', 'd']);
100                 value_of(obj).should_be({a:1, b:2, c:3, d:0});
101         },
103         // Array.link
105         'should link an array items to a new object according to the specified matchers': function(){
106                 var el = document.createElement('div');
107                 var assoc2 = [100, 'Hello', {foo: 'bar'}, el, false].link({
108                         myNumber: Number.type,
109                         myElement: Element.type,
110                         myObject: Object.type,
111                         myString: String.type,
112                         myBoolean: $defined
113                 });
115                 value_of(assoc2).should_be({
116                         myNumber: 100,
117                         myElement: el,
118                         myObject: {foo: 'bar'},
119                         myString: 'Hello',
120                         myBoolean: false
121                 });
122         },
124         // Array.extend
126         'should extend an array': function(){
127                 var a = [1,2,4];
128                 var b = [2,3,4,5];
129                 a.extend(b);
130                 value_of(a).should_be([1,2,4,2,3,4,5]);
131                 value_of(b).should_be([2,3,4,5]);
132         },
134         // Array.combine
136         'should combine an array': function(){
137                 var arr = [1,2,3,4].combine([3,1,4,5,6,7]);
138                 value_of(arr).should_be([1,2,3,4,5,6,7]);
139         },
141         // Array.include
143         'should include only new items': function(){
144                 var arr = [1,2,3,4].include(1).include(5);
145                 value_of(arr).should_be([1,2,3,4,5]);
146         },
148         // Array.getLast
150         'should return the last item in the array': function(){
151                 value_of([1,2,3,0,0,0].getLast()).should_be(0);
152                 value_of([3].getLast()).should_be(3);
153         },
155         'should return null if there are no items': function(){
156                 value_of([].getLast()).should_be(null);
157         },
159         // Array.empty
161         'should empty the array': function(){
162                 var arr = [1,2,3,4].empty();
163                 value_of(arr).should_be([]);
164         }
168 describe("Array Color Methods", {
170         // Array.hexToRgb
172         'should return null if the length of the array is not 3': function(){
173                 value_of([].hexToRgb()).should_be_null();
174         },
176         'should return a CSS rgb string': function(){
177                 value_of(['0','0','0'].hexToRgb()).should_be('rgb(0,0,0)');
178         },
180         'should support shorthand hex': function(){
181                 value_of(['c','c','c'].hexToRgb()).should_be('rgb(204,204,204)');
182         },
184         'should return an array with 16-based numbers when passed true': function(){
185                 value_of(['ff','ff','ff'].hexToRgb(true)).should_be([255,255,255]);
186         },
188         // Array.rgbToHex
190         'should return null if the array does not have at least 3 times': function(){
191                 value_of([0,1].rgbToHex()).should_be_null();
192         },
194         'should return a css hexadecimal string': function(){
195                 value_of(['255', '0', '0'].rgbToHex()).should_be('#ff0000');
196                 value_of([0,0,255].rgbToHex()).should_be('#0000ff');
197         },
199         'should return an array with hexadecimal string items': function(){
200                 value_of([0,255,0].rgbToHex(true)).should_be(['00', 'ff', '00']);
201         },
203         'should return `transparent` if the fourth item is 0 and first param is not true': function(){
204                 value_of([0,0,0,0].rgbToHex()).should_be('transparent');
205         }