-
Notifications
You must be signed in to change notification settings - Fork 129
/
spinner_test.go
338 lines (304 loc) · 8.93 KB
/
spinner_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
// Copyright (c) 2021 Brian J. Downs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spinner
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"reflect"
"strings"
"sync"
"testing"
"time"
"golang.org/x/term"
)
const baseWait = 3
// syncBuffer
type syncBuffer struct {
sync.Mutex
bytes.Buffer
}
// Write
func (b *syncBuffer) Write(data []byte) (int, error) {
b.Lock()
defer b.Unlock()
return b.Buffer.Write(data)
}
// withOutput
func withOutput(a []string, d time.Duration) (*Spinner, *syncBuffer) {
var out syncBuffer
s := New(a, d)
s.Writer = &out
return s, &out
}
// TestNew verifies that the returned instance is of the proper type
func TestNew(t *testing.T) {
for i := 0; i < len(CharSets); i++ {
s := New(CharSets[i], 1*time.Second)
if reflect.TypeOf(s).String() != "*spinner.Spinner" {
t.Errorf("New returned incorrect type kind=%d", i)
}
}
}
// TestStart will verify a spinner can be started
func TestStart(t *testing.T) {
s := New(CharSets[1], 100*time.Millisecond)
s.Color("red")
s.Start()
time.Sleep(baseWait * time.Second)
s.Stop()
time.Sleep(100 * time.Millisecond)
}
// TestActive will verify we can tell when a spinner is running
func TestActive(t *testing.T) {
if fd := os.Stdout.Fd(); !term.IsTerminal(int(fd)) {
t.Log("not running in a terminal")
return
}
s := New(CharSets[1], 100*time.Millisecond)
if s.Active() {
t.Error("expected a new spinner to not be active")
}
s.Start()
if !s.Active() {
t.Error("expected a started spinner to be active")
}
s.Stop()
if s.Active() {
t.Error("expected a stopped spinner to be active")
}
}
// TestStop will verify a spinner can be stopped
func TestStop(t *testing.T) {
p, out := withOutput(CharSets[14], 100*time.Millisecond)
p.Color("yellow")
p.Start()
time.Sleep(500 * time.Millisecond)
p.Stop()
// because the spinner will print an appropriate number of backspaces before stopping,
// let it complete that sleep
time.Sleep(100 * time.Millisecond)
out.Lock()
len1 := out.Len()
out.Unlock()
time.Sleep(300 * time.Millisecond)
out.Lock()
defer out.Unlock()
len2 := out.Len()
if len1 != len2 {
t.Errorf("expected equal, got %v != %v", len1, len2)
}
p = nil
}
// TestRestart will verify a spinner can be stopped and started again
func TestRestart(t *testing.T) {
s, out := withOutput(CharSets[4], 40*time.Millisecond)
s.Start()
time.Sleep(150 * time.Millisecond)
s.Restart()
time.Sleep(158 * time.Millisecond)
s.Stop()
time.Sleep(10 * time.Millisecond)
result := out.Bytes()
first := result[:len(result)/2]
second := result[len(result)/2:]
if !bytes.Equal(first, second) {
t.Errorf("expected restart output to match initial output. got=%q want=%q", first, second)
}
}
func TestDisable(t *testing.T) {
s, _ := withOutput(CharSets[4], 100*time.Millisecond)
s.Start()
time.Sleep(150 * time.Millisecond)
if !s.Enabled() {
t.Error("expected enabled spinner after startup")
}
time.Sleep(150 * time.Millisecond)
s.Disable()
time.Sleep(150 * time.Millisecond)
if s.Enabled() {
t.Error("expected disabling the spinner works")
}
time.Sleep(150 * time.Millisecond)
s.Enable()
time.Sleep(150 * time.Millisecond)
if !s.Enabled() {
t.Error("expected enabling the spinner works")
}
}
// TestHookFunctions will verify that hook functions works as expected
func TestHookFunctions(t *testing.T) {
if fd := os.Stdout.Fd(); !term.IsTerminal(int(fd)) {
t.Log("not running in a terminal")
return
}
s := New(CharSets[4], 50*time.Millisecond)
var out syncBuffer
s.Writer = &out
s.PreUpdate = func(s *Spinner) {
fmt.Fprintf(s.Writer, "pre-update")
}
s.PostUpdate = func(s *Spinner) {
fmt.Fprintf(s.Writer, "post-update")
}
s.Start()
s.Color("cyan")
time.Sleep(200 * time.Millisecond)
s.Stop()
time.Sleep(50 * time.Millisecond)
out.Lock()
defer out.Unlock()
result := out.Bytes()
if !bytes.Contains(result, []byte("pre-update")) {
t.Error("pre-update failed")
}
if !bytes.Contains(result, []byte("post-update")) {
t.Error("post-update failed")
}
s = nil
}
// TestReverse will verify that the given spinner can stop and start again reversed
func TestReverse(t *testing.T) {
a := New(CharSets[10], 1*time.Second)
a.Color("red")
a.Start()
time.Sleep(baseWait * time.Second)
a.Reverse()
a.Restart()
time.Sleep(baseWait * time.Second)
a.Reverse()
a.Restart()
time.Sleep(baseWait * time.Second)
a.Stop()
a = nil
}
// TestUpdateSpeed verifies that the delay can be updated
func TestUpdateSpeed(t *testing.T) {
s := New(CharSets[10], 1*time.Second)
delay1 := s.Delay
s.UpdateSpeed(baseWait * time.Second)
delay2 := s.Delay
if delay1 == delay2 {
t.Error("update of speed failed")
}
s = nil
}
// TestUpdateCharSet verifies that character sets can be updated
func TestUpdateCharSet(t *testing.T) {
s := New(CharSets[14], 1*time.Second)
charSet1 := s.chars
s.UpdateCharSet(CharSets[1])
charSet2 := s.chars
for i := range charSet1 {
if charSet1[i] == charSet2[i] {
t.Error("update of char set failed")
}
}
s = nil
}
// TestGenerateNumberSequence verifies that a string slice of a spefic size is returned
func TestGenerateNumberSequence(t *testing.T) {
elementCount := 100
seq := GenerateNumberSequence(elementCount)
if reflect.TypeOf(seq).String() != "[]string" {
t.Error("received incorrect type in return from GenerateNumberSequence")
}
t.Log("In: ", elementCount)
t.Log("Out: ", len(seq))
if len(seq) != elementCount {
t.Error("number of elements in slice doesn't match expected count")
}
}
// TestBackspace proves that the correct number of characters are removed.
func TestBackspace(t *testing.T) {
// Because of buffering of output and time weirdness, somethings
// are broken for an indeterminant reason without a wait
time.Sleep(75 * time.Millisecond)
fmt.Println()
s := New(CharSets[0], 100*time.Millisecond)
s.Color("blue")
s.Start()
fmt.Print("This is on the same line as the spinner: ")
time.Sleep(baseWait * time.Second)
s.Stop()
}
// TestColorError tests that if an invalid color string is passed to the Color
// function, the invalid color error is returned
func TestColorError(t *testing.T) {
s := New(CharSets[0], 100*time.Millisecond)
const invalidColorName = "bluez"
const validColorName = "green"
if s.Color(invalidColorName) != errInvalidColor {
t.Error("Color method did not return an error when given an invalid color.")
}
if s.Color(validColorName) != nil {
t.Error("Color method did not return nil when given a valid color name.")
}
}
func TestWithWriter(t *testing.T) {
s := New(CharSets[9], time.Millisecond*400, WithWriter(ioutil.Discard))
_ = s
}
func TestComputeNumberOfLinesNeededToPrintStringInternal(t *testing.T) {
tests := []struct {
description string
expectedCount int
printedLine string
maxLineWidth int
}{
{"BlankLine", 1, "", 50},
{"SingleLine", 1, "Hello world", 50},
{"SingleLineANSI", 1, "Hello \x1b[36mworld\x1b[0m", 20},
{"MultiLine", 2, "Hello\n world", 50},
{"MultiLineANSI", 2, "Hello\n \x1b[1;36mworld\x1b[0m", 20},
{"LongString", 2, "Hello world! I am a super long string that will be printed in 2 lines", 50},
{"LongStringWithNewlines", 4, "Hello world!\nI am a super long string that will be printed in 2 lines.\nAnother new line", 50},
{"NewlineCharAtStart", 2, "\nHello world!", 50},
{"NewlineCharAtStartANSI", 2, "\n\x1b[36mHello\x1b[0m world!", 50},
{"NewlineCharAtStartANSIFlipped", 2, "\x1b[36m\nHello\x1b[0m world!", 50},
{"MultipleNewlineCharAtStart", 4, "\n\n\nHello world!", 50},
{"NewlineCharAtEnd", 2, "Hello world!\n", 50},
{"NewlineCharAtEndANSI", 2, "Hello \x1b[36mworld!\x1b[0m\n", 50},
{"NewlineCharAtEndANSIFlipped", 2, "Hello \x1b[36mworld!\n\x1b[0m", 50},
{"StringExactlySizeOfScreen", 1, strings.Repeat("a", 50), 50},
{"StringExactlySizeOfScreenANSI", 1, "\x1b[36m" + strings.Repeat("a", 50), 50},
{"StringOneGreaterThanSizeOfScreen", 2, strings.Repeat("a", 51), 50},
}
for _, test := range tests {
result := computeNumberOfLinesNeededToPrintStringInternal(test.printedLine,
test.maxLineWidth)
if result != test.expectedCount {
// Output error, resetting leftover ANSI sequences
t.Errorf("%s: Line '%s\x1b[0m' shoud be printed on '%d' line, got '%d'",
test.description, test.printedLine, test.expectedCount, result)
}
}
}
/*
Benchmarks
*/
// BenchmarkNew runs a benchmark for the New() function
func BenchmarkNew(b *testing.B) {
for n := 0; n < b.N; n++ {
New(CharSets[1], 1*time.Second)
}
}
func BenchmarkNewStartStop(b *testing.B) {
for n := 0; n < b.N; n++ {
s := New(CharSets[1], 1*time.Second)
s.Start()
s.Stop()
}
}