1
1
'use strict' ;
2
2
3
3
const {
4
+ ArrayPrototypeJoin,
5
+ ArrayPrototypePop,
4
6
Error,
5
7
ErrorCaptureStackTrace,
6
8
MathMax,
@@ -9,6 +11,10 @@ const {
9
11
ObjectGetPrototypeOf,
10
12
ObjectKeys,
11
13
String,
14
+ StringPrototypeEndsWith,
15
+ StringPrototypeRepeat,
16
+ StringPrototypeSlice,
17
+ StringPrototypeSplit,
12
18
} = primordials ;
13
19
14
20
const { inspect } = require ( 'internal/util/inspect' ) ;
@@ -79,8 +85,8 @@ function createErrDiff(actual, expected, operator) {
79
85
let end = '' ;
80
86
let skipped = false ;
81
87
const actualInspected = inspectValue ( actual ) ;
82
- const actualLines = actualInspected . split ( '\n' ) ;
83
- const expectedLines = inspectValue ( expected ) . split ( '\n' ) ;
88
+ const actualLines = StringPrototypeSplit ( actualInspected , '\n' ) ;
89
+ const expectedLines = StringPrototypeSplit ( inspectValue ( expected ) , '\n' ) ;
84
90
85
91
let i = 0 ;
86
92
let indicator = '' ;
@@ -127,7 +133,7 @@ function createErrDiff(actual, expected, operator) {
127
133
if ( i > 2 ) {
128
134
// Add position indicator for the first mismatch in case it is a
129
135
// single line and the input length is less than the column length.
130
- indicator = `\n ${ ' ' . repeat ( i ) } ^` ;
136
+ indicator = `\n ${ StringPrototypeRepeat ( ' ' , i ) } ^` ;
131
137
i = 0 ;
132
138
}
133
139
}
@@ -144,8 +150,8 @@ function createErrDiff(actual, expected, operator) {
144
150
} else {
145
151
other = a ;
146
152
}
147
- actualLines . pop ( ) ;
148
- expectedLines . pop ( ) ;
153
+ ArrayPrototypePop ( actualLines ) ;
154
+ ArrayPrototypePop ( expectedLines ) ;
149
155
if ( actualLines . length === 0 || expectedLines . length === 0 )
150
156
break ;
151
157
a = actualLines [ actualLines . length - 1 ] ;
@@ -157,18 +163,19 @@ function createErrDiff(actual, expected, operator) {
157
163
// E.g., assert.deepStrictEqual({ a: Symbol() }, { a: Symbol() })
158
164
if ( maxLines === 0 ) {
159
165
// We have to get the result again. The lines were all removed before.
160
- const actualLines = actualInspected . split ( '\n' ) ;
166
+ const actualLines = StringPrototypeSplit ( actualInspected , '\n' ) ;
161
167
162
168
// Only remove lines in case it makes sense to collapse those.
163
169
// TODO: Accept env to always show the full error.
164
170
if ( actualLines . length > 50 ) {
165
171
actualLines [ 46 ] = `${ blue } ...${ white } ` ;
166
172
while ( actualLines . length > 47 ) {
167
- actualLines . pop ( ) ;
173
+ ArrayPrototypePop ( actualLines ) ;
168
174
}
169
175
}
170
176
171
- return `${ kReadableOperator . notIdentical } \n\n${ actualLines . join ( '\n' ) } \n` ;
177
+ return `${ kReadableOperator . notIdentical } \n\n` +
178
+ `${ ArrayPrototypeJoin ( actualLines , '\n' ) } \n` ;
172
179
}
173
180
174
181
// There were at least five identical lines at the end. Mark a couple of
@@ -235,9 +242,10 @@ function createErrDiff(actual, expected, operator) {
235
242
// If the lines diverge, specifically check for lines that only diverge by
236
243
// a trailing comma. In that case it is actually identical and we should
237
244
// mark it as such.
238
- let divergingLines = actualLine !== expectedLine &&
239
- ( ! actualLine . endsWith ( ',' ) ||
240
- actualLine . slice ( 0 , - 1 ) !== expectedLine ) ;
245
+ let divergingLines =
246
+ actualLine !== expectedLine &&
247
+ ( ! StringPrototypeEndsWith ( actualLine , ',' ) ||
248
+ StringPrototypeSlice ( actualLine , 0 , - 1 ) !== expectedLine ) ;
241
249
// If the expected line has a trailing comma but is otherwise identical,
242
250
// add a comma at the end of the actual line. Otherwise the output could
243
251
// look weird as in:
@@ -248,8 +256,8 @@ function createErrDiff(actual, expected, operator) {
248
256
// ]
249
257
//
250
258
if ( divergingLines &&
251
- expectedLine . endsWith ( ',' ) &&
252
- expectedLine . slice ( 0 , - 1 ) === actualLine ) {
259
+ StringPrototypeEndsWith ( expectedLine , ',' ) &&
260
+ StringPrototypeSlice ( expectedLine , 0 , - 1 ) === actualLine ) {
253
261
divergingLines = false ;
254
262
actualLine += ',' ;
255
263
}
@@ -362,7 +370,7 @@ class AssertionError extends Error {
362
370
// In case the objects are equal but the operator requires unequal, show
363
371
// the first object and say A equals B
364
372
let base = kReadableOperator [ operator ] ;
365
- const res = inspectValue ( actual ) . split ( '\n' ) ;
373
+ const res = StringPrototypeSplit ( inspectValue ( actual ) , '\n' ) ;
366
374
367
375
// In case "actual" is an object or a function, it should not be
368
376
// reference equal.
@@ -377,15 +385,15 @@ class AssertionError extends Error {
377
385
if ( res . length > 50 ) {
378
386
res [ 46 ] = `${ blue } ...${ white } ` ;
379
387
while ( res . length > 47 ) {
380
- res . pop ( ) ;
388
+ ArrayPrototypePop ( res ) ;
381
389
}
382
390
}
383
391
384
392
// Only print a single input.
385
393
if ( res . length === 1 ) {
386
394
super ( `${ base } ${ res [ 0 ] . length > 5 ? '\n\n' : ' ' } ${ res [ 0 ] } ` ) ;
387
395
} else {
388
- super ( `${ base } \n\n${ res . join ( '\n' ) } \n` ) ;
396
+ super ( `${ base } \n\n${ ArrayPrototypeJoin ( res , '\n' ) } \n` ) ;
389
397
}
390
398
} else {
391
399
let res = inspectValue ( actual ) ;
@@ -394,15 +402,15 @@ class AssertionError extends Error {
394
402
if ( operator === 'notDeepEqual' && res === other ) {
395
403
res = `${ knownOperator } \n\n${ res } ` ;
396
404
if ( res . length > 1024 ) {
397
- res = `${ res . slice ( 0 , 1021 ) } ...` ;
405
+ res = `${ StringPrototypeSlice ( res , 0 , 1021 ) } ...` ;
398
406
}
399
407
super ( res ) ;
400
408
} else {
401
409
if ( res . length > 512 ) {
402
- res = `${ res . slice ( 0 , 509 ) } ...` ;
410
+ res = `${ StringPrototypeSlice ( res , 0 , 509 ) } ...` ;
403
411
}
404
412
if ( other . length > 512 ) {
405
- other = `${ other . slice ( 0 , 509 ) } ...` ;
413
+ other = `${ StringPrototypeSlice ( other , 0 , 509 ) } ...` ;
406
414
}
407
415
if ( operator === 'deepEqual' ) {
408
416
res = `${ knownOperator } \n\n${ res } \n\nshould loosely deep-equal\n\n` ;
@@ -463,12 +471,12 @@ class AssertionError extends Error {
463
471
464
472
for ( const name of [ 'actual' , 'expected' ] ) {
465
473
if ( typeof this [ name ] === 'string' ) {
466
- const lines = this [ name ] . split ( '\n' ) ;
474
+ const lines = StringPrototypeSplit ( this [ name ] , '\n' ) ;
467
475
if ( lines . length > 10 ) {
468
476
lines . length = 10 ;
469
- this [ name ] = `${ lines . join ( '\n' ) } \n...` ;
477
+ this [ name ] = `${ ArrayPrototypeJoin ( lines , '\n' ) } \n...` ;
470
478
} else if ( this [ name ] . length > 512 ) {
471
- this [ name ] = `${ this [ name ] . slice ( 512 ) } ...` ;
479
+ this [ name ] = `${ StringPrototypeSlice ( this [ name ] , 512 ) } ...` ;
472
480
}
473
481
}
474
482
}
0 commit comments