summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/Reflect.construct.js
blob: f65bcd335681d5fc8d3517082f9bd095208a238d (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
54
load("test-common.js");

try {
    assert(Reflect.construct.length === 2);

    [null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
        assertThrowsError(() => {
            Reflect.construct(value);
        }, {
            error: TypeError,
            message: "First argument of Reflect.construct() must be a function"
        });

        assertThrowsError(() => {
            Reflect.construct(() => {}, value);
        }, {
            error: TypeError,
            message: "Arguments list must be an object"
        });

        assertThrowsError(() => {
            Reflect.construct(() => {}, [], value);
        }, {
            error: TypeError,
            message: "Optional third argument of Reflect.construct() must be a constructor"
        });
    });

    var a = Reflect.construct(Array, [5]);
    assert(a instanceof Array);
    assert(a.length === 5);

    var s = Reflect.construct(String, [123]);
    assert(s instanceof String);
    assert(s.length === 3);
    assert(s.toString() === "123");

    function Foo() {
        this.name = "foo";
    }
    
    function Bar() {
        this.name = "bar";
    }

    var o = Reflect.construct(Foo, [], Bar);
    assert(o.name === "foo");
    assert(o instanceof Foo === false);
    assert(o instanceof Bar === true);

    console.log("PASS");
} catch (e) {
    console.log("FAIL: " + e);
}