diff --git a/bar_test.go b/bar_test.go index 1f7fa0d..38b42c8 100644 --- a/bar_test.go +++ b/bar_test.go @@ -1,111 +1,81 @@ -package mpb +package mpb_test import ( - "reflect" + "bytes" "testing" + "time" + "unicode/utf8" + + "github.com/vbauerster/mpb" ) -func TestFillBar(t *testing.T) { - tests := []struct { - termWidth int - barWidth int - total int64 - current int64 - barRefill *Refill - want []byte - }{ - { - termWidth: 1, - barWidth: 100, - want: []byte{}, - }, - { - termWidth: 2, - barWidth: 100, - total: 100, - current: 20, - want: []byte("[]"), - }, - { - termWidth: 20, - barWidth: 100, - total: 100, - current: 20, - want: []byte("[===>--------------]"), - }, - { - termWidth: 50, - barWidth: 100, - total: 100, - current: 20, - want: []byte("[=========>--------------------------------------]"), - }, - { - termWidth: 100, - barWidth: 100, - total: 100, - current: 0, - want: []byte("[--------------------------------------------------------------------------------------------------]"), - }, - { - termWidth: 100, - barWidth: 100, - total: 100, - current: 1, - want: []byte("[>-------------------------------------------------------------------------------------------------]"), - }, - { - termWidth: 100, - barWidth: 100, - total: 100, - current: 40, - want: []byte("[======================================>-----------------------------------------------------------]"), - }, - { - termWidth: 100, - barWidth: 100, - total: 100, - current: 40, - barRefill: &Refill{'+', 32}, - want: []byte("[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]"), - }, - { - termWidth: 100, - barWidth: 100, - total: 100, - current: 99, - want: []byte("[================================================================================================>-]"), - }, - { - termWidth: 100, - barWidth: 100, - total: 100, - current: 100, - want: []byte("[==================================================================================================]"), - }, +func TestBarSetWidth(t *testing.T) { + var buf bytes.Buffer + p := mpb.New().SetOut(&buf) + // overwrite default width 80 + customWidth := 60 + bar := p.AddBar(100).SetWidth(customWidth). + TrimLeftSpace().TrimRightSpace() + for i := 0; i < 100; i++ { + bar.Incr(1) } + p.Stop() - prependWs := newWidthSync(nil, 1, 0) - appendWs := newWidthSync(nil, 1, 0) - for _, test := range tests { - s := newTestState() - s.width = test.barWidth - s.total = test.total - s.current = test.current - if test.barRefill != nil { - s.refill = test.barRefill + gotWidth := len(buf.Bytes()) + if gotWidth != customWidth+1 { // +1 for new line + t.Errorf("Expected width: %d, got: %d\n", customWidth, gotWidth) + } +} + +func TestBarSetInvalidWidth(t *testing.T) { + var buf bytes.Buffer + p := mpb.New().SetOut(&buf) + bar := p.AddBar(100).SetWidth(1). + TrimLeftSpace().TrimRightSpace() + for i := 0; i < 100; i++ { + bar.Incr(1) + } + p.Stop() + + wantWidth := 80 + gotWidth := len(buf.Bytes()) + if gotWidth != wantWidth+1 { // +1 for new line + t.Errorf("Expected width: %d, got: %d\n", wantWidth, gotWidth) + } +} + +func TestBarFormat(t *testing.T) { + var buf bytes.Buffer + cancel := make(chan struct{}) + p := mpb.New().WithCancel(cancel).SetOut(&buf) + customFormat := "(#>_)" + bar := p.AddBar(100).Format(customFormat). + TrimLeftSpace().TrimRightSpace() + + go func() { + for i := 0; i < 100; i++ { + time.Sleep(10 * time.Millisecond) + bar.Incr(1) } - got := draw(s, test.termWidth, prependWs, appendWs) - if !reflect.DeepEqual(test.want, got) { - t.Errorf("Want: %q, Got: %q\n", test.want, got) + }() + + time.Sleep(250 * time.Millisecond) + close(cancel) + p.Stop() + + bytes := buf.Bytes() + _, size := utf8.DecodeLastRune(bytes) + bytes = bytes[:len(bytes)-size] // removing new line + + seen := make(map[rune]bool) + for _, r := range string(bytes) { + if !seen[r] { + seen[r] = true + } + } + for _, r := range customFormat { + if !seen[r] { + t.Errorf("Rune %#U not found in bar\n", r) } } } - -func newTestState() *state { - return &state{ - format: barFmtRunes{'[', '=', '>', '-', ']'}, - trimLeftSpace: true, - trimRightSpace: true, - } -}