Debian Codegraph @

tblfmt_test.go

raw
1 package tblfmt
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "strings"
8 "testing"
9
10 "github.com/xo/tblfmt/internal"
11 )
12
13 func TestEncodeTableAll(t *testing.T) {
14 t.Parallel()
15 exp := ` my table
16 ╔══════════════════════╦══════════════════════════╦═══╗
17 ║ author_id ║ name ║ z ║
18 ╠══════════════════════╬══════════════════════════╬═══╣
19 ║ 14 ║ a b c d ║ x ║
20 ║ 15 ║ aoeu ↵║ ║
21 ║ ║ test ↵║ ║
22 ║ ║ ║ ║
23 ╚══════════════════════╩══════════════════════════╩═══╝
24 (2 rows)
25
26 my table
27 ╔══════════════════════╦═══════════════════════════╦════════════════════════╗
28 ║ author_id ║ name ║ z ║
29 ╠══════════════════════╬═══════════════════════════╬════════════════════════╣
30 ║ 16 ║ foo\bbar ║ ║
31 ║ 17 ║ 袈 袈 袈 ║ ║
32 ║ 18 ║ a b \r ↵║ a ↵║
33 ║ ║ a ║ ║
34 ║ 19 ║ 袈 袈 袈↵║ ║
35 ║ ║ ║ ║
36 ║ 20 ║ javascript ║ { ↵║
37 ║ ║ ║ "test21": "a value",↵║
38 ║ ║ ║ "test22": "foo\bbar"↵║
39 ║ ║ ║ } ║
40 ║ 23 ║ slice ║ [ ↵║
41 ║ ║ ║ "a", ↵║
42 ║ ║ ║ "b" ↵║
43 ║ ║ ║ ] ║
44 ╚══════════════════════╩═══════════════════════════╩════════════════════════╝
45 (6 rows)
46
47 my table
48 ╔══════════════════════╦═══════════════════════════╦════════════════════════╗
49 ║ author_id ║ name ║ z ║
50 ╠══════════════════════╬═══════════════════════════╬════════════════════════╣
51 ║ 38 ║ a b c d ║ x ║
52 ║ 39 ║ aoeu ↵║ ║
53 ║ ║ test ↵║ ║
54 ║ ║ ║ ║
55 ║ 40 ║ foo\bbar ║ ║
56 ║ 41 ║ 袈 袈 袈 ║ ║
57 ║ 42 ║ a b \r ↵║ a ↵║
58 ║ ║ a ║ ║
59 ║ 43 ║ 袈 袈 袈↵║ ║
60 ║ ║ ║ ║
61 ║ 44 ║ javascript ║ { ↵║
62 ║ ║ ║ "test45": "a value",↵║
63 ║ ║ ║ "test46": "foo\bbar"↵║
64 ║ ║ ║ } ║
65 ║ 47 ║ slice ║ [ ↵║
66 ║ ║ ║ "a", ↵║
67 ║ ║ ║ "b" ↵║
68 ║ ║ ║ ] ║
69 ╚══════════════════════╩═══════════════════════════╩════════════════════════╝
70 (8 rows)
71 `
72 buf := new(bytes.Buffer)
73 if err := EncodeTableAll(buf, internal.Multi(), WithBorder(2), WithLineStyle(UnicodeDoubleLineStyle()), WithTitle("my table"), WithWidths(20, 20)); err != nil {
74 t.Fatalf("expected no error, got: %v", err)
75 }
76 actual := buf.String()
77 if actual != exp {
78 t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual)
79 }
80 }
81
82 func TestEncodeJSONAll(t *testing.T) {
83 t.Parallel()
84 exp := `[
85 {
86 "author_id": 14,
87 "name": "a\tb\tc\td",
88 "z": "x"
89 },
90 {
91 "author_id": 15,
92 "name": "aoeu\ntest\n",
93 "z": null
94 }
95 ],
96 [
97 {
98 "author_id": 16,
99 "name": "foo\bbar",
100 "z": null
101 },
102 {
103 "author_id": 17,
104 "name": "袈\t袈\t\t袈",
105 "z": null
106 },
107 {
108 "author_id": 18,
109 "name": "a\tb\t\r\n\ta",
110 "z": "a\n"
111 },
112 {
113 "author_id": 19,
114 "name": "袈\t袈\t\t袈\n",
115 "z": null
116 },
117 {
118 "author_id": 20,
119 "name": "javascript",
120 "z": {
121 "test21": "a value",
122 "test22": "foo\bbar"
123 }
124 },
125 {
126 "author_id": 23,
127 "name": "slice",
128 "z": [
129 "a",
130 "b"
131 ]
132 }
133 ],
134 [
135 {
136 "author_id": 38,
137 "name": "a\tb\tc\td",
138 "z": "x"
139 },
140 {
141 "author_id": 39,
142 "name": "aoeu\ntest\n",
143 "z": null
144 },
145 {
146 "author_id": 40,
147 "name": "foo\bbar",
148 "z": null
149 },
150 {
151 "author_id": 41,
152 "name": "袈\t袈\t\t袈",
153 "z": null
154 },
155 {
156 "author_id": 42,
157 "name": "a\tb\t\r\n\ta",
158 "z": "a\n"
159 },
160 {
161 "author_id": 43,
162 "name": "袈\t袈\t\t袈\n",
163 "z": null
164 },
165 {
166 "author_id": 44,
167 "name": "javascript",
168 "z": {
169 "test45": "a value",
170 "test46": "foo\bbar"
171 }
172 },
173 {
174 "author_id": 47,
175 "name": "slice",
176 "z": [
177 "a",
178 "b"
179 ]
180 }
181 ]
182 `
183 buf := new(bytes.Buffer)
184 if err := EncodeJSONAll(buf, internal.Multi()); err != nil {
185 t.Fatalf("expected no error, got: %v", err)
186 }
187 actual := buf.String()
188 if actual != exp {
189 t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual)
190 }
191 }
192
193 func TestEncodeTemplateAll(t *testing.T) {
194 t.Parallel()
195 exp := `Row 0:
196 author_id = "14"
197 name = "a b c d"
198 z = "x"
199 Row 1:
200 author_id = "15"
201 name = "aoeu
202 test
203 "
204 z = "<nil>"
205
206 Row 0:
207 author_id = "16"
208 name = "foo\bbar"
209 z = "<nil>"
210 Row 1:
211 author_id = "17"
212 name = "袈 袈 袈"
213 z = "<nil>"
214 Row 2:
215 author_id = "18"
216 name = "a b \r
217 a"
218 z = "a
219 "
220 Row 3:
221 author_id = "19"
222 name = "袈 袈 袈
223 "
224 z = "<nil>"
225 Row 4:
226 author_id = "20"
227 name = "javascript"
228 z = "{
229 "test21": "a value",
230 "test22": "foo\bbar"
231 }"
232 Row 5:
233 author_id = "23"
234 name = "slice"
235 z = "[
236 "a",
237 "b"
238 ]"
239
240 Row 0:
241 author_id = "38"
242 name = "a b c d"
243 z = "x"
244 Row 1:
245 author_id = "39"
246 name = "aoeu
247 test
248 "
249 z = "<nil>"
250 Row 2:
251 author_id = "40"
252 name = "foo\bbar"
253 z = "<nil>"
254 Row 3:
255 author_id = "41"
256 name = "袈 袈 袈"
257 z = "<nil>"
258 Row 4:
259 author_id = "42"
260 name = "a b \r
261 a"
262 z = "a
263 "
264 Row 5:
265 author_id = "43"
266 name = "袈 袈 袈
267 "
268 z = "<nil>"
269 Row 6:
270 author_id = "44"
271 name = "javascript"
272 z = "{
273 "test45": "a value",
274 "test46": "foo\bbar"
275 }"
276 Row 7:
277 author_id = "47"
278 name = "slice"
279 z = "[
280 "a",
281 "b"
282 ]"
283 `
284 buf := new(bytes.Buffer)
285 if err := EncodeTemplateAll(buf, internal.Multi(), WithExecutor(testWriteTemplateTo), WithEmpty("<nil>")); err != nil {
286 t.Fatalf("expected no error, got: %v", err)
287 }
288 actual := buf.String()
289 if actual != exp {
290 t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual)
291 }
292 }
293
294 func TestEncodeUnalignedAll(t *testing.T) {
295 t.Parallel()
296 exp := `author_id|name|z
297 14|a b c d|x
298 15|aoeu
299 test
300 |
301 (2 rows)
302
303 author_id|name|z
304 16|foo` + "\b" + `bar|
305 17|袈 袈 袈|
306 18|a b ` + "\r" + `
307 a|a
308
309 19|袈 袈 袈
310 |
311 20|javascript|{
312 "test21": "a value",
313 "test22": "foo\bbar"
314 }
315 23|slice|[
316 "a",
317 "b"
318 ]
319 (6 rows)
320
321 author_id|name|z
322 38|a b c d|x
323 39|aoeu
324 test
325 |
326 40|foo` + "\b" + `bar|
327 41|袈 袈 袈|
328 42|a b ` + "\r" + `
329 a|a
330
331 43|袈 袈 袈
332 |
333 44|javascript|{
334 "test45": "a value",
335 "test46": "foo\bbar"
336 }
337 47|slice|[
338 "a",
339 "b"
340 ]
341 (8 rows)
342 `
343 buf := new(bytes.Buffer)
344 if err := EncodeUnalignedAll(buf, internal.Multi()); err != nil {
345 t.Fatalf("expected no error, got: %v", err)
346 }
347 actual := buf.String()
348 if actual != exp {
349 t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual)
350 }
351 }
352
353 func TestEncodeCSVAll(t *testing.T) {
354 t.Parallel()
355 exp := `author_id,name,z
356 14,"a b c d",x
357 15,"aoeu
358 test
359 ",
360
361 author_id,name,z
362 16,foo` + "\b" + `bar,
363 17,"袈 袈 袈",
364 18,"a b ` + "\r" + `
365 a","a
366 "
367 19,"袈 袈 袈
368 ",
369 20,javascript,"{
370 ""test21"": ""a value"",
371 ""test22"": ""foo\bbar""
372 }"
373 23,slice,"[
374 ""a"",
375 ""b""
376 ]"
377
378 author_id,name,z
379 38,"a b c d",x
380 39,"aoeu
381 test
382 ",
383 40,foo` + "\b" + `bar,
384 41,"袈 袈 袈",
385 42,"a b ` + "\r" + `
386 a","a
387 "
388 43,"袈 袈 袈
389 ",
390 44,javascript,"{
391 ""test45"": ""a value"",
392 ""test46"": ""foo\bbar""
393 }"
394 47,slice,"[
395 ""a"",
396 ""b""
397 ]"
398 `
399 buf := new(bytes.Buffer)
400 if err := EncodeCSVAll(buf, internal.Multi()); err != nil {
401 t.Fatalf("expected no error, got: %v", err)
402 }
403 actual := buf.String()
404 if actual != exp {
405 t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual)
406 }
407 }
408
409 func testWriteTemplateTo(w io.Writer, tpl *Template) error {
410 // {{ $headers := .Headers }}{{ range $i, $r := .Rows }}Row {{ $i }}:{{ range $j, $c := $r }}
411 // {{ index $headers $j }} = "{{ $c }}"{{ end }}
412 // {{ end }}
413 for i, r := range tpl.Rows {
414 fmt.Fprintf(w, "Row %d:\n", i)
415 for j, c := range r {
416 fmt.Fprintf(w, " %s = \"%s\"\n", tpl.Headers[j], c)
417 }
418 }
419 return nil
420 }
421
422 // TestEncodeNullAlign checks that a null value follows the alignment of its
423 // column, the way psql aligns the null string by the column's type, and does
424 // not always sit to the left.
425 //
426 // Measured against psql 18.6:
427 //
428 // $ psql -P null='(null)' -c "select 12345678901234567890::numeric as n,
429 // 'txt'::text as t
430 // union all select null, null;"
431 // n | t
432 // ----------------------+--------
433 // 12345678901234567890 | txt
434 // (null) | (null)
435 func TestEncodeNullAlign(t *testing.T) {
436 t.Parallel()
437 tests := []struct {
438 name string
439 vals [][]any
440 exp string
441 html string
442 }{
443 // note: the values are wider than the null string on purpose. A null
444 // string exactly as wide as its column fills it, and then the two
445 // alignments are indistinguishable and the test proves nothing.
446 {
447 "right aligned column",
448 [][]any{{uint64(12345678901234567890)}, {nil}},
449 " (null)",
450 `align="right"`,
451 },
452 {
453 "left aligned column",
454 [][]any{{"aaaaaaaaaaaaaaaaaaaa"}, {nil}},
455 " (null)",
456 `align="left"`,
457 },
458 {
459 // a column of more than one alignment has none to pass on
460 "mixed alignment column",
461 [][]any{{uint64(12345678901234567890)}, {"aaaaaaaaaaaaaaaaaaaa"}, {nil}},
462 " (null)",
463 `align="left"`,
464 },
465 {
466 "all null column",
467 [][]any{{nil}, {nil}},
468 " (null)",
469 `align="left"`,
470 },
471 }
472 for _, test := range tests {
473 t.Run(test.name, func(t *testing.T) {
474 t.Parallel()
475 buf := new(bytes.Buffer)
476 if err := EncodeAll(buf, internal.New([]string{"n"}, test.vals), map[string]string{
477 "format": "aligned",
478 "null": "(null)",
479 }); err != nil {
480 t.Fatalf("expected no error, got: %v", err)
481 }
482 t.Logf("aligned:\n%s", buf.String())
483 // the null row is the last row before the summary
484 lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
485 if len(lines) < 3 {
486 t.Fatalf("expected at least 3 lines, got: %d", len(lines))
487 }
488 if s := strings.TrimRight(lines[len(lines)-2], " "); s != test.exp {
489 t.Errorf("expected null row %q, got: %q", test.exp, s)
490 }
491 buf.Reset()
492 if err := EncodeAll(buf, internal.New([]string{"n"}, test.vals), map[string]string{
493 "format": "html",
494 "null": "(null)",
495 }); err != nil {
496 t.Fatalf("expected no error, got: %v", err)
497 }
498 t.Logf("html:\n%s", buf.String())
499 if s := test.html + ">(null)</td>"; !strings.Contains(buf.String(), s) {
500 t.Errorf("expected html to contain %q", s)
501 }
502 })
503 }
504 }