summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/object-basic.js
blob: 5b3b78fb19de3ce54710b1cc45f895c6a87f7f10 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
describe("correct behavior", () => {
    test("numeric indexing", () => {
        const o = { 1: 23 };

        expect(o[1]).toBe(23);
        expect(o[1n]).toBe(23);
        expect(o["1"]).toBe(23);

        o[10] = "123";
        expect(o[10]).toBe("123");
        expect(o["10"]).toBe("123");

        o[10n] = "1234";
        expect(o[10]).toBe("1234");
        expect(o["10"]).toBe("1234");
    });

    test("string indexing", () => {
        let foo = "bar";

        const o = {
            foo,
            bar: "baz",
            qux: true ? 10 : 20,
            hello: "friends",
        };

        expect(o.foo).toBe("bar");
        expect(o["foo"]).toBe("bar");
        expect(o.qux).toBe(10), expect(o.hello).toBe("friends");
        expect(o["hello"]).toBe("friends");
    });

    test("symbol keys", () => {
        let object = {};
        let symbol = Symbol("foo");

        object[symbol] = 2;
        expect(object[symbol]).toBe(2);
    });

    test("numeric keys", () => {
        const hex = { 0x10: "16" };
        const oct = { 0o10: "8" };
        const bin = { 0b10: "2" };
        const float = { 0.5: "0.5" };

        expect(hex["16"]).toBe("16");
        expect(oct["8"]).toBe("8");
        expect(bin["2"]).toBe("2");
        expect(float["0.5"]).toBe("0.5");
    });

    test("computed properties", () => {
        const foo = "bar";
        const computed = "computed";
        const o = {
            [1 + 2]: 42,
            [`I am a ${computed} key`]: foo,
        };

        expect(o[3]).toBe(42);
        expect(o["I am a computed key"]).toBe("bar");
    });

    test("duplicate keys", () => {
        const o = {
            duplicate: "hello",
            duplicate: "world",
        };
        expect(o.duplicate).toBe("world");
    });

    test("assigning after creation", () => {
        const o = {};
        o.baz = "test";

        expect(o.baz).toBe("test");
        expect(o["baz"]).toBe("test");

        expect(o[-1]).toBeUndefined();
        o[-1] = "hello friends";
        expect(o[-1]).toBe("hello friends");
        expect(o["-1"]).toBe("hello friends");
    });

    test("floating point keys", () => {
        const math = { 3.14: "pi" };
        expect(math["3.14"]).toBe("pi");
        expect(math[3.14]).toBe("pi");
    });

    test("keywords as property keys", () => {
        const o2 = {
            return: 1,
            yield: 1,
            for: 1,
            catch: 1,
            break: 1,
        };

        expect(o2.return).toBe(1);
        expect(o2.yield).toBe(1);
        expect(o2.for).toBe(1);
        expect(o2.catch).toBe(1);
        expect(o2.break).toBe(1);
    });

    test("prototypical inheritance", () => {
        var base = {
            getNumber() {
                return 10;
            },
        };

        var derived = {
            getNumber() {
                return 20 + super.getNumber();
            },
        };

        Object.setPrototypeOf(derived, base);
        expect(derived.getNumber()).toBe(30);
    });
});

describe("side effects", () => {
    let a;
    const append = x => {
        a.push(x);
    };

    test("computed key side effects", () => {
        a = [];
        const o3 = { [append(1)]: 1, [append(2)]: 2, [append(3)]: 3 };
        expect(a).toHaveLength(3);
        expect(a[0]).toBe(1);
        expect(a[1]).toBe(2);
        expect(a[2]).toBe(3);
        expect(o3.undefined).toBe(3);
    });

    test("value side effects", () => {
        a = [];
        const o4 = { test: append(1), test: append(2), test: append(3) };
        expect(a).toHaveLength(3);
        expect(a[0]).toBe(1);
        expect(a[1]).toBe(2);
        expect(a[2]).toBe(3);
        expect(o4.test).toBeUndefined();
    });
});

describe("errors", () => {
    test("syntax errors", () => {
        expect("({ foo: function() { super.bar; } })").not.toEval();
        expect("({ get ...foo })").not.toEval();
        expect("({ get... foo })").not.toEval();
        expect("({ get foo })").not.toEval();
        expect("({ get foo: bar })").not.toEval();
        expect("({ get [foo]: bar })").not.toEval();
        expect("({ get ...[foo] })").not.toEval();
        expect("({ get foo(bar) {} })").not.toEval();
        expect("({ set foo() {} })").not.toEval();
        expect("({ set foo(...bar) {} })").not.toEval();
        expect("({ set foo(bar, baz) {} })").not.toEval();
        expect("({ ...foo: bar })").not.toEval();
    });
});