|
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 = 312385; |
|
8 var summary = 'Generic methods with null or undefined |this|'; |
|
9 var actual = ''; |
|
10 var expect = true; |
|
11 var voids = [null, undefined]; |
|
12 |
|
13 |
|
14 function noop() { } |
|
15 |
|
16 var generics = { |
|
17 String: [{ quote: [] }, |
|
18 { substring: [] }, |
|
19 { toLowerCase: [] }, |
|
20 { toUpperCase: [] }, |
|
21 { charAt: [] }, |
|
22 { charCodeAt: [] }, |
|
23 { indexOf: [] }, |
|
24 { lastIndexOf: [] }, |
|
25 { toLocaleLowerCase: [] }, |
|
26 { toLocaleUpperCase: [] }, |
|
27 { localeCompare: [] }, |
|
28 { match: [/(?:)/] }, // match(regexp) |
|
29 { search: [] }, |
|
30 { replace: [] }, |
|
31 { split: [] }, |
|
32 { substr: [] }, |
|
33 { concat: [] }, |
|
34 { slice: [] }], |
|
35 |
|
36 Array: [{ join: [] }, |
|
37 { reverse: [] }, |
|
38 { sort: [] }, |
|
39 // { push: [0] }, // push(item1, ...) |
|
40 // { pop: [] }, |
|
41 // { shift: [] }, |
|
42 { unshift: [] }, |
|
43 // { splice: [0, 0, 1] }, // splice(start, deleteCount, item1, ...) |
|
44 { concat: [] }, |
|
45 { indexOf: [] }, |
|
46 { lastIndexOf: [] }, |
|
47 // forEach is excluded since it does not return a value... |
|
48 /* { forEach: [noop] }, // forEach(callback, thisObj) */ |
|
49 { map: [noop] }, // map(callback, thisObj) |
|
50 { filter: [noop] }, // filter(callback, thisObj) |
|
51 { some: [noop] }, // some(callback, thisObj) |
|
52 { every: [noop] } // every(callback, thisObj) |
|
53 ] |
|
54 }; |
|
55 |
|
56 printBugNumber(BUGNUMBER); |
|
57 printStatus (summary); |
|
58 |
|
59 var global = this; |
|
60 |
|
61 for (var c in generics) |
|
62 { |
|
63 var methods = generics[c]; |
|
64 for (var i = 0; i < methods.length; i++) |
|
65 { |
|
66 var method = methods[i]; |
|
67 |
|
68 for (var methodname in method) |
|
69 { |
|
70 for (var v = 0; v < voids.length; v++) |
|
71 { |
|
72 var Constructor = global[c] |
|
73 |
|
74 var argsLen = method[methodname].length; |
|
75 assertEq(argsLen === 0 || argsLen === 1, true, "not all arities handled"); |
|
76 |
|
77 var generic = Constructor[methodname]; |
|
78 var prototypy = Constructor.prototype[methodname]; |
|
79 |
|
80 assertEq(typeof generic, "function"); |
|
81 assertEq(typeof prototypy, "function"); |
|
82 |
|
83 // GENERIC METHOD TESTING |
|
84 |
|
85 try |
|
86 { |
|
87 switch (method[methodname].length) |
|
88 { |
|
89 case 0: |
|
90 generic(voids[v]); |
|
91 break; |
|
92 |
|
93 case 1: |
|
94 generic(voids[v], method[methodname][0]); |
|
95 break; |
|
96 } |
|
97 throw new Error(c + "." + methodname + " must throw for null or " + |
|
98 "undefined first argument"); |
|
99 } |
|
100 catch (e) |
|
101 { |
|
102 assertEq(e instanceof TypeError, true, |
|
103 "Didn't get a TypeError for " + c + "." + methodname + |
|
104 " called with null or undefined first argument"); |
|
105 } |
|
106 |
|
107 |
|
108 // PROTOTYPE METHOD TESTING |
|
109 |
|
110 try |
|
111 { |
|
112 prototypy.apply(voids[v], method[methodname][0]); |
|
113 throw new Error(c + ".prototype." + methodname + " must throw " + |
|
114 "for null or undefined this"); |
|
115 } |
|
116 catch (e) |
|
117 { |
|
118 assertEq(e instanceof TypeError, true, |
|
119 c + ".prototype." + methodname + "didn't throw a " + |
|
120 "TypeError when called with null or undefined this"); |
|
121 } |
|
122 } |
|
123 } |
|
124 } |
|
125 } |
|
126 |
|
127 if (typeof reportCompare === "function") |
|
128 reportCompare(true, true); |
|
129 |
|
130 print("Tests finished."); |