Debian Codegraph @

pkg/color/helpers_test.go

raw
1 package color
2
3 import (
4 "bytes"
5 "strconv"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 )
11
12 func Test_colorPrint(t *testing.T) {
13 t.Parallel()
14
15 tests := []struct {
16 name string
17 format string
18 attribute Attribute
19 args []any
20 noColor bool
21 checkOutput func(*testing.T, string)
22 }{
23 {
24 name: "prints with foreground color red",
25 format: "hello",
26 attribute: FgRed,
27 args: nil,
28 noColor: false,
29 checkOutput: func(t *testing.T, output string) {
30 t.Helper()
31 require.NotEmpty(t, output)
32 assert.Contains(t, output, "hello")
33 },
34 },
35 {
36 name: "prints with foreground color green",
37 format: "world",
38 attribute: FgGreen,
39 args: nil,
40 noColor: false,
41 checkOutput: func(t *testing.T, output string) {
42 t.Helper()
43 require.NotEmpty(t, output)
44 assert.Contains(t, output, "world")
45 },
46 },
47 {
48 name: "prints with format and args",
49 format: "value: %d",
50 attribute: FgBlue,
51 args: []any{42},
52 noColor: false,
53 checkOutput: func(t *testing.T, output string) {
54 t.Helper()
55 require.NotEmpty(t, output)
56 assert.Contains(t, output, "value: 42")
57 },
58 },
59 {
60 name: "prints nothing when NoColor is set",
61 format: "test",
62 attribute: FgRed,
63 args: nil,
64 noColor: true,
65 checkOutput: func(t *testing.T, output string) {
66 t.Helper()
67 // When NoColor is set, the color codes should not be present
68 assert.NotContains(t, output, "\x1b[")
69 },
70 },
71 {
72 name: "adds newline if not present",
73 format: "test",
74 attribute: FgRed,
75 args: nil,
76 noColor: false,
77 checkOutput: func(t *testing.T, output string) {
78 t.Helper()
79 assert.NotEmpty(t, output)
80 },
81 },
82 }
83
84 for _, tt := range tests {
85 t.Run(tt.name, func(t *testing.T) {
86 t.Parallel()
87
88 // Create a test config with custom output buffer
89 buf := &bytes.Buffer{}
90 cfg := &Config{
91 NoColor: tt.noColor,
92 Output: buf,
93 Error: buf,
94 }
95
96 // Create color with config and use Fprintln for testing
97 c := NewWithConfig(cfg, tt.attribute)
98 switch {
99 case !tt.noColor && tt.args == nil:
100 _, _ = c.Fprintln(buf, tt.format)
101 case !tt.noColor:
102 _, _ = c.Fprintf(buf, tt.format+"\n", tt.args...)
103 default:
104 _, _ = buf.WriteString(tt.format + "\n")
105 }
106
107 output := buf.String()
108
109 tt.checkOutput(t, output)
110 })
111 }
112 }
113
114 func Test_colorString(t *testing.T) {
115 t.Parallel()
116
117 tests := []struct {
118 name string
119 format string
120 attribute Attribute
121 args []any
122 noColor bool
123 want string
124 }{
125 {
126 name: "returns colored string for red",
127 format: "hello",
128 attribute: FgRed,
129 args: nil,
130 noColor: false,
131 want: "",
132 },
133 {
134 name: "returns colored string for green",
135 format: "world",
136 attribute: FgGreen,
137 args: nil,
138 noColor: false,
139 want: "",
140 },
141 {
142 name: "returns colored string with format args",
143 format: "value: %d",
144 attribute: FgBlue,
145 args: []any{42},
146 noColor: false,
147 want: "",
148 },
149 {
150 name: "returns plain string when NoColor is set",
151 format: "test",
152 attribute: FgRed,
153 args: nil,
154 noColor: true,
155 want: "test",
156 },
157 {
158 name: "returns plain string with args when NoColor is set",
159 format: "value: %d",
160 attribute: FgRed,
161 args: []any{123},
162 noColor: true,
163 want: "value: 123",
164 },
165 }
166
167 for _, tt := range tests {
168 t.Run(tt.name, func(t *testing.T) {
169 t.Parallel()
170
171 // Create color with config
172 buf := &bytes.Buffer{}
173 cfg := &Config{
174 NoColor: tt.noColor,
175 Output: buf,
176 Error: buf,
177 }
178 c := NewWithConfig(cfg, tt.attribute)
179
180 var result string
181 if tt.args == nil {
182 result = c.SprintFunc()(tt.format)
183 } else {
184 result = c.SprintfFunc()(tt.format, tt.args...)
185 }
186
187 if tt.noColor {
188 // When NoColor is set, result should be plain
189 assert.Equal(t, tt.want, result)
190 } else {
191 // When color is enabled, result should contain escape codes
192 require.NotEmpty(t, result)
193
194 if tt.args == nil {
195 assert.Contains(t, result, tt.format)
196 } else {
197 assert.Contains(t, result, "value: 42")
198 }
199 }
200 })
201 }
202 }
203
204 // Test helper functions with various color attributes.
205 func Test_colorPrint_variousAttributes(t *testing.T) {
206 t.Parallel()
207
208 attributes := []Attribute{
209 FgBlack, FgRed, FgGreen, FgYellow, FgBlue, FgMagenta, FgCyan, FgWhite,
210 FgHiBlack, FgHiRed, FgHiGreen, FgHiYellow, FgHiBlue, FgHiMagenta, FgHiCyan, FgHiWhite,
211 BgBlack, BgRed, BgGreen, BgYellow, BgBlue, BgMagenta, BgCyan, BgWhite,
212 Bold, Faint, Italic, Underline, BlinkSlow, ReverseVideo, Concealed, CrossedOut,
213 }
214
215 for _, attr := range attributes {
216 t.Run("attribute_"+strconv.Itoa(int(attr)), func(t *testing.T) {
217 t.Parallel()
218
219 buf := &bytes.Buffer{}
220 cfg := &Config{
221 NoColor: false,
222 Output: buf,
223 Error: buf,
224 }
225 c := NewWithConfig(cfg, attr)
226 _, _ = c.Fprintln(buf, "test")
227
228 output := buf.String()
229 assert.NotEmpty(t, output, "expected output for attribute %v", attr)
230 })
231 }
232 }
233
234 func Test_colorString_variousAttributes(t *testing.T) {
235 t.Parallel()
236
237 attributes := []Attribute{
238 FgBlack, FgRed, FgGreen, FgYellow, FgBlue, FgMagenta, FgCyan, FgWhite,
239 Bold, Italic, Underline,
240 }
241
242 for _, attr := range attributes {
243 t.Run("attribute_"+strconv.Itoa(int(attr)), func(t *testing.T) {
244 t.Parallel()
245
246 buf := &bytes.Buffer{}
247 cfg := &Config{
248 NoColor: false,
249 Output: buf,
250 Error: buf,
251 }
252 c := NewWithConfig(cfg, attr)
253 result := c.SprintFunc()("test")
254
255 assert.NotEmpty(t, result, "expected output for attribute %v", attr)
256 assert.Contains(t, result, "test", "result should contain the input string for attribute %v", attr)
257 })
258 }
259 }