Codebase list golang-github-vbauerster-mpb / 7e06554
TestBarIncrWithReFill Vladimir Bauer 9 years ago
1 changed file(s) with 34 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "bytes"
4 "fmt"
5 "strings"
46 "sync"
57 "testing"
68 "time"
187189 p.Stop()
188190 }
189191
192 func TestBarIncrWithReFill(t *testing.T) {
193 var buf bytes.Buffer
194
195 width := 100
196 p := mpb.New().SetWidth(width).SetOut(&buf)
197
198 total := 100
199 refill := 30
200 delta := total - refill
201 refillChar := '+'
202 bar := p.AddBar(int64(total)).TrimLeftSpace().TrimRightSpace()
203
204 bar.IncrWithReFill(refill, &mpb.Refill{Char: refillChar})
205
206 for i := 0; i < delta; i++ {
207 time.Sleep(10 * time.Millisecond)
208 bar.Incr(1)
209 }
210
211 p.Stop()
212
213 bytes := removeLastRune(buf.Bytes())
214
215 gotBar := string(bytes[len(bytes)-width:])
216 wantBar := fmt.Sprintf("[%s%s]",
217 strings.Repeat(string(refillChar), refill-1),
218 strings.Repeat("=", delta-1))
219 if gotBar != wantBar {
220 t.Errorf("Want bar: %s, got bar: %s\n", wantBar, gotBar)
221 }
222 }
223
190224 func removeLastRune(bytes []byte) []byte {
191225 _, size := utf8.DecodeLastRune(bytes)
192226 return bytes[:len(bytes)-size]