Codebase list golang-github-vbauerster-mpb / 9484504
Fix some tests Vladimir Bauer 8 years ago
3 changed file(s) with 47 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
33 "bytes"
44 "fmt"
55 "io"
6 "os"
76 "sync"
87 "time"
98 "unicode/utf8"
292291 }
293292
294293 func (b *Bar) render(tw int, prependWs, appendWs *widthSync) <-chan []byte {
295 ch := make(chan []byte)
294 ch := make(chan []byte, 1)
296295
297296 go func() {
298297 var st state
299298 defer func() {
300299 // recovering if external decorators panic
301300 if p := recover(); p != nil {
302 fmt.Fprintf(os.Stderr, "bar %d: %q\n", st.id, p)
301 ch <- []byte(fmt.Sprintf("bar%02d panic: %q\n", st.id, p))
303302 }
303 close(ch)
304304 }()
305305 result := make(chan state, 1)
306306 select {
319319 buf = concatenateBlocks(buf, st.bufP.Bytes(), st.bufB.Bytes(), st.bufA.Bytes())
320320 buf = append(buf, '\n')
321321 ch <- buf
322 close(ch)
323322 }()
324323
325324 return ch
141141 }
142142 }
143143
144 func TestGetSpinner(t *testing.T) {
145 var buf bytes.Buffer
146 p := mpb.New(mpb.Output(&buf))
147 bar := p.AddBar(0, mpb.BarTrim())
148
149 for i := 0; i < 100; i++ {
150 time.Sleep(10 * time.Millisecond)
151 bar.Incr(1)
152 }
153
154 p.Stop()
155
156 spinnerChars := []byte(`-\|/`)
157 seen := make(map[byte]bool)
158 for _, b := range bytes.Trim(buf.Bytes(), "\n") {
159 if !seen[b] {
160 seen[b] = true
161 }
162 }
163 for _, b := range spinnerChars {
164 if !seen[b] {
165 t.Errorf("Char %#U not found in bar's output\n", b)
166 }
167 }
168 }
169
170144 func TestBarGetID(t *testing.T) {
171145 var wg sync.WaitGroup
172146 var buf bytes.Buffer
237211 func TestBarPanics(t *testing.T) {
238212 var wg sync.WaitGroup
239213 var buf bytes.Buffer
240 p := mpb.New(mpb.Output(&buf))
214 p := mpb.New(mpb.Output(&buf), mpb.WithWaitGroup(&wg))
241215
242216 wantPanic := "Upps!!!"
243217 numBars := 3
258232 defer wg.Done()
259233 for i := 0; i < 100; i++ {
260234 time.Sleep(10 * time.Millisecond)
261 bar.Incr(1)
235 bar.Increment()
262236 }
263237 }()
264238 }
265239
266 wg.Wait()
267 p.Stop()
268
269 bytes := removeLastRune(buf.Bytes())
270 out := strings.Split(string(bytes), "\n")
240 p.Stop()
241
242 out := bytes.Split(removeLastRune(buf.Bytes()), []byte("\n"))
271243 gotPanic := out[len(out)-1]
272 if gotPanic != wantPanic {
273 t.Errorf("Want panic: %s, got panic: %s\n", wantPanic, gotPanic)
244 if string(gotPanic) != fmt.Sprintf("bar02 panic: %q", wantPanic) {
245 t.Errorf("Want: %q, got: %q\n", wantPanic, gotPanic)
274246 }
275247 }
276248
00 package mpb
11
22 import (
3 "reflect"
3 "bytes"
44 "testing"
55 )
66
1111 total int64
1212 current int64
1313 barRefill *refill
14 want []byte
14 want string
1515 }{
16 {
17 termWidth: 1,
18 barWidth: 100,
19 want: []byte{},
20 },
2116 {
2217 termWidth: 2,
2318 barWidth: 100,
19 want: "",
20 },
21 {
22 termWidth: 3,
23 barWidth: 100,
2424 total: 100,
2525 current: 20,
26 want: []byte("[]"),
26 want: "[-]",
27 },
28 {
29 termWidth: 5,
30 barWidth: 100,
31 total: 100,
32 current: 20,
33 want: "[>--]",
34 },
35 {
36 termWidth: 6,
37 barWidth: 100,
38 total: 100,
39 current: 20,
40 want: "[>---]",
2741 },
2842 {
2943 termWidth: 20,
3044 barWidth: 100,
3145 total: 100,
3246 current: 20,
33 want: []byte("[===>--------------]"),
47 want: "[===>--------------]",
3448 },
3549 {
3650 termWidth: 50,
3751 barWidth: 100,
3852 total: 100,
3953 current: 20,
40 want: []byte("[=========>--------------------------------------]"),
54 want: "[=========>--------------------------------------]",
4155 },
4256 {
4357 termWidth: 100,
4458 barWidth: 100,
4559 total: 100,
4660 current: 0,
47 want: []byte("[--------------------------------------------------------------------------------------------------]"),
61 want: "[--------------------------------------------------------------------------------------------------]",
4862 },
4963 {
5064 termWidth: 100,
5165 barWidth: 100,
5266 total: 100,
5367 current: 1,
54 want: []byte("[>-------------------------------------------------------------------------------------------------]"),
68 want: "[>-------------------------------------------------------------------------------------------------]",
5569 },
5670 {
5771 termWidth: 100,
5872 barWidth: 100,
5973 total: 100,
6074 current: 40,
61 want: []byte("[======================================>-----------------------------------------------------------]"),
75 want: "[======================================>-----------------------------------------------------------]",
6276 },
6377 {
6478 termWidth: 100,
6680 total: 100,
6781 current: 40,
6882 barRefill: &refill{'+', 32},
69 want: []byte("[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]"),
83 want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]",
7084 },
7185 {
7286 termWidth: 100,
7387 barWidth: 100,
7488 total: 100,
7589 current: 99,
76 want: []byte("[================================================================================================>-]"),
90 want: "[================================================================================================>-]",
7791 },
7892 {
7993 termWidth: 100,
8094 barWidth: 100,
8195 total: 100,
8296 current: 100,
83 want: []byte("[==================================================================================================]"),
97 want: "[==================================================================================================]",
8498 },
8599 }
86100
94108 if test.barRefill != nil {
95109 s.refill = test.barRefill
96110 }
97 got := draw(s, test.termWidth, prependWs, appendWs)
98 if !reflect.DeepEqual(test.want, got) {
111 // got := draw(s, test.termWidth, prependWs, appendWs)
112 s.draw(test.termWidth, prependWs, appendWs)
113 got := s.bufB.String()
114 if got != test.want {
99115 t.Errorf("Want: %q, Got: %q\n", test.want, got)
100116 }
101117 }
105121 s := &state{
106122 trimLeftSpace: true,
107123 trimRightSpace: true,
124 bufP: new(bytes.Buffer),
125 bufB: new(bytes.Buffer),
126 bufA: new(bytes.Buffer),
108127 }
109128 s.updateFormat("[=>-]")
110129 return s