summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/Reflect.get.js
blob: 0ad987132f40761d4520eaba1cc222b3b316f4b4 (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
55
56
load("test-common.js");

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

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

    assert(Reflect.get({}) === undefined);
    assert(Reflect.get({ undefined: 1 }) === 1);
    assert(Reflect.get({ foo: 1 }) === undefined);
    assert(Reflect.get({ foo: 1 }, "foo") === 1);

    assert(Reflect.get([]) === undefined);
    assert(Reflect.get([1, 2, 3]) === undefined);
    assert(Reflect.get([1, 2, 3], "0") === 1);
    assert(Reflect.get([1, 2, 3], 0) === 1);
    assert(Reflect.get([1, 2, 3], 1) === 2);
    assert(Reflect.get([1, 2, 3], 2) === 3);
    assert(Reflect.get([1, 2, 3], 4) === undefined);

    assert(Reflect.get(new String()) === undefined);
    assert(Reflect.get(new String(), 0) === undefined);
    assert(Reflect.get(new String("foo"), "0") === "f");
    assert(Reflect.get(new String("foo"), 0) === "f");
    assert(Reflect.get(new String("foo"), 1) === "o");
    assert(Reflect.get(new String("foo"), 2) === "o");
    assert(Reflect.get(new String("foo"), 3) === undefined);

    const foo = {
        get prop() {
            this.getPropCalled = true;
        }
    };
    const bar = {};
    Object.setPrototypeOf(bar, foo);

    assert(foo.getPropCalled === undefined);
    assert(bar.getPropCalled === undefined);
    Reflect.get(bar, "prop");
    assert(foo.getPropCalled === undefined);
    assert(bar.getPropCalled === true);
    Reflect.get(bar, "prop", foo);
    assert(foo.getPropCalled === true);
    assert(bar.getPropCalled === true);

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