Codebase list golang-github-vbauerster-mpb / 09e94b5
public bar tests Vladimir Bauer 9 years ago
1 changed file(s) with 70 addition(s) and 100 deletion(s). Raw diff Collapse all Expand all
0 package mpb
0 package mpb_test
11
22 import (
3 "reflect"
3 "bytes"
44 "testing"
5 "time"
6 "unicode/utf8"
7
8 "github.com/vbauerster/mpb"
59 )
610
7 func TestFillBar(t *testing.T) {
8 tests := []struct {
9 termWidth int
10 barWidth int
11 total int64
12 current int64
13 barRefill *Refill
14 want []byte
15 }{
16 {
17 termWidth: 1,
18 barWidth: 100,
19 want: []byte{},
20 },
21 {
22 termWidth: 2,
23 barWidth: 100,
24 total: 100,
25 current: 20,
26 want: []byte("[]"),
27 },
28 {
29 termWidth: 20,
30 barWidth: 100,
31 total: 100,
32 current: 20,
33 want: []byte("[===>--------------]"),
34 },
35 {
36 termWidth: 50,
37 barWidth: 100,
38 total: 100,
39 current: 20,
40 want: []byte("[=========>--------------------------------------]"),
41 },
42 {
43 termWidth: 100,
44 barWidth: 100,
45 total: 100,
46 current: 0,
47 want: []byte("[--------------------------------------------------------------------------------------------------]"),
48 },
49 {
50 termWidth: 100,
51 barWidth: 100,
52 total: 100,
53 current: 1,
54 want: []byte("[>-------------------------------------------------------------------------------------------------]"),
55 },
56 {
57 termWidth: 100,
58 barWidth: 100,
59 total: 100,
60 current: 40,
61 want: []byte("[======================================>-----------------------------------------------------------]"),
62 },
63 {
64 termWidth: 100,
65 barWidth: 100,
66 total: 100,
67 current: 40,
68 barRefill: &Refill{'+', 32},
69 want: []byte("[+++++++++++++++++++++++++++++++=======>-----------------------------------------------------------]"),
70 },
71 {
72 termWidth: 100,
73 barWidth: 100,
74 total: 100,
75 current: 99,
76 want: []byte("[================================================================================================>-]"),
77 },
78 {
79 termWidth: 100,
80 barWidth: 100,
81 total: 100,
82 current: 100,
83 want: []byte("[==================================================================================================]"),
84 },
11 func TestBarSetWidth(t *testing.T) {
12 var buf bytes.Buffer
13 p := mpb.New().SetOut(&buf)
14 // overwrite default width 80
15 customWidth := 60
16 bar := p.AddBar(100).SetWidth(customWidth).
17 TrimLeftSpace().TrimRightSpace()
18 for i := 0; i < 100; i++ {
19 bar.Incr(1)
8520 }
21 p.Stop()
8622
87 prependWs := newWidthSync(nil, 1, 0)
88 appendWs := newWidthSync(nil, 1, 0)
89 for _, test := range tests {
90 s := newTestState()
91 s.width = test.barWidth
92 s.total = test.total
93 s.current = test.current
94 if test.barRefill != nil {
95 s.refill = test.barRefill
23 gotWidth := len(buf.Bytes())
24 if gotWidth != customWidth+1 { // +1 for new line
25 t.Errorf("Expected width: %d, got: %d\n", customWidth, gotWidth)
26 }
27 }
28
29 func TestBarSetInvalidWidth(t *testing.T) {
30 var buf bytes.Buffer
31 p := mpb.New().SetOut(&buf)
32 bar := p.AddBar(100).SetWidth(1).
33 TrimLeftSpace().TrimRightSpace()
34 for i := 0; i < 100; i++ {
35 bar.Incr(1)
36 }
37 p.Stop()
38
39 wantWidth := 80
40 gotWidth := len(buf.Bytes())
41 if gotWidth != wantWidth+1 { // +1 for new line
42 t.Errorf("Expected width: %d, got: %d\n", wantWidth, gotWidth)
43 }
44 }
45
46 func TestBarFormat(t *testing.T) {
47 var buf bytes.Buffer
48 cancel := make(chan struct{})
49 p := mpb.New().WithCancel(cancel).SetOut(&buf)
50 customFormat := "(#>_)"
51 bar := p.AddBar(100).Format(customFormat).
52 TrimLeftSpace().TrimRightSpace()
53
54 go func() {
55 for i := 0; i < 100; i++ {
56 time.Sleep(10 * time.Millisecond)
57 bar.Incr(1)
9658 }
97 got := draw(s, test.termWidth, prependWs, appendWs)
98 if !reflect.DeepEqual(test.want, got) {
99 t.Errorf("Want: %q, Got: %q\n", test.want, got)
59 }()
60
61 time.Sleep(250 * time.Millisecond)
62 close(cancel)
63 p.Stop()
64
65 bytes := buf.Bytes()
66 _, size := utf8.DecodeLastRune(bytes)
67 bytes = bytes[:len(bytes)-size] // removing new line
68
69 seen := make(map[rune]bool)
70 for _, r := range string(bytes) {
71 if !seen[r] {
72 seen[r] = true
73 }
74 }
75 for _, r := range customFormat {
76 if !seen[r] {
77 t.Errorf("Rune %#U not found in bar\n", r)
10078 }
10179 }
10280 }
103
104 func newTestState() *state {
105 return &state{
106 format: barFmtRunes{'[', '=', '>', '-', ']'},
107 trimLeftSpace: true,
108 trimRightSpace: true,
109 }
110 }