1// Note(alexwlchan, 2026-11-04): This file was retrieved from
2// https://cs.opensource.google/go/x/perf/+/be2c69fb:cmd/benchstat/internal/texttab/table_test.go
4// Copyright 2020 The Go Authors. All rights reserved.
5// Use of this source code is governed by a BSD-style
6// license that can be found in the LICENSE file.
15func TestAlign(t *testing.T) {
16 check := func(s string, a align, w int, want string) {
20 t.Errorf("want %q, got %q", want, got)
24 check("abc", alignLeft, 10, "abc")
25 check("abc", alignCenter, 10, " abc")
26 check("abc", alignCenter, 11, " abc")
27 check("abc", alignRight, 10, " abc")
28 check("☃", alignRight, 4, " ☃")
31func TestTable(t *testing.T) {
33 check := func(want string) {
35 var gotBuf strings.Builder
37 got := gotBuf.String()
39 t.Errorf("want:\n%sgot:\n%s", want, got)
46 tab.Row().Cell("a").Cell("b").Cell("c")
47 tab.Row().Cell("d").Cell("e").Cell("f")
48 check("a b c\nd e f\n")
50 // Basic cell padding. Also checks that we don't print
51 // unnecessary spaces at the ends of lines.
52 tab.Row().Cell("a").Cell("b").Cell("c")
53 tab.Row().Cell("long").Cell("e").Cell("long")
54 check("a b c\nlong e long\n")
57 tab.Row().Cell("a", Left).Cell("b", Center).Cell("c", Right)
58 tab.Row().Cell("xxx").Cell("xxx").Cell("xxx")
59 check("a b c\nxxx xxx xxx\n")
62 tab.Row().Cell("a").Cell("b", LeftMargin(" "))
63 tab.Row().Cell("c").Cell("d")
64 tab.Row().Cell("e").Cell("f", LeftMargin("|"))
65 check("a b\nc d\ne |f\n")
67 // Missing cell in the middle.
68 tab.Row().Cell("a").Col(2).Cell("c")
69 tab.Row().Cell("d").Cell("e").Cell("f")
72 // Missing cells at the end.
74 tab.Row().Cell("d").Cell("e").Cell("f")
85 tab.Row().Cell("a").Cell("b")
86 tab.Row().Span(2, "abc")
89 // Spans expanding other cells.
90 tab.Row().Cell("a").Cell("b")
91 tab.Row().Span(2, "abcdefg")
92 check("a b\nabcdefg\n")
94 // Other cells expanding spans.
95 tab.Row().Cell("abc").Cell("def")
96 tab.Row().Span(2, "a", Right)
97 check("abc def\n a\n")
99 // Some cells are already large enough to complete the span.
100 tab.Row().Cell("a").Cell("def")
101 tab.Row().Span(2, "abdef", Right)
102 check("a def\nabdef\n")
104 // Larger cells are sufficient, but smaller cells need to be
106 tab.Row().Cell("a").Cell("def")
107 tab.Row().Span(2, "abcdef", Right)
108 check("a def\nabcdef\n")
110 // Spans with margins.
111 tab.Row().Cell("a").Cell("b", LeftMargin(" ")).Cell("x")
112 tab.Row().Span(2, "a__b").Cell("x")
113 check("a b x\na__b x\n")
116 tab.Row().Span(2, "abcdef")
117 tab.Row().Cell("a").Cell("bc")
118 tab.Row().Cell("x").Cell("y")
119 tab.SetShrink(1, true)
120 check("abcdef\na bc\nx y\n")