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

Side by Side Diff: test/mjsunit/harmony/computed-property-names.js

Issue 795573005: ES6 computed property names (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: git rebase 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 unified diff | Download patch
« no previous file with comments | « test/cctest/test-parsing.cc ('k') | test/mjsunit/harmony/computed-property-names-classes.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Flags: --harmony-computed-property-names
6
7
8 (function TestBasicsString() {
Dmitry Lomov (no reviews) 2014/12/15 10:43:47 Could you also add (more) tests where computed nam
arv (Not doing code reviews) 2014/12/15 14:59:01 I'm not sure it is worth optimizing [literal] sinc
9 var object = {
10 a: 'A',
11 ['b']: 'B',
12 c: 'C',
13 ['d']: 'D',
14 };
15 assertEquals('A', object.a);
16 assertEquals('B', object.b);
17 assertEquals('C', object.c);
18 assertEquals('D', object.d);
19 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object));
20 })();
21
22
23 (function TestBasicsNumber() {
24 var object = {
25 a: 'A',
26 [1]: 'B',
27 c: 'C',
28 [2]: 'D',
29 };
30 assertEquals('A', object.a);
31 assertEquals('B', object[1]);
32 assertEquals('C', object.c);
33 assertEquals('D', object[2]);
34 // Array indexes first.
35 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object));
36 })();
37
38
39 (function TestBasicsSymbol() {
40 var sym1 = Symbol();
41 var sym2 = Symbol();
42 var object = {
43 a: 'A',
44 [sym1]: 'B',
45 c: 'C',
46 [sym2]: 'D',
47 };
48 assertEquals('A', object.a);
49 assertEquals('B', object[sym1]);
50 assertEquals('C', object.c);
51 assertEquals('D', object[sym2]);
52 assertArrayEquals(['a', 'c'], Object.keys(object));
53 assertArrayEquals([sym1, sym2], Object.getOwnPropertySymbols(object));
54 })();
55
56
57 (function TestToNameSideEffects() {
58 var counter = 0;
59 var key1 = {
60 toString: function() {
61 assertEquals(0, counter++);
62 return 'b';
63 }
64 };
65 var key2 = {
66 toString: function() {
67 assertEquals(1, counter++);
68 return 'd';
69 }
70 };
71 var object = {
72 a: 'A',
73 [key1]: 'B',
74 c: 'C',
75 [key2]: 'D',
76 };
77 assertEquals(2, counter);
78 assertEquals('A', object.a);
79 assertEquals('B', object.b);
80 assertEquals('C', object.c);
81 assertEquals('D', object.d);
82 assertArrayEquals(['a', 'b', 'c', 'd'], Object.keys(object));
83 })();
84
85
86 (function TestToNameSideEffectsNumbers() {
87 var counter = 0;
88 var key1 = {
89 valueOf: function() {
90 assertEquals(0, counter++);
91 return 1;
92 },
93 toString: null
94 };
95 var key2 = {
96 valueOf: function() {
97 assertEquals(1, counter++);
98 return 2;
99 },
100 toString: null
101 };
102
103 var object = {
104 a: 'A',
105 [key1]: 'B',
106 c: 'C',
107 [key2]: 'D',
108 };
109 assertEquals(2, counter);
110 assertEquals('A', object.a);
111 assertEquals('B', object[1]);
112 assertEquals('C', object.c);
113 assertEquals('D', object[2]);
114 // Array indexes first.
115 assertArrayEquals(['1', '2', 'a', 'c'], Object.keys(object));
116 })();
117
118
119 (function TestDoubleName() {
120 var object = {
121 [1.2]: 'A',
122 [1e55]: 'B',
123 [0.000001]: 'C',
124 [-0]: 'D',
125 [Infinity]: 'E',
126 [-Infinity]: 'F',
127 [NaN]: 'G',
128 };
129 assertEquals('A', object['1.2']);
130 assertEquals('B', object['1e+55']);
131 assertEquals('C', object['0.000001']);
132 assertEquals('D', object[0]);
133 assertEquals('E', object[Infinity]);
134 assertEquals('F', object[-Infinity]);
135 assertEquals('G', object[NaN]);
136 })();
137
138
139 (function TestGetter() {
140 var object = {
141 get ['a']() {
142 return 'A';
143 }
144 };
145 assertEquals('A', object.a);
146
147 object = {
148 get b() {
149 assertUnreachable();
150 },
151 get ['b']() {
152 return 'B';
153 }
154 };
155 assertEquals('B', object.b);
156
157 object = {
158 get c() {
159 assertUnreachable();
160 },
161 get ['c']() {
162 assertUnreachable();
163 },
164 get ['c']() {
165 return 'C';
166 }
167 };
168 assertEquals('C', object.c);
169
170 object = {
171 get ['d']() {
172 assertUnreachable();
173 },
174 get d() {
175 return 'D';
176 }
177 };
178 assertEquals('D', object.d);
179 })();
180
181
182 (function TestSetter() {
183 var calls = 0;
184 var object = {
185 set ['a'](_) {
186 calls++;
187 }
188 };
189 object.a = 'A';
190 assertEquals(1, calls);
191
192 object = {
Dmitry Lomov (no reviews) 2014/12/15 10:43:47 Nit: here and below, set calls to 0 before executi
arv (Not doing code reviews) 2014/12/15 14:59:01 Done.
193 set b(_) {
194 assertUnreachable();
195 },
196 set ['b'](_) {
197 calls++;
198 }
199 };
200 object.b = 'B';
201 assertEquals(2, calls);
202
203 object = {
204 set c(_) {
205 assertUnreachable()
206 },
207 set ['c'](_) {
208 assertUnreachable()
209 },
210 set ['c'](_) {
211 calls++
212 }
213 };
214 object.c = 'C';
215 assertEquals(3, calls);
216
217 object = {
218 set ['d'](_) {
219 assertUnreachable()
220 },
221 set d(_) {
222 calls++
223 }
224 };
225 object.d = 'D';
226 assertEquals(4, calls);
227 })();
228
229
230 (function TestDuplicateKeys() {
231 'use strict';
232 // ES5 does not allow duplicate keys.
233 // ES6 does but we haven't changed our code yet.
234
235 var object = {
236 a: 1,
237 ['a']: 2,
238 };
239 assertEquals(2, object.a);
240 })();
241
242
243 (function TestProto() {
244 var proto = {};
245 var object = {
246 __proto__: proto
247 };
248 assertEquals(proto, Object.getPrototypeOf(object));
249
250 object = {
251 '__proto__': proto
252 };
253 assertEquals(proto, Object.getPrototypeOf(object));
254
255 var object = {
256 ['__proto__']: proto
257 };
258 assertEquals(Object.prototype, Object.getPrototypeOf(object));
259 assertEquals(proto, object.__proto__);
260 assertTrue(object.hasOwnProperty('__proto__'));
261 })();
OLDNEW
« no previous file with comments | « test/cctest/test-parsing.cc ('k') | test/mjsunit/harmony/computed-property-names-classes.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698