Codebase list golang-github-vbauerster-mpb / b794784
use decor.Any Vladimir Bauer 6 years ago
1 changed file(s) with 17 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
169169
170170 func TestBarPanicBeforeComplete(t *testing.T) {
171171 var buf bytes.Buffer
172 p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard))
172 p := New(
173 WithDebugOutput(&buf),
174 WithOutput(ioutil.Discard),
175 WithWidth(80),
176 )
173177
174178 total := 100
175179 panicMsg := "Upps!!!"
205209
206210 func TestBarPanicAfterComplete(t *testing.T) {
207211 var buf bytes.Buffer
208 p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard))
212 p := New(
213 WithDebugOutput(&buf),
214 WithOutput(ioutil.Discard),
215 WithWidth(80),
216 )
209217
210218 total := 100
211219 panicMsg := "Upps!!!"
240248 }
241249
242250 func panicDecorator(panicMsg string, cond func(decor.Statistics) bool) decor.Decorator {
243 d := &decorator{
244 panicMsg: panicMsg,
245 cond: cond,
246 }
247 d.Init()
248 return d
249 }
250
251 type decorator struct {
252 decor.WC
253 panicMsg string
254 cond func(decor.Statistics) bool
255 }
256
257 func (d *decorator) Decor(st decor.Statistics) string {
258 if d.cond(st) {
259 panic(d.panicMsg)
260 }
261 return d.FormatMsg("")
262 }
251 return decor.Any(func(st decor.Statistics) string {
252 if cond(st) {
253 panic(panicMsg)
254 }
255 return ""
256 })
257 }