Debian Codegraph @

spinner_test.go

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