summaryrefslogtreecommitdiff
path: root/tests/assertions.js
blob: c33c81ec531ebdbad558252ee6a72c5caeb26b35 (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
// noVNC specific assertions
chai.use(function (_chai, utils) {
    function _equal(a, b) {
        return a === b;
    }
    _chai.Assertion.addMethod('displayed', function (targetData, cmp=_equal) {
        const obj = this._obj;
        const ctx = obj._target.getContext('2d');
        const data = ctx.getImageData(0, 0, obj._target.width, obj._target.height).data;
        const len = data.length;
        new chai.Assertion(len).to.be.equal(targetData.length, "unexpected display size");
        let same = true;
        for (let i = 0; i < len; i++) {
            if (!cmp(data[i], targetData[i])) {
                same = false;
                break;
            }
        }
        if (!same) {
            // eslint-disable-next-line no-console
            console.log("expected data: %o, actual data: %o", targetData, data);
        }
        this.assert(same,
                    "expected #{this} to have displayed the image #{exp}, but instead it displayed #{act}",
                    "expected #{this} not to have displayed the image #{act}",
                    targetData,
                    data);
    });

    _chai.Assertion.addMethod('sent', function (targetData) {
        const obj = this._obj;
        obj.inspect = () => {
            const res = { _websocket: obj._websocket, rQi: obj._rQi, _rQ: new Uint8Array(obj._rQ.buffer, 0, obj._rQlen),
                          _sQ: new Uint8Array(obj._sQ.buffer, 0, obj._sQlen) };
            res.prototype = obj;
            return res;
        };
        const data = obj._websocket._getSentData();
        let same = true;
        if (data.length != targetData.length) {
            same = false;
        } else {
            for (let i = 0; i < data.length; i++) {
                if (data[i] != targetData[i]) {
                    same = false;
                    break;
                }
            }
        }
        if (!same) {
            // eslint-disable-next-line no-console
            console.log("expected data: %o, actual data: %o", targetData, data);
        }
        this.assert(same,
                    "expected #{this} to have sent the data #{exp}, but it actually sent #{act}",
                    "expected #{this} not to have sent the data #{act}",
                    Array.prototype.slice.call(targetData),
                    Array.prototype.slice.call(data));
    });

    _chai.Assertion.addProperty('array', function () {
        utils.flag(this, 'array', true);
    });

    _chai.Assertion.overwriteMethod('equal', function (_super) {
        return function assertArrayEqual(target) {
            if (utils.flag(this, 'array')) {
                const obj = this._obj;

                let same = true;

                if (utils.flag(this, 'deep')) {
                    for (let i = 0; i < obj.length; i++) {
                        if (!utils.eql(obj[i], target[i])) {
                            same = false;
                            break;
                        }
                    }

                    this.assert(same,
                                "expected #{this} to have elements deeply equal to #{exp}",
                                "expected #{this} not to have elements deeply equal to #{exp}",
                                Array.prototype.slice.call(target));
                } else {
                    for (let i = 0; i < obj.length; i++) {
                        if (obj[i] != target[i]) {
                            same = false;
                            break;
                        }
                    }

                    this.assert(same,
                                "expected #{this} to have elements equal to #{exp}",
                                "expected #{this} not to have elements equal to #{exp}",
                                Array.prototype.slice.call(target));
                }
            } else {
                _super.apply(this, arguments);
            }
        };
    });
});