2222
2323const { compare } = process . binding ( 'buffer' ) ;
2424const util = require ( 'util' ) ;
25- const Buffer = require ( 'buffer' ) . Buffer ;
25+ const { Buffer } = require ( 'buffer' ) ;
2626const pToString = ( obj ) => Object . prototype . toString . call ( obj ) ;
2727
2828// The assert module provides functions that throw
@@ -76,53 +76,65 @@ function getMessage(self) {
7676// both the actual and expected values to the assertion error for
7777// display purposes.
7878
79+ function innerFail ( actual , expected , message , operator , stackStartFunction ) {
80+ throw new assert . AssertionError ( {
81+ message,
82+ actual,
83+ expected,
84+ operator,
85+ stackStartFunction
86+ } ) ;
87+ }
88+
7989function fail ( actual , expected , message , operator , stackStartFunction ) {
80- if ( arguments . length === 1 )
90+ if ( arguments . length === 0 ) {
91+ message = 'Failed' ;
92+ }
93+ if ( arguments . length === 1 ) {
8194 message = actual ;
82- if ( arguments . length === 2 )
95+ actual = undefined ;
96+ }
97+ if ( arguments . length === 2 ) {
8398 operator = '!=' ;
84- throw new assert . AssertionError ( {
85- message : message ,
86- actual : actual ,
87- expected : expected ,
88- operator : operator ,
89- stackStartFunction : stackStartFunction
90- } ) ;
99+ }
100+ innerFail ( actual , expected , message , operator , stackStartFunction || fail ) ;
91101}
92102assert . fail = fail ;
93103
94104// Pure assertion tests whether a value is truthy, as determined
95105// by !!value.
96106function ok ( value , message ) {
97- if ( ! value ) fail ( value , true , message , '==' , ok ) ;
107+ if ( ! value ) innerFail ( value , true , message , '==' , ok ) ;
98108}
99109assert . ok = ok ;
100110
101111// The equality assertion tests shallow, coercive equality with ==.
102112/* eslint-disable no-restricted-properties */
103113assert . equal = function equal ( actual , expected , message ) {
104- if ( actual != expected ) fail ( actual , expected , message , '==' , assert . equal ) ;
114+ // eslint-disable-next-line eqeqeq
115+ if ( actual != expected ) innerFail ( actual , expected , message , '==' , equal ) ;
105116} ;
106117
107118// The non-equality assertion tests for whether two objects are not
108119// equal with !=.
109120assert . notEqual = function notEqual ( actual , expected , message ) {
121+ // eslint-disable-next-line eqeqeq
110122 if ( actual == expected ) {
111- fail ( actual , expected , message , '!=' , notEqual ) ;
123+ innerFail ( actual , expected , message , '!=' , notEqual ) ;
112124 }
113125} ;
114126
115127// The equivalence assertion tests a deep equality relation.
116128assert . deepEqual = function deepEqual ( actual , expected , message ) {
117129 if ( ! innerDeepEqual ( actual , expected , false ) ) {
118- fail ( actual , expected , message , 'deepEqual' , deepEqual ) ;
130+ innerFail ( actual , expected , message , 'deepEqual' , deepEqual ) ;
119131 }
120132} ;
121133/* eslint-enable */
122134
123135assert . deepStrictEqual = function deepStrictEqual ( actual , expected , message ) {
124136 if ( ! innerDeepEqual ( actual , expected , true ) ) {
125- fail ( actual , expected , message , 'deepStrictEqual' , deepStrictEqual ) ;
137+ innerFail ( actual , expected , message , 'deepStrictEqual' , deepStrictEqual ) ;
126138 }
127139} ;
128140
@@ -246,31 +258,30 @@ function objEquiv(a, b, strict, actualVisitedObjects) {
246258// The non-equivalence assertion tests for any deep inequality.
247259assert . notDeepEqual = function notDeepEqual ( actual , expected , message ) {
248260 if ( innerDeepEqual ( actual , expected , false ) ) {
249- fail ( actual , expected , message , 'notDeepEqual' , notDeepEqual ) ;
261+ innerFail ( actual , expected , message , 'notDeepEqual' , notDeepEqual ) ;
250262 }
251263} ;
252264
253265assert . notDeepStrictEqual = notDeepStrictEqual ;
254266function notDeepStrictEqual ( actual , expected , message ) {
255267 if ( innerDeepEqual ( actual , expected , true ) ) {
256- fail ( actual , expected , message , 'notDeepStrictEqual' ,
257- notDeepStrictEqual ) ;
268+ innerFail ( actual , expected , message , 'notDeepStrictEqual' ,
269+ notDeepStrictEqual ) ;
258270 }
259271}
260272
261-
262273// The strict equality assertion tests strict equality, as determined by ===.
263274assert . strictEqual = function strictEqual ( actual , expected , message ) {
264275 if ( actual !== expected ) {
265- fail ( actual , expected , message , '===' , strictEqual ) ;
276+ innerFail ( actual , expected , message , '===' , strictEqual ) ;
266277 }
267278} ;
268279
269280// The strict non-equality assertion tests for strict inequality, as
270281// determined by !==.
271282assert . notStrictEqual = function notStrictEqual ( actual , expected , message ) {
272283 if ( actual === expected ) {
273- fail ( actual , expected , message , '!==' , notStrictEqual ) ;
284+ innerFail ( actual , expected , message , '!==' , notStrictEqual ) ;
274285 }
275286} ;
276287
@@ -325,7 +336,7 @@ function innerThrows(shouldThrow, block, expected, message) {
325336 ( message ? ' ' + message : '.' ) ;
326337
327338 if ( shouldThrow && ! actual ) {
328- fail ( actual , expected , 'Missing expected exception' + message , fail ) ;
339+ innerFail ( actual , expected , 'Missing expected exception' + message , fail ) ;
329340 }
330341
331342 const userProvidedMessage = typeof message === 'string' ;
@@ -336,7 +347,7 @@ function innerThrows(shouldThrow, block, expected, message) {
336347 userProvidedMessage &&
337348 expectedException ( actual , expected ) ) ||
338349 isUnexpectedException ) {
339- fail ( actual , expected , 'Got unwanted exception' + message , fail ) ;
350+ innerFail ( actual , expected , 'Got unwanted exception' + message , fail ) ;
340351 }
341352
342353 if ( ( shouldThrow && actual && expected &&
@@ -354,4 +365,4 @@ assert.doesNotThrow = function doesNotThrow(block, error, message) {
354365 innerThrows ( false , block , error , message ) ;
355366} ;
356367
357- assert . ifError = function ifError ( err ) { if ( err ) throw err ; } ;
368+ assert . ifError = function ( err ) { if ( err ) throw err ; } ;
0 commit comments