forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
223 lines (201 loc) · 6.57 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
package pterm_test
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/MarvinJWendt/testza"
"github.com/pterm/pterm"
)
var printables = []interface{}{"Hello, World!", 1337, true, false, -1337, 'c', 1.5, "\\", "%s"}
var terminalWidth = 80
var terminalHeight = 60
func TestMain(m *testing.M) {
pterm.SetForcedTerminalSize(terminalWidth, terminalHeight)
setupStdoutCapture()
exitVal := m.Run()
teardownStdoutCapture()
os.Exit(exitVal)
}
// testPrintContains can be used to test Print methods.
func testPrintContains(t *testing.T, logic func(w io.Writer, a interface{})) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
s := captureStdout(func(w io.Writer) {
logic(w, printable)
})
testza.AssertContains(t, s, fmt.Sprint(printable))
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
s := captureStdout(func(w io.Writer) {
logic(w, printable)
})
testza.AssertContains(t, s, fmt.Sprint(printable))
})
pterm.EnableStyling()
}
}
// testPrintfContains can be used to test Printf methods.
func testPrintfContains(t *testing.T, logic func(w io.Writer, format string, a interface{})) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
s := captureStdout(func(w io.Writer) {
logic(w, "Hello, %v!", printable)
})
testza.AssertContains(t, s, fmt.Sprintf("Hello, %v!", fmt.Sprint(printable)))
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
s := captureStdout(func(w io.Writer) {
logic(w, "Hello, %v!", printable)
})
testza.AssertContains(t, s, fmt.Sprintf("Hello, %v!", fmt.Sprint(printable)))
})
pterm.EnableStyling()
}
}
// testPrintflnContains can be used to test Printfln methods.
func testPrintflnContains(t *testing.T, logic func(w io.Writer, format string, a interface{})) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testPrintfContains(t, logic)
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testPrintfContains(t, logic)
})
pterm.EnableStyling()
}
}
// testPrintlnContains can be used to test Println methods.
func testPrintlnContains(t *testing.T, logic func(w io.Writer, a interface{})) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testPrintContains(t, logic)
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testPrintContains(t, logic)
})
pterm.EnableStyling()
}
}
// testSprintContains can be used to test Sprint methods.
func testSprintContains(t *testing.T, logic func(a interface{}) string) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testza.AssertContains(t, logic(printable), fmt.Sprint(printable))
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testza.AssertContains(t, logic(printable), fmt.Sprint(printable))
})
pterm.EnableStyling()
}
}
// testSprintContainsWithoutError can be used to test Sprint methods which return an error.
func testSprintContainsWithoutError(t *testing.T, logic func(a interface{}) (string, error)) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
s, err := logic(printable)
testza.AssertContains(t, s, fmt.Sprint(printable))
testza.AssertNoError(t, err)
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
s, err := logic(printable)
testza.AssertContains(t, s, fmt.Sprint(printable))
testza.AssertNoError(t, err)
})
pterm.EnableStyling()
}
}
// testSprintfContains can be used to test Sprintf methods.
func testSprintfContains(t *testing.T, logic func(format string, a interface{}) string) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testza.AssertContains(t, logic("Hello, %v!", printable), fmt.Sprintf("Hello, %v!", printable))
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testza.AssertContains(t, logic("Hello, %v!", printable), fmt.Sprintf("Hello, %v!", printable))
})
pterm.EnableStyling()
}
}
// testSprintflnContains can be used to test Sprintfln methods.
func testSprintflnContains(t *testing.T, logic func(format string, a interface{}) string) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testSprintfContains(t, logic)
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testSprintfContains(t, logic)
})
pterm.EnableStyling()
}
}
// testSprintlnContains can be used to test Sprintln methods.
func testSprintlnContains(t *testing.T, logic func(a interface{}) string) {
for _, printable := range printables {
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testSprintContains(t, logic)
})
pterm.DisableStyling()
t.Run(fmt.Sprint(printable), func(t *testing.T) {
testSprintContains(t, logic)
})
pterm.EnableStyling()
}
}
// testDoesOutput can be used to test if something is outputted to stdout.
func testDoesOutput(t *testing.T, logic func(w io.Writer)) {
testza.AssertNotZero(t, captureStdout(logic))
pterm.DisableStyling()
testza.AssertNotZero(t, captureStdout(logic))
pterm.EnableStyling()
}
// testEmpty checks that a function does not return a string.
func testEmpty(t *testing.T, logic func(a interface{}) string) {
for _, printable := range printables {
testza.AssertZero(t, logic(printable))
pterm.DisableStyling()
testza.AssertZero(t, logic(printable))
pterm.EnableStyling()
}
}
// testDoesNotOutput can be used, to test that something does not output anything to stdout.
func testDoesNotOutput(t *testing.T, logic func(w io.Writer)) {
testza.AssertZero(t, captureStdout(logic))
pterm.DisableStyling()
testza.AssertZero(t, captureStdout(logic))
pterm.EnableStyling()
}
var outBuf bytes.Buffer
// setupStdoutCapture sets up a fake stdout capture.
func setupStdoutCapture() {
outBuf.Reset()
pterm.SetDefaultOutput(&outBuf)
}
// teardownStdoutCapture restores the real stdout.
func teardownStdoutCapture() {
pterm.SetDefaultOutput(os.Stdout)
}
// captureStdout simulates capturing of os.stdout with a buffer and returns what was writted to the screen
func captureStdout(f func(w io.Writer)) string {
setupStdoutCapture()
f(&outBuf)
return readStdout()
}
// readStdout reads the current stdout buffor. Assumes setupStdoutCapture() has been called before.
func readStdout() string {
content := outBuf.String()
outBuf.Reset()
return content
}
func proxyToDevNull() {
pterm.SetDefaultOutput(os.NewFile(0, os.DevNull))
}