test update
Vladimir Bauer
9 years ago
| 5 | 5 | ) |
| 6 | 6 | |
| 7 | 7 | func TestFillBar(t *testing.T) { |
| 8 | s := newTestState(80, 60) | |
| 9 | 8 | tests := []struct { |
| 10 | 9 | termWidth int |
| 11 | 10 | barWidth int |
| 11 | total int64 | |
| 12 | current int64 | |
| 13 | barRefill *refill | |
| 12 | 14 | want []byte |
| 13 | 15 | }{ |
| 14 | 16 | { |
| 15 | 17 | termWidth: 1, |
| 16 | barWidth: 60, | |
| 18 | barWidth: 100, | |
| 17 | 19 | want: []byte{}, |
| 18 | 20 | }, |
| 19 | 21 | { |
| 20 | 22 | termWidth: 2, |
| 21 | barWidth: 60, | |
| 23 | barWidth: 100, | |
| 24 | total: 100, | |
| 25 | current: 20, | |
| 22 | 26 | want: []byte("[]"), |
| 23 | 27 | }, |
| 24 | 28 | { |
| 25 | termWidth: 4, | |
| 26 | barWidth: 60, | |
| 27 | want: []byte("[=>]"), | |
| 29 | termWidth: 20, | |
| 30 | barWidth: 100, | |
| 31 | total: 100, | |
| 32 | current: 20, | |
| 33 | want: []byte("[==>---------------]"), | |
| 28 | 34 | }, |
| 29 | 35 | { |
| 30 | termWidth: 6, | |
| 31 | barWidth: 60, | |
| 32 | want: []byte("[==>-]"), | |
| 36 | termWidth: 50, | |
| 37 | barWidth: 100, | |
| 38 | total: 100, | |
| 39 | current: 20, | |
| 40 | want: []byte("[========>---------------------------------------]"), | |
| 33 | 41 | }, |
| 34 | 42 | { |
| 35 | termWidth: 8, | |
| 36 | barWidth: 60, | |
| 37 | want: []byte("[====>-]"), | |
| 43 | termWidth: 100, | |
| 44 | barWidth: 100, | |
| 45 | total: 100, | |
| 46 | current: 40, | |
| 47 | want: []byte("[======================================>-----------------------------------------------------------]"), | |
| 38 | 48 | }, |
| 39 | 49 | { |
| 40 | termWidth: 80, | |
| 41 | barWidth: 60, | |
| 42 | want: []byte("[===========================================>--------------]"), | |
| 50 | termWidth: 100, | |
| 51 | barWidth: 100, | |
| 52 | total: 100, | |
| 53 | current: 40, | |
| 54 | barRefill: &refill{'+', 32}, | |
| 55 | want: []byte("[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]"), | |
| 43 | 56 | }, |
| 44 | 57 | { |
| 45 | termWidth: 80, | |
| 46 | barWidth: 62, | |
| 47 | want: []byte("[============================================>---------------]"), | |
| 58 | termWidth: 100, | |
| 59 | barWidth: 100, | |
| 60 | total: 100, | |
| 61 | current: 99, | |
| 62 | want: []byte("[=================================================================================================>]"), | |
| 63 | }, | |
| 64 | { | |
| 65 | termWidth: 100, | |
| 66 | barWidth: 100, | |
| 67 | total: 100, | |
| 68 | current: 100, | |
| 69 | want: []byte("[==================================================================================================]"), | |
| 48 | 70 | }, |
| 49 | 71 | } |
| 50 | 72 | |
| 51 | 73 | for _, test := range tests { |
| 74 | s := newTestState() | |
| 52 | 75 | s.width = test.barWidth |
| 76 | s.total = test.total | |
| 77 | s.current = test.current | |
| 78 | if test.barRefill != nil { | |
| 79 | s.refill = test.barRefill | |
| 80 | } | |
| 53 | 81 | got := draw(s, test.termWidth) |
| 54 | 82 | if !reflect.DeepEqual(test.want, got) { |
| 55 | 83 | t.Errorf("Want: %q, Got: %q\n", test.want, got) |
| 57 | 85 | } |
| 58 | 86 | } |
| 59 | 87 | |
| 60 | func newTestState(total, current int64) *state { | |
| 88 | func newTestState() *state { | |
| 61 | 89 | return &state{ |
| 62 | 90 | format: barFmtRunes{'[', '=', '>', '-', ']'}, |
| 63 | total: total, | |
| 64 | current: current, | |
| 65 | 91 | trimLeftSpace: true, |
| 66 | 92 | trimRightSpace: true, |
| 67 | 93 | } |