some more tests
Vladimir Bauer
7 years ago
| 32 | 32 | |
| 33 | 33 | func TestBarID(t *testing.T) { |
| 34 | 34 | p := New(WithOutput(ioutil.Discard)) |
| 35 | total := 80 | |
| 35 | total := 100 | |
| 36 | 36 | wantID := 11 |
| 37 | 37 | bar := p.AddBar(int64(total), BarID(wantID)) |
| 38 | 38 | |
| 39 | go func() { | |
| 39 | go func(total int) { | |
| 40 | 40 | for i := 0; i < total; i++ { |
| 41 | 41 | time.Sleep(50 * time.Millisecond) |
| 42 | 42 | bar.Increment() |
| 43 | 43 | } |
| 44 | }() | |
| 44 | }(total) | |
| 45 | 45 | |
| 46 | 46 | gotID := bar.ID() |
| 47 | 47 | if gotID != wantID { |
| 83 | 83 | |
| 84 | 84 | if !strings.Contains(got, wantBar) { |
| 85 | 85 | 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) | |
| 86 | 114 | } |
| 87 | 115 | } |
| 88 | 116 | |
| 113 | 141 | } |
| 114 | 142 | } |
| 115 | 143 | |
| 116 | func TestBarPanics(t *testing.T) { | |
| 144 | func TestBarPanicBeforeComplete(t *testing.T) { | |
| 117 | 145 | var buf bytes.Buffer |
| 118 | 146 | p := New(WithDebugOutput(&buf), WithOutput(ioutil.Discard)) |
| 119 | 147 | |
| 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 { | |
| 141 | 217 | d := &decorator{ |
| 142 | 218 | panicMsg: panicMsg, |
| 219 | cond: cond, | |
| 143 | 220 | } |
| 144 | 221 | d.Init() |
| 145 | 222 | return d |
| 148 | 225 | type decorator struct { |
| 149 | 226 | decor.WC |
| 150 | 227 | panicMsg string |
| 228 | cond func(*decor.Statistics) bool | |
| 151 | 229 | } |
| 152 | 230 | |
| 153 | 231 | func (d *decorator) Decor(st *decor.Statistics) string { |
| 154 | if st.Current >= 42 { | |
| 232 | if d.cond(st) { | |
| 155 | 233 | panic(d.panicMsg) |
| 156 | 234 | } |
| 157 | 235 | return d.FormatMsg("") |