1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
package ioprogress
import (
"bytes"
"io"
"io/ioutil"
"testing"
"time"
)
func TestReader(t *testing.T) {
testR := &testReader{
Data: [][]byte{
[]byte("ab"),
[]byte("cd"),
[]byte("ef"),
},
}
var buf bytes.Buffer
r := &Reader{
Reader: testR,
Size: testR.Size(),
DrawFunc: DrawTerminal(&buf),
DrawInterval: time.Microsecond,
}
io.Copy(ioutil.Discard, r)
if buf.String() != drawReaderStr {
t.Fatalf("bad:\n\n%#v", buf.String())
}
}
const drawReaderStr = "0/6\n2/6\n4/6\n6/6\n6/6\n\n"
// testReader is a test structure to help with testing the Reader by
// returning fixed slices of data.
type testReader struct {
Data [][]byte
i int
}
func (r *testReader) Read(p []byte) (int, error) {
// This is just so that our interval will fire properly
time.Sleep(5 * time.Microsecond)
if r.i == len(r.Data) {
return 0, io.EOF
}
copy(p, r.Data[r.i])
r.i += 1
return len(r.Data[r.i-1]), nil
}
func (r *testReader) Size() (n int64) {
for _, d := range r.Data {
n += int64(len(d))
}
return
}
|