|
1 /* |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/licenses/publicdomain/ |
|
4 * Contributor: |
|
5 * Jeff Walden <jwalden+code@mit.edu> |
|
6 */ |
|
7 |
|
8 //----------------------------------------------------------------------------- |
|
9 var BUGNUMBER = 575535; |
|
10 var summary = 'Function.prototype.call'; |
|
11 print(BUGNUMBER + ": " + summary); |
|
12 |
|
13 /************** |
|
14 * BEGIN TEST * |
|
15 **************/ |
|
16 |
|
17 function expectTypeError(fun, msg) |
|
18 { |
|
19 try |
|
20 { |
|
21 fun(); |
|
22 assertEq(true, false, "should have thrown a TypeError"); |
|
23 } |
|
24 catch (e) |
|
25 { |
|
26 assertEq(e instanceof TypeError, true, msg + "; instead threw " + e); |
|
27 } |
|
28 } |
|
29 |
|
30 function fun() { } |
|
31 |
|
32 var global = this; |
|
33 |
|
34 assertEq(Function.prototype.call.length, 1); |
|
35 |
|
36 |
|
37 /* Step 1. */ |
|
38 var nonfuns = [null, 1, -1, 2.5, "[[Call]]", undefined, true, false, {}]; |
|
39 for (var i = 0, sz = nonfuns.length; i < sz; i++) |
|
40 { |
|
41 var f = function() |
|
42 { |
|
43 Function.prototype.call.apply(nonfuns[i]); |
|
44 }; |
|
45 var msg = |
|
46 "expected TypeError calling Function.prototype.call with uncallable this"; |
|
47 expectTypeError(f, msg); |
|
48 } |
|
49 |
|
50 |
|
51 /* Steps 2-4. */ |
|
52 function none() |
|
53 { |
|
54 assertEq(this, global, "bad this"); |
|
55 assertEq(arguments.length, 0, "wrong arguments"); |
|
56 } |
|
57 |
|
58 none.call(); |
|
59 none.call(undefined); |
|
60 none.call(null); |
|
61 |
|
62 var seenThis; |
|
63 function strictNone() |
|
64 { |
|
65 "use strict"; |
|
66 assertEq(this, seenThis, "bad this"); |
|
67 assertEq(arguments.length, 0, "wrong arguments"); |
|
68 } |
|
69 |
|
70 seenThis = undefined; |
|
71 strictNone.call(); |
|
72 strictNone.call(undefined); |
|
73 |
|
74 seenThis = null; |
|
75 strictNone.call(null); |
|
76 |
|
77 seenThis = 17; |
|
78 strictNone.call(17); |
|
79 |
|
80 var seenThisBox, args; |
|
81 function some() |
|
82 { |
|
83 assertEq(this instanceof seenThisBox, true, |
|
84 "this not instanceof " + seenThisBox); |
|
85 assertEq(this.valueOf(), seenThis, |
|
86 "wrong this valueOf()"); |
|
87 assertEq(arguments.length, args.length, "wrong arguments count"); |
|
88 for (var i = 0; i < args.length; i++) |
|
89 assertEq(arguments[i], args[i], "wrong argument " + i); |
|
90 } |
|
91 |
|
92 seenThis = false; |
|
93 seenThisBox = Boolean; |
|
94 args = [8, 6, 7, NaN, undefined, 0.3]; |
|
95 some.call(false, 8, 6, 7, NaN, undefined, 0.3); |
|
96 |
|
97 var obj = {}; |
|
98 |
|
99 seenThis = "foo"; |
|
100 seenThisBox = String; |
|
101 args = [obj]; |
|
102 some.call("foo", obj); |
|
103 |
|
104 seenThis = obj; |
|
105 seenThisBox = Object; |
|
106 some.call(obj, obj); |
|
107 |
|
108 function strictSome() |
|
109 { |
|
110 "use strict"; |
|
111 assertEq(this, seenThis, "wrong this"); |
|
112 assertEq(arguments.length, args.length, "wrong arguments count"); |
|
113 for (var i = 0; i < args.length; i++) |
|
114 assertEq(arguments[i], args[i], "wrong argument " + i); |
|
115 } |
|
116 |
|
117 seenThis = NaN; |
|
118 args = [8, 6, 7, NaN, undefined, 0.3]; |
|
119 strictSome.call(NaN, 8, 6, 7, NaN, undefined, 0.3); |
|
120 |
|
121 seenThis = "foo"; |
|
122 args = [obj]; |
|
123 strictSome.call("foo", obj); |
|
124 |
|
125 seenThis = obj; |
|
126 strictSome.call(obj, obj); |
|
127 |
|
128 |
|
129 /******************************************************************************/ |
|
130 |
|
131 if (typeof reportCompare === "function") |
|
132 reportCompare(true, true); |
|
133 |
|
134 print("All tests passed!"); |