Skip to content

Commit

Permalink
assert: fix wrong message indentation
Browse files Browse the repository at this point in the history
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: #20791
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
BridgeAR authored and MylesBorins committed May 23, 2018
1 parent c8c9211 commit 8f8a0e3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
13 changes: 13 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ function getErrMessage(call) {
if (EOL === '\r\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:' +
`\n\n assert${ok}(${message})\n`;
}
Expand Down
53 changes: 47 additions & 6 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,14 +694,55 @@ common.expectsError(
{
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'
message: 'The expression evaluated to a falsy value:\n\n' +
' assert((() => \'string\')()\n' +
' // eslint-disable-next-line\n' +
' ===\n' +
' 123 instanceof\n' +
' Buffer)\n'
}
);

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(
() => assert(null, undefined),
Expand Down

0 comments on commit 8f8a0e3

Please sign in to comment.