summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/new-expression.js
blob: 827b1569104529d2908939d58de84155b0c00e62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// prettier-ignore
test("new-expression parsing", () => {
    function Foo() {
        this.x = 1;
    }

    let foo = new Foo();
    expect(foo.x).toBe(1);

    foo = new Foo
    expect(foo.x).toBe(1);

    foo = new
    Foo
    ();
    expect(foo.x).toBe(1);

    foo = new Foo + 2
    expect(foo).toBe("[object Object]2");
});

// prettier-ignore
test("new-expressions with object keys", () => {
    let a = {
        b: function () {
            this.x = 2;
        },
    };

    foo = new a.b();
    expect(foo.x).toBe(2);

    foo = new a.b;
    expect(foo.x).toBe(2);

    foo = new
    a.b();
    expect(foo.x).toBe(2);
});

test("new-expressions with function calls", () => {
    function funcGetter() {
        return function (a, b) {
            this.x = a + b;
        };
    }

    foo = new funcGetter()(1, 5);
    expect(foo).toBeUndefined();

    foo = new (funcGetter())(1, 5);
    expect(foo.x).toBe(6);
});