/
utils_test.go
341 lines (305 loc) · 7.76 KB
/
utils_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
333
334
335
336
337
338
339
340
341
package color
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUtilFuncs(t *testing.T) {
is := assert.New(t)
// IsConsole
is.True(IsConsole(os.Stdin))
is.True(IsConsole(os.Stdout))
is.True(IsConsole(os.Stderr))
is.False(IsConsole(&bytes.Buffer{}))
ff, err := os.OpenFile("README.md", os.O_WRONLY, 0)
is.NoError(err)
is.False(IsConsole(ff))
// IsMSys
oldVal := os.Getenv("MSYSTEM")
is.NoError(os.Setenv("MSYSTEM", "MINGW64"))
is.True(IsMSys())
is.NoError(os.Unsetenv("MSYSTEM"))
is.False(IsMSys())
_ = os.Setenv("MSYSTEM", oldVal)
is.NotEmpty(TermColorLevel())
// is.NotEmpty(SupColorMark())
}
func TestDetectColorLevel(t *testing.T) {
is := assert.New(t)
// "COLORTERM=truecolor"
mockOsEnvByText("COLORTERM=truecolor", func() {
is.True(IsSupportColor())
is.Equal(LevelRgb, DetectColorLevel())
is.True(IsSupportRGBColor())
is.True(IsSupportTrueColor())
})
// TERMINAL_EMULATOR=JetBrains-JediTerm
mockOsEnvByText(`
TERM=xterm-256color
TERMINAL_EMULATOR=JetBrains-JediTerm
ZSH_TMUX_TERM=screen-256color
`, func() {
is.Equal(LevelRgb, DetectColorLevel())
is.True(IsSupportRGBColor())
is.True(IsSupport16Color())
is.True(IsSupportColor())
})
}
func TestIsDetectColorLevel_unix(t *testing.T) {
if IsWindows() {
return
}
is := assert.New(t)
mockOsEnvByText("TERM=screen-256color", func() {
is.Equal(Level256, DetectColorLevel())
is.False(IsSupportTrueColor())
is.True(IsSupport256Color())
is.True(IsSupportColor())
})
// TERM_PROGRAM=Terminus
mockOsEnvByText(`
TERMINUS_PLUGINS=
TERM=xterm-256color
TERM_PROGRAM=Terminus
ZSH_TMUX_TERM=screen-256color
`, func() {
is.Equal(LevelRgb, DetectColorLevel())
is.True(IsSupportRGBColor())
is.True(IsSupport16Color())
is.True(IsSupportColor())
})
// -------- tests on macOS ---------
// TERM_PROGRAM=Apple_Terminal
mockOsEnvByText(`
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
TERM_PROGRAM_VERSION=433
TERM_SESSION_ID=F17907FE-DCA5-488D-829B-7AFA8B323753
ZSH_TMUX_TERM=screen-256color
`, func() {
// fmt.Println(os.Environ())
is.Equal(Level256, DetectColorLevel())
is.False(IsSupportTrueColor())
is.True(IsSupport256Color())
is.True(IsSupport16Color())
is.True(IsSupportColor())
})
// TERM_PROGRAM=iTerm.app
mockOsEnvByText(`
LC_TERMINAL_VERSION=3.4.5beta1
ITERM_PROFILE=Default
TERM_PROGRAM_VERSION=3.4.5beta1
TERM_PROGRAM=iTerm.app
LC_TERMINAL=iTerm2
COLORTERM=truecolor
TERM=xterm-256color
ITERM_SESSION_ID=w0t2p0:3A53303E-BD72-4F1D-897D-EC15E3B4FDB5
ZSH_TMUX_TERM=screen-256color
`, func() {
is.Equal(LevelRgb, DetectColorLevel())
is.True(IsSupportTrueColor())
is.True(IsSupport256Color())
is.True(IsSupport16Color())
is.True(IsSupportColor())
})
// -------- tests on linux ---------
}
func TestIsDetectColorLevel_screen(t *testing.T) {
if IsWindows() {
return
}
is := assert.New(t)
// TERM_PROGRAM=Apple_Terminal use screen
mockOsEnvByText(`
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
TERM_PROGRAM_VERSION=433
TERM_SESSION_ID=F17907FE-DCA5-488D-829B-7AFA8B323753
ZSH_TMUX_TERM=screen-256color
`, func() {
// fmt.Println(os.Environ())
is.Equal(Level256, DetectColorLevel())
is.False(IsSupportTrueColor())
is.True(IsSupport256Color())
is.True(IsSupport16Color())
is.True(IsSupportColor())
})
// TERM_PROGRAM=iTerm.app use screen
mockOsEnvByText(`
TERM=screen
TERMCAP=SC|screen|VT 100/ANSI X3.64 virtual terminal:\
TERM_SESSION_ID=w0t2p0:3A53303E-BD72-4F1D-897D-EC15E3B4FDB5
LC_TERMINAL_VERSION=3.4.5beta1
ITERM_PROFILE=Default
TERM_PROGRAM_VERSION=3.4.5beta1
TERM_PROGRAM=iTerm.app
LC_TERMINAL=iTerm2
COLORTERM=truecolor
ITERM_SESSION_ID=w0t2p0:3A53303E-BD72-4F1D-897D-EC15E3B4FDB5
ZSH_TMUX_TERM=screen
`, func() {
is.Equal(Level256, DetectColorLevel())
is.False(IsSupportTrueColor())
is.True(IsSupport256Color())
is.True(IsSupport16Color())
is.True(IsSupportColor())
})
// TERM_PROGRAM=Terminus use screen
mockOsEnvByText(`
TERM=screen
TERMCAP=SC|screen|VT 100/ANSI X3.64 virtual terminal:\
TERMINUS_PLUGINS=
TERM_PROGRAM=Terminus
ZSH_TMUX_TERM=screen
`, func() {
is.Equal(Level256, DetectColorLevel())
is.False(IsSupportTrueColor())
is.True(IsSupport256Color())
is.True(IsSupport16Color())
is.True(IsSupportColor())
})
// TERMINAL_EMULATOR=JetBrains-JediTerm use screen
mockOsEnvByText(`
TERM=screen
TERMCAP=SC|screen|VT 100/ANSI X3.64 virtual terminal:\
TERMINAL_EMULATOR=JetBrains-JediTerm
ZSH_TMUX_TERM=screen
`, func() {
is.Equal(Level256, DetectColorLevel())
is.False(IsSupportTrueColor())
is.True(IsSupport256Color())
is.True(IsSupport16Color())
is.True(IsSupportColor())
})
}
func TestIsDetectColorLevel_win(t *testing.T) {
if !IsWindows() {
return
}
is := assert.New(t)
// ConEmuANSI
mockEnvValue("ConEmuANSI", "ON", func(_ string) {
is.Equal(LevelRgb, DetectColorLevel())
is.True(IsSupportColor())
is.True(IsSupport256Color())
is.True(IsSupportTrueColor())
})
// WSL_DISTRO_NAME=Debian
mockEnvValue("WSL_DISTRO_NAME", "Debian", func(_ string) {
is.True(IsSupportColor())
})
// ANSICON
mockEnvValue("ANSICON", "189x2000 (189x43)", func(_ string) {
is.True(IsSupportColor())
// is.Equal(Level256, DetectColorLevel())
// is.Equal("TERM=xterm-256color", SupColorMark())
})
}
func TestRgbTo256Table(t *testing.T) {
index := 0
for hex, c256 := range RgbTo256Table() {
Hex(hex).Print("RGB:", hex)
fmt.Print(" = ")
C256(c256).Print("C256:", c256)
fmt.Print(" | ")
index++
if index%5 == 0 {
fmt.Println()
}
}
fmt.Println()
}
func TestC256ToRgbV1(t *testing.T) {
for i := 0; i < 256; i++ {
c256 := uint8(i)
C256(c256).Printf("C256:%d", c256)
fmt.Print(" => ")
rgb := C256ToRgbV1(c256)
RGBFromSlice(rgb).Printf("RGB:%v | ", rgb)
// assert.Equal(t, item.want, rgb, fmt.Sprint("256 code:", c256))
if i%4 == 0 {
fmt.Println()
}
}
fmt.Println()
}
func TestC256ToRgb(t *testing.T) {
for i := 0; i < 256; i++ {
c256 := uint8(i)
C256(c256).Printf("C256:%d", c256)
fmt.Print(" => ")
rgb := C256ToRgb(c256)
RGBFromSlice(rgb).Printf("RGB:%v | ", rgb)
// assert.Equal(t, item.want, rgb, fmt.Sprint("256 code:", c256))
if i%4 == 0 {
fmt.Println()
}
}
fmt.Println()
}
func TestHexToRgb(t *testing.T) {
tests := []struct {
given string
want []int
}{
{"666", []int{102, 102, 102}},
{"ccc", []int{204, 204, 204}},
{"#abc", []int{170, 187, 204}},
{"#aa99cd", []int{170, 153, 205}},
}
for _, item := range tests {
assert.Equal(t, HexToRgb(item.given), item.want)
assert.Equal(t, HexToRGB(item.given), item.want)
assert.Equal(t, Hex2rgb(item.given), item.want)
}
assert.Len(t, HexToRgb(""), 0)
assert.Len(t, HexToRgb("13"), 0)
}
func TestRgbToHex(t *testing.T) {
tests := []struct {
want string
given []int
}{
{"666666", []int{102, 102, 102}},
{"cccccc", []int{204, 204, 204}},
{"aabbcc", []int{170, 187, 204}},
{"aa99cd", []int{170, 153, 205}},
}
for _, item := range tests {
assert.Equal(t, RgbToHex(item.given), item.want)
assert.Equal(t, Rgb2hex(item.given), item.want)
}
}
func TestRgbToAnsi(t *testing.T) {
tests := []struct {
want uint8
rgb []uint8
isBg bool
}{
{40, []uint8{102, 102, 102}, true},
{37, []uint8{204, 204, 204}, false},
{47, []uint8{170, 78, 204}, true},
{37, []uint8{170, 153, 245}, false},
{30, []uint8{127, 127, 127}, false},
{40, []uint8{127, 127, 127}, true},
{90, []uint8{128, 128, 128}, false},
{97, []uint8{34, 56, 255}, false},
{31, []uint8{134, 56, 56}, false},
{30, []uint8{0, 0, 0}, false},
{40, []uint8{0, 0, 0}, true},
{97, []uint8{255, 255, 255}, false},
{107, []uint8{255, 255, 255}, true},
}
for _, item := range tests {
r, g, b := item.rgb[0], item.rgb[1], item.rgb[2]
assert.Equal(
t,
item.want,
RgbToAnsi(r, g, b, item.isBg),
fmt.Sprint("rgb=", item.rgb, ", is bg? ", item.isBg),
)
assert.Equal(t, item.want, Rgb2ansi(r, g, b, item.isBg))
}
}