TestBarPanics
Vladimir Bauer
7 years ago
| 8 | 8 | "time" |
| 9 | 9 | |
| 10 | 10 | . "github.com/vbauerster/mpb" |
| 11 | "github.com/vbauerster/mpb/decor" | |
| 11 | 12 | ) |
| 12 | 13 | |
| 13 | 14 | func TestBarCompleted(t *testing.T) { |
| 80 | 81 | } |
| 81 | 82 | } |
| 82 | 83 | |
| 83 | // func TestBarPanics(t *testing.T) { | |
| 84 | // var buf bytes.Buffer | |
| 85 | // p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard)) | |
| 84 | func TestBarPanics(t *testing.T) { | |
| 85 | var buf bytes.Buffer | |
| 86 | p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard)) | |
| 86 | 87 | |
| 87 | // wantPanic := "Upps!!!" | |
| 88 | // total := 100 | |
| 88 | wantPanic := "Upps!!!" | |
| 89 | total := 100 | |
| 89 | 90 | |
| 90 | // bar := p.AddBar(int64(total), PrependDecorators( | |
| 91 | // decor.DecoratorFunc(func(s *decor.Statistics, _ chan<- int, _ <-chan int) string { | |
| 92 | // if s.Current >= 42 { | |
| 93 | // panic(wantPanic) | |
| 94 | // } | |
| 95 | // return "test" | |
| 96 | // }), | |
| 97 | // )) | |
| 91 | bar := p.AddBar(int64(total), PrependDecorators(panicDecorator(wantPanic))) | |
| 98 | 92 | |
| 99 | // go func() { | |
| 100 | // for i := 0; i < 100; i++ { | |
| 101 | // time.Sleep(10 * time.Millisecond) | |
| 102 | // bar.Increment() | |
| 103 | // } | |
| 104 | // }() | |
| 93 | go func() { | |
| 94 | for i := 0; i < 100; i++ { | |
| 95 | time.Sleep(10 * time.Millisecond) | |
| 96 | bar.Increment() | |
| 97 | } | |
| 98 | }() | |
| 105 | 99 | |
| 106 | // p.Wait() | |
| 100 | p.Wait() | |
| 107 | 101 | |
| 108 | // wantPanic = fmt.Sprintf("panic: %s", wantPanic) | |
| 109 | // debugStr := buf.String() | |
| 110 | // if !strings.Contains(debugStr, wantPanic) { | |
| 111 | // t.Errorf("%q doesn't contain %q\n", debugStr, wantPanic) | |
| 112 | // } | |
| 113 | // } | |
| 102 | wantPanic = fmt.Sprintf("panic: %s", wantPanic) | |
| 103 | debugStr := buf.String() | |
| 104 | if !strings.Contains(debugStr, wantPanic) { | |
| 105 | t.Errorf("%q doesn't contain %q\n", debugStr, wantPanic) | |
| 106 | } | |
| 107 | } | |
| 108 | ||
| 109 | func panicDecorator(panicMsg string) decor.Decorator { | |
| 110 | d := &decorator{ | |
| 111 | panicMsg: panicMsg, | |
| 112 | } | |
| 113 | d.Init() | |
| 114 | return d | |
| 115 | } | |
| 116 | ||
| 117 | type decorator struct { | |
| 118 | decor.WC | |
| 119 | panicMsg string | |
| 120 | } | |
| 121 | ||
| 122 | func (d *decorator) Decor(st *decor.Statistics) string { | |
| 123 | if st.Current >= 42 { | |
| 124 | panic(d.panicMsg) | |
| 125 | } | |
| 126 | return d.FormatMsg("") | |
| 127 | } | |