1 | define(["doh", "dojo/_base/array", "dojo/_base/lang"], function(doh, array, lang){ |
---|
2 | |
---|
3 | doh.register("tests._base.array", [ |
---|
4 | function testIndexOf(t){ |
---|
5 | var foo = [128, 256, 512]; |
---|
6 | var bar = ["aaa", "bbb", "ccc"]; |
---|
7 | |
---|
8 | t.assertEqual(1, array.indexOf([45, 56, 85], 56)); |
---|
9 | t.assertEqual(1, array.indexOf([Number, String, Date], String)); |
---|
10 | t.assertEqual(1, array.indexOf(foo, foo[1])); |
---|
11 | t.assertEqual(2, array.indexOf(foo, foo[2])); |
---|
12 | t.assertEqual(1, array.indexOf(bar, bar[1])); |
---|
13 | t.assertEqual(2, array.indexOf(bar, bar[2])); |
---|
14 | t.assertEqual(-1, array.indexOf({a:1}, "a")); |
---|
15 | |
---|
16 | foo.push(bar); |
---|
17 | t.assertEqual(3, array.indexOf(foo, bar)); |
---|
18 | }, |
---|
19 | |
---|
20 | function testIndexOfFromIndex(t){ |
---|
21 | var foo = [128, 256, 512]; |
---|
22 | var bar = ["aaa", "bbb", "ccc"]; |
---|
23 | |
---|
24 | t.assertEqual(-1, array.indexOf([45, 56, 85], 56, 2)); |
---|
25 | t.assertEqual(1, array.indexOf([45, 56, 85], 56, 1)); |
---|
26 | t.assertEqual(1, array.indexOf([45, 56, 85], 56, -3)); |
---|
27 | // Make sure going out of bounds doesn't throw us in an infinite loop |
---|
28 | t.assertEqual(-1, array.indexOf([45, 56, 85], 56, 3)); |
---|
29 | }, |
---|
30 | |
---|
31 | function testLastIndexOf(t){ |
---|
32 | var foo = [128, 256, 512]; |
---|
33 | var bar = ["aaa", "bbb", "aaa", "ccc"]; |
---|
34 | |
---|
35 | t.assertEqual(1, array.indexOf([45, 56, 85], 56)); |
---|
36 | t.assertEqual(1, array.indexOf([Number, String, Date], String)); |
---|
37 | t.assertEqual(1, array.lastIndexOf(foo, foo[1])); |
---|
38 | t.assertEqual(2, array.lastIndexOf(foo, foo[2])); |
---|
39 | t.assertEqual(1, array.lastIndexOf(bar, bar[1])); |
---|
40 | t.assertEqual(2, array.lastIndexOf(bar, bar[2])); |
---|
41 | t.assertEqual(2, array.lastIndexOf(bar, bar[0])); |
---|
42 | }, |
---|
43 | |
---|
44 | function testLastIndexOfFromIndex(t){ |
---|
45 | t.assertEqual(1, array.lastIndexOf([45, 56, 85], 56, 1)); |
---|
46 | t.assertEqual(-1, array.lastIndexOf([45, 56, 85], 85, 1)); |
---|
47 | t.assertEqual(-1, array.lastIndexOf([45, 56, 85], 85, -2)); |
---|
48 | t.assertEqual(0, array.lastIndexOf([45, 56, 45], 45, 0)); |
---|
49 | }, |
---|
50 | |
---|
51 | function testForEach(t){ |
---|
52 | var foo = [128, "bbb", 512]; |
---|
53 | array.forEach(foo, function(elt, idx, array){ |
---|
54 | switch(idx){ |
---|
55 | case 0: t.assertEqual(128, elt); break; |
---|
56 | case 1: t.assertEqual("bbb", elt); break; |
---|
57 | case 2: t.assertEqual(512, elt); break; |
---|
58 | default: t.assertTrue(false); |
---|
59 | } |
---|
60 | }); |
---|
61 | |
---|
62 | var noException = true; |
---|
63 | try{ |
---|
64 | array.forEach(undefined, function(){}); |
---|
65 | }catch(e){ |
---|
66 | noException = false; |
---|
67 | } |
---|
68 | t.assertTrue(noException); |
---|
69 | }, |
---|
70 | |
---|
71 | function testForEach_str(t){ |
---|
72 | var bar = 'abc'; |
---|
73 | array.forEach(bar, function(elt, idx, array){ |
---|
74 | switch(idx){ |
---|
75 | case 0: t.assertEqual("a", elt); break; |
---|
76 | case 1: t.assertEqual("b", elt); break; |
---|
77 | case 2: t.assertEqual("c", elt); break; |
---|
78 | default: t.assertTrue(false); |
---|
79 | } |
---|
80 | }); |
---|
81 | }, |
---|
82 | // FIXME: test forEach w/ a NodeList()? |
---|
83 | |
---|
84 | function testForEach_string_callback(t){ |
---|
85 | // Test using strings as callback", which accept the parameters with |
---|
86 | // the names "item", "index" and "array"! |
---|
87 | var foo = [128, "bbb", 512]; |
---|
88 | // Test that the variable "item" contains the value of each item. |
---|
89 | var obj = { |
---|
90 | _res: "" |
---|
91 | }; |
---|
92 | array.forEach(foo, "this._res += item", obj); |
---|
93 | t.assertEqual(obj._res, "128bbb512"); |
---|
94 | // Test that the variable "index" contains each index. |
---|
95 | obj._res = []; |
---|
96 | array.forEach(foo, "this._res.push(index)", obj); |
---|
97 | t.assertEqual(obj._res, [0,1,2]); |
---|
98 | // Test that the variable "array" always contains the entire array. |
---|
99 | obj._res = []; |
---|
100 | array.forEach(foo, "this._res.push(array)", obj); |
---|
101 | t.assertEqual(obj._res, [ |
---|
102 | [128, "bbb", 512], |
---|
103 | [128, "bbb", 512], |
---|
104 | [128, "bbb", 512] |
---|
105 | ]); |
---|
106 | // Catch undefined variable usage (I used to use "i" :-)). |
---|
107 | var caughtException = false; |
---|
108 | try{ |
---|
109 | array.forEach(foo, "this._res += arr[i];", obj); |
---|
110 | }catch(e){ |
---|
111 | caughtException = true; |
---|
112 | } |
---|
113 | t.assertTrue(caughtException); |
---|
114 | }, |
---|
115 | |
---|
116 | // FIXME: test forEach w/ a NodeList()? |
---|
117 | function testEvery(t){ |
---|
118 | var foo = [128, "bbb", 512]; |
---|
119 | |
---|
120 | t.assertTrue( |
---|
121 | array.every(foo, function(elt, idx, array){ |
---|
122 | t.assertEqual(Array, array.constructor); |
---|
123 | t.assertTrue(lang.isArray(array)); |
---|
124 | t.assertTrue(typeof idx == "number"); |
---|
125 | if(idx == 1){ t.assertEqual("bbb" , elt); } |
---|
126 | return true; |
---|
127 | }) |
---|
128 | ); |
---|
129 | |
---|
130 | t.assertTrue( |
---|
131 | array.every(foo, function(elt, idx, array){ |
---|
132 | switch(idx){ |
---|
133 | case 0: t.assertEqual(128, elt); return true; |
---|
134 | case 1: t.assertEqual("bbb", elt); return true; |
---|
135 | case 2: t.assertEqual(512, elt); return true; |
---|
136 | default: return false; |
---|
137 | } |
---|
138 | }) |
---|
139 | ); |
---|
140 | |
---|
141 | t.assertFalse( |
---|
142 | array.every(foo, function(elt, idx, array){ |
---|
143 | switch(idx){ |
---|
144 | case 0: t.assertEqual(128, elt); return true; |
---|
145 | case 1: t.assertEqual("bbb", elt); return true; |
---|
146 | case 2: t.assertEqual(512, elt); return false; |
---|
147 | default: return true; |
---|
148 | } |
---|
149 | }) |
---|
150 | ); |
---|
151 | |
---|
152 | }, |
---|
153 | |
---|
154 | function testEvery_str(t){ |
---|
155 | var bar = 'abc'; |
---|
156 | t.assertTrue( |
---|
157 | array.every(bar, function(elt, idx, array){ |
---|
158 | switch(idx){ |
---|
159 | case 0: t.assertEqual("a", elt); return true; |
---|
160 | case 1: t.assertEqual("b", elt); return true; |
---|
161 | case 2: t.assertEqual("c", elt); return true; |
---|
162 | default: return false; |
---|
163 | } |
---|
164 | }) |
---|
165 | ); |
---|
166 | |
---|
167 | t.assertFalse( |
---|
168 | array.every(bar, function(elt, idx, array){ |
---|
169 | switch(idx){ |
---|
170 | case 0: t.assertEqual("a", elt); return true; |
---|
171 | case 1: t.assertEqual("b", elt); return true; |
---|
172 | case 2: t.assertEqual("c", elt); return false; |
---|
173 | default: return true; |
---|
174 | } |
---|
175 | }) |
---|
176 | ); |
---|
177 | }, |
---|
178 | // FIXME: test NodeList for every()? |
---|
179 | |
---|
180 | function testSome(t){ |
---|
181 | var foo = [128, "bbb", 512]; |
---|
182 | t.assertTrue( |
---|
183 | array.some(foo, function(elt, idx, array){ |
---|
184 | t.assertEqual(3, array.length); |
---|
185 | return true; |
---|
186 | }) |
---|
187 | ); |
---|
188 | |
---|
189 | t.assertTrue( |
---|
190 | array.some(foo, function(elt, idx, array){ |
---|
191 | return idx < 1; |
---|
192 | |
---|
193 | }) |
---|
194 | ); |
---|
195 | |
---|
196 | t.assertFalse( |
---|
197 | array.some(foo, function(elt, idx, array){ |
---|
198 | return false; |
---|
199 | }) |
---|
200 | ); |
---|
201 | |
---|
202 | t.assertTrue( |
---|
203 | array.some(foo, function(elt, idx, array){ |
---|
204 | t.assertEqual(Array, array.constructor); |
---|
205 | t.assertTrue(lang.isArray(array)); |
---|
206 | t.assertTrue(typeof idx == "number"); |
---|
207 | if(idx == 1){ t.assertEqual("bbb" , elt); } |
---|
208 | return true; |
---|
209 | }) |
---|
210 | ); |
---|
211 | }, |
---|
212 | |
---|
213 | function testSome_str(t){ |
---|
214 | var bar = 'abc'; |
---|
215 | t.assertTrue( |
---|
216 | array.some(bar, function(elt, idx, array){ |
---|
217 | t.assertEqual(3, array.length); |
---|
218 | switch(idx){ |
---|
219 | case 0: t.assertEqual("a", elt); return true; |
---|
220 | case 1: t.assertEqual("b", elt); return true; |
---|
221 | case 2: t.assertEqual("c", elt); return true; |
---|
222 | default: return false; |
---|
223 | } |
---|
224 | }) |
---|
225 | ); |
---|
226 | |
---|
227 | t.assertTrue( |
---|
228 | array.some(bar, function(elt, idx, array){ |
---|
229 | switch(idx){ |
---|
230 | case 0: t.assertEqual("a", elt); return true; |
---|
231 | case 1: t.assertEqual("b", elt); return true; |
---|
232 | case 2: t.assertEqual("c", elt); return false; |
---|
233 | default: return true; |
---|
234 | } |
---|
235 | }) |
---|
236 | ); |
---|
237 | |
---|
238 | t.assertFalse( |
---|
239 | array.some(bar, function(elt, idx, array){ |
---|
240 | return false; |
---|
241 | }) |
---|
242 | ); |
---|
243 | }, |
---|
244 | // FIXME: need to add scoping tests for all of these!!! |
---|
245 | |
---|
246 | function testFilter(t){ |
---|
247 | var foo = ["foo", "bar", 10]; |
---|
248 | |
---|
249 | t.assertEqual(["foo"], |
---|
250 | array.filter(foo, function(elt, idx, array){ |
---|
251 | return idx < 1; |
---|
252 | }) |
---|
253 | ); |
---|
254 | |
---|
255 | t.assertEqual(["foo"], |
---|
256 | array.filter(foo, function(elt, idx, array){ |
---|
257 | return elt == "foo"; |
---|
258 | }) |
---|
259 | ); |
---|
260 | |
---|
261 | t.assertEqual([], |
---|
262 | array.filter(foo, function(elt, idx, array){ |
---|
263 | return false; |
---|
264 | }) |
---|
265 | ); |
---|
266 | |
---|
267 | t.assertEqual([10], |
---|
268 | array.filter(foo, function(elt, idx, array){ |
---|
269 | return typeof elt == "number"; |
---|
270 | }) |
---|
271 | ); |
---|
272 | }, |
---|
273 | |
---|
274 | function testFilter_str(t){ |
---|
275 | var foo = "thinger blah blah blah"; |
---|
276 | t.assertEqual(["t", "h", "i"], |
---|
277 | array.filter(foo, function(elt, idx, array){ |
---|
278 | return idx < 3; |
---|
279 | }) |
---|
280 | ); |
---|
281 | |
---|
282 | t.assertEqual([], |
---|
283 | array.filter(foo, function(elt, idx, array){ |
---|
284 | return false; |
---|
285 | }) |
---|
286 | ); |
---|
287 | }, |
---|
288 | |
---|
289 | function testMap(t){ |
---|
290 | t.assertEqual([], |
---|
291 | array.map([], function(){ return true; }) |
---|
292 | ); |
---|
293 | |
---|
294 | t.assertEqual([1, 2, 3], |
---|
295 | array.map(["cat", "dog", "mouse"], function(elt, idx, array){ |
---|
296 | return idx+1; |
---|
297 | }) |
---|
298 | ); |
---|
299 | } |
---|
300 | ]); |
---|
301 | }); |
---|
302 | |
---|