Codebase list golang-github-vbauerster-mpb / 5994902
Refactoring tests Vladimir Bauer 8 years ago
4 changed file(s) with 52 addition(s) and 83 deletion(s). Raw diff Collapse all Expand all
0 package cwriter
1
2 var ClearCursorAndLine = clearCursorAndLine
33
44 import (
55 "bytes"
6 "fmt"
76 "testing"
87
9 "github.com/vbauerster/mpb/cwriter"
10 )
11
12 var (
13 cursorUp = fmt.Sprintf("%c[%dA", cwriter.ESC, 1)
14 clearLine = fmt.Sprintf("%c[2K\r", cwriter.ESC)
15 clearCursorAndLine = cursorUp + clearLine
8 . "github.com/vbauerster/mpb/cwriter"
169 )
1710
1811 // TestWriterPosix by writing and flushing many times. The output buffer
1912 // must contain the clearCursor and clearLine sequences.
2013 func TestWriterPosix(t *testing.T) {
2114 out := new(bytes.Buffer)
22 w := cwriter.New(out)
15 w := New(out)
2316
2417 for _, tcase := range []struct {
2518 input, expectedOutput string
2619 }{
2720 {input: "foo\n", expectedOutput: "foo\n"},
28 {input: "bar\n", expectedOutput: "foo\n" + clearCursorAndLine + "bar\n"},
29 {input: "fizz\n", expectedOutput: "foo\n" + clearCursorAndLine + "bar\n" + clearCursorAndLine + "fizz\n"},
21 {input: "bar\n", expectedOutput: "foo\n" + ClearCursorAndLine + "bar\n"},
22 {input: "fizz\n", expectedOutput: "foo\n" + ClearCursorAndLine + "bar\n" + ClearCursorAndLine + "fizz\n"},
3023 } {
3124 t.Run(tcase.input, func(t *testing.T) {
3225 w.Write([]byte(tcase.input))
44 "testing"
55 )
66
7 func TestFillBar(t *testing.T) {
7 func TestDraw(t *testing.T) {
88 // key is termWidth
99 testSuite := map[int]map[string]struct {
1010 total, current int64
179179
180180 prependWs := newWidthSyncer(nil, 1, 0)
181181 appendWs := newWidthSyncer(nil, 1, 0)
182 var temp bytes.Buffer
182183 for termWidth, cases := range testSuite {
183184 for name, tc := range cases {
184185 s := newTestState()
188189 if tc.barRefill != nil {
189190 s.refill = tc.barRefill
190191 }
191 s.draw(termWidth, prependWs, appendWs)
192 got := s.bufB.String()
193 if got != tc.want {
194 t.Errorf("termWidth %d; %s: want: %s %d, got: %s %d\n", termWidth, name, tc.want, len(tc.want), got, len(got))
192 temp.Reset()
193 temp.ReadFrom(s.draw(termWidth, prependWs, appendWs))
194 got := temp.String()
195 if got != tc.want+"\n" {
196 t.Errorf("termWidth %d; %s: want: %s %d, got: %s %d\n", termWidth, name, tc.want+"\n", len(tc.want), got, len(got))
195197 }
196198 }
197199 }
205207 bufB: new(bytes.Buffer),
206208 bufA: new(bytes.Buffer),
207209 }
208 s.updateFormat("[=>-]")
210 s.runes = strToBarRunes("[=>-]")
209211 return s
210212 }
44 "fmt"
55 "io/ioutil"
66 "math/rand"
7 "strings"
87 "sync"
98 "testing"
109 "time"
1110
12 "github.com/vbauerster/mpb"
11 . "github.com/vbauerster/mpb"
12 "github.com/vbauerster/mpb/cwriter"
1313 )
1414
1515 func init() {
1717 }
1818
1919 func TestBarCount(t *testing.T) {
20 p := mpb.New(mpb.WithOutput(ioutil.Discard))
20 p := New(WithOutput(ioutil.Discard))
2121
2222 var wg sync.WaitGroup
2323 wg.Add(1)
4343 }
4444
4545 func TestBarAbort(t *testing.T) {
46 p := mpb.New(mpb.WithOutput(ioutil.Discard))
46 p := New(WithOutput(ioutil.Discard))
4747
4848 var wg sync.WaitGroup
4949 wg.Add(1)
50 bars := make([]*mpb.Bar, 3)
50 bars := make([]*Bar, 3)
5151 for i := 0; i < 3; i++ {
5252 b := p.AddBar(100)
5353 bars[i] = b
7676 func TestWithCancel(t *testing.T) {
7777 cancel := make(chan struct{})
7878 shutdown := make(chan struct{})
79 p := mpb.New(
80 mpb.WithOutput(ioutil.Discard),
81 mpb.WithCancel(cancel),
82 mpb.WithShutdownNotifier(shutdown),
79 p := New(
80 WithOutput(ioutil.Discard),
81 WithCancel(cancel),
82 WithShutdownNotifier(shutdown),
8383 )
8484
8585 numBars := 3
86 bars := make([]*mpb.Bar, 0, numBars)
86 bars := make([]*Bar, 0, numBars)
8787 for i := 0; i < numBars; i++ {
88 bar := p.AddBar(int64(1000), mpb.BarID(i))
88 bar := p.AddBar(int64(1000), BarID(i))
8989 bars = append(bars, bar)
9090 go func() {
9191 for !bar.Completed() {
92 time.Sleep(randomDuration(40 * time.Millisecond))
92 time.Sleep(randomDuration(100 * time.Millisecond))
9393 bar.Increment()
9494 }
9595 }()
100100 })
101101
102102 p.Wait()
103 for _, bar := range bars {
104 if bar.Current() >= bar.Total() {
105 t.Errorf("bar %d: total = %d, current = %d\n", bar.ID(), bar.Total(), bar.Current())
106 }
107 }
103
108104 select {
109105 case <-shutdown:
110 case <-time.After(100 * time.Millisecond):
111 t.Error("Progress didn't stop")
106 case <-time.After(200 * time.Millisecond):
107 t.FailNow()
112108 }
113109 }
114110
115 func TestCustomFormat(t *testing.T) {
111 var (
112 cursorUp = fmt.Sprintf("%c[%dA", cwriter.ESC, 1)
113 clearLine = fmt.Sprintf("%c[2K\r", cwriter.ESC)
114 clearCursorAndLine = cursorUp + clearLine
115 )
116
117 func TestWithFormat(t *testing.T) {
116118 var buf bytes.Buffer
117 cancel := make(chan struct{})
118119 customFormat := "╢▌▌░╟"
119 p := mpb.New(
120 mpb.WithOutput(&buf),
121 mpb.WithCancel(cancel),
122 mpb.WithFormat(customFormat),
123 )
124 bar := p.AddBar(80, mpb.BarTrim())
125
126 var wg sync.WaitGroup
127 wg.Add(1)
128 go func() {
129 for i := 0; i < 80; i++ {
130 if i == 33 {
131 wg.Done()
132 }
133 time.Sleep(randomDuration(80 * time.Millisecond))
134 bar.Increment()
135 }
136 }()
137
138 wg.Wait()
139 close(cancel)
140 p.Wait()
141
142 for _, r := range customFormat {
143 if !bytes.ContainsRune(buf.Bytes(), r) {
144 t.Errorf("Rune %#U not found in bar\n", r)
145 }
146 }
147 }
148
149 func TestInvalidFormatWidth(t *testing.T) {
150 var buf bytes.Buffer
151 customWidth := 60
152 customFormat := "(#>=_)"
153 p := mpb.New(
154 mpb.WithOutput(&buf),
155 mpb.WithWidth(customWidth),
156 mpb.WithFormat(customFormat),
157 )
158 bar := p.AddBar(100, mpb.BarTrim())
120 p := New(WithOutput(&buf), WithFormat(customFormat))
121 bar := p.AddBar(100, BarTrim())
159122
160123 for i := 0; i < 100; i++ {
161 time.Sleep(randomDuration(40 * time.Millisecond))
124 if i == 33 {
125 p.Abort(bar)
126 break
127 }
128 time.Sleep(randomDuration(100 * time.Millisecond))
162129 bar.Increment()
163130 }
164131
165132 p.Wait()
166133
167 got := buf.String()
168 want := fmt.Sprintf("[%s]", strings.Repeat("=", customWidth-2))
169 if !strings.Contains(got, want) {
170 t.Errorf("Expected format: %s, got %s\n", want, got)
134 bb := bytes.Split(buf.Bytes(), []byte("\n"))
135 lastLine := bb[len(bb)-2]
136 lastLine = lastLine[len(clearCursorAndLine):]
137
138 for _, r := range customFormat {
139 if !bytes.ContainsRune(lastLine, r) {
140 t.Errorf("Rune %#U not found in bar\n", r)
141 }
171142 }
172143 }
173144