blob: 775a3353d0e64d00e0b301f5a7eb70b9e460da32 [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 ES6 class syntax declarations');
25
26var constructorCallCount = 0;
27const staticMethodValue = [1];
28const instanceMethodValue = [2];
29const getterValue = [3];
30var setterValue = undefined;
31class A {
32 constructor() { constructorCallCount++; }
33 static someStaticMethod() { return staticMethodValue; }
34 static get someStaticGetter() { return getterValue; }
35 static set someStaticSetter(value) { setterValue = value; }
36 someInstanceMethod() { return instanceMethodValue; }
37 get someGetter() { return getterValue; }
38 set someSetter(value) { setterValue = value; }
39}
40
41shouldBe("constructorCallCount", "0");
42shouldBe("A.someStaticMethod()", "staticMethodValue");
43shouldBe("A.someStaticGetter", "getterValue");
44shouldBe("setterValue = undefined; A.someStaticSetter = 123; setterValue", "123");
45shouldBe("(new A).someInstanceMethod()", "instanceMethodValue");
46shouldBe("constructorCallCount", "1");
47shouldBe("(new A).someGetter", "getterValue");
48shouldBe("constructorCallCount", "2");
49shouldBe("(new A).someGetter", "getterValue");
50shouldBe("setterValue = undefined; (new A).someSetter = 789; setterValue", "789");
51shouldBe("(new A).__proto__", "A.prototype");
52shouldBe("A.prototype.constructor", "A");
53
54shouldThrow("class", "'SyntaxError: Unexpected end of input'");
55shouldThrow("class [", "'SyntaxError: Unexpected token ['");
56shouldThrow("class {", "'SyntaxError: Unexpected token {'");
57shouldThrow("class X {", "'SyntaxError: Unexpected end of input'");
58shouldThrow("class X { ( }", "'SyntaxError: Unexpected token ('");
59shouldNotThrow("class X {}");
60
61shouldThrow("class X { constructor() {} constructor() {} }", "'SyntaxError: A class may only have one constructor'");
62shouldThrow("class X { get constructor() {} }", "'SyntaxError: Class constructor may not be an accessor'");
63shouldThrow("class X { set constructor() {} }", "'SyntaxError: Class constructor may not be an accessor'");
64shouldNotThrow("class X { constructor() {} static constructor() { return staticMethodValue; } }");
65shouldBe("class X { constructor() {} static constructor() { return staticMethodValue; } }; X.constructor()", "staticMethodValue");
66
67shouldThrow("class X { constructor() {} static prototype() {} }", "'SyntaxError: Classes may not have static property named prototype'");
68shouldThrow("class X { constructor() {} static get prototype() {} }", "'SyntaxError: Classes may not have static property named prototype'");
69shouldThrow("class X { constructor() {} static set prototype() {} }", "'SyntaxError: Classes may not have static property named prototype'");
70shouldNotThrow("class X { constructor() {} prototype() { return instanceMethodValue; } }");
71shouldBe("class X { constructor() {} prototype() { return instanceMethodValue; } }; (new X).prototype()", "instanceMethodValue");
72
73shouldNotThrow("class X { constructor() {} set foo(a) {} }");
74shouldNotThrow("class X { constructor() {} set foo({x, y}) {} }");
75shouldThrow("class X { constructor() {} set foo() {} }");
76shouldThrow("class X { constructor() {} set foo(a, b) {} }");
77shouldNotThrow("class X { constructor() {} get foo() {} }");
78shouldThrow("class X { constructor() {} get foo(x) {} }");
79shouldThrow("class X { constructor() {} get foo({x, y}) {} }");
80
81var successfullyParsed = true;