@@ -64,25 +64,11 @@ var matchers = {
64
64
} ;
65
65
} ,
66
66
67
- // toBeCloseTo... but for arrays
68
67
toBeCloseToArray : function ( ) {
69
68
return {
70
69
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 ) ;
86
72
87
73
var message = [
88
74
'Expected' , actual , 'to be close to' , expected , msgExtra
@@ -96,30 +82,11 @@ var matchers = {
96
82
} ;
97
83
} ,
98
84
99
- // toBeCloseTo... but for 2D arrays
100
85
toBeCloseTo2DArray : function ( ) {
101
86
return {
102
87
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 ) ;
123
90
124
91
var message = [
125
92
'Expected' ,
@@ -140,7 +107,29 @@ var matchers = {
140
107
toBeWithin : function ( ) {
141
108
return {
142
109
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 ) ;
144
133
145
134
var message = [
146
135
'Expected' , actual ,
@@ -183,15 +172,59 @@ var matchers = {
183
172
}
184
173
} ;
185
174
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
+ ) ;
189
185
}
186
+ return false ;
187
+ }
190
188
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
+ } ;
195
228
}
196
229
197
230
function coercePosition ( precision ) {
0 commit comments