| 7 | 7 |
"sync"
|
| 8 | 8 |
"testing"
|
| 9 | 9 |
"time"
|
| 10 | |
"unicode/utf8"
|
| 11 | 10 |
|
| 12 | 11 |
"github.com/vbauerster/mpb"
|
| 13 | 12 |
"github.com/vbauerster/mpb/decor"
|
| 14 | 13 |
)
|
| 15 | 14 |
|
| 16 | |
func TestDefaultWidth(t *testing.T) {
|
| 17 | |
var buf bytes.Buffer
|
| 18 | |
p := mpb.New(mpb.Output(&buf))
|
| 19 | |
bar := p.AddBar(100, mpb.BarTrim())
|
| 20 | |
|
| 21 | |
for i := 0; i < 100; i++ {
|
| 22 | |
bar.Incr(1)
|
| 23 | |
}
|
| 24 | |
p.Stop()
|
| 25 | |
|
| 26 | |
wantWidth := 80
|
| 27 | |
gotWidth := utf8.RuneCount(buf.Bytes())
|
| 28 | |
if gotWidth != wantWidth+1 { // + 1 for new line
|
| 29 | |
t.Errorf("Expected default width: %d, got: %d\n", wantWidth, gotWidth)
|
| 30 | |
}
|
| 31 | |
}
|
| 32 | |
|
| 33 | |
func TestCustomWidth(t *testing.T) {
|
| 34 | |
wantWidth := 60
|
| 35 | |
var buf bytes.Buffer
|
| 36 | |
p := mpb.New(mpb.WithWidth(wantWidth), mpb.Output(&buf))
|
| 37 | |
bar := p.AddBar(100, mpb.BarTrim())
|
| 38 | |
|
| 39 | |
for i := 0; i < 100; i++ {
|
| 40 | |
bar.Incr(1)
|
| 41 | |
}
|
| 42 | |
p.Stop()
|
| 43 | |
|
| 44 | |
gotWidth := utf8.RuneCount(buf.Bytes())
|
| 45 | |
if gotWidth != wantWidth+1 { // +1 for new line
|
| 46 | |
t.Errorf("Expected default width: %d, got: %d\n", wantWidth, gotWidth)
|
| 47 | |
}
|
| 48 | |
}
|
| 49 | |
|
| 50 | 15 |
func TestAddBar(t *testing.T) {
|
| 51 | |
var wg sync.WaitGroup
|
| 52 | |
var buf bytes.Buffer
|
| 53 | |
p := mpb.New(mpb.Output(&buf), mpb.WithWaitGroup(&wg))
|
|
16 |
p := mpb.New()
|
| 54 | 17 |
|
| 55 | 18 |
count := p.BarCount()
|
| 56 | 19 |
if count != 0 {
|
| 57 | 20 |
t.Errorf("BarCount want: %q, got: %q\n", 0, count)
|
| 58 | 21 |
}
|
| 59 | 22 |
|
| 60 | |
numBars := 3
|
| 61 | |
wg.Add(numBars)
|
|
23 |
bar := p.AddBar(100)
|
| 62 | 24 |
|
| 63 | |
for i := 0; i < numBars; i++ {
|
| 64 | |
name := fmt.Sprintf("Bar#%d:", i)
|
| 65 | |
bar := p.AddBar(100, mpb.PrependDecorators(decor.StaticName(name, len(name), 0)))
|
| 66 | |
|
| 67 | |
go func() {
|
| 68 | |
defer wg.Done()
|
| 69 | |
for i := 0; i < 100; i++ {
|
| 70 | |
bar.Incr(1)
|
| 71 | |
}
|
| 72 | |
}()
|
|
25 |
count = p.BarCount()
|
|
26 |
if count != 1 {
|
|
27 |
t.Errorf("BarCount want: %q, got: %q\n", 1, count)
|
| 73 | 28 |
}
|
| 74 | 29 |
|
| 75 | |
count = p.BarCount()
|
| 76 | |
if count != numBars {
|
| 77 | |
t.Errorf("BarCount want: %q, got: %q\n", numBars, count)
|
| 78 | |
}
|
|
30 |
bar.Complete()
|
| 79 | 31 |
p.Stop()
|
| 80 | 32 |
}
|
| 81 | 33 |
|
| 82 | 34 |
func TestRemoveBar(t *testing.T) {
|
| 83 | 35 |
p := mpb.New()
|
| 84 | 36 |
|
| 85 | |
b := p.AddBar(10)
|
|
37 |
bar := p.AddBar(10)
|
| 86 | 38 |
|
| 87 | |
if !p.RemoveBar(b) {
|
|
39 |
if !p.RemoveBar(bar) {
|
| 88 | 40 |
t.Error("RemoveBar failure")
|
| 89 | 41 |
}
|
| 90 | 42 |
|
|
| 92 | 44 |
if count != 0 {
|
| 93 | 45 |
t.Errorf("BarCount want: %q, got: %q\n", 0, count)
|
| 94 | 46 |
}
|
|
47 |
|
|
48 |
bar.Complete()
|
| 95 | 49 |
p.Stop()
|
| 96 | 50 |
}
|
| 97 | 51 |
|
|
| 152 | 106 |
go func() {
|
| 153 | 107 |
for i := 0; i < 100; i++ {
|
| 154 | 108 |
time.Sleep(10 * time.Millisecond)
|
| 155 | |
bar.Incr(1)
|
|
109 |
bar.Increment()
|
| 156 | 110 |
}
|
| 157 | 111 |
}()
|
| 158 | 112 |
|
| 159 | |
time.Sleep(250 * time.Millisecond)
|
|
113 |
time.Sleep(300 * time.Millisecond)
|
| 160 | 114 |
close(cancel)
|
| 161 | 115 |
p.Stop()
|
| 162 | 116 |
|
| 163 | |
bytes := removeLastRune(buf.Bytes())
|
| 164 | |
|
| 165 | |
seen := make(map[rune]bool)
|
| 166 | |
for _, r := range string(bytes) {
|
| 167 | |
if !seen[r] {
|
| 168 | |
seen[r] = true
|
| 169 | |
}
|
| 170 | |
}
|
| 171 | 117 |
for _, r := range customFormat {
|
| 172 | |
if !seen[r] {
|
|
118 |
if !bytes.ContainsRune(buf.Bytes(), r) {
|
| 173 | 119 |
t.Errorf("Rune %#U not found in bar\n", r)
|
| 174 | 120 |
}
|
| 175 | 121 |
}
|