diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-06-03 17:05:37 +0300 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-06-13 05:50:03 +0300 |
commit | 81496567e76006a7d07a8552215fc6333c183480 (patch) | |
tree | 6c080c0da1be8b0ced5a01814c181fddcda78f70 /lib/internal/assert | |
parent | e4ec4f2656f14d3992da154c46f6c0c44025f30d (diff) |
assert: print more lines in the error diff
So far consequitive identical lines were collapsed if there were at
least three. Now they are only collapsed from five identical lines on.
This also simplifies the implementation a tiny bit by abstracting some
logic.
PR-URL: https://github.com/nodejs/node/pull/28058
Reviewed-By: Rich Trott <rtrott@gmail.com>
Diffstat (limited to 'lib/internal/assert')
-rw-r--r-- | lib/internal/assert/assertion_error.js | 84 |
1 files changed, 46 insertions, 38 deletions
diff --git a/lib/internal/assert/assertion_error.js b/lib/internal/assert/assertion_error.js index 8943a702fa1..cdeba1941c4 100644 --- a/lib/internal/assert/assertion_error.js +++ b/lib/internal/assert/assertion_error.js @@ -122,7 +122,7 @@ function createErrDiff(actual, expected, operator) { let a = actualLines[actualLines.length - 1]; let b = expectedLines[expectedLines.length - 1]; while (a === b) { - if (i++ < 2) { + if (i++ < 3) { end = `\n ${a}${end}`; } else { other = a; @@ -154,7 +154,9 @@ function createErrDiff(actual, expected, operator) { return `${kReadableOperator.notIdentical}\n\n${actualLines.join('\n')}\n`; } - if (i > 3) { + // There were at least five identical lines at the end. Mark a couple of + // skipped. + if (i >= 5) { end = `\n${blue}...${white}${end}`; skipped = true; } @@ -169,46 +171,46 @@ function createErrDiff(actual, expected, operator) { `\n${green}+ actual${white} ${red}- expected${white}`; const skippedMsg = ` ${blue}...${white} Lines skipped`; + let lines = actualLines; + let plusMinus = `${green}+${white}`; + let maxLength = expectedLines.length; + if (actualLines.length < maxLines) { + lines = expectedLines; + plusMinus = `${red}-${white}`; + maxLength = actualLines.length; + } + for (i = 0; i < maxLines; i++) { - if (actualLines.length < i + 1) { - // If more than one former line is identical, print that. Collapse those - // in case more than three lines before were identical. - if (identical > 1) { + if (maxLength < i + 1) { + // If more than two former lines are identical, print them. Collapse them + // in case more than five lines were identical. + if (identical > 2) { if (identical > 3) { - res += `\n${blue}...${white}`; - skipped = true; - } else if (identical > 2) { - res += `\n ${expectedLines[i - 2]}`; + if (identical > 4) { + if (identical === 5) { + res += `\n ${lines[i - 3]}`; + printedLines++; + } else { + res += `\n${blue}...${white}`; + skipped = true; + } + } + res += `\n ${lines[i - 2]}`; printedLines++; } - res += `\n ${expectedLines[i - 1]}`; + res += `\n ${lines[i - 1]}`; printedLines++; } // No identical lines before. identical = 0; // Add the expected line to the cache. - other += `\n${red}-${white} ${expectedLines[i]}`; - printedLines++; - // Only extra actual lines exist - } else if (expectedLines.length < i + 1) { - // If more than one former line is identical, print that. Collapse those - // in case more than three lines before were identical. - if (identical > 1) { - if (identical > 3) { - res += `\n${blue}...${white}`; - skipped = true; - } else if (identical > 2) { - res += `\n ${actualLines[i - 2]}`; - printedLines++; - } - res += `\n ${actualLines[i - 1]}`; - printedLines++; + if (lines === actualLines) { + res += `\n${plusMinus} ${lines[i]}`; + } else { + other += `\n${plusMinus} ${lines[i]}`; } - // No identical lines before. - identical = 0; - // Add the actual line to the result. - res += `\n${green}+${white} ${actualLines[i]}`; printedLines++; + // Only extra actual lines exist // Lines diverge } else { const expectedLine = expectedLines[i]; @@ -235,13 +237,19 @@ function createErrDiff(actual, expected, operator) { actualLine += ','; } if (divergingLines) { - // If more than one former line is identical, print that. Collapse those - // in case more than three lines before were identical. - if (identical > 1) { + // If more than two former lines are identical, print them. Collapse + // them in case more than five lines were identical. + if (identical > 2) { if (identical > 3) { - res += `\n${blue}...${white}`; - skipped = true; - } else if (identical > 2) { + if (identical > 4) { + if (identical === 5) { + res += `\n ${actualLines[i - 3]}`; + printedLines++; + } else { + res += `\n${blue}...${white}`; + skipped = true; + } + } res += `\n ${actualLines[i - 2]}`; printedLines++; } @@ -264,7 +272,7 @@ function createErrDiff(actual, expected, operator) { identical++; // The very first identical line since the last diverging line is be // added to the result. - if (identical === 1) { + if (identical <= 2) { res += `\n ${actualLine}`; printedLines++; } |