Debian Codegraph @

tests/rgb_test.go

raw
1 package tests
2
3 import (
4 "bytes"
5 "strings"
6 "testing"
7
8 "github.com/fatih/color"
9 "github.com/olekukonko/tablewriter"
10 "github.com/olekukonko/tablewriter/renderer"
11 "github.com/olekukonko/tablewriter/tw"
12 )
13
14 // withColor forces fatih/color output on for the duration of a test, since the
15 // test output is not a TTY and color is otherwise disabled.
16 func withColor(t *testing.T) {
17 t.Helper()
18 prev := color.NoColor
19 color.NoColor = false
20 t.Cleanup(func() { color.NoColor = prev })
21 }
22
23 func TestRGBApply(t *testing.T) {
24 withColor(t)
25
26 // Apply wraps the text in an opening SGR sequence and a reset. The opening
27 // sequence is what these helpers construct; the exact reset is produced by
28 // fatih/color, so the tests assert the opening sequence and that the text
29 // itself is preserved.
30 tests := []struct {
31 name string
32 tint renderer.Tint
33 wantOpen string
34 }{
35 {
36 name: "foreground",
37 tint: renderer.Tint{FG: renderer.RGB(255, 128, 0)},
38 wantOpen: "\x1b[38;2;255;128;0m",
39 },
40 {
41 name: "background",
42 tint: renderer.Tint{BG: renderer.BgRGB(0, 0, 0)},
43 wantOpen: "\x1b[48;2;0;0;0m",
44 },
45 {
46 name: "foreground and background",
47 tint: renderer.Tint{FG: renderer.RGB(255, 128, 0), BG: renderer.BgRGB(0, 0, 0)},
48 wantOpen: "\x1b[38;2;255;128;0;48;2;0;0;0m",
49 },
50 {
51 name: "combined with attribute via append",
52 tint: renderer.Tint{FG: append(renderer.RGB(255, 128, 0), color.Bold)},
53 wantOpen: "\x1b[38;2;255;128;0;1m",
54 },
55 }
56
57 for _, tc := range tests {
58 t.Run(tc.name, func(t *testing.T) {
59 got := tc.tint.Apply("hi")
60 if !strings.HasPrefix(got, tc.wantOpen) {
61 t.Errorf("Apply() = %q, want prefix %q", got, tc.wantOpen)
62 }
63 if stripped := StripColors(got); stripped != "hi" {
64 t.Errorf("Apply() stripped = %q, want %q", stripped, "hi")
65 }
66 })
67 }
68 }
69
70 func TestRGBClamping(t *testing.T) {
71 tests := []struct {
72 name string
73 r, g, b int
74 wantR, wantG, wantB color.Attribute
75 }{
76 {"in range", 10, 20, 30, 10, 20, 30},
77 {"upper bound", 255, 255, 255, 255, 255, 255},
78 {"lower bound", 0, 0, 0, 0, 0, 0},
79 {"above max clamps to 255", 300, 256, 999, 255, 255, 255},
80 {"below min clamps to 0", -1, -50, -999, 0, 0, 0},
81 }
82
83 for _, tc := range tests {
84 t.Run(tc.name, func(t *testing.T) {
85 got := renderer.RGB(tc.r, tc.g, tc.b)
86 want := renderer.Colors{38, 2, tc.wantR, tc.wantG, tc.wantB}
87 if !colorsEqual(got, want) {
88 t.Errorf("RGB(%d,%d,%d) = %v, want %v", tc.r, tc.g, tc.b, got, want)
89 }
90 })
91 }
92 }
93
94 func TestHex(t *testing.T) {
95 tests := []struct {
96 name string
97 in string
98 want renderer.Colors
99 wantErr bool
100 }{
101 {"full with hash", "#FF8000", renderer.Colors{38, 2, 255, 128, 0}, false},
102 {"full without hash", "FF8000", renderer.Colors{38, 2, 255, 128, 0}, false},
103 {"lowercase", "#ff8000", renderer.Colors{38, 2, 255, 128, 0}, false},
104 {"shorthand", "#f80", renderer.Colors{38, 2, 255, 136, 0}, false},
105 {"surrounding space", " #FF8000 ", renderer.Colors{38, 2, 255, 128, 0}, false},
106 {"black", "#000000", renderer.Colors{38, 2, 0, 0, 0}, false},
107 {"white", "#ffffff", renderer.Colors{38, 2, 255, 255, 255}, false},
108 {"empty", "", nil, true},
109 {"too short", "#12", nil, true},
110 {"wrong length", "#12345", nil, true},
111 {"non-hex digit", "#gggggg", nil, true},
112 }
113
114 for _, tc := range tests {
115 t.Run(tc.name, func(t *testing.T) {
116 got, err := renderer.Hex(tc.in)
117 if (err != nil) != tc.wantErr {
118 t.Fatalf("Hex(%q) error = %v, wantErr = %v", tc.in, err, tc.wantErr)
119 }
120 if !colorsEqual(got, tc.want) {
121 t.Errorf("Hex(%q) = %v, want %v", tc.in, got, tc.want)
122 }
123 })
124 }
125 }
126
127 func TestBgHex(t *testing.T) {
128 got, err := renderer.BgHex("#FF8000")
129 if err != nil {
130 t.Fatalf("BgHex returned error: %v", err)
131 }
132 want := renderer.Colors{48, 2, 255, 128, 0}
133 if !colorsEqual(got, want) {
134 t.Errorf("BgHex(#FF8000) = %v, want %v", got, want)
135 }
136 }
137
138 // TestRGBColorizedTable checks that RGB tints survive rendering through the
139 // colorized renderer end to end.
140 func TestRGBColorizedTable(t *testing.T) {
141 withColor(t)
142
143 var buf bytes.Buffer
144 table := tablewriter.NewTable(&buf,
145 tablewriter.WithRenderer(renderer.NewColorized(renderer.ColorizedConfig{
146 Borders: tw.Border{Left: tw.Off, Right: tw.Off, Top: tw.Off, Bottom: tw.Off},
147 Header: renderer.Tint{FG: renderer.RGB(255, 128, 0)},
148 Column: renderer.Tint{FG: renderer.RGB(0, 200, 100)},
149 })),
150 )
151 table.Header([]string{"Name"})
152 table.Append([]string{"Alice"})
153 table.Render()
154
155 out := buf.String()
156 if !strings.Contains(out, "\x1b[38;2;255;128;0m") {
157 t.Errorf("expected header RGB sequence in output, got:\n%q", out)
158 }
159 if !strings.Contains(out, "\x1b[38;2;0;200;100m") {
160 t.Errorf("expected column RGB sequence in output, got:\n%q", out)
161 }
162 if stripped := StripColors(out); !strings.Contains(stripped, "Alice") {
163 t.Errorf("expected cell content preserved, got:\n%q", stripped)
164 }
165 }
166
167 func colorsEqual(a, b renderer.Colors) bool {
168 if len(a) != len(b) {
169 return false
170 }
171 for i := range a {
172 if a[i] != b[i] {
173 return false
174 }
175 }
176 return true
177 }