| 6 | 6 |
|
| 7 | 7 |
func TestDraw(t *testing.T) {
|
| 8 | 8 |
// key is termWidth
|
| 9 | |
testSuite := map[int]map[string]struct {
|
|
9 |
testSuite := map[int][]struct {
|
|
10 |
name string
|
| 10 | 11 |
total, current int64
|
| 11 | 12 |
barWidth int
|
| 12 | 13 |
barRefill *refill
|
|
14 |
trimSpace bool
|
| 13 | 15 |
want string
|
| 14 | 16 |
}{
|
| 15 | 17 |
100: {
|
| 16 | |
"t,c,bw{100,100,0}": {
|
|
18 |
{
|
|
19 |
name: "t,c,bw{100,100,0}",
|
| 17 | 20 |
total: 100,
|
| 18 | 21 |
current: 0,
|
| 19 | 22 |
barWidth: 100,
|
| 20 | |
want: "[--------------------------------------------------------------------------------------------------]",
|
| 21 | |
},
|
| 22 | |
"t,c,bw{100,1,100}": {
|
|
23 |
want: " [------------------------------------------------------------------------------------------------] ",
|
|
24 |
},
|
|
25 |
{
|
|
26 |
name: "t,c,bw{100,100,0}:trimSpace",
|
|
27 |
total: 100,
|
|
28 |
current: 0,
|
|
29 |
barWidth: 100,
|
|
30 |
trimSpace: true,
|
|
31 |
want: "[--------------------------------------------------------------------------------------------------]",
|
|
32 |
},
|
|
33 |
{
|
|
34 |
name: "t,c,bw{100,1,100}",
|
| 23 | 35 |
total: 100,
|
| 24 | 36 |
current: 1,
|
| 25 | 37 |
barWidth: 100,
|
| 26 | |
want: "[>-------------------------------------------------------------------------------------------------]",
|
| 27 | |
},
|
| 28 | |
"t,c,bw{100,40,100}": {
|
|
38 |
want: " [>-----------------------------------------------------------------------------------------------] ",
|
|
39 |
},
|
|
40 |
{
|
|
41 |
name: "t,c,bw{100,1,100}:trimSpace",
|
|
42 |
total: 100,
|
|
43 |
current: 1,
|
|
44 |
barWidth: 100,
|
|
45 |
trimSpace: true,
|
|
46 |
want: "[>-------------------------------------------------------------------------------------------------]",
|
|
47 |
},
|
|
48 |
{
|
|
49 |
name: "t,c,bw{100,40,100}",
|
| 29 | 50 |
total: 100,
|
| 30 | 51 |
current: 40,
|
| 31 | 52 |
barWidth: 100,
|
| 32 | |
want: "[======================================>-----------------------------------------------------------]",
|
| 33 | |
},
|
| 34 | |
"t,c,bw{100,40,100}refill{'+', 32}": {
|
|
53 |
want: " [=====================================>----------------------------------------------------------] ",
|
|
54 |
},
|
|
55 |
{
|
|
56 |
name: "t,c,bw{100,40,100}:refill{'+', 32}",
|
| 35 | 57 |
total: 100,
|
| 36 | 58 |
current: 40,
|
| 37 | 59 |
barWidth: 100,
|
| 38 | 60 |
barRefill: &refill{'+', 32},
|
|
61 |
want: " [+++++++++++++++++++++++++++++++======>----------------------------------------------------------] ",
|
|
62 |
},
|
|
63 |
{
|
|
64 |
name: "t,c,bw{100,40,100}:refill{'+', 32}:trimSpace",
|
|
65 |
total: 100,
|
|
66 |
current: 40,
|
|
67 |
barWidth: 100,
|
|
68 |
barRefill: &refill{'+', 32},
|
|
69 |
trimSpace: true,
|
| 39 | 70 |
want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]",
|
| 40 | 71 |
},
|
| 41 | |
"t,c,bw{100,99,100}": {
|
|
72 |
{
|
|
73 |
name: "t,c,bw{100,99,100}",
|
| 42 | 74 |
total: 100,
|
| 43 | 75 |
current: 99,
|
| 44 | 76 |
barWidth: 100,
|
| 45 | |
want: "[================================================================================================>-]",
|
| 46 | |
},
|
| 47 | |
"t,c,bw{100,100,100}": {
|
| 48 | |
total: 100,
|
| 49 | |
current: 100,
|
| 50 | |
barWidth: 100,
|
| 51 | |
want: "[==================================================================================================]",
|
|
77 |
want: " [==============================================================================================>-] ",
|
|
78 |
},
|
|
79 |
{
|
|
80 |
name: "t,c,bw{100,100,100}",
|
|
81 |
total: 100,
|
|
82 |
current: 100,
|
|
83 |
barWidth: 100,
|
|
84 |
want: " [================================================================================================] ",
|
| 52 | 85 |
},
|
| 53 | 86 |
},
|
| 54 | 87 |
2: {
|
| 55 | |
"t,c,bw{0,0,100}": {
|
| 56 | |
barWidth: 100,
|
| 57 | |
want: "[]",
|
| 58 | |
},
|
| 59 | |
"t,c,bw{60,20,80}": {
|
|
88 |
{
|
|
89 |
name: "t,c,bw{0,0,100}",
|
|
90 |
barWidth: 100,
|
|
91 |
want: " [] ",
|
|
92 |
},
|
|
93 |
{
|
|
94 |
name: "t,c,bw{60,20,80}",
|
| 60 | 95 |
total: 60,
|
| 61 | 96 |
current: 20,
|
| 62 | 97 |
barWidth: 80,
|
| 63 | |
want: "[]",
|
| 64 | |
},
|
| 65 | |
},
|
| 66 | |
3: {
|
| 67 | |
"t,c,bw{100,20,100}": {
|
| 68 | |
total: 100,
|
| 69 | |
current: 20,
|
| 70 | |
barWidth: 100,
|
| 71 | |
want: "[-]",
|
| 72 | |
},
|
| 73 | |
"t,c,bw{100,98,100}": {
|
| 74 | |
total: 100,
|
| 75 | |
current: 98,
|
| 76 | |
barWidth: 100,
|
| 77 | |
want: "[=]",
|
| 78 | |
},
|
| 79 | |
"t,c,bw{100,100,100}": {
|
| 80 | |
total: 100,
|
| 81 | |
current: 100,
|
| 82 | |
barWidth: 100,
|
| 83 | |
want: "[=]",
|
| 84 | |
},
|
| 85 | |
},
|
| 86 | |
5: {
|
| 87 | |
"t,c,bw{100,20,100}": {
|
| 88 | |
total: 100,
|
| 89 | |
current: 20,
|
| 90 | |
barWidth: 100,
|
| 91 | |
want: "[>--]",
|
| 92 | |
},
|
| 93 | |
"t,c,bw{100,98,100}": {
|
| 94 | |
total: 100,
|
| 95 | |
current: 98,
|
| 96 | |
barWidth: 100,
|
| 97 | |
want: "[===]",
|
| 98 | |
},
|
| 99 | |
"t,c,bw{100,100,100}": {
|
| 100 | |
total: 100,
|
| 101 | |
current: 100,
|
| 102 | |
barWidth: 100,
|
| 103 | |
want: "[===]",
|
| 104 | |
},
|
| 105 | |
},
|
| 106 | |
6: {
|
| 107 | |
"t,c,bw{100,20,100}": {
|
| 108 | |
total: 100,
|
| 109 | |
current: 20,
|
| 110 | |
barWidth: 100,
|
| 111 | |
want: "[>---]",
|
| 112 | |
},
|
| 113 | |
"t,c,bw{100,98,100}": {
|
| 114 | |
total: 100,
|
| 115 | |
current: 98,
|
| 116 | |
barWidth: 100,
|
| 117 | |
want: "[====]",
|
| 118 | |
},
|
| 119 | |
"t,c,bw{100,100,100}": {
|
| 120 | |
total: 100,
|
| 121 | |
current: 100,
|
| 122 | |
barWidth: 100,
|
| 123 | |
want: "[====]",
|
|
98 |
want: " [] ",
|
|
99 |
},
|
|
100 |
},
|
|
101 |
4: {
|
|
102 |
{
|
|
103 |
name: "t,c,bw{100,20,100}",
|
|
104 |
total: 100,
|
|
105 |
current: 20,
|
|
106 |
barWidth: 100,
|
|
107 |
want: " [] ",
|
|
108 |
},
|
|
109 |
{
|
|
110 |
name: "t,c,bw{100,98,100}",
|
|
111 |
total: 100,
|
|
112 |
current: 98,
|
|
113 |
barWidth: 100,
|
|
114 |
want: " [] ",
|
|
115 |
},
|
|
116 |
{
|
|
117 |
name: "t,c,bw{100,100,100}",
|
|
118 |
total: 100,
|
|
119 |
current: 100,
|
|
120 |
barWidth: 100,
|
|
121 |
want: " [] ",
|
|
122 |
},
|
|
123 |
},
|
|
124 |
8: {
|
|
125 |
{
|
|
126 |
name: "t,c,bw{100,20,100}",
|
|
127 |
total: 100,
|
|
128 |
current: 20,
|
|
129 |
barWidth: 100,
|
|
130 |
want: " [>---] ",
|
|
131 |
},
|
|
132 |
{
|
|
133 |
name: "t,c,bw{100,98,100}",
|
|
134 |
total: 100,
|
|
135 |
current: 98,
|
|
136 |
barWidth: 100,
|
|
137 |
want: " [====] ",
|
|
138 |
},
|
|
139 |
{
|
|
140 |
name: "t,c,bw{100,100,100}",
|
|
141 |
total: 100,
|
|
142 |
current: 100,
|
|
143 |
barWidth: 100,
|
|
144 |
want: " [====] ",
|
| 124 | 145 |
},
|
| 125 | 146 |
},
|
| 126 | 147 |
20: {
|
| 127 | |
"t,c,bw{100,20,100}": {
|
| 128 | |
total: 100,
|
| 129 | |
current: 20,
|
| 130 | |
barWidth: 100,
|
| 131 | |
want: "[===>--------------]",
|
| 132 | |
},
|
| 133 | |
"t,c,bw{100,60,100}": {
|
|
148 |
{
|
|
149 |
name: "t,c,bw{100,20,100}",
|
|
150 |
total: 100,
|
|
151 |
current: 20,
|
|
152 |
barWidth: 100,
|
|
153 |
want: " [==>-------------] ",
|
|
154 |
},
|
|
155 |
{
|
|
156 |
name: "t,c,bw{100,60,100}",
|
| 134 | 157 |
total: 100,
|
| 135 | 158 |
current: 60,
|
| 136 | 159 |
barWidth: 100,
|
| 137 | |
want: "[==========>-------]",
|
| 138 | |
},
|
| 139 | |
"t,c,bw{100,98,100}": {
|
| 140 | |
total: 100,
|
| 141 | |
current: 98,
|
| 142 | |
barWidth: 100,
|
| 143 | |
want: "[==================]",
|
| 144 | |
},
|
| 145 | |
"t,c,bw{100,100,100}": {
|
| 146 | |
total: 100,
|
| 147 | |
current: 100,
|
| 148 | |
barWidth: 100,
|
| 149 | |
want: "[==================]",
|
|
160 |
want: " [=========>------] ",
|
|
161 |
},
|
|
162 |
{
|
|
163 |
name: "t,c,bw{100,98,100}",
|
|
164 |
total: 100,
|
|
165 |
current: 98,
|
|
166 |
barWidth: 100,
|
|
167 |
want: " [================] ",
|
|
168 |
},
|
|
169 |
{
|
|
170 |
name: "t,c,bw{100,100,100}",
|
|
171 |
total: 100,
|
|
172 |
current: 100,
|
|
173 |
barWidth: 100,
|
|
174 |
want: " [================] ",
|
| 150 | 175 |
},
|
| 151 | 176 |
},
|
| 152 | 177 |
50: {
|
| 153 | |
"t,c,bw{100,20,100}": {
|
| 154 | |
total: 100,
|
| 155 | |
current: 20,
|
| 156 | |
barWidth: 100,
|
| 157 | |
want: "[=========>--------------------------------------]",
|
| 158 | |
},
|
| 159 | |
"t,c,bw{100,60,100}": {
|
|
178 |
{
|
|
179 |
name: "t,c,bw{100,20,100}",
|
|
180 |
total: 100,
|
|
181 |
current: 20,
|
|
182 |
barWidth: 100,
|
|
183 |
want: " [========>-------------------------------------] ",
|
|
184 |
},
|
|
185 |
{
|
|
186 |
name: "t,c,bw{100,60,100}",
|
| 160 | 187 |
total: 100,
|
| 161 | 188 |
current: 60,
|
| 162 | 189 |
barWidth: 100,
|
| 163 | |
want: "[============================>-------------------]",
|
| 164 | |
},
|
| 165 | |
"t,c,bw{100,98,100}": {
|
| 166 | |
total: 100,
|
| 167 | |
current: 98,
|
| 168 | |
barWidth: 100,
|
| 169 | |
want: "[==============================================>-]",
|
| 170 | |
},
|
| 171 | |
"t,c,bw{100,100,100}": {
|
| 172 | |
total: 100,
|
| 173 | |
current: 100,
|
| 174 | |
barWidth: 100,
|
| 175 | |
want: "[================================================]",
|
|
190 |
want: " [===========================>------------------] ",
|
|
191 |
},
|
|
192 |
{
|
|
193 |
name: "t,c,bw{100,98,100}",
|
|
194 |
total: 100,
|
|
195 |
current: 98,
|
|
196 |
barWidth: 100,
|
|
197 |
want: " [============================================>-] ",
|
|
198 |
},
|
|
199 |
{
|
|
200 |
name: "t,c,bw{100,100,100}",
|
|
201 |
total: 100,
|
|
202 |
current: 100,
|
|
203 |
barWidth: 100,
|
|
204 |
want: " [==============================================] ",
|
| 176 | 205 |
},
|
| 177 | 206 |
},
|
| 178 | 207 |
}
|
| 179 | 208 |
|
| 180 | 209 |
var tmpBuf bytes.Buffer
|
| 181 | 210 |
for termWidth, cases := range testSuite {
|
| 182 | |
for name, tc := range cases {
|
|
211 |
for _, tc := range cases {
|
| 183 | 212 |
s := newTestState()
|
| 184 | 213 |
s.width = tc.barWidth
|
| 185 | 214 |
s.total = tc.total
|
| 186 | 215 |
s.current = tc.current
|
|
216 |
s.trimSpace = tc.trimSpace
|
| 187 | 217 |
if tc.barRefill != nil {
|
| 188 | |
s.refill = tc.barRefill
|
|
218 |
s.filler.(*barFiller).refill = tc.barRefill
|
| 189 | 219 |
}
|
| 190 | 220 |
tmpBuf.Reset()
|
| 191 | 221 |
tmpBuf.ReadFrom(s.draw(termWidth))
|
| 192 | |
got := tmpBuf.String()
|
| 193 | |
want := tc.want + "\n"
|
| 194 | |
if got != want {
|
| 195 | |
t.Errorf("termWidth %d; %s: want: %s %d, got: %s %d\n", termWidth, name, want, len(want), got, len(got))
|
|
222 |
by := tmpBuf.Bytes()
|
|
223 |
by = by[:len(by)-1]
|
|
224 |
got := string(by)
|
|
225 |
if got != tc.want {
|
|
226 |
t.Errorf("termWidth %d; %s: want: %q %d, got: %q %d\n", termWidth, tc.name, tc.want, len(tc.want), got, len(got))
|
| 196 | 227 |
}
|
| 197 | 228 |
}
|
| 198 | 229 |
}
|
|
| 200 | 231 |
|
| 201 | 232 |
func newTestState() *bState {
|
| 202 | 233 |
s := &bState{
|
| 203 | |
trimLeftSpace: true,
|
| 204 | |
trimRightSpace: true,
|
| 205 | |
bufP: new(bytes.Buffer),
|
| 206 | |
bufB: new(bytes.Buffer),
|
| 207 | |
bufA: new(bytes.Buffer),
|
|
234 |
filler: &barFiller{format: defaultBarStyle},
|
|
235 |
bufP: new(bytes.Buffer),
|
|
236 |
bufB: new(bytes.Buffer),
|
|
237 |
bufA: new(bytes.Buffer),
|
| 208 | 238 |
}
|
| 209 | |
s.runes = strToBarRunes(pformat)
|
| 210 | 239 |
return s
|
| 211 | 240 |
}
|