|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 //----------------------------------------------------------------------------- |
|
7 var BUGNUMBER = 304828; |
|
8 var summary = 'Array Generic Methods'; |
|
9 var actual = ''; |
|
10 var expect = ''; |
|
11 printBugNumber(BUGNUMBER); |
|
12 printStatus (summary); |
|
13 |
|
14 var value; |
|
15 |
|
16 // use Array methods on a String |
|
17 // join |
|
18 value = '123'; |
|
19 expect = '1,2,3'; |
|
20 try |
|
21 { |
|
22 actual = Array.prototype.join.call(value); |
|
23 } |
|
24 catch(e) |
|
25 { |
|
26 actual = e + ''; |
|
27 } |
|
28 reportCompare(expect, actual, summary + ': join'); |
|
29 |
|
30 // reverse |
|
31 value = '123'; |
|
32 expect = 'TypeError: Array.prototype.reverse.call(...) is read-only'; |
|
33 try |
|
34 { |
|
35 actual = Array.prototype.reverse.call(value) + ''; |
|
36 } |
|
37 catch(e) |
|
38 { |
|
39 actual = e + ''; |
|
40 } |
|
41 reportCompare(expect, actual, summary + ': reverse'); |
|
42 |
|
43 // sort |
|
44 value = 'cba'; |
|
45 expect = 'TypeError: Array.prototype.sort.call(...) is read-only'; |
|
46 try |
|
47 { |
|
48 actual = Array.prototype.sort.call(value) + ''; |
|
49 } |
|
50 catch(e) |
|
51 { |
|
52 actual = e + ''; |
|
53 } |
|
54 reportCompare(expect, actual, summary + ': sort'); |
|
55 |
|
56 // push |
|
57 value = 'abc'; |
|
58 expect = 6; |
|
59 try |
|
60 { |
|
61 Array.prototype.push.call(value, 'd', 'e', 'f'); |
|
62 throw new Error("didn't throw"); |
|
63 } |
|
64 catch(e) |
|
65 { |
|
66 reportCompare(true, e instanceof TypeError, |
|
67 "push on a string primitive should throw TypeError"); |
|
68 } |
|
69 reportCompare('abc', value, summary + ': push string primitive'); |
|
70 |
|
71 value = new String("abc"); |
|
72 expect = 6; |
|
73 try |
|
74 { |
|
75 Array.prototype.push.call(value, 'd', 'e', 'f'); |
|
76 throw new Error("didn't throw"); |
|
77 } |
|
78 catch(e) |
|
79 { |
|
80 reportCompare(true, e instanceof TypeError, |
|
81 "push on a String object should throw TypeError"); |
|
82 } |
|
83 reportCompare("d", value[3], summary + ': push String object index 3'); |
|
84 reportCompare("e", value[4], summary + ': push String object index 4'); |
|
85 reportCompare("f", value[5], summary + ': push String object index 5'); |
|
86 |
|
87 // pop |
|
88 value = 'abc'; |
|
89 expect = "TypeError: property Array.prototype.pop.call(...) is non-configurable and can't be deleted"; |
|
90 try |
|
91 { |
|
92 actual = Array.prototype.pop.call(value); |
|
93 } |
|
94 catch(e) |
|
95 { |
|
96 actual = e + ''; |
|
97 } |
|
98 reportCompare(expect, actual, summary + ': pop'); |
|
99 reportCompare('abc', value, summary + ': pop'); |
|
100 |
|
101 // unshift |
|
102 value = 'def'; |
|
103 expect = 'TypeError: Array.prototype.unshift.call(...) is read-only'; |
|
104 try |
|
105 { |
|
106 actual = Array.prototype.unshift.call(value, 'a', 'b', 'c'); |
|
107 } |
|
108 catch(e) |
|
109 { |
|
110 actual = e + ''; |
|
111 } |
|
112 reportCompare(expect, actual, summary + ': unshift'); |
|
113 reportCompare('def', value, summary + ': unshift'); |
|
114 |
|
115 // shift |
|
116 value = 'abc'; |
|
117 expect = 'TypeError: Array.prototype.shift.call(...) is read-only'; |
|
118 try |
|
119 { |
|
120 actual = Array.prototype.shift.call(value); |
|
121 } |
|
122 catch(e) |
|
123 { |
|
124 actual = e + ''; |
|
125 } |
|
126 reportCompare(expect, actual, summary + ': shift'); |
|
127 reportCompare('abc', value, summary + ': shift'); |
|
128 |
|
129 // splice |
|
130 value = 'abc'; |
|
131 expect = 'TypeError: Array.prototype.splice.call(...) is read-only'; |
|
132 try |
|
133 { |
|
134 actual = Array.prototype.splice.call(value, 1, 1) + ''; |
|
135 } |
|
136 catch(e) |
|
137 { |
|
138 actual = e + ''; |
|
139 } |
|
140 reportCompare(expect, actual, summary + ': splice'); |
|
141 |
|
142 // concat |
|
143 value = 'abc'; |
|
144 expect = 'abc,d,e,f'; |
|
145 try |
|
146 { |
|
147 actual = Array.prototype.concat.call(value, 'd', 'e', 'f') + ''; |
|
148 } |
|
149 catch(e) |
|
150 { |
|
151 actual = e + ''; |
|
152 } |
|
153 reportCompare(expect, actual, summary + ': concat'); |
|
154 |
|
155 // slice |
|
156 value = 'abc'; |
|
157 expect = 'b'; |
|
158 try |
|
159 { |
|
160 actual = Array.prototype.slice.call(value, 1, 2) + ''; |
|
161 } |
|
162 catch(e) |
|
163 { |
|
164 actual = e + ''; |
|
165 } |
|
166 reportCompare(expect, actual, summary + ': slice'); |
|
167 |
|
168 // indexOf |
|
169 value = 'abc'; |
|
170 expect = 1; |
|
171 try |
|
172 { |
|
173 actual = Array.prototype.indexOf.call(value, 'b'); |
|
174 } |
|
175 catch(e) |
|
176 { |
|
177 actual = e + ''; |
|
178 } |
|
179 reportCompare(expect, actual, summary + ': indexOf'); |
|
180 |
|
181 // lastIndexOf |
|
182 value = 'abcabc'; |
|
183 expect = 4; |
|
184 try |
|
185 { |
|
186 actual = Array.prototype.lastIndexOf.call(value, 'b'); |
|
187 } |
|
188 catch(e) |
|
189 { |
|
190 actual = e + ''; |
|
191 } |
|
192 reportCompare(expect, actual, summary + ': lastIndexOf'); |
|
193 |
|
194 // forEach |
|
195 value = 'abc'; |
|
196 expect = 'ABC'; |
|
197 actual = ''; |
|
198 try |
|
199 { |
|
200 Array.prototype.forEach.call(value, |
|
201 function (v, index, array) |
|
202 {actual += array[index].toUpperCase();}); |
|
203 } |
|
204 catch(e) |
|
205 { |
|
206 actual = e + ''; |
|
207 } |
|
208 reportCompare(expect, actual, summary + ': forEach'); |
|
209 |
|
210 // map |
|
211 value = 'abc'; |
|
212 expect = 'A,B,C'; |
|
213 try |
|
214 { |
|
215 actual = Array.prototype.map.call(value, |
|
216 function (v, index, array) |
|
217 {return v.toUpperCase();}) + ''; |
|
218 } |
|
219 catch(e) |
|
220 { |
|
221 actual = e + ''; |
|
222 } |
|
223 reportCompare(expect, actual, summary + ': map'); |
|
224 |
|
225 // filter |
|
226 value = '1234567890'; |
|
227 expect = '2,4,6,8,0'; |
|
228 try |
|
229 { |
|
230 actual = Array.prototype.filter.call(value, |
|
231 function (v, index, array) |
|
232 {return array[index] % 2 == 0;}) + ''; |
|
233 } |
|
234 catch(e) |
|
235 { |
|
236 actual = e + ''; |
|
237 } |
|
238 reportCompare(expect, actual, summary + ': filter'); |
|
239 |
|
240 // every |
|
241 value = '1234567890'; |
|
242 expect = false; |
|
243 try |
|
244 { |
|
245 actual = Array.prototype.every.call(value, |
|
246 function (v, index, array) |
|
247 {return array[index] % 2 == 0;}); |
|
248 } |
|
249 catch(e) |
|
250 { |
|
251 actual = e + ''; |
|
252 } |
|
253 reportCompare(expect, actual, summary + ': every'); |
|
254 |
|
255 |
|
256 |