refactoring: bar_test
Vladimir Bauer
5 years ago
| 60 | 60 | |
| 61 | 61 | total := 100 |
| 62 | 62 | till := 30 |
| 63 | refillRune, _ := utf8.DecodeLastRuneInString(mpb.BarDefaultStyle) | |
| 64 | ||
| 65 | bar := p.AddBar(int64(total), mpb.BarFillerTrim()) | |
| 63 | refiller := "+" | |
| 64 | ||
| 65 | bar := p.Add(int64(total), mpb.NewBarFiller(mpb.BarStyle().Refiller(refiller)), mpb.BarFillerTrim()) | |
| 66 | 66 | |
| 67 | 67 | bar.SetRefill(int64(till)) |
| 68 | 68 | bar.IncrBy(till) |
| 75 | 75 | p.Wait() |
| 76 | 76 | |
| 77 | 77 | wantBar := fmt.Sprintf("[%s%s]", |
| 78 | strings.Repeat(string(refillRune), till-1), | |
| 78 | strings.Repeat(refiller, till-1), | |
| 79 | 79 | strings.Repeat("=", total-till-1), |
| 80 | 80 | ) |
| 81 | 81 | |
| 142 | 142 | func TestBarStyle(t *testing.T) { |
| 143 | 143 | var buf bytes.Buffer |
| 144 | 144 | customFormat := "╢▌▌░╟" |
| 145 | runes := []rune(customFormat) | |
| 145 | 146 | total := 80 |
| 146 | 147 | p := mpb.New(mpb.WithWidth(total), mpb.WithOutput(&buf)) |
| 147 | bar := p.Add(int64(total), mpb.NewBarFiller(customFormat), mpb.BarFillerTrim()) | |
| 148 | ||
| 149 | for i := 0; i < total; i++ { | |
| 150 | bar.Increment() | |
| 151 | time.Sleep(10 * time.Millisecond) | |
| 152 | } | |
| 153 | ||
| 154 | p.Wait() | |
| 155 | ||
| 156 | runes := []rune(customFormat) | |
| 157 | wantBar := fmt.Sprintf("%s%s%s", | |
| 148 | bs := mpb.BarStyle() | |
| 149 | bs.Lbound(string(runes[0])) | |
| 150 | bs.Filler(string(runes[1])) | |
| 151 | bs.Tip(string(runes[2])) | |
| 152 | bs.Padding(string(runes[3])) | |
| 153 | bs.Rbound(string(runes[4])) | |
| 154 | bar := p.Add(int64(total), mpb.NewBarFiller(bs), mpb.BarFillerTrim()) | |
| 155 | ||
| 156 | for i := 0; i < total; i++ { | |
| 157 | bar.Increment() | |
| 158 | time.Sleep(10 * time.Millisecond) | |
| 159 | } | |
| 160 | ||
| 161 | p.Wait() | |
| 162 | ||
| 163 | wantBar := fmt.Sprintf("%s%s%s%s", | |
| 158 | 164 | string(runes[0]), |
| 159 | strings.Repeat(string(runes[1]), total-2), | |
| 160 | string(runes[len(runes)-1]), | |
| 165 | strings.Repeat(string(runes[1]), total-3), | |
| 166 | string(runes[2]), | |
| 167 | string(runes[4]), | |
| 161 | 168 | ) |
| 162 | 169 | got := string(getLastLine(buf.Bytes())) |
| 163 | 170 | |