-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
223 lines (160 loc) · 7.35 KB
/
test.js
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
"use strict";
const assert = require ('assert'),
asTable = require (process.env.AS_TABLE_TEST_FILE),
ansi = require ('ansicolor').nice
describe ('as-table', () => {
it ('array printing works', () => {
var testData = [['qwe', '123456789', 'zxcvbnm'],
['qwerty', '12', 'zxcvb'],
['💩wertyiop', '1234567', 'z']]
assert.equal (asTable (testData),
'qwe 123456789 zxcvbnm\n' +
'qwerty 12 zxcvb \n' +
'💩wertyiop 1234567 z ')
assert.equal (asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (testData),
'qwe | 1234… | zxc…\n' +
'qwer… | 12 | zxc…\n' +
'💩wer… | 1234… | z ')
console.log (asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (testData))
})
it ('object printing works', () => {
var testData =
[ { foo: true, string: 'abcde', num: 42 },
{ foo: false, string: 'qwertyuiop', num: 43 },
{ string: null, num: 44 } ]
assert.equal (asTable (testData),
'foo string num\n' +
'----------------------\n' +
'true abcde 42 \n' +
'false qwertyuiop 43 \n' +
' null 44 ')
})
it ('object printing works (with ANSI styling)', () => {
var testData =
[ { foo: true, string: 'abcde'.cyan.bgYellow, num: 42 },
{ foo: false, string: 'qwertyuiop', num: 43 },
{ string: null, num: 44 } ]
assert.equal (asTable (testData),
'foo string num\n' +
'----------------------\n' +
'true \u001b[43m\u001b[36mabcde\u001b[39m\u001b[49m 42 \n' +
'false qwertyuiop 43 \n' +
' null 44 ')
})
it ('maxTotalWidth correctly handles object field names', () => {
assert.equal (
asTable.configure ({ maxTotalWidth: 15 }) ([{
'0123456789': '0123456789',
'abcdefxyzw': 'abcdefxyzw' }]),
'01234… abcde…\n' +
'--------------\n' +
'01234… abcde…'
)
})
it ('maxTotalWidth correctly handles object field names (with ANSI styling)', () => {
assert.equal (
asTable.configure ({ maxTotalWidth: 15 }) ([{
'0123456789': '0123456789',
'abcdefxyzw': 'abcdefxyzw'.cyan.bgYellow.italic.inverse.bright }]),
'01234… abcde…\n' +
'--------------\n' +
'01234… ' + 'abcde'.cyan.bgYellow.italic.inverse.bright + '…'
)
})
it ('everything renders as singleline', () => {
assert.equal (asTable ([['fooo\n\nbar']]), 'fooo\\n\\nbar')
})
it ('configuring works', () => {
const asTable25 = asTable.configure ({ maxTotalWidth: 25 }),
asTable25Delim = asTable25.configure ({ delimiter: ' | ' })
assert.notEqual (asTable25, asTable25Delim)
assert.equal (asTable25.maxTotalWidth, 25)
assert.equal (asTable25Delim.delimiter, ' | ')
})
it ('degenerate case works', () => {
assert.equal (asTable ([]), '\n')
assert.equal (asTable ([{}]), '\n\n')
})
it ('null/undefined prints correctly', () => {
assert.equal (asTable.configure ({ delimiter: '|' }) ([[null, undefined, 1, 2, 3]]), 'null||1|2|3')
})
it ('custom printer works', () => {
var testData =
[ { foo: true, string: 'abcde', num: 42 },
{ foo: false, string: 'qwertyuiop', num: 43 },
{ string: null, num: 44 } ]
const formatsBooleansAsYesNo = asTable.configure ({ print: obj => (typeof obj === 'boolean') ? (obj ? 'yes' : 'no') : String (obj) })
assert.equal (formatsBooleansAsYesNo (testData),
'foo string num\n' +
'--------------------\n' +
'yes abcde 42 \n' +
'no qwertyuiop 43 \n' +
' null 44 ')
})
it ('custom printer works with object titles', () => {
var testData =
[ { foo: true, string: 'abcde', num: 42, timestamp: 1561202591572 },
{ foo: false, string: 'qwertyuiop', num: 43, timestamp: 1558524240034 },
{ string: null, num: 44, timestamp: 1555932240034 } ]
const formats = asTable.configure ({
print: (obj, title) => {
if (title === 'foo') {
return obj ? 'yes' : 'no';
}
if (title === 'timestamp') {
return new Date(obj).toGMTString();
}
return String(obj);
}
})
assert.equal (formats (testData),
'foo string num timestamp \n' +
'---------------------------------------------------\n' +
'yes abcde 42 Sat, 22 Jun 2019 11:23:11 GMT\n' +
'no qwertyuiop 43 Wed, 22 May 2019 11:24:00 GMT\n' +
' null 44 Mon, 22 Apr 2019 11:24:00 GMT')
})
it ('custom printer works with array keys', () => {
var testData =
[ [ true, 'abcde', 42, 1561202591572 ],
[ false, 'qwertyuiop', 43, 1558524240034 ] ]
const formats = asTable.configure ({
print: (obj, index) => {
if (index === 0) {
return obj ? 'yes' : 'no';
}
if (index === 3) {
return new Date(obj).toGMTString();
}
return String(obj);
}
})
assert.equal (formats (testData),
'yes abcde 42 Sat, 22 Jun 2019 11:23:11 GMT\n' +
'no qwertyuiop 43 Wed, 22 May 2019 11:24:00 GMT')
})
it ('right align works', () => {
var testData =
[ { foo: 1234.567, bar: 12 },
{ foo: '4.567'.bgMagenta.green, bar: 1234.456890 } ]
assert.equal (asTable.configure ({ right: true }) (testData),
' foo bar\n' +
'--------------------\n' +
'1234.567 12\n' +
' ' + '4.567'.bgMagenta.green + ' 1234.45689')
})
it ('ANSI coloring works', () => {
const testData =
[ { foo: true, string: 'abcde', num: 42 },
{ foo: false, string: '💩wertyuiop'.bgMagenta.green.bright, num: 43 } ]
const d = ' | '.dim.cyan
const _ = '-'.bright.cyan
const result = asTable.configure ({ title: x => x.bright, delimiter: d, dash: _ }) (testData)
console.log (result)
assert.equal (result,
['foo'.bright + ' ', 'string'.bright + ' ', 'num'.bright].join (d) + '\n' +
_.repeat (24) + '\n' +
['true ', 'abcde ', '42 '].join (d) + '\n' +
['false', '💩wertyuiop'.bgMagenta.green.bright, '43 '].join (d))
})
})