| 2 | 2 |
import (
|
| 3 | 3 |
"bytes"
|
| 4 | 4 |
"testing"
|
|
5 |
"unicode/utf8"
|
| 5 | 6 |
)
|
| 6 | 7 |
|
| 7 | 8 |
func TestDraw(t *testing.T) {
|
|
| 14 | 15 |
rup int
|
| 15 | 16 |
want string
|
| 16 | 17 |
}{
|
|
18 |
2: {
|
|
19 |
{
|
|
20 |
name: "t,c,bw{60,20,80}",
|
|
21 |
total: 60,
|
|
22 |
current: 20,
|
|
23 |
barWidth: 80,
|
|
24 |
want: " ",
|
|
25 |
},
|
|
26 |
{
|
|
27 |
name: "t,c,bw,trim{60,20,80,true}",
|
|
28 |
total: 60,
|
|
29 |
current: 20,
|
|
30 |
barWidth: 80,
|
|
31 |
trimSpace: true,
|
|
32 |
want: "",
|
|
33 |
},
|
|
34 |
},
|
|
35 |
3: {
|
|
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,trim{60,20,80,true}",
|
|
45 |
total: 60,
|
|
46 |
current: 20,
|
|
47 |
barWidth: 80,
|
|
48 |
trimSpace: true,
|
|
49 |
want: "",
|
|
50 |
},
|
|
51 |
},
|
|
52 |
4: {
|
|
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,trim{60,20,80,true}",
|
|
62 |
total: 60,
|
|
63 |
current: 20,
|
|
64 |
barWidth: 80,
|
|
65 |
trimSpace: true,
|
|
66 |
want: "[]",
|
|
67 |
},
|
|
68 |
},
|
|
69 |
5: {
|
|
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,trim{60,20,80,true}",
|
|
79 |
total: 60,
|
|
80 |
current: 20,
|
|
81 |
barWidth: 80,
|
|
82 |
trimSpace: true,
|
|
83 |
want: "[>--]",
|
|
84 |
},
|
|
85 |
},
|
|
86 |
6: {
|
|
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,trim{60,20,80,true}",
|
|
96 |
total: 60,
|
|
97 |
current: 20,
|
|
98 |
barWidth: 80,
|
|
99 |
trimSpace: true,
|
|
100 |
want: "[>---]",
|
|
101 |
},
|
|
102 |
},
|
|
103 |
7: {
|
|
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,trim{60,20,80,true}",
|
|
113 |
total: 60,
|
|
114 |
current: 20,
|
|
115 |
barWidth: 80,
|
|
116 |
trimSpace: true,
|
|
117 |
want: "[=>---]",
|
|
118 |
},
|
|
119 |
},
|
|
120 |
8: {
|
|
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,trim{60,20,80,true}",
|
|
130 |
total: 60,
|
|
131 |
current: 20,
|
|
132 |
barWidth: 80,
|
|
133 |
trimSpace: true,
|
|
134 |
want: "[=>----]",
|
|
135 |
},
|
|
136 |
},
|
|
137 |
80: {
|
|
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,trim{60,20,80,true}",
|
|
147 |
total: 60,
|
|
148 |
current: 20,
|
|
149 |
barWidth: 80,
|
|
150 |
trimSpace: true,
|
|
151 |
want: "[=========================>----------------------------------------------------]",
|
|
152 |
},
|
|
153 |
},
|
| 17 | 154 |
100: {
|
| 18 | 155 |
{
|
| 19 | 156 |
name: "t,c,bw{100,100,0}",
|
|
| 23 | 160 |
want: " [------------------------------------------------------------------------------------------------] ",
|
| 24 | 161 |
},
|
| 25 | 162 |
{
|
| 26 | |
name: "t,c,bw{100,100,0}:trimSpace",
|
|
163 |
name: "t,c,bw,trim{100,100,0,true}",
|
| 27 | 164 |
total: 100,
|
| 28 | 165 |
current: 0,
|
| 29 | 166 |
barWidth: 100,
|
|
| 38 | 175 |
want: " [>-----------------------------------------------------------------------------------------------] ",
|
| 39 | 176 |
},
|
| 40 | 177 |
{
|
| 41 | |
name: "t,c,bw{100,1,100}:trimSpace",
|
|
178 |
name: "t,c,bw,trim{100,1,100,true}",
|
| 42 | 179 |
total: 100,
|
| 43 | 180 |
current: 1,
|
| 44 | 181 |
barWidth: 100,
|
|
| 46 | 183 |
want: "[>-------------------------------------------------------------------------------------------------]",
|
| 47 | 184 |
},
|
| 48 | 185 |
{
|
| 49 | |
name: "t,c,bw{100,40,100}",
|
|
186 |
name: "t,c,bw{100,33,100}",
|
|
187 |
total: 100,
|
|
188 |
current: 33,
|
|
189 |
barWidth: 100,
|
|
190 |
want: " [===============================>----------------------------------------------------------------] ",
|
|
191 |
},
|
|
192 |
{
|
|
193 |
name: "t,c,bw,trim{100,33,100,true}",
|
|
194 |
total: 100,
|
|
195 |
current: 33,
|
|
196 |
barWidth: 100,
|
|
197 |
trimSpace: true,
|
|
198 |
want: "[===============================>------------------------------------------------------------------]",
|
|
199 |
},
|
|
200 |
{
|
|
201 |
name: "t,c,bw,rup{100,33,100,33}",
|
|
202 |
total: 100,
|
|
203 |
current: 33,
|
|
204 |
barWidth: 100,
|
|
205 |
rup: 33,
|
|
206 |
want: " [+++++++++++++++++++++++++++++++>----------------------------------------------------------------] ",
|
|
207 |
},
|
|
208 |
{
|
|
209 |
name: "t,c,bw,rup,trim{100,33,100,33,true}",
|
|
210 |
total: 100,
|
|
211 |
current: 33,
|
|
212 |
barWidth: 100,
|
|
213 |
rup: 33,
|
|
214 |
trimSpace: true,
|
|
215 |
want: "[+++++++++++++++++++++++++++++++>------------------------------------------------------------------]",
|
|
216 |
},
|
|
217 |
{
|
|
218 |
name: "t,c,bw,rup{100,40,100,32}",
|
| 50 | 219 |
total: 100,
|
| 51 | 220 |
current: 40,
|
| 52 | 221 |
barWidth: 100,
|
| 53 | |
want: " [=====================================>----------------------------------------------------------] ",
|
| 54 | |
},
|
| 55 | |
{
|
| 56 | |
name: "t,c,bw,rup{100,40,100,32}",
|
| 57 | |
total: 100,
|
| 58 | |
current: 40,
|
| 59 | |
barWidth: 100,
|
| 60 | |
rup: 32,
|
| 61 | |
want: " [+++++++++++++++++++++++++++++++======>----------------------------------------------------------] ",
|
| 62 | |
},
|
| 63 | |
{
|
| 64 | |
name: "t,c,bw,rup{100,40,100,32}:trimSpace",
|
|
222 |
rup: 33,
|
|
223 |
want: " [++++++++++++++++++++++++++++++++=====>----------------------------------------------------------] ",
|
|
224 |
},
|
|
225 |
{
|
|
226 |
name: "t,c,bw,rup,trim{100,40,100,32,true}",
|
| 65 | 227 |
total: 100,
|
| 66 | 228 |
current: 40,
|
| 67 | 229 |
barWidth: 100,
|
| 68 | |
rup: 32,
|
| 69 | |
trimSpace: true,
|
| 70 | |
want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]",
|
|
230 |
rup: 33,
|
|
231 |
trimSpace: true,
|
|
232 |
want: "[++++++++++++++++++++++++++++++++======>-----------------------------------------------------------]",
|
| 71 | 233 |
},
|
| 72 | 234 |
{
|
| 73 | 235 |
name: "t,c,bw{100,99,100}",
|
|
| 77 | 239 |
want: " [==============================================================================================>-] ",
|
| 78 | 240 |
},
|
| 79 | 241 |
{
|
|
242 |
name: "t,c,bw,trim{100,99,100,true}",
|
|
243 |
total: 100,
|
|
244 |
current: 99,
|
|
245 |
barWidth: 100,
|
|
246 |
trimSpace: true,
|
|
247 |
want: "[================================================================================================>-]",
|
|
248 |
},
|
|
249 |
{
|
| 80 | 250 |
name: "t,c,bw{100,100,100}",
|
| 81 | 251 |
total: 100,
|
| 82 | 252 |
current: 100,
|
| 83 | 253 |
barWidth: 100,
|
| 84 | 254 |
want: " [================================================================================================] ",
|
| 85 | 255 |
},
|
| 86 | |
},
|
| 87 | |
2: {
|
| 88 | |
{
|
| 89 | |
name: "t,c,bw{0,0,100}",
|
| 90 | |
barWidth: 100,
|
| 91 | |
want: " [] ",
|
| 92 | |
},
|
| 93 | |
{
|
| 94 | |
name: "t,c,bw{60,20,80}",
|
| 95 | |
total: 60,
|
| 96 | |
current: 20,
|
| 97 | |
barWidth: 80,
|
| 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: " [====] ",
|
| 145 | |
},
|
| 146 | |
},
|
| 147 | |
20: {
|
| 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}",
|
| 157 | |
total: 100,
|
| 158 | |
current: 60,
|
| 159 | |
barWidth: 100,
|
| 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: " [================] ",
|
| 175 | |
},
|
| 176 | |
},
|
| 177 | |
50: {
|
| 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}",
|
| 187 | |
total: 100,
|
| 188 | |
current: 60,
|
| 189 | |
barWidth: 100,
|
| 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: " [==============================================] ",
|
|
256 |
{
|
|
257 |
name: "t,c,bw,trim{100,100,100,true}",
|
|
258 |
total: 100,
|
|
259 |
current: 100,
|
|
260 |
barWidth: 100,
|
|
261 |
trimSpace: true,
|
|
262 |
want: "[==================================================================================================]",
|
| 205 | 263 |
},
|
| 206 | 264 |
},
|
| 207 | 265 |
}
|
|
| 223 | 281 |
tmpBuf.ReadFrom(s.draw(termWidth))
|
| 224 | 282 |
by := tmpBuf.Bytes()
|
| 225 | 283 |
by = by[:len(by)-1]
|
|
284 |
|
|
285 |
if utf8.RuneCount(by) > termWidth {
|
|
286 |
t.Errorf("termWidth:%d %q barWidth:%d overflow termWidth\n", termWidth, tc.name, utf8.RuneCount(by))
|
|
287 |
}
|
|
288 |
|
| 226 | 289 |
got := string(by)
|
| 227 | 290 |
if got != tc.want {
|
| 228 | |
t.Errorf("termWidth %d; %s: want: %q %d, got: %q %d\n", termWidth, tc.name, tc.want, len(tc.want), got, len(got))
|
|
291 |
t.Errorf("termWidth:%d %q want: %q %d, got: %q %d\n", termWidth, tc.name, tc.want, len(tc.want), got, len(got))
|
| 229 | 292 |
}
|
| 230 | 293 |
}
|
| 231 | 294 |
}
|
|
| 233 | 296 |
|
| 234 | 297 |
func newTestState() *bState {
|
| 235 | 298 |
s := &bState{
|
| 236 | |
filler: &barFiller{format: defaultBarStyle},
|
|
299 |
filler: newDefaultBarFiller(),
|
| 237 | 300 |
bufP: new(bytes.Buffer),
|
| 238 | 301 |
bufB: new(bytes.Buffer),
|
| 239 | 302 |
bufA: new(bytes.Buffer),
|