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. 463
      test/parallel/test-util-inspect.js

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

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