Skip to content
Snippets Groups Projects
Commit bd682d8e authored by Rick Waldron's avatar Rick Waldron
Browse files

Import tests from Google V8 (Computed Property Names)

These tests are derived from the following files within the Google V8
project:

        test/mjsunit/harmony/computed-property-names-classes.js
        test/mjsunit/harmony/computed-property-names-object-literals-methods.js
        test/mjsunit/harmony/computed-property-names-super.js
        test/mjsunit/harmony/computed-property-names.js
parent 2eca2c71
No related branches found
No related tags found
No related merge requests found
Showing
with 479 additions and 0 deletions
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
computed property names can be a number
includes: [compareArray.js]
---*/
function ID(x) {
return x;
}
var object = {
a: 'A',
[1]: 'B',
c: 'C',
[ID(2)]: 'D',
};
assert.sameValue(object.a, 'A');
assert.sameValue(object[1], 'B');
assert.sameValue(object.c, 'C');
assert.sameValue(object[2], 'D');
assert(compareArray(Object.keys(object), ['1', '2', 'a', 'c']));
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
computed property names can be a string
includes: [compareArray.js]
---*/
function ID(x) {
return x;
}
var object = {
a: 'A',
['b']: 'B',
c: 'C',
[ID('d')]: 'D',
};
assert.sameValue(object.a, 'A');
assert.sameValue(object.b, 'B');
assert.sameValue(object.c, 'C');
assert.sameValue(object.d, 'D');
assert(compareArray(Object.keys(object), ['a', 'b', 'c', 'd']));
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
computed property names can be a symbol
includes: [compareArray.js]
---*/
function ID(x) {
return x;
}
var sym1 = Symbol();
var sym2 = Symbol();
var object = {
a: 'A',
[sym1]: 'B',
c: 'C',
[ID(sym2)]: 'D',
};
assert.sameValue(object.a, 'A');
assert.sameValue(object[sym1], 'B');
assert.sameValue(object.c, 'C');
assert.sameValue(object[sym2], 'D');
assert(compareArray(Object.keys(object), ['a', 'c']));
// compareArray expects arguments to be sorted,
// which will cause an array containing symbols to
// throw an exception when toString() is called.
//
// Since there is no guarantee of order:
//
// - Assert only that the symbol is present
// - Assert that the length is correct
//
var symbols = Object.getOwnPropertySymbols(object);
assert(symbols.indexOf(sym1) !== -1);
assert(symbols.indexOf(sym2) !== -1);
assert.sameValue(symbols.length, 2);
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, duplicate computed property getter names produce only a single property of
that name, whose value is the value of the last property of that name.
---*/
class C {
get ['a']() {
return 'A';
}
}
assert.sameValue(new C().a, 'A');
class C2 {
get b() {
assert(false);
}
get ['b']() {
return 'B';
}
}
assert.sameValue(new C2().b, 'B');
class C3 {
get c() {
assert(false);
}
get ['c']() {
assert(false);
}
get ['c']() {
return 'C';
}
}
assert.sameValue(new C3().c, 'C');
class C4 {
get ['d']() {
assert(false);
}
get d() {
return 'D';
}
}
assert.sameValue(new C4().d, 'D');
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
Computed property names for getters
---*/
class C {
get ['a']() {
return 'A';
}
}
assert.sameValue(new C().a, 'A');
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, duplicate computed property setter names produce only a single property of
that name, whose value is the value of the last property of that name.
---*/
var calls = 0;
class C {
set ['a'](_) {
calls++;
}
}
new C().a = 'A';
assert.sameValue(calls, 1);
calls = 0;
class C2 {
set b(_) {
assert(false);
}
set ['b'](_) {
calls++;
}
}
new C2().b = 'B';
assert.sameValue(calls, 1);
calls = 0;
class C3 {
set c(_) {
assert(false)
}
set ['c'](_) {
assert(false)
}
set ['c'](_) {
calls++
}
}
new C3().c = 'C';
assert.sameValue(calls, 1);
calls = 0;
class C4 {
set ['d'](_) {
assert(false)
}
set d(_) {
calls++
}
}
new C4().d = 'D';
assert.sameValue(calls, 1);
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
Computed property names for setters
---*/
var calls = 0;
class C {
set ['a'](_) {
calls++;
}
}
new C().a = 'A';
assert.sameValue(calls, 1);
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
computed property names can be "constructor"
---*/
class C {
['constructor']() {
return 1;
}
}
assert(C !== C.prototype.constructor);
assert.sameValue(new C().constructor(), 1);
class C2 {
get ['constructor']() {
return 2;
}
}
assert.sameValue(new C2().constructor, 2);
var calls = 0;
class C3 {
set ['constructor'](x) {
assert.sameValue(x, 3);
calls++;
}
}
new C3().constructor = 3;
assert.sameValue(calls, 1);
class C4 {
*['constructor']() {
yield 1;
yield 2;
}
}
assert(C4 !== C4.prototype.constructor);
assert.sameValue(new C().constructor(), 1);
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
computed property names can be used as the name of a generator method in a class
includes: [compareArray.js]
---*/
class C {
*['a']() {
yield 1;
yield 2;
}
}
assert.sameValue(Object.keys(C.prototype).length, 0);
assert(compareArray(Object.getOwnPropertyNames(C.prototype), ['constructor', 'a']));
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
computed property class method names can be a number
includes: [compareArray.js]
---*/
function ID(x) {
return x;
}
class C {
a() { return 'A'; }
[1]() { return 'B'; }
c() { return 'C'; }
[ID(2)]() { return 'D'; }
}
assert.sameValue(new C().a(), 'A');
assert.sameValue(new C()[1](), 'B');
assert.sameValue(new C().c(), 'C');
assert.sameValue(new C()[2](), 'D');
assert(compareArray(Object.keys(C.prototype), []));
assert(compareArray(Object.getOwnPropertyNames(C.prototype), ['1', '2', 'constructor', 'a', 'c']));
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
computed property class method names can be a string
includes: [compareArray.js]
---*/
function ID(x) {
return x;
}
class C {
a() { return 'A'}
['b']() { return 'B'; }
c() { return 'C'; }
[ID('d')]() { return 'D'; }
}
assert.sameValue(new C().a(), 'A');
assert.sameValue(new C().b(), 'B');
assert.sameValue(new C().c(), 'C');
assert.sameValue(new C().d(), 'D');
assert(compareArray(Object.keys(C.prototype), []));
assert(compareArray(Object.getOwnPropertyNames(C.prototype), ['constructor', 'a', 'b', 'c', 'd']));
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
computed property class method names can be a symbol
includes: [compareArray.js]
---*/
function ID(x) {
return x;
}
var sym1 = Symbol();
var sym2 = Symbol();
class C {
a() { return 'A'; }
[sym1]() { return 'B'; }
c() { return 'C'; }
[ID(sym2)]() { return 'D'; }
}
assert.sameValue(new C().a(), 'A');
assert.sameValue(new C()[sym1](), 'B');
assert.sameValue(new C().c(), 'C');
assert.sameValue(new C()[sym2](), 'D');
assert(compareArray(Object.keys(C.prototype), []));
assert(compareArray(Object.getOwnPropertyNames(C.prototype), ['constructor', 'a', 'c']));
// compareArray expects arguments to be sorted,
// which will cause an array containing symbols to
// throw an exception when toString() is called.
//
// Since there is no guarantee of order:
//
// - Assert only that the symbol is present
// - Assert that the length is correct
//
var symbols = Object.getOwnPropertySymbols(C.prototype);
assert(symbols.indexOf(sym1) !== -1);
assert(symbols.indexOf(sym2) !== -1);
assert.sameValue(symbols.length, 2);
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, computed property names for static
generators cannot be "constructor"
negative: SyntaxError
---*/
class C {
static * ['constructor']() {}
}
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, computed property names for static
generators cannot be "prototype"
negative: SyntaxError
---*/
class C {
static *['prototype']() {}
}
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, computed property names for static
getters cannot be "constructor"
negative: SyntaxError
---*/
class C {
static get ['constructor']() {}
}
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, computed property names for static
getters cannot be "prototype"
negative: SyntaxError
---*/
class C {
static get ['prototype']() {}
}
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, computed property names for static
methods cannot be "constructor"
negative: SyntaxError
---*/
class C {
static ['constructor']() {}
}
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, static computed property method names can be a number
includes: [compareArray.js]
---*/
class C {
static a() { return 'A'; }
static [1]() { return 'B'; }
static c() { return 'C'; }
static [2]() { return 'D'; }
}
assert.sameValue(C.a(), 'A');
assert.sameValue(C[1](), 'B');
assert.sameValue(C.c(), 'C');
assert.sameValue(C[2](), 'D');
assert(compareArray(Object.keys(C), []));
assert(compareArray(Object.getOwnPropertyNames(C), ['1', '2', 'length', 'name', 'prototype', 'a', 'c']));
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, computed property names for static
methods cannot be "prototype"
negative: SyntaxError
---*/
class C {
static ['prototype']() {}
}
// Copyright (C) Copyright 2014 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 12.2.5
description: >
In a class, static computed property method names can be a string
includes: [compareArray.js]
---*/
class C {
static a() { return 'A'}
static ['b']() { return 'B'; }
static c() { return 'C'; }
static ['d']() { return 'D'; }
}
assert.sameValue(C.a(), 'A');
assert.sameValue(C.b(), 'B');
assert.sameValue(C.c(), 'C');
assert.sameValue(C.d(), 'D');
assert(compareArray(Object.keys(C), []));
assert(compareArray(Object.getOwnPropertyNames(C), ['length', 'name', 'prototype', 'a', 'b', 'c', 'd']));
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment