OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-reflect --harmony | |
6 | |
7 // TODO(neis): Test with proxies. | |
8 | |
9 | |
10 | |
11 //////////////////////////////////////////////////////////////////////////////// | |
12 // (Auxiliaries) | |
13 | |
14 | |
15 var global = this; | |
16 | |
17 var sym = Symbol("gaga"); | |
18 | |
19 var objects = [ | |
20 {}, | |
21 [], | |
22 function() {}, | |
23 function() { | |
24 return arguments; | |
25 }(), | |
26 function() { | |
27 'use strict'; | |
28 return arguments; | |
29 }(), | |
30 Object(1), | |
31 Object(true), | |
32 Object('bla'), | |
33 new Date, | |
34 new RegExp, | |
35 new Set, | |
36 new Map, | |
37 new WeakMap, | |
38 new WeakSet, | |
39 new ArrayBuffer(10), | |
40 new Int32Array(5), | |
41 Object, | |
42 Function, | |
43 Date, | |
44 RegExp, | |
45 global | |
46 ]; | |
47 | |
48 function prepare(tgt) { | |
49 tgt["bla"] = true; | |
50 tgt[4] = 42; | |
51 tgt[sym] = "foo"; | |
52 tgt["noconf"] = 43; | |
53 Object.defineProperty(tgt, "noconf", {configurable: false}); | |
54 return tgt; | |
55 } | |
56 | |
57 | |
58 | |
59 //////////////////////////////////////////////////////////////////////////////// | |
60 // Reflect.get | |
61 | |
62 | |
63 (function testReflectGetArity() { | |
64 assertEquals(3, Reflect.get.length); | |
65 })(); | |
66 | |
67 | |
68 (function testReflectGetOnNonObject() { | |
69 assertThrows(function() { Reflect.get(); }, TypeError); | |
70 assertThrows(function() { Reflect.get(42, "bla"); }, TypeError); | |
71 assertThrows(function() { Reflect.get(null, "bla"); }, TypeError); | |
72 })(); | |
73 | |
74 | |
75 (function testReflectGetKeyConversion() { | |
76 var tgt = {bla: 42}; | |
77 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; | |
78 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; | |
79 assertEquals(42, Reflect.get(tgt, a)); | |
80 assertThrows(function() { Reflect.get(tgt, b); }, "gaga"); | |
81 })(); | |
82 | |
83 | |
84 (function testReflectGetOnObject() { | |
85 for (let obj of objects) { | |
86 var tgt = prepare(obj); | |
rossberg
2015/10/07 11:15:02
Nit: no need for this extra binding, or 'prepare'
| |
87 assertEquals(true, Reflect.get(tgt, "bla")); | |
88 assertEquals(42, Reflect.get(tgt, 4)); | |
89 assertEquals(42, Reflect.get(tgt, "4")); | |
90 assertEquals("foo", Reflect.get(tgt, sym)); | |
91 assertEquals(undefined, Reflect.get(tgt, "doesnotexist")); | |
92 assertEquals(undefined, Reflect.get(tgt, 666)); | |
93 } | |
94 })(); | |
95 | |
96 | |
97 | |
98 //////////////////////////////////////////////////////////////////////////////// | |
99 // Reflect.has | |
100 | |
101 | |
102 (function testReflectHasArity() { | |
103 assertEquals(2, Reflect.has.length); | |
104 })(); | |
105 | |
106 | |
107 (function testReflectHasOnNonObject() { | |
108 assertThrows(function() { Reflect.has(); }, TypeError); | |
109 assertThrows(function() { Reflect.has(42, "bla"); }, TypeError); | |
110 assertThrows(function() { Reflect.has(null, "bla"); }, TypeError); | |
111 })(); | |
112 | |
113 | |
114 (function testReflectHasKeyConversion() { | |
115 var tgt = {bla: 42}; | |
116 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; | |
117 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; | |
118 assertTrue(Reflect.has(tgt, a)); | |
119 assertThrows(function() { Reflect.has(tgt, b); }, "gaga"); | |
120 })(); | |
121 | |
122 | |
123 (function testReflectHasOnObject() { | |
124 for (let obj of objects) { | |
125 var tgt = prepare(obj); | |
126 assertTrue(Reflect.has(tgt, "bla")); | |
127 assertTrue(Reflect.has(tgt, 4)); | |
128 assertTrue(Reflect.has(tgt, "4")); | |
129 assertTrue(Reflect.has(tgt, sym)); | |
130 assertFalse(Reflect.has(tgt, "doesnotexist")); | |
131 assertFalse(Reflect.has(tgt, 666)); | |
132 } | |
133 })(); | |
134 | |
135 | |
136 | |
137 //////////////////////////////////////////////////////////////////////////////// | |
138 // Reflect.deleteProperty | |
139 | |
140 | |
141 (function testReflectDeletePropertyArity() { | |
142 assertEquals(2, Reflect.deleteProperty.length); | |
143 })(); | |
144 | |
145 | |
146 (function testReflectDeletePropertyOnNonObject() { | |
147 assertThrows(function() { Reflect.deleteProperty(); }, TypeError); | |
148 assertThrows(function() { Reflect.deleteProperty(42, "bla"); }, TypeError); | |
149 assertThrows(function() { Reflect.deleteProperty(null, "bla"); }, TypeError); | |
150 })(); | |
151 | |
152 | |
153 (function testReflectDeletePropertyKeyConversion() { | |
154 var tgt = {bla: 42}; | |
155 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; | |
156 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; | |
157 assertTrue(Reflect.deleteProperty(tgt, a)); | |
158 assertThrows(function() { Reflect.deleteProperty(tgt, b); }, "gaga"); | |
159 })(); | |
160 | |
161 | |
162 (function testReflectDeletePropertyOnObject() { | |
163 for (let obj of objects) { | |
164 var tgt = prepare(obj); | |
165 assertTrue(Reflect.deleteProperty(tgt, "bla")); | |
166 assertEquals(undefined, Object.getOwnPropertyDescriptor(tgt, "bla")); | |
167 if (tgt instanceof Int32Array) { | |
168 assertFalse(Reflect.deleteProperty(tgt, 4)); | |
169 } else { | |
170 assertTrue(Reflect.deleteProperty(tgt, 4)); | |
171 assertEquals(undefined, Object.getOwnPropertyDescriptor(tgt, 4)); | |
172 } | |
173 assertTrue(Reflect.deleteProperty(tgt, sym)); | |
174 assertEquals(undefined, Object.getOwnPropertyDescriptor(tgt, sym)); | |
175 assertTrue(Reflect.deleteProperty(tgt, "doesnotexist")); | |
176 assertTrue(Reflect.deleteProperty(tgt, 666)); | |
177 assertFalse(Reflect.deleteProperty(tgt, "noconf")); | |
178 assertEquals(43, tgt.noconf); | |
179 } | |
180 })(); | |
181 | |
182 | |
183 | |
184 //////////////////////////////////////////////////////////////////////////////// | |
185 // Reflect.isExtensible | |
186 | |
187 | |
188 (function testReflectIsExtensibleArity() { | |
189 assertEquals(1, Reflect.isExtensible.length); | |
190 })(); | |
191 | |
192 | |
193 (function testReflectIsExtensibleOnNonObject() { | |
194 assertThrows(function() { Reflect.isExtensible(); }, TypeError); | |
195 assertThrows(function() { Reflect.isExtensible(42); }, TypeError); | |
196 assertThrows(function() { Reflect.isExtensible(null); }, TypeError); | |
197 })(); | |
198 | |
199 | |
200 (function testReflectIsExtensibleOnObject() { | |
201 // This should be the last test as it modifies the objects irreversibly. | |
202 for (let obj of objects) { | |
203 var tgt = prepare(obj); | |
204 if (tgt instanceof Int32Array) continue; // issue v8:4460 | |
205 assertTrue(Reflect.isExtensible(tgt)); | |
206 Object.preventExtensions(tgt); | |
207 assertFalse(Reflect.isExtensible(tgt)); | |
208 } | |
209 })(); | |
OLD | NEW |