Codebase list golang-github-vbauerster-mpb / 8e52fe7
refactor tests Vladimir Bauer 7 years ago
5 changed file(s) with 222 addition(s) and 180 deletion(s). Raw diff Collapse all Expand all
66 "strings"
77 "testing"
88 "time"
9 "unicode/utf8"
910
1011 . "github.com/vbauerster/mpb"
1112 "github.com/vbauerster/mpb/decor"
6162 till := 30
6263 refillRune := '+'
6364
64 bar := p.AddBar(int64(total), BarTrim())
65 bar := p.AddBar(int64(total), TrimSpace())
6566
6667 bar.SetRefill(till, refillRune)
6768 bar.IncrBy(till)
7576
7677 wantBar := fmt.Sprintf("[%s%s]",
7778 strings.Repeat(string(refillRune), till-1),
78 strings.Repeat("=", total-till-1))
79 strings.Repeat("=", total-till-1),
80 )
7981
80 if !strings.Contains(buf.String(), wantBar) {
81 t.Errorf("Want bar: %s, got bar: %s\n", wantBar, buf.String())
82 got := string(getLastLine(buf.Bytes()))
83
84 if got != wantBar {
85 t.Errorf("Want bar: %q, got bar: %q\n", wantBar, got)
86 }
87 }
88
89 func TestBarStyle(t *testing.T) {
90 var buf bytes.Buffer
91 customFormat := "╢▌▌░╟"
92 p := New(WithOutput(&buf))
93 total := 80
94 bar := p.AddBar(int64(total), BarStyle(customFormat), TrimSpace())
95
96 for i := 0; i < total; i++ {
97 bar.Increment()
98 time.Sleep(10 * time.Millisecond)
99 }
100
101 p.Wait()
102
103 runes := []rune(customFormat)
104 wantBar := fmt.Sprintf("%s%s%s",
105 string(runes[0]),
106 strings.Repeat(string(runes[1]), total-2),
107 string(runes[len(runes)-1]),
108 )
109 got := string(getLastLine(buf.Bytes()))
110
111 if got != wantBar {
112 t.Errorf("Want bar: %q:%d, got bar: %q:%d\n", wantBar, utf8.RuneCountInString(wantBar), got, utf8.RuneCountInString(got))
82113 }
83114 }
84115
66
77 func TestDraw(t *testing.T) {
88 // key is termWidth
9 testSuite := map[int]map[string]struct {
9 testSuite := map[int][]struct {
10 name string
1011 total, current int64
1112 barWidth int
1213 barRefill *refill
14 trimSpace bool
1315 want string
1416 }{
1517 100: {
16 "t,c,bw{100,100,0}": {
18 {
19 name: "t,c,bw{100,100,0}",
1720 total: 100,
1821 current: 0,
1922 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}",
2335 total: 100,
2436 current: 1,
2537 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}",
2950 total: 100,
3051 current: 40,
3152 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}",
3557 total: 100,
3658 current: 40,
3759 barWidth: 100,
3860 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,
3970 want: "[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]",
4071 },
41 "t,c,bw{100,99,100}": {
72 {
73 name: "t,c,bw{100,99,100}",
4274 total: 100,
4375 current: 99,
4476 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: " [================================================================================================] ",
5285 },
5386 },
5487 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}",
6095 total: 60,
6196 current: 20,
6297 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: " [====] ",
124145 },
125146 },
126147 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}",
134157 total: 100,
135158 current: 60,
136159 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: " [================] ",
150175 },
151176 },
152177 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}",
160187 total: 100,
161188 current: 60,
162189 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: " [==============================================] ",
176205 },
177206 },
178207 }
179208
180209 var tmpBuf bytes.Buffer
181210 for termWidth, cases := range testSuite {
182 for name, tc := range cases {
211 for _, tc := range cases {
183212 s := newTestState()
184213 s.width = tc.barWidth
185214 s.total = tc.total
186215 s.current = tc.current
216 s.trimSpace = tc.trimSpace
187217 if tc.barRefill != nil {
188 s.refill = tc.barRefill
218 s.filler.(*barFiller).refill = tc.barRefill
189219 }
190220 tmpBuf.Reset()
191221 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))
196227 }
197228 }
198229 }
200231
201232 func newTestState() *bState {
202233 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),
208238 }
209 s.runes = strToBarRunes(pformat)
210239 return s
211240 }
1414 p := mpb.New(
1515 // override default (80) width
1616 mpb.WithWidth(64),
17 // override default "[=>-]" format
18 mpb.WithFormat("╢▌▌░╟"),
1917 // override default 120ms refresh rate
2018 mpb.WithRefreshRate(180*time.Millisecond),
2119 )
2422 name := "Single Bar:"
2523 // adding a single bar
2624 bar := p.AddBar(int64(total),
25 // override default "[=>-]" style
26 mpb.BarStyle("╢▌▌░╟"),
2727 mpb.PrependDecorators(
2828 // display our name with one space on the right
2929 decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}),
111111 }
112112 }
113113
114 func TestWithFormat(t *testing.T) {
115 var buf bytes.Buffer
116 customFormat := "╢▌▌░╟"
117 p := New(WithOutput(&buf), WithFormat(customFormat))
118 bar := p.AddBar(100, BarTrim())
119
120 for i := 0; i < 100; i++ {
121 if i == 33 {
122 p.Abort(bar, true)
123 break
124 }
125 time.Sleep(randomDuration(100 * time.Millisecond))
126 bar.Increment()
127 }
128
129 p.Wait()
130
131 lastLine := getLastLine(buf.Bytes())
132
133 for _, r := range customFormat {
134 if !bytes.ContainsRune(lastLine, r) {
135 t.Errorf("Rune %#U not found in bar\n", r)
136 }
137 }
138 }
139
140114 func getLastLine(bb []byte) []byte {
141115 split := bytes.Split(bb, []byte("\n"))
142116 lastLine := split[len(split)-2]
00 package mpb_test
11
22 import (
3 "bytes"
43 "io"
54 "io/ioutil"
65 "strings"
1716 cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
1817 est laborum.`
1918
19 type testReader struct {
20 io.Reader
21 called bool
22 }
23
24 func (r *testReader) Read(p []byte) (n int, err error) {
25 r.called = true
26 return r.Reader.Read(p)
27 }
28
2029 func TestProxyReader(t *testing.T) {
21 var buf bytes.Buffer
22 p := mpb.New(mpb.WithOutput(&buf))
2330
24 reader := strings.NewReader(content)
31 p := mpb.New(mpb.WithOutput(ioutil.Discard))
32
33 reader := &testReader{Reader: strings.NewReader(content)}
2534
2635 total := len(content)
27 bar := p.AddBar(100, mpb.BarTrim())
28 preader := bar.ProxyReader(reader)
36 bar := p.AddBar(100, mpb.TrimSpace())
2937
30 if _, ok := preader.(io.Closer); !ok {
31 t.Error("type assertion to io.Closer is not ok")
32 }
33
34 written, err := io.Copy(ioutil.Discard, preader)
38 written, err := io.Copy(ioutil.Discard, bar.ProxyReader(reader))
3539 if err != nil {
3640 t.Errorf("Error copying from reader: %+v\n", err)
3741 }
3842
3943 p.Wait()
4044
45 if !reader.called {
46 t.Error("Read not called")
47 }
48
4149 if written != int64(total) {
4250 t.Errorf("Expected written: %d, got: %d\n", total, written)
4351 }