@@ -81,12 +81,35 @@ const assert = module.exports = ok;
81
81
82
82
const NO_EXCEPTION_SENTINEL = { } ;
83
83
84
+ class Comparison {
85
+ constructor ( obj , keys , actual ) {
86
+ for ( const key of keys ) {
87
+ if ( key in obj ) {
88
+ if ( actual !== undefined &&
89
+ typeof actual [ key ] === 'string' &&
90
+ isRegExp ( obj [ key ] ) &&
91
+ RegExpPrototypeExec ( obj [ key ] , actual [ key ] ) !== null ) {
92
+ this [ key ] = actual [ key ] ;
93
+ } else {
94
+ this [ key ] = obj [ key ] ;
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
100
+
84
101
class Assert {
85
102
constructor ( options = { } ) {
86
103
this . options = options ;
87
104
this . AssertionError = AssertionError ;
88
105
}
89
106
107
+ // All of the following functions must throw an AssertionError
108
+ // when a corresponding condition is not met, with a message that
109
+ // may be undefined if not provided. All assertion methods provide
110
+ // both the actual and expected values to the assertion error for
111
+ // display purposes.
112
+
90
113
#innerFail( obj ) {
91
114
if ( obj . message instanceof Error ) throw obj . message ;
92
115
@@ -323,7 +346,7 @@ class Assert {
323
346
error = undefined ;
324
347
}
325
348
326
- if ( ! error || hasMatchingError ( actual , error ) ) {
349
+ if ( ! error || this . # hasMatchingError( actual , error ) ) {
327
350
const details = message ? `: ${ message } ` : '.' ;
328
351
const fnType = stackStartFn === Assert . prototype . doesNotReject ?
329
352
'rejection' : 'exception' ;
@@ -339,6 +362,26 @@ class Assert {
339
362
throw actual ;
340
363
}
341
364
365
+ #hasMatchingError( actual , expected ) {
366
+ if ( typeof expected !== 'function' ) {
367
+ if ( isRegExp ( expected ) ) {
368
+ const str = String ( actual ) ;
369
+ return RegExpPrototypeExec ( expected , str ) !== null ;
370
+ }
371
+ throw new ERR_INVALID_ARG_TYPE (
372
+ 'expected' , [ 'Function' , 'RegExp' ] , expected ,
373
+ ) ;
374
+ }
375
+ // Guard instanceof against arrow functions as they don't have a prototype.
376
+ if ( expected . prototype !== undefined && actual instanceof expected ) {
377
+ return true ;
378
+ }
379
+ if ( ObjectPrototypeIsPrototypeOf ( Error , expected ) ) {
380
+ return false ;
381
+ }
382
+ return ReflectApply ( expected , { } , [ actual ] ) === true ;
383
+ }
384
+
342
385
async #waitForActual( promiseFn ) {
343
386
let resultPromise ;
344
387
if ( typeof promiseFn === 'function' ) {
@@ -796,48 +839,6 @@ function ok(...args) {
796
839
ObjectAssign ( assert , assertInstance ) ;
797
840
assert . ok = ok ;
798
841
799
- // All of the following functions must throw an AssertionError
800
- // when a corresponding condition is not met, with a message that
801
- // may be undefined if not provided. All assertion methods provide
802
- // both the actual and expected values to the assertion error for
803
- // display purposes.
804
- class Comparison {
805
- constructor ( obj , keys , actual ) {
806
- for ( const key of keys ) {
807
- if ( key in obj ) {
808
- if ( actual !== undefined &&
809
- typeof actual [ key ] === 'string' &&
810
- isRegExp ( obj [ key ] ) &&
811
- RegExpPrototypeExec ( obj [ key ] , actual [ key ] ) !== null ) {
812
- this [ key ] = actual [ key ] ;
813
- } else {
814
- this [ key ] = obj [ key ] ;
815
- }
816
- }
817
- }
818
- }
819
- }
820
-
821
- function hasMatchingError ( actual , expected ) {
822
- if ( typeof expected !== 'function' ) {
823
- if ( isRegExp ( expected ) ) {
824
- const str = String ( actual ) ;
825
- return RegExpPrototypeExec ( expected , str ) !== null ;
826
- }
827
- throw new ERR_INVALID_ARG_TYPE (
828
- 'expected' , [ 'Function' , 'RegExp' ] , expected ,
829
- ) ;
830
- }
831
- // Guard instanceof against arrow functions as they don't have a prototype.
832
- if ( expected . prototype !== undefined && actual instanceof expected ) {
833
- return true ;
834
- }
835
- if ( ObjectPrototypeIsPrototypeOf ( Error , expected ) ) {
836
- return false ;
837
- }
838
- return ReflectApply ( expected , { } , [ actual ] ) === true ;
839
- }
840
-
841
842
/**
842
843
* Expose a strict only variant of assert.
843
844
* @param {...any } args
0 commit comments