Codebase list golang-github-vbauerster-mpb / c0de378
TestCustomFormat Vladimir Bauer 9 years ago
1 changed file(s) with 26 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
33 "bytes"
44 "fmt"
55 "math/rand"
6 "strings"
76 "sync"
87 "testing"
98 "time"
1514 func TestDefaultWidth(t *testing.T) {
1615 var buf bytes.Buffer
1716 p := mpb.New().SetOut(&buf)
18 bar := p.AddBar(100)
17 bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
1918 for i := 0; i < 100; i++ {
2019 bar.Incr(1)
2120 }
2221 p.Stop()
23 runeCount := utf8.RuneCountInString(strings.TrimSpace(buf.String()))
24 defWidth := 80
22 runeCount := utf8.RuneCountInString(buf.String())
23 defWidth := 81 // + 1 for new line
2524 if runeCount != defWidth {
26 defWidth = 78 // when testing with ./...
27 if runeCount != defWidth {
28 t.Errorf("Expected default width: %d, got: %d\n", defWidth, runeCount)
29 }
25 t.Errorf("Expected default width: %d, got: %d\n", defWidth, runeCount)
3026 }
3127 }
3228
3430 customWidth := 60
3531 var buf bytes.Buffer
3632 p := mpb.New().SetWidth(customWidth).SetOut(&buf)
37 bar := p.AddBar(100)
33 bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
3834 for i := 0; i < 100; i++ {
3935 bar.Incr(1)
4036 }
4137 p.Stop()
42 runeCount := utf8.RuneCountInString(strings.TrimSpace(buf.String()))
43 if runeCount != customWidth {
44 customWidth = 58 // when testing with ./...
45 if runeCount != customWidth {
46 t.Errorf("Expected default width: %d, got: %d\n", customWidth, runeCount)
47 }
38 runeCount := utf8.RuneCountInString(buf.String())
39 if runeCount != customWidth+1 { // +1 for new line
40 t.Errorf("Expected default width: %d, got: %d\n", customWidth, runeCount)
4841 }
4942 }
5043
154147 _ = mpb.New().WithCancel(nil)
155148 }
156149
157 func TestFormat(t *testing.T) {
150 func TestCustomFormat(t *testing.T) {
158151 var buf bytes.Buffer
159152 cancel := make(chan struct{})
160153 shutdown := make(chan struct{})
161154 customFormat := "╢▌▌░╟"
162 p := mpb.New().Format(customFormat)
163 p.WithCancel(cancel)
164 p.ShutdownNotify(shutdown)
165 p.SetOut(&buf)
155 p := mpb.New().Format(customFormat).
156 WithCancel(cancel).
157 ShutdownNotify(shutdown).
158 SetOut(&buf)
166159 bar := p.AddBar(100).TrimLeftSpace().TrimRightSpace()
167160
168161 go func() {
169162 for i := 0; i < 100; i++ {
163 time.Sleep(10 * time.Millisecond)
170164 bar.Incr(1)
171 time.Sleep(10 * time.Millisecond)
172 if i == 42 {
173 close(cancel)
174 }
175165 }
176166 }()
177167
178 // p.Stop()
179 // time.Sleep(300 * time.Millisecond)
168 time.Sleep(300 * time.Millisecond)
169 close(cancel)
170 p.Stop()
180171
181 // gotBar := strings.TrimSpace(buf.String())
182 gotBar := buf.String()
172 bytes := buf.Bytes()
173 _, size := utf8.DecodeLastRune(bytes)
174 bytes = bytes[:len(bytes)-size] // removing new line
175
183176 seen := make(map[rune]bool)
184 for _, r := range gotBar {
177 for _, r := range string(bytes) {
185178 if !seen[r] {
186179 seen[r] = true
187180 }
188181 }
189 fmt.Println(gotBar)
190 for r, _ := range seen {
191 fmt.Printf("%#U\n", r)
182 for _, r := range customFormat {
183 if !seen[r] {
184 t.Errorf("Rune %#U not found in bar\n", r)
185 }
192186 }
193 // expectBar := "╢▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌╟"
194 // if gotBar != expectBar {
195 // t.Errorf("Expected for")
196 // }
197187 }