TestBarIncrWithReFill
Vladimir Bauer
9 years ago
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | 3 | "bytes" |
| 4 | "fmt" | |
| 5 | "strings" | |
| 4 | 6 | "sync" |
| 5 | 7 | "testing" |
| 6 | 8 | "time" |
| 187 | 189 | p.Stop() |
| 188 | 190 | } |
| 189 | 191 | |
| 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 | ||
| 190 | 224 | func removeLastRune(bytes []byte) []byte { |
| 191 | 225 | _, size := utf8.DecodeLastRune(bytes) |
| 192 | 226 | return bytes[:len(bytes)-size] |