|
0 |
package mpb_test
|
|
1 |
|
|
2 |
import (
|
|
3 |
"sync"
|
|
4 |
"testing"
|
|
5 |
"time"
|
|
6 |
|
|
7 |
. "github.com/vbauerster/mpb"
|
|
8 |
"github.com/vbauerster/mpb/decor"
|
|
9 |
)
|
|
10 |
|
|
11 |
func TestStaticName(t *testing.T) {
|
|
12 |
tests := []struct {
|
|
13 |
fn decor.DecoratorFunc
|
|
14 |
want string
|
|
15 |
}{
|
|
16 |
{
|
|
17 |
fn: decor.StaticName("Test", 0, 0),
|
|
18 |
want: "Test",
|
|
19 |
},
|
|
20 |
{
|
|
21 |
fn: decor.StaticName("Test", len("Test"), 0),
|
|
22 |
want: "Test",
|
|
23 |
},
|
|
24 |
{
|
|
25 |
fn: decor.StaticName("Test", 10, 0),
|
|
26 |
want: " Test",
|
|
27 |
},
|
|
28 |
{
|
|
29 |
fn: decor.StaticName("Test", 10, decor.DidentRight),
|
|
30 |
want: "Test ",
|
|
31 |
},
|
|
32 |
}
|
|
33 |
|
|
34 |
for _, test := range tests {
|
|
35 |
got := test.fn(nil, nil, nil)
|
|
36 |
if got != test.want {
|
|
37 |
t.Errorf("Want: %q, Got: %q\n", test.want, got)
|
|
38 |
}
|
|
39 |
}
|
|
40 |
}
|
|
41 |
|
|
42 |
type step struct {
|
|
43 |
stat *decor.Statistics
|
|
44 |
want string
|
|
45 |
}
|
|
46 |
|
|
47 |
func TestPercentageDwidthSync(t *testing.T) {
|
|
48 |
|
|
49 |
testCases := [][]step{
|
|
50 |
[]step{
|
|
51 |
{&decor.Statistics{Total: 100, Current: 8}, "8 %"},
|
|
52 |
{&decor.Statistics{Total: 100, Current: 9}, "9 %"},
|
|
53 |
},
|
|
54 |
[]step{
|
|
55 |
{&decor.Statistics{Total: 100, Current: 9}, " 9 %"},
|
|
56 |
{&decor.Statistics{Total: 100, Current: 10}, "10 %"},
|
|
57 |
},
|
|
58 |
[]step{
|
|
59 |
{&decor.Statistics{Total: 100, Current: 9}, " 9 %"},
|
|
60 |
{&decor.Statistics{Total: 100, Current: 100}, "100 %"},
|
|
61 |
},
|
|
62 |
}
|
|
63 |
|
|
64 |
dfn := decor.Percentage(3, decor.DwidthSync)
|
|
65 |
testDecoratorConcurrently(t, dfn, testCases)
|
|
66 |
}
|
|
67 |
|
|
68 |
func TestPercentageDwidthSyncDidentRight(t *testing.T) {
|
|
69 |
|
|
70 |
testCases := [][]step{
|
|
71 |
[]step{
|
|
72 |
{&decor.Statistics{Total: 100, Current: 8}, "8 %"},
|
|
73 |
{&decor.Statistics{Total: 100, Current: 9}, "9 %"},
|
|
74 |
},
|
|
75 |
[]step{
|
|
76 |
{&decor.Statistics{Total: 100, Current: 9}, "9 % "},
|
|
77 |
{&decor.Statistics{Total: 100, Current: 10}, "10 %"},
|
|
78 |
},
|
|
79 |
[]step{
|
|
80 |
{&decor.Statistics{Total: 100, Current: 9}, "9 % "},
|
|
81 |
{&decor.Statistics{Total: 100, Current: 100}, "100 %"},
|
|
82 |
},
|
|
83 |
}
|
|
84 |
|
|
85 |
dfn := decor.Percentage(3, decor.DwidthSync|decor.DidentRight)
|
|
86 |
testDecoratorConcurrently(t, dfn, testCases)
|
|
87 |
}
|
|
88 |
|
|
89 |
func TestPercentageDSyncSpace(t *testing.T) {
|
|
90 |
|
|
91 |
testCases := [][]step{
|
|
92 |
[]step{
|
|
93 |
{&decor.Statistics{Total: 100, Current: 8}, " 8 %"},
|
|
94 |
{&decor.Statistics{Total: 100, Current: 9}, " 9 %"},
|
|
95 |
},
|
|
96 |
[]step{
|
|
97 |
{&decor.Statistics{Total: 100, Current: 9}, " 9 %"},
|
|
98 |
{&decor.Statistics{Total: 100, Current: 10}, " 10 %"},
|
|
99 |
},
|
|
100 |
[]step{
|
|
101 |
{&decor.Statistics{Total: 100, Current: 9}, " 9 %"},
|
|
102 |
{&decor.Statistics{Total: 100, Current: 100}, " 100 %"},
|
|
103 |
},
|
|
104 |
}
|
|
105 |
|
|
106 |
dfn := decor.Percentage(3, decor.DSyncSpace)
|
|
107 |
testDecoratorConcurrently(t, dfn, testCases)
|
|
108 |
}
|
|
109 |
|
|
110 |
func testDecoratorConcurrently(t *testing.T, dfn decor.DecoratorFunc, testCases [][]step) {
|
|
111 |
if len(testCases) == 0 {
|
|
112 |
t.Fail()
|
|
113 |
}
|
|
114 |
|
|
115 |
numBars := len(testCases[0])
|
|
116 |
var wg sync.WaitGroup
|
|
117 |
for _, columnCase := range testCases {
|
|
118 |
wg.Add(numBars)
|
|
119 |
timeout := make(chan struct{})
|
|
120 |
time.AfterFunc(100*time.Millisecond, func() {
|
|
121 |
close(timeout)
|
|
122 |
})
|
|
123 |
ws := NewWidthSync(timeout, numBars, 1)
|
|
124 |
res := make([]chan string, numBars)
|
|
125 |
for i := 0; i < numBars; i++ {
|
|
126 |
res[i] = make(chan string, 1)
|
|
127 |
go func(s step, ch chan string) {
|
|
128 |
defer wg.Done()
|
|
129 |
ch <- dfn(s.stat, ws.Listen[0], ws.Result[0])
|
|
130 |
}(columnCase[i], res[i])
|
|
131 |
}
|
|
132 |
wg.Wait()
|
|
133 |
|
|
134 |
var i int
|
|
135 |
for got := range fanIn(res...) {
|
|
136 |
want := columnCase[i].want
|
|
137 |
if got != want {
|
|
138 |
t.Errorf("Want: %q, Got: %q\n", want, got)
|
|
139 |
}
|
|
140 |
i++
|
|
141 |
}
|
|
142 |
}
|
|
143 |
}
|
|
144 |
|
|
145 |
func fanIn(in ...chan string) <-chan string {
|
|
146 |
ch := make(chan string)
|
|
147 |
go func() {
|
|
148 |
defer close(ch)
|
|
149 |
for _, ich := range in {
|
|
150 |
ch <- <-ich
|
|
151 |
}
|
|
152 |
}()
|
|
153 |
return ch
|
|
154 |
}
|