blob: 6f0b40cd5fef2b68773eb88bbc581988130c0ae1 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1. Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024description('Tests for the descriptors of the properties implicitly defined by ES6 class syntax');
25
26function descriptor(object, propertyName) {
27 return Object.getOwnPropertyDescriptor(object, propertyName);
28}
29
30function enumeratedProperties(object) {
31 var properties = [];
32 for (var propertyName in object)
33 properties.push(propertyName);
34 return properties;
35}
36
37Array.prototype.includes = function(element) {
38 return this.indexOf(element) !== -1;
39};
40
41shouldBeFalse('class A {}; descriptor(A, "prototype").writable');
42shouldBe('class A {}; var x = A.prototype; A.prototype = 3; A.prototype', 'x');
43shouldBeFalse('class A {}; descriptor(A, "prototype").enumerable');
44shouldBeTrue('class A {}; A.foo = "foo"; enumeratedProperties(A).includes("foo")');
45shouldBeFalse('class A {}; enumeratedProperties(A).includes("prototype")');
46shouldBeFalse('class A {}; descriptor(A, "prototype").configurable');
47shouldThrow('class A {}; Object.defineProperty(A, "prototype", {value: "foo"})', '"TypeError: Cannot redefine property: prototype"');
48
49shouldBeTrue('class A { static foo() {} }; descriptor(A, "foo").writable');
50shouldBe('class A { static foo() {} }; A.foo = 3; A.foo', '3');
51shouldBeFalse('class A { static foo() {} }; descriptor(A, "foo").enumerable');
52shouldBeFalse('class A { static foo() {} }; enumeratedProperties(A).includes("foo")');
53shouldBeTrue('class A { static foo() {} }; descriptor(A, "foo").configurable');
54shouldBe('class A { static foo() {} }; Object.defineProperty(A, "foo", {value: "bar"}); A.foo', '"bar"');
55
56shouldBe('class A { static get foo() {} }; descriptor(A, "foo").writable', 'undefined');
57shouldBe('class A { static get foo() { return 5; } }; A.foo = 3; A.foo', '5');
58shouldBeFalse('class A { static get foo() {} }; descriptor(A, "foo").enumerable');
59shouldBeFalse('class A { static get foo() {} }; enumeratedProperties(A).includes("foo")');
60shouldBeFalse('class A { static get foo() {} }; enumeratedProperties(new A).includes("foo")');
61shouldBeTrue('class A { static get foo() {} }; descriptor(A, "foo").configurable');
62shouldBe('class A { static get foo() {} }; Object.defineProperty(A, "foo", {value: "bar"}); A.foo', '"bar"');
63
64shouldBeTrue('class A { foo() {} }; descriptor(A.prototype, "foo").writable');
65shouldBe('class A { foo() {} }; A.prototype.foo = 3; A.prototype.foo', '3');
66shouldBeFalse('class A { foo() {} }; descriptor(A.prototype, "foo").enumerable');
67shouldBeFalse('class A { foo() {} }; enumeratedProperties(A.prototype).includes("foo")');
68shouldBeFalse('class A { foo() {} }; enumeratedProperties(new A).includes("foo")');
69shouldBeTrue('class A { foo() {} }; descriptor(A.prototype, "foo").configurable');
70shouldBe('class A { foo() {} }; Object.defineProperty(A.prototype, "foo", {value: "bar"}); A.prototype.foo', '"bar"');
71
72shouldBe('class A { get foo() {} }; descriptor(A.prototype, "foo").writable', 'undefined');
73shouldBe('class A { get foo() { return 5; } }; A.prototype.foo = 3; A.prototype.foo', '5');
74shouldBeFalse('class A { get foo() {} }; descriptor(A.prototype, "foo").enumerable');
75shouldBeFalse('class A { get foo() {} }; enumeratedProperties(A.prototype).includes("foo")');
76shouldBeFalse('class A { get foo() {} }; enumeratedProperties(new A).includes("foo")');
77shouldBeTrue('class A { get foo() {} }; descriptor(A.prototype, "foo").configurable');
78shouldBe('class A { get foo() {} }; Object.defineProperty(A.prototype, "foo", {value: "bar"}); A.prototype.foo', '"bar"');
79
80shouldBeTrue('class A { }; descriptor(A.prototype, "constructor").writable');
81shouldBe('class A { }; A.prototype.constructor = 3; A.prototype.constructor', '3');
82shouldBeTrue('class A { }; x = {}; A.prototype.constructor = function () { return x; }; (new A) instanceof A');
83shouldBeFalse('class A { }; descriptor(A.prototype, "constructor").enumerable');
84shouldBeFalse('class A { }; enumeratedProperties(A.prototype).includes("constructor")');
85shouldBeFalse('class A { }; enumeratedProperties(new A).includes("constructor")');
86shouldBeTrue('class A { }; descriptor(A.prototype, "constructor").configurable');
87shouldBe('class A { }; Object.defineProperty(A.prototype, "constructor", {value: "bar"}); A.prototype.constructor', '"bar"');
88
89var successfullyParsed = true;