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");
5 tests.register("tests._base.array",
7 function testIndexOf(t){
8 var foo = [128, 256, 512];
9 var bar = ["aaa", "bbb", "ccc"];
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"));
20 t.assertEqual(3, dojo.indexOf(foo, bar));
23 function testIndexOfFromIndex(t){
24 var foo = [128, 256, 512];
25 var bar = ["aaa", "bbb", "ccc"];
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));
34 function testLastIndexOf(t){
35 var foo = [128, 256, 512];
36 var bar = ["aaa", "bbb", "aaa", "ccc"];
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]));
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));
54 function testForEach(t){
55 var foo = [128, "bbb", 512];
56 dojo.forEach(foo, function(elt, idx, array){
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);
65 var noException = true;
67 dojo.forEach(undefined, function(){});
71 t.assertTrue(noException);
74 function testForEach_str(t){
76 dojo.forEach(bar, function(elt, idx, array){
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);
85 // FIXME: test forEach w/ a NodeList()?
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.
93 dojo.forEach(foo, 'this._res+=item', this);
94 t.assertEqual(this._res, "128bbb512");
95 // Test that the variable "index" contains each index.
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.
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;
106 dojo.forEach(foo, 'this._res+=i', this);
108 caughtException = true;
110 t.assertTrue(caughtException);
113 // FIXME: test forEach w/ a NodeList()?
114 function testEvery(t){
115 var foo = [128, "bbb", 512];
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); }
128 dojo.every(foo, function(elt, idx, array){
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;
139 dojo.every(foo, function(elt, idx, array){
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;
151 function testEvery_str(t){
154 dojo.every(bar, function(elt, idx, array){
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;
165 dojo.every(bar, function(elt, idx, array){
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;
175 // FIXME: test NodeList for every()?
177 function testSome(t){
178 var foo = [128, "bbb", 512];
180 dojo.some(foo, function(elt, idx, array){
181 t.assertEqual(3, array.length);
187 dojo.some(foo, function(elt, idx, array){
188 if(idx < 1){ return true; }
194 dojo.some(foo, function(elt, idx, array){
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); }
210 function testSome_str(t){
213 dojo.some(bar, function(elt, idx, array){
214 t.assertEqual(3, array.length);
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;
225 dojo.some(bar, function(elt, idx, array){
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;
236 dojo.some(bar, function(elt, idx, array){
241 // FIXME: need to add scoping tests for all of these!!!
243 function testFilter(t){
244 var foo = ["foo", "bar", 10];
246 t.assertEqual(["foo"],
247 dojo.filter(foo, function(elt, idx, array){
252 t.assertEqual(["foo"],
253 dojo.filter(foo, function(elt, idx, array){
259 dojo.filter(foo, function(elt, idx, array){
265 dojo.filter(foo, function(elt, idx, array){
266 return typeof elt == "number";
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){
280 dojo.filter(foo, function(elt, idx, array){
288 dojo.map([], function(){ return true; })
291 t.assertEqual([1, 2, 3],
292 dojo.map(["cat", "dog", "mouse"], function(elt, idx, array){