Codebase list golang-github-vbauerster-mpb / 6ec0cf8
Update progress_test Vladimir Bauer 9 years ago
2 changed file(s) with 20 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
1010 "time"
1111
1212 "github.com/vbauerster/mpb"
13 "github.com/vbauerster/mpb/decor"
1314 )
1415
1516 func TestWithContext(t *testing.T) {
2425
2526 for i := 0; i < numBars; i++ {
2627 name := fmt.Sprintf("Bar#%d:", i)
27 bar := p.AddBarWithID(i, int64(total)).PrependName(name, len(name), 0)
28 bar := p.AddBar(int64(total), mpb.BarID(i),
29 mpb.PrependDecorators(decor.Name(name, len(name), 0)))
2830
2931 go func() {
30 defer func() {
31 // fmt.Printf("%s done\n", name)
32 wg.Done()
33 }()
32 defer wg.Done()
3433 for i := 0; i < total; i++ {
3534 select {
3635 case <-ctx.Done():
5453 t.Error("ProgressBar didn't stop")
5554 }
5655 }
57
58 func TestWithNilContext(t *testing.T) {
59 defer func() {
60 if p := recover(); p != nil {
61 if msg, ok := p.(string); ok && msg == "nil context" {
62 return
63 }
64 t.Errorf("Expected nil context panic, got: %+v", p)
65 }
66 }()
67 _ = mpb.New().WithContext(nil)
68 }
99 "unicode/utf8"
1010
1111 "github.com/vbauerster/mpb"
12 "github.com/vbauerster/mpb/decor"
1213 )
1314
1415 func TestDefaultWidth(t *testing.T) {
1516 var buf bytes.Buffer
16 p := mpb.New().SetOut(&buf)
17 bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
17 p := mpb.New(mpb.Output(&buf))
18 bar := p.AddBar(100, mpb.BarTrim())
19
1820 for i := 0; i < 100; i++ {
1921 bar.Incr(1)
2022 }
3032 func TestCustomWidth(t *testing.T) {
3133 wantWidth := 60
3234 var buf bytes.Buffer
33 p := mpb.New().SetWidth(wantWidth).SetOut(&buf)
34 bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
35 p := mpb.New(mpb.WithWidth(wantWidth), mpb.Output(&buf))
36 bar := p.AddBar(100, mpb.BarTrim())
37
3538 for i := 0; i < 100; i++ {
3639 bar.Incr(1)
3740 }
4548
4649 func TestInvalidWidth(t *testing.T) {
4750 var buf bytes.Buffer
48 p := mpb.New().SetWidth(1).SetOut(&buf)
49 bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
51 p := mpb.New(mpb.WithWidth(1), mpb.Output(&buf))
52 bar := p.AddBar(100, mpb.BarTrim())
53
5054 for i := 0; i < 100; i++ {
5155 bar.Incr(1)
5256 }
6266 func TestAddBar(t *testing.T) {
6367 var wg sync.WaitGroup
6468 var buf bytes.Buffer
65 p := mpb.New().SetOut(&buf)
69 p := mpb.New(mpb.Output(&buf))
6670
6771 count := p.BarCount()
6872 if count != 0 {
7478
7579 for i := 0; i < numBars; i++ {
7680 name := fmt.Sprintf("Bar#%d:", i)
77 bar := p.AddBar(100).PrependName(name, len(name), 0)
81 bar := p.AddBar(100, mpb.PrependDecorators(decor.Name(name, len(name), 0)))
7882
7983 go func() {
8084 defer wg.Done()
120124
121125 for i := 0; i < numBars; i++ {
122126 name := fmt.Sprintf("Bar#%d:", i)
123 bar := p.AddBarWithID(i, int64(total)).PrependName(name, len(name), 0)
127 bar := p.AddBar(int64(total), mpb.BarID(i),
128 mpb.PrependDecorators(decor.Name(name, len(name), 0)))
124129
125130 go func() {
126131 defer func() {
153158 }
154159 }
155160
156 func TestWithNilCancel(t *testing.T) {
157 defer func() {
158 if p := recover(); p != nil {
159 if msg, ok := p.(string); ok && msg == "nil cancel channel" {
160 return
161 }
162 t.Errorf("Expected nil channel panic, got: %+v", p)
163 }
164 }()
165 _ = mpb.New().WithCancel(nil)
166 }
167
168161 func TestCustomFormat(t *testing.T) {
169162 var buf bytes.Buffer
170163 cancel := make(chan struct{})
171164 customFormat := "╢▌▌░╟"
172 p := mpb.New().Format(customFormat).
173 WithCancel(cancel).
174 SetOut(&buf)
175 bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
165 p := mpb.New(mpb.Output(&buf), mpb.WithCancel(cancel), mpb.WithFormat(customFormat))
166 bar := p.AddBar(100, mpb.BarTrim())
176167
177168 go func() {
178169 for i := 0; i < 100; i++ {