blob: d70f2a1b2bc512f1a586096262bebbb9da81c355 [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 "super"');
25
26var baseMethodValue = {};
27var valueInSetter = null;
28
29class Base {
30 constructor() { }
31 baseMethod() { return baseMethodValue; }
32 chainMethod() { return 'base'; }
33 static staticMethod() { return 'base3'; }
34}
35
36class Derived extends Base {
37 constructor() { super(); }
38 chainMethod() { return [super.chainMethod(), 'derived']; }
39 callBaseMethod() { return super.baseMethod(); }
40 get callBaseMethodInGetter() { return super['baseMethod'](); }
41 set callBaseMethodInSetter(x) { valueInSetter = super.baseMethod(); }
42 get baseMethodInGetterSetter() { return super.baseMethod; }
43 set baseMethodInGetterSetter(x) { valueInSetter = super['baseMethod']; }
44 static staticMethod() { return super.staticMethod(); }
45}
46
47class SecondDerived extends Derived {
48 constructor() { super(); }
49 chainMethod() { return super.chainMethod().concat(['secondDerived']); }
50}
51
52shouldBeTrue('(new Base) instanceof Base');
53shouldBeTrue('(new Derived) instanceof Derived');
54shouldBe('(new Derived).callBaseMethod()', 'baseMethodValue');
55shouldBe('x = (new Derived).callBaseMethod; x()', 'baseMethodValue');
56shouldBe('(new Derived).callBaseMethodInGetter', 'baseMethodValue');
57shouldBe('(new Derived).callBaseMethodInSetter = 1; valueInSetter', 'baseMethodValue');
58shouldBe('(new Derived).baseMethodInGetterSetter', '(new Base).baseMethod');
59shouldBe('(new Derived).baseMethodInGetterSetter = 1; valueInSetter', '(new Base).baseMethod');
60shouldBe('Derived.staticMethod()', '"base3"');
61shouldBe('(new SecondDerived).chainMethod()', '["base", "derived", "secondDerived"]');
62shouldNotThrow('x = class extends Base { constructor() { super(); } super() {} }');
63shouldThrow('x = class extends Base { constructor() { super(); } method() { super() } }',
64 '"SyntaxError: \'super\' keyword unexpected here"');
65shouldThrow('x = class extends Base { constructor() { super(); } method() { super } }', '"SyntaxError: \'super\' keyword unexpected here"');
66shouldThrow('x = class extends Base { constructor() { super(); } method() { return new super } }', '"SyntaxError: \'super\' keyword unexpected here"');
67// shouldBeTrue('(new x).method() instanceof Base');
68// shouldBeFalse('(new x).method() instanceof x');
69shouldNotThrow('x = class extends Base { constructor() { super(); } method1() { delete (super.foo) } method2() { delete super["foo"] } }');
70shouldThrow('(new x).method1()', '"ReferenceError: Unsupported reference to \'super\'"');
71shouldThrow('(new x).method2()', '"ReferenceError: Unsupported reference to \'super\'"');
72shouldBeTrue('new (class { constructor() { return undefined; } }) instanceof Object');
73shouldBeTrue('new (class { constructor() { return 1; } }) instanceof Object');
74shouldThrow('new (class extends Base { constructor() { return undefined } })');
75shouldBeTrue('new (class extends Base { constructor() { super(); return undefined } }) instanceof Object');
76shouldBe('x = { }; new (class extends Base { constructor() { return x } });', 'x');
77shouldBeFalse('x instanceof Base');
78shouldThrow('new (class extends Base { constructor() { } })', '"ReferenceError: this is not defined"');
79shouldThrow('new (class extends Base { constructor() { return 1; } })', '"TypeError: Derived constructors may only return object or undefined"');
80shouldThrow('new (class extends null { constructor() { return undefined } })');
81shouldThrow('new (class extends null { constructor() { super(); return undefined } })', '"TypeError: super is not a constructor"');
82shouldBe('x = { }; new (class extends null { constructor() { return x } });', 'x');
83shouldBeTrue('x instanceof Object');
84shouldThrow('new (class extends null { constructor() { } })', '"ReferenceError: this is not defined"');
85shouldThrow('new (class extends null { constructor() { return 1; } })', '"TypeError: Derived constructors may only return object or undefined"');
86shouldThrow('new (class extends null { constructor() { super() } })', '"TypeError: super is not a constructor"');
87shouldThrow('new (class { constructor() { super() } })', '"SyntaxError: \'super\' keyword unexpected here"');
88shouldThrow('function x() { super(); }', '"SyntaxError: \'super\' keyword unexpected here"');
89shouldThrow('new (class extends Object { constructor() { function x() { super() } } })', '"SyntaxError: \'super\' keyword unexpected here"');
90shouldThrow('new (class extends Object { constructor() { function x() { super.method } } })', '"SyntaxError: \'super\' keyword unexpected here"');
91shouldThrow('function x() { super.method(); }', '"SyntaxError: \'super\' keyword unexpected here"');
92shouldThrow('function x() { super(); }', '"SyntaxError: \'super\' keyword unexpected here"');
93
94var successfullyParsed = true;