1 | "use strict";
|
2 |
|
3 | const assert = require ('assert'),
|
4 | asTable = require (process.env.AS_TABLE_TEST_FILE),
|
5 | ansi = require ('ansicolor').nice
|
6 |
|
7 | describe ('as-table', () => {
|
8 |
|
9 | it ('array printing works', () => {
|
10 |
|
11 | var testData = [['qwe', '123456789', 'zxcvbnm'],
|
12 | ['qwerty', '12', 'zxcvb'],
|
13 | ['💩wertyiop', '1234567', 'z']]
|
14 |
|
15 | assert.equal (asTable (testData),
|
16 |
|
17 | 'qwe 123456789 zxcvbnm\n' +
|
18 | 'qwerty 12 zxcvb \n' +
|
19 | '💩wertyiop 1234567 z ')
|
20 |
|
21 | assert.equal (asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (testData),
|
22 |
|
23 | 'qwe | 1234… | zxc…\n' +
|
24 | 'qwer… | 12 | zxc…\n' +
|
25 | '💩wer… | 1234… | z ')
|
26 |
|
27 | console.log (asTable.configure ({ maxTotalWidth: 22, delimiter: ' | ' }) (testData))
|
28 | })
|
29 |
|
30 | it ('object printing works', () => {
|
31 |
|
32 | var testData =
|
33 | [ { foo: true, string: 'abcde', num: 42 },
|
34 | { foo: false, string: 'qwertyuiop', num: 43 },
|
35 | { string: null, num: 44 } ]
|
36 |
|
37 | assert.equal (asTable (testData),
|
38 |
|
39 | 'foo string num\n' +
|
40 | '----------------------\n' +
|
41 | 'true abcde 42 \n' +
|
42 | 'false qwertyuiop 43 \n' +
|
43 | ' null 44 ')
|
44 | })
|
45 |
|
46 |
|
47 | it ('object printing works (with ANSI styling)', () => {
|
48 |
|
49 | var testData =
|
50 | [ { foo: true, string: 'abcde'.cyan.bgYellow, num: 42 },
|
51 | { foo: false, string: 'qwertyuiop', num: 43 },
|
52 | { string: null, num: 44 } ]
|
53 |
|
54 | assert.equal (asTable (testData),
|
55 |
|
56 | 'foo string num\n' +
|
57 | '----------------------\n' +
|
58 | 'true \u001b[43m\u001b[36mabcde\u001b[39m\u001b[49m 42 \n' +
|
59 | 'false qwertyuiop 43 \n' +
|
60 | ' null 44 ')
|
61 | })
|
62 |
|
63 | it ('maxTotalWidth correctly handles object field names', () => {
|
64 |
|
65 | assert.equal (
|
66 |
|
67 | asTable.configure ({ maxTotalWidth: 15 }) ([{
|
68 |
|
69 | '0123456789': '0123456789',
|
70 | 'abcdefxyzw': 'abcdefxyzw' }]),
|
71 |
|
72 | '01234… abcde…\n' +
|
73 | '--------------\n' +
|
74 | '01234… abcde…'
|
75 | )
|
76 | })
|
77 |
|
78 | it ('maxTotalWidth correctly handles object field names (with ANSI styling)', () => {
|
79 |
|
80 | assert.equal (
|
81 |
|
82 | asTable.configure ({ maxTotalWidth: 15 }) ([{
|
83 |
|
84 | '0123456789': '0123456789',
|
85 | 'abcdefxyzw': 'abcdefxyzw'.cyan.bgYellow.italic.inverse.bright }]),
|
86 |
|
87 | '01234… abcde…\n' +
|
88 | '--------------\n' +
|
89 | '01234… ' + 'abcde'.cyan.bgYellow.italic.inverse.bright + '…'
|
90 | )
|
91 | })
|
92 |
|
93 | it ('everything renders as singleline', () => {
|
94 |
|
95 | assert.equal (asTable ([['fooo\n\nbar']]), 'fooo\\n\\nbar')
|
96 | })
|
97 |
|
98 | it ('configuring works', () => {
|
99 |
|
100 | const asTable25 = asTable.configure ({ maxTotalWidth: 25 }),
|
101 | asTable25Delim = asTable25.configure ({ delimiter: ' | ' })
|
102 |
|
103 | assert.notEqual (asTable25, asTable25Delim)
|
104 | assert.equal (asTable25.maxTotalWidth, 25)
|
105 | assert.equal (asTable25Delim.delimiter, ' | ')
|
106 | })
|
107 |
|
108 | it ('degenerate case works', () => {
|
109 |
|
110 | assert.equal (asTable ([]), '\n')
|
111 | assert.equal (asTable ([{}]), '\n\n')
|
112 | })
|
113 |
|
114 | it ('null/undefined prints correctly', () => {
|
115 |
|
116 | assert.equal (asTable.configure ({ delimiter: '|' }) ([[null, undefined, 1, 2, 3]]), 'null||1|2|3')
|
117 | })
|
118 |
|
119 | it ('custom printer works', () => {
|
120 |
|
121 | var testData =
|
122 | [ { foo: true, string: 'abcde', num: 42 },
|
123 | { foo: false, string: 'qwertyuiop', num: 43 },
|
124 | { string: null, num: 44 } ]
|
125 |
|
126 | const formatsBooleansAsYesNo = asTable.configure ({ print: obj => (typeof obj === 'boolean') ? (obj ? 'yes' : 'no') : String (obj) })
|
127 |
|
128 | assert.equal (formatsBooleansAsYesNo (testData),
|
129 |
|
130 | 'foo string num\n' +
|
131 | '--------------------\n' +
|
132 | 'yes abcde 42 \n' +
|
133 | 'no qwertyuiop 43 \n' +
|
134 | ' null 44 ')
|
135 | })
|
136 |
|
137 | it ('custom printer works with object titles', () => {
|
138 |
|
139 | var testData =
|
140 | [ { foo: true, string: 'abcde', num: 42, timestamp: 1561202591572 },
|
141 | { foo: false, string: 'qwertyuiop', num: 43, timestamp: 1558524240034 },
|
142 | { string: null, num: 44, timestamp: 1555932240034 } ]
|
143 |
|
144 | const formats = asTable.configure ({
|
145 | print: (obj, title) => {
|
146 | if (title === 'foo') {
|
147 | return obj ? 'yes' : 'no';
|
148 | }
|
149 | if (title === 'timestamp') {
|
150 | return new Date(obj).toGMTString();
|
151 | }
|
152 | return String(obj);
|
153 | }
|
154 | })
|
155 |
|
156 | assert.equal (formats (testData),
|
157 |
|
158 | 'foo string num timestamp \n' +
|
159 | '---------------------------------------------------\n' +
|
160 | 'yes abcde 42 Sat, 22 Jun 2019 11:23:11 GMT\n' +
|
161 | 'no qwertyuiop 43 Wed, 22 May 2019 11:24:00 GMT\n' +
|
162 | ' null 44 Mon, 22 Apr 2019 11:24:00 GMT')
|
163 | })
|
164 |
|
165 | it ('custom printer works with array keys', () => {
|
166 |
|
167 | var testData =
|
168 | [ [ true, 'abcde', 42, 1561202591572 ],
|
169 | [ false, 'qwertyuiop', 43, 1558524240034 ] ]
|
170 |
|
171 | const formats = asTable.configure ({
|
172 | print: (obj, index) => {
|
173 | if (index === 0) {
|
174 | return obj ? 'yes' : 'no';
|
175 | }
|
176 | if (index === 3) {
|
177 | return new Date(obj).toGMTString();
|
178 | }
|
179 | return String(obj);
|
180 | }
|
181 | })
|
182 |
|
183 | assert.equal (formats (testData),
|
184 |
|
185 | 'yes abcde 42 Sat, 22 Jun 2019 11:23:11 GMT\n' +
|
186 | 'no qwertyuiop 43 Wed, 22 May 2019 11:24:00 GMT')
|
187 | })
|
188 |
|
189 |
|
190 | it ('right align works', () => {
|
191 |
|
192 | var testData =
|
193 | [ { foo: 1234.567, bar: 12 },
|
194 | { foo: '4.567'.bgMagenta.green, bar: 1234.456890 } ]
|
195 |
|
196 | assert.equal (asTable.configure ({ right: true }) (testData),
|
197 | ' foo bar\n' +
|
198 | '--------------------\n' +
|
199 | '1234.567 12\n' +
|
200 | ' ' + '4.567'.bgMagenta.green + ' 1234.45689')
|
201 | })
|
202 |
|
203 | it ('ANSI coloring works', () => {
|
204 |
|
205 | const testData =
|
206 | [ { foo: true, string: 'abcde', num: 42 },
|
207 | { foo: false, string: '💩wertyuiop'.bgMagenta.green.bright, num: 43 } ]
|
208 |
|
209 | const d = ' | '.dim.cyan
|
210 | const _ = '-'.bright.cyan
|
211 |
|
212 | const result = asTable.configure ({ title: x => x.bright, delimiter: d, dash: _ }) (testData)
|
213 |
|
214 | console.log (result)
|
215 |
|
216 | assert.equal (result,
|
217 |
|
218 | ['foo'.bright + ' ', 'string'.bright + ' ', 'num'.bright].join (d) + '\n' +
|
219 | _.repeat (24) + '\n' +
|
220 | ['true ', 'abcde ', '42 '].join (d) + '\n' +
|
221 | ['false', '💩wertyuiop'.bgMagenta.green.bright, '43 '].join (d))
|
222 | })
|
223 | })
|