-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcolor_test.go
332 lines (301 loc) · 7.77 KB
/
color_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package color
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"testing"
)
type helperFunc func(fmt string, a ...interface{})
type helperFuncString func(fmt string, a ...interface{}) string
func newMockConsole(out io.Writer) *Console {
return &Console{
current: out,
}
}
func assertEqualS(t *testing.T, want, got string) {
t.Helper()
if got != want {
t.Logf("got %q", got)
t.Logf("want %q", want)
t.Fatal("mismatch")
}
}
func TestColorEnabled(t *testing.T) {
t.Parallel()
c := New(FgRed)
got := c.Sprint("foo")
want := fmt.Sprintf("%s%smfoo%s", escape, attributeToSGRCode[FgRed], colorReset)
assertEqualS(t, want, got)
got = c.Sprintf("foo %d", 2)
want = fmt.Sprintf("%s%smfoo 2%s", escape, attributeToSGRCode[FgRed], colorReset)
assertEqualS(t, want, got)
got = c.Sprintln("foo")
want = fmt.Sprintf("%s%smfoo%s\n", escape, attributeToSGRCode[FgRed], colorReset)
assertEqualS(t, want, got)
}
func TestColorDisabled(t *testing.T) {
// can't be parallel since we're toggling global color which will interfere with concurrently running tests
Disable(true)
defer func() {
Disable(false)
}()
c := New(FgRed)
got := c.Sprint("foo")
want := fmt.Sprint("foo")
assertEqualS(t, want, got)
got = c.Sprintf("foo %d", 3)
want = fmt.Sprintf("foo %d", 3)
assertEqualS(t, want, got)
got = c.Sprintln("foo")
want = fmt.Sprintln("foo")
assertEqualS(t, want, got)
}
func TestColor(t *testing.T) {
t.Parallel()
tt := []struct {
text string
code Attribute
}{
{"black", FgBlack},
{text: "red", code: FgRed},
{text: "green", code: FgGreen},
{text: "yellow", code: FgYellow},
{text: "blue", code: FgBlue},
{text: "magent", code: FgMagenta},
{text: "cyan", code: FgCyan},
{text: "white", code: FgWhite},
{text: "hblack", code: FgHiBlack},
{text: "hred", code: FgHiRed},
{text: "hgreen", code: FgHiGreen},
{text: "hyellow", code: FgHiYellow},
{text: "hblue", code: FgHiBlue},
{text: "hmagent", code: FgHiMagenta},
{text: "hcyan", code: FgHiCyan},
{text: "hwhite", code: FgHiWhite},
}
for _, tc := range tt {
t.Run(tc.text, func(t *testing.T) {
t.Run(tc.text+" print", func(t *testing.T) {
cache().clear()
var buff bytes.Buffer
cons := newMockConsole(&buff)
f := cons.PrintFunc(New(tc.code))
f(tc.text)
got := buff.String()
t.Log(got)
want := fmt.Sprintf("%s%sm%s%s0m", escape, tc.code, tc.text, escape)
if got != want {
t.Logf("got %q", got)
t.Logf("want %q", want)
t.Fatal()
}
})
t.Run(tc.text+" printf", func(t *testing.T) {
cache().clear()
var buff bytes.Buffer
cons := newMockConsole(&buff)
f := cons.PrintfFunc(New(tc.code))
f(tc.text)
got := buff.String()
t.Log(got)
want := fmt.Sprintf("%s%sm%s%s0m", escape, tc.code, tc.text, escape)
if got != want {
t.Logf("got %q", got)
t.Logf("want %q", want)
t.Fatal()
}
})
t.Run(tc.text+" println", func(t *testing.T) {
cache().clear()
var buff bytes.Buffer
cons := newMockConsole(&buff)
f := cons.PrintlnFunc(New(tc.code))
f(tc.text)
got := buff.String()
t.Log(got)
want := fmt.Sprintf("%s%sm%s%s0m\n", escape, tc.code, tc.text, escape)
if got != want {
t.Logf("got %q", got)
t.Logf("want %q", want)
t.Fatal()
}
})
})
}
}
func TestMultiAttribute(t *testing.T) {
t.Parallel()
cache().clear()
var buff bytes.Buffer
cons := newMockConsole(&buff)
col := New(FgWhite, Bold, Underline)
_, _ = cons.Print(col, "bold white")
want := "\x1b[37;1;4mbold white\x1b[0m"
got := buff.String()
t.Log(got)
if got != want {
t.Fatalf("want %q got %q", want, got)
}
}
func TestMissingAttribute(t *testing.T) {
t.Parallel()
cache().clear()
var buff bytes.Buffer
cons := newMockConsole(&buff)
col := New()
_, _ = cons.Print(col, "no color")
want := "\x1b[0mno color\x1b[0m"
got := buff.String()
t.Log(got)
if got != want {
t.Fatalf("want %q got %q", want, got)
}
}
func TestStringHelperFuncs(t *testing.T) {
t.Parallel()
tt := []struct {
code Attribute
test helperFuncString
}{
{FgBlack, BlackString},
{FgRed, RedString},
{FgGreen, GreenString},
{FgYellow, YellowString},
{FgBlue, BlueString},
{FgMagenta, MagentaString},
{FgCyan, CyanString},
{FgHiBlack, HiBlackString},
{FgHiRed, HiRedString},
{FgHiGreen, HiGreenString},
{FgHiYellow, HiYellowString},
{FgHiBlue, HiBlueString},
{FgHiMagenta, HiMagentaString},
{FgHiCyan, HiCyanString},
{FgHiWhite, HiWhiteString},
{FgWhite, WhiteString},
}
for _, tc := range tt {
t.Run(tc.code.String(), func(t *testing.T) {
want := fmt.Sprintf("\x1b[%smcolor - %q\x1b[0m", tc.code, tc.code.Name())
got := tc.test("color - %q", tc.code.Name())
assertEqualS(t, want, got)
})
}
}
func TestHelperStdoutFuncs(t *testing.T) {
t.Parallel()
tt := []struct {
code Attribute
testStdout helperFunc
testStdErr helperFunc
}{
{FgBlack, Black, BlackE},
{FgRed, Red, RedE},
{FgGreen, Green, GreenE},
{FgYellow, Yellow, YellowE},
{FgBlue, Blue, BlueE},
{FgMagenta, Magenta, MagentaE},
{FgCyan, Cyan, CyanE},
{FgWhite, White, WhiteE},
{FgHiBlack, HiBlack, HiBlackE},
{FgHiRed, HiRed, HiRedE},
{FgHiGreen, HiGreen, HiGreenE},
{FgHiYellow, HiYellow, HiYellowE},
{FgHiBlue, HiBlue, HiBlueE},
{FgHiMagenta, HiMagenta, HiMagentaE},
{FgHiCyan, HiCyan, HiCyanE},
{FgHiWhite, HiWhite, HiWhiteE},
}
for _, tc := range tt {
want := fmt.Sprintf("\x1b[%smcolor - %q\x1b[0m\n", tc.code, tc.code.Name())
t.Run(tc.code.String()+"_stdout", func(t *testing.T) {
var buff bytes.Buffer
cons := Stdout()
oldWriter := cons.current
cons.current = &buff
defer func() {
cons.current = oldWriter
}()
tc.testStdout("color - %q", tc.code.Name())
t.Log(buff.String())
assertEqualS(t, want, buff.String())
})
t.Run(tc.code.String()+"_stderr", func(t *testing.T) {
var buff bytes.Buffer
cons := Stderr()
oldWriter := cons.current
cons.current = &buff
defer func() {
cons.current = oldWriter
}()
tc.testStdErr("color - %q", tc.code.Name())
assertEqualS(t, want, buff.String())
})
}
}
func BenchmarkColorFuncs(b *testing.B) {
cons := Stdout()
oldWriter := cons.current
cons.current = ioutil.Discard
defer func() {
cons.current = oldWriter
}()
for i := 0; i < b.N; i++ {
Black("hello from %s", "black")
Green("hello from %s", "green")
Red("hello from %q. i'm %d", "red", 23)
}
}
func BenchmarkColorFuncsParallel(b *testing.B) {
cons := Stdout()
oldWriter := cons.current
cons.current = ioutil.Discard
defer func() {
cons.current = oldWriter
}()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Black("hello from %s xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "black")
Green("hello from %s yyyyyhfhhdhehehehehhskdkdkdkdkdkdkdkkkekkekekekeekekkk", "green")
Red("hello from %q. i'm %d", "red", 23)
Black("more black stuff")
Green("hello from %s xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "green")
Red("hello from %q. i'm %d", "red", 23)
}
})
}
func BenchmarkColorStruct(b *testing.B) {
attrs := []Attribute{
FgBlack,
FgRed,
FgGreen,
FgYellow,
FgBlue,
FgMagenta,
FgCyan,
FgWhite,
}
cons := newMockConsole(ioutil.Discard)
cache().clear()
for i := 0; i < b.N; i++ {
for i := 0; i < 100; i++ {
col := New(attrs...)
cons.Print(col, "jjjjjjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjfjjf")
}
}
}
func ExampleRed() {
// Print some red text.
Red("Hello red!")
}
func ExampleConsole_Println() {
// Output underlined white text to stdout.
clr := New(FgWhite, Underline)
_, _ = Stdout().Println(clr, "I'm underlined and white!")
}
func ExampleColor_SprintFunc() {
// Create functions that add color information
emphasized := New(FgRed, Bold, Underline).SprintFunc()
fmt.Println("Wow, this is", emphasized("exciting!"))
}