@@ -22,14 +22,18 @@ assert.doesNotThrow(function() {
22
22
} ) ;
23
23
24
24
// an Object with a custom .inspect() function
25
- var custom_inspect = { foo : 'bar' , inspect : function ( ) { return 'inspect' ; } } ;
25
+ const custom_inspect = { foo : 'bar' , inspect : ( ) => { return 'inspect' ; } } ;
26
26
27
- var stdout_write = global . process . stdout . write ;
28
- var strings = [ ] ;
27
+ const stdout_write = global . process . stdout . write ;
28
+ const stderr_write = global . process . stderr . write ;
29
+ const strings = [ ] ;
30
+ const errStrings = [ ] ;
29
31
global . process . stdout . write = function ( string ) {
30
32
strings . push ( string ) ;
31
33
} ;
32
- console . _stderr = process . stdout ;
34
+ global . process . stderr . write = function ( string ) {
35
+ errStrings . push ( string ) ;
36
+ } ;
33
37
34
38
// test console.log()
35
39
console . log ( 'foo' ) ;
@@ -38,6 +42,27 @@ console.log('%s %s', 'foo', 'bar', 'hop');
38
42
console . log ( { slashes : '\\\\' } ) ;
39
43
console . log ( custom_inspect ) ;
40
44
45
+ // test console.info()
46
+ console . info ( 'foo' ) ;
47
+ console . info ( 'foo' , 'bar' ) ;
48
+ console . info ( '%s %s' , 'foo' , 'bar' , 'hop' ) ;
49
+ console . info ( { slashes : '\\\\' } ) ;
50
+ console . info ( custom_inspect ) ;
51
+
52
+ // test console.error()
53
+ console . error ( 'foo' ) ;
54
+ console . error ( 'foo' , 'bar' ) ;
55
+ console . error ( '%s %s' , 'foo' , 'bar' , 'hop' ) ;
56
+ console . error ( { slashes : '\\\\' } ) ;
57
+ console . error ( custom_inspect ) ;
58
+
59
+ // test console.warn()
60
+ console . warn ( 'foo' ) ;
61
+ console . warn ( 'foo' , 'bar' ) ;
62
+ console . warn ( '%s %s' , 'foo' , 'bar' , 'hop' ) ;
63
+ console . warn ( { slashes : '\\\\' } ) ;
64
+ console . warn ( custom_inspect ) ;
65
+
41
66
// test console.dir()
42
67
console . dir ( custom_inspect ) ;
43
68
console . dir ( custom_inspect , { showHidden : false } ) ;
@@ -60,6 +85,7 @@ console.time('hasOwnProperty');
60
85
console . timeEnd ( 'hasOwnProperty' ) ;
61
86
62
87
global . process . stdout . write = stdout_write ;
88
+ global . process . stderr . write = stderr_write ;
63
89
64
90
// verify that console.timeEnd() doesn't leave dead links
65
91
const timesMapSize = console . _times . size ;
@@ -71,22 +97,34 @@ console.timeEnd('label2');
71
97
console . timeEnd ( 'label3' ) ;
72
98
assert . strictEqual ( console . _times . size , timesMapSize ) ;
73
99
74
- assert . equal ( 'foo\n' , strings . shift ( ) ) ;
75
- assert . equal ( 'foo bar\n' , strings . shift ( ) ) ;
76
- assert . equal ( 'foo bar hop\n' , strings . shift ( ) ) ;
77
- assert . equal ( "{ slashes: '\\\\\\\\' }\n" , strings . shift ( ) ) ;
78
- assert . equal ( 'inspect\n' , strings . shift ( ) ) ;
100
+ const expectedStrings = [
101
+ 'foo' , 'foo bar' , 'foo bar hop' , "{ slashes: '\\\\\\\\' }" , 'inspect'
102
+ ] ;
103
+
104
+ for ( const expected of expectedStrings ) {
105
+ assert . equal ( expected + '\n' , strings . shift ( ) ) ; // console.log (stdout)
106
+ assert . equal ( expected + '\n' , errStrings . shift ( ) ) ; // console.error (stderr)
107
+ }
108
+
109
+ for ( const expected of expectedStrings ) {
110
+ assert . equal ( expected + '\n' , strings . shift ( ) ) ; // console.info (stdout)
111
+ assert . equal ( expected + '\n' , errStrings . shift ( ) ) ; // console.warn (stderr)
112
+ }
113
+
79
114
assert . equal ( "{ foo: 'bar', inspect: [Function] }\n" , strings . shift ( ) ) ;
80
115
assert . equal ( "{ foo: 'bar', inspect: [Function] }\n" , strings . shift ( ) ) ;
81
116
assert . notEqual ( - 1 , strings . shift ( ) . indexOf ( 'foo: [Object]' ) ) ;
82
117
assert . equal ( - 1 , strings . shift ( ) . indexOf ( 'baz' ) ) ;
83
- assert . equal ( 'Trace: This is a {"formatted":"trace"} 10 foo' ,
84
- strings . shift ( ) . split ( '\n' ) . shift ( ) ) ;
85
118
assert . ok ( / ^ l a b e l : \d + \. \d { 3 } m s $ / . test ( strings . shift ( ) . trim ( ) ) ) ;
86
119
assert . ok ( / ^ _ _ p r o t o _ _ : \d + \. \d { 3 } m s $ / . test ( strings . shift ( ) . trim ( ) ) ) ;
87
120
assert . ok ( / ^ c o n s t r u c t o r : \d + \. \d { 3 } m s $ / . test ( strings . shift ( ) . trim ( ) ) ) ;
88
121
assert . ok ( / ^ h a s O w n P r o p e r t y : \d + \. \d { 3 } m s $ / . test ( strings . shift ( ) . trim ( ) ) ) ;
122
+
123
+ assert . equal ( 'Trace: This is a {"formatted":"trace"} 10 foo' ,
124
+ errStrings . shift ( ) . split ( '\n' ) . shift ( ) ) ;
125
+
89
126
assert . equal ( strings . length , 0 ) ;
127
+ assert . equal ( errStrings . length , 0 ) ;
90
128
91
129
assert . throws ( ( ) => {
92
130
console . assert ( false , 'should throw' ) ;
0 commit comments