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