Codebase list golang-github-vbauerster-mpb / 515f086
TestBarPanics Vladimir Bauer 7 years ago
1 changed file(s) with 40 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
88 "time"
99
1010 . "github.com/vbauerster/mpb"
11 "github.com/vbauerster/mpb/decor"
1112 )
1213
1314 func TestBarCompleted(t *testing.T) {
8081 }
8182 }
8283
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))
8687
87 // wantPanic := "Upps!!!"
88 // total := 100
88 wantPanic := "Upps!!!"
89 total := 100
8990
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)))
9892
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 }()
10599
106 // p.Wait()
100 p.Wait()
107101
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 }