-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcustom_matchers.js
216 lines (183 loc) · 6.44 KB
/
custom_matchers.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* custom_matchers - to be included in karma.conf.js, so it can
* add these matchers to jasmine globally and all suites have access.
*
* Also adds `.negateIf` which is not a matcher but a conditional `.not`:
*
* expect(x).negateIf(condition).toBe(0);
*
* is equivalent to:
*
* if(condition) expect(x).toBe(0);
* else expect(x).not.toBe(0);
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var isPlainObject = require('../../../src/lib/is_plain_object');
var extendDeep = require('../../../src/lib/extend').extendDeep;
var deepEqual = require('deep-equal');
var matchers = {
// toEqual except with sparse arrays populated. This arises because:
//
// var x = new Array(2)
// expect(x).toEqual([undefined, undefined])
//
// will fail assertion even though x[0] === undefined and x[1] === undefined.
// This is because the array elements don't exist until assigned. Of course it
// only fails on *some* platforms (old firefox, looking at you), which is why
// this is worth all the footwork.
toLooseDeepEqual: function() {
function populateUndefinedArrayEls(x) {
var i;
if(Array.isArray(x)) {
for(i = 0; i < x.length; i++) {
if(x[i] === undefined) x[i] = undefined;
}
} else if(isPlainObject(x)) {
var keys = Object.keys(x);
for(i = 0; i < keys.length; i++) {
populateUndefinedArrayEls(x[keys[i]]);
}
}
return x;
}
return {
compare: function(actual, expected, msgExtra) {
var actualExpanded = populateUndefinedArrayEls(extendDeep({}, actual));
var expectedExpanded = populateUndefinedArrayEls(extendDeep({}, expected));
var passed = deepEqual(actualExpanded, expectedExpanded);
var message = [
'Expected', JSON.stringify(actual), 'to be close to', JSON.stringify(expected), msgExtra
].join(' ');
return {
pass: passed,
message: message
};
}
};
},
// toBeCloseTo... but for arrays
toBeCloseToArray: function() {
return {
compare: function(actual, expected, precision, msgExtra) {
precision = coercePosition(precision);
var passed;
if(Array.isArray(actual) && Array.isArray(expected)) {
var tested = actual.map(function(element, i) {
return isClose(element, expected[i], precision);
});
passed = (
expected.length === actual.length &&
tested.indexOf(false) < 0
);
}
else passed = false;
var message = [
'Expected', actual, 'to be close to', expected, msgExtra
].join(' ');
return {
pass: passed,
message: message
};
}
};
},
// toBeCloseTo... but for 2D arrays
toBeCloseTo2DArray: function() {
return {
compare: function(actual, expected, precision, msgExtra) {
precision = coercePosition(precision);
var passed = true;
if(expected.length !== actual.length) passed = false;
else {
for(var i = 0; i < expected.length; ++i) {
if(expected[i].length !== actual[i].length) {
passed = false;
break;
}
for(var j = 0; j < expected[i].length; ++j) {
if(!isClose(actual[i][j], expected[i][j], precision)) {
passed = false;
break;
}
}
}
}
var message = [
'Expected',
arrayToStr(actual.map(arrayToStr)),
'to be close to',
arrayToStr(expected.map(arrayToStr)),
msgExtra
].join('\n');
return {
pass: passed,
message: message
};
}
};
},
toBeWithin: function() {
return {
compare: function(actual, expected, tolerance, msgExtra) {
var passed = Math.abs(actual - expected) < tolerance;
var message = [
'Expected', actual,
'to be close to', expected,
'within', tolerance,
msgExtra
].join(' ');
return {
pass: passed,
message: message
};
}
};
},
toBeClassed: function() {
return {
compare: function(node, _expected, msgExtra) {
var actual = node.classList;
var expected = Array.isArray(_expected) ? _expected : [_expected];
var passed = (
actual.length === expected.length &&
expected.every(function(e) { return actual.contains(e); })
);
var message = [
'Expected classList', '[' + actual + ']',
'to have classes', expected,
msgExtra
].join(' ');
return {
pass: passed,
message: message
};
}
};
}
};
function isClose(actual, expected, precision) {
if(isNumeric(actual) && isNumeric(expected)) {
return Math.abs(actual - expected) < precision;
}
return (
actual === expected ||
(isNaN(actual) && isNaN(expected))
);
}
function coercePosition(precision) {
if(precision !== 0) {
precision = Math.pow(10, -precision) / 2 || 0.005;
}
return precision;
}
function arrayToStr(array) {
return '[' + array.join(', ') + ']';
}
beforeAll(function() {
jasmine.addMatchers(matchers);
jasmine.Expectation.prototype.negateIf = function(negate) {
if(negate) return this.not;
return this;
};
});