diff --git a/bar_test.go b/bar_test.go index 9bd3702..c5020bc 100644 --- a/bar_test.go +++ b/bar_test.go @@ -8,48 +8,13 @@ "sync" "testing" "time" - "unicode/utf8" "github.com/vbauerster/mpb" "github.com/vbauerster/mpb/decor" ) -func TestWithWidth(t *testing.T) { - cases := map[string]struct{ w, expected int }{ - "WithWidth-1": {-1, 81}, - "WithWidth0": {0, 3}, - "WithWidth1": {1, 3}, - "WithWidth2": {2, 3}, - "WithWidth3": {3, 4}, - "WithWidth60": {60, 61}, - } - - var buf bytes.Buffer - for k, tc := range cases { - buf.Reset() - p := mpb.New( - mpb.Output(&buf), - mpb.WithWidth(tc.w), - ) - bar := p.AddBar(10, mpb.BarTrim()) - - for i := 0; i < 10; i++ { - bar.Increment() - } - - p.Wait() - - gotWidth := utf8.RuneCount(buf.Bytes()) - if gotWidth != tc.expected { - t.Errorf("%s: Expected width: %d, got: %d\n", k, tc.expected, gotWidth) - } - } -} - func TestBarCompleted(t *testing.T) { - p := mpb.New( - mpb.Output(ioutil.Discard), - ) + p := mpb.New(mpb.Output(ioutil.Discard)) total := 80 bar := p.AddBar(int64(total)) diff --git a/progress_test.go b/progress_test.go index 1a3505a..b12a24b 100644 --- a/progress_test.go +++ b/progress_test.go @@ -17,19 +17,19 @@ rand.Seed(time.Now().UnixNano()) } -func TestAddBar(t *testing.T) { +func TestBarCount(t *testing.T) { p := mpb.New(mpb.Output(ioutil.Discard)) var wg sync.WaitGroup wg.Add(1) - b := p.AddBar(80) + b := p.AddBar(100) go func() { - for i := 0; i < 80; i++ { + for i := 0; i < 100; i++ { if i == 33 { wg.Done() } b.Increment() - time.Sleep(randomDuration(80 * time.Millisecond)) + time.Sleep(randomDuration(100 * time.Millisecond)) } }() @@ -39,39 +39,38 @@ t.Errorf("BarCount want: %q, got: %q\n", 1, count) } - b.Complete() + p.Abort(b) p.Wait() } -func TestRemoveBars(t *testing.T) { +func TestBarAbort(t *testing.T) { p := mpb.New(mpb.Output(ioutil.Discard)) var wg sync.WaitGroup + wg.Add(1) bars := make([]*mpb.Bar, 3) for i := 0; i < 3; i++ { - wg.Add(1) - b := p.AddBar(80) + b := p.AddBar(100) bars[i] = b - go func() { - for i := 0; i < 80; i++ { - if i == 33 { + go func(n int) { + for i := 0; i < 100; i++ { + if n == 0 && i == 33 { + p.Abort(b) wg.Done() } b.Increment() - time.Sleep(randomDuration(80 * time.Millisecond)) + time.Sleep(randomDuration(100 * time.Millisecond)) } - }() + }(i) } wg.Wait() - for i := 0; i < 3; i++ { - i := i - go func() { - if ok := p.RemoveBar(bars[i]); !ok { - t.Errorf("bar %d: remove failed\n", i) - } - }() + count := p.BarCount() + if count != 2 { + t.Errorf("BarCount want: %q, got: %q\n", 2, count) } + p.Abort(bars[1]) + p.Abort(bars[2]) p.Wait() }