Codebase list golang-github-vbauerster-mpb / bcc377e
update bar_test Vladimir Bauer 9 years ago
1 changed file(s) with 58 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
99 "unicode/utf8"
1010
1111 "github.com/vbauerster/mpb"
12 "github.com/vbauerster/mpb/decor"
1213 )
1314
1415 func TestBarSetWidth(t *testing.T) {
1516 var buf bytes.Buffer
16 p := mpb.New().SetOut(&buf)
1717 // overwrite default width 80
1818 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
2426 p.Stop()
2527
2628 gotWidth := len(buf.Bytes())
3133
3234 func TestBarSetInvalidWidth(t *testing.T) {
3335 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
4043 p.Stop()
4144
4245 wantWidth := 80
4952 func TestBarFormat(t *testing.T) {
5053 var buf bytes.Buffer
5154 cancel := make(chan struct{})
52 p := mpb.New().WithCancel(cancel).SetOut(&buf)
5355 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())
5662
5763 go func() {
5864 for i := 0; i < 100; i++ {
6571 close(cancel)
6672 p.Stop()
6773
68 // removing new line
69 bytes := removeLastRune(buf.Bytes())
74 barAsStr := strings.Trim(buf.String(), "\n")
7075
7176 seen := make(map[rune]bool)
72 for _, r := range string(bytes) {
77 for _, r := range barAsStr {
7378 if !seen[r] {
7479 seen[r] = true
7580 }
8489 func TestBarInvalidFormat(t *testing.T) {
8590 var buf bytes.Buffer
8691 customWidth := 60
87 p := mpb.New().SetWidth(customWidth).SetOut(&buf)
8892 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())
9199
92100 for i := 0; i < 100; i++ {
93101 time.Sleep(10 * time.Millisecond)
106114 func TestBarInProgress(t *testing.T) {
107115 var buf bytes.Buffer
108116 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())
111122
112123 stopped := make(chan struct{})
113124
132143
133144 func TestGetSpinner(t *testing.T) {
134145 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())
137148
138149 for i := 0; i < 100; i++ {
139150 time.Sleep(10 * time.Millisecond)
144155
145156 spinnerChars := []byte(`-\|/`)
146157 seen := make(map[byte]bool)
147 for _, b := range buf.Bytes() {
158 for _, b := range bytes.Trim(buf.Bytes(), "\n") {
148159 if !seen[b] {
149160 seen[b] = true
150161 }
159170 func TestBarGetID(t *testing.T) {
160171 var wg sync.WaitGroup
161172 var buf bytes.Buffer
162 p := mpb.New().SetOut(&buf)
173 p := mpb.New(mpb.Output(&buf))
163174
164175 numBars := 3
165176 wg.Add(numBars)
166177
167178 bars := make([]*mpb.Bar, numBars)
168179 for i := 0; i < numBars; i++ {
169 bars[i] = p.AddBarWithID(i, 100)
180 bars[i] = p.AddBar(100, mpb.BarID(i))
170181
171182 go func(bar *mpb.Bar) {
172183 defer wg.Done()
178189 }
179190
180191 for wantID, bar := range bars {
181 gotID := bar.GetID()
192 gotID := bar.ID()
182193 if gotID != wantID {
183194 t.Errorf("Expected bar id: %d, got %d\n", wantID, gotID)
184195 }
192203 var buf bytes.Buffer
193204
194205 width := 100
195 p := mpb.New().SetWidth(width).SetOut(&buf)
206 p := mpb.New(
207 mpb.Output(&buf),
208 mpb.WithWidth(width),
209 )
196210
197211 total := 100
198 refill := 30
199 delta := total - refill
212 till := 30
200213 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++ {
206220 time.Sleep(10 * time.Millisecond)
207221 bar.Incr(1)
208222 }
213227
214228 gotBar := string(bytes[len(bytes)-width:])
215229 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))
218232 if gotBar != wantBar {
219233 t.Errorf("Want bar: %s, got bar: %s\n", wantBar, gotBar)
220234 }
223237 func TestBarPanics(t *testing.T) {
224238 var wg sync.WaitGroup
225239 var buf bytes.Buffer
226 p := mpb.New().SetOut(&buf)
240 p := mpb.New(mpb.Output(&buf))
227241
228242 wantPanic := "Upps!!!"
229243 numBars := 3
231245
232246 for i := 0; i < numBars; i++ {
233247 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 {
236250 if s.ID == 2 && s.Current >= 42 {
237251 panic(wantPanic)
238252 }
239253 return name
240 })
254 },
255 ))
241256
242257 go func() {
243258 defer wg.Done()