diff --git a/bar_test.go b/bar_test.go index de4171c..7a7a3e9 100644 --- a/bar_test.go +++ b/bar_test.go @@ -63,9 +63,8 @@ close(cancel) p.Stop() - bytes := buf.Bytes() - _, size := utf8.DecodeLastRune(bytes) - bytes = bytes[:len(bytes)-size] // removing new line + // removing new line + bytes := removeLastRune(buf.Bytes()) seen := make(map[rune]bool) for _, r := range string(bytes) { @@ -77,6 +76,29 @@ if !seen[r] { t.Errorf("Rune %#U not found in bar\n", r) } + } +} + +func TestBarInvalidFormat(t *testing.T) { + var buf bytes.Buffer + customWidth := 60 + p := mpb.New().SetWidth(customWidth).SetOut(&buf) + customFormat := "(#>=_)" + bar := p.AddBar(100).Format(customFormat). + TrimLeftSpace().TrimRightSpace() + + for i := 0; i < 100; i++ { + time.Sleep(10 * time.Millisecond) + bar.Incr(1) + } + + p.Stop() + + bytes := removeLastRune(buf.Bytes()) + got := string(bytes[len(bytes)-customWidth:]) + want := "[==========================================================]" + if got != want { + t.Errorf("Expected format: %s, got %s\n", want, got) } } @@ -106,3 +128,8 @@ t.Error("bar.InProgress returns true after cancel") } } + +func removeLastRune(bytes []byte) []byte { + _, size := utf8.DecodeLastRune(bytes) + return bytes[:len(bytes)-size] +}