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