Skip to content

Commit 9bdf62f

Browse files
aqrlnevanlucas
authored andcommittedMay 2, 2017
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 <#11576 (comment)>. PR-URL: #11779 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent b1d3f59 commit 9bdf62f

File tree

1 file changed

+314
-273
lines changed

1 file changed

+314
-273
lines changed
 

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

+314-273
Original file line numberDiff line numberDiff line change
@@ -233,74 +233,91 @@ assert.strictEqual(
233233

234234

235235
// Dynamic properties
236-
assert.strictEqual(util.inspect({get readonly() {}}),
237-
'{ readonly: [Getter] }');
236+
{
237+
assert.strictEqual(util.inspect({get readonly() {}}),
238+
'{ readonly: [Getter] }');
238239

239-
assert.strictEqual(util.inspect({get readwrite() {}, set readwrite(val) {}}),
240-
'{ readwrite: [Getter/Setter] }');
240+
assert.strictEqual(util.inspect({get readwrite() {}, set readwrite(val) {}}),
241+
'{ readwrite: [Getter/Setter] }');
241242

242-
assert.strictEqual(util.inspect({set writeonly(val) {}}),
243-
'{ writeonly: [Setter] }');
243+
assert.strictEqual(util.inspect({set writeonly(val) {}}),
244+
'{ writeonly: [Setter] }');
244245

245-
let value = {};
246-
value['a'] = value;
247-
assert.strictEqual(util.inspect(value), '{ a: [Circular] }');
246+
const value = {};
247+
value['a'] = value;
248+
assert.strictEqual(util.inspect(value), '{ a: [Circular] }');
249+
}
248250

249251
// Array with dynamic properties
250-
value = [1, 2, 3];
251-
Object.defineProperty(
252-
value,
253-
'growingLength',
254-
{
255-
enumerable: true,
256-
get: () => { this.push(true); return this.length; }
257-
}
258-
);
259-
assert.strictEqual(util.inspect(value), '[ 1, 2, 3, growingLength: [Getter] ]');
252+
{
253+
const value = [1, 2, 3];
254+
Object.defineProperty(
255+
value,
256+
'growingLength',
257+
{
258+
enumerable: true,
259+
get: function() { this.push(true); return this.length; }
260+
}
261+
);
262+
assert.strictEqual(util.inspect(value),
263+
'[ 1, 2, 3, growingLength: [Getter] ]');
264+
}
260265

261266
// Function with properties
262-
value = function() {};
263-
value.aprop = 42;
264-
assert.strictEqual(util.inspect(value), '{ [Function: value] aprop: 42 }');
267+
{
268+
const value = function() {};
269+
value.aprop = 42;
270+
assert.strictEqual(util.inspect(value), '{ [Function: value] aprop: 42 }');
271+
}
265272

266273
// Anonymous function with properties
267-
value = (() => function() {})();
268-
value.aprop = 42;
269-
assert.strictEqual(util.inspect(value), '{ [Function] aprop: 42 }');
274+
{
275+
const value = (() => function() {})();
276+
value.aprop = 42;
277+
assert.strictEqual(util.inspect(value), '{ [Function] aprop: 42 }');
278+
}
270279

271280
// Regular expressions with properties
272-
value = /123/ig;
273-
value.aprop = 42;
274-
assert.strictEqual(util.inspect(value), '{ /123/gi aprop: 42 }');
281+
{
282+
const value = /123/ig;
283+
value.aprop = 42;
284+
assert.strictEqual(util.inspect(value), '{ /123/gi aprop: 42 }');
285+
}
275286

276287
// Dates with properties
277-
value = new Date('Sun, 14 Feb 2010 11:48:40 GMT');
278-
value.aprop = 42;
279-
assert.strictEqual(util.inspect(value), '{ 2010-02-14T11:48:40.000Z aprop: 42 }'
280-
);
288+
{
289+
const value = new Date('Sun, 14 Feb 2010 11:48:40 GMT');
290+
value.aprop = 42;
291+
assert.strictEqual(util.inspect(value),
292+
'{ 2010-02-14T11:48:40.000Z aprop: 42 }');
293+
}
281294

282295
// test the internal isDate implementation
283-
const Date2 = vm.runInNewContext('Date');
284-
const d = new Date2();
285-
const orig = util.inspect(d);
286-
Date2.prototype.foo = 'bar';
287-
const after = util.inspect(d);
288-
assert.strictEqual(orig, after);
296+
{
297+
const Date2 = vm.runInNewContext('Date');
298+
const d = new Date2();
299+
const orig = util.inspect(d);
300+
Date2.prototype.foo = 'bar';
301+
const after = util.inspect(d);
302+
assert.strictEqual(orig, after);
303+
}
289304

290305
// test positive/negative zero
291306
assert.strictEqual(util.inspect(0), '0');
292307
assert.strictEqual(util.inspect(-0), '-0');
293308

294309
// test for sparse array
295-
const a = ['foo', 'bar', 'baz'];
296-
assert.strictEqual(util.inspect(a), '[ \'foo\', \'bar\', \'baz\' ]');
297-
delete a[1];
298-
assert.strictEqual(util.inspect(a), '[ \'foo\', , \'baz\' ]');
299-
assert.strictEqual(
300-
util.inspect(a, true),
301-
'[ \'foo\', , \'baz\', [length]: 3 ]'
302-
);
303-
assert.strictEqual(util.inspect(new Array(5)), '[ , , , , ]');
310+
{
311+
const a = ['foo', 'bar', 'baz'];
312+
assert.strictEqual(util.inspect(a), '[ \'foo\', \'bar\', \'baz\' ]');
313+
delete a[1];
314+
assert.strictEqual(util.inspect(a), '[ \'foo\', , \'baz\' ]');
315+
assert.strictEqual(
316+
util.inspect(a, true),
317+
'[ \'foo\', , \'baz\', [length]: 3 ]'
318+
);
319+
assert.strictEqual(util.inspect(new Array(5)), '[ , , , , ]');
320+
}
304321

305322
// test for Array constructor in different context
306323
{
@@ -318,98 +335,107 @@ assert.strictEqual(util.inspect(new Array(5)), '[ , , , , ]');
318335
}
319336

320337
// test for other constructors in different context
321-
let obj = vm.runInNewContext('(function(){return {}})()', {});
322-
assert.strictEqual(util.inspect(obj), '{}');
323-
obj = vm.runInNewContext('var m=new Map();m.set(1,2);m', {});
324-
assert.strictEqual(util.inspect(obj), 'Map { 1 => 2 }');
325-
obj = vm.runInNewContext('var s=new Set();s.add(1);s.add(2);s', {});
326-
assert.strictEqual(util.inspect(obj), 'Set { 1, 2 }');
327-
obj = vm.runInNewContext('fn=function(){};new Promise(fn,fn)', {});
328-
assert.strictEqual(util.inspect(obj), 'Promise { <pending> }');
338+
{
339+
let obj = vm.runInNewContext('(function(){return {}})()', {});
340+
assert.strictEqual(util.inspect(obj), '{}');
341+
obj = vm.runInNewContext('var m=new Map();m.set(1,2);m', {});
342+
assert.strictEqual(util.inspect(obj), 'Map { 1 => 2 }');
343+
obj = vm.runInNewContext('var s=new Set();s.add(1);s.add(2);s', {});
344+
assert.strictEqual(util.inspect(obj), 'Set { 1, 2 }');
345+
obj = vm.runInNewContext('fn=function(){};new Promise(fn,fn)', {});
346+
assert.strictEqual(util.inspect(obj), 'Promise { <pending> }');
347+
}
329348

330349
// test for property descriptors
331-
const getter = Object.create(null, {
332-
a: {
333-
get: function() { return 'aaa'; }
334-
}
335-
});
336-
const setter = Object.create(null, {
337-
b: {
338-
set: function() {}
339-
}
340-
});
341-
const getterAndSetter = Object.create(null, {
342-
c: {
343-
get: function() { return 'ccc'; },
344-
set: function() {}
345-
}
346-
});
347-
assert.strictEqual(util.inspect(getter, true), '{ [a]: [Getter] }');
348-
assert.strictEqual(util.inspect(setter, true), '{ [b]: [Setter] }');
349-
assert.strictEqual(
350-
util.inspect(getterAndSetter, true),
351-
'{ [c]: [Getter/Setter] }'
352-
);
350+
{
351+
const getter = Object.create(null, {
352+
a: {
353+
get: function() { return 'aaa'; }
354+
}
355+
});
356+
const setter = Object.create(null, {
357+
b: {
358+
set: function() {}
359+
}
360+
});
361+
const getterAndSetter = Object.create(null, {
362+
c: {
363+
get: function() { return 'ccc'; },
364+
set: function() {}
365+
}
366+
});
367+
assert.strictEqual(util.inspect(getter, true), '{ [a]: [Getter] }');
368+
assert.strictEqual(util.inspect(setter, true), '{ [b]: [Setter] }');
369+
assert.strictEqual(
370+
util.inspect(getterAndSetter, true),
371+
'{ [c]: [Getter/Setter] }'
372+
);
373+
}
353374

354375
// exceptions should print the error message, not '{}'
355-
const errors = [];
356-
errors.push(new Error());
357-
errors.push(new Error('FAIL'));
358-
errors.push(new TypeError('FAIL'));
359-
errors.push(new SyntaxError('FAIL'));
360-
errors.forEach(function(err) {
361-
assert.strictEqual(util.inspect(err), err.stack);
362-
});
363-
try {
364-
undef(); // eslint-disable-line no-undef
365-
} catch (e) {
366-
assert.strictEqual(util.inspect(e), e.stack);
367-
}
368-
const ex = util.inspect(new Error('FAIL'), true);
369-
assert(ex.includes('Error: FAIL'));
370-
assert(ex.includes('[stack]'));
371-
assert(ex.includes('[message]'));
376+
{
377+
const errors = [];
378+
errors.push(new Error());
379+
errors.push(new Error('FAIL'));
380+
errors.push(new TypeError('FAIL'));
381+
errors.push(new SyntaxError('FAIL'));
382+
errors.forEach((err) => {
383+
assert.strictEqual(util.inspect(err), err.stack);
384+
});
385+
try {
386+
undef(); // eslint-disable-line no-undef
387+
} catch (e) {
388+
assert.strictEqual(util.inspect(e), e.stack);
389+
}
390+
const ex = util.inspect(new Error('FAIL'), true);
391+
assert(ex.includes('Error: FAIL'));
392+
assert(ex.includes('[stack]'));
393+
assert(ex.includes('[message]'));
394+
}
395+
372396
// Doesn't capture stack trace
373-
function BadCustomError(msg) {
374-
Error.call(this);
375-
Object.defineProperty(this, 'message',
376-
{ value: msg, enumerable: false });
377-
Object.defineProperty(this, 'name',
378-
{ value: 'BadCustomError', enumerable: false });
379-
}
380-
util.inherits(BadCustomError, Error);
381-
assert.strictEqual(
382-
util.inspect(new BadCustomError('foo')),
383-
'[BadCustomError: foo]'
384-
);
397+
{
398+
function BadCustomError(msg) {
399+
Error.call(this);
400+
Object.defineProperty(this, 'message',
401+
{ value: msg, enumerable: false });
402+
Object.defineProperty(this, 'name',
403+
{ value: 'BadCustomError', enumerable: false });
404+
}
405+
util.inherits(BadCustomError, Error);
406+
assert.strictEqual(
407+
util.inspect(new BadCustomError('foo')),
408+
'[BadCustomError: foo]'
409+
);
410+
}
385411

386412
// GH-1941
387413
// should not throw:
388414
assert.strictEqual(util.inspect(Object.create(Date.prototype)), 'Date {}');
389415

390416
// GH-1944
391-
assert.doesNotThrow(function() {
417+
assert.doesNotThrow(() => {
392418
const d = new Date();
393419
d.toUTCString = null;
394420
util.inspect(d);
395421
});
396422

397-
assert.doesNotThrow(function() {
423+
assert.doesNotThrow(() => {
398424
const d = new Date();
399425
d.toISOString = null;
400426
util.inspect(d);
401427
});
402428

403-
assert.doesNotThrow(function() {
429+
assert.doesNotThrow(() => {
404430
const r = /regexp/;
405431
r.toString = null;
406432
util.inspect(r);
407433
});
408434

409435
// bug with user-supplied inspect function returns non-string
410-
assert.doesNotThrow(function() {
436+
assert.doesNotThrow(() => {
411437
util.inspect([{
412-
inspect: function() { return 123; }
438+
inspect: () => 123
413439
}]);
414440
});
415441

@@ -420,53 +446,57 @@ assert.doesNotThrow(function() {
420446
}
421447

422448
// util.inspect should not display the escaped value of a key.
423-
const w = {
424-
'\\': 1,
425-
'\\\\': 2,
426-
'\\\\\\': 3,
427-
'\\\\\\\\': 4,
428-
};
429-
430-
const y = ['a', 'b', 'c'];
431-
y['\\\\\\'] = 'd';
449+
{
450+
const w = {
451+
'\\': 1,
452+
'\\\\': 2,
453+
'\\\\\\': 3,
454+
'\\\\\\\\': 4,
455+
};
432456

433-
assert.strictEqual(
434-
util.inspect(w),
435-
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }'
436-
);
437-
assert.strictEqual(
438-
util.inspect(y),
439-
'[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]'
440-
);
457+
const y = ['a', 'b', 'c'];
458+
y['\\\\\\'] = 'd';
441459

442-
// util.inspect.styles and util.inspect.colors
443-
function test_color_style(style, input, implicit) {
444-
const color_name = util.inspect.styles[style];
445-
let color = ['', ''];
446-
if (util.inspect.colors[color_name])
447-
color = util.inspect.colors[color_name];
448-
449-
const without_color = util.inspect(input, false, 0, false);
450-
const with_color = util.inspect(input, false, 0, true);
451-
const expect = '\u001b[' + color[0] + 'm' + without_color +
452-
'\u001b[' + color[1] + 'm';
453460
assert.strictEqual(
454-
with_color,
455-
expect,
456-
`util.inspect color for style ${style}`);
461+
util.inspect(w),
462+
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }'
463+
);
464+
assert.strictEqual(
465+
util.inspect(y),
466+
'[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]'
467+
);
457468
}
458469

459-
test_color_style('special', function() {});
460-
test_color_style('number', 123.456);
461-
test_color_style('boolean', true);
462-
test_color_style('undefined', undefined);
463-
test_color_style('null', null);
464-
test_color_style('string', 'test string');
465-
test_color_style('date', new Date());
466-
test_color_style('regexp', /regexp/);
470+
// util.inspect.styles and util.inspect.colors
471+
{
472+
function testColorStyle(style, input, implicit) {
473+
const colorName = util.inspect.styles[style];
474+
let color = ['', ''];
475+
if (util.inspect.colors[colorName])
476+
color = util.inspect.colors[colorName];
477+
478+
const withoutColor = util.inspect(input, false, 0, false);
479+
const withColor = util.inspect(input, false, 0, true);
480+
const expect = '\u001b[' + color[0] + 'm' + withoutColor +
481+
'\u001b[' + color[1] + 'm';
482+
assert.strictEqual(
483+
withColor,
484+
expect,
485+
`util.inspect color for style ${style}`);
486+
}
487+
488+
testColorStyle('special', function() {});
489+
testColorStyle('number', 123.456);
490+
testColorStyle('boolean', true);
491+
testColorStyle('undefined', undefined);
492+
testColorStyle('null', null);
493+
testColorStyle('string', 'test string');
494+
testColorStyle('date', new Date());
495+
testColorStyle('regexp', /regexp/);
496+
}
467497

468498
// an object with "hasOwnProperty" overwritten should not throw
469-
assert.doesNotThrow(function() {
499+
assert.doesNotThrow(() => {
470500
util.inspect({
471501
hasOwnProperty: null
472502
});
@@ -509,7 +539,7 @@ assert.doesNotThrow(function() {
509539

510540
{
511541
// "customInspect" option can enable/disable calling inspect() on objects
512-
const subject = { inspect: function() { return 123; } };
542+
const subject = { inspect: () => 123 };
513543

514544
assert.strictEqual(
515545
util.inspect(subject, { customInspect: true }).includes('123'),
@@ -529,11 +559,11 @@ assert.doesNotThrow(function() {
529559
);
530560

531561
// custom inspect() functions should be able to return other Objects
532-
subject.inspect = function() { return { foo: 'bar' }; };
562+
subject.inspect = () => ({ foo: 'bar' });
533563

534564
assert.strictEqual(util.inspect(subject), '{ foo: \'bar\' }');
535565

536-
subject.inspect = function(depth, opts) {
566+
subject.inspect = (depth, opts) => {
537567
assert.strictEqual(opts.customInspectOptions, true);
538568
};
539569

@@ -542,7 +572,7 @@ assert.doesNotThrow(function() {
542572

543573
{
544574
// "customInspect" option can enable/disable calling [util.inspect.custom]()
545-
const subject = { [util.inspect.custom]: function() { return 123; } };
575+
const subject = { [util.inspect.custom]: () => 123 };
546576

547577
assert.strictEqual(
548578
util.inspect(subject, { customInspect: true }).includes('123'),
@@ -554,11 +584,11 @@ assert.doesNotThrow(function() {
554584
);
555585

556586
// a custom [util.inspect.custom]() should be able to return other Objects
557-
subject[util.inspect.custom] = function() { return { foo: 'bar' }; };
587+
subject[util.inspect.custom] = () => ({ foo: 'bar' });
558588

559589
assert.strictEqual(util.inspect(subject), '{ foo: \'bar\' }');
560590

561-
subject[util.inspect.custom] = function(depth, opts) {
591+
subject[util.inspect.custom] = (depth, opts) => {
562592
assert.strictEqual(opts.customInspectOptions, true);
563593
};
564594

@@ -596,38 +626,32 @@ assert.doesNotThrow(function() {
596626
'{ a: 123, inspect: [Function: inspect] }');
597627

598628
const subject = { a: 123, [util.inspect.custom]() { return this; } };
599-
assert.strictEqual(util.inspect(subject),
600-
'{ a: 123 }');
629+
assert.strictEqual(util.inspect(subject), '{ a: 123 }');
601630
}
602631

603632
// util.inspect with "colors" option should produce as many lines as without it
604-
function test_lines(input) {
605-
const count_lines = function(str) {
606-
return (str.match(/\n/g) || []).length;
607-
};
633+
{
634+
function testLines(input) {
635+
const countLines = (str) => (str.match(/\n/g) || []).length;
636+
const withoutColor = util.inspect(input);
637+
const withColor = util.inspect(input, {colors: true});
638+
assert.strictEqual(countLines(withoutColor), countLines(withColor));
639+
}
608640

609-
const without_color = util.inspect(input);
610-
const with_color = util.inspect(input, {colors: true});
611-
assert.strictEqual(count_lines(without_color), count_lines(with_color));
641+
const bigArray = new Array(100).fill().map((value, index) => index);
642+
643+
testLines([1, 2, 3, 4, 5, 6, 7]);
644+
testLines(bigArray);
645+
testLines({foo: 'bar', baz: 35, b: {a: 35}});
646+
testLines({
647+
foo: 'bar',
648+
baz: 35,
649+
b: {a: 35},
650+
veryLongKey: 'very long value',
651+
evenLongerKey: ['with even longer value in array']
652+
});
612653
}
613654

614-
test_lines([1, 2, 3, 4, 5, 6, 7]);
615-
test_lines(function() {
616-
const big_array = [];
617-
for (let i = 0; i < 100; i++) {
618-
big_array.push(i);
619-
}
620-
return big_array;
621-
}());
622-
test_lines({foo: 'bar', baz: 35, b: {a: 35}});
623-
test_lines({
624-
foo: 'bar',
625-
baz: 35,
626-
b: {a: 35},
627-
very_long_key: 'very_long_value',
628-
even_longer_key: ['with even longer value in array']
629-
});
630-
631655
// test boxed primitives output the correct values
632656
assert.strictEqual(util.inspect(new String('test')), '[String: \'test\']');
633657
assert.strictEqual(
@@ -642,17 +666,19 @@ assert.strictEqual(util.inspect(new Number(-1.1)), '[Number: -1.1]');
642666
assert.strictEqual(util.inspect(new Number(13.37)), '[Number: 13.37]');
643667

644668
// test boxed primitives with own properties
645-
const str = new String('baz');
646-
str.foo = 'bar';
647-
assert.strictEqual(util.inspect(str), '{ [String: \'baz\'] foo: \'bar\' }');
669+
{
670+
const str = new String('baz');
671+
str.foo = 'bar';
672+
assert.strictEqual(util.inspect(str), '{ [String: \'baz\'] foo: \'bar\' }');
648673

649-
const bool = new Boolean(true);
650-
bool.foo = 'bar';
651-
assert.strictEqual(util.inspect(bool), '{ [Boolean: true] foo: \'bar\' }');
674+
const bool = new Boolean(true);
675+
bool.foo = 'bar';
676+
assert.strictEqual(util.inspect(bool), '{ [Boolean: true] foo: \'bar\' }');
652677

653-
const num = new Number(13.37);
654-
num.foo = 'bar';
655-
assert.strictEqual(util.inspect(num), '{ [Number: 13.37] foo: \'bar\' }');
678+
const num = new Number(13.37);
679+
num.foo = 'bar';
680+
assert.strictEqual(util.inspect(num), '{ [Number: 13.37] foo: \'bar\' }');
681+
}
656682

657683
// test es6 Symbol
658684
if (typeof Symbol !== 'undefined') {
@@ -682,14 +708,16 @@ if (typeof Symbol !== 'undefined') {
682708
}
683709

684710
// test Set
685-
assert.strictEqual(util.inspect(new Set()), 'Set {}');
686-
assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set { 1, 2, 3 }');
687-
const set = new Set(['foo']);
688-
set.bar = 42;
689-
assert.strictEqual(
690-
util.inspect(set, true),
691-
'Set { \'foo\', [size]: 1, bar: 42 }'
692-
);
711+
{
712+
assert.strictEqual(util.inspect(new Set()), 'Set {}');
713+
assert.strictEqual(util.inspect(new Set([1, 2, 3])), 'Set { 1, 2, 3 }');
714+
const set = new Set(['foo']);
715+
set.bar = 42;
716+
assert.strictEqual(
717+
util.inspect(set, true),
718+
'Set { \'foo\', [size]: 1, bar: 42 }'
719+
);
720+
}
693721

694722
// test Map
695723
{
@@ -703,83 +731,92 @@ assert.strictEqual(
703731
}
704732

705733
// test Promise
706-
assert.strictEqual(util.inspect(Promise.resolve(3)), 'Promise { 3 }');
707-
708734
{
735+
const resolved = Promise.resolve(3);
736+
assert.strictEqual(util.inspect(resolved), 'Promise { 3 }');
737+
709738
const rejected = Promise.reject(3);
710739
assert.strictEqual(util.inspect(rejected), 'Promise { <rejected> 3 }');
711740
// squelch UnhandledPromiseRejection
712741
rejected.catch(() => {});
713-
}
714742

715-
assert.strictEqual(
716-
util.inspect(new Promise(function() {})),
717-
'Promise { <pending> }'
718-
);
719-
const promise = Promise.resolve('foo');
720-
promise.bar = 42;
721-
assert.strictEqual(util.inspect(promise), 'Promise { \'foo\', bar: 42 }');
743+
const pending = new Promise(() => {});
744+
assert.strictEqual(util.inspect(pending), 'Promise { <pending> }');
745+
746+
const promiseWithProperty = Promise.resolve('foo');
747+
promiseWithProperty.bar = 42;
748+
assert.strictEqual(util.inspect(promiseWithProperty),
749+
'Promise { \'foo\', bar: 42 }');
750+
}
722751

723752
// Make sure it doesn't choke on polyfills. Unlike Set/Map, there is no standard
724753
// interface to synchronously inspect a Promise, so our techniques only work on
725754
// a bonafide native Promise.
726-
const oldPromise = Promise;
727-
global.Promise = function() { this.bar = 42; };
728-
assert.strictEqual(util.inspect(new Promise()), '{ bar: 42 }');
729-
global.Promise = oldPromise;
730-
731-
// Map/Set Iterators
732-
const m = new Map([['foo', 'bar']]);
733-
assert.strictEqual(util.inspect(m.keys()), 'MapIterator { \'foo\' }');
734-
assert.strictEqual(util.inspect(m.values()), 'MapIterator { \'bar\' }');
735-
assert.strictEqual(util.inspect(m.entries()),
736-
'MapIterator { [ \'foo\', \'bar\' ] }');
737-
// make sure the iterator doesn't get consumed
738-
let keys = m.keys();
739-
assert.strictEqual(util.inspect(keys), 'MapIterator { \'foo\' }');
740-
assert.strictEqual(util.inspect(keys), 'MapIterator { \'foo\' }');
741-
742-
const s = new Set([1, 3]);
743-
assert.strictEqual(util.inspect(s.keys()), 'SetIterator { 1, 3 }');
744-
assert.strictEqual(util.inspect(s.values()), 'SetIterator { 1, 3 }');
745-
assert.strictEqual(util.inspect(s.entries()),
746-
'SetIterator { [ 1, 1 ], [ 3, 3 ] }');
747-
// make sure the iterator doesn't get consumed
748-
keys = s.keys();
749-
assert.strictEqual(util.inspect(keys), 'SetIterator { 1, 3 }');
750-
assert.strictEqual(util.inspect(keys), 'SetIterator { 1, 3 }');
755+
{
756+
const oldPromise = Promise;
757+
global.Promise = function() { this.bar = 42; };
758+
assert.strictEqual(util.inspect(new Promise()), '{ bar: 42 }');
759+
global.Promise = oldPromise;
760+
}
761+
762+
// Test Map iterators
763+
{
764+
const map = new Map([['foo', 'bar']]);
765+
assert.strictEqual(util.inspect(map.keys()), 'MapIterator { \'foo\' }');
766+
assert.strictEqual(util.inspect(map.values()), 'MapIterator { \'bar\' }');
767+
assert.strictEqual(util.inspect(map.entries()),
768+
'MapIterator { [ \'foo\', \'bar\' ] }');
769+
// make sure the iterator doesn't get consumed
770+
const keys = map.keys();
771+
assert.strictEqual(util.inspect(keys), 'MapIterator { \'foo\' }');
772+
assert.strictEqual(util.inspect(keys), 'MapIterator { \'foo\' }');
773+
}
774+
775+
// Test Set iterators
776+
{
777+
const aSet = new Set([1, 3]);
778+
assert.strictEqual(util.inspect(aSet.keys()), 'SetIterator { 1, 3 }');
779+
assert.strictEqual(util.inspect(aSet.values()), 'SetIterator { 1, 3 }');
780+
assert.strictEqual(util.inspect(aSet.entries()),
781+
'SetIterator { [ 1, 1 ], [ 3, 3 ] }');
782+
// make sure the iterator doesn't get consumed
783+
const keys = aSet.keys();
784+
assert.strictEqual(util.inspect(keys), 'SetIterator { 1, 3 }');
785+
assert.strictEqual(util.inspect(keys), 'SetIterator { 1, 3 }');
786+
}
751787

752788
// Test alignment of items in container
753789
// Assumes that the first numeric character is the start of an item.
790+
{
791+
function checkAlignment(container) {
792+
const lines = util.inspect(container).split('\n');
793+
let pos;
794+
lines.forEach((line) => {
795+
const npos = line.search(/\d/);
796+
if (npos !== -1) {
797+
if (pos !== undefined) {
798+
assert.strictEqual(pos, npos, 'container items not aligned');
799+
}
800+
pos = npos;
801+
}
802+
});
803+
}
754804

755-
function checkAlignment(container) {
756-
const lines = util.inspect(container).split('\n');
757-
let pos;
758-
lines.forEach(function(line) {
759-
const npos = line.search(/\d/);
760-
if (npos !== -1) {
761-
if (pos !== undefined)
762-
assert.strictEqual(pos, npos, 'container items not aligned');
763-
pos = npos;
764-
}
765-
});
766-
}
767-
768-
const big_array = [];
769-
for (let i = 0; i < 100; i++) {
770-
big_array.push(i);
771-
}
805+
const bigArray = [];
806+
for (let i = 0; i < 100; i++) {
807+
bigArray.push(i);
808+
}
772809

773-
checkAlignment(big_array);
774-
checkAlignment(function() {
775810
const obj = {};
776-
big_array.forEach(function(v) {
777-
obj[v] = null;
811+
bigArray.forEach((prop) => {
812+
obj[prop] = null;
778813
});
779-
return obj;
780-
}());
781-
checkAlignment(new Set(big_array));
782-
checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
814+
815+
checkAlignment(bigArray);
816+
checkAlignment(obj);
817+
checkAlignment(new Set(bigArray));
818+
checkAlignment(new Map(bigArray.map((number) => [number, null])));
819+
}
783820

784821

785822
// Test display of constructors
@@ -800,7 +837,7 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
800837
'SetSubclass { 1, 2, 3 }');
801838
assert.strictEqual(util.inspect(new MapSubclass([['foo', 42]])),
802839
'MapSubclass { \'foo\' => 42 }');
803-
assert.strictEqual(util.inspect(new PromiseSubclass(function() {})),
840+
assert.strictEqual(util.inspect(new PromiseSubclass(() => {})),
804841
'PromiseSubclass { <pending> }');
805842
}
806843

@@ -836,54 +873,58 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
836873
// https://github.com/nodejs/node/pull/6334 is backported.
837874
{
838875
const x = Array(101);
839-
assert(/1 more item/.test(util.inspect(x)));
876+
assert(util.inspect(x).endsWith('1 more item ]'));
840877
}
841878

842879
{
843880
const x = Array(101);
844-
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101})));
881+
assert(!util.inspect(x, { maxArrayLength: 101 }).endsWith('1 more item ]'));
845882
}
846883

847884
{
848885
const x = Array(101);
849-
assert(/^\[ ... 101 more items ]$/.test(
850-
util.inspect(x, {maxArrayLength: 0})));
886+
assert.strictEqual(util.inspect(x, { maxArrayLength: 0 }),
887+
'[ ... 101 more items ]');
851888
}
852889

853890
{
854891
const x = new Uint8Array(101);
855-
assert(/1 more item/.test(util.inspect(x)));
892+
assert(util.inspect(x).endsWith('1 more item ]'));
856893
}
857894

858895
{
859896
const x = new Uint8Array(101);
860-
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101})));
897+
assert(!util.inspect(x, { maxArrayLength: 101 }).endsWith('1 more item ]'));
861898
}
862899

863900
{
864901
const x = new Uint8Array(101);
865-
assert(/\[ ... 101 more items ]$/.test(
866-
util.inspect(x, {maxArrayLength: 0})));
902+
assert.strictEqual(util.inspect(x, { maxArrayLength: 0 }),
903+
'Uint8Array [ ... 101 more items ]');
867904
}
868905

869906
{
870907
const x = Array(101);
871-
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null})));
908+
assert(!util.inspect(x, { maxArrayLength: null }).endsWith('1 more item ]'));
872909
}
873910

874911
{
875912
const x = Array(101);
876-
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
913+
assert(!util.inspect(
914+
x, { maxArrayLength: Infinity }
915+
).endsWith('1 more item ]'));
877916
}
878917

879918
{
880919
const x = new Uint8Array(101);
881-
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null})));
920+
assert(!util.inspect(x, { maxArrayLength: null }).endsWith('1 more item ]'));
882921
}
883922

884923
{
885924
const x = new Uint8Array(101);
886-
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
925+
assert(!util.inspect(
926+
x, { maxArrayLength: Infinity }
927+
).endsWith('1 more item ]'));
887928
}
888929

889930
{

0 commit comments

Comments
 (0)
Please sign in to comment.