summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/Reflect.defineProperty.js
blob: 3046357ccc49e77c7e171857f7479d338b5541c4 (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
57
58
59
load("test-common.js");

try {
    assert(Reflect.defineProperty.length === 3);

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

        assertThrowsError(() => {
            Reflect.defineProperty({}, "foo", value);
        }, {
            error: TypeError,
            message: "Descriptor argument is not an object"
        });
    });

    var o = {};

    assert(Reflect.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false }) === true);
    assert(o.foo === 1);
    o.foo = 2;
    assert(o.foo === 1);

    assert(Reflect.defineProperty(o, "bar", { value: "hi", writable: true, enumerable: true }) === true);
    assert(o.bar === "hi");
    o.bar = "ho";
    assert(o.bar === "ho");

    assert(Reflect.defineProperty(o, "bar", { value: "xx", enumerable: false }) === false);

    var d = Reflect.getOwnPropertyDescriptor(o, "foo");
    assert(d.configurable === false);
    assert(d.enumerable === false);
    assert(d.writable === false);
    assert(d.value === 1);

    d = Reflect.getOwnPropertyDescriptor(o, "bar");
    assert(d.configurable === false);
    assert(d.enumerable === true);
    assert(d.writable === true);
    assert(d.value === "ho");

    assert(Reflect.defineProperty(o, "baz", { value: 9, configurable: true, writable: false }) === true);
    assert(Reflect.defineProperty(o, "baz", { configurable: true, writable: true }) === true);

    d = Reflect.getOwnPropertyDescriptor(o, "baz");
    assert(d.configurable === true);
    assert(d.writable === true);
    assert(d.value === 9);

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