OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 'use strict'; | |
6 | |
7 // Flags: --harmony-computed-property-names | |
8 | |
9 | |
10 function ID(x) { | |
11 return x; | |
12 } | |
13 | |
14 | |
15 (function TestClassMethodString() { | |
16 class C { | |
17 a() { return 'A'} | |
18 ['b']() { return 'B'; } | |
19 c() { return 'C'; } | |
20 [ID('d')]() { return 'D'; } | |
21 } | |
22 assertEquals('A', new C().a()); | |
23 assertEquals('B', new C().b()); | |
24 assertEquals('C', new C().c()); | |
25 assertEquals('D', new C().d()); | |
26 assertArrayEquals([], Object.keys(C.prototype)); | |
27 assertArrayEquals(['constructor', 'a', 'b', 'c', 'd'], | |
28 Object.getOwnPropertyNames(C.prototype)); | |
29 })(); | |
30 | |
31 | |
32 (function TestClassMethodNumber() { | |
33 class C { | |
34 a() { return 'A'; } | |
35 [1]() { return 'B'; } | |
36 c() { return 'C'; } | |
37 [ID(2)]() { return 'D'; } | |
38 } | |
39 assertEquals('A', new C().a()); | |
40 assertEquals('B', new C()[1]()); | |
41 assertEquals('C', new C().c()); | |
42 assertEquals('D', new C()[2]()); | |
43 // Array indexes first. | |
44 assertArrayEquals([], Object.keys(C.prototype)); | |
45 assertArrayEquals(['1', '2', 'constructor', 'a', 'c'], | |
46 Object.getOwnPropertyNames(C.prototype)); | |
47 })(); | |
48 | |
49 | |
50 (function TestClassMethodSymbol() { | |
51 var sym1 = Symbol(); | |
52 var sym2 = Symbol(); | |
53 class C { | |
54 a() { return 'A'; } | |
55 [sym1]() { return 'B'; } | |
56 c() { return 'C'; } | |
57 [ID(sym2)]() { return 'D'; } | |
58 } | |
59 assertEquals('A', new C().a()); | |
60 assertEquals('B', new C()[sym1]()); | |
61 assertEquals('C', new C().c()); | |
62 assertEquals('D', new C()[sym2]()); | |
63 assertArrayEquals([], Object.keys(C.prototype)); | |
64 assertArrayEquals(['constructor', 'a', 'c'], | |
65 Object.getOwnPropertyNames(C.prototype)); | |
66 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(C.prototype)); | |
67 })(); | |
68 | |
69 | |
70 | |
71 (function TestStaticClassMethodString() { | |
72 class C { | |
73 static a() { return 'A'} | |
74 static ['b']() { return 'B'; } | |
75 static c() { return 'C'; } | |
76 static ['d']() { return 'D'; } | |
77 } | |
78 assertEquals('A', C.a()); | |
79 assertEquals('B', C.b()); | |
80 assertEquals('C', C.c()); | |
81 assertEquals('D', C.d()); | |
82 assertArrayEquals([], Object.keys(C)); | |
83 // TODO(arv): It is not clear that we are adding the "standard" properties | |
84 // in the right order. As far as I can tell the spec adds them in alphabetical | |
85 // order. | |
86 assertArrayEquals(['length', 'name', 'prototype', 'a', 'b', 'c', 'd'], | |
87 Object.getOwnPropertyNames(C)); | |
88 })(); | |
89 | |
90 | |
91 (function TestStaticClassMethodNumber() { | |
92 class C { | |
93 static a() { return 'A'; } | |
94 static [1]() { return 'B'; } | |
95 static c() { return 'C'; } | |
96 static [2]() { return 'D'; } | |
97 } | |
98 assertEquals('A', C.a()); | |
99 assertEquals('B', C[1]()); | |
100 assertEquals('C', C.c()); | |
101 assertEquals('D', C[2]()); | |
102 // Array indexes first. | |
103 assertArrayEquals([], Object.keys(C)); | |
104 assertArrayEquals(['1', '2', 'length', 'name', 'prototype', 'a', 'c'], | |
105 Object.getOwnPropertyNames(C)); | |
106 })(); | |
107 | |
108 | |
109 (function TestStaticClassMethodSymbol() { | |
110 var sym1 = Symbol(); | |
111 var sym2 = Symbol(); | |
112 class C { | |
113 static a() { return 'A'; } | |
114 static [sym1]() { return 'B'; } | |
115 static c() { return 'C'; } | |
116 static [sym2]() { return 'D'; } | |
117 } | |
118 assertEquals('A', C.a()); | |
119 assertEquals('B', C[sym1]()); | |
120 assertEquals('C', C.c()); | |
121 assertEquals('D', C[sym2]()); | |
122 assertArrayEquals([], Object.keys(C)); | |
123 assertArrayEquals(['length', 'name', 'prototype', 'a', 'c'], | |
124 Object.getOwnPropertyNames(C)); | |
125 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(C)); | |
126 })(); | |
127 | |
128 | |
129 | |
130 function assertIteratorResult(value, done, result) { | |
131 assertEquals({ value: value, done: done}, result); | |
132 } | |
133 | |
134 | |
135 (function TestGeneratorComputedName() { | |
136 class C { | |
137 *['a']() { | |
138 yield 1; | |
139 yield 2; | |
140 } | |
141 } | |
142 var iter = new C().a(); | |
143 assertIteratorResult(1, false, iter.next()); | |
144 assertIteratorResult(2, false, iter.next()); | |
145 assertIteratorResult(undefined, true, iter.next()); | |
146 assertArrayEquals([], Object.keys(C.prototype)); | |
147 assertArrayEquals(['constructor', 'a'], | |
148 Object.getOwnPropertyNames(C.prototype)); | |
149 })(); | |
150 | |
151 | |
152 (function TestToNameSideEffects() { | |
153 var counter = 0; | |
154 var key1 = { | |
155 toString: function() { | |
156 assertEquals(0, counter++); | |
157 return 'b'; | |
158 } | |
159 }; | |
160 var key2 = { | |
161 toString: function() { | |
162 assertEquals(1, counter++); | |
163 return 'd'; | |
164 } | |
165 }; | |
166 class C { | |
167 a() { return 'A'; } | |
168 [key1]() { return 'B'; } | |
169 c() { return 'C'; } | |
170 [key2]() { return 'D'; } | |
171 } | |
172 assertEquals(2, counter); | |
173 assertEquals('A', new C().a()); | |
174 assertEquals('B', new C().b()); | |
175 assertEquals('C', new C().c()); | |
176 assertEquals('D', new C().d()); | |
177 assertArrayEquals([], Object.keys(C.prototype)); | |
178 assertArrayEquals(['constructor', 'a', 'b', 'c', 'd'], | |
179 Object.getOwnPropertyNames(C.prototype)); | |
180 })(); | |
181 | |
182 | |
183 (function TestToNameSideEffectsNumbers() { | |
184 var counter = 0; | |
185 var key1 = { | |
186 valueOf: function() { | |
187 assertEquals(0, counter++); | |
188 return 1; | |
189 }, | |
190 toString: null | |
191 }; | |
192 var key2 = { | |
193 valueOf: function() { | |
194 assertEquals(1, counter++); | |
195 return 2; | |
196 }, | |
197 toString: null | |
198 }; | |
199 | |
200 class C { | |
201 a() { return 'A'; } | |
202 [key1]() { return 'B'; } | |
203 c() { return 'C'; } | |
204 [key2]() { return 'D'; } | |
205 } | |
206 assertEquals(2, counter); | |
207 assertEquals('A', new C().a()); | |
208 assertEquals('B', new C()[1]()); | |
209 assertEquals('C', new C().c()); | |
210 assertEquals('D', new C()[2]()); | |
211 // Array indexes first. | |
212 assertArrayEquals([], Object.keys(C.prototype)); | |
213 assertArrayEquals(['1', '2', 'constructor', 'a', 'c'], | |
214 Object.getOwnPropertyNames(C.prototype)); | |
215 })(); | |
216 | |
217 | |
218 (function TestGetter() { | |
219 class C { | |
220 get ['a']() { | |
221 return 'A'; | |
222 } | |
223 } | |
224 assertEquals('A', new C().a); | |
225 | |
226 class C2 { | |
227 get b() { | |
228 assertUnreachable(); | |
229 } | |
230 get ['b']() { | |
231 return 'B'; | |
232 } | |
233 } | |
234 assertEquals('B', new C2().b); | |
235 | |
236 class C3 { | |
237 get c() { | |
238 assertUnreachable(); | |
239 } | |
240 get ['c']() { | |
241 assertUnreachable(); | |
242 } | |
243 get ['c']() { | |
244 return 'C'; | |
245 } | |
246 } | |
247 assertEquals('C', new C3().c); | |
248 | |
249 class C4 { | |
250 get ['d']() { | |
251 assertUnreachable(); | |
252 } | |
253 get d() { | |
254 return 'D'; | |
255 } | |
256 } | |
257 assertEquals('D', new C4().d); | |
258 })(); | |
259 | |
260 | |
261 (function TestSetter() { | |
262 var calls = 0; | |
263 class C { | |
264 set ['a'](_) { | |
265 calls++; | |
266 } | |
267 } | |
268 new C().a = 'A'; | |
269 assertEquals(1, calls); | |
270 | |
271 calls = 0; | |
272 class C2 { | |
273 set b(_) { | |
274 assertUnreachable(); | |
275 } | |
276 set ['b'](_) { | |
277 calls++; | |
278 } | |
279 } | |
280 new C2().b = 'B'; | |
281 assertEquals(1, calls); | |
282 | |
283 calls = 0; | |
284 class C3 { | |
285 set c(_) { | |
286 assertUnreachable() | |
287 } | |
288 set ['c'](_) { | |
289 assertUnreachable() | |
290 } | |
291 set ['c'](_) { | |
292 calls++ | |
293 } | |
294 } | |
295 new C3().c = 'C'; | |
296 assertEquals(1, calls); | |
297 | |
298 calls = 0; | |
299 class C4 { | |
300 set ['d'](_) { | |
301 assertUnreachable() | |
302 } | |
303 set d(_) { | |
304 calls++ | |
305 } | |
306 } | |
307 new C4().d = 'D'; | |
308 assertEquals(1, calls); | |
309 })(); | |
310 | |
311 | |
312 (function TestPrototype() { | |
313 assertThrows(function() { | |
314 class C { | |
315 static ['prototype']() { | |
316 return 1; | |
317 } | |
318 } | |
319 }, TypeError); | |
320 | |
321 assertThrows(function() { | |
322 class C2 { | |
323 static get ['prototype']() { | |
324 return 2; | |
325 } | |
326 } | |
327 }, TypeError); | |
328 | |
329 assertThrows(function() { | |
330 class C3 { | |
331 static set ['prototype'](x) { | |
332 assertEquals(3, x); | |
333 } | |
334 } | |
335 }, TypeError); | |
336 | |
337 assertThrows(function() { | |
338 class C4 { | |
339 static *['prototype']() { | |
340 yield 1; | |
341 yield 2; | |
342 } | |
343 } | |
344 }, TypeError); | |
345 })(); | |
346 | |
347 | |
348 (function TestPrototypeConcat() { | |
349 assertThrows(function() { | |
350 class C { | |
351 static ['pro' + 'tot' + 'ype']() { | |
352 return 1; | |
353 } | |
354 } | |
355 }, TypeError); | |
356 | |
357 assertThrows(function() { | |
358 class C2 { | |
359 static get ['pro' + 'tot' + 'ype']() { | |
360 return 2; | |
361 } | |
362 } | |
363 }, TypeError); | |
364 | |
365 assertThrows(function() { | |
366 class C3 { | |
367 static set ['pro' + 'tot' + 'ype'](x) { | |
368 assertEquals(3, x); | |
369 } | |
370 } | |
371 }, TypeError); | |
372 | |
373 assertThrows(function() { | |
374 class C4 { | |
375 static *['pro' + 'tot' + 'ype']() { | |
376 yield 1; | |
377 yield 2; | |
378 } | |
379 } | |
380 }, TypeError); | |
381 })(); | |
382 | |
383 | |
384 (function TestConstructor() { | |
385 // Normally a constructor property is not allowed. | |
386 class C { | |
387 ['constructor']() { | |
388 return 1; | |
389 } | |
390 } | |
391 assertTrue(C !== C.prototype.constructor); | |
392 assertEquals(1, new C().constructor()); | |
393 | |
394 class C2 { | |
395 get ['constructor']() { | |
396 return 2; | |
397 } | |
398 } | |
399 assertEquals(2, new C2().constructor); | |
400 | |
401 var calls = 0; | |
402 class C3 { | |
403 set ['constructor'](x) { | |
404 assertEquals(3, x); | |
405 calls++; | |
406 } | |
407 } | |
408 new C3().constructor = 3; | |
409 assertEquals(1, calls); | |
410 | |
411 class C4 { | |
412 *['constructor']() { | |
413 yield 1; | |
414 yield 2; | |
415 } | |
416 } | |
417 var iter = new C4().constructor(); | |
418 assertIteratorResult(1, false, iter.next()); | |
419 assertIteratorResult(2, false, iter.next()); | |
420 assertIteratorResult(undefined, true, iter.next()); | |
421 })(); | |
422 | |
423 | |
424 (function TestExceptionInName() { | |
425 function MyError() {}; | |
426 function throwMyError() { | |
427 throw new MyError(); | |
428 } | |
429 assertThrows(function() { | |
430 class C { | |
431 [throwMyError()]() {} | |
432 } | |
433 }, MyError); | |
434 assertThrows(function() { | |
435 class C { | |
436 get [throwMyError()]() { return 42; } | |
437 } | |
438 }, MyError); | |
439 assertThrows(function() { | |
440 class C { | |
441 set [throwMyError()](_) { } | |
442 } | |
443 }, MyError); | |
444 })(); | |
445 | |
446 | |
447 (function TestTdzName() { | |
448 assertThrows(function() { | |
449 class C { | |
450 [C]() {} | |
451 } | |
452 }, ReferenceError); | |
453 assertThrows(function() { | |
454 class C { | |
455 get [C]() { return 42; } | |
456 } | |
457 }, ReferenceError); | |
458 assertThrows(function() { | |
459 class C { | |
460 set [C](_) { } | |
461 } | |
462 }, ReferenceError); | |
463 })(); | |
OLD | NEW |