Skip to content

Commit 3b314fe

Browse files
committed
add toBeWithinArray custom jasmine matcher
1 parent ca1de5d commit 3b314fe

File tree

1 file changed

+78
-45
lines changed

1 file changed

+78
-45
lines changed

test/jasmine/assets/custom_matchers.js

Lines changed: 78 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,11 @@ var matchers = {
6464
};
6565
},
6666

67-
// toBeCloseTo... but for arrays
6867
toBeCloseToArray: function() {
6968
return {
7069
compare: function(actual, expected, precision, msgExtra) {
71-
precision = coercePosition(precision);
72-
73-
var passed;
74-
75-
if(Array.isArray(actual) && Array.isArray(expected)) {
76-
var tested = actual.map(function(element, i) {
77-
return isClose(element, expected[i], precision);
78-
});
79-
80-
passed = (
81-
expected.length === actual.length &&
82-
tested.indexOf(false) < 0
83-
);
84-
}
85-
else passed = false;
70+
var testFn = makeIsCloseFn(coercePosition(precision));
71+
var passed = assertArray(actual, expected, testFn);
8672

8773
var message = [
8874
'Expected', actual, 'to be close to', expected, msgExtra
@@ -96,30 +82,11 @@ var matchers = {
9682
};
9783
},
9884

99-
// toBeCloseTo... but for 2D arrays
10085
toBeCloseTo2DArray: function() {
10186
return {
10287
compare: function(actual, expected, precision, msgExtra) {
103-
precision = coercePosition(precision);
104-
105-
var passed = true;
106-
107-
if(expected.length !== actual.length) passed = false;
108-
else {
109-
for(var i = 0; i < expected.length; ++i) {
110-
if(expected[i].length !== actual[i].length) {
111-
passed = false;
112-
break;
113-
}
114-
115-
for(var j = 0; j < expected[i].length; ++j) {
116-
if(!isClose(actual[i][j], expected[i][j], precision)) {
117-
passed = false;
118-
break;
119-
}
120-
}
121-
}
122-
}
88+
var testFn = makeIsCloseFn(coercePosition(precision));
89+
var passed = assert2DArray(actual, expected, testFn);
12390

12491
var message = [
12592
'Expected',
@@ -140,7 +107,29 @@ var matchers = {
140107
toBeWithin: function() {
141108
return {
142109
compare: function(actual, expected, tolerance, msgExtra) {
143-
var passed = Math.abs(actual - expected) < tolerance;
110+
var testFn = makeIsWithinFn(tolerance);
111+
var passed = testFn(actual, expected);
112+
113+
var message = [
114+
'Expected', actual,
115+
'to be close to', expected,
116+
'within', tolerance,
117+
msgExtra
118+
].join(' ');
119+
120+
return {
121+
pass: passed,
122+
message: message
123+
};
124+
}
125+
};
126+
},
127+
128+
toBeWithinArray: function() {
129+
return {
130+
compare: function(actual, expected, tolerance, msgExtra) {
131+
var testFn = makeIsWithinFn(tolerance);
132+
var passed = assertArray(actual, expected, testFn);
144133

145134
var message = [
146135
'Expected', actual,
@@ -183,15 +172,59 @@ var matchers = {
183172
}
184173
};
185174

186-
function isClose(actual, expected, precision) {
187-
if(isNumeric(actual) && isNumeric(expected)) {
188-
return Math.abs(actual - expected) < precision;
175+
function assertArray(actual, expected, testFn) {
176+
if(Array.isArray(actual) && Array.isArray(expected)) {
177+
var tested = actual.map(function(element, i) {
178+
return testFn(element, expected[i]);
179+
});
180+
181+
return (
182+
expected.length === actual.length &&
183+
tested.indexOf(false) < 0
184+
);
189185
}
186+
return false;
187+
}
190188

191-
return (
192-
actual === expected ||
193-
(isNaN(actual) && isNaN(expected))
194-
);
189+
function assert2DArray(actual, expected, testFn) {
190+
if(expected.length !== actual.length) return false;
191+
192+
for(var i = 0; i < expected.length; i++) {
193+
if(expected[i].length !== actual[i].length) {
194+
return false;
195+
}
196+
197+
for(var j = 0; j < expected[i].length; j++) {
198+
if(!testFn(actual[i][j], expected[i][j])) {
199+
return false;
200+
}
201+
}
202+
}
203+
return true;
204+
}
205+
206+
function makeIsCloseFn(precision) {
207+
return function isClose(actual, expected) {
208+
if(isNumeric(actual) && isNumeric(expected)) {
209+
return Math.abs(actual - expected) < precision;
210+
}
211+
return (
212+
actual === expected ||
213+
(isNaN(actual) && isNaN(expected))
214+
);
215+
};
216+
}
217+
218+
function makeIsWithinFn(tolerance) {
219+
return function isWithin(actual, expected) {
220+
if(isNumeric(actual) && isNumeric(expected)) {
221+
return Math.abs(actual - expected) < tolerance;
222+
}
223+
return (
224+
actual === expected ||
225+
(isNaN(actual) && isNaN(expected))
226+
);
227+
};
195228
}
196229

197230
function coercePosition(precision) {

0 commit comments

Comments
 (0)