Browse Source

test: refactor test-util-inspect.js

* Enclose tests that used to introduce module-level variables into
  their own scopes.
* Replace ES5 anonymous functions with arrow functions where it makes
  sense.
* And make one arrow function a regular function thus fixing a bug in a
  getter inside an object created in "Array with dynamic properties"
  test.  This getter has never been invoked though, so the test hasn't been
  failing.
* Convert snake_case identifiers to camelCase.
* Make some variable names more readable.
* Replace regular expressions in maxArrayLength tests with simple
  assert.strictEquals() and assert(...endsWith()) checks, as suggested
  in <https://github.com/nodejs/node/pull/11576#discussion_r103738263>.

PR-URL: https://github.com/nodejs/node/pull/11779
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
v7.x
Alexey Orlenko 8 years ago
committed by Evan Lucas
parent
commit
9bdf62f7a7
  1. 257
      test/parallel/test-util-inspect.js

257
test/parallel/test-util-inspect.js

@ -233,6 +233,7 @@ assert.strictEqual(
// Dynamic properties
{
assert.strictEqual(util.inspect({get readonly() {}}),
'{ readonly: [Getter] }');
@ -242,56 +243,71 @@ assert.strictEqual(util.inspect({get readwrite() {}, set readwrite(val) {}}),
assert.strictEqual(util.inspect({set writeonly(val) {}}),
'{ writeonly: [Setter] }');
let value = {};
const value = {};
value['a'] = value;
assert.strictEqual(util.inspect(value), '{ a: [Circular] }');
}
// Array with dynamic properties
value = [1, 2, 3];
{
const value = [1, 2, 3];
Object.defineProperty(
value,
'growingLength',
{
enumerable: true,
get: () => { this.push(true); return this.length; }
get: function() { this.push(true); return this.length; }
}
);
assert.strictEqual(util.inspect(value), '[ 1, 2, 3, growingLength: [Getter] ]');
assert.strictEqual(util.inspect(value),
'[ 1, 2, 3, growingLength: [Getter] ]');
}
// Function with properties
value = function() {};
{
const value = function() {};
value.aprop = 42;
assert.strictEqual(util.inspect(value), '{ [Function: value] aprop: 42 }');
}
// Anonymous function with properties
value = (() => function() {})();
{
const value = (() => function() {})();
value.aprop = 42;
assert.strictEqual(util.inspect(value), '{ [Function] aprop: 42 }');
}
// Regular expressions with properties
value = /123/ig;
{
const value = /123/ig;
value.aprop = 42;
assert.strictEqual(util.inspect(value), '{ /123/gi aprop: 42 }');
}
// Dates with properties
value = new Date('Sun, 14 Feb 2010 11:48:40 GMT');
{
const value = new Date('Sun, 14 Feb 2010 11:48:40 GMT');
value.aprop = 42;
assert.strictEqual(util.inspect(value), '{ 2010-02-14T11:48:40.000Z aprop: 42 }'
);
assert.strictEqual(util.inspect(value),
'{ 2010-02-14T11:48:40.000Z aprop: 42 }');
}
// test the internal isDate implementation
{
const Date2 = vm.runInNewContext('Date');
const d = new Date2();
const orig = util.inspect(d);
Date2.prototype.foo = 'bar';
const after = util.inspect(d);
assert.strictEqual(orig, after);
}
// test positive/negative zero
assert.strictEqual(util.inspect(0), '0');
assert.strictEqual(util.inspect(-0), '-0');
// test for sparse array
{
const a = ['foo', 'bar', 'baz'];
assert.strictEqual(util.inspect(a), '[ \'foo\', \'bar\', \'baz\' ]');
delete a[1];
@ -301,6 +317,7 @@ assert.strictEqual(
'[ \'foo\', , \'baz\', [length]: 3 ]'
);
assert.strictEqual(util.inspect(new Array(5)), '[ , , , , ]');
}
// test for Array constructor in different context
{
@ -318,6 +335,7 @@ assert.strictEqual(util.inspect(new Array(5)), '[ , , , , ]');
}
// test for other constructors in different context
{
let obj = vm.runInNewContext('(function(){return {}})()', {});
assert.strictEqual(util.inspect(obj), '{}');
obj = vm.runInNewContext('var m=new Map();m.set(1,2);m', {});
@ -326,8 +344,10 @@ obj = vm.runInNewContext('var s=new Set();s.add(1);s.add(2);s', {});
assert.strictEqual(util.inspect(obj), 'Set { 1, 2 }');
obj = vm.runInNewContext('fn=function(){};new Promise(fn,fn)', {});
assert.strictEqual(util.inspect(obj), 'Promise { <pending> }');
}
// test for property descriptors
{
const getter = Object.create(null, {
a: {
get: function() { return 'aaa'; }
@ -350,14 +370,16 @@ assert.strictEqual(
util.inspect(getterAndSetter, true),
'{ [c]: [Getter/Setter] }'
);
}
// exceptions should print the error message, not '{}'
{
const errors = [];
errors.push(new Error());
errors.push(new Error('FAIL'));
errors.push(new TypeError('FAIL'));
errors.push(new SyntaxError('FAIL'));
errors.forEach(function(err) {
errors.forEach((err) => {
assert.strictEqual(util.inspect(err), err.stack);
});
try {
@ -369,7 +391,10 @@ const ex = util.inspect(new Error('FAIL'), true);
assert(ex.includes('Error: FAIL'));
assert(ex.includes('[stack]'));
assert(ex.includes('[message]'));
}
// Doesn't capture stack trace
{
function BadCustomError(msg) {
Error.call(this);
Object.defineProperty(this, 'message',
@ -382,34 +407,35 @@ assert.strictEqual(
util.inspect(new BadCustomError('foo')),
'[BadCustomError: foo]'
);
}
// GH-1941
// should not throw:
assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
// GH-1944
assert.doesNotThrow(function() {
assert.doesNotThrow(() => {
const d = new Date();
d.toUTCString = null;
util.inspect(d);
});
assert.doesNotThrow(function() {
assert.doesNotThrow(() => {
const d = new Date();
d.toISOString = null;
util.inspect(d);
});
assert.doesNotThrow(function() {
assert.doesNotThrow(() => {
const r = /regexp/;
r.toString = null;
util.inspect(r);
});
// bug with user-supplied inspect function returns non-string
assert.doesNotThrow(function() {
assert.doesNotThrow(() => {
util.inspect([{
inspect: function() { return 123; }
inspect: () => 123
}]);
});
@ -420,6 +446,7 @@ assert.doesNotThrow(function() {
}
// util.inspect should not display the escaped value of a key.
{
const w = {
'\\': 1,
'\\\\': 2,
@ -438,35 +465,38 @@ assert.strictEqual(
util.inspect(y),
'[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]'
);
}
// util.inspect.styles and util.inspect.colors
function test_color_style(style, input, implicit) {
const color_name = util.inspect.styles[style];
{
function testColorStyle(style, input, implicit) {
const colorName = util.inspect.styles[style];
let color = ['', ''];
if (util.inspect.colors[color_name])
color = util.inspect.colors[color_name];
if (util.inspect.colors[colorName])
color = util.inspect.colors[colorName];
const without_color = util.inspect(input, false, 0, false);
const with_color = util.inspect(input, false, 0, true);
const expect = '\u001b[' + color[0] + 'm' + without_color +
const withoutColor = util.inspect(input, false, 0, false);
const withColor = util.inspect(input, false, 0, true);
const expect = '\u001b[' + color[0] + 'm' + withoutColor +
'\u001b[' + color[1] + 'm';
assert.strictEqual(
with_color,
withColor,
expect,
`util.inspect color for style ${style}`);
}
test_color_style('special', function() {});
test_color_style('number', 123.456);
test_color_style('boolean', true);
test_color_style('undefined', undefined);
test_color_style('null', null);
test_color_style('string', 'test string');
test_color_style('date', new Date());
test_color_style('regexp', /regexp/);
testColorStyle('special', function() {});
testColorStyle('number', 123.456);
testColorStyle('boolean', true);
testColorStyle('undefined', undefined);
testColorStyle('null', null);
testColorStyle('string', 'test string');
testColorStyle('date', new Date());
testColorStyle('regexp', /regexp/);
}
// an object with "hasOwnProperty" overwritten should not throw
assert.doesNotThrow(function() {
assert.doesNotThrow(() => {
util.inspect({
hasOwnProperty: null
});
@ -509,7 +539,7 @@ assert.doesNotThrow(function() {
{
// "customInspect" option can enable/disable calling inspect() on objects
const subject = { inspect: function() { return 123; } };
const subject = { inspect: () => 123 };
assert.strictEqual(
util.inspect(subject, { customInspect: true }).includes('123'),
@ -529,11 +559,11 @@ assert.doesNotThrow(function() {
);
// custom inspect() functions should be able to return other Objects
subject.inspect = function() { return { foo: 'bar' }; };
subject.inspect = () => ({ foo: 'bar' });
assert.strictEqual(util.inspect(subject), '{ foo: \'bar\' }');
subject.inspect = function(depth, opts) {
subject.inspect = (depth, opts) => {
assert.strictEqual(opts.customInspectOptions, true);
};
@ -542,7 +572,7 @@ assert.doesNotThrow(function() {
{
// "customInspect" option can enable/disable calling [util.inspect.custom]()
const subject = { [util.inspect.custom]: function() { return 123; } };
const subject = { [util.inspect.custom]: () => 123 };
assert.strictEqual(
util.inspect(subject, { customInspect: true }).includes('123'),
@ -554,11 +584,11 @@ assert.doesNotThrow(function() {
);
// a custom [util.inspect.custom]() should be able to return other Objects
subject[util.inspect.custom] = function() { return { foo: 'bar' }; };
subject[util.inspect.custom] = () => ({ foo: 'bar' });
assert.strictEqual(util.inspect(subject), '{ foo: \'bar\' }');
subject[util.inspect.custom] = function(depth, opts) {
subject[util.inspect.custom] = (depth, opts) => {
assert.strictEqual(opts.customInspectOptions, true);
};
@ -596,37 +626,31 @@ assert.doesNotThrow(function() {
'{ a: 123, inspect: [Function: inspect] }');
const subject = { a: 123, [util.inspect.custom]() { return this; } };
assert.strictEqual(util.inspect(subject),
'{ a: 123 }');
assert.strictEqual(util.inspect(subject), '{ a: 123 }');
}
// util.inspect with "colors" option should produce as many lines as without it
function test_lines(input) {
const count_lines = function(str) {
return (str.match(/\n/g) || []).length;
};
const without_color = util.inspect(input);
const with_color = util.inspect(input, {colors: true});
assert.strictEqual(count_lines(without_color), count_lines(with_color));
{
function testLines(input) {
const countLines = (str) => (str.match(/\n/g) || []).length;
const withoutColor = util.inspect(input);
const withColor = util.inspect(input, {colors: true});
assert.strictEqual(countLines(withoutColor), countLines(withColor));
}
test_lines([1, 2, 3, 4, 5, 6, 7]);
test_lines(function() {
const big_array = [];
for (let i = 0; i < 100; i++) {
big_array.push(i);
}
return big_array;
}());
test_lines({foo: 'bar', baz: 35, b: {a: 35}});
test_lines({
const bigArray = new Array(100).fill().map((value, index) => index);
testLines([1, 2, 3, 4, 5, 6, 7]);
testLines(bigArray);
testLines({foo: 'bar', baz: 35, b: {a: 35}});
testLines({
foo: 'bar',
baz: 35,
b: {a: 35},
very_long_key: 'very_long_value',
even_longer_key: ['with even longer value in array']
veryLongKey: 'very long value',
evenLongerKey: ['with even longer value in array']
});
}
// test boxed primitives output the correct values
assert.strictEqual(util.inspect(new String('test')), '[String: \'test\']');
@ -642,6 +666,7 @@ assert.strictEqual(util.inspect(new Number(-1.1)), '[Number: -1.1]');
assert.strictEqual(util.inspect(new Number(13.37)), '[Number: 13.37]');
// test boxed primitives with own properties
{
const str = new String('baz');
str.foo = 'bar';
assert.strictEqual(util.inspect(str), '{ [String: \'baz\'] foo: \'bar\' }');
@ -653,6 +678,7 @@ assert.strictEqual(util.inspect(bool), '{ [Boolean: true] foo: \'bar\' }');
const num = new Number(13.37);
num.foo = 'bar';
assert.strictEqual(util.inspect(num), '{ [Number: 13.37] foo: \'bar\' }');
}
// test es6 Symbol
if (typeof Symbol !== 'undefined') {
@ -682,6 +708,7 @@ if (typeof Symbol !== 'undefined') {
}
// test Set
{
assert.strictEqual(util.inspect(new Set()), 'Set {}');
assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set { 1, 2, 3 }');
const set = new Set(['foo']);
@ -690,6 +717,7 @@ assert.strictEqual(
util.inspect(set, true),
'Set { \'foo\', [size]: 1, bar: 42 }'
);
}
// test Map
{
@ -703,83 +731,92 @@ assert.strictEqual(
}
// test Promise
assert.strictEqual(util.inspect(Promise.resolve(3)), 'Promise { 3 }');
{
const resolved = Promise.resolve(3);
assert.strictEqual(util.inspect(resolved), 'Promise { 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> }'
);
const promise = Promise.resolve('foo');
promise.bar = 42;
assert.strictEqual(util.inspect(promise), 'Promise { \'foo\', bar: 42 }');
const pending = new Promise(() => {});
assert.strictEqual(util.inspect(pending), 'Promise { <pending> }');
const promiseWithProperty = Promise.resolve('foo');
promiseWithProperty.bar = 42;
assert.strictEqual(util.inspect(promiseWithProperty),
'Promise { \'foo\', bar: 42 }');
}
// Make sure it doesn't choke on polyfills. Unlike Set/Map, there is no standard
// interface to synchronously inspect a Promise, so our techniques only work on
// a bonafide native Promise.
{
const oldPromise = Promise;
global.Promise = function() { this.bar = 42; };
assert.strictEqual(util.inspect(new Promise()), '{ bar: 42 }');
global.Promise = oldPromise;
}
// Map/Set Iterators
const m = new Map([['foo', 'bar']]);
assert.strictEqual(util.inspect(m.keys()), 'MapIterator { \'foo\' }');
assert.strictEqual(util.inspect(m.values()), 'MapIterator { \'bar\' }');
assert.strictEqual(util.inspect(m.entries()),
// Test Map iterators
{
const map = new Map([['foo', 'bar']]);
assert.strictEqual(util.inspect(map.keys()), 'MapIterator { \'foo\' }');
assert.strictEqual(util.inspect(map.values()), 'MapIterator { \'bar\' }');
assert.strictEqual(util.inspect(map.entries()),
'MapIterator { [ \'foo\', \'bar\' ] }');
// make sure the iterator doesn't get consumed
let keys = m.keys();
const keys = map.keys();
assert.strictEqual(util.inspect(keys), 'MapIterator { \'foo\' }');
assert.strictEqual(util.inspect(keys), 'MapIterator { \'foo\' }');
}
const s = new Set([1, 3]);
assert.strictEqual(util.inspect(s.keys()), 'SetIterator { 1, 3 }');
assert.strictEqual(util.inspect(s.values()), 'SetIterator { 1, 3 }');
assert.strictEqual(util.inspect(s.entries()),
// Test Set iterators
{
const aSet = new Set([1, 3]);
assert.strictEqual(util.inspect(aSet.keys()), 'SetIterator { 1, 3 }');
assert.strictEqual(util.inspect(aSet.values()), 'SetIterator { 1, 3 }');
assert.strictEqual(util.inspect(aSet.entries()),
'SetIterator { [ 1, 1 ], [ 3, 3 ] }');
// make sure the iterator doesn't get consumed
keys = s.keys();
const keys = aSet.keys();
assert.strictEqual(util.inspect(keys), 'SetIterator { 1, 3 }');
assert.strictEqual(util.inspect(keys), 'SetIterator { 1, 3 }');
}
// Test alignment of items in container
// Assumes that the first numeric character is the start of an item.
{
function checkAlignment(container) {
const lines = util.inspect(container).split('\n');
let pos;
lines.forEach(function(line) {
lines.forEach((line) => {
const npos = line.search(/\d/);
if (npos !== -1) {
if (pos !== undefined)
if (pos !== undefined) {
assert.strictEqual(pos, npos, 'container items not aligned');
}
pos = npos;
}
});
}
const big_array = [];
const bigArray = [];
for (let i = 0; i < 100; i++) {
big_array.push(i);
bigArray.push(i);
}
checkAlignment(big_array);
checkAlignment(function() {
const obj = {};
big_array.forEach(function(v) {
obj[v] = null;
bigArray.forEach((prop) => {
obj[prop] = null;
});
return obj;
}());
checkAlignment(new Set(big_array));
checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
checkAlignment(bigArray);
checkAlignment(obj);
checkAlignment(new Set(bigArray));
checkAlignment(new Map(bigArray.map((number) => [number, null])));
}
// Test display of constructors
@ -800,7 +837,7 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
'SetSubclass { 1, 2, 3 }');
assert.strictEqual(util.inspect(new MapSubclass([['foo', 42]])),
'MapSubclass { \'foo\' => 42 }');
assert.strictEqual(util.inspect(new PromiseSubclass(function() {})),
assert.strictEqual(util.inspect(new PromiseSubclass(() => {})),
'PromiseSubclass { <pending> }');
}
@ -836,54 +873,58 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
// https://github.com/nodejs/node/pull/6334 is backported.
{
const x = Array(101);
assert(/1 more item/.test(util.inspect(x)));
assert(util.inspect(x).endsWith('1 more item ]'));
}
{
const x = Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101})));
assert(!util.inspect(x, { maxArrayLength: 101 }).endsWith('1 more item ]'));
}
{
const x = Array(101);
assert(/^\[ ... 101 more items ]$/.test(
util.inspect(x, {maxArrayLength: 0})));
assert.strictEqual(util.inspect(x, { maxArrayLength: 0 }),
'[ ... 101 more items ]');
}
{
const x = new Uint8Array(101);
assert(/1 more item/.test(util.inspect(x)));
assert(util.inspect(x).endsWith('1 more item ]'));
}
{
const x = new Uint8Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101})));
assert(!util.inspect(x, { maxArrayLength: 101 }).endsWith('1 more item ]'));
}
{
const x = new Uint8Array(101);
assert(/\[ ... 101 more items ]$/.test(
util.inspect(x, {maxArrayLength: 0})));
assert.strictEqual(util.inspect(x, { maxArrayLength: 0 }),
'Uint8Array [ ... 101 more items ]');
}
{
const x = Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null})));
assert(!util.inspect(x, { maxArrayLength: null }).endsWith('1 more item ]'));
}
{
const x = Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
assert(!util.inspect(
x, { maxArrayLength: Infinity }
).endsWith('1 more item ]'));
}
{
const x = new Uint8Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null})));
assert(!util.inspect(x, { maxArrayLength: null }).endsWith('1 more item ]'));
}
{
const x = new Uint8Array(101);
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
assert(!util.inspect(
x, { maxArrayLength: Infinity }
).endsWith('1 more item ]'));
}
{

Loading…
Cancel
Save