Skip to content

Commit

Permalink
assert: improve diff output
Browse files Browse the repository at this point in the history
The output is now a tiny bit improved by sorting object properties
when inspecting the values that are compared with each other. That
reduces the overall diff for identical objects with a different
property insertion order.

PR-URL: nodejs#22788
Refs: nodejs#22763
Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
BridgeAR committed Sep 19, 2018
1 parent b95b0d8 commit 28902f3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
3 changes: 2 additions & 1 deletion lib/internal/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function inspectValue(val) {
// comparison.
breakLength: Infinity,
// Assert does not detect proxies currently.
showProxy: false
showProxy: false,
sorted: true
}
);
}
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function re(literals, ...values) {
depth: 1000,
customInspect: false,
maxArrayLength: Infinity,
breakLength: Infinity
breakLength: Infinity,
sorted: true
});
// Need to escape special characters.
result += str;
Expand Down
39 changes: 26 additions & 13 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ testAssertionMessage(-Infinity, '-Infinity');
testAssertionMessage([1, 2, 3], '[\n+ 1,\n+ 2,\n+ 3\n+ ]');
testAssertionMessage(function f() {}, '[Function: f]');
testAssertionMessage(function() {}, '[Function]');
testAssertionMessage(circular, '{\n+ y: 1,\n+ x: [Circular]\n+ }');
testAssertionMessage(circular, '{\n+ x: [Circular],\n+ y: 1\n+ }');
testAssertionMessage({ a: undefined, b: null },
'{\n+ a: undefined,\n+ b: null\n+ }');
testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
Expand Down Expand Up @@ -602,8 +602,8 @@ assert.throws(
'\n' +
'+ {}\n' +
'- {\n' +
"- loop: 'forever',\n" +
'- [Symbol(nodejs.util.inspect.custom)]: [Function]\n' +
'- [Symbol(nodejs.util.inspect.custom)]: [Function],\n' +
"- loop: 'forever'\n" +
'- }'
});

Expand Down Expand Up @@ -885,9 +885,12 @@ common.expectsError(
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
message: `${start}\n${actExp}\n\n` +
" Comparison {\n name: 'TypeError',\n" +
" message: 'Wrong value',\n+ code: 404\n" +
'- code: 404,\n- foo: undefined\n }'
' Comparison {\n' +
' code: 404,\n' +
'- foo: undefined,\n' +
" message: 'Wrong value',\n" +
" name: 'TypeError'\n" +
' }'
}
);

Expand All @@ -899,9 +902,13 @@ common.expectsError(
code: 'ERR_ASSERTION',
name: 'AssertionError [ERR_ASSERTION]',
message: `${start}\n${actExp}\n\n` +
" Comparison {\n name: 'TypeError',\n" +
" message: 'Wrong value',\n+ code: 404\n" +
"- code: '404',\n- foo: undefined\n }"
' Comparison {\n' +
'+ code: 404,\n' +
"- code: '404',\n" +
'- foo: undefined,\n' +
" message: 'Wrong value',\n" +
" name: 'TypeError'\n" +
' }'
}
);

Expand Down Expand Up @@ -931,8 +938,11 @@ common.expectsError(
name: 'AssertionError [ERR_ASSERTION]',
code: 'ERR_ASSERTION',
message: `${start}\n${actExp}\n\n` +
" Comparison {\n+ name: 'TypeError',\n- name: 'Error'," +
"\n message: 'e'\n }"
' Comparison {\n' +
" message: 'e',\n" +
"+ name: 'TypeError'\n" +
"- name: 'Error'\n" +
' }'
}
);
assert.throws(
Expand All @@ -942,8 +952,11 @@ common.expectsError(
code: 'ERR_ASSERTION',
generatedMessage: true,
message: `${start}\n${actExp}\n\n` +
" Comparison {\n name: 'Error',\n+ message: 'foo'" +
"\n- message: ''\n }"
' Comparison {\n' +
"+ message: 'foo',\n" +
"- message: '',\n" +
" name: 'Error'\n" +
' }'
}
);

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ common.expectsError(() => {
}, {
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: /\+ message: 'Error for testing purposes: a'\n- message: \/\^Error/
message: /\+ message: 'Error for testing purposes: a',\n- message: \/\^Error/
});

// Test ERR_INVALID_FD_TYPE
Expand Down

0 comments on commit 28902f3

Please sign in to comment.