assert: fix wrong message indentation
If code is read from a file and that code is indented, it would be misaligned. This makes sure it has a natural indentation that is relative to the starting point of the assertion. PR-URL: https://github.com/nodejs/node/pull/20791 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
352ae23974
commit
9c2e67ba97
@ -193,6 +193,19 @@ function getErrMessage(call) {
|
|||||||
if (EOL === '\r\n') {
|
if (EOL === '\r\n') {
|
||||||
message = message.replace(/\r\n/g, '\n');
|
message = message.replace(/\r\n/g, '\n');
|
||||||
}
|
}
|
||||||
|
// Always normalize indentation, otherwise the message could look weird.
|
||||||
|
if (message.indexOf('\n') !== -1) {
|
||||||
|
const tmp = message.split('\n');
|
||||||
|
message = tmp[0];
|
||||||
|
for (var i = 1; i < tmp.length; i++) {
|
||||||
|
let pos = 0;
|
||||||
|
while (pos < column &&
|
||||||
|
(tmp[i][pos] === ' ' || tmp[i][pos] === '\t')) {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
message += `\n ${tmp[i].slice(pos)}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
message = 'The expression evaluated to a falsy value:' +
|
message = 'The expression evaluated to a falsy value:' +
|
||||||
`\n\n assert${ok}(${message})\n`;
|
`\n\n assert${ok}(${message})\n`;
|
||||||
}
|
}
|
||||||
|
@ -695,7 +695,7 @@ common.expectsError(
|
|||||||
code: 'ERR_ASSERTION',
|
code: 'ERR_ASSERTION',
|
||||||
type: assert.AssertionError,
|
type: assert.AssertionError,
|
||||||
message: 'The expression evaluated to a falsy value:\n\n' +
|
message: 'The expression evaluated to a falsy value:\n\n' +
|
||||||
"assert((() => 'string')()\n" +
|
' assert((() => \'string\')()\n' +
|
||||||
' // eslint-disable-next-line\n' +
|
' // eslint-disable-next-line\n' +
|
||||||
' ===\n' +
|
' ===\n' +
|
||||||
' 123 instanceof\n' +
|
' 123 instanceof\n' +
|
||||||
@ -703,6 +703,47 @@ common.expectsError(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
common.expectsError(
|
||||||
|
() => {
|
||||||
|
a(
|
||||||
|
(() => 'string')()
|
||||||
|
// eslint-disable-next-line
|
||||||
|
===
|
||||||
|
123 instanceof
|
||||||
|
Buffer
|
||||||
|
);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'ERR_ASSERTION',
|
||||||
|
type: assert.AssertionError,
|
||||||
|
message: 'The expression evaluated to a falsy value:\n\n' +
|
||||||
|
' assert((() => \'string\')()\n' +
|
||||||
|
' // eslint-disable-next-line\n' +
|
||||||
|
' ===\n' +
|
||||||
|
' 123 instanceof\n' +
|
||||||
|
' Buffer)\n'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
/* eslint-disable indent */
|
||||||
|
common.expectsError(() => {
|
||||||
|
a((
|
||||||
|
() => 'string')() ===
|
||||||
|
123 instanceof
|
||||||
|
Buffer
|
||||||
|
);
|
||||||
|
}, {
|
||||||
|
code: 'ERR_ASSERTION',
|
||||||
|
type: assert.AssertionError,
|
||||||
|
message: 'The expression evaluated to a falsy value:\n\n' +
|
||||||
|
' assert((\n' +
|
||||||
|
' () => \'string\')() ===\n' +
|
||||||
|
' 123 instanceof\n' +
|
||||||
|
' Buffer)\n'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
/* eslint-enable indent */
|
||||||
|
|
||||||
common.expectsError(
|
common.expectsError(
|
||||||
() => assert(null, undefined),
|
() => assert(null, undefined),
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user