summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/Object.defineProperty.js
blob: 2fce7a98358479b1c15310320ff4d09c53562ac6 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
load("test-common.js");

try {
    var o = {};
    Object.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false });

    assert(o.foo === 1);
    o.foo = 2;
    assert(o.foo === 1);
    Object.defineProperty(o, 2, { get() { return 10; } });
    assert(o[2] === 10);

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

    Object.defineProperty(o, "bar", { value: "hi", writable: true, enumerable: true });

    assert(o.bar === "hi");
    o.bar = "ho";
    assert(o.bar === "ho");

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

    assertThrowsError(() => {
        Object.defineProperty(o, "bar", { value: "xx", enumerable: false });
    }, {
        error: TypeError
    });

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

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

    Object.defineProperty(o, "qux", {
        configurable: true,
        get() {
            return o.secret_qux + 1;
        },
        set(value) {
            this.secret_qux = value + 1;
        },
    });

    o.qux = 10;
    assert(o.qux === 12);
    o.qux = 20;
    assert(o.qux = 22);

    Object.defineProperty(o, "qux", { configurable: true, value: 4 });

    assert(o.qux === 4);
    o.qux = 5;
    assert(o.qux = 4);

    Object.defineProperty(o, "qux", {
        configurable: false,
        get() {
            return this.secret_qux + 2;
        },
        set(value) {
            o.secret_qux = value + 2;
        },
    });

    o.qux = 10;
    assert(o.qux === 14);
    o.qux = 20;
    assert(o.qux = 24);

    assertThrowsError(() => {
        Object.defineProperty(o, "qux", {
            configurable: false,
            get() {
                return this.secret_qux + 2;
            },
        });
    }, {
        error: TypeError,
        message: "Cannot change attributes of non-configurable property 'qux'",
    });

    assertThrowsError(() => {
        Object.defineProperty(o, "qux", { value: 2 });
    }, {
        error: TypeError,
        message: "Cannot change attributes of non-configurable property 'qux'",
    });

    assertThrowsError(() => {
        Object.defineProperty(o, "a", {
            get() {},
            value: 9,
        });
    }, {
        error: TypeError,
        message: "Accessor property descriptor cannot specify a value or writable key",
    });

    assertThrowsError(() => {
        Object.defineProperty(o, "a", {
            set() {},
            writable: true,
        });
    }, {
        error: TypeError,
        message: "Accessor property descriptor cannot specify a value or writable key",
    });

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