diff --git a/bar_test.go b/bar_test.go index 3cb7981..480bad9 100644 --- a/bar_test.go +++ b/bar_test.go @@ -187,20 +187,17 @@ bar.ResumeFill(refillChar, int64(till)) for i := 0; i < total; i++ { - time.Sleep(10 * time.Millisecond) - bar.Incr(1) - } - - p.Stop() - - bytes := removeLastRune(buf.Bytes()) - - gotBar := string(bytes[len(bytes)-width:]) + bar.Increment() + } + + p.Stop() + wantBar := fmt.Sprintf("[%s%s]", strings.Repeat(string(refillChar), till-1), strings.Repeat("=", total-till-1)) - if gotBar != wantBar { - t.Errorf("Want bar: %s, got bar: %s\n", wantBar, gotBar) + + if !strings.Contains(buf.String(), wantBar) { + t.Errorf("Want bar: %s, got bar: %s\n", wantBar, buf.String()) } } @@ -235,16 +232,9 @@ p.Stop() - out := bytes.Split(removeLastRune(buf.Bytes()), []byte("\n")) - gotPanic := out[len(out)-1] wantPanic = fmt.Sprintf("b#%02d panic: %v", 2, wantPanic) - if string(gotPanic) != wantPanic { - t.Errorf("Want: %q, got: %q\n", wantPanic, gotPanic) - } -} - -func removeLastRune(bytes []byte) []byte { - _, size := utf8.DecodeLastRune(bytes) - return bytes[:len(bytes)-size] -} + if !strings.Contains(buf.String(), wantPanic) { + t.Errorf("Want: %q, got: %q\n", wantPanic, buf.String()) + } +} diff --git a/progress_test.go b/progress_test.go index 4e4729f..f1b8456 100644 --- a/progress_test.go +++ b/progress_test.go @@ -8,84 +8,36 @@ "sync" "testing" "time" - "unicode/utf8" "github.com/vbauerster/mpb" "github.com/vbauerster/mpb/decor" ) -func TestDefaultWidth(t *testing.T) { - var buf bytes.Buffer - p := mpb.New(mpb.Output(&buf)) - bar := p.AddBar(100, mpb.BarTrim()) - - for i := 0; i < 100; i++ { - bar.Incr(1) - } - p.Stop() - - wantWidth := 80 - gotWidth := utf8.RuneCount(buf.Bytes()) - if gotWidth != wantWidth+1 { // + 1 for new line - t.Errorf("Expected default width: %d, got: %d\n", wantWidth, gotWidth) - } -} - -func TestCustomWidth(t *testing.T) { - wantWidth := 60 - var buf bytes.Buffer - p := mpb.New(mpb.WithWidth(wantWidth), mpb.Output(&buf)) - bar := p.AddBar(100, mpb.BarTrim()) - - for i := 0; i < 100; i++ { - bar.Incr(1) - } - p.Stop() - - gotWidth := utf8.RuneCount(buf.Bytes()) - if gotWidth != wantWidth+1 { // +1 for new line - t.Errorf("Expected default width: %d, got: %d\n", wantWidth, gotWidth) - } -} - func TestAddBar(t *testing.T) { - var wg sync.WaitGroup - var buf bytes.Buffer - p := mpb.New(mpb.Output(&buf), mpb.WithWaitGroup(&wg)) + p := mpb.New() count := p.BarCount() if count != 0 { t.Errorf("BarCount want: %q, got: %q\n", 0, count) } - numBars := 3 - wg.Add(numBars) + bar := p.AddBar(100) - for i := 0; i < numBars; i++ { - name := fmt.Sprintf("Bar#%d:", i) - bar := p.AddBar(100, mpb.PrependDecorators(decor.StaticName(name, len(name), 0))) - - go func() { - defer wg.Done() - for i := 0; i < 100; i++ { - bar.Incr(1) - } - }() + count = p.BarCount() + if count != 1 { + t.Errorf("BarCount want: %q, got: %q\n", 1, count) } - count = p.BarCount() - if count != numBars { - t.Errorf("BarCount want: %q, got: %q\n", numBars, count) - } + bar.Complete() p.Stop() } func TestRemoveBar(t *testing.T) { p := mpb.New() - b := p.AddBar(10) + bar := p.AddBar(10) - if !p.RemoveBar(b) { + if !p.RemoveBar(bar) { t.Error("RemoveBar failure") } @@ -93,6 +45,8 @@ if count != 0 { t.Errorf("BarCount want: %q, got: %q\n", 0, count) } + + bar.Complete() p.Stop() } @@ -153,24 +107,16 @@ go func() { for i := 0; i < 100; i++ { time.Sleep(10 * time.Millisecond) - bar.Incr(1) + bar.Increment() } }() - time.Sleep(250 * time.Millisecond) + time.Sleep(300 * time.Millisecond) close(cancel) p.Stop() - bytes := removeLastRune(buf.Bytes()) - - seen := make(map[rune]bool) - for _, r := range string(bytes) { - if !seen[r] { - seen[r] = true - } - } for _, r := range customFormat { - if !seen[r] { + if !bytes.ContainsRune(buf.Bytes(), r) { t.Errorf("Rune %#U not found in bar\n", r) } }