@@ -52,25 +52,18 @@ function inspectValue(val) {
52
52
maxArrayLength : Infinity ,
53
53
// Assert compares only enumerable properties (with a few exceptions).
54
54
showHidden : false ,
55
- // Having a long line as error is better than wrapping the line for
56
- // comparison for now.
57
- // TODO(BridgeAR): `breakLength` should be limited as soon as soon as we
58
- // have meta information about the inspected properties (i.e., know where
59
- // in what line the property starts and ends).
60
- breakLength : Infinity ,
61
55
// Assert does not detect proxies currently.
62
56
showProxy : false ,
63
57
sorted : true ,
64
58
// Inspect getters as we also check them when comparing entries.
65
- getters : true
59
+ getters : true ,
66
60
}
67
61
) ;
68
62
}
69
63
70
64
function createErrDiff ( actual , expected , operator ) {
71
65
let other = '' ;
72
66
let res = '' ;
73
- let lastPos = 0 ;
74
67
let end = '' ;
75
68
let skipped = false ;
76
69
const actualInspected = inspectValue ( actual ) ;
@@ -171,51 +164,48 @@ function createErrDiff(actual, expected, operator) {
171
164
}
172
165
173
166
let printedLines = 0 ;
167
+ let identical = 0 ;
174
168
const msg = kReadableOperator [ operator ] +
175
169
`\n${ green } + actual${ white } ${ red } - expected${ white } ` ;
176
170
const skippedMsg = ` ${ blue } ...${ white } Lines skipped` ;
177
171
178
172
for ( i = 0 ; i < maxLines ; i ++ ) {
179
- // Only extra expected lines exist
180
- const cur = i - lastPos ;
181
173
if ( actualLines . length < i + 1 ) {
182
- // If the last diverging line is more than one line above and the
183
- // current line is at least line three, add some of the former lines and
184
- // also add dots to indicate skipped entries.
185
- if ( cur > 1 && i > 2 ) {
186
- if ( cur > 4 ) {
174
+ // If more than one former line is identical, print that. Collapse those
175
+ // in case more than three lines before were identical.
176
+ if ( identical > 1 ) {
177
+ if ( identical > 3 ) {
187
178
res += `\n${ blue } ...${ white } ` ;
188
179
skipped = true ;
189
- } else if ( cur > 3 ) {
180
+ } else if ( identical > 2 ) {
190
181
res += `\n ${ expectedLines [ i - 2 ] } ` ;
191
182
printedLines ++ ;
192
183
}
193
184
res += `\n ${ expectedLines [ i - 1 ] } ` ;
194
185
printedLines ++ ;
195
186
}
196
- // Mark the current line as the last diverging one .
197
- lastPos = i ;
187
+ // No identical lines before .
188
+ identical = 0 ;
198
189
// Add the expected line to the cache.
199
190
other += `\n${ red } -${ white } ${ expectedLines [ i ] } ` ;
200
191
printedLines ++ ;
201
192
// Only extra actual lines exist
202
193
} else if ( expectedLines . length < i + 1 ) {
203
- // If the last diverging line is more than one line above and the
204
- // current line is at least line three, add some of the former lines and
205
- // also add dots to indicate skipped entries.
206
- if ( cur > 1 && i > 2 ) {
207
- if ( cur > 4 ) {
194
+ // If more than one former line is identical, print that. Collapse those
195
+ // in case more than three lines before were identical.
196
+ if ( identical > 1 ) {
197
+ if ( identical > 3 ) {
208
198
res += `\n${ blue } ...${ white } ` ;
209
199
skipped = true ;
210
- } else if ( cur > 3 ) {
200
+ } else if ( identical > 2 ) {
211
201
res += `\n ${ actualLines [ i - 2 ] } ` ;
212
202
printedLines ++ ;
213
203
}
214
204
res += `\n ${ actualLines [ i - 1 ] } ` ;
215
205
printedLines ++ ;
216
206
}
217
- // Mark the current line as the last diverging one .
218
- lastPos = i ;
207
+ // No identical lines before .
208
+ identical = 0 ;
219
209
// Add the actual line to the result.
220
210
res += `\n${ green } +${ white } ${ actualLines [ i ] } ` ;
221
211
printedLines ++ ;
@@ -245,22 +235,21 @@ function createErrDiff(actual, expected, operator) {
245
235
actualLine += ',' ;
246
236
}
247
237
if ( divergingLines ) {
248
- // If the last diverging line is more than one line above and the
249
- // current line is at least line three, add some of the former lines and
250
- // also add dots to indicate skipped entries.
251
- if ( cur > 1 && i > 2 ) {
252
- if ( cur > 4 ) {
238
+ // If more than one former line is identical, print that. Collapse those
239
+ // in case more than three lines before were identical.
240
+ if ( identical > 1 ) {
241
+ if ( identical > 3 ) {
253
242
res += `\n${ blue } ...${ white } ` ;
254
243
skipped = true ;
255
- } else if ( cur > 3 ) {
244
+ } else if ( identical > 2 ) {
256
245
res += `\n ${ actualLines [ i - 2 ] } ` ;
257
246
printedLines ++ ;
258
247
}
259
248
res += `\n ${ actualLines [ i - 1 ] } ` ;
260
249
printedLines ++ ;
261
250
}
262
- // Mark the current line as the last diverging one .
263
- lastPos = i ;
251
+ // No identical lines before .
252
+ identical = 0 ;
264
253
// Add the actual line to the result and cache the expected diverging
265
254
// line so consecutive diverging lines show up as +++--- and not +-+-+-.
266
255
res += `\n${ green } +${ white } ${ actualLine } ` ;
@@ -272,9 +261,10 @@ function createErrDiff(actual, expected, operator) {
272
261
// and reset the cache.
273
262
res += other ;
274
263
other = '' ;
275
- // If the last diverging line is exactly one line above or if it is the
276
- // very first line, add the line to the result.
277
- if ( cur === 1 || i === 0 ) {
264
+ identical ++ ;
265
+ // The very first identical line since the last diverging line is be
266
+ // added to the result.
267
+ if ( identical === 1 ) {
278
268
res += `\n ${ actualLine } ` ;
279
269
printedLines ++ ;
280
270
}
@@ -316,7 +306,7 @@ class AssertionError extends Error {
316
306
if ( process . stderr . isTTY ) {
317
307
// Reset on each call to make sure we handle dynamically set environment
318
308
// variables correct.
319
- if ( process . stderr . getColorDepth ( ) !== 1 ) {
309
+ if ( process . stderr . hasColors ( ) ) {
320
310
blue = '\u001b[34m' ;
321
311
green = '\u001b[32m' ;
322
312
white = '\u001b[39m' ;
0 commit comments