TestInvalidWidth
Vladimir Bauer
9 years ago
| 19 | 19 | bar.Incr(1) |
| 20 | 20 | } |
| 21 | 21 | p.Stop() |
| 22 | runeCount := utf8.RuneCountInString(buf.String()) | |
| 23 | defWidth := 81 // + 1 for new line | |
| 24 | if runeCount != defWidth { | |
| 25 | t.Errorf("Expected default width: %d, got: %d\n", defWidth, runeCount) | |
| 22 | ||
| 23 | wantWidth := 80 | |
| 24 | gotWidth := utf8.RuneCount(buf.Bytes()) | |
| 25 | if gotWidth != wantWidth+1 { // + 1 for new line | |
| 26 | t.Errorf("Expected default width: %d, got: %d\n", wantWidth, gotWidth) | |
| 26 | 27 | } |
| 27 | 28 | } |
| 28 | 29 | |
| 29 | 30 | func TestCustomWidth(t *testing.T) { |
| 30 | customWidth := 60 | |
| 31 | var buf bytes.Buffer | |
| 32 | p := mpb.New().SetWidth(customWidth).SetOut(&buf) | |
| 31 | wantWidth := 60 | |
| 32 | var buf bytes.Buffer | |
| 33 | p := mpb.New().SetWidth(wantWidth).SetOut(&buf) | |
| 33 | 34 | bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace() |
| 34 | 35 | for i := 0; i < 100; i++ { |
| 35 | 36 | bar.Incr(1) |
| 36 | 37 | } |
| 37 | 38 | p.Stop() |
| 38 | runeCount := utf8.RuneCountInString(buf.String()) | |
| 39 | if runeCount != customWidth+1 { // +1 for new line | |
| 40 | t.Errorf("Expected default width: %d, got: %d\n", customWidth, runeCount) | |
| 39 | ||
| 40 | gotWidth := utf8.RuneCount(buf.Bytes()) | |
| 41 | if gotWidth != wantWidth+1 { // +1 for new line | |
| 42 | t.Errorf("Expected default width: %d, got: %d\n", wantWidth, gotWidth) | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | func TestInvalidWidth(t *testing.T) { | |
| 47 | var buf bytes.Buffer | |
| 48 | p := mpb.New().SetWidth(1).SetOut(&buf) | |
| 49 | bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace() | |
| 50 | for i := 0; i < 100; i++ { | |
| 51 | bar.Incr(1) | |
| 52 | } | |
| 53 | p.Stop() | |
| 54 | ||
| 55 | wantWidth := 80 | |
| 56 | gotWidth := utf8.RuneCount(buf.Bytes()) | |
| 57 | if gotWidth != wantWidth+1 { // +1 for new line | |
| 58 | t.Errorf("Expected width: %d, got: %d\n", wantWidth, gotWidth) | |
| 41 | 59 | } |
| 42 | 60 | } |
| 43 | 61 |