refactoring TestDecorStatisticsAvailableWidth
Vladimir Bauer
3 years ago
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | func TestDecorStatisticsAvailableWidth(t *testing.T) { |
| 201 | var called [2]bool | |
| 201 | ch := make(chan int, 2) | |
| 202 | 202 | td1 := func(s decor.Statistics) string { |
| 203 | if s.AvailableWidth != 80 { | |
| 204 | t.Errorf("expected AvailableWidth %d got %d", 80, s.AvailableWidth) | |
| 205 | } | |
| 206 | called[0] = true | |
| 203 | ch <- s.AvailableWidth | |
| 207 | 204 | return fmt.Sprintf("\x1b[31;1;4m%s\x1b[0m", strings.Repeat("0", 20)) |
| 208 | 205 | } |
| 209 | 206 | td2 := func(s decor.Statistics) string { |
| 210 | if s.AvailableWidth != 40 { | |
| 211 | t.Errorf("expected AvailableWidth %d got %d", 40, s.AvailableWidth) | |
| 212 | } | |
| 213 | called[1] = true | |
| 207 | ch <- s.AvailableWidth | |
| 214 | 208 | return "" |
| 215 | 209 | } |
| 216 | 210 | ctx, cancel := context.WithCancel(context.Background()) |
| 217 | 211 | refresh := make(chan interface{}) |
| 218 | p := mpb.NewWithContext(ctx, mpb.WithWidth(100), | |
| 212 | p := mpb.NewWithContext(ctx, | |
| 213 | mpb.WithWidth(100), | |
| 219 | 214 | mpb.WithManualRefresh(refresh), |
| 220 | 215 | mpb.WithOutput(io.Discard), |
| 221 | 216 | ) |
| 231 | 226 | ), |
| 232 | 227 | ) |
| 233 | 228 | refresh <- time.Now() |
| 234 | cancel() | |
| 235 | p.Wait() | |
| 236 | for i, ok := range called { | |
| 237 | if !ok { | |
| 238 | t.Errorf("Decorator %d isn't called", i+1) | |
| 239 | } | |
| 240 | } | |
| 241 | } | |
| 229 | go func() { | |
| 230 | time.Sleep(10 * time.Millisecond) | |
| 231 | cancel() | |
| 232 | }() | |
| 233 | p.Wait() | |
| 234 | ||
| 235 | if availableWidth := <-ch; availableWidth != 80 { | |
| 236 | t.Errorf("expected AvailableWidth %d got %d", 80, availableWidth) | |
| 237 | } | |
| 238 | ||
| 239 | if availableWidth := <-ch; availableWidth != 40 { | |
| 240 | t.Errorf("expected AvailableWidth %d got %d", 40, availableWidth) | |
| 241 | } | |
| 242 | } | |