diff --git a/bar_test.go b/bar_test.go index f97d5c7..8792859 100644 --- a/bar_test.go +++ b/bar_test.go @@ -7,6 +7,7 @@ "strings" "testing" "time" + "unicode/utf8" . "github.com/vbauerster/mpb" "github.com/vbauerster/mpb/decor" @@ -62,7 +63,7 @@ till := 30 refillRune := '+' - bar := p.AddBar(int64(total), BarTrim()) + bar := p.AddBar(int64(total), TrimSpace()) bar.SetRefill(till, refillRune) bar.IncrBy(till) @@ -76,10 +77,40 @@ wantBar := fmt.Sprintf("[%s%s]", strings.Repeat(string(refillRune), till-1), - strings.Repeat("=", total-till-1)) + strings.Repeat("=", total-till-1), + ) - if !strings.Contains(buf.String(), wantBar) { - t.Errorf("Want bar: %s, got bar: %s\n", wantBar, buf.String()) + got := string(getLastLine(buf.Bytes())) + + if got != wantBar { + t.Errorf("Want bar: %q, got bar: %q\n", wantBar, got) + } +} + +func TestBarStyle(t *testing.T) { + var buf bytes.Buffer + customFormat := "╢▌▌░╟" + p := New(WithOutput(&buf)) + total := 80 + bar := p.AddBar(int64(total), BarStyle(customFormat), TrimSpace()) + + for i := 0; i < total; i++ { + bar.Increment() + time.Sleep(10 * time.Millisecond) + } + + p.Wait() + + runes := []rune(customFormat) + wantBar := fmt.Sprintf("%s%s%s", + string(runes[0]), + strings.Repeat(string(runes[1]), total-2), + string(runes[len(runes)-1]), + ) + got := string(getLastLine(buf.Bytes())) + + if got != wantBar { + t.Errorf("Want bar: %q:%d, got bar: %q:%d\n", wantBar, utf8.RuneCountInString(wantBar), got, utf8.RuneCountInString(got)) } } diff --git a/draw_test.go b/draw_test.go index 77bf654..0da35c2 100644 --- a/draw_test.go +++ b/draw_test.go @@ -7,193 +7,224 @@ func TestDraw(t *testing.T) { // key is termWidth - testSuite := map[int]map[string]struct { + testSuite := map[int][]struct { + name string total, current int64 barWidth int barRefill *refill + trimSpace bool want string }{ 100: { - "t,c,bw{100,100,0}": { + { + name: "t,c,bw{100,100,0}", total: 100, current: 0, barWidth: 100, - want: "[--------------------------------------------------------------------------------------------------]", - }, - "t,c,bw{100,1,100}": { + want: " [------------------------------------------------------------------------------------------------] ", + }, + { + name: "t,c,bw{100,100,0}:trimSpace", + total: 100, + current: 0, + barWidth: 100, + trimSpace: true, + want: "[--------------------------------------------------------------------------------------------------]", + }, + { + name: "t,c,bw{100,1,100}", total: 100, current: 1, barWidth: 100, - want: "[>-------------------------------------------------------------------------------------------------]", - }, - "t,c,bw{100,40,100}": { + want: " [>-----------------------------------------------------------------------------------------------] ", + }, + { + name: "t,c,bw{100,1,100}:trimSpace", + total: 100, + current: 1, + barWidth: 100, + trimSpace: true, + want: "[>-------------------------------------------------------------------------------------------------]", + }, + { + name: "t,c,bw{100,40,100}", total: 100, current: 40, barWidth: 100, - want: "[======================================>-----------------------------------------------------------]", - }, - "t,c,bw{100,40,100}refill{'+', 32}": { + want: " [=====================================>----------------------------------------------------------] ", + }, + { + name: "t,c,bw{100,40,100}:refill{'+', 32}", total: 100, current: 40, barWidth: 100, barRefill: &refill{'+', 32}, + want: " [+++++++++++++++++++++++++++++++======>----------------------------------------------------------] ", + }, + { + name: "t,c,bw{100,40,100}:refill{'+', 32}:trimSpace", + total: 100, + current: 40, + barWidth: 100, + barRefill: &refill{'+', 32}, + trimSpace: true, want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]", }, - "t,c,bw{100,99,100}": { + { + name: "t,c,bw{100,99,100}", total: 100, current: 99, barWidth: 100, - want: "[================================================================================================>-]", - }, - "t,c,bw{100,100,100}": { - total: 100, - current: 100, - barWidth: 100, - want: "[==================================================================================================]", + want: " [==============================================================================================>-] ", + }, + { + name: "t,c,bw{100,100,100}", + total: 100, + current: 100, + barWidth: 100, + want: " [================================================================================================] ", }, }, 2: { - "t,c,bw{0,0,100}": { - barWidth: 100, - want: "[]", - }, - "t,c,bw{60,20,80}": { + { + name: "t,c,bw{0,0,100}", + barWidth: 100, + want: " [] ", + }, + { + name: "t,c,bw{60,20,80}", total: 60, current: 20, barWidth: 80, - want: "[]", - }, - }, - 3: { - "t,c,bw{100,20,100}": { - total: 100, - current: 20, - barWidth: 100, - want: "[-]", - }, - "t,c,bw{100,98,100}": { - total: 100, - current: 98, - barWidth: 100, - want: "[=]", - }, - "t,c,bw{100,100,100}": { - total: 100, - current: 100, - barWidth: 100, - want: "[=]", - }, - }, - 5: { - "t,c,bw{100,20,100}": { - total: 100, - current: 20, - barWidth: 100, - want: "[>--]", - }, - "t,c,bw{100,98,100}": { - total: 100, - current: 98, - barWidth: 100, - want: "[===]", - }, - "t,c,bw{100,100,100}": { - total: 100, - current: 100, - barWidth: 100, - want: "[===]", - }, - }, - 6: { - "t,c,bw{100,20,100}": { - total: 100, - current: 20, - barWidth: 100, - want: "[>---]", - }, - "t,c,bw{100,98,100}": { - total: 100, - current: 98, - barWidth: 100, - want: "[====]", - }, - "t,c,bw{100,100,100}": { - total: 100, - current: 100, - barWidth: 100, - want: "[====]", + want: " [] ", + }, + }, + 4: { + { + name: "t,c,bw{100,20,100}", + total: 100, + current: 20, + barWidth: 100, + want: " [] ", + }, + { + name: "t,c,bw{100,98,100}", + total: 100, + current: 98, + barWidth: 100, + want: " [] ", + }, + { + name: "t,c,bw{100,100,100}", + total: 100, + current: 100, + barWidth: 100, + want: " [] ", + }, + }, + 8: { + { + name: "t,c,bw{100,20,100}", + total: 100, + current: 20, + barWidth: 100, + want: " [>---] ", + }, + { + name: "t,c,bw{100,98,100}", + total: 100, + current: 98, + barWidth: 100, + want: " [====] ", + }, + { + name: "t,c,bw{100,100,100}", + total: 100, + current: 100, + barWidth: 100, + want: " [====] ", }, }, 20: { - "t,c,bw{100,20,100}": { - total: 100, - current: 20, - barWidth: 100, - want: "[===>--------------]", - }, - "t,c,bw{100,60,100}": { + { + name: "t,c,bw{100,20,100}", + total: 100, + current: 20, + barWidth: 100, + want: " [==>-------------] ", + }, + { + name: "t,c,bw{100,60,100}", total: 100, current: 60, barWidth: 100, - want: "[==========>-------]", - }, - "t,c,bw{100,98,100}": { - total: 100, - current: 98, - barWidth: 100, - want: "[==================]", - }, - "t,c,bw{100,100,100}": { - total: 100, - current: 100, - barWidth: 100, - want: "[==================]", + want: " [=========>------] ", + }, + { + name: "t,c,bw{100,98,100}", + total: 100, + current: 98, + barWidth: 100, + want: " [================] ", + }, + { + name: "t,c,bw{100,100,100}", + total: 100, + current: 100, + barWidth: 100, + want: " [================] ", }, }, 50: { - "t,c,bw{100,20,100}": { - total: 100, - current: 20, - barWidth: 100, - want: "[=========>--------------------------------------]", - }, - "t,c,bw{100,60,100}": { + { + name: "t,c,bw{100,20,100}", + total: 100, + current: 20, + barWidth: 100, + want: " [========>-------------------------------------] ", + }, + { + name: "t,c,bw{100,60,100}", total: 100, current: 60, barWidth: 100, - want: "[============================>-------------------]", - }, - "t,c,bw{100,98,100}": { - total: 100, - current: 98, - barWidth: 100, - want: "[==============================================>-]", - }, - "t,c,bw{100,100,100}": { - total: 100, - current: 100, - barWidth: 100, - want: "[================================================]", + want: " [===========================>------------------] ", + }, + { + name: "t,c,bw{100,98,100}", + total: 100, + current: 98, + barWidth: 100, + want: " [============================================>-] ", + }, + { + name: "t,c,bw{100,100,100}", + total: 100, + current: 100, + barWidth: 100, + want: " [==============================================] ", }, }, } var tmpBuf bytes.Buffer for termWidth, cases := range testSuite { - for name, tc := range cases { + for _, tc := range cases { s := newTestState() s.width = tc.barWidth s.total = tc.total s.current = tc.current + s.trimSpace = tc.trimSpace if tc.barRefill != nil { - s.refill = tc.barRefill + s.filler.(*barFiller).refill = tc.barRefill } tmpBuf.Reset() tmpBuf.ReadFrom(s.draw(termWidth)) - got := tmpBuf.String() - want := tc.want + "\n" - if got != want { - t.Errorf("termWidth %d; %s: want: %s %d, got: %s %d\n", termWidth, name, want, len(want), got, len(got)) + by := tmpBuf.Bytes() + by = by[:len(by)-1] + got := string(by) + if got != tc.want { + t.Errorf("termWidth %d; %s: want: %q %d, got: %q %d\n", termWidth, tc.name, tc.want, len(tc.want), got, len(got)) } } } @@ -201,12 +232,10 @@ func newTestState() *bState { s := &bState{ - trimLeftSpace: true, - trimRightSpace: true, - bufP: new(bytes.Buffer), - bufB: new(bytes.Buffer), - bufA: new(bytes.Buffer), + filler: &barFiller{format: defaultBarStyle}, + bufP: new(bytes.Buffer), + bufB: new(bytes.Buffer), + bufA: new(bytes.Buffer), } - s.runes = strToBarRunes(pformat) return s } diff --git a/example_test.go b/example_test.go index 97f1ded..2f4f9d3 100644 --- a/example_test.go +++ b/example_test.go @@ -15,8 +15,6 @@ p := mpb.New( // override default (80) width mpb.WithWidth(64), - // override default "[=>-]" format - mpb.WithFormat("╢▌▌░╟"), // override default 120ms refresh rate mpb.WithRefreshRate(180*time.Millisecond), ) @@ -25,6 +23,8 @@ name := "Single Bar:" // adding a single bar bar := p.AddBar(int64(total), + // override default "[=>-]" style + mpb.BarStyle("╢▌▌░╟"), mpb.PrependDecorators( // display our name with one space on the right decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}), diff --git a/progress_test.go b/progress_test.go index 0c4388c..2331a3f 100644 --- a/progress_test.go +++ b/progress_test.go @@ -112,32 +112,6 @@ } } -func TestWithFormat(t *testing.T) { - var buf bytes.Buffer - customFormat := "╢▌▌░╟" - p := New(WithOutput(&buf), WithFormat(customFormat)) - bar := p.AddBar(100, BarTrim()) - - for i := 0; i < 100; i++ { - if i == 33 { - p.Abort(bar, true) - break - } - time.Sleep(randomDuration(100 * time.Millisecond)) - bar.Increment() - } - - p.Wait() - - lastLine := getLastLine(buf.Bytes()) - - for _, r := range customFormat { - if !bytes.ContainsRune(lastLine, r) { - t.Errorf("Rune %#U not found in bar\n", r) - } - } -} - func getLastLine(bb []byte) []byte { split := bytes.Split(bb, []byte("\n")) lastLine := split[len(split)-2] diff --git a/proxyreader_test.go b/proxyreader_test.go index d1c0862..7006674 100644 --- a/proxyreader_test.go +++ b/proxyreader_test.go @@ -1,7 +1,6 @@ package mpb_test import ( - "bytes" "io" "io/ioutil" "strings" @@ -18,27 +17,36 @@ cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.` +type testReader struct { + io.Reader + called bool +} + +func (r *testReader) Read(p []byte) (n int, err error) { + r.called = true + return r.Reader.Read(p) +} + func TestProxyReader(t *testing.T) { - var buf bytes.Buffer - p := mpb.New(mpb.WithOutput(&buf)) - reader := strings.NewReader(content) + p := mpb.New(mpb.WithOutput(ioutil.Discard)) + + reader := &testReader{Reader: strings.NewReader(content)} total := len(content) - bar := p.AddBar(100, mpb.BarTrim()) - preader := bar.ProxyReader(reader) + bar := p.AddBar(100, mpb.TrimSpace()) - if _, ok := preader.(io.Closer); !ok { - t.Error("type assertion to io.Closer is not ok") - } - - written, err := io.Copy(ioutil.Discard, preader) + written, err := io.Copy(ioutil.Discard, bar.ProxyReader(reader)) if err != nil { t.Errorf("Error copying from reader: %+v\n", err) } p.Wait() + if !reader.called { + t.Error("Read not called") + } + if written != int64(total) { t.Errorf("Expected written: %d, got: %d\n", total, written) }