-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_test.go
More file actions
123 lines (111 loc) · 2.56 KB
/
Copy pathglobal_test.go
File metadata and controls
123 lines (111 loc) · 2.56 KB
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
package tint
import (
"bytes"
"testing"
)
func TestConvenienceStringFunctions(t *testing.T) {
orig := NoColor
NoColor = false
defer func() { NoColor = orig }()
tests := []struct {
name string
fn func(string, ...interface{}) string
code int
}{
{"BlackString", BlackString, 30},
{"RedString", RedString, 31},
{"GreenString", GreenString, 32},
{"YellowString", YellowString, 33},
{"BlueString", BlueString, 34},
{"MagentaString", MagentaString, 35},
{"CyanString", CyanString, 36},
{"WhiteString", WhiteString, 37},
{"HiBlackString", HiBlackString, 90},
{"HiRedString", HiRedString, 91},
{"HiGreenString", HiGreenString, 92},
{"HiYellowString", HiYellowString, 93},
{"HiBlueString", HiBlueString, 94},
{"HiMagentaString", HiMagentaString, 95},
{"HiCyanString", HiCyanString, 96},
{"HiWhiteString", HiWhiteString, 97},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.fn("hello")
want := "\033[" + itoa(tt.code) + "mhello\033[0m"
if got != want {
t.Fatalf("got %q, want %q", got, want)
}
})
}
}
func TestConvenienceStringFormat(t *testing.T) {
orig := NoColor
NoColor = false
defer func() { NoColor = orig }()
got := RedString("n=%d", 5)
want := "\033[31mn=5\033[0m"
if got != want {
t.Fatalf("got %q, want %q", got, want)
}
}
func TestConveniencePrintFunctions(t *testing.T) {
orig := NoColor
origOut := Output
NoColor = false
defer func() { NoColor = orig; Output = origOut }()
tests := []struct {
name string
fn func(string, ...interface{})
code int
}{
{"Black", Black, 30},
{"Red", Red, 31},
{"Green", Green, 32},
{"Yellow", Yellow, 33},
{"Blue", Blue, 34},
{"Magenta", Magenta, 35},
{"Cyan", Cyan, 36},
{"White", White, 37},
{"HiBlack", HiBlack, 90},
{"HiRed", HiRed, 91},
{"HiGreen", HiGreen, 92},
{"HiYellow", HiYellow, 93},
{"HiBlue", HiBlue, 94},
{"HiMagenta", HiMagenta, 95},
{"HiCyan", HiCyan, 96},
{"HiWhite", HiWhite, 97},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
Output = &buf
tt.fn("hello")
got := buf.String()
want := "\033[" + itoa(tt.code) + "mhello\033[0m"
if got != want {
t.Fatalf("got %q, want %q", got, want)
}
})
}
}
func TestConvenienceNoColor(t *testing.T) {
orig := NoColor
NoColor = true
defer func() { NoColor = orig }()
got := RedString("hello")
if got != "hello" {
t.Fatalf("expected plain text, got %q", got)
}
}
func itoa(i int) string {
s := ""
if i == 0 {
return "0"
}
for i > 0 {
s = string(rune('0'+i%10)) + s
i /= 10
}
return s
}