1
|
// Copyright Joyent, Inc. and other Node contributors.
|
2
|
//
|
3
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
4
|
// copy of this software and associated documentation files (the
|
5
|
// "Software"), to deal in the Software without restriction, including
|
6
|
// without limitation the rights to use, copy, modify, merge, publish,
|
7
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
8
|
// persons to whom the Software is furnished to do so, subject to the
|
9
|
// following conditions:
|
10
|
//
|
11
|
// The above copyright notice and this permission notice shall be included
|
12
|
// in all copies or substantial portions of the Software.
|
13
|
//
|
14
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
15
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
17
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
18
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
19
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
20
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
|
22
|
|
23
|
|
24
|
|
25
|
var assert = require('assert');
|
26
|
var util = require('../../');
|
27
|
|
28
|
// test the internal isDate implementation
|
29
|
var Date2 = require('vm').runInNewContext('Date');
|
30
|
var d = new Date2();
|
31
|
var orig = util.inspect(d);
|
32
|
Date2.prototype.foo = 'bar';
|
33
|
var after = util.inspect(d);
|
34
|
assert.equal(orig, after);
|
35
|
|
36
|
// test for sparse array
|
37
|
var a = ['foo', 'bar', 'baz'];
|
38
|
assert.equal(util.inspect(a), '[ \'foo\', \'bar\', \'baz\' ]');
|
39
|
delete a[1];
|
40
|
assert.equal(util.inspect(a), '[ \'foo\', , \'baz\' ]');
|
41
|
assert.equal(util.inspect(a, true), '[ \'foo\', , \'baz\', [length]: 3 ]');
|
42
|
assert.equal(util.inspect(new Array(5)), '[ , , , , ]');
|
43
|
|
44
|
// test for property descriptors
|
45
|
var getter = Object.create(null, {
|
46
|
a: {
|
47
|
get: function() { return 'aaa'; }
|
48
|
}
|
49
|
});
|
50
|
var setter = Object.create(null, {
|
51
|
b: {
|
52
|
set: function() {}
|
53
|
}
|
54
|
});
|
55
|
var getterAndSetter = Object.create(null, {
|
56
|
c: {
|
57
|
get: function() { return 'ccc'; },
|
58
|
set: function() {}
|
59
|
}
|
60
|
});
|
61
|
assert.equal(util.inspect(getter, true), '{ [a]: [Getter] }');
|
62
|
assert.equal(util.inspect(setter, true), '{ [b]: [Setter] }');
|
63
|
assert.equal(util.inspect(getterAndSetter, true), '{ [c]: [Getter/Setter] }');
|
64
|
|
65
|
// exceptions should print the error message, not '{}'
|
66
|
assert.equal(util.inspect(new Error()), '[Error]');
|
67
|
assert.equal(util.inspect(new Error('FAIL')), '[Error: FAIL]');
|
68
|
assert.equal(util.inspect(new TypeError('FAIL')), '[TypeError: FAIL]');
|
69
|
assert.equal(util.inspect(new SyntaxError('FAIL')), '[SyntaxError: FAIL]');
|
70
|
try {
|
71
|
undef();
|
72
|
} catch (e) {
|
73
|
assert.equal(util.inspect(e), '[ReferenceError: undef is not defined]');
|
74
|
}
|
75
|
var ex = util.inspect(new Error('FAIL'), true);
|
76
|
assert.ok(ex.indexOf('[Error: FAIL]') != -1);
|
77
|
assert.ok(ex.indexOf('[stack]') != -1);
|
78
|
assert.ok(ex.indexOf('[message]') != -1);
|
79
|
|
80
|
// GH-1941
|
81
|
// should not throw:
|
82
|
assert.equal(util.inspect(Object.create(Date.prototype)), '{}');
|
83
|
|
84
|
// GH-1944
|
85
|
assert.doesNotThrow(function() {
|
86
|
var d = new Date();
|
87
|
d.toUTCString = null;
|
88
|
util.inspect(d);
|
89
|
});
|
90
|
|
91
|
assert.doesNotThrow(function() {
|
92
|
var r = /regexp/;
|
93
|
r.toString = null;
|
94
|
util.inspect(r);
|
95
|
});
|
96
|
|
97
|
// bug with user-supplied inspect function returns non-string
|
98
|
assert.doesNotThrow(function() {
|
99
|
util.inspect([{
|
100
|
inspect: function() { return 123; }
|
101
|
}]);
|
102
|
});
|
103
|
|
104
|
// GH-2225
|
105
|
var x = { inspect: util.inspect };
|
106
|
assert.ok(util.inspect(x).indexOf('inspect') != -1);
|
107
|
|
108
|
// util.inspect.styles and util.inspect.colors
|
109
|
function test_color_style(style, input, implicit) {
|
110
|
var color_name = util.inspect.styles[style];
|
111
|
var color = ['', ''];
|
112
|
if(util.inspect.colors[color_name])
|
113
|
color = util.inspect.colors[color_name];
|
114
|
|
115
|
var without_color = util.inspect(input, false, 0, false);
|
116
|
var with_color = util.inspect(input, false, 0, true);
|
117
|
var expect = '\u001b[' + color[0] + 'm' + without_color +
|
118
|
'\u001b[' + color[1] + 'm';
|
119
|
assert.equal(with_color, expect, 'util.inspect color for style '+style);
|
120
|
}
|
121
|
|
122
|
test_color_style('special', function(){});
|
123
|
test_color_style('number', 123.456);
|
124
|
test_color_style('boolean', true);
|
125
|
test_color_style('undefined', undefined);
|
126
|
test_color_style('null', null);
|
127
|
test_color_style('string', 'test string');
|
128
|
test_color_style('date', new Date);
|
129
|
test_color_style('regexp', /regexp/);
|
130
|
|
131
|
// an object with "hasOwnProperty" overwritten should not throw
|
132
|
assert.doesNotThrow(function() {
|
133
|
util.inspect({
|
134
|
hasOwnProperty: null
|
135
|
});
|
136
|
});
|
137
|
|
138
|
// new API, accepts an "options" object
|
139
|
var subject = { foo: 'bar', hello: 31, a: { b: { c: { d: 0 } } } };
|
140
|
Object.defineProperty(subject, 'hidden', { enumerable: false, value: null });
|
141
|
|
142
|
assert(util.inspect(subject, { showHidden: false }).indexOf('hidden') === -1);
|
143
|
assert(util.inspect(subject, { showHidden: true }).indexOf('hidden') !== -1);
|
144
|
assert(util.inspect(subject, { colors: false }).indexOf('\u001b[32m') === -1);
|
145
|
assert(util.inspect(subject, { colors: true }).indexOf('\u001b[32m') !== -1);
|
146
|
assert(util.inspect(subject, { depth: 2 }).indexOf('c: [Object]') !== -1);
|
147
|
assert(util.inspect(subject, { depth: 0 }).indexOf('a: [Object]') !== -1);
|
148
|
assert(util.inspect(subject, { depth: null }).indexOf('{ d: 0 }') !== -1);
|
149
|
|
150
|
// "customInspect" option can enable/disable calling inspect() on objects
|
151
|
subject = { inspect: function() { return 123; } };
|
152
|
|
153
|
assert(util.inspect(subject, { customInspect: true }).indexOf('123') !== -1);
|
154
|
assert(util.inspect(subject, { customInspect: true }).indexOf('inspect') === -1);
|
155
|
assert(util.inspect(subject, { customInspect: false }).indexOf('123') === -1);
|
156
|
assert(util.inspect(subject, { customInspect: false }).indexOf('inspect') !== -1);
|
157
|
|
158
|
// custom inspect() functions should be able to return other Objects
|
159
|
subject.inspect = function() { return { foo: 'bar' }; };
|
160
|
|
161
|
assert.equal(util.inspect(subject), '{ foo: \'bar\' }');
|
162
|
|
163
|
subject.inspect = function(depth, opts) {
|
164
|
assert.strictEqual(opts.customInspectOptions, true);
|
165
|
};
|
166
|
|
167
|
util.inspect(subject, { customInspectOptions: true });
|
168
|
|
169
|
// util.inspect with "colors" option should produce as many lines as without it
|
170
|
function test_lines(input) {
|
171
|
var count_lines = function(str) {
|
172
|
return (str.match(/\n/g) || []).length;
|
173
|
}
|
174
|
|
175
|
var without_color = util.inspect(input);
|
176
|
var with_color = util.inspect(input, {colors: true});
|
177
|
assert.equal(count_lines(without_color), count_lines(with_color));
|
178
|
}
|
179
|
|
180
|
test_lines([1, 2, 3, 4, 5, 6, 7]);
|
181
|
test_lines(function() {
|
182
|
var big_array = [];
|
183
|
for (var i = 0; i < 100; i++) {
|
184
|
big_array.push(i);
|
185
|
}
|
186
|
return big_array;
|
187
|
}());
|
188
|
test_lines({foo: 'bar', baz: 35, b: {a: 35}});
|
189
|
test_lines({
|
190
|
foo: 'bar',
|
191
|
baz: 35,
|
192
|
b: {a: 35},
|
193
|
very_long_key: 'very_long_value',
|
194
|
even_longer_key: ['with even longer value in array']
|
195
|
});
|