Codebase list golang-github-vbauerster-mpb / c675564
More test fixes Vladimir Bauer 8 years ago
2 changed file(s) with 25 addition(s) and 89 deletion(s). Raw diff Collapse all Expand all
186186 bar.ResumeFill(refillChar, int64(till))
187187
188188 for i := 0; i < total; i++ {
189 time.Sleep(10 * time.Millisecond)
190 bar.Incr(1)
191 }
192
193 p.Stop()
194
195 bytes := removeLastRune(buf.Bytes())
196
197 gotBar := string(bytes[len(bytes)-width:])
189 bar.Increment()
190 }
191
192 p.Stop()
193
198194 wantBar := fmt.Sprintf("[%s%s]",
199195 strings.Repeat(string(refillChar), till-1),
200196 strings.Repeat("=", total-till-1))
201 if gotBar != wantBar {
202 t.Errorf("Want bar: %s, got bar: %s\n", wantBar, gotBar)
197
198 if !strings.Contains(buf.String(), wantBar) {
199 t.Errorf("Want bar: %s, got bar: %s\n", wantBar, buf.String())
203200 }
204201 }
205202
234231
235232 p.Stop()
236233
237 out := bytes.Split(removeLastRune(buf.Bytes()), []byte("\n"))
238 gotPanic := out[len(out)-1]
239234 wantPanic = fmt.Sprintf("b#%02d panic: %v", 2, wantPanic)
240235
241 if string(gotPanic) != wantPanic {
242 t.Errorf("Want: %q, got: %q\n", wantPanic, gotPanic)
243 }
244 }
245
246 func removeLastRune(bytes []byte) []byte {
247 _, size := utf8.DecodeLastRune(bytes)
248 return bytes[:len(bytes)-size]
249 }
236 if !strings.Contains(buf.String(), wantPanic) {
237 t.Errorf("Want: %q, got: %q\n", wantPanic, buf.String())
238 }
239 }
77 "sync"
88 "testing"
99 "time"
10 "unicode/utf8"
1110
1211 "github.com/vbauerster/mpb"
1312 "github.com/vbauerster/mpb/decor"
1413 )
1514
16 func TestDefaultWidth(t *testing.T) {
17 var buf bytes.Buffer
18 p := mpb.New(mpb.Output(&buf))
19 bar := p.AddBar(100, mpb.BarTrim())
20
21 for i := 0; i < 100; i++ {
22 bar.Incr(1)
23 }
24 p.Stop()
25
26 wantWidth := 80
27 gotWidth := utf8.RuneCount(buf.Bytes())
28 if gotWidth != wantWidth+1 { // + 1 for new line
29 t.Errorf("Expected default width: %d, got: %d\n", wantWidth, gotWidth)
30 }
31 }
32
33 func TestCustomWidth(t *testing.T) {
34 wantWidth := 60
35 var buf bytes.Buffer
36 p := mpb.New(mpb.WithWidth(wantWidth), mpb.Output(&buf))
37 bar := p.AddBar(100, mpb.BarTrim())
38
39 for i := 0; i < 100; i++ {
40 bar.Incr(1)
41 }
42 p.Stop()
43
44 gotWidth := utf8.RuneCount(buf.Bytes())
45 if gotWidth != wantWidth+1 { // +1 for new line
46 t.Errorf("Expected default width: %d, got: %d\n", wantWidth, gotWidth)
47 }
48 }
49
5015 func TestAddBar(t *testing.T) {
51 var wg sync.WaitGroup
52 var buf bytes.Buffer
53 p := mpb.New(mpb.Output(&buf), mpb.WithWaitGroup(&wg))
16 p := mpb.New()
5417
5518 count := p.BarCount()
5619 if count != 0 {
5720 t.Errorf("BarCount want: %q, got: %q\n", 0, count)
5821 }
5922
60 numBars := 3
61 wg.Add(numBars)
23 bar := p.AddBar(100)
6224
63 for i := 0; i < numBars; i++ {
64 name := fmt.Sprintf("Bar#%d:", i)
65 bar := p.AddBar(100, mpb.PrependDecorators(decor.StaticName(name, len(name), 0)))
66
67 go func() {
68 defer wg.Done()
69 for i := 0; i < 100; i++ {
70 bar.Incr(1)
71 }
72 }()
25 count = p.BarCount()
26 if count != 1 {
27 t.Errorf("BarCount want: %q, got: %q\n", 1, count)
7328 }
7429
75 count = p.BarCount()
76 if count != numBars {
77 t.Errorf("BarCount want: %q, got: %q\n", numBars, count)
78 }
30 bar.Complete()
7931 p.Stop()
8032 }
8133
8234 func TestRemoveBar(t *testing.T) {
8335 p := mpb.New()
8436
85 b := p.AddBar(10)
37 bar := p.AddBar(10)
8638
87 if !p.RemoveBar(b) {
39 if !p.RemoveBar(bar) {
8840 t.Error("RemoveBar failure")
8941 }
9042
9244 if count != 0 {
9345 t.Errorf("BarCount want: %q, got: %q\n", 0, count)
9446 }
47
48 bar.Complete()
9549 p.Stop()
9650 }
9751
152106 go func() {
153107 for i := 0; i < 100; i++ {
154108 time.Sleep(10 * time.Millisecond)
155 bar.Incr(1)
109 bar.Increment()
156110 }
157111 }()
158112
159 time.Sleep(250 * time.Millisecond)
113 time.Sleep(300 * time.Millisecond)
160114 close(cancel)
161115 p.Stop()
162116
163 bytes := removeLastRune(buf.Bytes())
164
165 seen := make(map[rune]bool)
166 for _, r := range string(bytes) {
167 if !seen[r] {
168 seen[r] = true
169 }
170 }
171117 for _, r := range customFormat {
172 if !seen[r] {
118 if !bytes.ContainsRune(buf.Bytes(), r) {
173119 t.Errorf("Rune %#U not found in bar\n", r)
174120 }
175121 }