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