Codebase list golang-github-vbauerster-mpb / 3f2aee1
some more tests Vladimir Bauer 7 years ago
1 changed file(s) with 104 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
3232
3333 func TestBarID(t *testing.T) {
3434 p := New(WithOutput(ioutil.Discard))
35 total := 80
35 total := 100
3636 wantID := 11
3737 bar := p.AddBar(int64(total), BarID(wantID))
3838
39 go func() {
39 go func(total int) {
4040 for i := 0; i < total; i++ {
4141 time.Sleep(50 * time.Millisecond)
4242 bar.Increment()
4343 }
44 }()
44 }(total)
4545
4646 gotID := bar.ID()
4747 if gotID != wantID {
8383
8484 if !strings.Contains(got, wantBar) {
8585 t.Errorf("Want bar: %q, got bar: %q\n", wantBar, got)
86 }
87 }
88
89 func TestBarHas100PercentAfterOnComplete(t *testing.T) {
90 var buf bytes.Buffer
91
92 p := New(WithOutput(&buf))
93
94 total := 50
95
96 bar := p.AddBar(int64(total),
97 AppendDecorators(
98 decor.OnComplete(
99 decor.Percentage(), "done",
100 ),
101 ),
102 )
103
104 for i := 0; i < total; i++ {
105 bar.Increment()
106 time.Sleep(10 * time.Millisecond)
107 }
108
109 p.Wait()
110
111 hundred := "100 %"
112 if !bytes.Contains(buf.Bytes(), []byte(hundred)) {
113 t.Errorf("Bar's buffer does not contain: %q\n", hundred)
86114 }
87115 }
88116
113141 }
114142 }
115143
116 func TestBarPanics(t *testing.T) {
144 func TestBarPanicBeforeComplete(t *testing.T) {
117145 var buf bytes.Buffer
118146 p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard))
119147
120 wantPanic := "Upps!!!"
121 total := 100
122
123 bar := p.AddBar(int64(total), PrependDecorators(panicDecorator(wantPanic)))
124
125 go func() {
126 for i := 0; i < 100; i++ {
127 time.Sleep(10 * time.Millisecond)
128 bar.Increment()
129 }
130 }()
131
132 p.Wait()
133
134 debugStr := buf.String()
135 if !strings.Contains(debugStr, wantPanic) {
136 t.Errorf("%q doesn't contain %q\n", debugStr, wantPanic)
137 }
138 }
139
140 func panicDecorator(panicMsg string) decor.Decorator {
148 total := 100
149 panicMsg := "Upps!!!"
150 var pCount int
151 bar := p.AddBar(int64(total),
152 PrependDecorators(panicDecorator(panicMsg,
153 func(st *decor.Statistics) bool {
154 if st.Current >= 42 {
155 pCount++
156 return true
157 }
158 return false
159 },
160 )),
161 )
162
163 for i := 0; i < total; i++ {
164 time.Sleep(10 * time.Millisecond)
165 bar.Increment()
166 }
167
168 p.Wait()
169
170 if pCount != 1 {
171 t.Errorf("Decor called after panic %d times\n", pCount-1)
172 }
173
174 barStr := buf.String()
175 if !strings.Contains(barStr, panicMsg) {
176 t.Errorf("%q doesn't contain %q\n", barStr, panicMsg)
177 }
178 }
179
180 func TestBarPanicAfterComplete(t *testing.T) {
181 var buf bytes.Buffer
182 p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard))
183
184 total := 100
185 panicMsg := "Upps!!!"
186 var pCount int
187 bar := p.AddBar(int64(total),
188 PrependDecorators(panicDecorator(panicMsg,
189 func(st *decor.Statistics) bool {
190 if st.Completed {
191 pCount++
192 return true
193 }
194 return false
195 },
196 )),
197 )
198
199 for i := 0; i < total; i++ {
200 time.Sleep(10 * time.Millisecond)
201 bar.Increment()
202 }
203
204 p.Wait()
205
206 if pCount != 1 {
207 t.Errorf("Decor called after panic %d times\n", pCount-1)
208 }
209
210 barStr := buf.String()
211 if !strings.Contains(barStr, panicMsg) {
212 t.Errorf("%q doesn't contain %q\n", barStr, panicMsg)
213 }
214 }
215
216 func panicDecorator(panicMsg string, cond func(*decor.Statistics) bool) decor.Decorator {
141217 d := &decorator{
142218 panicMsg: panicMsg,
219 cond: cond,
143220 }
144221 d.Init()
145222 return d
148225 type decorator struct {
149226 decor.WC
150227 panicMsg string
228 cond func(*decor.Statistics) bool
151229 }
152230
153231 func (d *decorator) Decor(st *decor.Statistics) string {
154 if st.Current >= 42 {
232 if d.cond(st) {
155233 panic(d.panicMsg)
156234 }
157235 return d.FormatMsg("")