Debian Codegraph @

tests/merge_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 TestVerticalMerge(t *testing.T) {
13 var buf bytes.Buffer
14 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
15 Row: tw.CellConfig{
16 Merging: tw.CellMerging{
17 Mode: tw.MergeVertical,
18 },
19 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignRight}},
20 },
21 }), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
22 Settings: tw.Settings{
23 Separators: tw.Separators{
24 BetweenRows: tw.Off,
25 },
26 },
27 })),
28 )
29 table.Header([]string{"Name", "Sign", "Rating"})
30 table.Append([]string{"A", "The Good", "500"})
31 table.Append([]string{"A", "The Very very Bad Man", "288"})
32 table.Append([]string{"B", "", "120"})
33 table.Append([]string{"B", "", "200"})
34 table.Render()
35
36 expected := `
37 ┌──────┬───────────────────────┬────────┐
38 │ NAME │ SIGN │ RATING │
39 ├──────┼───────────────────────┼────────┤
40 │ A │ The Good │ 500 │
41 │ │ The Very very Bad Man │ 288 │
42 │ B │ │ 120 │
43 │ │ │ 200 │
44 └──────┴───────────────────────┴────────┘
45 `
46
47 if !visualCheck(t, "VerticalMerge", buf.String(), expected) {
48 t.Error(table.Debug())
49 }
50 }
51
52 func TestHorizontalMerge(t *testing.T) {
53 var buf bytes.Buffer
54 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
55 Row: tw.CellConfig{
56 Merging: tw.CellMerging{
57 Mode: tw.MergeHorizontal,
58 },
59 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignCenter, tw.AlignCenter, tw.AlignCenter, tw.AlignCenter}},
60 },
61 }), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
62 Settings: tw.Settings{
63 Separators: tw.Separators{
64 BetweenRows: tw.Off,
65 },
66 },
67 })),
68 )
69 table.Header([]string{"Col1", "Col2", "Col2"})
70 table.Append([]string{"A", "B", "B"})
71 table.Append([]string{"A", "A", "C"})
72 table.Append([]string{"A", "B", "C"})
73 table.Append([]string{"B", "C", "C"})
74 table.Append([]string{"B", "C", "D"})
75 table.Append([]string{"D", "D", "D"})
76 table.Render()
77
78 expected := `
79 ┌───────┬───────┬───────┐
80 │ COL 1 │ COL 2 │ COL 2 │
81 ├───────┼───────┴───────┤
82 │ A │ B │
83 │ A │ C │
84 │ A │ B │ C │
85 │ B │ C │
86 │ B │ C │ D │
87 │ D │
88 └───────────────────────┘
89
90 `
91
92 if !visualCheck(t, "TestHorizontalMerge", buf.String(), expected) {
93 t.Error(table.Debug())
94 }
95 }
96
97 func TestHorizontalMergeEachLine(t *testing.T) {
98 var buf bytes.Buffer
99 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
100 Row: tw.CellConfig{
101 Merging: tw.CellMerging{
102 Mode: tw.MergeHorizontal,
103 },
104 },
105 Footer: tw.CellConfig{
106 Merging: tw.CellMerging{
107 Mode: tw.MergeHorizontal,
108 },
109 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}},
110 },
111 }),
112 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
113 // Symbols: tw.NewSymbols(tw.StyleMerger),
114 Settings: tw.Settings{
115 Separators: tw.Separators{
116 BetweenRows: tw.On,
117 },
118 },
119 })),
120 )
121 table.Header([]string{"Date", "Section A", "Section B", "Section C", "Section D", "Section E"})
122 table.Append([]string{"1/1/2014", "apple", "boy", "cat", "dog", "elephant"})
123 table.Append([]string{"1/1/2014", "apple", "apple", "boy", "dog", "elephant"})
124 table.Append([]string{"1/1/2014", "apple", "boy", "boy", "cat", "dog"})
125 table.Append([]string{"1/1/2014", "apple", "boy", "cat", "cat", "dog"})
126 table.Render()
127
128 expected := `
129 ┌──────────┬───────────┬───────────┬───────────┬───────────┬───────────┐
130 │ DATE │ SECTION A │ SECTION B │ SECTION C │ SECTION D │ SECTION E │
131 ├──────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
132 │ 1/1/2014 │ apple │ boy │ cat │ dog │ elephant │
133 ├──────────┼───────────┴───────────┼───────────┼───────────┼───────────┤
134 │ 1/1/2014 │ apple │ boy │ dog │ elephant │
135 ├──────────┼───────────┬───────────┴───────────┼───────────┼───────────┤
136 │ 1/1/2014 │ apple │ boy │ cat │ dog │
137 ├──────────┼───────────┼───────────┬───────────┴───────────┼───────────┤
138 │ 1/1/2014 │ apple │ boy │ cat │ dog │
139 └──────────┴───────────┴───────────┴───────────────────────┴───────────┘
140
141
142 `
143 check := visualCheck(t, "HorizontalMergeEachLine", buf.String(), expected)
144 if !check {
145 t.Error(table.Debug())
146 }
147 }
148
149 func TestHorizontalMergeEachLineCenter(t *testing.T) {
150 var buf bytes.Buffer
151 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
152 Row: tw.CellConfig{
153 Merging: tw.CellMerging{
154 Mode: tw.MergeHorizontal,
155 },
156 Alignment: tw.CellAlignment{Global: tw.AlignCenter},
157 },
158 }),
159 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
160 // Symbols: tw.NewSymbols(tw.StyleMerger),
161 Settings: tw.Settings{
162 Separators: tw.Separators{
163 BetweenRows: tw.On,
164 },
165 },
166 })),
167 )
168 table.Header([]string{"Date", "Section A", "Section B", "Section C", "Section D", "Section E"})
169 table.Append([]string{"1/1/2014", "apple", "boy", "cat", "dog", "elephant"})
170 table.Append([]string{"1/1/2014", "apple", "apple", "boy", "dog", "elephant"})
171 table.Append([]string{"1/1/2014", "apple", "boy", "boy", "cat", "dog"})
172 table.Append([]string{"1/1/2014", "apple", "boy", "cat", "cat", "dog"})
173 table.Render()
174
175 expected := `
176 ┌──────────┬───────────┬───────────┬───────────┬───────────┬───────────┐
177 │ DATE │ SECTION A │ SECTION B │ SECTION C │ SECTION D │ SECTION E │
178 ├──────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
179 │ 1/1/2014 │ apple │ boy │ cat │ dog │ elephant │
180 ├──────────┼───────────┴───────────┼───────────┼───────────┼───────────┤
181 │ 1/1/2014 │ apple │ boy │ dog │ elephant │
182 ├──────────┼───────────┬───────────┴───────────┼───────────┼───────────┤
183 │ 1/1/2014 │ apple │ boy │ cat │ dog │
184 ├──────────┼───────────┼───────────┬───────────┴───────────┼───────────┤
185 │ 1/1/2014 │ apple │ boy │ cat │ dog │
186 └──────────┴───────────┴───────────┴───────────────────────┴───────────┘
187
188 `
189 check := visualCheck(t, "HorizontalMergeEachLineCenter", buf.String(), expected)
190 if !check {
191 t.Error(table.Debug())
192 }
193 }
194
195 func TestHorizontalMergeAlignFooter(t *testing.T) {
196 var buf bytes.Buffer
197 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
198 Row: tw.CellConfig{
199 Merging: tw.CellMerging{
200 Mode: tw.MergeHorizontal,
201 },
202 },
203 Footer: tw.CellConfig{
204 Merging: tw.CellMerging{
205 Mode: tw.MergeHorizontal,
206 },
207 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}},
208 },
209 }),
210 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
211 Settings: tw.Settings{
212 Separators: tw.Separators{
213 BetweenRows: tw.On,
214 },
215 },
216 })),
217 )
218 table.Header([]string{"Date", "Description", "Status", "Conclusion"})
219 table.Append([]string{"1/1/2014", "Domain name", "Successful", "Successful"})
220 table.Append([]string{"1/1/2014", "Domain name", "Pending", "Waiting"})
221 table.Append([]string{"1/1/2014", "Domain name", "Successful", "Rejected"})
222 table.Footer([]string{"", "", "TOTAL", "$145.93"}) // Fixed from Append
223 table.Render()
224
225 expected := `
226 ┌──────────┬─────────────┬────────────┬────────────┐
227 │ DATE │ DESCRIPTION │ STATUS │ CONCLUSION │
228 ├──────────┼─────────────┼────────────┴────────────┤
229 │ 1/1/2014 │ Domain name │ Successful │
230 ├──────────┼─────────────┼────────────┬────────────┤
231 │ 1/1/2014 │ Domain name │ Pending │ Waiting │
232 ├──────────┼─────────────┼────────────┼────────────┤
233 │ 1/1/2014 │ Domain name │ Successful │ Rejected │
234 ├──────────┴─────────────┴────────────┼────────────┤
235 │ TOTAL │ $145.93 │
236 └─────────────────────────────────────┴────────────┘
237 `
238 check := visualCheck(t, "HorizontalMergeAlignFooter", buf.String(), expected)
239 if !check {
240 t.Error(table.Debug())
241 }
242 }
243
244 func TestVerticalMergeLines(t *testing.T) {
245 var buf bytes.Buffer
246 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
247 Row: tw.CellConfig{
248 Merging: tw.CellMerging{
249 Mode: tw.MergeVertical,
250 },
251 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignRight}},
252 },
253 }), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
254 // Symbols: tw.NewSymbols(tw.StyleMerger),
255 Settings: tw.Settings{
256 Separators: tw.Separators{
257 BetweenRows: tw.On,
258 },
259 },
260 })))
261 table.Header([]string{"Name", "Sign", "Rating"})
262 table.Append([]string{"A", "The Good", "500"})
263 table.Append([]string{"A", "The Very very Bad Man", "288"})
264 table.Append([]string{"B", "C", "120"})
265 table.Append([]string{"B", "C", "200"})
266 table.Render()
267
268 expected := `
269 ┌──────┬───────────────────────┬────────┐
270 │ NAME │ SIGN │ RATING │
271 ├──────┼───────────────────────┼────────┤
272 │ A │ The Good │ 500 │
273 │ ├───────────────────────┼────────┤
274 │ │ The Very very Bad Man │ 288 │
275 ├──────┼───────────────────────┼────────┤
276 │ B │ C │ 120 │
277 │ │ ├────────┤
278 │ │ │ 200 │
279 └──────┴───────────────────────┴────────┘
280 `
281
282 if !visualCheck(t, "VerticalMergeLines", buf.String(), expected) {
283 t.Error(table.Debug())
284 }
285 }
286
287 func TestMergeBoth(t *testing.T) {
288 var buf bytes.Buffer
289
290 c := tablewriter.Config{
291 Row: tw.CellConfig{
292 Merging: tw.CellMerging{
293 Mode: tw.MergeBoth,
294 },
295 },
296 Footer: tw.CellConfig{
297 Merging: tw.CellMerging{
298 Mode: tw.MergeHorizontal,
299 },
300 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignRight, tw.AlignRight, tw.AlignRight, tw.AlignLeft}},
301 },
302 }
303
304 r := renderer.NewBlueprint(tw.Rendition{
305 Settings: tw.Settings{
306 Separators: tw.Separators{
307 BetweenRows: tw.On,
308 },
309 },
310 })
311
312 t.Run("mixed-1", func(t *testing.T) {
313 buf.Reset()
314 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(c), tablewriter.WithRenderer(r))
315 table.Header([]string{"Date", "Description", "Status", "Conclusion"})
316 table.Append([]string{"1/1/2014", "Domain name", "Successful", "Successful"})
317 table.Append([]string{"1/1/2014", "Domain name", "Pending", "Waiting"})
318 table.Append([]string{"1/1/2014", "Domain name", "Successful", "Rejected"})
319 table.Footer([]string{"TOTAL", "TOTAL", "TOTAL", "$145.93"})
320 table.Render()
321
322 expected := `
323 ┌──────────┬─────────────┬────────────┬────────────┐
324 │ DATE │ DESCRIPTION │ STATUS │ CONCLUSION │
325 ├──────────┼─────────────┼────────────┴────────────┤
326 │ 1/1/2014 │ Domain name │ Successful │
327 │ │ ├────────────┬────────────┤
328 │ │ │ Pending │ Waiting │
329 │ │ ├────────────┼────────────┤
330 │ │ │ Successful │ Rejected │
331 ├──────────┴─────────────┴────────────┼────────────┤
332 │ TOTAL │ $145.93 │
333 └─────────────────────────────────────┴────────────┘
334
335 `
336 check := visualCheck(t, "TestMergeBoth-mixed-1", buf.String(), expected)
337 if !check {
338 t.Log(table.Debug())
339 }
340 })
341
342 t.Run("mixed-2", func(t *testing.T) {
343 buf.Reset()
344 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(c), tablewriter.WithRenderer(r))
345 table.Header([]string{"Date", "Description", "Status", "Conclusion"})
346 table.Append([]string{"1/1/2014", "Domain name", "Successful", "Successful"})
347 table.Append([]string{"1/1/2014", "Domain name", "Pending", "Waiting"})
348 table.Append([]string{"1/1/2015", "Domain name", "Successful", "Rejected"})
349 table.Footer([]string{"TOTAL", "TOTAL", "TOTAL", "$145.93"})
350 table.Render()
351
352 expected := `
353 ┌──────────┬─────────────┬────────────┬────────────┐
354 │ DATE │ DESCRIPTION │ STATUS │ CONCLUSION │
355 ├──────────┼─────────────┼────────────┴────────────┤
356 │ 1/1/2014 │ Domain name │ Successful │
357 │ │ ├────────────┬────────────┤
358 │ │ │ Pending │ Waiting │
359 ├──────────┤ ├────────────┼────────────┤
360 │ 1/1/2015 │ │ Successful │ Rejected │
361 ├──────────┴─────────────┴────────────┼────────────┤
362 │ TOTAL │ $145.93 │
363 └─────────────────────────────────────┴────────────┘
364 `
365 check := visualCheck(t, "TestMergeBoth-mixed-2", buf.String(), expected)
366 if !check {
367 t.Log(table.Debug())
368 }
369 })
370 }
371
372 func TestMergeHierarchical(t *testing.T) {
373 var buf bytes.Buffer
374 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
375 Row: tw.CellConfig{
376 Merging: tw.CellMerging{
377 Mode: tw.MergeHierarchical,
378 },
379 },
380 }),
381 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
382 Symbols: tw.NewSymbols(tw.StyleASCII),
383 Settings: tw.Settings{
384 Separators: tw.Separators{
385 BetweenRows: tw.On,
386 },
387 },
388 })),
389 )
390 table.Header([]string{"0", "1", "2", "3"})
391 table.Append([]string{"A", "a", "c", "-"})
392 table.Append([]string{"A", "b", "c", "-"})
393 table.Append([]string{"A", "b", "d", "-"})
394 table.Append([]string{"B", "b", "d", "-"})
395 table.Render()
396
397 expected := `
398 +---+---+---+---+
399 | 0 | 1 | 2 | 3 |
400 +---+---+---+---+
401 | A | a | c | - |
402 | +---+---+---+
403 | | b | c | - |
404 | | +---+---+
405 | | | d | - |
406 +---+---+---+---+
407 | B | b | d | - |
408 +---+---+---+---+
409 `
410 check := visualCheck(t, "MergeHierarchical", buf.String(), expected)
411 if !check {
412 t.Error(table.Debug())
413 }
414 }
415
416 func TestMergeHierarchicalUnicode(t *testing.T) {
417 var buf bytes.Buffer
418 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
419 Row: tw.CellConfig{
420 Merging: tw.CellMerging{
421 Mode: tw.MergeHierarchical,
422 },
423 },
424 }),
425 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
426 // Symbols: tw.NewSymbols(tw.StyleRounded),
427 Settings: tw.Settings{
428 Separators: tw.Separators{
429 BetweenRows: tw.On,
430 },
431 },
432 })),
433 )
434 table.Header([]string{"0", "1", "2", "3"})
435 table.Append([]string{"A", "a", "c", "-"})
436 table.Append([]string{"A", "b", "c", "-"})
437 table.Append([]string{"A", "b", "d", "-"})
438 table.Append([]string{"B", "b", "d", "-"})
439 table.Render()
440
441 expected := `
442 ┌───┬───┬───┬───┐
443 │ 0 │ 1 │ 2 │ 3 │
444 ├───┼───┼───┼───┤
445 │ A │ a │ c │ - │
446 │ ├───┼───┼───┤
447 │ │ b │ c │ - │
448 │ │ ├───┼───┤
449 │ │ │ d │ - │
450 ├───┼───┼───┼───┤
451 │ B │ b │ d │ - │
452 └───┴───┴───┴───┘
453 `
454 check := visualCheck(t, "MergeHierarchicalUnicode", buf.String(), expected)
455 if !check {
456 t.Error(table.Debug())
457 }
458 }
459
460 func TestMergeWithPadding(t *testing.T) {
461 var buf bytes.Buffer
462
463 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
464 Row: tw.CellConfig{
465 Merging: tw.CellMerging{
466 Mode: tw.MergeBoth,
467 },
468 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}},
469 },
470 Footer: tw.CellConfig{
471 Padding: tw.CellPadding{
472 Global: tw.Padding{Left: "*", Right: "*", Top: "", Bottom: ""},
473 PerColumn: []tw.Padding{{}, {}, {Bottom: "^"}, {Bottom: "."}},
474 },
475 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}},
476 },
477 }), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
478 // Symbols: tw.NewSymbols(tw.StyleASCII),
479 Settings: tw.Settings{
480 Separators: tw.Separators{
481 BetweenRows: tw.On,
482 },
483 },
484 })))
485
486 table.Header([]string{"Date", "Description", "Status", "Conclusion"})
487 table.Append([]string{"1/1/2014", "Domain name", "Successful", "Successful"})
488 table.Append([]string{"1/1/2014", "Domain name", "Pending", "Waiting"})
489 table.Append([]string{"1/1/2014", "Domain name", "Successful", "Rejected"})
490 table.Append([]string{"", "", "TOTAL", "$145.93"})
491 table.Render()
492
493 expected := `
494 ┌──────────┬─────────────┬────────────┬────────────┐
495 │ DATE │ DESCRIPTION │ STATUS │ CONCLUSION │
496 ├──────────┼─────────────┼────────────┴────────────┤
497 │ 1/1/2014 │ Domain name │ Successful │
498 │ │ ├────────────┬────────────┤
499 │ │ │ Pending │ Waiting │
500 │ │ ├────────────┼────────────┤
501 │ │ │ Successful │ Rejected │
502 ├──────────┼─────────────┼────────────┼────────────┤
503 │ │ │ TOTAL │ $145.93 │
504 │ │ │^^^^^^^^^^^^│............│
505 └──────────┴─────────────┴────────────┴────────────┘
506 `
507 visualCheck(t, "MergeWithPadding", buf.String(), expected)
508 }
509
510 func TestMergeWithMultipleLines(t *testing.T) {
511 var buf bytes.Buffer
512
513 data := [][]string{
514 {"Module", "Description", "Version", "Status"},
515 {"core\nutils", "Utility\nfunctions", "v1.0.0", "stable"},
516 {"core\nutils", "Helper\nroutines", "v1.1.0", "beta"},
517 {"web\nserver", "HTTP\nserver", "v2.0.0", "stable"},
518 {"web\nserver", "", "v2.1.0", "testing"},
519 {"db\nclient", "Database\naccess", "v3.0.0", ""},
520 }
521
522 t.Run("Horizontal", func(t *testing.T) {
523 buf.Reset()
524 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
525 Row: tw.CellConfig{
526 Merging: tw.CellMerging{
527 Mode: tw.MergeHorizontal,
528 },
529 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignLeft, tw.AlignLeft, tw.AlignLeft, tw.AlignLeft}},
530 },
531 }), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
532 Settings: tw.Settings{
533 Separators: tw.Separators{
534 BetweenRows: tw.On,
535 },
536 },
537 })))
538 table.Header(data[0])
539 table.Bulk(data[1:])
540 table.Render()
541
542 expected := `
543 ┌────────┬─────────────┬─────────┬─────────┐
544 │ MODULE │ DESCRIPTION │ VERSION │ STATUS │
545 ├────────┼─────────────┼─────────┼─────────┤
546 │ core │ Utility │ v1.0.0 │ stable │
547 │ utils │ functions │ │ │
548 ├────────┼─────────────┼─────────┼─────────┤
549 │ core │ Helper │ v1.1.0 │ beta │
550 │ utils │ routines │ │ │
551 ├────────┼─────────────┼─────────┼─────────┤
552 │ web │ HTTP │ v2.0.0 │ stable │
553 │ server │ server │ │ │
554 ├────────┼─────────────┼─────────┼─────────┤
555 │ web │ │ v2.1.0 │ testing │
556 │ server │ │ │ │
557 ├────────┼─────────────┼─────────┼─────────┤
558 │ db │ Database │ v3.0.0 │ │
559 │ client │ access │ │ │
560 └────────┴─────────────┴─────────┴─────────┘
561 `
562 // t.Log("====== LOG", table.Logger().Enabled())
563 if !visualCheck(t, "Horizontal", buf.String(), expected) {
564 t.Error(table.Debug())
565 }
566 })
567
568 t.Run("Vertical", func(t *testing.T) {
569 buf.Reset()
570 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
571 Row: tw.CellConfig{
572 Merging: tw.CellMerging{
573 Mode: tw.MergeVertical,
574 },
575 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignLeft, tw.AlignLeft, tw.AlignLeft, tw.AlignLeft}},
576 },
577 Debug: true,
578 }), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
579 Settings: tw.Settings{
580 Separators: tw.Separators{
581 BetweenRows: tw.On,
582 },
583 },
584 })))
585 table.Header(data[0])
586 table.Bulk(data[1:])
587 table.Render()
588
589 expected := `
590 ┌────────┬─────────────┬─────────┬─────────┐
591 │ MODULE │ DESCRIPTION │ VERSION │ STATUS │
592 ├────────┼─────────────┼─────────┼─────────┤
593 │ core │ Utility │ v1.0.0 │ stable │
594 │ utils │ functions │ │ │
595 │ ├─────────────┼─────────┼─────────┤
596 │ │ Helper │ v1.1.0 │ beta │
597 │ │ routines │ │ │
598 ├────────┼─────────────┼─────────┼─────────┤
599 │ web │ HTTP │ v2.0.0 │ stable │
600 │ server │ server │ │ │
601 │ ├─────────────┼─────────┼─────────┤
602 │ │ │ v2.1.0 │ testing │
603 ├────────┼─────────────┼─────────┼─────────┤
604 │ db │ Database │ v3.0.0 │ │
605 │ client │ access │ │ │
606 └────────┴─────────────┴─────────┴─────────┘
607 `
608 if !visualCheck(t, "Vertical", buf.String(), expected) {
609 t.Error(table.Debug())
610 }
611 })
612
613 t.Run("Hierarch", func(t *testing.T) {
614 buf.Reset()
615 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
616 Row: tw.CellConfig{
617 Merging: tw.CellMerging{
618 Mode: tw.MergeHierarchical,
619 },
620 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignLeft, tw.AlignLeft, tw.AlignLeft, tw.AlignLeft}},
621 },
622 }), tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
623 Settings: tw.Settings{
624 Separators: tw.Separators{
625 BetweenRows: tw.On,
626 },
627 },
628 })))
629 table.Header(data[0])
630 table.Bulk(data[1:])
631 table.Render()
632
633 expected := `
634 ┌────────┬─────────────┬─────────┬─────────┐
635 │ MODULE │ DESCRIPTION │ VERSION │ STATUS │
636 ├────────┼─────────────┼─────────┼─────────┤
637 │ core │ Utility │ v1.0.0 │ stable │
638 │ utils │ functions │ │ │
639 │ ├─────────────┼─────────┼─────────┤
640 │ │ Helper │ v1.1.0 │ beta │
641 │ │ routines │ │ │
642 ├────────┼─────────────┼─────────┼─────────┤
643 │ web │ HTTP │ v2.0.0 │ stable │
644 │ server │ server │ │ │
645 │ ├─────────────┼─────────┼─────────┤
646 │ │ │ v2.1.0 │ testing │
647 ├────────┼─────────────┼─────────┼─────────┤
648 │ db │ Database │ v3.0.0 │ │
649 │ client │ access │ │ │
650 └────────┴─────────────┴─────────┴─────────┘
651 `
652 if !visualCheck(t, "Hierarch", buf.String(), expected) {
653 // t.Error(table.Debug())
654 }
655 })
656 }
657
658 func TestMergeHierarchicalCombined(t *testing.T) {
659 var buf bytes.Buffer
660 table := tablewriter.NewTable(&buf, tablewriter.WithConfig(tablewriter.Config{
661 Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}},
662 Row: tw.CellConfig{
663 Merging: tw.CellMerging{Mode: tw.MergeHierarchical},
664 Alignment: tw.CellAlignment{Global: tw.AlignLeft},
665 },
666 Footer: tw.CellConfig{
667 Alignment: tw.CellAlignment{Global: tw.AlignLeft},
668 Merging: tw.CellMerging{
669 Mode: tw.MergeHorizontal,
670 },
671 },
672 Debug: true,
673 }),
674 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
675 Settings: tw.Settings{Separators: tw.Separators{BetweenRows: tw.On}},
676 })),
677 )
678
679 data := [][]string{
680 {"Engineering", "Backend", "API Team", "Alice"},
681 {"Engineering", "Backend", "Database Team", "Bob"},
682 {"Engineering", "Frontend", "UI Team", "Charlie"},
683 {"Marketing", "Digital", "SEO Team", "Dave"},
684 {"Marketing", "Digital", "Content Team", "Eve"},
685 }
686
687 table.Header([]string{"Department", "Division", "Team", "Lead"})
688 table.Bulk(data)
689 table.Footer([]string{"Total Teams", "5", "5", "5"})
690 table.Render()
691
692 expected := `
693 ┌─────────────┬──────────┬───────────────┬─────────┐
694 │ DEPARTMENT │ DIVISION │ TEAM │ LEAD │
695 ├─────────────┼──────────┼───────────────┼─────────┤
696 │ Engineering │ Backend │ API Team │ Alice │
697 │ │ ├───────────────┼─────────┤
698 │ │ │ Database Team │ Bob │
699 │ ├──────────┼───────────────┼─────────┤
700 │ │ Frontend │ UI Team │ Charlie │
701 ├─────────────┼──────────┼───────────────┼─────────┤
702 │ Marketing │ Digital │ SEO Team │ Dave │
703 │ │ ├───────────────┼─────────┤
704 │ │ │ Content Team │ Eve │
705 ├─────────────┼──────────┴───────────────┴─────────┤
706 │ Total Teams │ 5 │
707 └─────────────┴────────────────────────────────────┘
708
709 `
710 check := visualCheck(t, "MergeHierarchical", buf.String(), expected)
711 if !check {
712 t.Error(table.Debug())
713 }
714 }
715
716 // TestMergeVerticalByColumnIndex verifies that vertical merging
717 // only applies to the columns specified by the new API.
718 func TestMergeVerticalByColumnIndex(t *testing.T) {
719 var buf bytes.Buffer
720
721 // Data where columns 0, 1, and 3 have duplicates that could be merged.
722 data := [][]string{
723 {"A", "The Good", "500", "OK"},
724 {"A", "The Bad", "288", "OK"},
725 {"B", "The Ugly", "120", "FAIL"},
726 {"B", "The Ugly", "200", "FAIL"},
727 {"B", "The Ugly", "300", "OK"},
728 }
729
730 // Use the new fluent builder to configure column-specific merging.
731 b := tablewriter.NewConfigBuilder()
732 b.Row().Merging().
733 WithMode(tw.MergeVertical). // Enable Vertical Merging
734 ByColumnIndex([]int{0, 3}). // ONLY apply it to column 0 and 3
735 Build()
736
737 table := tablewriter.NewTable(&buf,
738 tablewriter.WithConfig(b.Build()),
739 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
740 Settings: tw.Settings{
741 Separators: tw.Separators{
742 BetweenRows: tw.On,
743 },
744 },
745 })))
746
747 table.Header([]string{"Name", "Sign", "Rating", "Status"})
748 table.Bulk(data)
749 table.Render()
750
751 // EXPECTED:
752 // - Column 0 ("Name") SHOULD be merged.
753 // - Column 1 ("Sign") should NOT be merged, even though "The Ugly" repeats.
754 // - Column 3 ("Status") SHOULD be merged.
755 expected := `
756 ┌──────┬──────────┬────────┬────────┐
757 │ NAME │ SIGN │ RATING │ STATUS │
758 ├──────┼──────────┼────────┼────────┤
759 │ A │ The Good │ 500 │ OK │
760 │ ├──────────┼────────┤ │
761 │ │ The Bad │ 288 │ │
762 ├──────┼──────────┼────────┼────────┤
763 │ B │ The Ugly │ 120 │ FAIL │
764 │ ├──────────┼────────┤ │
765 │ │ The Ugly │ 200 │ │
766 │ ├──────────┼────────┼────────┤
767 │ │ The Ugly │ 300 │ OK │
768 └──────┴──────────┴────────┴────────┘
769 `
770
771 if !visualCheck(t, "MergeVerticalByColumnIndex", buf.String(), expected) {
772 t.Error(table.Debug())
773 }
774 }
775
776 // TestMergeHierarchicalByColumnIndex verifies that hierarchical merging
777 // correctly respects the column index filter.
778 func TestMergeHierarchicalByColumnIndex(t *testing.T) {
779 var buf bytes.Buffer
780
781 data := [][]string{
782 {"Engineering", "Backend", "API Team", "Alice"},
783 {"Engineering", "Backend", "Database Team", "Bob"},
784 {"Engineering", "Frontend", "UI Team", "Charlie"},
785 {"Marketing", "Digital", "SEO Team", "Dave"},
786 {"Marketing", "Digital", "Content Team", "Eve"},
787 {"Marketing", "Brand", "PR Team", "Frank"},
788 }
789
790 // Use the new fluent builder to enable hierarchical merging for columns 0 and 1 only.
791 b := tablewriter.NewConfigBuilder()
792 b.Row().Merging().
793 WithMode(tw.MergeHierarchical).
794 ByColumnIndex([]int{0, 1}).
795 Build()
796
797 table := tablewriter.NewTable(&buf,
798 tablewriter.WithConfig(b.Build()),
799 tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{
800 Settings: tw.Settings{
801 Separators: tw.Separators{
802 BetweenRows: tw.On,
803 },
804 },
805 })))
806
807 table.Header([]string{"Department", "Division", "Team", "Lead"})
808 table.Bulk(data)
809 table.Render()
810
811 // EXPECTED:
812 // - Column 0 ("Department") SHOULD merge.
813 // - Column 1 ("Division") SHOULD merge hierarchically (e.g., "Backend" and "Digital").
814 // - Column 2 ("Team") should NOT merge because it's not in the index list.
815 // This will break the hierarchical chain for column 3.
816 // - Column 3 ("Lead") should NOT merge.
817 expected := `
818 ┌─────────────┬──────────┬───────────────┬─────────┐
819 │ DEPARTMENT │ DIVISION │ TEAM │ LEAD │
820 ├─────────────┼──────────┼───────────────┼─────────┤
821 │ Engineering │ Backend │ API Team │ Alice │
822 │ │ ├───────────────┼─────────┤
823 │ │ │ Database Team │ Bob │
824 │ ├──────────┼───────────────┼─────────┤
825 │ │ Frontend │ UI Team │ Charlie │
826 ├─────────────┼──────────┼───────────────┼─────────┤
827 │ Marketing │ Digital │ SEO Team │ Dave │
828 │ │ ├───────────────┼─────────┤
829 │ │ │ Content Team │ Eve │
830 │ ├──────────┼───────────────┼─────────┤
831 │ │ Brand │ PR Team │ Frank │
832 └─────────────┴──────────┴───────────────┴─────────┘
833 `
834 if !visualCheck(t, "MergeHierarchicalByColumnIndex", buf.String(), expected) {
835 t.Error(table.Debug())
836 }
837 }