-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermcols_test.go
198 lines (191 loc) · 4.87 KB
/
termcols_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
package termcols
import (
"testing"
)
// NOTE: benchResult is added to avoid compiler optimization
var (
benchResult string
)
// BenchmarkColorize was arranged to test the performance of the slice type
// conversion using unsafe.Pointer as opposed to looping over the source
// slice and appending elements one after another to the target slice.
//
// In my testing slice type conversion with unsafe.Pointer is almost four
// times faster than the memory-safe alternative.
func BenchmarkColorize(b *testing.B) {
var s string
for i := 0; i < b.N; i++ {
s = Colorize("Colorize me!", BlackFg)
}
benchResult = s
}
func TestColorize(t *testing.T) {
cases := []struct {
attrs []SgrAttr
expOut string
}{
{
[]SgrAttr{},
" Colorize me! ",
},
{
[]SgrAttr{Bold, BlackFg, WhiteBbg},
"\033[1m\033[30m\033[107m Colorize me! \033[0m",
},
{
[]SgrAttr{Faint, RedFg, CyanBbg},
"\033[2m\033[31m\033[106m Colorize me! \033[0m",
},
{
[]SgrAttr{Italic, GreenFg, MagentaBbg},
"\033[3m\033[32m\033[105m Colorize me! \033[0m",
},
{
[]SgrAttr{Underline, YellowFg, BlueBbg},
"\033[4m\033[33m\033[104m Colorize me! \033[0m",
},
{
[]SgrAttr{Blink, BlueFg, YellowBbg},
"\033[5m\033[34m\033[103m Colorize me! \033[0m",
},
{
[]SgrAttr{Reverse, MagentaFg, GreenBbg},
"\033[7m\033[35m\033[102m Colorize me! \033[0m",
},
{
[]SgrAttr{Hide, CyanFg, RedBbg},
"\033[8m\033[36m\033[101m Colorize me! \033[0m",
},
{
[]SgrAttr{Strike, WhiteFg, BlackBbg},
"\033[9m\033[37m\033[100m Colorize me! \033[0m",
},
{
[]SgrAttr{DefaultStyle, DefaultFg, BlackBbg},
"\033[10m\033[39m\033[100m Colorize me! \033[0m",
},
{
[]SgrAttr{Bold, BlackBfg, DefaultBg},
"\033[1m\033[90m\033[49m Colorize me! \033[0m",
},
{
[]SgrAttr{Faint, RedBfg, WhiteBg},
"\033[2m\033[91m\033[47m Colorize me! \033[0m",
},
{
[]SgrAttr{Italic, GreenBfg, CyanBg},
"\033[3m\033[92m\033[46m Colorize me! \033[0m",
},
{
[]SgrAttr{Underline, YellowBfg, MagentaBg},
"\033[4m\033[93m\033[45m Colorize me! \033[0m",
},
{
[]SgrAttr{Blink, BlueBfg, YellowBg},
"\033[5m\033[94m\033[43m Colorize me! \033[0m",
},
{
[]SgrAttr{Reverse, MagentaBfg, BlueBg},
"\033[7m\033[95m\033[44m Colorize me! \033[0m",
},
{
[]SgrAttr{Hide, CyanBfg, GreenBg},
"\033[8m\033[96m\033[42m Colorize me! \033[0m",
},
{
[]SgrAttr{Strike, WhiteBfg, RedBg},
"\033[9m\033[97m\033[41m Colorize me! \033[0m",
},
{
[]SgrAttr{DefaultStyle, WhiteBfg, BlackBg},
"\033[10m\033[97m\033[40m Colorize me! \033[0m",
},
}
for _, c := range cases {
t.Run(c.expOut, func(t *testing.T) {
if out := Colorize(" Colorize me! ", c.attrs...); out != c.expOut {
t.Errorf("Have: %s, want: %s", out, c.expOut)
}
})
}
}
func TestRgb8(t *testing.T) {
cases := []struct {
l Layer
col uint8
expOut SgrAttr
}{
{FG, 12, SgrAttr("\033[38;5;12m")},
{BG, 82, SgrAttr("\033[48;5;82m")},
{FG, 32, SgrAttr("\033[38;5;32m")},
{BG, 180, SgrAttr("\033[48;5;180m")},
{FG, 255, SgrAttr("\033[38;5;255m")},
{BG, 234, SgrAttr("\033[48;5;234m")},
}
for _, c := range cases {
t.Run(string(c.expOut), func(t *testing.T) {
if out := Rgb8(c.l, c.col); out != c.expOut {
t.Errorf("Have: %s, want: %s", out, c.expOut)
}
})
}
}
func TestRgb24(t *testing.T) {
cases := []struct {
l Layer
r, g, b uint8
expOut SgrAttr
}{
{FG, 12, 145, 67, SgrAttr("\033[38;2;12;145;67m")},
{BG, 200, 247, 23, SgrAttr("\033[48;2;200;247;23m")},
{FG, 89, 12, 2, SgrAttr("\033[38;2;89;12;2m")},
{BG, 150, 73, 0, SgrAttr("\033[48;2;150;73;0m")},
{FG, 0, 255, 255, SgrAttr("\033[38;2;0;255;255m")},
{BG, 12, 59, 90, SgrAttr("\033[48;2;12;59;90m")},
}
for _, c := range cases {
t.Run(string(c.expOut), func(t *testing.T) {
if out := Rgb24(c.l, c.r, c.g, c.b); out != c.expOut {
t.Errorf("Have: %s, want: %s", out, c.expOut)
}
})
}
}
func TestCombination(t *testing.T) {
cases := []struct {
attrs []SgrAttr
expOut string
}{
{
[]SgrAttr{Italic, GreenFg, Rgb8(BG, 176)},
"\033[3m\033[32m\033[48;5;176m Colorize me! \033[0m",
},
{
[]SgrAttr{Underline, Rgb8(FG, 44), MagentaBg},
"\033[4m\033[38;5;44m\033[45m Colorize me! \033[0m",
},
{
[]SgrAttr{Reverse, MagentaBfg, Rgb8(BG, 255)},
"\033[7m\033[95m\033[48;5;255m Colorize me! \033[0m",
},
{
[]SgrAttr{Bold, Rgb24(FG, 12, 45, 62), Rgb8(BG, 89)},
"\033[1m\033[38;2;12;45;62m\033[48;5;89m Colorize me! \033[0m",
},
{
[]SgrAttr{Strike, Rgb24(FG, 78, 22, 0), BlackBbg},
"\033[9m\033[38;2;78;22;0m\033[100m Colorize me! \033[0m",
},
{
[]SgrAttr{Hide, CyanBfg, Rgb24(BG, 255, 255, 255)},
"\033[8m\033[96m\033[48;2;255;255;255m Colorize me! \033[0m",
},
}
for _, c := range cases {
t.Run(c.expOut, func(t *testing.T) {
if out := Colorize(" Colorize me! ", c.attrs...); out != c.expOut {
t.Errorf("Have: %s, want: %s", out, c.expOut)
}
})
}
}