Skip to content

Commit

Permalink
test: refactor test-util-inspect
Browse files Browse the repository at this point in the history
* Handle a rejected Promise as that is expected to cause the process to
exit in a future version of Node.js. (Currently, it emits a warning.)

* Remove unneeded escaping in regexps

* Replace template strings with strings where there is no variable
substitution.

* A few minor readability changes (whitespace).

PR-URL: #9804
Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
Trott authored and addaleax committed Dec 5, 2016
1 parent 4c2ad8c commit 70633f9
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ for (const showHidden of [true, false]) {
assert.strictEqual(
util.inspect(array, true),
`${constructor.name} [\n` +
` 65,\n` +
` 97,\n` +
' 65,\n' +
' 97,\n' +
` [BYTES_PER_ELEMENT]: ${constructor.BYTES_PER_ELEMENT},\n` +
` [length]: ${length},\n` +
` [byteLength]: ${byteLength},\n` +
` [byteOffset]: 0,\n` +
' [byteOffset]: 0,\n' +
` [buffer]: ArrayBuffer { byteLength: ${byteLength} } ]`);
assert.strictEqual(
util.inspect(array, false),
Expand All @@ -168,23 +168,21 @@ for (const showHidden of [true, false]) {
Uint8ClampedArray ].forEach((constructor) => {
const length = 2;
const byteLength = length * constructor.BYTES_PER_ELEMENT;
const array = vm.runInNewContext('new constructor(new ArrayBuffer(' +
'byteLength), 0, length)',
{ constructor: constructor,
byteLength: byteLength,
length: length
});
const array = vm.runInNewContext(
'new constructor(new ArrayBuffer(byteLength), 0, length)',
{ constructor, byteLength, length }
);
array[0] = 65;
array[1] = 97;
assert.strictEqual(
util.inspect(array, true),
`${constructor.name} [\n` +
` 65,\n` +
` 97,\n` +
' 65,\n' +
' 97,\n' +
` [BYTES_PER_ELEMENT]: ${constructor.BYTES_PER_ELEMENT},\n` +
` [length]: ${length},\n` +
` [byteLength]: ${byteLength},\n` +
` [byteOffset]: 0,\n` +
' [byteOffset]: 0,\n' +
` [buffer]: ArrayBuffer { byteLength: ${byteLength} } ]`);
assert.strictEqual(
util.inspect(array, false),
Expand All @@ -208,8 +206,8 @@ for (const showHidden of [true, false]) {
// Objects without prototype
{
const out = util.inspect(Object.create(null,
{ name: {value: 'Tim', enumerable: true},
hidden: {value: 'secret'}}), true);
{ name: {value: 'Tim', enumerable: true},
hidden: {value: 'secret'}}), true);
if (out !== "{ [hidden]: 'secret', name: 'Tim' }" &&
out !== "{ name: 'Tim', [hidden]: 'secret' }") {
common.fail(`unexpected value for out ${out}`);
Expand Down Expand Up @@ -696,7 +694,14 @@ assert.strictEqual(

// test Promise
assert.strictEqual(util.inspect(Promise.resolve(3)), 'Promise { 3 }');
assert.strictEqual(util.inspect(Promise.reject(3)), 'Promise { <rejected> 3 }');

{
const rejected = Promise.reject(3);
assert.strictEqual(util.inspect(rejected), 'Promise { <rejected> 3 }');
// squelch UnhandledPromiseRejection
rejected.catch(() => {});
}

assert.strictEqual(
util.inspect(new Promise(function() {})),
'Promise { <pending> }'
Expand Down Expand Up @@ -831,7 +836,7 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));

{
const x = Array(101);
assert(/^\[ ... 101 more items \]$/.test(
assert(/^\[ ... 101 more items ]$/.test(
util.inspect(x, {maxArrayLength: 0})));
}

Expand All @@ -847,7 +852,7 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));

{
const x = new Uint8Array(101);
assert(/\[ ... 101 more items \]$/.test(
assert(/\[ ... 101 more items ]$/.test(
util.inspect(x, {maxArrayLength: 0})));
}

Expand Down

0 comments on commit 70633f9

Please sign in to comment.