@@ -233,74 +233,91 @@ assert.strictEqual(
233
233
234
234
235
235
// Dynamic properties
236
- assert . strictEqual ( util . inspect ( { get readonly ( ) { } } ) ,
237
- '{ readonly: [Getter] }' ) ;
236
+ {
237
+ assert . strictEqual ( util . inspect ( { get readonly ( ) { } } ) ,
238
+ '{ readonly: [Getter] }' ) ;
238
239
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] }' ) ;
241
242
242
- assert . strictEqual ( util . inspect ( { set writeonly ( val ) { } } ) ,
243
- '{ writeonly: [Setter] }' ) ;
243
+ assert . strictEqual ( util . inspect ( { set writeonly ( val ) { } } ) ,
244
+ '{ writeonly: [Setter] }' ) ;
244
245
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
+ }
248
250
249
251
// 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
+ }
260
265
261
266
// 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
+ }
265
272
266
273
// 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
+ }
270
279
271
280
// Regular expressions with properties
272
- value = / 1 2 3 / ig;
273
- value . aprop = 42 ;
274
- assert . strictEqual ( util . inspect ( value ) , '{ /123/gi aprop: 42 }' ) ;
281
+ {
282
+ const value = / 1 2 3 / ig;
283
+ value . aprop = 42 ;
284
+ assert . strictEqual ( util . inspect ( value ) , '{ /123/gi aprop: 42 }' ) ;
285
+ }
275
286
276
287
// 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
+ }
281
294
282
295
// 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
+ }
289
304
290
305
// test positive/negative zero
291
306
assert . strictEqual ( util . inspect ( 0 ) , '0' ) ;
292
307
assert . strictEqual ( util . inspect ( - 0 ) , '-0' ) ;
293
308
294
309
// 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
+ }
304
321
305
322
// test for Array constructor in different context
306
323
{
@@ -318,98 +335,107 @@ assert.strictEqual(util.inspect(new Array(5)), '[ , , , , ]');
318
335
}
319
336
320
337
// 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
+ }
329
348
330
349
// 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
+ }
353
374
354
375
// 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
+
372
396
// 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
+ }
385
411
386
412
// GH-1941
387
413
// should not throw:
388
414
assert . strictEqual ( util . inspect ( Object . create ( Date . prototype ) ) , 'Date {}' ) ;
389
415
390
416
// GH-1944
391
- assert . doesNotThrow ( function ( ) {
417
+ assert . doesNotThrow ( ( ) => {
392
418
const d = new Date ( ) ;
393
419
d . toUTCString = null ;
394
420
util . inspect ( d ) ;
395
421
} ) ;
396
422
397
- assert . doesNotThrow ( function ( ) {
423
+ assert . doesNotThrow ( ( ) => {
398
424
const d = new Date ( ) ;
399
425
d . toISOString = null ;
400
426
util . inspect ( d ) ;
401
427
} ) ;
402
428
403
- assert . doesNotThrow ( function ( ) {
429
+ assert . doesNotThrow ( ( ) => {
404
430
const r = / r e g e x p / ;
405
431
r . toString = null ;
406
432
util . inspect ( r ) ;
407
433
} ) ;
408
434
409
435
// bug with user-supplied inspect function returns non-string
410
- assert . doesNotThrow ( function ( ) {
436
+ assert . doesNotThrow ( ( ) => {
411
437
util . inspect ( [ {
412
- inspect : function ( ) { return 123 ; }
438
+ inspect : ( ) => 123
413
439
} ] ) ;
414
440
} ) ;
415
441
@@ -420,53 +446,57 @@ assert.doesNotThrow(function() {
420
446
}
421
447
422
448
// 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
+ } ;
432
456
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' ;
441
459
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' ;
453
460
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
+ ) ;
457
468
}
458
469
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' , / r e g e x p / ) ;
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' , / r e g e x p / ) ;
496
+ }
467
497
468
498
// an object with "hasOwnProperty" overwritten should not throw
469
- assert . doesNotThrow ( function ( ) {
499
+ assert . doesNotThrow ( ( ) => {
470
500
util . inspect ( {
471
501
hasOwnProperty : null
472
502
} ) ;
@@ -509,7 +539,7 @@ assert.doesNotThrow(function() {
509
539
510
540
{
511
541
// "customInspect" option can enable/disable calling inspect() on objects
512
- const subject = { inspect : function ( ) { return 123 ; } } ;
542
+ const subject = { inspect : ( ) => 123 } ;
513
543
514
544
assert . strictEqual (
515
545
util . inspect ( subject , { customInspect : true } ) . includes ( '123' ) ,
@@ -529,11 +559,11 @@ assert.doesNotThrow(function() {
529
559
) ;
530
560
531
561
// custom inspect() functions should be able to return other Objects
532
- subject . inspect = function ( ) { return { foo : 'bar' } ; } ;
562
+ subject . inspect = ( ) => ( { foo : 'bar' } ) ;
533
563
534
564
assert . strictEqual ( util . inspect ( subject ) , '{ foo: \'bar\' }' ) ;
535
565
536
- subject . inspect = function ( depth , opts ) {
566
+ subject . inspect = ( depth , opts ) => {
537
567
assert . strictEqual ( opts . customInspectOptions , true ) ;
538
568
} ;
539
569
@@ -542,7 +572,7 @@ assert.doesNotThrow(function() {
542
572
543
573
{
544
574
// "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 } ;
546
576
547
577
assert . strictEqual (
548
578
util . inspect ( subject , { customInspect : true } ) . includes ( '123' ) ,
@@ -554,11 +584,11 @@ assert.doesNotThrow(function() {
554
584
) ;
555
585
556
586
// 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' } ) ;
558
588
559
589
assert . strictEqual ( util . inspect ( subject ) , '{ foo: \'bar\' }' ) ;
560
590
561
- subject [ util . inspect . custom ] = function ( depth , opts ) {
591
+ subject [ util . inspect . custom ] = ( depth , opts ) => {
562
592
assert . strictEqual ( opts . customInspectOptions , true ) ;
563
593
} ;
564
594
@@ -596,38 +626,32 @@ assert.doesNotThrow(function() {
596
626
'{ a: 123, inspect: [Function: inspect] }' ) ;
597
627
598
628
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 }' ) ;
601
630
}
602
631
603
632
// 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
+ }
608
640
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
+ } ) ;
612
653
}
613
654
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
-
631
655
// test boxed primitives output the correct values
632
656
assert . strictEqual ( util . inspect ( new String ( 'test' ) ) , '[String: \'test\']' ) ;
633
657
assert . strictEqual (
@@ -642,17 +666,19 @@ assert.strictEqual(util.inspect(new Number(-1.1)), '[Number: -1.1]');
642
666
assert . strictEqual ( util . inspect ( new Number ( 13.37 ) ) , '[Number: 13.37]' ) ;
643
667
644
668
// 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\' }' ) ;
648
673
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\' }' ) ;
652
677
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
+ }
656
682
657
683
// test es6 Symbol
658
684
if ( typeof Symbol !== 'undefined' ) {
@@ -682,14 +708,16 @@ if (typeof Symbol !== 'undefined') {
682
708
}
683
709
684
710
// 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
+ }
693
721
694
722
// test Map
695
723
{
@@ -703,83 +731,92 @@ assert.strictEqual(
703
731
}
704
732
705
733
// test Promise
706
- assert . strictEqual ( util . inspect ( Promise . resolve ( 3 ) ) , 'Promise { 3 }' ) ;
707
-
708
734
{
735
+ const resolved = Promise . resolve ( 3 ) ;
736
+ assert . strictEqual ( util . inspect ( resolved ) , 'Promise { 3 }' ) ;
737
+
709
738
const rejected = Promise . reject ( 3 ) ;
710
739
assert . strictEqual ( util . inspect ( rejected ) , 'Promise { <rejected> 3 }' ) ;
711
740
// squelch UnhandledPromiseRejection
712
741
rejected . catch ( ( ) => { } ) ;
713
- }
714
742
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
+ }
722
751
723
752
// Make sure it doesn't choke on polyfills. Unlike Set/Map, there is no standard
724
753
// interface to synchronously inspect a Promise, so our techniques only work on
725
754
// 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
+ }
751
787
752
788
// Test alignment of items in container
753
789
// 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
+ }
754
804
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
+ }
772
809
773
- checkAlignment ( big_array ) ;
774
- checkAlignment ( function ( ) {
775
810
const obj = { } ;
776
- big_array . forEach ( function ( v ) {
777
- obj [ v ] = null ;
811
+ bigArray . forEach ( ( prop ) => {
812
+ obj [ prop ] = null ;
778
813
} ) ;
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
+ }
783
820
784
821
785
822
// Test display of constructors
@@ -800,7 +837,7 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
800
837
'SetSubclass { 1, 2, 3 }' ) ;
801
838
assert . strictEqual ( util . inspect ( new MapSubclass ( [ [ 'foo' , 42 ] ] ) ) ,
802
839
'MapSubclass { \'foo\' => 42 }' ) ;
803
- assert . strictEqual ( util . inspect ( new PromiseSubclass ( function ( ) { } ) ) ,
840
+ assert . strictEqual ( util . inspect ( new PromiseSubclass ( ( ) => { } ) ) ,
804
841
'PromiseSubclass { <pending> }' ) ;
805
842
}
806
843
@@ -836,54 +873,58 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
836
873
// https://github.com/nodejs/node/pull/6334 is backported.
837
874
{
838
875
const x = Array ( 101 ) ;
839
- assert ( / 1 m o r e i t e m / . test ( util . inspect ( x ) ) ) ;
876
+ assert ( util . inspect ( x ) . endsWith ( '1 more item ]' ) ) ;
840
877
}
841
878
842
879
{
843
880
const x = Array ( 101 ) ;
844
- assert ( ! / 1 m o r e i t e m / . test ( util . inspect ( x , { maxArrayLength : 101 } ) ) ) ;
881
+ assert ( ! util . inspect ( x , { maxArrayLength : 101 } ) . endsWith ( '1 more item ]' ) ) ;
845
882
}
846
883
847
884
{
848
885
const x = Array ( 101 ) ;
849
- assert ( / ^ \[ ... 1 0 1 m o r e i t e m s ] $ / . test (
850
- util . inspect ( x , { maxArrayLength : 0 } ) ) ) ;
886
+ assert . strictEqual ( util . inspect ( x , { maxArrayLength : 0 } ) ,
887
+ '[ ... 101 more items ]' ) ;
851
888
}
852
889
853
890
{
854
891
const x = new Uint8Array ( 101 ) ;
855
- assert ( / 1 m o r e i t e m / . test ( util . inspect ( x ) ) ) ;
892
+ assert ( util . inspect ( x ) . endsWith ( '1 more item ]' ) ) ;
856
893
}
857
894
858
895
{
859
896
const x = new Uint8Array ( 101 ) ;
860
- assert ( ! / 1 m o r e i t e m / . test ( util . inspect ( x , { maxArrayLength : 101 } ) ) ) ;
897
+ assert ( ! util . inspect ( x , { maxArrayLength : 101 } ) . endsWith ( '1 more item ]' ) ) ;
861
898
}
862
899
863
900
{
864
901
const x = new Uint8Array ( 101 ) ;
865
- assert ( / \[ ... 1 0 1 m o r e i t e m s ] $ / . test (
866
- util . inspect ( x , { maxArrayLength : 0 } ) ) ) ;
902
+ assert . strictEqual ( util . inspect ( x , { maxArrayLength : 0 } ) ,
903
+ 'Uint8Array [ ... 101 more items ]' ) ;
867
904
}
868
905
869
906
{
870
907
const x = Array ( 101 ) ;
871
- assert ( ! / 1 m o r e i t e m / . test ( util . inspect ( x , { maxArrayLength : null } ) ) ) ;
908
+ assert ( ! util . inspect ( x , { maxArrayLength : null } ) . endsWith ( '1 more item ]' ) ) ;
872
909
}
873
910
874
911
{
875
912
const x = Array ( 101 ) ;
876
- assert ( ! / 1 m o r e i t e m / . test ( util . inspect ( x , { maxArrayLength : Infinity } ) ) ) ;
913
+ assert ( ! util . inspect (
914
+ x , { maxArrayLength : Infinity }
915
+ ) . endsWith ( '1 more item ]' ) ) ;
877
916
}
878
917
879
918
{
880
919
const x = new Uint8Array ( 101 ) ;
881
- assert ( ! / 1 m o r e i t e m / . test ( util . inspect ( x , { maxArrayLength : null } ) ) ) ;
920
+ assert ( ! util . inspect ( x , { maxArrayLength : null } ) . endsWith ( '1 more item ]' ) ) ;
882
921
}
883
922
884
923
{
885
924
const x = new Uint8Array ( 101 ) ;
886
- assert ( ! / 1 m o r e i t e m / . test ( util . inspect ( x , { maxArrayLength : Infinity } ) ) ) ;
925
+ assert ( ! util . inspect (
926
+ x , { maxArrayLength : Infinity }
927
+ ) . endsWith ( '1 more item ]' ) ) ;
887
928
}
888
929
889
930
{
0 commit comments