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