@@ -124,12 +124,20 @@ class LiteralMatch extends Matcher {
124
124
125
125
const result = new MatchResult ( actual ) ;
126
126
if ( typeof this . pattern !== typeof actual ) {
127
- result . push ( this , [ ] , `Expected type ${ typeof this . pattern } but received ${ getType ( actual ) } ` ) ;
127
+ result . recordFailure ( {
128
+ matcher : this ,
129
+ path : [ ] ,
130
+ message : `Expected type ${ typeof this . pattern } but received ${ getType ( actual ) } ` ,
131
+ } ) ;
128
132
return result ;
129
133
}
130
134
131
135
if ( actual !== this . pattern ) {
132
- result . push ( this , [ ] , `Expected ${ this . pattern } but received ${ actual } ` ) ;
136
+ result . recordFailure ( {
137
+ matcher : this ,
138
+ path : [ ] ,
139
+ message : `Expected ${ this . pattern } but received ${ actual } ` ,
140
+ } ) ;
133
141
}
134
142
135
143
return result ;
@@ -166,10 +174,18 @@ class ArrayMatch extends Matcher {
166
174
167
175
public test ( actual : any ) : MatchResult {
168
176
if ( ! Array . isArray ( actual ) ) {
169
- return new MatchResult ( actual ) . push ( this , [ ] , `Expected type array but received ${ getType ( actual ) } ` ) ;
177
+ return new MatchResult ( actual ) . recordFailure ( {
178
+ matcher : this ,
179
+ path : [ ] ,
180
+ message : `Expected type array but received ${ getType ( actual ) } ` ,
181
+ } ) ;
170
182
}
171
183
if ( ! this . subsequence && this . pattern . length !== actual . length ) {
172
- return new MatchResult ( actual ) . push ( this , [ ] , `Expected array of length ${ this . pattern . length } but received ${ actual . length } ` ) ;
184
+ return new MatchResult ( actual ) . recordFailure ( {
185
+ matcher : this ,
186
+ path : [ ] ,
187
+ message : `Expected array of length ${ this . pattern . length } but received ${ actual . length } ` ,
188
+ } ) ;
173
189
}
174
190
175
191
let patternIdx = 0 ;
@@ -200,7 +216,11 @@ class ArrayMatch extends Matcher {
200
216
for ( ; patternIdx < this . pattern . length ; patternIdx ++ ) {
201
217
const pattern = this . pattern [ patternIdx ] ;
202
218
const element = ( Matcher . isMatcher ( pattern ) || typeof pattern === 'object' ) ? ' ' : ` [${ pattern } ] ` ;
203
- result . push ( this , [ ] , `Missing element${ element } at pattern index ${ patternIdx } ` ) ;
219
+ result . recordFailure ( {
220
+ matcher : this ,
221
+ path : [ ] ,
222
+ message : `Missing element${ element } at pattern index ${ patternIdx } ` ,
223
+ } ) ;
204
224
}
205
225
206
226
return result ;
@@ -236,21 +256,33 @@ class ObjectMatch extends Matcher {
236
256
237
257
public test ( actual : any ) : MatchResult {
238
258
if ( typeof actual !== 'object' || Array . isArray ( actual ) ) {
239
- return new MatchResult ( actual ) . push ( this , [ ] , `Expected type object but received ${ getType ( actual ) } ` ) ;
259
+ return new MatchResult ( actual ) . recordFailure ( {
260
+ matcher : this ,
261
+ path : [ ] ,
262
+ message : `Expected type object but received ${ getType ( actual ) } ` ,
263
+ } ) ;
240
264
}
241
265
242
266
const result = new MatchResult ( actual ) ;
243
267
if ( ! this . partial ) {
244
268
for ( const a of Object . keys ( actual ) ) {
245
269
if ( ! ( a in this . pattern ) ) {
246
- result . push ( this , [ `/${ a } ` ] , 'Unexpected key' ) ;
270
+ result . recordFailure ( {
271
+ matcher : this ,
272
+ path : [ `/${ a } ` ] ,
273
+ message : 'Unexpected key' ,
274
+ } ) ;
247
275
}
248
276
}
249
277
}
250
278
251
279
for ( const [ patternKey , patternVal ] of Object . entries ( this . pattern ) ) {
252
280
if ( ! ( patternKey in actual ) && ! ( patternVal instanceof AbsentMatch ) ) {
253
- result . push ( this , [ `/${ patternKey } ` ] , 'Missing key' ) ;
281
+ result . recordFailure ( {
282
+ matcher : this ,
283
+ path : [ `/${ patternKey } ` ] ,
284
+ message : 'Missing key' ,
285
+ } ) ;
254
286
continue ;
255
287
}
256
288
const matcher = Matcher . isMatcher ( patternVal ) ?
@@ -275,15 +307,23 @@ class SerializedJson extends Matcher {
275
307
public test ( actual : any ) : MatchResult {
276
308
const result = new MatchResult ( actual ) ;
277
309
if ( getType ( actual ) !== 'string' ) {
278
- result . push ( this , [ ] , `Expected JSON as a string but found ${ getType ( actual ) } ` ) ;
310
+ result . recordFailure ( {
311
+ matcher : this ,
312
+ path : [ ] ,
313
+ message : `Expected JSON as a string but found ${ getType ( actual ) } ` ,
314
+ } ) ;
279
315
return result ;
280
316
}
281
317
let parsed ;
282
318
try {
283
319
parsed = JSON . parse ( actual ) ;
284
320
} catch ( err ) {
285
321
if ( err instanceof SyntaxError ) {
286
- result . push ( this , [ ] , `Invalid JSON string: ${ actual } ` ) ;
322
+ result . recordFailure ( {
323
+ matcher : this ,
324
+ path : [ ] ,
325
+ message : `Invalid JSON string: ${ actual } ` ,
326
+ } ) ;
287
327
return result ;
288
328
} else {
289
329
throw err ;
@@ -311,7 +351,11 @@ class NotMatch extends Matcher {
311
351
const innerResult = matcher . test ( actual ) ;
312
352
const result = new MatchResult ( actual ) ;
313
353
if ( innerResult . failCount === 0 ) {
314
- result . push ( this , [ ] , `Found unexpected match: ${ JSON . stringify ( actual , undefined , 2 ) } ` ) ;
354
+ result . recordFailure ( {
355
+ matcher : this ,
356
+ path : [ ] ,
357
+ message : `Found unexpected match: ${ JSON . stringify ( actual , undefined , 2 ) } ` ,
358
+ } ) ;
315
359
}
316
360
return result ;
317
361
}
@@ -325,7 +369,11 @@ class AnyMatch extends Matcher {
325
369
public test ( actual : any ) : MatchResult {
326
370
const result = new MatchResult ( actual ) ;
327
371
if ( actual == null ) {
328
- result . push ( this , [ ] , 'Expected a value but found none' ) ;
372
+ result . recordFailure ( {
373
+ matcher : this ,
374
+ path : [ ] ,
375
+ message : 'Expected a value but found none' ,
376
+ } ) ;
329
377
}
330
378
return result ;
331
379
}
0 commit comments