Debian Codegraph @

spinner_test.go

raw ◀ ▶
1 Debian patch touch this file
1 // Copyright (c) 2021 Brian J. Downs
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package spinner
16
17 import (
18 "bytes"
19 "fmt"
20 "io/ioutil"
•21 "os"
22 "reflect"
23 "sync"
24 "testing"
25 "time"
•26
27 "github.com/mattn/go-isatty"
28 )
29
30 const baseWait = 3
31
32 // syncBuffer
33 type syncBuffer struct {
34 sync.Mutex
35 bytes.Buffer
36 }
37
38 // Write
39 func (b *syncBuffer) Write(data []byte) (int, error) {
40 b.Lock()
41 defer b.Unlock()
42 return b.Buffer.Write(data)
43 }
44
45 // withOutput
46 func withOutput(a []string, d time.Duration) (*Spinner, *syncBuffer) {
47 var out syncBuffer
48 s := New(a, d)
49 s.Writer = &out
50 return s, &out
51 }
52
53 // TestNew verifies that the returned instance is of the proper type
54 func TestNew(t *testing.T) {
55 for i := 0; i < len(CharSets); i++ {
56 s := New(CharSets[i], 1*time.Second)
57 if reflect.TypeOf(s).String() != "*spinner.Spinner" {
58 t.Errorf("New returned incorrect type kind=%d", i)
59 }
60 }
61 }
62
63 // TestStart will verify a spinner can be started
64 func TestStart(t *testing.T) {
65 s := New(CharSets[1], 100*time.Millisecond)
66 s.Color("red")
67 s.Start()
68 time.Sleep(baseWait * time.Second)
69 s.Stop()
70 time.Sleep(100 * time.Millisecond)
71 }
72
73 // TestActive will verify we can tell when a spinner is running
74 func TestActive(t *testing.T) {
•75 if !isatty.IsTerminal(os.Stdout.Fd()) {
76 t.Log("not running in a terminal")
77 return
78 }
79 s := New(CharSets[1], 100*time.Millisecond)
80 if s.Active() {
81 t.Error("expected a new spinner to not be active")
82 }
83 s.Start()
84 if !s.Active() {
85 t.Error("expected a started spinner to be active")
86 }
87 s.Stop()
88 if s.Active() {
89 t.Error("expected a stopped spinner to be active")
90 }
91 }
92
93 // TestStop will verify a spinner can be stopped
94 func TestStop(t *testing.T) {
95 p, out := withOutput(CharSets[14], 100*time.Millisecond)
96 p.Color("yellow")
97 p.Start()
98 time.Sleep(500 * time.Millisecond)
99 p.Stop()
100 // because the spinner will print an appropriate number of backspaces before stopping,
101 // let it complete that sleep
102 time.Sleep(100 * time.Millisecond)
103 out.Lock()
104 len1 := out.Len()
105 out.Unlock()
106 time.Sleep(300 * time.Millisecond)
107 out.Lock()
108 defer out.Unlock()
109 len2 := out.Len()
110 if len1 != len2 {
111 t.Errorf("expected equal, got %v != %v", len1, len2)
112 }
113 p = nil
114 }
115
116 // TestRestart will verify a spinner can be stopped and started again
117 func TestRestart(t *testing.T) {
118 s, out := withOutput(CharSets[4], 40*time.Millisecond)
119
120 s.Start()
121 time.Sleep(150 * time.Millisecond)
122 s.Restart()
123 time.Sleep(158 * time.Millisecond)
124 s.Stop()
125 time.Sleep(10 * time.Millisecond)
126
127 result := out.Bytes()
128 first := result[:len(result)/2]
129 second := result[len(result)/2:]
130 if !bytes.Equal(first, second) {
131 t.Errorf("expected restart output to match initial output. got=%q want=%q", first, second)
132 }
133 }
134
135 // TestHookFunctions will verify that hook functions works as expected
136 func TestHookFunctions(t *testing.T) {
•137 if !isatty.IsTerminal(os.Stdout.Fd()) {
138 t.Log("not running in a termian")
139 return
140 }
141 s := New(CharSets[4], 50*time.Millisecond)
142 var out syncBuffer
143 s.Writer = &out
144 s.PreUpdate = func(s *Spinner) {
145 fmt.Fprintf(s.Writer, "pre-update")
146 }
147 s.PostUpdate = func(s *Spinner) {
148 fmt.Fprintf(s.Writer, "post-update")
149 }
150
151 s.Start()
152 s.Color("cyan")
153 time.Sleep(200 * time.Millisecond)
154 s.Stop()
155 time.Sleep(50 * time.Millisecond)
156 out.Lock()
157 defer out.Unlock()
158 result := out.Bytes()
159 if !bytes.Contains(result, []byte("pre-update")) {
160 t.Error("pre-update failed")
161 }
162
163 if !bytes.Contains(result, []byte("post-update")) {
164 t.Error("post-update failed")
165 }
166 s = nil
167 }
168
169 // TestReverse will verify that the given spinner can stop and start again reversed
170 func TestReverse(t *testing.T) {
171 a := New(CharSets[10], 1*time.Second)
172 a.Color("red")
173 a.Start()
174 time.Sleep(baseWait * time.Second)
175 a.Reverse()
176 a.Restart()
177 time.Sleep(baseWait * time.Second)
178 a.Reverse()
179 a.Restart()
180 time.Sleep(baseWait * time.Second)
181 a.Stop()
182 a = nil
183 }
184
185 // TestUpdateSpeed verifies that the delay can be updated
186 func TestUpdateSpeed(t *testing.T) {
187 s := New(CharSets[10], 1*time.Second)
188 delay1 := s.Delay
189 s.UpdateSpeed(baseWait * time.Second)
190 delay2 := s.Delay
191 if delay1 == delay2 {
192 t.Error("update of speed failed")
193 }
194 s = nil
195 }
196
197 // TestUpdateCharSet verifies that character sets can be updated
198 func TestUpdateCharSet(t *testing.T) {
199 s := New(CharSets[14], 1*time.Second)
200 charSet1 := s.chars
201 s.UpdateCharSet(CharSets[1])
202 charSet2 := s.chars
203 for i := range charSet1 {
204 if charSet1[i] == charSet2[i] {
205 t.Error("update of char set failed")
206 }
207 }
208 s = nil
209 }
210
211 // TestGenerateNumberSequence verifies that a string slice of a spefic size is returned
212 func TestGenerateNumberSequence(t *testing.T) {
213 elementCount := 100
214 seq := GenerateNumberSequence(elementCount)
215 if reflect.TypeOf(seq).String() != "[]string" {
216 t.Error("received incorrect type in return from GenerateNumberSequence")
217 }
218 t.Log("In: ", elementCount)
219 t.Log("Out: ", len(seq))
220 if len(seq) != elementCount {
221 t.Error("number of elements in slice doesn't match expected count")
222 }
223 }
224
225 // TestBackspace proves that the correct number of characters are removed.
226 func TestBackspace(t *testing.T) {
227 // Because of buffering of output and time weirdness, somethings
228 // are broken for an indeterminant reason without a wait
229 time.Sleep(75 * time.Millisecond)
230 fmt.Println()
231 s := New(CharSets[0], 100*time.Millisecond)
232 s.Color("blue")
233 s.Start()
234 fmt.Print("This is on the same line as the spinner: ")
235 time.Sleep(baseWait * time.Second)
236 s.Stop()
237 }
238
239 // TestColorError tests that if an invalid color string is passed to the Color
240 // function, the invalid color error is returned
241 func TestColorError(t *testing.T) {
242 s := New(CharSets[0], 100*time.Millisecond)
243
244 const invalidColorName = "bluez"
245 const validColorName = "green"
246
247 if s.Color(invalidColorName) != errInvalidColor {
248 t.Error("Color method did not return an error when given an invalid color.")
249 }
250
251 if s.Color(validColorName) != nil {
252 t.Error("Color method did not return nil when given a valid color name.")
253 }
254 }
255
256 func TestWithWriter(t *testing.T) {
257 s := New(CharSets[9], time.Millisecond*400, WithWriter(ioutil.Discard))
258 _ = s
259 }
260
261 /*
262 Benchmarks
263 */
264
265 // BenchmarkNew runs a benchmark for the New() function
266 func BenchmarkNew(b *testing.B) {
267 for n := 0; n < b.N; n++ {
268 New(CharSets[1], 1*time.Second)
269 }
270 }
271
272 func BenchmarkNewStartStop(b *testing.B) {
273 for n := 0; n < b.N; n++ {
274 s := New(CharSets[1], 1*time.Second)
275 s.Start()
276 s.Stop()
277 }
278 }