Skip to main content

internal/texttab/table_test.go

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.
8package texttab
10import (
11 "strings"
12 "testing"
15func TestAlign(t *testing.T) {
16 check := func(s string, a align, w int, want string) {
17 t.Helper()
18 got := a.lpad(s, w)
19 if got != want {
20 t.Errorf("want %q, got %q", want, got)
21 }
22 }
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) {
32 var tab Table
33 check := func(want string) {
34 t.Helper()
35 var gotBuf strings.Builder
36 tab.Format(&gotBuf)
37 got := gotBuf.String()
38 if want != got {
39 t.Errorf("want:\n%sgot:\n%s", want, got)
40 }
41 // Reset tab.
42 tab = Table{}
43 }
45 // Basic test.
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")
56 // Cell alignment.
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")
61 // Margins.
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")
70 check("a c\nd e f\n")
72 // Missing cells at the end.
73 tab.Row().Cell("a")
74 tab.Row().Cell("d").Cell("e").Cell("f")
75 check("a\nd e f\n")
77 // Blank rows.
78 tab.Row().Cell("a")
79 tab.Row()
80 tab.Row()
81 tab.Row().Cell("b")
82 check("a\n\n\nb\n")
84 // Basic spans.
85 tab.Row().Cell("a").Cell("b")
86 tab.Row().Span(2, "abc")
87 check("a b\nabc\n")
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
105 // expanded.
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")
115 // Shrink columns.
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")