summaryrefslogtreecommitdiff
path: root/installed-tests/js/testPrint.js
blob: 5432c8c3514e19546901256d4c6349d2b29d05cf (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2020 Philip Chimento <philip.chimento@gmail.com>
// SPDX-FileCopyrightText: 2022 Nasah Kuma <nasahnash19@gmail.com>

imports.gi.versions.Gdk = '3.0';
const Gdk = imports.gi.Gdk;
const {getPrettyPrintFunction} = imports._print;
let prettyPrint = getPrettyPrintFunction(globalThis);

describe('print', function () {
    it('can be spied upon', function () {
        spyOn(globalThis, 'print');
        print('foo');
        expect(print).toHaveBeenCalledWith('foo');
    });
});

describe('printerr', function () {
    it('can be spied upon', function () {
        spyOn(globalThis, 'printerr');
        printerr('foo');
        expect(printerr).toHaveBeenCalledWith('foo');
    });
});

describe('log', function () {
    it('can be spied upon', function () {
        spyOn(globalThis, 'log');
        log('foo');
        expect(log).toHaveBeenCalledWith('foo');
    });
});

describe('logError', function () {
    it('can be spied upon', function () {
        spyOn(globalThis, 'logError');
        logError('foo', 'bar');
        expect(logError).toHaveBeenCalledWith('foo', 'bar');
    });
});

describe('prettyPrint', function () {
    it('property value primitive', function () {
        expect(
            prettyPrint({greeting: 'hi'})
        ).toBe('{ greeting: "hi" }');
    });

    it('property value is object reference', function () {
        let obj = {a: 5};
        obj.b = obj;
        expect(
            prettyPrint(obj)
        ).toBe('{ a: 5, b: [Circular] }');
    });

    it('more than one property', function () {
        expect(
            prettyPrint({a: 1, b: 2, c: 3})
        ).toBe('{ a: 1, b: 2, c: 3 }');
    });

    it('add property value after property value object reference', function () {
        let obj = {a: 5};
        obj.b = obj;
        obj.c = 4;
        expect(
            prettyPrint(obj)
        ).toBe('{ a: 5, b: [Circular], c: 4 }');
    });

    it('array', function () {
        expect(
            prettyPrint([1, 2, 3, 4, 5])
        ).toBe('[1, 2, 3, 4, 5]');
    });

    it('property value array', function () {
        expect(
            prettyPrint({arr: [1, 2, 3, 4, 5]})
        ).toBe('{ arr: [1, 2, 3, 4, 5] }');
    });

    it('array reference is the only array element', function () {
        let arr = [];
        arr.push(arr);
        expect(
            prettyPrint(arr)
        ).toBe('[[Circular]]');
    });

    it('array reference is one of multiple array elements', function () {
        let arr = [];
        arr.push(4);
        arr.push(arr);
        arr.push(5);
        expect(
            prettyPrint(arr)
        ).toBe('[4, [Circular], 5]');
    });

    it('nested array', function () {
        expect(
            prettyPrint([1, 2, [3, 4], 5])
        ).toBe('[1, 2, [3, 4], 5]');
    });

    it('property value nested array', function () {
        expect(
            prettyPrint({arr: [1, 2, [3, 4], 5]})
        ).toBe('{ arr: [1, 2, [3, 4], 5] }');
    });

    it('function', function () {
        expect(
            prettyPrint(function sum(a, b) {
                return a + b;
            })
        ).toBe('[ Function: sum ]');
    });

    it('property value function', function () {
        expect(
            prettyPrint({
                sum: function sum(a, b) {
                    return a + b;
                },
            })
        ).toBe('{ sum: [ Function: sum ] }');
    });

    it('date', function () {
        expect(
            prettyPrint(new Date(Date.UTC(2018, 11, 24, 10, 33, 30)))
        ).toBe('2018-12-24T10:33:30.000Z');
    });

    it('property value date', function () {
        expect(
            prettyPrint({date: new Date(Date.UTC(2018, 11, 24, 10, 33, 30))})
        ).toBe('{ date: 2018-12-24T10:33:30.000Z }');
    });

    it('toString is overridden on object', function () {
        expect(
            prettyPrint(new Gdk.Rectangle())
        ).toMatch(/\[boxed instance wrapper GIName:.*\]/);
    });

    it('string tag supplied', function () {
        expect(
            prettyPrint(Gdk)
        ).toMatch('[object GIRepositoryNamespace]');
    });

    it('symbol', function () {
        expect(prettyPrint(Symbol('foo'))).toEqual('Symbol("foo")');
    });

    it('property key symbol', function () {
        expect(prettyPrint({[Symbol('foo')]: 'symbol'}))
            .toEqual('{ [Symbol("foo")]: "symbol" }');
    });

    it('property value symbol', function () {
        expect(prettyPrint({symbol: Symbol('foo')}))
            .toEqual('{ symbol: Symbol("foo") }');
    });

    it('registered symbol', function () {
        expect(prettyPrint(Symbol.for('foo'))).toEqual('Symbol.for("foo")');
    });

    it('property key registered symbol', function () {
        expect(prettyPrint({[Symbol.for('foo')]: 'symbol'}))
            .toEqual('{ [Symbol.for("foo")]: "symbol" }');
    });

    it('property value registered symbol', function () {
        expect(prettyPrint({symbol: Symbol.for('foo')}))
            .toEqual('{ symbol: Symbol.for("foo") }');
    });

    it('well-known symbol', function () {
        expect(prettyPrint(Symbol.hasInstance)).toEqual('Symbol.hasInstance');
    });

    it('property key well-known symbol', function () {
        expect(prettyPrint({[Symbol.iterator]: 'symbol'}))
            .toEqual('{ [Symbol.iterator]: "symbol" }');
    });

    it('property value well-known symbol', function () {
        expect(prettyPrint({symbol: Symbol.hasInstance}))
            .toEqual('{ symbol: Symbol.hasInstance }');
    });
});