assert: fix error diff
In some edge cases an identical line could be printed twice. This is now fixed by changing the algorithm a bit. It will now verify how many lines were identical before the current one. PR-URL: https://github.com/nodejs/node/pull/28058 Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
a3ea54f5ab
commit
e4ec4f2656
@ -52,17 +52,11 @@ function inspectValue(val) {
|
|||||||
maxArrayLength: Infinity,
|
maxArrayLength: Infinity,
|
||||||
// Assert compares only enumerable properties (with a few exceptions).
|
// Assert compares only enumerable properties (with a few exceptions).
|
||||||
showHidden: false,
|
showHidden: false,
|
||||||
// Having a long line as error is better than wrapping the line for
|
|
||||||
// comparison for now.
|
|
||||||
// TODO(BridgeAR): `breakLength` should be limited as soon as soon as we
|
|
||||||
// have meta information about the inspected properties (i.e., know where
|
|
||||||
// in what line the property starts and ends).
|
|
||||||
breakLength: Infinity,
|
|
||||||
// Assert does not detect proxies currently.
|
// Assert does not detect proxies currently.
|
||||||
showProxy: false,
|
showProxy: false,
|
||||||
sorted: true,
|
sorted: true,
|
||||||
// Inspect getters as we also check them when comparing entries.
|
// Inspect getters as we also check them when comparing entries.
|
||||||
getters: true
|
getters: true,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -70,7 +64,6 @@ function inspectValue(val) {
|
|||||||
function createErrDiff(actual, expected, operator) {
|
function createErrDiff(actual, expected, operator) {
|
||||||
let other = '';
|
let other = '';
|
||||||
let res = '';
|
let res = '';
|
||||||
let lastPos = 0;
|
|
||||||
let end = '';
|
let end = '';
|
||||||
let skipped = false;
|
let skipped = false;
|
||||||
const actualInspected = inspectValue(actual);
|
const actualInspected = inspectValue(actual);
|
||||||
@ -171,51 +164,48 @@ function createErrDiff(actual, expected, operator) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let printedLines = 0;
|
let printedLines = 0;
|
||||||
|
let identical = 0;
|
||||||
const msg = kReadableOperator[operator] +
|
const msg = kReadableOperator[operator] +
|
||||||
`\n${green}+ actual${white} ${red}- expected${white}`;
|
`\n${green}+ actual${white} ${red}- expected${white}`;
|
||||||
const skippedMsg = ` ${blue}...${white} Lines skipped`;
|
const skippedMsg = ` ${blue}...${white} Lines skipped`;
|
||||||
|
|
||||||
for (i = 0; i < maxLines; i++) {
|
for (i = 0; i < maxLines; i++) {
|
||||||
// Only extra expected lines exist
|
|
||||||
const cur = i - lastPos;
|
|
||||||
if (actualLines.length < i + 1) {
|
if (actualLines.length < i + 1) {
|
||||||
// If the last diverging line is more than one line above and the
|
// If more than one former line is identical, print that. Collapse those
|
||||||
// current line is at least line three, add some of the former lines and
|
// in case more than three lines before were identical.
|
||||||
// also add dots to indicate skipped entries.
|
if (identical > 1) {
|
||||||
if (cur > 1 && i > 2) {
|
if (identical > 3) {
|
||||||
if (cur > 4) {
|
|
||||||
res += `\n${blue}...${white}`;
|
res += `\n${blue}...${white}`;
|
||||||
skipped = true;
|
skipped = true;
|
||||||
} else if (cur > 3) {
|
} else if (identical > 2) {
|
||||||
res += `\n ${expectedLines[i - 2]}`;
|
res += `\n ${expectedLines[i - 2]}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
}
|
}
|
||||||
res += `\n ${expectedLines[i - 1]}`;
|
res += `\n ${expectedLines[i - 1]}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
}
|
}
|
||||||
// Mark the current line as the last diverging one.
|
// No identical lines before.
|
||||||
lastPos = i;
|
identical = 0;
|
||||||
// Add the expected line to the cache.
|
// Add the expected line to the cache.
|
||||||
other += `\n${red}-${white} ${expectedLines[i]}`;
|
other += `\n${red}-${white} ${expectedLines[i]}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
// Only extra actual lines exist
|
// Only extra actual lines exist
|
||||||
} else if (expectedLines.length < i + 1) {
|
} else if (expectedLines.length < i + 1) {
|
||||||
// If the last diverging line is more than one line above and the
|
// If more than one former line is identical, print that. Collapse those
|
||||||
// current line is at least line three, add some of the former lines and
|
// in case more than three lines before were identical.
|
||||||
// also add dots to indicate skipped entries.
|
if (identical > 1) {
|
||||||
if (cur > 1 && i > 2) {
|
if (identical > 3) {
|
||||||
if (cur > 4) {
|
|
||||||
res += `\n${blue}...${white}`;
|
res += `\n${blue}...${white}`;
|
||||||
skipped = true;
|
skipped = true;
|
||||||
} else if (cur > 3) {
|
} else if (identical > 2) {
|
||||||
res += `\n ${actualLines[i - 2]}`;
|
res += `\n ${actualLines[i - 2]}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
}
|
}
|
||||||
res += `\n ${actualLines[i - 1]}`;
|
res += `\n ${actualLines[i - 1]}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
}
|
}
|
||||||
// Mark the current line as the last diverging one.
|
// No identical lines before.
|
||||||
lastPos = i;
|
identical = 0;
|
||||||
// Add the actual line to the result.
|
// Add the actual line to the result.
|
||||||
res += `\n${green}+${white} ${actualLines[i]}`;
|
res += `\n${green}+${white} ${actualLines[i]}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
@ -245,22 +235,21 @@ function createErrDiff(actual, expected, operator) {
|
|||||||
actualLine += ',';
|
actualLine += ',';
|
||||||
}
|
}
|
||||||
if (divergingLines) {
|
if (divergingLines) {
|
||||||
// If the last diverging line is more than one line above and the
|
// If more than one former line is identical, print that. Collapse those
|
||||||
// current line is at least line three, add some of the former lines and
|
// in case more than three lines before were identical.
|
||||||
// also add dots to indicate skipped entries.
|
if (identical > 1) {
|
||||||
if (cur > 1 && i > 2) {
|
if (identical > 3) {
|
||||||
if (cur > 4) {
|
|
||||||
res += `\n${blue}...${white}`;
|
res += `\n${blue}...${white}`;
|
||||||
skipped = true;
|
skipped = true;
|
||||||
} else if (cur > 3) {
|
} else if (identical > 2) {
|
||||||
res += `\n ${actualLines[i - 2]}`;
|
res += `\n ${actualLines[i - 2]}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
}
|
}
|
||||||
res += `\n ${actualLines[i - 1]}`;
|
res += `\n ${actualLines[i - 1]}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
}
|
}
|
||||||
// Mark the current line as the last diverging one.
|
// No identical lines before.
|
||||||
lastPos = i;
|
identical = 0;
|
||||||
// Add the actual line to the result and cache the expected diverging
|
// Add the actual line to the result and cache the expected diverging
|
||||||
// line so consecutive diverging lines show up as +++--- and not +-+-+-.
|
// line so consecutive diverging lines show up as +++--- and not +-+-+-.
|
||||||
res += `\n${green}+${white} ${actualLine}`;
|
res += `\n${green}+${white} ${actualLine}`;
|
||||||
@ -272,9 +261,10 @@ function createErrDiff(actual, expected, operator) {
|
|||||||
// and reset the cache.
|
// and reset the cache.
|
||||||
res += other;
|
res += other;
|
||||||
other = '';
|
other = '';
|
||||||
// If the last diverging line is exactly one line above or if it is the
|
identical++;
|
||||||
// very first line, add the line to the result.
|
// The very first identical line since the last diverging line is be
|
||||||
if (cur === 1 || i === 0) {
|
// added to the result.
|
||||||
|
if (identical === 1) {
|
||||||
res += `\n ${actualLine}`;
|
res += `\n ${actualLine}`;
|
||||||
printedLines++;
|
printedLines++;
|
||||||
}
|
}
|
||||||
@ -316,7 +306,7 @@ class AssertionError extends Error {
|
|||||||
if (process.stderr.isTTY) {
|
if (process.stderr.isTTY) {
|
||||||
// Reset on each call to make sure we handle dynamically set environment
|
// Reset on each call to make sure we handle dynamically set environment
|
||||||
// variables correct.
|
// variables correct.
|
||||||
if (process.stderr.getColorDepth() !== 1) {
|
if (process.stderr.hasColors()) {
|
||||||
blue = '\u001b[34m';
|
blue = '\u001b[34m';
|
||||||
green = '\u001b[32m';
|
green = '\u001b[32m';
|
||||||
white = '\u001b[39m';
|
white = '\u001b[39m';
|
||||||
|
@ -68,7 +68,6 @@ assert.deepEqual(arr, buf);
|
|||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
|
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
|
||||||
' Buffer [Uint8Array] [\n' +
|
' Buffer [Uint8Array] [\n' +
|
||||||
' 120,\n' +
|
|
||||||
'...\n' +
|
'...\n' +
|
||||||
' 10,\n' +
|
' 10,\n' +
|
||||||
'+ prop: 1\n' +
|
'+ prop: 1\n' +
|
||||||
@ -87,7 +86,6 @@ assert.deepEqual(arr, buf);
|
|||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
|
message: `${defaultMsgStartFull} ... Lines skipped\n\n` +
|
||||||
' Uint8Array [\n' +
|
' Uint8Array [\n' +
|
||||||
' 120,\n' +
|
|
||||||
'...\n' +
|
'...\n' +
|
||||||
' 10,\n' +
|
' 10,\n' +
|
||||||
'- prop: 5\n' +
|
'- prop: 5\n' +
|
||||||
@ -921,13 +919,30 @@ assert.deepStrictEqual(obj1, obj2);
|
|||||||
const a = new TypeError('foo');
|
const a = new TypeError('foo');
|
||||||
const b = new TypeError('foo');
|
const b = new TypeError('foo');
|
||||||
a.foo = 'bar';
|
a.foo = 'bar';
|
||||||
b.foo = 'baz';
|
b.foo = 'baz.';
|
||||||
|
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => assert.deepStrictEqual(a, b),
|
() => assert.throws(
|
||||||
|
() => assert.deepStrictEqual(a, b),
|
||||||
|
{
|
||||||
|
operator: 'throws',
|
||||||
|
message: `${defaultMsgStartFull}\n\n` +
|
||||||
|
' [TypeError: foo] {\n+ foo: \'bar\'\n- foo: \'baz\'\n }',
|
||||||
|
}
|
||||||
|
),
|
||||||
{
|
{
|
||||||
message: `${defaultMsgStartFull}\n\n` +
|
message: 'Expected values to be strictly deep-equal:\n' +
|
||||||
' [TypeError: foo] {\n+ foo: \'bar\'\n- foo: \'baz\'\n }'
|
'+ actual - expected ... Lines skipped\n' +
|
||||||
|
'\n' +
|
||||||
|
' Comparison {\n' +
|
||||||
|
'...\n' +
|
||||||
|
" \"+ foo: 'bar'\\n\" +\n" +
|
||||||
|
"+ \"- foo: 'baz.'\\n\" +\n" +
|
||||||
|
"- \"- foo: 'baz'\\n\" +\n" +
|
||||||
|
" ' }',\n" +
|
||||||
|
"+ operator: 'deepStrictEqual'\n" +
|
||||||
|
"- operator: 'throws'\n" +
|
||||||
|
' }'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user