3
3
const {
4
4
ArrayPrototypeJoin,
5
5
ArrayPrototypePop,
6
- ArrayPrototypeSlice,
7
6
Error,
8
7
ErrorCaptureStackTrace,
9
8
ObjectAssign,
10
9
ObjectDefineProperty,
11
10
ObjectGetPrototypeOf,
12
11
ObjectPrototypeHasOwnProperty,
13
12
String,
14
- StringPrototypeRepeat,
15
13
StringPrototypeSlice,
16
14
StringPrototypeSplit,
17
15
} = primordials ;
18
16
19
17
const { isError } = require ( 'internal/util' ) ;
18
+ const { diff } = require ( 'util' ) ;
20
19
21
20
const { inspect } = require ( 'internal/util/inspect' ) ;
22
21
const colors = require ( 'internal/util/colors' ) ;
22
+ const {
23
+ inspectValue,
24
+ } = require ( 'internal/util/diff' ) ;
23
25
const { validateObject } = require ( 'internal/validators' ) ;
24
26
const { isErrorStackTraceLimitWritable } = require ( 'internal/errors' ) ;
25
- const { myersDiff, printMyersDiff, printSimpleMyersDiff } = require ( 'internal/assert/myers_diff' ) ;
26
-
27
- const kReadableOperator = {
28
- deepStrictEqual : 'Expected values to be strictly deep-equal:' ,
29
- partialDeepStrictEqual : 'Expected values to be partially and strictly deep-equal:' ,
30
- strictEqual : 'Expected values to be strictly equal:' ,
31
- strictEqualObject : 'Expected "actual" to be reference-equal to "expected":' ,
32
- deepEqual : 'Expected values to be loosely deep-equal:' ,
33
- notDeepStrictEqual : 'Expected "actual" not to be strictly deep-equal to:' ,
34
- notStrictEqual : 'Expected "actual" to be strictly unequal to:' ,
35
- notStrictEqualObject :
36
- 'Expected "actual" not to be reference-equal to "expected":' ,
37
- notDeepEqual : 'Expected "actual" not to be loosely deep-equal to:' ,
38
- notIdentical : 'Values have same structure but are not reference-equal:' ,
39
- notDeepEqualUnequal : 'Expected values not to be loosely deep-equal:' ,
40
- } ;
27
+ const { kReadableOperator } = require ( 'internal/assert/consts' ) ;
41
28
42
- const kMaxShortStringLength = 12 ;
43
29
const kMaxLongStringLength = 512 ;
44
30
45
31
const kMethodsWithCustomMessageDiff = [ 'deepStrictEqual' , 'strictEqual' , 'partialDeepStrictEqual' ] ;
@@ -65,28 +51,6 @@ function copyError(source) {
65
51
return target ;
66
52
}
67
53
68
- function inspectValue ( val ) {
69
- // The util.inspect default values could be changed. This makes sure the
70
- // error messages contain the necessary information nevertheless.
71
- return inspect ( val , {
72
- compact : false ,
73
- customInspect : false ,
74
- depth : 1000 ,
75
- maxArrayLength : Infinity ,
76
- // Assert compares only enumerable properties (with a few exceptions).
77
- showHidden : false ,
78
- // Assert does not detect proxies currently.
79
- showProxy : false ,
80
- sorted : true ,
81
- // Inspect getters as we also check them when comparing entries.
82
- getters : true ,
83
- } ) ;
84
- }
85
-
86
- function getErrorMessage ( operator , message ) {
87
- return message || kReadableOperator [ operator ] ;
88
- }
89
-
90
54
function checkOperator ( actual , expected , operator ) {
91
55
// In case both values are objects or functions explicitly mark them as not
92
56
// reference equal for the `strictEqual` operator.
@@ -104,131 +68,10 @@ function checkOperator(actual, expected, operator) {
104
68
return operator ;
105
69
}
106
70
107
- function getColoredMyersDiff ( actual , expected ) {
108
- const header = `${ colors . green } actual${ colors . white } ${ colors . red } expected${ colors . white } ` ;
109
- const skipped = false ;
110
-
111
- const diff = myersDiff ( StringPrototypeSplit ( actual , '' ) , StringPrototypeSplit ( expected , '' ) ) ;
112
- let message = printSimpleMyersDiff ( diff ) ;
113
-
114
- if ( skipped ) {
115
- message += '...' ;
116
- }
117
-
118
- return { message, header, skipped } ;
119
- }
120
-
121
- function getStackedDiff ( actual , expected ) {
122
- const isStringComparison = typeof actual === 'string' && typeof expected === 'string' ;
123
-
124
- let message = `\n${ colors . green } +${ colors . white } ${ actual } \n${ colors . red } - ${ colors . white } ${ expected } ` ;
125
- const stringsLen = actual . length + expected . length ;
126
- const maxTerminalLength = process . stderr . isTTY ? process . stderr . columns : 80 ;
127
- const showIndicator = isStringComparison && ( stringsLen <= maxTerminalLength ) ;
128
-
129
- if ( showIndicator ) {
130
- let indicatorIdx = - 1 ;
131
-
132
- for ( let i = 0 ; i < actual . length ; i ++ ) {
133
- if ( actual [ i ] !== expected [ i ] ) {
134
- // Skip the indicator for the first 2 characters because the diff is immediately apparent
135
- // It is 3 instead of 2 to account for the quotes
136
- if ( i >= 3 ) {
137
- indicatorIdx = i ;
138
- }
139
- break ;
140
- }
141
- }
142
-
143
- if ( indicatorIdx !== - 1 ) {
144
- message += `\n${ StringPrototypeRepeat ( ' ' , indicatorIdx + 2 ) } ^` ;
145
- }
146
- }
147
-
148
- return { message } ;
149
- }
150
-
151
- function getSimpleDiff ( originalActual , actual , originalExpected , expected ) {
152
- let stringsLen = actual . length + expected . length ;
153
- // Accounting for the quotes wrapping strings
154
- if ( typeof originalActual === 'string' ) {
155
- stringsLen -= 2 ;
156
- }
157
- if ( typeof originalExpected === 'string' ) {
158
- stringsLen -= 2 ;
159
- }
160
- if ( stringsLen <= kMaxShortStringLength && ( originalActual !== 0 || originalExpected !== 0 ) ) {
161
- return { message : `${ actual } !== ${ expected } ` , header : '' } ;
162
- }
163
-
164
- const isStringComparison = typeof originalActual === 'string' && typeof originalExpected === 'string' ;
165
- // colored myers diff
166
- if ( isStringComparison && colors . hasColors ) {
167
- return getColoredMyersDiff ( actual , expected ) ;
168
- }
169
-
170
- return getStackedDiff ( actual , expected ) ;
171
- }
172
-
173
- function isSimpleDiff ( actual , inspectedActual , expected , inspectedExpected ) {
174
- if ( inspectedActual . length > 1 || inspectedExpected . length > 1 ) {
175
- return false ;
176
- }
177
-
178
- return typeof actual !== 'object' || actual === null || typeof expected !== 'object' || expected === null ;
179
- }
180
-
181
71
function createErrDiff ( actual , expected , operator , customMessage ) {
182
72
operator = checkOperator ( actual , expected , operator ) ;
183
73
184
- let skipped = false ;
185
- let message = '' ;
186
- const inspectedActual = inspectValue ( actual ) ;
187
- const inspectedExpected = inspectValue ( expected ) ;
188
- const inspectedSplitActual = StringPrototypeSplit ( inspectedActual , '\n' ) ;
189
- const inspectedSplitExpected = StringPrototypeSplit ( inspectedExpected , '\n' ) ;
190
- const showSimpleDiff = isSimpleDiff ( actual , inspectedSplitActual , expected , inspectedSplitExpected ) ;
191
- let header = `${ colors . green } + actual${ colors . white } ${ colors . red } - expected${ colors . white } ` ;
192
-
193
- if ( showSimpleDiff ) {
194
- const simpleDiff = getSimpleDiff ( actual , inspectedSplitActual [ 0 ] , expected , inspectedSplitExpected [ 0 ] ) ;
195
- message = simpleDiff . message ;
196
- if ( typeof simpleDiff . header !== 'undefined' ) {
197
- header = simpleDiff . header ;
198
- }
199
- if ( simpleDiff . skipped ) {
200
- skipped = true ;
201
- }
202
- } else if ( inspectedActual === inspectedExpected ) {
203
- // Handles the case where the objects are structurally the same but different references
204
- operator = 'notIdentical' ;
205
- if ( inspectedSplitActual . length > 50 ) {
206
- message = `${ ArrayPrototypeJoin ( ArrayPrototypeSlice ( inspectedSplitActual , 0 , 50 ) , '\n' ) } \n...}` ;
207
- skipped = true ;
208
- } else {
209
- message = ArrayPrototypeJoin ( inspectedSplitActual , '\n' ) ;
210
- }
211
- header = '' ;
212
- } else {
213
- const checkCommaDisparity = actual != null && typeof actual === 'object' ;
214
- const diff = myersDiff ( inspectedSplitActual , inspectedSplitExpected , checkCommaDisparity ) ;
215
-
216
- const myersDiffMessage = printMyersDiff ( diff , operator ) ;
217
- message = myersDiffMessage . message ;
218
-
219
- if ( operator === 'partialDeepStrictEqual' ) {
220
- header = `${ colors . gray } ${ colors . hasColors ? '' : '+ ' } actual${ colors . white } ${ colors . red } - expected${ colors . white } ` ;
221
- }
222
-
223
- if ( myersDiffMessage . skipped ) {
224
- skipped = true ;
225
- }
226
- }
227
-
228
- const headerMessage = `${ getErrorMessage ( operator , customMessage ) } \n${ header } ` ;
229
- const skippedMessage = skipped ? '\n... Skipped lines' : '' ;
230
-
231
- return `${ headerMessage } ${ skippedMessage } \n${ message } \n` ;
74
+ return diff ( actual , expected , operator , customMessage ) ;
232
75
}
233
76
234
77
function addEllipsis ( string ) {
0 commit comments