use decor.Any
Vladimir Bauer
6 years ago
| 169 | 169 | |
| 170 | 170 | func TestBarPanicBeforeComplete(t *testing.T) { |
| 171 | 171 | 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 | ) | |
| 173 | 177 | |
| 174 | 178 | total := 100 |
| 175 | 179 | panicMsg := "Upps!!!" |
| 205 | 209 | |
| 206 | 210 | func TestBarPanicAfterComplete(t *testing.T) { |
| 207 | 211 | 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 | ) | |
| 209 | 217 | |
| 210 | 218 | total := 100 |
| 211 | 219 | panicMsg := "Upps!!!" |
| 240 | 248 | } |
| 241 | 249 | |
| 242 | 250 | 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 | } | |