/
chai-shallow-deep-equal.js
101 lines (84 loc) · 2.91 KB
/
chai-shallow-deep-equal.js
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
"use strict";
(function (plugin) {
if (
typeof require === "function" &&
typeof exports === "object" &&
typeof module === "object"
) {
// NodeJS
module.exports = plugin;
} else if (
typeof define === "function" &&
define.amd
) {
// AMD
define(function () {
return plugin;
});
} else {
// Other environment (usually <script> tag): plug in to global chai instance directly.
chai.use(plugin);
}
}(function (chai, utils) {
function shallowDeepEqual(expect, actual, path) {
// null value
if (expect === null) {
if (! (actual === null)) {
throw 'Expected to have null but got "' + actual +'" at path "'+ path +'".';
}
return true;
}
// undefined expected value
if (typeof expect == 'undefined') {
if (typeof actual != 'undefined') {
throw 'Expected to have undefined but got "' + actual +'" at path "'+ path +'".';
}
return true;
}
// scalar description
if (/boolean|number|string/.test(typeof expect)) {
if (expect != actual) {
throw 'Expected to have "' + expect +'" but got "'+ actual +'" at path "'+ path +'".';
}
return true;
}
// dates
if (expect instanceof Date) {
if (actual instanceof Date) {
if (expect.getTime() != actual.getTime()) {
throw(
'Expected to have date "' + expect.toISOString() + '" but got ' +
'"' + actual.toISOString() + '" at path "' + path + '".'
);
}
} else {
throw(
'Expected to have date "' + expect.toISOString() + '" but got ' +
'"' + actual + '" at path "' + path + '".'
);
}
}
if (actual === null) {
throw 'Expected to have an array/object but got null at path "' + path + '".';
}
// array/object description
for (var prop in expect) {
if (typeof actual[prop] == 'undefined' && typeof expect[prop] != 'undefined') {
throw 'Expected "' + prop + '" field to be defined at path "' + path + '".';
}
shallowDeepEqual(expect[prop], actual[prop], path + (path == '/' ? '' : '/') + prop);
}
return true;
}
chai.Assertion.addMethod('shallowDeepEqual', function (expect) {
try {
shallowDeepEqual(expect, this._obj, '/');
}
catch (msg) {
this.assert(false, msg, undefined, expect, this._obj, true);
}
});
chai.assert.shallowDeepEqual = function(val, exp, msg) {
new chai.Assertion(val, msg).to.be.shallowDeepEqual(exp);
}
}));