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