Skip to content

Commit eccc659

Browse files
committed
assert: add comments for diff algorithm
It became hard to follow what was actually happening in the algorithm. This adds comments to improve the situation. PR-URL: #23048 Refs: #22763 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 02c44a4 commit eccc659

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

lib/internal/assert.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ function createErrDiff(actual, expected, operator) {
174174
// Only extra expected lines exist
175175
const cur = i - lastPos;
176176
if (actualLines.length < i + 1) {
177+
// If the last diverging line is more than one line above and the
178+
// current line is at least line three, add some of the former lines and
179+
// also add dots to indicate skipped entries.
177180
if (cur > 1 && i > 2) {
178181
if (cur > 4) {
179182
res += `\n${blue}...${white}`;
@@ -185,11 +188,16 @@ function createErrDiff(actual, expected, operator) {
185188
res += `\n ${expectedLines[i - 1]}`;
186189
printedLines++;
187190
}
191+
// Mark the current line as the last diverging one.
188192
lastPos = i;
193+
// Add the expected line to the cache.
189194
other += `\n${red}-${white} ${expectedLines[i]}`;
190195
printedLines++;
191196
// Only extra actual lines exist
192197
} else if (expectedLines.length < i + 1) {
198+
// If the last diverging line is more than one line above and the
199+
// current line is at least line three, add some of the former lines and
200+
// also add dots to indicate skipped entries.
193201
if (cur > 1 && i > 2) {
194202
if (cur > 4) {
195203
res += `\n${blue}...${white}`;
@@ -201,23 +209,40 @@ function createErrDiff(actual, expected, operator) {
201209
res += `\n ${actualLines[i - 1]}`;
202210
printedLines++;
203211
}
212+
// Mark the current line as the last diverging one.
204213
lastPos = i;
214+
// Add the actual line to the result.
205215
res += `\n${green}+${white} ${actualLines[i]}`;
206216
printedLines++;
207217
// Lines diverge
208218
} else {
209219
const expectedLine = expectedLines[i];
210220
let actualLine = actualLines[i];
221+
// If the lines diverge, specifically check for lines that only diverge by
222+
// a trailing comma. In that case it is actually identical and we should
223+
// mark it as such.
211224
let divergingLines = actualLine !== expectedLine &&
212225
(!actualLine.endsWith(',') ||
213226
actualLine.slice(0, -1) !== expectedLine);
227+
// If the expected line has a trailing comma but is otherwise identical,
228+
// add a comma at the end of the actual line. Otherwise the output could
229+
// look weird as in:
230+
//
231+
// [
232+
// 1 // No comma at the end!
233+
// + 2
234+
// ]
235+
//
214236
if (divergingLines &&
215237
expectedLine.endsWith(',') &&
216238
expectedLine.slice(0, -1) === actualLine) {
217239
divergingLines = false;
218240
actualLine += ',';
219241
}
220242
if (divergingLines) {
243+
// If the last diverging line is more than one line above and the
244+
// current line is at least line three, add some of the former lines and
245+
// also add dots to indicate skipped entries.
221246
if (cur > 1 && i > 2) {
222247
if (cur > 4) {
223248
res += `\n${blue}...${white}`;
@@ -229,14 +254,21 @@ function createErrDiff(actual, expected, operator) {
229254
res += `\n ${actualLines[i - 1]}`;
230255
printedLines++;
231256
}
257+
// Mark the current line as the last diverging one.
232258
lastPos = i;
259+
// Add the actual line to the result and cache the expected diverging
260+
// line so consecutive diverging lines show up as +++--- and not +-+-+-.
233261
res += `\n${green}+${white} ${actualLine}`;
234262
other += `\n${red}-${white} ${expectedLine}`;
235263
printedLines += 2;
236264
// Lines are identical
237265
} else {
266+
// Add all cached information to the result before adding other things
267+
// and reset the cache.
238268
res += other;
239269
other = '';
270+
// If the last diverging line is exactly one line above or if it is the
271+
// very first line, add the line to the result.
240272
if (cur === 1 || i === 0) {
241273
res += `\n ${actualLine}`;
242274
printedLines++;
@@ -246,7 +278,7 @@ function createErrDiff(actual, expected, operator) {
246278
// Inspected object to big (Show ~20 rows max)
247279
if (printedLines > 20 && i < maxLines - 2) {
248280
return `${msg}${skippedMsg}\n${res}\n${blue}...${white}${other}\n` +
249-
`${blue}...${white}`;
281+
`${blue}...${white}`;
250282
}
251283
}
252284

0 commit comments

Comments
 (0)