Chromium Code Reviews| Index: test/mjsunit/object-literal-computed-names.js |
| diff --git a/test/mjsunit/object-literal-computed-names.js b/test/mjsunit/object-literal-computed-names.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e7813aff3ee645f236a1d548480de4ea101a7d90 |
| --- /dev/null |
| +++ b/test/mjsunit/object-literal-computed-names.js |
| @@ -0,0 +1,69 @@ |
| +// 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. |
| + |
| +// Test computed properties syntax. |
|
Michael Starzinger
2014/06/13 08:14:19
This file should definitely go into the "harmony"
wingo
2014/06/16 08:32:27
Done.
|
| + |
| +// Flags: --harmony-symbols --harmony-strings |
| + |
| +function assertObjectEquals(expected, actual) { |
| + var expected_props = Object.getOwnPropertyNames(expected); |
| + var actual_props = Object.getOwnPropertyNames(actual); |
| + expected_props += Object.getOwnPropertySymbols(expected); |
| + actual_props += Object.getOwnPropertySymbols(actual); |
| + |
| + assertEquals(expected_props.length, actual_props.length); |
| + for (var i = 0; i < expected_props.length; i++) { |
| + var prop = expected_props[i]; |
| + assertEquals(prop, actual_props[i]); |
| + assertEquals(expected[prop], actual[prop]); |
| + } |
| +} |
| + |
| +function preparse(str, strict) { |
| + // Trigger the preparser by fooling it into thinking that the |
| + // function is really big. |
|
marja
2014/06/13 14:10:37
This is not lazily parsed (i.e., parsed with PrePa
|
| + var buffer = '/* ' + ' '.repeat(4096) + '*/'; |
| + var body = (strict ? '"use strict"; ' : '') + 'return ' + buffer + str; |
| + return Function(body)(); |
| +} |
| + |
| +function parse(str, strict) { |
| + var indirect_eval = eval; |
| + var body = (strict ? '"use strict"; ' : '') + '(' + str + ')'; |
| + return indirect_eval(body); |
| +} |
| + |
| +function test(expected, str) { |
| + assertObjectEquals(expected, parse(str, true)); |
| + assertObjectEquals(expected, parse(str, false)); |
| + assertObjectEquals(expected, preparse(str, true)); |
| + assertObjectEquals(expected, preparse(str, false)); |
| +} |
| + |
| +var sym = Symbol(); |
| +var expected_with_sym = {}; |
| +expected_with_sym[sym] = 'ohai'; |
| + |
| +test({foo: 42}, "{['foo']: 42}"); |
| +test({foo: 10}, "{['foo']: 42, foo: 10}"); |
| +test({foo: 42}, "{foo: 10, ['foo']: 42}"); |
| +test({foo: 42, bar: 35}, "{foo: 10, ['foo']: 42, bar: 35}"); |
| +test({foo: 42, bar: 35}, "{foo: 10, bar: 35, ['foo']: 42}"); |
| +test({bar: 35, foo: 42}, "{bar: 35, foo: 10, ['foo']: 42}"); |
| +test({bar: 35, foo: 10}, "{bar: 35, ['foo']: 42, foo: 10}"); |
| +test({bar: 35, foo: 10}, "{['bar']: 35, ['foo']: 42, foo: 10}"); |
| +test({bar: 35, foo: 42}, "{['bar']: 35, foo: 10, ['foo']: 42}"); |
| +test({foo: 42}, "{foo: 10, [1 ? 'foo' : 'bar']: 42}"); |
| +test({foo: 10, bar: 42}, "{foo: 10, [0 ? 'foo' : 'bar']: 42}"); |
| +test(expected_with_sym, "{[sym]: 'ohai'}"); |
| +test(expected_with_sym, "{[1 ? sym : 'foo']: 'ohai'}"); |
| +test({foo: 'ohai'}, "{[0 ? sym : 'foo']: 'ohai'}"); |
| +test({foo: 42}, "{['foo']: 10, get foo() { return 42 }}"); |
| +test({foo: 10}, "{get foo() { return 42 }, ['foo']: 10}"); |
| + |
| +var test_proto = {}; |
| +var test_obj = {['__proto__']: test_proto}; |
| +assertEquals(Object.prototype, Object.getPrototypeOf(test_obj)); |
| +assertTrue(test_obj.hasOwnProperty('__proto__')); |
| +assertEquals(test_proto, test_obj.__proto__); |