Skip to content

Commit

Permalink
Expanded Theme tests, and covered new BarStartFilled and BarEndFilled.
Browse files Browse the repository at this point in the history
  • Loading branch information
janpfeifer committed Oct 3, 2024
1 parent d1e3b99 commit e026913
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
14 changes: 6 additions & 8 deletions progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,10 +867,12 @@ func (p *ProgressBar) render() error {
}

if !p.config.useANSICodes {
// first, clear the existing progress bar
err := clearProgressBar(p.config, p.state)
if err != nil {
return err
// first, clear the existing progress bar, if not yet finished.
if !p.state.finished {
err := clearProgressBar(p.config, p.state)
if err != nil {
return err
}
}
}

Expand All @@ -897,10 +899,6 @@ func (p *ProgressBar) render() error {
if err != nil {
return err
}
} else if !p.config.clearOnFinish {
// Since bar was cleared, re-render it.
io.Copy(p.config.writer, &p.config.stdBuffer)
renderProgressBar(p.config, &p.state)
}
return nil
}
Expand Down
57 changes: 55 additions & 2 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,16 +452,69 @@ func TestOptionSetTheme(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(
10,
OptionSetTheme(Theme{Saucer: "#", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
OptionSetTheme(
Theme{Saucer: "#", SaucerPadding: "-",
BarStart: ">", BarEnd: "<"}),
OptionSetWidth(10),
OptionSetWriter(&buf),
)
bar.RenderBlank()
result := strings.TrimSpace(buf.String())
expect := "0% >----------<"
if strings.Index(result, expect) == -1 {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
buf.Reset()

bar.Add(5)
result = strings.TrimSpace(buf.String())
expect = "50% >#####-----< [0s:0s]"
if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
buf.Reset()

bar.Finish()
result = strings.TrimSpace(buf.String())
expect = "100% >##########<"
if strings.Index(result, expect) == -1 {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
}

func TestOptionSetThemeFilled(t *testing.T) {
buf := strings.Builder{}
bar := NewOptions(
10,
OptionSetTheme(
Theme{Saucer: "#", SaucerPadding: "-",
BarStart: ">", BarStartFilled: "]",
BarEnd: "<", BarEndFilled: "["}),
OptionSetWidth(10),
OptionSetWriter(&buf),
)
bar.RenderBlank()
result := strings.TrimSpace(buf.String())
expect := "50% >#####-----< [0s:0s]"
expect := "0% >----------<"
if strings.Index(result, expect) == -1 {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
buf.Reset()

bar.Add(5)
result = strings.TrimSpace(buf.String())
expect = "50% ]#####-----< [0s:0s]"
if result != expect {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
buf.Reset()

bar.Finish()
result = strings.TrimSpace(buf.String())
expect = "100% ]##########["
if strings.Index(result, expect) == -1 {
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
}
}

// TestOptionSetPredictTime ensures that when predict time is turned off, the progress
Expand Down

0 comments on commit e026913

Please sign in to comment.