blob: a1b8f1292d917760b1830789e3587cd0ca78c172 [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 syntax "extends"');
27
28class Base {
29 constructor() { }
30 baseMethod() { return 'base'; }
31 overridenMethod() { return 'base'; }
32 static staticBaseMethod() { return 'base'; }
33 static staticOverridenMethod() { return 'base'; }
34}
35
36class Derived extends Base {
37 constructor() { super(); }
38 overridenMethod() { return 'derived'; }
39 static staticOverridenMethod() { return 'derived'; }
40}
41
42shouldBeTrue('(new Base) instanceof Base');
43shouldBe('Object.getPrototypeOf(new Base)', 'Base.prototype');
44shouldBeTrue('(new Derived) instanceof Derived');
45shouldBe('Object.getPrototypeOf(new Derived)', 'Derived.prototype');
46shouldBe('Object.getPrototypeOf(Derived.prototype)', 'Base.prototype');
47shouldBe('(new Derived).baseMethod()', '"base"');
48shouldBe('(new Derived).overridenMethod()', '"derived"');
49shouldBe('Derived.staticBaseMethod()', '"base"');
50shouldBe('Derived.staticOverridenMethod()', '"derived"');
51
52shouldThrow('x = class extends', '"SyntaxError: Unexpected end of input"');
53shouldThrow('x = class extends', '"SyntaxError: Unexpected end of input"');
54shouldThrow('x = class extends Base {', '"SyntaxError: Unexpected end of input"');
55shouldNotThrow('x = class extends Base { }');
56shouldNotThrow('x = class extends Base { constructor() { } }');
57shouldBe('x.__proto__', 'Base');
58shouldBe('Object.getPrototypeOf(x)', 'Base');
59shouldBe('x.prototype.__proto__', 'Base.prototype');
60shouldBe('Object.getPrototypeOf(x.prototype)', 'Base.prototype');
61shouldBe('x = class extends null { constructor() { } }; x.__proto__', 'Function.prototype');
62shouldBe('x.__proto__', 'Function.prototype');
63shouldThrow('x = class extends 3 { constructor() { } }; x.__proto__', '"TypeError: Class extends value 3 is not a function or null"');
64shouldThrow('x = class extends "abc" { constructor() { } }; x.__proto__', '"TypeError: Class extends value abc is not a function or null"');
65shouldNotThrow('baseWithBadPrototype = function () {}; baseWithBadPrototype.prototype = 3; new baseWithBadPrototype');
66shouldThrow('x = class extends baseWithBadPrototype { constructor() { } }', '"TypeError: Class extends value does not have valid prototype property 3"');
67shouldNotThrow('baseWithBadPrototype.prototype = "abc"');
68shouldThrow('x = class extends baseWithBadPrototype { constructor() { } }', '"TypeError: Class extends value does not have valid prototype property abc"');
69shouldNotThrow('baseWithBadPrototype.prototype = null; x = class extends baseWithBadPrototype { constructor() { } }');
70
71shouldThrow('x = 1; c = class extends ++x { constructor() { } };');
72shouldThrow('x = 1; c = class extends x++ { constructor() { } };');
73shouldThrow('x = 1; c = class extends (++x) { constructor() { } };');
74shouldThrow('x = 1; c = class extends (x++) { constructor() { } };');
75shouldBe('x = 1; try { c = class extends (++x) { constructor() { } } } catch (e) { }; x', '2');
76shouldBe('x = 1; try { c = class extends (x++) { constructor() { } } } catch (e) { }; x', '2');
77
78shouldNotThrow('namespace = {}; namespace.A = class { }; namespace.B = class extends namespace.A { }');
79shouldNotThrow('namespace = {}; namespace.A = class A { }; namespace.B = class B extends namespace.A { }');
80shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends namespace.A { constructor() { } }');
81shouldNotThrow('namespace = {}; namespace.A = class A { constructor() { } }; namespace.B = class B extends namespace.A { constructor() { } }');
82shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends (namespace.A) { constructor() { } }');
83shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends namespace["A"] { constructor() { } }');
84shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; function getClassA() { return namespace.A }; namespace.B = class extends getClassA() { constructor() { } }');
85shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; function getClass(prop) { return namespace[prop] }; namespace.B = class extends getClass("A") { constructor() { } }');
86shouldNotThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends (false||null||namespace.A) { constructor() { } }');
87shouldThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends false||null||namespace.A { constructor() { } }');
88shouldNotThrow('x = 1; namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends (x++, namespace.A) { constructor() { } };');
89shouldThrow('x = 1; namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends (namespace.A, x++) { constructor() { } };');
90shouldThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends new namespace.A { constructor() { } }');
91shouldThrow('namespace = {}; namespace.A = class { constructor() { } }; namespace.B = class extends new namespace.A() { constructor() { } }');
92shouldBe('x = 1; namespace = {}; namespace.A = class { constructor() { } }; try { namespace.B = class extends (x++, namespace.A) { constructor() { } } } catch (e) { } x', '2');
93shouldBe('x = 1; namespace = {}; namespace.A = class { constructor() { } }; try { namespace.B = class extends (namespace.A, x++) { constructor() { } } } catch (e) { } x', '2');
94
95shouldBe('Object.getPrototypeOf((class { constructor () { } }).prototype)', 'Object.prototype');
96shouldBe('Object.getPrototypeOf((class extends null { constructor () { super(); } }).prototype)', 'null');
97shouldThrow('new (class extends undefined { constructor () { this } })', '"TypeError: Class extends value undefined is not a function or null"');
98shouldThrow('new (class extends undefined { constructor () { super(); } })', '"TypeError: Class extends value undefined is not a function or null"');
99shouldThrow('x = {}; new (class extends undefined { constructor () { return x; } })', '"TypeError: Class extends value undefined is not a function or null"');
100shouldThrow('y = 12; new (class extends undefined { constructor () { return y; } })', '"TypeError: Class extends value undefined is not a function or null"');
101shouldBeTrue ('class x {}; new (class extends null { constructor () { return new x; } }) instanceof x');
102shouldThrow('new (class extends null { constructor () { this; } })', '"ReferenceError: this is not defined"');
103shouldThrow('new (class extends null { constructor () { super(); } })', '"TypeError: super is not a constructor"');
104shouldBe('x = {}; new (class extends null { constructor () { return x } })', 'x');
105shouldThrow('y = 12; new (class extends null { constructor () { return y; } })', '"TypeError: Derived constructors may only return object or undefined"');
106shouldBeTrue ('class x {}; new (class extends null { constructor () { return new x; } }) instanceof x');
107shouldBe('x = null; Object.getPrototypeOf((class extends x { }).prototype)', 'null');
108shouldBeTrue('Object.prototype.isPrototypeOf(class { })');
109shouldBeTrue('Function.prototype.isPrototypeOf(class { })');
110
111var successfullyParsed = true;