peco.peco/reader_test.go
2014-08-27 14:17:13 +09:00

42 lines
667 B
Go

package peco
import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"
)
func TestReader(t *testing.T) {
ctx := NewCtx(nil)
buf := strings.NewReader(`
1. Foo
2. Bar
3. Baz
`)
rdr := ctx.NewBufferReader(ioutil.NopCloser(buf))
inputReady := false
time.AfterFunc(time.Second, func() {
fmt.Fprintf(os.Stderr, "afterfunc fired\n")
if !inputReady {
t.Errorf("inputReady not receieved even after 1 second")
close(rdr.inputReadyCh)
ctx.Stop()
}
})
go func() {
<-rdr.inputReadyCh
inputReady = true
}()
ctx.AddWaitGroup(1)
rdr.Loop()
if len(ctx.lines) != 3 {
t.Errorf("Expected 3 lines from input, only got %d", len(ctx.lines))
}
}