update bar_test
Vladimir Bauer
9 years ago
| 9 | 9 | "unicode/utf8" |
| 10 | 10 | |
| 11 | 11 | "github.com/vbauerster/mpb" |
| 12 | "github.com/vbauerster/mpb/decor" | |
| 12 | 13 | ) |
| 13 | 14 | |
| 14 | 15 | func TestBarSetWidth(t *testing.T) { |
| 15 | 16 | var buf bytes.Buffer |
| 16 | p := mpb.New().SetOut(&buf) | |
| 17 | 17 | // overwrite default width 80 |
| 18 | 18 | customWidth := 60 |
| 19 | bar := p.AddBar(100).SetWidth(customWidth). | |
| 20 | TrimLeftSpace().TrimRightSpace() | |
| 21 | for i := 0; i < 100; i++ { | |
| 22 | bar.Incr(1) | |
| 23 | } | |
| 19 | p := mpb.New(mpb.Output(&buf), mpb.WithWidth(customWidth)) | |
| 20 | bar := p.AddBar(100, mpb.BarTrim()) | |
| 21 | ||
| 22 | for i := 0; i < 100; i++ { | |
| 23 | bar.Incr(1) | |
| 24 | } | |
| 25 | ||
| 24 | 26 | p.Stop() |
| 25 | 27 | |
| 26 | 28 | gotWidth := len(buf.Bytes()) |
| 31 | 33 | |
| 32 | 34 | func TestBarSetInvalidWidth(t *testing.T) { |
| 33 | 35 | var buf bytes.Buffer |
| 34 | p := mpb.New().SetOut(&buf) | |
| 35 | bar := p.AddBar(100).SetWidth(1). | |
| 36 | TrimLeftSpace().TrimRightSpace() | |
| 37 | for i := 0; i < 100; i++ { | |
| 38 | bar.Incr(1) | |
| 39 | } | |
| 36 | p := mpb.New(mpb.Output(&buf), mpb.WithWidth(1)) | |
| 37 | bar := p.AddBar(100, mpb.BarTrim()) | |
| 38 | ||
| 39 | for i := 0; i < 100; i++ { | |
| 40 | bar.Incr(1) | |
| 41 | } | |
| 42 | ||
| 40 | 43 | p.Stop() |
| 41 | 44 | |
| 42 | 45 | wantWidth := 80 |
| 49 | 52 | func TestBarFormat(t *testing.T) { |
| 50 | 53 | var buf bytes.Buffer |
| 51 | 54 | cancel := make(chan struct{}) |
| 52 | p := mpb.New().WithCancel(cancel).SetOut(&buf) | |
| 53 | 55 | customFormat := "(#>_)" |
| 54 | bar := p.AddBar(100).Format(customFormat). | |
| 55 | TrimLeftSpace().TrimRightSpace() | |
| 56 | p := mpb.New( | |
| 57 | mpb.Output(&buf), | |
| 58 | mpb.WithCancel(cancel), | |
| 59 | mpb.WithFormat(customFormat), | |
| 60 | ) | |
| 61 | bar := p.AddBar(100, mpb.BarTrim()) | |
| 56 | 62 | |
| 57 | 63 | go func() { |
| 58 | 64 | for i := 0; i < 100; i++ { |
| 65 | 71 | close(cancel) |
| 66 | 72 | p.Stop() |
| 67 | 73 | |
| 68 | // removing new line | |
| 69 | bytes := removeLastRune(buf.Bytes()) | |
| 74 | barAsStr := strings.Trim(buf.String(), "\n") | |
| 70 | 75 | |
| 71 | 76 | seen := make(map[rune]bool) |
| 72 | for _, r := range string(bytes) { | |
| 77 | for _, r := range barAsStr { | |
| 73 | 78 | if !seen[r] { |
| 74 | 79 | seen[r] = true |
| 75 | 80 | } |
| 84 | 89 | func TestBarInvalidFormat(t *testing.T) { |
| 85 | 90 | var buf bytes.Buffer |
| 86 | 91 | customWidth := 60 |
| 87 | p := mpb.New().SetWidth(customWidth).SetOut(&buf) | |
| 88 | 92 | customFormat := "(#>=_)" |
| 89 | bar := p.AddBar(100).Format(customFormat). | |
| 90 | TrimLeftSpace().TrimRightSpace() | |
| 93 | p := mpb.New( | |
| 94 | mpb.Output(&buf), | |
| 95 | mpb.WithWidth(customWidth), | |
| 96 | mpb.WithFormat(customFormat), | |
| 97 | ) | |
| 98 | bar := p.AddBar(100, mpb.BarTrim()) | |
| 91 | 99 | |
| 92 | 100 | for i := 0; i < 100; i++ { |
| 93 | 101 | time.Sleep(10 * time.Millisecond) |
| 106 | 114 | func TestBarInProgress(t *testing.T) { |
| 107 | 115 | var buf bytes.Buffer |
| 108 | 116 | cancel := make(chan struct{}) |
| 109 | p := mpb.New().WithCancel(cancel).SetOut(&buf) | |
| 110 | bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace() | |
| 117 | p := mpb.New( | |
| 118 | mpb.Output(&buf), | |
| 119 | mpb.WithCancel(cancel), | |
| 120 | ) | |
| 121 | bar := p.AddBar(100, mpb.BarTrim()) | |
| 111 | 122 | |
| 112 | 123 | stopped := make(chan struct{}) |
| 113 | 124 | |
| 132 | 143 | |
| 133 | 144 | func TestGetSpinner(t *testing.T) { |
| 134 | 145 | var buf bytes.Buffer |
| 135 | p := mpb.New().SetOut(&buf) | |
| 136 | bar := p.AddBar(0).TrimLeftSpace().TrimRightSpace() | |
| 146 | p := mpb.New(mpb.Output(&buf)) | |
| 147 | bar := p.AddBar(0, mpb.BarTrim()) | |
| 137 | 148 | |
| 138 | 149 | for i := 0; i < 100; i++ { |
| 139 | 150 | time.Sleep(10 * time.Millisecond) |
| 144 | 155 | |
| 145 | 156 | spinnerChars := []byte(`-\|/`) |
| 146 | 157 | seen := make(map[byte]bool) |
| 147 | for _, b := range buf.Bytes() { | |
| 158 | for _, b := range bytes.Trim(buf.Bytes(), "\n") { | |
| 148 | 159 | if !seen[b] { |
| 149 | 160 | seen[b] = true |
| 150 | 161 | } |
| 159 | 170 | func TestBarGetID(t *testing.T) { |
| 160 | 171 | var wg sync.WaitGroup |
| 161 | 172 | var buf bytes.Buffer |
| 162 | p := mpb.New().SetOut(&buf) | |
| 173 | p := mpb.New(mpb.Output(&buf)) | |
| 163 | 174 | |
| 164 | 175 | numBars := 3 |
| 165 | 176 | wg.Add(numBars) |
| 166 | 177 | |
| 167 | 178 | bars := make([]*mpb.Bar, numBars) |
| 168 | 179 | for i := 0; i < numBars; i++ { |
| 169 | bars[i] = p.AddBarWithID(i, 100) | |
| 180 | bars[i] = p.AddBar(100, mpb.BarID(i)) | |
| 170 | 181 | |
| 171 | 182 | go func(bar *mpb.Bar) { |
| 172 | 183 | defer wg.Done() |
| 178 | 189 | } |
| 179 | 190 | |
| 180 | 191 | for wantID, bar := range bars { |
| 181 | gotID := bar.GetID() | |
| 192 | gotID := bar.ID() | |
| 182 | 193 | if gotID != wantID { |
| 183 | 194 | t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID) |
| 184 | 195 | } |
| 192 | 203 | var buf bytes.Buffer |
| 193 | 204 | |
| 194 | 205 | width := 100 |
| 195 | p := mpb.New().SetWidth(width).SetOut(&buf) | |
| 206 | p := mpb.New( | |
| 207 | mpb.Output(&buf), | |
| 208 | mpb.WithWidth(width), | |
| 209 | ) | |
| 196 | 210 | |
| 197 | 211 | total := 100 |
| 198 | refill := 30 | |
| 199 | delta := total - refill | |
| 212 | till := 30 | |
| 200 | 213 | refillChar := '+' |
| 201 | bar := p.AddBar(int64(total)).TrimLeftSpace().TrimRightSpace() | |
| 202 | ||
| 203 | bar.IncrWithReFill(refill, &mpb.Refill{Char: refillChar}) | |
| 204 | ||
| 205 | for i := 0; i < delta; i++ { | |
| 214 | ||
| 215 | bar := p.AddBar(100, mpb.BarTrim()) | |
| 216 | ||
| 217 | bar.ResumeFill(refillChar, int64(till)) | |
| 218 | ||
| 219 | for i := 0; i < total; i++ { | |
| 206 | 220 | time.Sleep(10 * time.Millisecond) |
| 207 | 221 | bar.Incr(1) |
| 208 | 222 | } |
| 213 | 227 | |
| 214 | 228 | gotBar := string(bytes[len(bytes)-width:]) |
| 215 | 229 | wantBar := fmt.Sprintf("[%s%s]", |
| 216 | strings.Repeat(string(refillChar), refill-1), | |
| 217 | strings.Repeat("=", delta-1)) | |
| 230 | strings.Repeat(string(refillChar), till-1), | |
| 231 | strings.Repeat("=", total-till-1)) | |
| 218 | 232 | if gotBar != wantBar { |
| 219 | 233 | t.Errorf("Want bar: %s, got bar: %s\n", wantBar, gotBar) |
| 220 | 234 | } |
| 223 | 237 | func TestBarPanics(t *testing.T) { |
| 224 | 238 | var wg sync.WaitGroup |
| 225 | 239 | var buf bytes.Buffer |
| 226 | p := mpb.New().SetOut(&buf) | |
| 240 | p := mpb.New(mpb.Output(&buf)) | |
| 227 | 241 | |
| 228 | 242 | wantPanic := "Upps!!!" |
| 229 | 243 | numBars := 3 |
| 231 | 245 | |
| 232 | 246 | for i := 0; i < numBars; i++ { |
| 233 | 247 | name := fmt.Sprintf("b#%02d:", i) |
| 234 | bar := p.AddBarWithID(i, 100). | |
| 235 | PrependFunc(func(s *mpb.Statistics, _ chan<- int, _ <-chan int) string { | |
| 248 | bar := p.AddBar(100, mpb.BarID(i), mpb.PrependDecorators( | |
| 249 | func(s *decor.Statistics, _ chan<- int, _ <-chan int) string { | |
| 236 | 250 | if s.ID == 2 && s.Current >= 42 { |
| 237 | 251 | panic(wantPanic) |
| 238 | 252 | } |
| 239 | 253 | return name |
| 240 | }) | |
| 254 | }, | |
| 255 | )) | |
| 241 | 256 | |
| 242 | 257 | go func() { |
| 243 | 258 | defer wg.Done() |