Codebase list golang-github-vbauerster-mpb / 2616cf2
refactoring draw test Vladimir Bauer 6 years ago
1 changed file(s) with 233 addition(s) and 240 deletion(s). Raw diff Collapse all Expand all
22 import (
33 "bytes"
44 "testing"
5 "unicode/utf8"
56 )
67
78 func TestDraw(t *testing.T) {
89 // key is termWidth
910 testSuite := map[int][]struct {
1011 name string
12 style string
1113 total, current int64
1214 barWidth int
1315 trimSpace bool
1719 }{
1820 0: {
1921 {
20 name: "t,c,bw{60,20,80}",
22 name: "t,c{60,20}",
23 total: 60,
24 current: 20,
25 want: "… ",
26 },
27 {
28 name: "t,c{60,20}trim",
29 total: 60,
30 current: 20,
31 trimSpace: true,
32 want: "",
33 },
34 },
35 1: {
36 {
37 name: "t,c{60,20}",
38 total: 60,
39 current: 20,
40 want: "… ",
41 },
42 {
43 name: "t,c{60,20}trim",
44 total: 60,
45 current: 20,
46 trimSpace: true,
47 want: "",
48 },
49 },
50 2: {
51 {
52 name: "t,c{60,20}",
53 total: 60,
54 current: 20,
55 want: " ",
56 },
57 {
58 name: "t,c{60,20}trim",
59 total: 60,
60 current: 20,
61 trimSpace: true,
62 want: "[]",
63 },
64 },
65 3: {
66 {
67 name: "t,c{60,20}",
68 total: 60,
69 current: 20,
70 want: " ",
71 },
72 {
73 name: "t,c{60,20}trim",
74 total: 60,
75 current: 20,
76 trimSpace: true,
77 want: "[-]",
78 },
79 },
80 4: {
81 {
82 name: "t,c{60,20}",
83 total: 60,
84 current: 20,
85 want: " [] ",
86 },
87 {
88 name: "t,c{60,20}trim",
89 total: 60,
90 current: 20,
91 trimSpace: true,
92 want: "[>-]",
93 },
94 },
95 5: {
96 {
97 name: "t,c{60,20}",
98 total: 60,
99 current: 20,
100 want: " [-] ",
101 },
102 {
103 name: "t,c{60,20}trim",
104 total: 60,
105 current: 20,
106 trimSpace: true,
107 want: "[>--]",
108 },
109 },
110 6: {
111 {
112 name: "t,c{60,20}",
113 total: 60,
114 current: 20,
115 want: " [>-] ",
116 },
117 {
118 name: "t,c{60,20}trim",
119 total: 60,
120 current: 20,
121 trimSpace: true,
122 want: "[>---]",
123 },
124 },
125 7: {
126 {
127 name: "t,c{60,20}",
128 total: 60,
129 current: 20,
130 want: " [>--] ",
131 },
132 {
133 name: "t,c{60,20}trim",
134 total: 60,
135 current: 20,
136 trimSpace: true,
137 want: "[=>---]",
138 },
139 },
140 8: {
141 {
142 name: "t,c{60,20}",
143 total: 60,
144 current: 20,
145 want: " [>---] ",
146 },
147 {
148 name: "t,c{60,20}trim",
149 total: 60,
150 current: 20,
151 trimSpace: true,
152 want: "[=>----]",
153 },
154 },
155 80: {
156 {
157 name: "t,c{60,20}",
158 total: 60,
159 current: 20,
160 want: " [========================>---------------------------------------------------] ",
161 },
162 {
163 name: "t,c{60,20}trim",
164 total: 60,
165 current: 20,
166 trimSpace: true,
167 want: "[=========================>----------------------------------------------------]",
168 },
169 {
170 name: "t,c,bw{60,20,60}",
21171 total: 60,
22172 current: 20,
23 barWidth: 80,
24 want: "… ",
25 },
26 {
27 name: "t,c,bw{60,20,80}trim",
28 total: 60,
29 current: 20,
30 barWidth: 80,
31 trimSpace: true,
32 want: "",
33 },
34 },
35 1: {
36 {
37 name: "t,c,bw{60,20,80}",
38 total: 60,
39 current: 20,
40 barWidth: 80,
41 want: "… ",
42 },
43 {
44 name: "t,c,bw{60,20,80}trim",
45 total: 60,
46 current: 20,
47 barWidth: 80,
48 trimSpace: true,
49 want: "",
50 },
51 },
52 2: {
53 {
54 name: "t,c,bw{60,20,80}",
55 total: 60,
56 current: 20,
57 barWidth: 80,
58 want: " ",
59 },
60 {
61 name: "t,c,bw{60,20,80}trim",
62 total: 60,
63 current: 20,
64 barWidth: 80,
65 trimSpace: true,
66 want: "[]",
67 },
68 },
69 3: {
70 {
71 name: "t,c,bw{60,20,80}",
72 total: 60,
73 current: 20,
74 barWidth: 80,
75 want: " ",
76 },
77 {
78 name: "t,c,bw{60,20,80}trim",
79 total: 60,
80 current: 20,
81 barWidth: 80,
82 trimSpace: true,
83 want: "[-]",
84 },
85 },
86 4: {
87 {
88 name: "t,c,bw{60,20,80}",
89 total: 60,
90 current: 20,
91 barWidth: 80,
92 want: " [] ",
93 },
94 {
95 name: "t,c,bw{60,20,80}trim",
96 total: 60,
97 current: 20,
98 barWidth: 80,
99 trimSpace: true,
100 want: "[>-]",
101 },
102 },
103 5: {
104 {
105 name: "t,c,bw{60,20,80}",
106 total: 60,
107 current: 20,
108 barWidth: 80,
109 want: " [-] ",
110 },
111 {
112 name: "t,c,bw{60,20,80}trim",
113 total: 60,
114 current: 20,
115 barWidth: 80,
116 trimSpace: true,
117 want: "[>--]",
118 },
119 },
120 6: {
121 {
122 name: "t,c,bw{60,20,80}",
123 total: 60,
124 current: 20,
125 barWidth: 80,
126 want: " [>-] ",
127 },
128 {
129 name: "t,c,bw{60,20,80}trim",
130 total: 60,
131 current: 20,
132 barWidth: 80,
133 trimSpace: true,
134 want: "[>---]",
135 },
136 },
137 7: {
138 {
139 name: "t,c,bw{60,20,80}",
140 total: 60,
141 current: 20,
142 barWidth: 80,
143 want: " [>--] ",
144 },
145 {
146 name: "t,c,bw{60,20,80}trim",
147 total: 60,
148 current: 20,
149 barWidth: 80,
150 trimSpace: true,
151 want: "[=>---]",
152 },
153 },
154 8: {
155 {
156 name: "t,c,bw{60,20,80}",
157 total: 60,
158 current: 20,
159 barWidth: 80,
160 want: " [>---] ",
161 },
162 {
163 name: "t,c,bw{60,20,80}trim",
164 total: 60,
165 current: 20,
166 barWidth: 80,
167 trimSpace: true,
168 want: "[=>----]",
169 },
170 },
171 80: {
172 {
173 name: "t,c,bw{60,20,80}",
174 total: 60,
175 current: 20,
176 barWidth: 80,
177 want: " [========================>---------------------------------------------------] ",
178 },
179 {
180 name: "t,c,bw{60,20,80}trim",
181 total: 60,
182 current: 20,
183 barWidth: 80,
184 trimSpace: true,
185 want: "[=========================>----------------------------------------------------]",
173 barWidth: 60,
174 want: " [==================>---------------------------------------] ",
175 },
176 {
177 name: "t,c,bw{60,20,60}trim",
178 total: 60,
179 current: 20,
180 barWidth: 60,
181 trimSpace: true,
182 want: "[==================>---------------------------------------]",
186183 },
187184 },
188185 100: {
189186 {
190 name: "t,c,bw{100,100,0}",
191 total: 100,
192 current: 0,
193 barWidth: 100,
194 want: " [------------------------------------------------------------------------------------------------] ",
195 },
196 {
197 name: "t,c,bw{100,100,0}trim",
187 name: "t,c{100,0}",
188 total: 100,
189 current: 0,
190 want: " [------------------------------------------------------------------------------------------------] ",
191 },
192 {
193 name: "t,c{100,0}trim",
198194 total: 100,
199195 current: 0,
200 barWidth: 100,
201196 trimSpace: true,
202197 want: "[--------------------------------------------------------------------------------------------------]",
203198 },
204199 {
205 name: "t,c,bw{100,1,100}",
206 total: 100,
207 current: 1,
208 barWidth: 100,
209 want: " [>-----------------------------------------------------------------------------------------------] ",
210 },
211 {
212 name: "t,c,bw{100,1,100}trim",
200 name: "t,c{100,1}",
201 total: 100,
202 current: 1,
203 want: " [>-----------------------------------------------------------------------------------------------] ",
204 },
205 {
206 name: "t,c{100,1}trim",
213207 total: 100,
214208 current: 1,
215 barWidth: 100,
216209 trimSpace: true,
217210 want: "[>-------------------------------------------------------------------------------------------------]",
218211 },
219212 {
220 name: "t,c,bw{100,33,100}",
221 total: 100,
222 current: 33,
223 barWidth: 100,
224 want: " [===============================>----------------------------------------------------------------] ",
225 },
226 {
227 name: "t,c,bw{100,33,100}trim",
213 name: "t,c{100,33}",
214 total: 100,
215 current: 33,
216 want: " [===============================>----------------------------------------------------------------] ",
217 },
218 {
219 name: "t,c{100,33}trim",
228220 total: 100,
229221 current: 33,
230 barWidth: 100,
231222 trimSpace: true,
232223 want: "[===============================>------------------------------------------------------------------]",
233224 },
234225 {
235 name: "t,c,bw,rev{100,33,100}trim",
226 name: "t,c{100,33}trim,rev",
236227 total: 100,
237228 current: 33,
238 barWidth: 100,
239229 trimSpace: true,
240230 reverse: true,
241231 want: "[------------------------------------------------------------------<===============================]",
242232 },
243233 {
244 name: "t,c,bw,rup{100,33,100,33}",
245 total: 100,
246 current: 33,
247 barWidth: 100,
248 rup: 33,
249 want: " [+++++++++++++++++++++++++++++++>----------------------------------------------------------------] ",
250 },
251 {
252 name: "t,c,bw,rup{100,33,100,33}trim",
234 name: "t,c,rup{100,33,33}",
235 total: 100,
236 current: 33,
237 rup: 33,
238 want: " [+++++++++++++++++++++++++++++++>----------------------------------------------------------------] ",
239 },
240 {
241 name: "t,c,rup{100,33,33}trim",
253242 total: 100,
254243 current: 33,
255 barWidth: 100,
256244 rup: 33,
257245 trimSpace: true,
258246 want: "[+++++++++++++++++++++++++++++++>------------------------------------------------------------------]",
259247 },
260248 {
261 name: "t,c,bw,rup,rev{100,33,100,33}trim",
249 name: "t,c,rup{100,33,33}trim,rev",
262250 total: 100,
263251 current: 33,
264 barWidth: 100,
265252 rup: 33,
266253 trimSpace: true,
267254 reverse: true,
268255 want: "[------------------------------------------------------------------<+++++++++++++++++++++++++++++++]",
269256 },
270257 {
271 name: "t,c,bw,rup{100,40,100,32}",
272 total: 100,
273 current: 40,
274 barWidth: 100,
275 rup: 33,
276 want: " [++++++++++++++++++++++++++++++++=====>----------------------------------------------------------] ",
277 },
278 {
279 name: "t,c,bw,rup{100,40,100,32}trim",
258 name: "t,c,rup{100,40,33}",
259 total: 100,
260 current: 40,
261 rup: 33,
262 want: " [++++++++++++++++++++++++++++++++=====>----------------------------------------------------------] ",
263 },
264 {
265 name: "t,c,rup{100,40,33}trim",
280266 total: 100,
281267 current: 40,
282 barWidth: 100,
283268 rup: 33,
284269 trimSpace: true,
285270 want: "[++++++++++++++++++++++++++++++++======>-----------------------------------------------------------]",
286271 },
287272 {
288 name: "t,c,bw{100,99,100}",
289 total: 100,
290 current: 99,
291 barWidth: 100,
292 want: " [==============================================================================================>-] ",
293 },
294 {
295 name: "t,c,bw{100,99,100}trim",
273 name: "t,c{100,99}",
274 total: 100,
275 current: 99,
276 want: " [==============================================================================================>-] ",
277 },
278 {
279 name: "t,c{100,99}trim",
296280 total: 100,
297281 current: 99,
298 barWidth: 100,
299282 trimSpace: true,
300283 want: "[================================================================================================>-]",
301284 },
302285 {
303 name: "t,c,bw{100,100,100}",
304 total: 100,
305 current: 100,
306 barWidth: 100,
307 want: " [================================================================================================] ",
308 },
309 {
310 name: "t,c,bw{100,100,100}trim",
286 name: "t,c{100,100}",
287 total: 100,
288 current: 100,
289 want: " [================================================================================================] ",
290 },
291 {
292 name: "t,c{100,100}trim",
311293 total: 100,
312294 current: 100,
313 barWidth: 100,
314295 trimSpace: true,
315296 want: "[==================================================================================================]",
297 },
298 {
299 name: "[=の-] t,c{100,100}",
300 style: "[=の-]",
301 total: 100,
302 current: 1,
303 want: " [の---------------------------------------------------------------------------------------------…] ",
316304 },
317305 },
318306 }
320308 var tmpBuf bytes.Buffer
321309 for tw, cases := range testSuite {
322310 for _, tc := range cases {
323 s := newTestState(tc.reverse)
311 s := newTestState(tc.style, tc.reverse)
324312 s.reqWidth = tc.barWidth
325313 s.total = tc.total
326314 s.current = tc.current
333321 tmpBuf.Reset()
334322 tmpBuf.ReadFrom(s.draw(newStatistics(tw, s)))
335323 by := tmpBuf.Bytes()
336 by = by[:len(by)-1]
337
338 got := string(by)
324
325 got := string(by[:len(by)-1])
326 if !utf8.ValidString(got) {
327 t.Fail()
328 }
339329 if got != tc.want {
340 t.Errorf("termWidth:%d %q want: %q %d, got: %q %d\n", tw, tc.name, tc.want, len(tc.want), got, len(got))
330 t.Errorf("termWidth:%d %q want: %q %d, got: %q %d\n", tw, tc.name, tc.want, utf8.RuneCountInString(tc.want), got, utf8.RuneCountInString(got))
341331 }
342332 }
343333 }
344334 }
345335
346 func newTestState(reverse bool) *bState {
336 func newTestState(style string, reverse bool) *bState {
337 if style == "" {
338 style = DefaultBarStyle
339 }
347340 s := &bState{
348 filler: NewBarFiller(DefaultBarStyle, reverse),
341 filler: NewBarFiller(style, reverse),
349342 bufP: new(bytes.Buffer),
350343 bufB: new(bytes.Buffer),
351344 bufA: new(bytes.Buffer),