refactoring draw test
Vladimir Bauer
6 years ago
| 2 | 2 | import ( |
| 3 | 3 | "bytes" |
| 4 | 4 | "testing" |
| 5 | "unicode/utf8" | |
| 5 | 6 | ) |
| 6 | 7 | |
| 7 | 8 | func TestDraw(t *testing.T) { |
| 8 | 9 | // key is termWidth |
| 9 | 10 | testSuite := map[int][]struct { |
| 10 | 11 | name string |
| 12 | style string | |
| 11 | 13 | total, current int64 |
| 12 | 14 | barWidth int |
| 13 | 15 | trimSpace bool |
| 17 | 19 | }{ |
| 18 | 20 | 0: { |
| 19 | 21 | { |
| 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}", | |
| 21 | 171 | total: 60, |
| 22 | 172 | 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: "[==================>---------------------------------------]", | |
| 186 | 183 | }, |
| 187 | 184 | }, |
| 188 | 185 | 100: { |
| 189 | 186 | { |
| 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", | |
| 198 | 194 | total: 100, |
| 199 | 195 | current: 0, |
| 200 | barWidth: 100, | |
| 201 | 196 | trimSpace: true, |
| 202 | 197 | want: "[--------------------------------------------------------------------------------------------------]", |
| 203 | 198 | }, |
| 204 | 199 | { |
| 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", | |
| 213 | 207 | total: 100, |
| 214 | 208 | current: 1, |
| 215 | barWidth: 100, | |
| 216 | 209 | trimSpace: true, |
| 217 | 210 | want: "[>-------------------------------------------------------------------------------------------------]", |
| 218 | 211 | }, |
| 219 | 212 | { |
| 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", | |
| 228 | 220 | total: 100, |
| 229 | 221 | current: 33, |
| 230 | barWidth: 100, | |
| 231 | 222 | trimSpace: true, |
| 232 | 223 | want: "[===============================>------------------------------------------------------------------]", |
| 233 | 224 | }, |
| 234 | 225 | { |
| 235 | name: "t,c,bw,rev{100,33,100}trim", | |
| 226 | name: "t,c{100,33}trim,rev", | |
| 236 | 227 | total: 100, |
| 237 | 228 | current: 33, |
| 238 | barWidth: 100, | |
| 239 | 229 | trimSpace: true, |
| 240 | 230 | reverse: true, |
| 241 | 231 | want: "[------------------------------------------------------------------<===============================]", |
| 242 | 232 | }, |
| 243 | 233 | { |
| 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", | |
| 253 | 242 | total: 100, |
| 254 | 243 | current: 33, |
| 255 | barWidth: 100, | |
| 256 | 244 | rup: 33, |
| 257 | 245 | trimSpace: true, |
| 258 | 246 | want: "[+++++++++++++++++++++++++++++++>------------------------------------------------------------------]", |
| 259 | 247 | }, |
| 260 | 248 | { |
| 261 | name: "t,c,bw,rup,rev{100,33,100,33}trim", | |
| 249 | name: "t,c,rup{100,33,33}trim,rev", | |
| 262 | 250 | total: 100, |
| 263 | 251 | current: 33, |
| 264 | barWidth: 100, | |
| 265 | 252 | rup: 33, |
| 266 | 253 | trimSpace: true, |
| 267 | 254 | reverse: true, |
| 268 | 255 | want: "[------------------------------------------------------------------<+++++++++++++++++++++++++++++++]", |
| 269 | 256 | }, |
| 270 | 257 | { |
| 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", | |
| 280 | 266 | total: 100, |
| 281 | 267 | current: 40, |
| 282 | barWidth: 100, | |
| 283 | 268 | rup: 33, |
| 284 | 269 | trimSpace: true, |
| 285 | 270 | want: "[++++++++++++++++++++++++++++++++======>-----------------------------------------------------------]", |
| 286 | 271 | }, |
| 287 | 272 | { |
| 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", | |
| 296 | 280 | total: 100, |
| 297 | 281 | current: 99, |
| 298 | barWidth: 100, | |
| 299 | 282 | trimSpace: true, |
| 300 | 283 | want: "[================================================================================================>-]", |
| 301 | 284 | }, |
| 302 | 285 | { |
| 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", | |
| 311 | 293 | total: 100, |
| 312 | 294 | current: 100, |
| 313 | barWidth: 100, | |
| 314 | 295 | trimSpace: true, |
| 315 | 296 | want: "[==================================================================================================]", |
| 297 | }, | |
| 298 | { | |
| 299 | name: "[=の-] t,c{100,100}", | |
| 300 | style: "[=の-]", | |
| 301 | total: 100, | |
| 302 | current: 1, | |
| 303 | want: " [の---------------------------------------------------------------------------------------------…] ", | |
| 316 | 304 | }, |
| 317 | 305 | }, |
| 318 | 306 | } |
| 320 | 308 | var tmpBuf bytes.Buffer |
| 321 | 309 | for tw, cases := range testSuite { |
| 322 | 310 | for _, tc := range cases { |
| 323 | s := newTestState(tc.reverse) | |
| 311 | s := newTestState(tc.style, tc.reverse) | |
| 324 | 312 | s.reqWidth = tc.barWidth |
| 325 | 313 | s.total = tc.total |
| 326 | 314 | s.current = tc.current |
| 333 | 321 | tmpBuf.Reset() |
| 334 | 322 | tmpBuf.ReadFrom(s.draw(newStatistics(tw, s))) |
| 335 | 323 | 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 | } | |
| 339 | 329 | 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)) | |
| 341 | 331 | } |
| 342 | 332 | } |
| 343 | 333 | } |
| 344 | 334 | } |
| 345 | 335 | |
| 346 | func newTestState(reverse bool) *bState { | |
| 336 | func newTestState(style string, reverse bool) *bState { | |
| 337 | if style == "" { | |
| 338 | style = DefaultBarStyle | |
| 339 | } | |
| 347 | 340 | s := &bState{ |
| 348 | filler: NewBarFiller(DefaultBarStyle, reverse), | |
| 341 | filler: NewBarFiller(style, reverse), | |
| 349 | 342 | bufP: new(bytes.Buffer), |
| 350 | 343 | bufB: new(bytes.Buffer), |
| 351 | 344 | bufA: new(bytes.Buffer), |