TestBarInvalidFormat
Vladimir Bauer
9 years ago
| 62 | 62 | close(cancel) |
| 63 | 63 | p.Stop() |
| 64 | 64 | |
| 65 | bytes := buf.Bytes() | |
| 66 | _, size := utf8.DecodeLastRune(bytes) | |
| 67 | bytes = bytes[:len(bytes)-size] // removing new line | |
| 65 | // removing new line | |
| 66 | bytes := removeLastRune(buf.Bytes()) | |
| 68 | 67 | |
| 69 | 68 | seen := make(map[rune]bool) |
| 70 | 69 | for _, r := range string(bytes) { |
| 76 | 75 | if !seen[r] { |
| 77 | 76 | t.Errorf("Rune %#U not found in bar\n", r) |
| 78 | 77 | } |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | func TestBarInvalidFormat(t *testing.T) { | |
| 82 | var buf bytes.Buffer | |
| 83 | customWidth := 60 | |
| 84 | p := mpb.New().SetWidth(customWidth).SetOut(&buf) | |
| 85 | customFormat := "(#>=_)" | |
| 86 | bar := p.AddBar(100).Format(customFormat). | |
| 87 | TrimLeftSpace().TrimRightSpace() | |
| 88 | ||
| 89 | for i := 0; i < 100; i++ { | |
| 90 | time.Sleep(10 * time.Millisecond) | |
| 91 | bar.Incr(1) | |
| 92 | } | |
| 93 | ||
| 94 | p.Stop() | |
| 95 | ||
| 96 | bytes := removeLastRune(buf.Bytes()) | |
| 97 | got := string(bytes[len(bytes)-customWidth:]) | |
| 98 | want := "[==========================================================]" | |
| 99 | if got != want { | |
| 100 | t.Errorf("Expected format: %s, got %s\n", want, got) | |
| 79 | 101 | } |
| 80 | 102 | } |
| 81 | 103 | |
| 105 | 127 | t.Error("bar.InProgress returns true after cancel") |
| 106 | 128 | } |
| 107 | 129 | } |
| 130 | ||
| 131 | func removeLastRune(bytes []byte) []byte { | |
| 132 | _, size := utf8.DecodeLastRune(bytes) | |
| 133 | return bytes[:len(bytes)-size] | |
| 134 | } | |