indent-option.js
6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var test = require('tape');
var forEach = require('for-each');
var inspect = require('../');
test('bad indent options', function (t) {
forEach([
undefined,
true,
false,
-1,
1.2,
Infinity,
-Infinity,
NaN
], function (indent) {
t['throws'](
function () { inspect('', { indent: indent }); },
TypeError,
inspect(indent) + ' is invalid'
);
});
t.end();
});
test('simple object with indent', function (t) {
t.plan(2);
var obj = { a: 1, b: 2 };
var expectedSpaces = [
'{',
' a: 1,',
' b: 2',
'}'
].join('\n');
var expectedTabs = [
'{',
' a: 1,',
' b: 2',
'}'
].join('\n');
t.equal(inspect(obj, { indent: 2 }), expectedSpaces, 'two');
t.equal(inspect(obj, { indent: '\t' }), expectedTabs, 'tabs');
});
test('two deep object with indent', function (t) {
t.plan(2);
var obj = { a: 1, b: { c: 3, d: 4 } };
var expectedSpaces = [
'{',
' a: 1,',
' b: {',
' c: 3,',
' d: 4',
' }',
'}'
].join('\n');
var expectedTabs = [
'{',
' a: 1,',
' b: {',
' c: 3,',
' d: 4',
' }',
'}'
].join('\n');
t.equal(inspect(obj, { indent: 2 }), expectedSpaces, 'two');
t.equal(inspect(obj, { indent: '\t' }), expectedTabs, 'tabs');
});
test('simple array with all single line elements', function (t) {
t.plan(2);
var obj = [1, 2, 3, 'asdf\nsdf'];
var expected = '[ 1, 2, 3, \'asdf\\nsdf\' ]';
t.equal(inspect(obj, { indent: 2 }), expected, 'two');
t.equal(inspect(obj, { indent: '\t' }), expected, 'tabs');
});
test('array with complex elements', function (t) {
t.plan(2);
var obj = [1, { a: 1, b: { c: 1 } }, 'asdf\nsdf'];
var expectedSpaces = [
'[',
' 1,',
' {',
' a: 1,',
' b: {',
' c: 1',
' }',
' },',
' \'asdf\\nsdf\'',
']'
].join('\n');
var expectedTabs = [
'[',
' 1,',
' {',
' a: 1,',
' b: {',
' c: 1',
' }',
' },',
' \'asdf\\nsdf\'',
']'
].join('\n');
t.equal(inspect(obj, { indent: 2 }), expectedSpaces, 'two');
t.equal(inspect(obj, { indent: '\t' }), expectedTabs, 'tabs');
});
test('values', function (t) {
t.plan(2);
var obj = [{}, [], { 'a-b': 5 }];
var expectedSpaces = [
'[',
' {},',
' [],',
' {',
' \'a-b\': 5',
' }',
']'
].join('\n');
var expectedTabs = [
'[',
' {},',
' [],',
' {',
' \'a-b\': 5',
' }',
']'
].join('\n');
t.equal(inspect(obj, { indent: 2 }), expectedSpaces, 'two');
t.equal(inspect(obj, { indent: '\t' }), expectedTabs, 'tabs');
});
test('Map', { skip: typeof Map !== 'function' }, function (t) {
var map = new Map();
map.set({ a: 1 }, ['b']);
map.set(3, NaN);
var expectedStringSpaces = [
'Map (2) {',
' { a: 1 } => [ \'b\' ],',
' 3 => NaN',
'}'
].join('\n');
var expectedStringTabs = [
'Map (2) {',
' { a: 1 } => [ \'b\' ],',
' 3 => NaN',
'}'
].join('\n');
var expectedStringTabsDoubleQuotes = [
'Map (2) {',
' { a: 1 } => [ "b" ],',
' 3 => NaN',
'}'
].join('\n');
t.equal(
inspect(map, { indent: 2 }),
expectedStringSpaces,
'Map keys are not indented (two)'
);
t.equal(
inspect(map, { indent: '\t' }),
expectedStringTabs,
'Map keys are not indented (tabs)'
);
t.equal(
inspect(map, { indent: '\t', quoteStyle: 'double' }),
expectedStringTabsDoubleQuotes,
'Map keys are not indented (tabs + double quotes)'
);
t.equal(inspect(new Map(), { indent: 2 }), 'Map (0) {}', 'empty Map should show as empty (two)');
t.equal(inspect(new Map(), { indent: '\t' }), 'Map (0) {}', 'empty Map should show as empty (tabs)');
var nestedMap = new Map();
nestedMap.set(nestedMap, map);
var expectedNestedSpaces = [
'Map (1) {',
' [Circular] => Map (2) {',
' { a: 1 } => [ \'b\' ],',
' 3 => NaN',
' }',
'}'
].join('\n');
var expectedNestedTabs = [
'Map (1) {',
' [Circular] => Map (2) {',
' { a: 1 } => [ \'b\' ],',
' 3 => NaN',
' }',
'}'
].join('\n');
t.equal(inspect(nestedMap, { indent: 2 }), expectedNestedSpaces, 'Map containing a Map should work (two)');
t.equal(inspect(nestedMap, { indent: '\t' }), expectedNestedTabs, 'Map containing a Map should work (tabs)');
t.end();
});
test('Set', { skip: typeof Set !== 'function' }, function (t) {
var set = new Set();
set.add({ a: 1 });
set.add(['b']);
var expectedStringSpaces = [
'Set (2) {',
' {',
' a: 1',
' },',
' [ \'b\' ]',
'}'
].join('\n');
var expectedStringTabs = [
'Set (2) {',
' {',
' a: 1',
' },',
' [ \'b\' ]',
'}'
].join('\n');
t.equal(inspect(set, { indent: 2 }), expectedStringSpaces, 'new Set([{ a: 1 }, ["b"]]) should show size and contents (two)');
t.equal(inspect(set, { indent: '\t' }), expectedStringTabs, 'new Set([{ a: 1 }, ["b"]]) should show size and contents (tabs)');
t.equal(inspect(new Set(), { indent: 2 }), 'Set (0) {}', 'empty Set should show as empty (two)');
t.equal(inspect(new Set(), { indent: '\t' }), 'Set (0) {}', 'empty Set should show as empty (tabs)');
var nestedSet = new Set();
nestedSet.add(set);
nestedSet.add(nestedSet);
var expectedNestedSpaces = [
'Set (2) {',
' Set (2) {',
' {',
' a: 1',
' },',
' [ \'b\' ]',
' },',
' [Circular]',
'}'
].join('\n');
var expectedNestedTabs = [
'Set (2) {',
' Set (2) {',
' {',
' a: 1',
' },',
' [ \'b\' ]',
' },',
' [Circular]',
'}'
].join('\n');
t.equal(inspect(nestedSet, { indent: 2 }), expectedNestedSpaces, 'Set containing a Set should work (two)');
t.equal(inspect(nestedSet, { indent: '\t' }), expectedNestedTabs, 'Set containing a Set should work (tabs)');
t.end();
});