blob: 16045651ef35adc5869ec73246e6cf658a4e0cea [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
24// Flags: --harmony-sloppy
25
26description('Tests for ES6 class name semantics in class statements and expressions');
27
28function runTestShouldBe(statement, result) {
29 shouldBe(statement, result);
30 shouldBe("'use strict'; " + statement, result);
31}
32
33function runTestShouldBeTrue(statement) {
34 shouldBeTrue(statement);
35 shouldBeTrue("'use strict'; " + statement);
36}
37
38function runTestShouldThrow(statement) {
39 shouldThrow(statement);
40 shouldThrow("'use strict'; " + statement);
41}
42
43function runTestShouldNotThrow(statement) {
44 shouldNotThrow(statement);
45 shouldNotThrow("'use strict'; " + statement);
46}
47
48// Class statement. Class name added to global scope. Class name is available inside class scope and in global scope.
49debug('Class statement');
50runTestShouldThrow("A");
51runTestShouldThrow("class {}");
52runTestShouldThrow("class { constructor() {} }");
53runTestShouldNotThrow("class A { constructor() {} }");
54runTestShouldBe("class A { constructor() {} }; A.toString()", "'class A { constructor() {} }'");
55runTestShouldBeTrue("class A { constructor() {} }; (new A) instanceof A");
56runTestShouldBe("class A { constructor() { this.base = A; } }; (new A).base.toString()", "'class A { constructor() { this.base = A; } }'");
57runTestShouldNotThrow("class A { constructor() {} }; class B extends A {};");
58runTestShouldBe("class A { constructor() {} }; class B extends A { constructor() {} }; B.toString()", "'class B extends A { constructor() {} }'");
59runTestShouldBeTrue("class A { constructor() {} }; class B extends A {}; (new B) instanceof A");
60runTestShouldBeTrue("class A { constructor() {} }; class B extends A {}; (new B) instanceof B");
61runTestShouldBe("class A { constructor() {} }; class B extends A { constructor() { super(); this.base = A; this.derived = B; } }; (new B).base.toString()", "'class A { constructor() {} }'");
62runTestShouldBe("class A { constructor() {} }; class B extends A { constructor() { super(); this.base = A; this.derived = B; } }; (new B).derived.toString()", "'class B extends A { constructor() { super(); this.base = A; this.derived = B; } }'");
63
64// Class expression. Class name not added to scope. Class name is available inside class scope.
65debug(''); debug('Class expression');
66runTestShouldThrow("A");
67runTestShouldNotThrow("(class {})");
68runTestShouldNotThrow("(class { constructor(){} })");
69runTestShouldBe("typeof (class {})", '"function"');
70runTestShouldNotThrow("(class A {})");
71runTestShouldBe("typeof (class A {})", '"function"');
72runTestShouldThrow("(class A {}); A");
73runTestShouldNotThrow("new (class A {})");
74runTestShouldBe("typeof (new (class A {}))", '"object"');
75runTestShouldNotThrow("(new (class A { constructor() { this.base = A; } })).base");
76runTestShouldBe("(new (class A { constructor() { this.base = A; } })).base.toString()", '"class A { constructor() { this.base = A; } }"');
77runTestShouldNotThrow("class A {}; (class B extends A {})");
78runTestShouldThrow("class A {}; (class B extends A {}); B");
79runTestShouldNotThrow("class A {}; new (class B extends A {})");
80runTestShouldNotThrow("class A {}; new (class B extends A { constructor() { super(); this.base = A; this.derived = B; } })");
81runTestShouldBeTrue("class A {}; (new (class B extends A { constructor() { super(); this.base = A; this.derived = B; } })) instanceof A");
82runTestShouldBe("class A { constructor() {} }; (new (class B extends A { constructor() { super(); this.base = A; this.derived = B; } })).base.toString()", "'class A { constructor() {} }'");
83runTestShouldBe("class A { constructor() {} }; (new (class B extends A { constructor() { super(); this.base = A; this.derived = B; } })).derived.toString()", "'class B extends A { constructor() { super(); this.base = A; this.derived = B; } }'");
84
85// Assignment of a class expression to a variable. Variable name available in scope, class name is not. Class name is available inside class scope.
86debug(''); debug('Class expression assignment to variable');
87runTestShouldThrow("A");
88runTestShouldNotThrow("var VarA = class {}");
89runTestShouldBe("var VarA = class { constructor() {} }; VarA.toString()", "'class { constructor() {} }'");
90runTestShouldThrow("VarA");
91runTestShouldNotThrow("var VarA = class A { constructor() {} }");
92runTestShouldBe("var VarA = class A { constructor() {} }; VarA.toString()", "'class A { constructor() {} }'");
93runTestShouldThrow("var VarA = class A { constructor() {} }; A.toString()");
94runTestShouldBeTrue("var VarA = class A { constructor() {} }; (new VarA) instanceof VarA");
95runTestShouldBe("var VarA = class A { constructor() { this.base = A; } }; (new VarA).base.toString()", "'class A { constructor() { this.base = A; } }'");
96runTestShouldNotThrow("var VarA = class A { constructor() {} }; var VarB = class B extends VarA { constructor() {} };");
97runTestShouldThrow("var VarA = class A { constructor() {} }; var VarB = class B extends VarA { constructor() {} }; B");
98runTestShouldBe("var VarA = class A { constructor() {} }; var VarB = class B extends VarA { constructor() {} }; VarB.toString()", "'class B extends VarA { constructor() {} }'");
99runTestShouldBeTrue("var VarA = class A { constructor() {} }; var VarB = class B extends VarA { }; (new VarB) instanceof VarA");
100runTestShouldBeTrue("var VarA = class A { constructor() {} }; var VarB = class B extends VarA { }; (new VarB) instanceof VarB");
101runTestShouldBeTrue("var VarA = class A { constructor() {} }; var VarB = class B extends VarA { constructor() { super(); this.base = VarA; this.derived = B; this.derivedVar = VarB; } }; (new VarB).base === VarA");
102runTestShouldBeTrue("var VarA = class A { constructor() {} }; var VarB = class B extends VarA { constructor() { super(); this.base = VarA; this.derived = B; this.derivedVar = VarB; } }; (new VarB).derived === VarB");
103runTestShouldBeTrue("var VarA = class A { constructor() {} }; var VarB = class B extends VarA { constructor() { super(); this.base = VarA; this.derived = B; this.derivedVar = VarB; } }; (new VarB).derivedVar === VarB");
104
105// FIXME: Class statement binding should be like `let`, not `var`.
106debug(''); debug('Class statement binding in other circumstances');
107runTestShouldThrow("var result = A; result");
108runTestShouldThrow("var result = A; class A {}; result");
109runTestShouldThrow("class A { constructor() { A = 1; } }; new A");
110runTestShouldBe("class A { constructor() { } }; A = 1; A", "1");
111runTestShouldNotThrow("class A {}; var result = A; result");
112shouldBe("eval('var Foo = 10'); Foo", "10");
113shouldThrow("'use strict'; eval('var Foo = 10'); Foo");
114shouldBe("eval('class Bar { constructor() {} }; Bar.toString()')", "'class Bar { constructor() {} }'");
115shouldThrow("'use strict'; eval('class Bar { constructor() {} }'); Bar.toString()");