Index: test/mjsunit/harmony/computed-property-names.js |
diff --git a/test/mjsunit/harmony/computed-property-names.js b/test/mjsunit/harmony/computed-property-names.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..36ce6754d598b28f979f86e305bfd896bf901ff8 |
--- /dev/null |
+++ b/test/mjsunit/harmony/computed-property-names.js |
@@ -0,0 +1,270 @@ |
+// Copyright 2014 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Flags: --harmony-computed-property-names |
+ |
+ |
+function ID(x) { |
+ return x; |
+} |
+ |
+ |
+ |
+(function TestBasicsString() { |
+ var object = { |
+ a: 'A', |
+ ['b']: 'B', |
+ c: 'C', |
+ [ID('d')]: 'D', |
+ }; |
+ assertEquals('A', object.a); |
+ assertEquals('B', object.b); |
+ assertEquals('C', object.c); |
+ assertEquals('D', object.d); |
+ assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object)); |
+})(); |
+ |
+ |
+(function TestBasicsNumber() { |
+ var object = { |
+ a: 'A', |
+ [1]: 'B', |
+ c: 'C', |
+ [ID(2)]: 'D', |
+ }; |
+ assertEquals('A', object.a); |
+ assertEquals('B', object[1]); |
+ assertEquals('C', object.c); |
+ assertEquals('D', object[2]); |
+ // Array indexes first. |
+ assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object)); |
+})(); |
+ |
+ |
+(function TestBasicsSymbol() { |
+ var sym1 = Symbol(); |
+ var sym2 = Symbol(); |
+ var object = { |
+ a: 'A', |
+ [sym1]: 'B', |
+ c: 'C', |
+ [ID(sym2)]: 'D', |
+ }; |
+ assertEquals('A', object.a); |
+ assertEquals('B', object[sym1]); |
+ assertEquals('C', object.c); |
+ assertEquals('D', object[sym2]); |
+ assertArrayEquals(['a', 'c'], Object.keys(object)); |
+ assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(object)); |
+})(); |
+ |
+ |
+(function TestToNameSideEffects() { |
+ var counter = 0; |
+ var key1 = { |
+ toString: function() { |
+ assertEquals(0, counter++); |
+ return 'b'; |
+ } |
+ }; |
+ var key2 = { |
+ toString: function() { |
+ assertEquals(1, counter++); |
+ return 'd'; |
+ } |
+ }; |
+ var object = { |
+ a: 'A', |
+ [key1]: 'B', |
+ c: 'C', |
+ [key2]: 'D', |
+ }; |
+ assertEquals(2, counter); |
+ assertEquals('A', object.a); |
+ assertEquals('B', object.b); |
+ assertEquals('C', object.c); |
+ assertEquals('D', object.d); |
+ assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object)); |
+})(); |
+ |
+ |
+(function TestToNameSideEffectsNumbers() { |
+ var counter = 0; |
+ var key1 = { |
+ valueOf: function() { |
+ assertEquals(0, counter++); |
+ return 1; |
+ }, |
+ toString: null |
+ }; |
+ var key2 = { |
+ valueOf: function() { |
+ assertEquals(1, counter++); |
+ return 2; |
+ }, |
+ toString: null |
+ }; |
+ |
+ var object = { |
+ a: 'A', |
+ [key1]: 'B', |
+ c: 'C', |
+ [key2]: 'D', |
+ }; |
+ assertEquals(2, counter); |
+ assertEquals('A', object.a); |
+ assertEquals('B', object[1]); |
+ assertEquals('C', object.c); |
+ assertEquals('D', object[2]); |
+ // Array indexes first. |
+ assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object)); |
+})(); |
+ |
+ |
+(function TestDoubleName() { |
+ var object = { |
+ [1.2]: 'A', |
+ [1e55]: 'B', |
+ [0.000001]: 'C', |
+ [-0]: 'D', |
+ [Infinity]: 'E', |
+ [-Infinity]: 'F', |
+ [NaN]: 'G', |
+ }; |
+ assertEquals('A', object['1.2']); |
+ assertEquals('B', object['1e+55']); |
+ assertEquals('C', object['0.000001']); |
+ assertEquals('D', object[0]); |
+ assertEquals('E', object[Infinity]); |
+ assertEquals('F', object[-Infinity]); |
+ assertEquals('G', object[NaN]); |
+})(); |
+ |
+ |
+(function TestGetter() { |
+ var object = { |
+ get ['a']() { |
+ return 'A'; |
+ } |
+ }; |
+ assertEquals('A', object.a); |
+ |
+ object = { |
+ get b() { |
+ assertUnreachable(); |
+ }, |
+ get ['b']() { |
+ return 'B'; |
+ } |
+ }; |
+ assertEquals('B', object.b); |
+ |
+ object = { |
+ get c() { |
+ assertUnreachable(); |
+ }, |
+ get ['c']() { |
+ assertUnreachable(); |
+ }, |
+ get ['c']() { |
+ return 'C'; |
+ } |
+ }; |
+ assertEquals('C', object.c); |
+ |
+ object = { |
+ get ['d']() { |
+ assertUnreachable(); |
+ }, |
+ get d() { |
+ return 'D'; |
+ } |
+ }; |
+ assertEquals('D', object.d); |
+})(); |
+ |
+ |
+(function TestSetter() { |
+ var calls = 0; |
+ var object = { |
+ set ['a'](_) { |
+ calls++; |
+ } |
+ }; |
+ object.a = 'A'; |
+ assertEquals(1, calls); |
+ |
+ calls = 0; |
+ object = { |
+ set b(_) { |
+ assertUnreachable(); |
+ }, |
+ set ['b'](_) { |
+ calls++; |
+ } |
+ }; |
+ object.b = 'B'; |
+ assertEquals(1, calls); |
+ |
+ calls = 0; |
+ object = { |
+ set c(_) { |
+ assertUnreachable() |
+ }, |
+ set ['c'](_) { |
+ assertUnreachable() |
+ }, |
+ set ['c'](_) { |
+ calls++ |
+ } |
+ }; |
+ object.c = 'C'; |
+ assertEquals(1, calls); |
+ |
+ calls = 0; |
+ object = { |
+ set ['d'](_) { |
+ assertUnreachable() |
+ }, |
+ set d(_) { |
+ calls++ |
+ } |
+ }; |
+ object.d = 'D'; |
+ assertEquals(1, calls); |
+})(); |
+ |
+ |
+(function TestDuplicateKeys() { |
+ 'use strict'; |
+ // ES5 does not allow duplicate keys. |
+ // ES6 does but we haven't changed our code yet. |
+ |
+ var object = { |
+ a: 1, |
+ ['a']: 2, |
+ }; |
+ assertEquals(2, object.a); |
+})(); |
+ |
+ |
+(function TestProto() { |
+ var proto = {}; |
+ var object = { |
+ __proto__: proto |
+ }; |
+ assertEquals(proto, Object.getPrototypeOf(object)); |
+ |
+ object = { |
+ '__proto__': proto |
+ }; |
+ assertEquals(proto, Object.getPrototypeOf(object)); |
+ |
+ var object = { |
+ ['__proto__']: proto |
+ }; |
+ assertEquals(Object.prototype, Object.getPrototypeOf(object)); |
+ assertEquals(proto, object.__proto__); |
+ assertTrue(object.hasOwnProperty('__proto__')); |
+})(); |