-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymbols_test.go
154 lines (129 loc) · 3.54 KB
/
symbols_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
package logsymbols
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/gookit/color.v1"
)
func ExampleForceNoColors() {
ForceNoColors()
fmt.Println("\nno color")
fmt.Printf("%s info\n", Info)
fmt.Printf("%s success\n", Success)
fmt.Printf("%s warning\n", Warn)
fmt.Printf("%s error\n", Error)
// Output: no color
// ℹ info
// ✔ success
// ⚠ warning
// ✖ error
}
func ExampleFallbackSymbols() {
f := FallbackSymbols()
fmt.Println("\nfallback")
fmt.Printf("%s info\n", f.Info)
fmt.Printf("%s success\n", f.Success)
fmt.Printf("%s warning\n", f.Warning)
fmt.Printf("%s error\n", f.Error)
// Output: fallback
// i info
// √ success
// ‼ warning
// × error
}
func TestNormalSymbols(t *testing.T) {
sets := map[string]Symbols{
"normal": NormalSymbols(),
"fallback": FallbackSymbols(),
"current": CurrentSymbols(),
}
for n, set := range sets {
s := set
name := n
t.Run("no color "+name, func(t *testing.T) {
ForceNoColors()
var expected Symbols
switch name {
case "normal":
expected = NormalSymbols()
case "current":
expected = CurrentSymbols()
case "fallback":
expected = FallbackSymbols()
}
assert.Equal(t, s.Info, expected.Info)
assert.Equal(t, s.Success, expected.Success)
assert.Equal(t, s.Ok, expected.Ok)
assert.Equal(t, s.Warning, expected.Warning)
assert.Equal(t, s.Error, expected.Error)
})
t.Run("color "+name, func(t *testing.T) {
ForceColors()
var expected Symbols
switch name {
case "normal":
expected = NormalSymbols()
case "current":
expected = CurrentSymbols()
case "fallback":
expected = FallbackSymbols()
}
assert.Contains(t, expected.Info, s.Info)
assert.Contains(t, expected.Success, s.Success)
assert.Contains(t, expected.Ok, s.Ok)
assert.Contains(t, expected.Warning, s.Warning)
assert.Contains(t, expected.Error, s.Error)
})
}
}
func TestFallbackSymbols(t *testing.T) {
t.Run("no color", func(t *testing.T) {
ForceNoColors()
s := FallbackSymbols()
assert.Equal(t, s.Info, fallback.Info)
assert.Equal(t, s.Success, fallback.Success)
assert.Equal(t, s.Ok, fallback.Ok)
assert.Equal(t, s.Warning, fallback.Warning)
assert.Equal(t, s.Error, fallback.Error)
})
t.Run("color", func(t *testing.T) {
ForceColors()
s := FallbackSymbols()
assert.Equal(t, s.Info, Symbol(color.Notice.Render(fallback.Info)))
assert.Equal(t, s.Success, Symbol(color.Success.Render(fallback.Success)))
assert.Equal(t, s.Ok, Symbol(color.Success.Render(fallback.Ok)))
assert.Equal(t, s.Warning, Symbol(color.Warn.Render(fallback.Warning)))
assert.Equal(t, s.Error, Symbol(color.Danger.Render(fallback.Error)))
})
}
func TestBaseSymbols(t *testing.T) {
s := BaseSymbols()
assert.Equal(t, s.Info, osBaseSymbols.Info)
assert.Equal(t, s.Success, osBaseSymbols.Success)
assert.Equal(t, s.Warning, osBaseSymbols.Warning)
assert.Equal(t, s.Error, osBaseSymbols.Error)
}
func TestAutodetectTTY(t *testing.T) {
t.Run("autodetect", func(t *testing.T) {
AutodetectTTY(os.Stdout)
// During tests Stdout is not a TTY
assert.False(t, colorOn)
})
t.Run("try tty1", func(t *testing.T) {
f, err := os.OpenFile("/dev/pts/1", os.O_RDONLY, 0444)
if err != nil {
t.SkipNow() // this environment don't have TTY or PTS
}
AutodetectTTY(f)
assert.True(t, colorOn)
})
t.Run("nil", func(t *testing.T) {
AutodetectTTY(nil)
assert.False(t, colorOn)
})
}
func TestSymbols_String(t *testing.T) {
s := osBaseSymbols.String()
assert.Equal(t, "Info: ℹ Success: ✔ Warning: ⚠ Error: ✖", s)
}