]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
1 | // WeakMap constructor behaviors. |
2 | ||
3 | if (typeof WeakMap !== 'function') | |
4 | throw "Error: bad value" + typeof WeakMap; | |
5 | ||
6 | function testCallTypeError(item) { | |
7 | var error = null; | |
8 | try { | |
9 | var map = WeakMap(item); | |
10 | } catch (e) { | |
11 | error = e; | |
12 | } | |
13 | if (!error) | |
14 | throw "Error: error not thrown"; | |
15 | if (String(error) !== "TypeError: WeakMap cannot be called as a function") | |
16 | throw "Error: bad error " + String(error); | |
17 | } | |
18 | var obj1 = {}; | |
19 | var obj2 = []; | |
20 | var obj3 = new Date(); | |
21 | var obj4 = new Error(); | |
22 | ||
23 | var pass = [ | |
24 | null, | |
25 | undefined, | |
26 | [], | |
27 | new Set(), | |
28 | new Map(), | |
29 | "", | |
30 | ||
31 | [ | |
32 | [obj1, 1], | |
33 | [obj2, 2], | |
34 | [obj3, 3], | |
35 | ], | |
36 | ||
37 | [ | |
38 | [obj1, 1], | |
39 | [obj1, 2], | |
40 | [obj1, 3], | |
41 | ], | |
42 | ||
43 | [ | |
44 | { 0: obj2, 1: 'O' }, | |
45 | { 0: obj3, 1: 'K' }, | |
46 | { 0: obj4, 1: 'K' }, | |
47 | ], | |
48 | ||
49 | new Map([ | |
50 | [obj1, 1], | |
51 | [obj2, 2], | |
52 | [obj3, 3], | |
53 | ]), | |
54 | ||
55 | new Map([ | |
56 | [obj1, 1], | |
57 | [obj1, 2], | |
58 | [obj1, 3], | |
59 | ]), | |
60 | ||
61 | new Map([ | |
62 | { 0: obj2, 1: 'O' }, | |
63 | { 0: obj3, 1: 'K' }, | |
64 | { 0: obj4, 1: 'K' }, | |
65 | ]), | |
66 | ]; | |
67 | ||
68 | for (var value of pass) { | |
69 | var map = new WeakMap(value); | |
70 | testCallTypeError(value); | |
71 | } | |
72 | ||
73 | function testTypeError(item, message) { | |
74 | var error = null; | |
75 | try { | |
76 | var map = new WeakMap(item); | |
77 | } catch (e) { | |
78 | error = e; | |
79 | } | |
80 | if (!error) | |
81 | throw "Error: error not thrown"; | |
82 | if (!message) | |
83 | message = "TypeError: Type error"; | |
84 | if (String(error) !== message) | |
85 | throw "Error: bad error " + String(error); | |
86 | } | |
87 | ||
88 | var nonIterable = [ | |
89 | 42, | |
90 | Symbol("Cappuccino"), | |
91 | true, | |
92 | false, | |
93 | {}, | |
94 | new Date(), | |
95 | new Error(), | |
96 | Object(Symbol("Matcha")), | |
97 | (function () { }), | |
98 | ]; | |
99 | ||
100 | for (var item of nonIterable) { | |
101 | testTypeError(item); | |
102 | testCallTypeError(item); | |
103 | } | |
104 | ||
105 | var notContainNextItem = [ | |
106 | "Cocoa", | |
107 | [0, 1, 2, 3, 4], | |
108 | [0, 0, 0, 1, 0], | |
109 | ["A", "B", "A"], | |
110 | new String("cocoa"), | |
111 | new String("Cocoa"), | |
112 | new Set([0,1,2,3,4]), | |
113 | new Set([1,1,1,1]), | |
114 | ]; | |
115 | ||
116 | for (var item of notContainNextItem) { | |
117 | testTypeError(item); | |
118 | testCallTypeError(item); | |
119 | } | |
120 | ||
121 | var nonObjectKeys = [ | |
122 | [ | |
123 | [0, 1], | |
124 | [1, 2], | |
125 | [1, 3], | |
126 | ], | |
127 | ||
128 | [ | |
129 | [1, 1], | |
130 | [1, 2], | |
131 | [1, 3], | |
132 | ], | |
133 | ||
134 | [ | |
135 | { 0: 'C', 1: 'O' }, | |
136 | { 0: 'C', 1: 'K' }, | |
137 | { 0: 'V', 1: 'K' }, | |
138 | ], | |
139 | ]; | |
140 | ||
141 | for (var item of nonObjectKeys) { | |
142 | testTypeError(item, 'TypeError: Attempted to set a non-object key in a WeakMap'); | |
143 | testCallTypeError(item); | |
144 | } |