123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- test("method inheritance", () => {
- class Parent {
- method() {
- return 3;
- }
- }
- class Child extends Parent {}
- const p = new Parent();
- const c = new Child();
- expect(p.method()).toBe(3);
- expect(c.method()).toBe(3);
- });
- test("method overriding", () => {
- class Parent {
- method() {
- return 3;
- }
- }
- class Child extends Parent {
- method() {
- return 10;
- }
- }
- const p = new Parent();
- const c = new Child();
- expect(p.method()).toBe(3);
- expect(c.method()).toBe(10);
- });
- test("parent method reference with super", () => {
- class Parent {
- method() {
- return 3;
- }
- }
- class Child extends Parent {
- method() {
- return super.method() * 2;
- }
- }
- const p = new Parent();
- const c = new Child();
- expect(p.method()).toBe(3);
- expect(c.method()).toBe(6);
- });
- test("child class access to parent class initialized properties", () => {
- class Parent {
- constructor() {
- this.x = 3;
- }
- }
- class Child extends Parent {}
- const p = new Parent();
- const c = new Child();
- expect(p.x).toBe(3);
- expect(c.x).toBe(3);
- });
- test("child class modification of parent class properties", () => {
- class Parent {
- constructor() {
- this.x = 3;
- }
- }
- class Child extends Parent {
- change() {
- this.x = 10;
- }
- }
- const p = new Parent();
- const c = new Child();
- expect(p.x).toBe(3);
- expect(c.x).toBe(3);
- c.change();
- expect(c.x).toBe(10);
- });
- test("inheritance and hasOwnProperty", () => {
- class Parent {
- constructor() {
- this.x = 3;
- }
- }
- class Child extends Parent {
- method() {
- this.y = 10;
- }
- }
- const p = new Parent();
- const c = new Child();
- expect(p.hasOwnProperty("x")).toBeTrue();
- expect(p.hasOwnProperty("y")).toBeFalse();
- expect(c.hasOwnProperty("x")).toBeTrue();
- expect(c.hasOwnProperty("y")).toBeFalse();
- c.method();
- expect(c.hasOwnProperty("x")).toBeTrue();
- expect(c.hasOwnProperty("y")).toBeTrue();
- });
- test("super constructor call from child class with argument", () => {
- class Parent {
- constructor(x) {
- this.x = x;
- }
- }
- class Child extends Parent {
- constructor() {
- super(10);
- }
- }
- const p = new Parent(3);
- const c = new Child(3);
- expect(p.x).toBe(3);
- expect(c.x).toBe(10);
- });
- test("advanced 'extends' RHS", () => {
- const foo = {
- bar() {
- return {
- baz() {
- return function () {
- return function () {
- return { quux: Number };
- };
- };
- },
- };
- },
- };
- class Foo extends foo.bar()["baz"]()`qux`().quux {}
- expect(new Foo()).toBeInstanceOf(Number);
- });
- test("issue #7045, super constructor call from child class in catch {}", () => {
- class Parent {
- constructor(x) {
- this.x = x;
- }
- }
- class Child extends Parent {
- constructor() {
- try {
- throw new Error("Error in Child constructor");
- } catch (e) {
- super(e.message);
- }
- }
- }
- const c = new Child();
- expect(c.x).toBe("Error in Child constructor");
- });
- test("Issue #7044, super property access before super() call", () => {
- class Foo {
- constructor() {
- super.bar;
- }
- }
- new Foo();
- });
- test("Issue #8574, super property access before super() call", () => {
- var hit = false;
- class Foo extends Object {
- constructor() {
- expect(() => {
- const foo = super.bar();
- }).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
- hit = true;
- }
- }
- // Note: We catch two exceptions here.
- expect(() => {
- new Foo();
- }).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
- expect(hit).toBeTrue();
- });
- test("can access super via direct eval", () => {
- let superCalled = false;
- const aObject = { a: 1 };
- const bObject = { b: 2 };
- class A {
- constructor() {
- superCalled = true;
- }
- foo() {
- return aObject;
- }
- bar() {
- return bObject;
- }
- }
- class B extends A {
- constructor() {
- eval("super()");
- }
- }
- expect(() => {
- new B();
- }).not.toThrow();
- expect(superCalled).toBeTrue();
- superCalled = false;
- class C extends A {
- constructor() {
- eval("super()");
- return eval("super.foo()");
- }
- }
- expect(() => {
- new C();
- }).not.toThrow();
- expect(superCalled).toBeTrue();
- superCalled = false;
- expect(new C()).toBe(aObject);
- expect(superCalled).toBeTrue();
- superCalled = false;
- class D extends A {
- constructor() {
- eval("super()");
- return eval("super['bar']()");
- }
- }
- expect(() => {
- new D();
- }).not.toThrow();
- expect(superCalled).toBeTrue();
- superCalled = false;
- expect(new D()).toBe(bObject);
- expect(superCalled).toBeTrue();
- });
- test("cannot access super via indirect eval", () => {
- const indirect = eval;
- let superCalled = false;
- const aObject = { a: 1 };
- const bObject = { b: 1 };
- class A {
- constructor() {
- superCalled = true;
- this.a = aObject;
- this.c = bObject;
- }
- }
- class B extends A {
- constructor() {
- indirect("super()");
- }
- }
- expect(() => {
- new B();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(superCalled).toBeFalse();
- class C extends A {
- constructor() {
- super();
- return indirect("super.a");
- }
- }
- expect(() => {
- new C();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(superCalled).toBeTrue();
- superCalled = false;
- class D extends A {
- constructor() {
- super();
- return indirect("super['b']");
- }
- }
- expect(() => {
- new D();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(superCalled).toBeTrue();
- });
- test("super outside of derived class fails to parse", () => {
- expect("super").not.toEval();
- expect("super()").not.toEval();
- expect("super.a").not.toEval();
- expect("super['b']").not.toEval();
- expect("function a() { super }").not.toEval();
- expect("function a() { super() }").not.toEval();
- expect("function a() { super.a }").not.toEval();
- expect("function a() { super['b'] }").not.toEval();
- expect("() => { super }").not.toEval();
- expect("() => { super() }").not.toEval();
- expect("() => { super.a }").not.toEval();
- expect("() => { super['b'] }").not.toEval();
- expect("class A { constructor() { super } }").not.toEval();
- expect("class A { constructor() { super() } }").not.toEval();
- expect(() => {
- eval("super");
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(() => {
- eval("super()");
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(() => {
- eval("super.a");
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(() => {
- eval("super['b']");
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- function a() {
- eval("super");
- }
- expect(() => {
- a();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(() => {
- new a();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- function b() {
- eval("super()");
- }
- expect(() => {
- b();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(() => {
- new b();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- function c() {
- eval("super.a");
- }
- expect(() => {
- c();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(() => {
- new c();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- function d() {
- eval("super['b']");
- }
- expect(() => {
- d();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- expect(() => {
- new d();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- const e = () => eval("super");
- expect(() => {
- e();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- const f = () => eval("super()");
- expect(() => {
- f();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- const g = () => eval("super.a");
- expect(() => {
- g();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- const h = () => eval("super['b']");
- expect(() => {
- h();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- class I {
- constructor() {
- eval("super");
- }
- }
- expect(() => {
- new I();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- class J {
- constructor() {
- eval("super()");
- }
- }
- expect(() => {
- new J();
- }).toThrowWithMessage(SyntaxError, "'super' keyword unexpected here");
- });
|