| 2 | 2 |
import (
|
| 3 | 3 |
"bytes"
|
| 4 | 4 |
"fmt"
|
|
5 |
"io/ioutil"
|
| 5 | 6 |
"strings"
|
| 6 | 7 |
"sync"
|
| 7 | 8 |
"testing"
|
|
| 44 | 45 |
}
|
| 45 | 46 |
}
|
| 46 | 47 |
|
| 47 | |
func TestBarFormat(t *testing.T) {
|
| 48 | |
var buf bytes.Buffer
|
| 49 | |
cancel := make(chan struct{})
|
| 50 | |
customFormat := "(#>_)"
|
| 51 | |
p := mpb.New(
|
| 52 | |
mpb.Output(&buf),
|
| 53 | |
mpb.WithCancel(cancel),
|
| 54 | |
mpb.WithFormat(customFormat),
|
| 55 | |
)
|
| 56 | |
bar := p.AddBar(100, mpb.BarTrim())
|
| 57 | |
|
| 58 | |
go func() {
|
| 59 | |
for i := 0; i < 100; i++ {
|
| 60 | |
time.Sleep(10 * time.Millisecond)
|
| 61 | |
bar.Increment()
|
| 62 | |
}
|
| 63 | |
}()
|
| 64 | |
|
| 65 | |
time.Sleep(250 * time.Millisecond)
|
| 66 | |
close(cancel)
|
| 67 | |
p.Stop()
|
| 68 | |
|
| 69 | |
barAsStr := strings.Trim(buf.String(), "\n")
|
| 70 | |
|
| 71 | |
seen := make(map[rune]bool)
|
| 72 | |
for _, r := range barAsStr {
|
| 73 | |
if !seen[r] {
|
| 74 | |
seen[r] = true
|
| 75 | |
}
|
| 76 | |
}
|
| 77 | |
for _, r := range customFormat {
|
| 78 | |
if !seen[r] {
|
| 79 | |
t.Errorf("Rune %#U not found in bar\n", r)
|
| 80 | |
}
|
| 81 | |
}
|
| 82 | |
}
|
| 83 | |
|
| 84 | |
func TestBarInvalidFormat(t *testing.T) {
|
| 85 | |
var buf bytes.Buffer
|
| 86 | |
customWidth := 60
|
| 87 | |
customFormat := "(#>=_)"
|
| 88 | |
p := mpb.New(
|
| 89 | |
mpb.Output(&buf),
|
| 90 | |
mpb.WithWidth(customWidth),
|
| 91 | |
mpb.WithFormat(customFormat),
|
| 92 | |
)
|
| 93 | |
bar := p.AddBar(100, mpb.BarTrim())
|
| 94 | |
|
| 95 | |
for i := 0; i < 100; i++ {
|
| 96 | |
time.Sleep(10 * time.Millisecond)
|
| 97 | |
bar.Incr(1)
|
| 98 | |
}
|
| 99 | |
|
| 100 | |
p.Stop()
|
| 101 | |
|
| 102 | |
got := buf.String()
|
| 103 | |
want := fmt.Sprintf("[%s]", strings.Repeat("=", customWidth-2))
|
| 104 | |
if !strings.Contains(got, want) {
|
| 105 | |
t.Errorf("Expected format: %s, got %s\n", want, got)
|
| 106 | |
}
|
| 107 | |
}
|
| 108 | |
|
| 109 | 48 |
func TestBarInProgress(t *testing.T) {
|
| 110 | 49 |
var buf bytes.Buffer
|
| 111 | 50 |
cancel := make(chan struct{})
|
|
| 138 | 77 |
|
| 139 | 78 |
func TestBarGetID(t *testing.T) {
|
| 140 | 79 |
var wg sync.WaitGroup
|
| 141 | |
var buf bytes.Buffer
|
| 142 | |
p := mpb.New(mpb.Output(&buf))
|
|
80 |
p := mpb.New(
|
|
81 |
mpb.Output(ioutil.Discard),
|
|
82 |
mpb.WithWaitGroup(&wg),
|
|
83 |
)
|
| 143 | 84 |
|
| 144 | 85 |
numBars := 3
|
| 145 | 86 |
wg.Add(numBars)
|
|
| 164 | 105 |
}
|
| 165 | 106 |
}
|
| 166 | 107 |
|
| 167 | |
wg.Wait()
|
| 168 | 108 |
p.Stop()
|
| 169 | 109 |
}
|
| 170 | 110 |
|