some decorators_test
Vladimir Bauer
9 years ago
| 0 | package mpb_test | |
| 1 | ||
| 2 | import ( | |
| 3 | "bytes" | |
| 4 | "fmt" | |
| 5 | "io" | |
| 6 | "io/ioutil" | |
| 7 | "strings" | |
| 8 | "testing" | |
| 9 | ||
| 10 | "github.com/vbauerster/mpb" | |
| 11 | ) | |
| 12 | ||
| 13 | func TestPrependName(t *testing.T) { | |
| 14 | var buf bytes.Buffer | |
| 15 | p := mpb.New().SetOut(&buf) | |
| 16 | wantName := "TestBar" | |
| 17 | bar := p.AddBar(100).PrependName(wantName, 0, 0) | |
| 18 | for i := 0; i < 100; i++ { | |
| 19 | bar.Incr(1) | |
| 20 | } | |
| 21 | p.Stop() | |
| 22 | if !strings.Contains(buf.String(), wantName) { | |
| 23 | t.Errorf("%q not found in bar\n", wantName) | |
| 24 | } | |
| 25 | } | |
| 26 | ||
| 27 | func TestPrependCounters(t *testing.T) { | |
| 28 | var buf bytes.Buffer | |
| 29 | p := mpb.New().SetOut(&buf) | |
| 30 | ||
| 31 | reader := strings.NewReader(content) | |
| 32 | ||
| 33 | total := int64(len(content)) | |
| 34 | bar := p.AddBar(total).TrimLeftSpace().TrimRightSpace(). | |
| 35 | PrependCounters("%3s / %3s", mpb.UnitBytes, 0, 0) | |
| 36 | preader := bar.ProxyReader(reader) | |
| 37 | ||
| 38 | _, err := io.Copy(ioutil.Discard, preader) | |
| 39 | if err != nil { | |
| 40 | t.Errorf("Error copying from reader: %+v\n", err) | |
| 41 | } | |
| 42 | ||
| 43 | p.Stop() | |
| 44 | ||
| 45 | out := buf.String() | |
| 46 | out = out[:strings.IndexRune(out, '[')] | |
| 47 | want := fmt.Sprintf("%[1]db / %[1]db", total) | |
| 48 | if out != want { | |
| 49 | t.Errorf("Expected: %s, got %s\n", want, out) | |
| 50 | } | |
| 51 | } |