Debian Codegraph @

tests/extra_test.go

raw
1 package tests
2
3 import (
4 "bytes"
5 "testing"
6
7 "github.com/olekukonko/tablewriter"
8 "github.com/olekukonko/tablewriter/renderer"
9 "github.com/olekukonko/tablewriter/tw"
10 )
11
12 func TestFilterMasking(t *testing.T) {
13 tests := []struct {
14 name string
15 filter tw.Filter
16 data [][]string
17 expected string
18 }{
19 {
20 name: "MaskEmail",
21 filter: MaskEmail,
22 data: [][]string{
23 {"Alice", "[email protected]", "25"},
24 {"Bob", "[email protected]", "30"},
25 },
26 expected: `
27 ┌───────┬─────────────────────┬─────┐
28 │ NAME │ EMAIL │ AGE │
29 ├───────┼─────────────────────┼─────┤
30 │ Alice │ a****@example.com │ 25 │
31 │ Bob │ b*******@domain.org │ 30 │
32 └───────┴─────────────────────┴─────┘
33 `,
34 },
35 {
36 name: "MaskPassword",
37 filter: MaskPassword,
38 data: [][]string{
39 {"Alice", "secretpassword", "25"},
40 {"Bob", "pass1234", "30"},
41 },
42 expected: `
43 ┌───────┬────────────────┬─────┐
44 │ NAME │ PASSWORD │ AGE │
45 ├───────┼────────────────┼─────┤
46 │ Alice │ ************** │ 25 │
47 │ Bob │ ******** │ 30 │
48 └───────┴────────────────┴─────┘
49 `,
50 },
51 {
52 name: "MaskCard",
53 filter: MaskCard,
54 data: [][]string{
55 {"Alice", "4111-1111-1111-1111", "25"},
56 {"Bob", "5105105105105100", "30"},
57 },
58 expected: `
59 ┌───────┬─────────────────────┬─────┐
60 │ NAME │ CREDIT CARD │ AGE │
61 ├───────┼─────────────────────┼─────┤
62 │ Alice │ ****-****-****-1111 │ 25 │
63 │ Bob │ 5105105105105100 │ 30 │
64 └───────┴─────────────────────┴─────┘
65 `,
66 },
67 }
68
69 for _, tt := range tests {
70 t.Run(tt.name, func(t *testing.T) {
71 var buf bytes.Buffer
72 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
73 Header: tw.CellConfig{
74 Formatting: tw.CellFormatting{AutoFormat: tw.On},
75 Alignment: tw.CellAlignment{Global: tw.AlignCenter},
76 Padding: tw.CellPadding{Global: tw.Padding{Left: " ", Right: " "}},
77 },
78 Row: tw.CellConfig{
79 Alignment: tw.CellAlignment{Global: tw.AlignLeft},
80 Padding: tw.CellPadding{Global: tw.Padding{Left: " ", Right: " "}},
81 Filter: tw.CellFilter{
82 Global: tt.filter,
83 },
84 },
85 }))
86 header := []string{"Name", tt.name, "Age"}
87 switch tt.name {
88 case "MaskEmail":
89 header[1] = "Email"
90 case "MaskPassword":
91 header[1] = "Password"
92 case "MaskCard":
93 header[1] = "Credit Card"
94 }
95 table.Header(header)
96 table.Bulk(tt.data)
97 table.Render()
98
99 if !visualCheck(t, tt.name, buf.String(), tt.expected) {
100 t.Error(table.Debug())
101 }
102 })
103 }
104 }
105
106 func TestMasterClass(t *testing.T) {
107 var buf bytes.Buffer
108 littleConfig := tablewriter.Config{
109 MaxWidth: 30,
110 Row: tw.CellConfig{
111 Alignment: tw.CellAlignment{Global: tw.AlignCenter},
112 Padding: tw.CellPadding{
113 Global: tw.Padding{Left: tw.Skip, Right: tw.Skip, Top: tw.Skip, Bottom: tw.Skip},
114 },
115 },
116 }
117
118 bigConfig := tablewriter.Config{
119 MaxWidth: 50,
120 Header: tw.CellConfig{Formatting: tw.CellFormatting{
121 AutoWrap: tw.WrapTruncate,
122 }},
123 Row: tw.CellConfig{
124 Alignment: tw.CellAlignment{Global: tw.AlignCenter},
125 Padding: tw.CellPadding{
126 Global: tw.Padding{Left: tw.Skip, Right: tw.Skip, Top: tw.Skip, Bottom: tw.Skip},
127 },
128 },
129 }
130
131 little := func(s string) string {
132 var b bytes.Buffer
133 table := tablewriter.NewTable(&b,
134 tablewriter.WithConfig(littleConfig),
135 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
136 Borders: tw.BorderNone,
137 Settings: tw.Settings{
138 Separators: tw.Separators{
139 ShowHeader: tw.Off,
140 ShowFooter: tw.Off,
141 BetweenRows: tw.On,
142 BetweenColumns: tw.Off,
143 },
144 Lines: tw.Lines{
145 ShowTop: tw.Off,
146 ShowBottom: tw.Off,
147 ShowHeaderLine: tw.Off,
148 ShowFooterLine: tw.On,
149 },
150 },
151 })),
152 )
153 table.Append([]string{s, s})
154 table.Append([]string{s, s})
155 table.Render()
156
157 return b.String()
158 }
159
160 table := tablewriter.NewTable(&buf,
161 tablewriter.WithConfig(bigConfig),
162 tablewriter.WithDebug(true),
163 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
164 Borders: tw.BorderNone,
165 Settings: tw.Settings{
166 Separators: tw.Separators{
167 ShowHeader: tw.Off,
168 ShowFooter: tw.Off,
169 BetweenRows: tw.Off,
170 BetweenColumns: tw.On,
171 },
172 Lines: tw.Lines{
173 ShowTop: tw.Off,
174 ShowBottom: tw.Off,
175 ShowHeaderLine: tw.Off,
176 ShowFooterLine: tw.Off,
177 },
178 },
179 })),
180 )
181 table.Append([]string{little("A"), little("B")})
182 table.Append([]string{little("C"), little("D")})
183 table.Render()
184
185 expected := `
186 A A │ B B
187 ────── │ ──────
188 A A │ B B
189 C C │ D D
190 ────── │ ──────
191 C C │ D D
192 `
193 if !visualCheck(t, "Master Class", buf.String(), expected) {
194 t.Error(table.Debug())
195 }
196 }
197
198 func TestConfigAutoHideDefault(t *testing.T) {
199 var buf bytes.Buffer
200 table := tablewriter.NewTable(&buf)
201
202 // Use the new exported Config() method
203 cfg := table.Config()
204 if cfg.Behavior.AutoHide.Enabled() {
205 t.Errorf("Expected AutoHide default to be false, got true")
206 }
207 }
208
209 func TestAutoHideFeature(t *testing.T) {
210 data := [][]string{
211 {"A", "The Good", ""}, // Rating is empty
212 {"B", "The Bad", " "}, // Rating is whitespace
213 {"C", "The Ugly", " "}, // Rating is whitespace
214 {"D", "The Gopher", ""}, // Rating is empty
215 // Add a row where Rating is NOT empty to test the opposite case
216 {"E", "The Tester", "999"},
217 }
218
219 // Test Case 1: Hide Empty Column
220 t.Run("HideWhenEmpty", func(t *testing.T) {
221 var buf bytes.Buffer
222 table := tablewriter.NewTable(&buf,
223 tablewriter.WithAutoHide(tw.On), // Enable the feature
224 tablewriter.WithDebug(false),
225 )
226 table.Header([]string{"Name", "Sign", "Rating"}) // Header IS included
227
228 // Use only data where the last column IS empty
229 emptyData := [][]string{
230 {"A", "The Good", ""},
231 {"B", "The Bad", " "},
232 {"C", "The Ugly", " "},
233 {"D", "The Gopher", ""},
234 }
235 for _, v := range emptyData {
236 table.Append(v)
237 }
238
239 table.Render()
240
241 // Expected output: Rating column should be completely gone
242 expected := `
243 ┌──────┬────────────┐
244 │ NAME │ SIGN │
245 ├──────┼────────────┤
246 │ A │ The Good │
247 │ B │ The Bad │
248 │ C │ The Ugly │
249 │ D │ The Gopher │
250 └──────┴────────────┘
251 `
252 // Use visualCheck, expect it might fail initially if Blueprint isn't perfect yet
253 if !visualCheck(t, "AutoHide_HideWhenEmpty", buf.String(), expected) {
254 t.Log("Output for HideWhenEmpty was not as expected (might be OK if Blueprint needs more fixes):")
255 t.Error(table.Debug())
256 }
257 })
258
259 // Test Case 2: Show Column When Not Empty
260 t.Run("ShowWhenNotEmpty", func(t *testing.T) {
261 var buf bytes.Buffer
262 table := tablewriter.NewTable(&buf,
263 tablewriter.WithAutoHide(tw.On), // Feature enabled
264 // tablewriter.WithRenderer(renderer.NewBlueprint()),
265 )
266 table.Header([]string{"Name", "Sign", "Rating"})
267
268 // Use data where at least one row has content in the last column
269 for _, v := range data { // Use the original data mix
270 table.Append(v)
271 }
272
273 table.Render()
274
275 // Expected output: Rating column should be present because row "E" has content
276 expected := `
277 ┌──────┬────────────┬────────┐
278 │ NAME │ SIGN │ RATING │
279 ├──────┼────────────┼────────┤
280 │ A │ The Good │ │
281 │ B │ The Bad │ │
282 │ C │ The Ugly │ │
283 │ D │ The Gopher │ │
284 │ E │ The Tester │ 999 │
285 └──────┴────────────┴────────┘
286 `
287 if !visualCheck(t, "AutoHide_ShowWhenNotEmpty", buf.String(), expected) {
288 t.Log("Output for ShowWhenNotEmpty was not as expected (might be OK if Blueprint needs more fixes):")
289 t.Error(table.Debug())
290 }
291 })
292
293 // Test Case 3: Feature Disabled
294 t.Run("DisabledShowsEmpty", func(t *testing.T) {
295 var buf bytes.Buffer
296 table := tablewriter.NewTable(&buf,
297 tablewriter.WithAutoHide(tw.Off), // Feature explicitly disabled
298 // tablewriter.WithRenderer(renderer.NewBlueprint()),
299 )
300 table.Header([]string{"Name", "Sign", "Rating"})
301
302 // Use only data where the last column IS empty
303 emptyData := [][]string{
304 {"A", "The Good", ""},
305 {"B", "The Bad", " "},
306 {"C", "The Ugly", " "},
307 {"D", "The Gopher", ""},
308 }
309 for _, v := range emptyData {
310 table.Append(v)
311 }
312
313 table.Render()
314
315 // Expected output: Rating column should be present but empty
316 expected := `
317 ┌──────┬────────────┬────────┐
318 │ NAME │ SIGN │ RATING │
319 ├──────┼────────────┼────────┤
320 │ A │ The Good │ │
321 │ B │ The Bad │ │
322 │ C │ The Ugly │ │
323 │ D │ The Gopher │ │
324 └──────┴────────────┴────────┘
325 `
326 // This one should ideally PASS if the default behavior is preserved
327 if !visualCheck(t, "AutoHide_DisabledShowsEmpty", buf.String(), expected) {
328 t.Errorf("AutoHide disabled test failed!")
329 t.Error(table.Debug())
330 }
331 })
332 }
333
334 func TestEmojiTable(t *testing.T) {
335 // Original Boston Test (Updated expectation as discussed)
336 t.Run("Cityscape", func(t *testing.T) {
337 var buf bytes.Buffer
338 table := tablewriter.NewTable(&buf, tablewriter.WithDebug(true))
339 table.Header([]string{"Name 😺", "Age 🎂", "City 🌍"})
340 data := [][]string{
341 {"Alice 😊", "25", "New York 🌆"},
342 {"Bob 😎", "30", "Boston 🏙️"},
343 {"Charlie 🤓", "28", "Tokyo 🗼"},
344 }
345 table.Bulk(data)
346 table.Footer([]string{"", "Total 👥", "3"})
347 table.Configure(func(config *tablewriter.Config) {
348 config.Row.Alignment.Global = tw.AlignLeft
349 config.Footer.Alignment.Global = tw.AlignRight
350 })
351 table.Render()
352
353 expected := `
354 ┌────────────┬──────────┬─────────────┐
355 │ NAME 😺 │ AGE 🎂 │ CITY 🌍 │
356 ├────────────┼──────────┼─────────────┤
357 │ Alice 😊 │ 25 │ New York 🌆 │
358 │ Bob 😎 │ 30 │ Boston 🏙️ │
359 │ Charlie 🤓 │ 28 │ Tokyo 🗼 │
360 ├────────────┼──────────┼─────────────┤
361 │ │ Total 👥 │ 3 │
362 └────────────┴──────────┴─────────────┘
363 `
364 if !visualCheck(t, "Cityscape", buf.String(), expected) {
365 t.Error(table.Debug())
366 }
367 })
368
369 // New Test: Common Emojis
370 // This verifies if standard faces/objects also calculate as Width 1
371 // causing the "extra space" visual effect in the padding.
372 t.Run("CommonEmojis", func(t *testing.T) {
373 var buf bytes.Buffer
374 table := tablewriter.NewTable(&buf)
375 // "Status" is 6 chars wide.
376 // "Icon" is 4 chars wide.
377 table.Header([]string{"Item", "Status", "Icon"})
378
379 table.Append([]string{"Work", "Done", "✅"}) // Check mark
380 table.Append([]string{"Happy", "Great", "😀"}) // Grinning Face
381 table.Append([]string{"Food", "Yum", "🍕"}) // Pizza
382 table.Append([]string{"Tech", "Fast", "🚀"}) // Rocket
383
384 table.Render()
385 expected := `
386 ┌───────┬────────┬──────┐
387 │ ITEM │ STATUS │ ICON │
388 ├───────┼────────┼──────┤
389 │ Work │ Done │ ✅ │
390 │ Happy │ Great │ 😀 │
391 │ Food │ Yum │ 🍕 │
392 │ Tech │ Fast │ 🚀 │
393 └───────┴────────┴──────┘
394 `
395
396 if !visualCheck(t, "CommonEmojis", buf.String(), expected) {
397 t.Error(table.Debug())
398 }
399 })
400 }
401
402 func TestUnicodeTableDefault(t *testing.T) {
403 // Original test case: Mixed ASCII, Latin-1, Emoji/Symbols, and Devanagari
404 t.Run("Mixed", func(t *testing.T) {
405 var buf bytes.Buffer
406
407 table := tablewriter.NewTable(&buf)
408 table.Header([]string{"Name", "Age", "City"})
409 table.Append([]string{"Alice", "25", "New York"})
410 table.Append([]string{"Bøb", "30", "Tōkyō"}) // Contains ø and ō
411 table.Append([]string{"José", "28", "México"}) // Contains é and accented e (e + combining acute)
412 table.Append([]string{"张三", "35", "北京"}) // Chinese characters
413 table.Append([]string{"अनु", "40", "मुंबई"}) // Devanagari script
414 table.Render()
415
416 expected := `
417 ┌───────┬─────┬──────────┐
418 │ NAME │ AGE │ CITY │
419 ├───────┼─────┼──────────┤
420 │ Alice │ 25 │ New York │
421 │ Bøb │ 30 │ Tōkyō │
422 │ José │ 28 │ México │
423 │ 张三 │ 35 │ 北京 │
424 │ अनु │ 40 │ मुंबई │
425 └───────┴─────┴──────────┘
426 `
427 if !visualCheck(t, "UnicodeTableRendering_Mixed", buf.String(), expected) {
428 t.Error(table.Debug())
429 }
430 })
431
432 // New test case: 100% Wide Characters (Chinese)
433 // This verifies alignment when every single character is Double Width (2 cells).
434 t.Run("PureChinese", func(t *testing.T) {
435 var buf bytes.Buffer
436
437 table := tablewriter.NewTable(&buf)
438 // Headers: Name, Age, Hometown
439 table.Header([]string{"姓名", "年龄", "籍贯"})
440
441 // Row 1: Zhang San, Twenty-Five, Beijing
442 table.Append([]string{"张三", "二十五", "北京"})
443
444 // Row 2: Li Si, Thirty, Shanghai
445 // Note: "三十" (30) is 2 chars (4 width), "二十五" (25) is 3 chars (6 width).
446 // This forces alignment padding on the "30" cell.
447 table.Append([]string{"李四", "三十", "上海"})
448
449 table.Render()
450
451 // Logic:
452 // Col 1: Max width 4 ("张三"). Visual: 1 space + 4 content + 1 space = 6.
453 // Col 2: Max width 6 ("二十五"). Visual: 1 space + 6 content + 1 space = 8.
454 // Col 3: Max width 4 ("北京"). Visual: 1 space + 4 content + 1 space = 6.
455 // "三十" needs 2 extra spaces padding to match "二十五".
456 expected := `
457 ┌──────┬────────┬──────┐
458 │ 姓名 │ 年龄 │ 籍贯 │
459 ├──────┼────────┼──────┤
460 │ 张三 │ 二十五 │ 北京 │
461 │ 李四 │ 三十 │ 上海 │
462 └──────┴────────┴──────┘
463 `
464 if !visualCheck(t, "UnicodeTableRendering_PureChinese", buf.String(), expected) {
465 t.Error(table.Debug())
466 }
467 })
468 }
469
470 func TestSpaces(t *testing.T) {
471 var buf bytes.Buffer
472 data := [][]string{
473 {"No", "Age", " City"},
474 {" 1", "25", "New York"},
475 {"2", "30", "x"},
476 {" 3", "28", " Lagos"},
477 }
478 t.Run("Trim", func(t *testing.T) {
479 buf.Reset()
480 table := tablewriter.NewTable(&buf, tablewriter.WithDebug(false), tablewriter.WithTrimSpace(tw.On))
481 table.Header(data[0])
482 table.Bulk(data[1:])
483 table.Render()
484
485 expected := `
486 ┌────┬─────┬──────────┐
487 │ NO │ AGE │ CITY │
488 ├────┼─────┼──────────┤
489 │ 1 │ 25 │ New York │
490 │ 2 │ 30 │ x │
491 │ 3 │ 28 │ Lagos │
492 └────┴─────┴──────────┘
493 `
494 if !visualCheck(t, "UnicodeTableRendering", buf.String(), expected) {
495 t.Error(table.Debug())
496 }
497 })
498
499 t.Run("NoTrim", func(t *testing.T) {
500 buf.Reset()
501 table := tablewriter.NewTable(&buf, tablewriter.WithTrimSpace(tw.Off))
502 table.Header(data[0])
503 table.Bulk(data[1:])
504 table.Render()
505
506 expected := `
507 ┌──────────┬─────┬────────────┐
508 │ NO │ AGE │ CITY │
509 ├──────────┼─────┼────────────┤
510 │ 1 │ 25 │ New York │
511 │ 2 │ 30 │ x │
512 │ 3 │ 28 │ Lagos │
513 └──────────┴─────┴────────────┘
514
515 `
516 visualCheck(t, "UnicodeTableRendering", buf.String(), expected)
517 })
518 }
519
520 func TestControl(t *testing.T) {
521 var buf bytes.Buffer
522 data := [][]string{
523 {"No", "Age", " City"},
524 {" 1", "25", "New York"},
525 {"2", "30", "x"},
526 {" 3", "28", " Lagos"},
527 }
528 t.Run("Trim", func(t *testing.T) {
529 buf.Reset()
530 table := tablewriter.NewTable(&buf,
531 tablewriter.WithDebug(false),
532 tablewriter.WithTrimSpace(tw.On),
533 tablewriter.WithHeaderControl(tw.Control{Hide: tw.On}),
534 )
535 table.Header(data[0])
536 table.Bulk(data[1:])
537 table.Render()
538
539 expected := `
540 ┌───┬────┬──────────┐
541 │ 1 │ 25 │ New York │
542 │ 2 │ 30 │ x │
543 │ 3 │ 28 │ Lagos │
544 └───┴────┴──────────┘
545
546 `
547 if !visualCheck(t, "UnicodeTableRendering", buf.String(), expected) {
548 t.Log(table.Debug())
549 }
550 })
551
552 t.Run("NoTrim", func(t *testing.T) {
553 buf.Reset()
554 table := tablewriter.NewTable(&buf,
555 tablewriter.WithTrimSpace(tw.On),
556 tablewriter.WithHeaderControl(tw.Control{Hide: tw.Off}),
557 )
558 table.Header(data[0])
559 table.Bulk(data[1:])
560 table.Render()
561
562 expected := `
563 ┌────┬─────┬──────────┐
564 │ NO │ AGE │ CITY │
565 ├────┼─────┼──────────┤
566 │ 1 │ 25 │ New York │
567 │ 2 │ 30 │ x │
568 │ 3 │ 28 │ Lagos │
569 └────┴─────┴──────────┘
570
571
572 `
573 if !visualCheck(t, "UnicodeTableRendering", buf.String(), expected) {
574 t.Error(table.Debug())
575 }
576 })
577 }
578
579 func TestPadding(t *testing.T) {
580 var buf bytes.Buffer
581 data := [][]string{
582 {"Column A", "Column B"},
583 {"aaaaa", "bbbbb"},
584 {"a", "b"},
585 }
586
587 alignments := tw.Alignment{
588 tw.AlignDefault,
589 tw.AlignRight,
590 }
591
592 t.Run("Right", func(t *testing.T) {
593 buf.Reset()
594 table := tablewriter.NewTable(
595 &buf,
596 tablewriter.WithRendition(tw.Rendition{
597 Borders: tw.BorderNone,
598 Settings: tw.Settings{
599 Lines: tw.LinesNone,
600 Separators: tw.SeparatorsNone,
601 },
602 }),
603 tablewriter.WithPadding(tw.Padding{
604 Left: "",
605 Right: ">",
606 Overwrite: true,
607 }),
608 tablewriter.WithHeaderAutoFormat(tw.Off),
609 tablewriter.WithAlignment(alignments),
610 tablewriter.WithDebug(true),
611 )
612
613 table.Header(data[0])
614 table.Bulk(data[1:])
615 table.Render()
616
617 expected := `
618 Column A>Column B>
619 aaaaa>>>> bbbbb>
620 a>>>>>>>> b>
621 `
622 if !visualCheck(t, "UnicodeTableRendering", buf.String(), expected) {
623 t.Log(table.Debug())
624 }
625 })
626
627 t.Run("Space", func(t *testing.T) {
628 buf.Reset()
629 table := tablewriter.NewTable(
630 &buf,
631 tablewriter.WithRendition(tw.Rendition{
632 Borders: tw.BorderNone,
633 Settings: tw.Settings{
634 Lines: tw.LinesNone,
635 Separators: tw.SeparatorsNone,
636 },
637 }),
638 tablewriter.WithPadding(tw.Padding{
639 Left: "",
640 Right: " ",
641 Overwrite: true,
642 }),
643 tablewriter.WithHeaderAutoFormat(tw.Off),
644 tablewriter.WithAlignment(alignments),
645 tablewriter.WithDebug(true),
646 )
647
648 table.Header(data[0])
649 table.Bulk(data[1:])
650 table.Render()
651
652 expected := `
653 Column A Column B
654 aaaaa bbbbb
655 a b
656 `
657 if !visualCheck(t, "UnicodeTableRendering", buf.String(), expected) {
658 t.Error(table.Debug())
659 }
660 })
661 }