Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Unified Diff: test/mjsunit/harmony/computed-property-names-object-literals-methods.js

Issue 792233008: ES6 computed property names (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/mjsunit/harmony/computed-property-names-classes.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/harmony/computed-property-names-object-literals-methods.js
diff --git a/test/mjsunit/harmony/computed-property-names-object-literals-methods.js b/test/mjsunit/harmony/computed-property-names-object-literals-methods.js
new file mode 100644
index 0000000000000000000000000000000000000000..135d09854e573c8e67a8e719e0a397b8bec04ff9
--- /dev/null
+++ b/test/mjsunit/harmony/computed-property-names-object-literals-methods.js
@@ -0,0 +1,121 @@
+// 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 --harmony-object-literals
+
+
+function ID(x) {
+ return x;
+}
+
+
+(function TestMethodComputedNameString() {
+ var object = {
+ a() { return 'A'},
+ ['b']() { return 'B'; },
+ c() { return 'C'; },
+ [ID('d')]() { return '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 TestMethodComputedNameNumber() {
+ var object = {
+ a() { return 'A'; },
+ [1]() { return 'B'; },
+ c() { return 'C'; },
+ [ID(2)]() { return '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 TestMethodComputedNameSymbol() {
+ var sym1 = Symbol();
+ var sym2 = Symbol();
+ var object = {
+ a() { return 'A'; },
+ [sym1]() { return 'B'; },
+ c() { return 'C'; },
+ [ID(sym2)]() { return '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 assertIteratorResult(value, done, result) {
+ assertEquals({ value: value, done: done}, result);
+}
+
+
+(function TestGeneratorComputedName() {
+ var object = {
+ *['a']() {
+ yield 1;
+ yield 2;
+ }
+ };
+ var iter = object.a();
+ assertIteratorResult(1, false, iter.next());
+ assertIteratorResult(2, false, iter.next());
+ assertIteratorResult(undefined, true, iter.next());
+ assertArrayEquals(['a'], Object.keys(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() { return 'A'; },
+ [key1]() { return 'B'; },
+ c() { return 'C'; },
+ [key2]() { return '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 TestDuplicateKeys() {
+ 'use strict';
+ // ES5 does not allow duplicate keys.
+ // ES6 does but we haven't changed our code yet.
+
+ var object = {
+ a() { return 1; },
+ ['a']() { return 2; },
+ };
+ assertEquals(2, object.a());
+})();
« no previous file with comments | « test/mjsunit/harmony/computed-property-names-classes.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698