refactoring: draw_test
Vladimir Bauer
5 years ago
| 8 | 8 | func TestDraw(t *testing.T) { |
| 9 | 9 | // key is termWidth |
| 10 | 10 | testSuite := map[int][]struct { |
| 11 | style BarStyleComposer | |
| 11 | 12 | name string |
| 12 | style string | |
| 13 | 13 | total int64 |
| 14 | 14 | current int64 |
| 15 | 15 | refill int64 |
| 16 | 16 | barWidth int |
| 17 | 17 | trim bool |
| 18 | reverse bool | |
| 19 | 18 | want string |
| 20 | 19 | }{ |
| 21 | 20 | 0: { |
| 258 | 257 | want: "[===============================>------------------------------------------------------------------]", |
| 259 | 258 | }, |
| 260 | 259 | { |
| 260 | style: BarStyle().Tip("<").Reverse(), | |
| 261 | 261 | name: "t,c{100,33}trim,rev", |
| 262 | 262 | total: 100, |
| 263 | 263 | current: 33, |
| 264 | 264 | trim: true, |
| 265 | reverse: true, | |
| 266 | 265 | want: "[------------------------------------------------------------------<===============================]", |
| 267 | 266 | }, |
| 268 | 267 | { |
| 281 | 280 | want: "[+++++++++++++++++++++++++++++++>------------------------------------------------------------------]", |
| 282 | 281 | }, |
| 283 | 282 | { |
| 283 | style: BarStyle().Tip("<").Reverse(), | |
| 284 | 284 | name: "t,c,r{100,33,33}trim,rev", |
| 285 | 285 | total: 100, |
| 286 | 286 | current: 33, |
| 287 | 287 | refill: 33, |
| 288 | 288 | trim: true, |
| 289 | reverse: true, | |
| 290 | 289 | want: "[------------------------------------------------------------------<+++++++++++++++++++++++++++++++]", |
| 291 | 290 | }, |
| 292 | 291 | { |
| 305 | 304 | want: "[++++++++++++++++++++++++++++++++======>-----------------------------------------------------------]", |
| 306 | 305 | }, |
| 307 | 306 | { |
| 307 | style: BarStyle().Tip("<").Reverse(), | |
| 308 | 308 | name: "t,c,r{100,40,33},rev", |
| 309 | 309 | total: 100, |
| 310 | 310 | current: 40, |
| 311 | 311 | refill: 33, |
| 312 | reverse: true, | |
| 313 | 312 | want: " [----------------------------------------------------------<=====++++++++++++++++++++++++++++++++] ", |
| 314 | 313 | }, |
| 315 | 314 | { |
| 315 | style: BarStyle().Tip("<").Reverse(), | |
| 316 | 316 | name: "t,c,r{100,40,33}trim,rev", |
| 317 | 317 | total: 100, |
| 318 | 318 | current: 40, |
| 319 | 319 | refill: 33, |
| 320 | 320 | trim: true, |
| 321 | reverse: true, | |
| 322 | 321 | want: "[-----------------------------------------------------------<======++++++++++++++++++++++++++++++++]", |
| 323 | 322 | }, |
| 324 | 323 | { |
| 324 | style: BarStyle().Lbound("[").Filler("=").Tip("の").Padding("-").Rbound("]"), | |
| 325 | 325 | name: "[=の-] t,c{100,1}", |
| 326 | style: "[=の-]", | |
| 327 | 326 | total: 100, |
| 328 | 327 | current: 1, |
| 329 | 328 | want: " [の---------------------------------------------------------------------------------------------…] ", |
| 345 | 344 | var tmpBuf bytes.Buffer |
| 346 | 345 | for tw, cases := range testSuite { |
| 347 | 346 | for _, tc := range cases { |
| 348 | s := newTestState(tc.style, tc.reverse) | |
| 347 | if tc.style == nil { | |
| 348 | tc.style = BarStyle() | |
| 349 | } | |
| 350 | s := newTestState(NewBarFiller(tc.style)) | |
| 349 | 351 | s.reqWidth = tc.barWidth |
| 350 | 352 | s.total = tc.total |
| 351 | 353 | s.current = tc.current |
| 366 | 368 | } |
| 367 | 369 | } |
| 368 | 370 | |
| 369 | func newTestState(style string, rev bool) *bState { | |
| 371 | func newTestState(filler BarFiller) *bState { | |
| 370 | 372 | s := &bState{ |
| 371 | filler: NewBarFillerPick(style, rev), | |
| 373 | filler: filler, | |
| 372 | 374 | bufP: new(bytes.Buffer), |
| 373 | 375 | bufB: new(bytes.Buffer), |
| 374 | 376 | bufA: new(bytes.Buffer), |