-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchalk_test.go
146 lines (133 loc) · 3.1 KB
/
chalk_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
package chalk
import (
"fmt"
"testing"
)
func init() {
// Force tests to print escape sequence regardless of terminal capabilities.
HasColors = true
}
var codes = []struct {
p Parameter
code int
name string
}{
{FGBlack, 30, "black"},
{FGRed, 31, "red"},
{FGGreen, 32, "green"},
{FGYellow, 33, "yellow"},
{FGBlue, 34, "blue"},
{FGMagenta, 35, "magenta"},
{FGCyan, 36, "cyan"},
{FGWhite, 37, "white"},
{FGGray, 90, "gray"},
{FGBrightBlack, 90, "brightblack"},
{FGBrightRed, 91, "brightred"},
{FGBrightGreen, 92, "brightgreen"},
{FGBrightYellow, 93, "brightyellow"},
{FGBrightBlue, 94, "brightblue"},
{FGBrightMagenta, 95, "brightmagenta"},
{FGBrightCyan, 96, "brightcyan"},
{FGBrightWhite, 97, "brightwhite"},
}
func TestAddColors(t *testing.T) {
for i := range codes {
chalk := NewChalk()
// Foreground colors on the same chalk should override each other.
for j := 0; j <= i; j++ {
chalk.Add(codes[j].p)
}
want := fmt.Sprintf("\x1b[%dm%s\x1b[0m", codes[i].code, codes[i].name)
if s := chalk.Sprint(codes[i].name); s != want {
t.Errorf("got:%q\nwant:%q", s, want)
}
}
}
func TestChalk_Sprint(t *testing.T) {
for _, test := range codes {
chalk := NewChalk()
chalk.Add(test.p)
want := fmt.Sprintf("\x1b[%dm%s\x1b[0m", test.code, test.name)
if s := chalk.Sprint(test.name); s != want {
t.Errorf("Sprint(%s) = %q, want %q", test.name, s, want)
}
}
}
func TestChalk_Sprintf(t *testing.T) {
for _, test := range codes {
chalk := NewChalk()
chalk.Add(test.p)
want := fmt.Sprintf("\x1b[%dmhello, %s!\x1b[0m", test.code, test.name)
if s := chalk.Sprintf("hello, %s!", test.name); s != want {
t.Errorf("Sprint(%s) = %q, want %q", test.name, s, want)
}
}
}
func TestChalk_Sprintln(t *testing.T) {
for _, test := range codes {
chalk := NewChalk()
chalk.Add(test.p)
want := fmt.Sprintf("\x1b[%dm%s\n\x1b[0m", test.code, test.name)
if s := chalk.Sprintln(test.name); s != want {
t.Errorf("Sprintln(%s) = %q, want %q", test.name, s, want)
}
}
}
func ExampleChalk_Print() {
// TODO: Explore ways of capturing stdout
for _, test := range codes[:8] {
chalk := NewChalk()
chalk.Add(test.p)
chalk.Println(test.name)
}
// Output:
// [30mblack
// [0m[31mred
// [0m[32mgreen
// [0m[33myellow
// [0m[34mblue
// [0m[35mmagenta
// [0m[36mcyan
// [0m[37mwhite
// [0m
}
func ExampleChalk_Printf() {
for _, test := range codes[:8] {
chalk := NewChalk()
chalk.Add(test.p)
chalk.Printf("hello, %s!", test.name)
fmt.Println()
}
// Output:
// [30mhello, black![0m
// [31mhello, red![0m
// [32mhello, green![0m
// [33mhello, yellow![0m
// [34mhello, blue![0m
// [35mhello, magenta![0m
// [36mhello, cyan![0m
// [37mhello, white![0m
}
func ExampleRed() {
Red("red")
fmt.Println()
Red("hello, %s!", "red")
// Output:
// [31mred[0m
// [31mhello, red![0m
}
func ExampleGreen() {
Green("green")
fmt.Println()
Green("hello, %s!", "green")
// Output:
// [32mgreen[0m
// [32mhello, green![0m
}
func BenchmarkChalk_Print(b *testing.B) {
for i := 0; i < b.N; i++ {
c := NewChalk()
c.Add(FGRed)
_ = c.Sprint("hello")
}
}