diff --git a/cwriter/export_test.go b/cwriter/export_test.go new file mode 100644 index 0000000..1219508 --- /dev/null +++ b/cwriter/export_test.go @@ -0,0 +1,3 @@ +package cwriter + +var ClearCursorAndLine = clearCursorAndLine diff --git a/cwriter/writer_posix_test.go b/cwriter/writer_posix_test.go index c936954..42cc40e 100644 --- a/cwriter/writer_posix_test.go +++ b/cwriter/writer_posix_test.go @@ -4,30 +4,23 @@ import ( "bytes" - "fmt" "testing" - "github.com/vbauerster/mpb/cwriter" -) - -var ( - cursorUp = fmt.Sprintf("%c[%dA", cwriter.ESC, 1) - clearLine = fmt.Sprintf("%c[2K\r", cwriter.ESC) - clearCursorAndLine = cursorUp + clearLine + . "github.com/vbauerster/mpb/cwriter" ) // TestWriterPosix by writing and flushing many times. The output buffer // must contain the clearCursor and clearLine sequences. func TestWriterPosix(t *testing.T) { out := new(bytes.Buffer) - w := cwriter.New(out) + w := New(out) for _, tcase := range []struct { input, expectedOutput string }{ {input: "foo\n", expectedOutput: "foo\n"}, - {input: "bar\n", expectedOutput: "foo\n" + clearCursorAndLine + "bar\n"}, - {input: "fizz\n", expectedOutput: "foo\n" + clearCursorAndLine + "bar\n" + clearCursorAndLine + "fizz\n"}, + {input: "bar\n", expectedOutput: "foo\n" + ClearCursorAndLine + "bar\n"}, + {input: "fizz\n", expectedOutput: "foo\n" + ClearCursorAndLine + "bar\n" + ClearCursorAndLine + "fizz\n"}, } { t.Run(tcase.input, func(t *testing.T) { w.Write([]byte(tcase.input)) diff --git a/draw_test.go b/draw_test.go index dab45ad..ef5fd03 100644 --- a/draw_test.go +++ b/draw_test.go @@ -5,7 +5,7 @@ "testing" ) -func TestFillBar(t *testing.T) { +func TestDraw(t *testing.T) { // key is termWidth testSuite := map[int]map[string]struct { total, current int64 @@ -180,6 +180,7 @@ prependWs := newWidthSyncer(nil, 1, 0) appendWs := newWidthSyncer(nil, 1, 0) + var temp bytes.Buffer for termWidth, cases := range testSuite { for name, tc := range cases { s := newTestState() @@ -189,10 +190,11 @@ if tc.barRefill != nil { s.refill = tc.barRefill } - s.draw(termWidth, prependWs, appendWs) - got := s.bufB.String() - if got != tc.want { - t.Errorf("termWidth %d; %s: want: %s %d, got: %s %d\n", termWidth, name, tc.want, len(tc.want), got, len(got)) + temp.Reset() + temp.ReadFrom(s.draw(termWidth, prependWs, appendWs)) + got := temp.String() + if got != tc.want+"\n" { + t.Errorf("termWidth %d; %s: want: %s %d, got: %s %d\n", termWidth, name, tc.want+"\n", len(tc.want), got, len(got)) } } } @@ -206,6 +208,6 @@ bufB: new(bytes.Buffer), bufA: new(bytes.Buffer), } - s.updateFormat("[=>-]") + s.runes = strToBarRunes("[=>-]") return s } diff --git a/progress_test.go b/progress_test.go index 44783a6..0f62385 100644 --- a/progress_test.go +++ b/progress_test.go @@ -5,12 +5,12 @@ "fmt" "io/ioutil" "math/rand" - "strings" "sync" "testing" "time" - "github.com/vbauerster/mpb" + . "github.com/vbauerster/mpb" + "github.com/vbauerster/mpb/cwriter" ) func init() { @@ -18,7 +18,7 @@ } func TestBarCount(t *testing.T) { - p := mpb.New(mpb.WithOutput(ioutil.Discard)) + p := New(WithOutput(ioutil.Discard)) var wg sync.WaitGroup wg.Add(1) @@ -44,11 +44,11 @@ } func TestBarAbort(t *testing.T) { - p := mpb.New(mpb.WithOutput(ioutil.Discard)) + p := New(WithOutput(ioutil.Discard)) var wg sync.WaitGroup wg.Add(1) - bars := make([]*mpb.Bar, 3) + bars := make([]*Bar, 3) for i := 0; i < 3; i++ { b := p.AddBar(100) bars[i] = b @@ -77,20 +77,20 @@ func TestWithCancel(t *testing.T) { cancel := make(chan struct{}) shutdown := make(chan struct{}) - p := mpb.New( - mpb.WithOutput(ioutil.Discard), - mpb.WithCancel(cancel), - mpb.WithShutdownNotifier(shutdown), + p := New( + WithOutput(ioutil.Discard), + WithCancel(cancel), + WithShutdownNotifier(shutdown), ) numBars := 3 - bars := make([]*mpb.Bar, 0, numBars) + bars := make([]*Bar, 0, numBars) for i := 0; i < numBars; i++ { - bar := p.AddBar(int64(1000), mpb.BarID(i)) + bar := p.AddBar(int64(1000), BarID(i)) bars = append(bars, bar) go func() { for !bar.Completed() { - time.Sleep(randomDuration(40 * time.Millisecond)) + time.Sleep(randomDuration(100 * time.Millisecond)) bar.Increment() } }() @@ -101,74 +101,45 @@ }) p.Wait() - for _, bar := range bars { - if bar.Current() >= bar.Total() { - t.Errorf("bar %d: total = %d, current = %d\n", bar.ID(), bar.Total(), bar.Current()) - } - } + select { case <-shutdown: - case <-time.After(100 * time.Millisecond): - t.Error("Progress didn't stop") + case <-time.After(200 * time.Millisecond): + t.FailNow() } } -func TestCustomFormat(t *testing.T) { +var ( + cursorUp = fmt.Sprintf("%c[%dA", cwriter.ESC, 1) + clearLine = fmt.Sprintf("%c[2K\r", cwriter.ESC) + clearCursorAndLine = cursorUp + clearLine +) + +func TestWithFormat(t *testing.T) { var buf bytes.Buffer - cancel := make(chan struct{}) customFormat := "╢▌▌░╟" - p := mpb.New( - mpb.WithOutput(&buf), - mpb.WithCancel(cancel), - mpb.WithFormat(customFormat), - ) - bar := p.AddBar(80, mpb.BarTrim()) - - var wg sync.WaitGroup - wg.Add(1) - go func() { - for i := 0; i < 80; i++ { - if i == 33 { - wg.Done() - } - time.Sleep(randomDuration(80 * time.Millisecond)) - bar.Increment() - } - }() - - wg.Wait() - close(cancel) - p.Wait() - - for _, r := range customFormat { - if !bytes.ContainsRune(buf.Bytes(), r) { - t.Errorf("Rune %#U not found in bar\n", r) - } - } -} - -func TestInvalidFormatWidth(t *testing.T) { - var buf bytes.Buffer - customWidth := 60 - customFormat := "(#>=_)" - p := mpb.New( - mpb.WithOutput(&buf), - mpb.WithWidth(customWidth), - mpb.WithFormat(customFormat), - ) - bar := p.AddBar(100, mpb.BarTrim()) + p := New(WithOutput(&buf), WithFormat(customFormat)) + bar := p.AddBar(100, BarTrim()) for i := 0; i < 100; i++ { - time.Sleep(randomDuration(40 * time.Millisecond)) + if i == 33 { + p.Abort(bar) + break + } + time.Sleep(randomDuration(100 * time.Millisecond)) bar.Increment() } p.Wait() - got := buf.String() - want := fmt.Sprintf("[%s]", strings.Repeat("=", customWidth-2)) - if !strings.Contains(got, want) { - t.Errorf("Expected format: %s, got %s\n", want, got) + bb := bytes.Split(buf.Bytes(), []byte("\n")) + lastLine := bb[len(bb)-2] + lastLine = lastLine[len(clearCursorAndLine):] + + for _, r := range customFormat { + if !bytes.ContainsRune(lastLine, r) { + t.Errorf("Rune %#U not found in bar\n", r) + } } }