1 | "use strict"; |
||
2 | |||
3 | const assert = require ('assert'), |
||
4 | asTable = require (process.env.AS_TABLE_TEST_FILE), |
||
5 | ansi = require ('ansicolor').nice |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
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)) |
||
0 ignored issues
–
show
|
|||
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 | |||
138 | it ('right align works', () => { |
||
139 | |||
140 | var testData = |
||
141 | [ { foo: 1234.567, bar: 12 }, |
||
142 | { foo: '4.567'.bgMagenta.green, bar: 1234.456890 } ] |
||
143 | |||
144 | assert.equal (asTable.configure ({ right: true }) (testData), |
||
145 | ' foo bar\n' + |
||
146 | '--------------------\n' + |
||
147 | '1234.567 12\n' + |
||
148 | ' ' + '4.567'.bgMagenta.green + ' 1234.45689') |
||
149 | }) |
||
150 | |||
151 | it ('ANSI coloring works', () => { |
||
152 | |||
153 | const testData = |
||
154 | [ { foo: true, string: 'abcde', num: 42 }, |
||
155 | { foo: false, string: '💩wertyuiop'.bgMagenta.green.bright, num: 43 } ] |
||
156 | |||
157 | const d = ' | '.dim.cyan |
||
158 | const _ = '-'.bright.cyan |
||
159 | |||
160 | const result = asTable.configure ({ title: x => x.bright, delimiter: d, dash: _ }) (testData) |
||
161 | |||
162 | console.log (result) |
||
0 ignored issues
–
show
|
|||
163 | |||
164 | assert.equal (result, |
||
165 | |||
166 | ['foo'.bright + ' ', 'string'.bright + ' ', 'num'.bright].join (d) + '\n' + |
||
167 | _.repeat (24) + '\n' + |
||
168 | ['true ', 'abcde ', '42 '].join (d) + '\n' + |
||
169 | ['false', '💩wertyuiop'.bgMagenta.green.bright, '43 '].join (d)) |
||
170 | }) |
||
171 | }) |