fzf/src/reader_test.go

64 lines
1.3 KiB
Go
Raw Normal View History

2015-01-01 19:49:30 +00:00
package fzf
2015-01-12 03:56:17 +00:00
import (
"testing"
"time"
2015-01-12 03:56:17 +00:00
"github.com/junegunn/fzf/src/util"
)
2015-01-01 19:49:30 +00:00
func TestReadFromCommand(t *testing.T) {
strs := []string{}
2015-01-12 03:56:17 +00:00
eb := util.NewEventBox()
reader := NewReader(
func(s []byte) bool { strs = append(strs, string(s)); return true },
eb, false, true)
reader.startEventPoller()
2015-01-01 19:49:30 +00:00
// Check EventBox
2015-02-17 10:28:10 +00:00
if eb.Peek(EvtReadNew) {
2015-01-11 18:01:24 +00:00
t.Error("EvtReadNew should not be set yet")
2015-01-01 19:49:30 +00:00
}
// Normal command
reader.fin(reader.readFromCommand(nil, `echo abc && echo def`))
2015-01-01 19:49:30 +00:00
if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {
t.Errorf("%s", strs)
}
// Check EventBox again
eb.WaitFor(EvtReadFin)
2015-01-01 19:49:30 +00:00
// Wait should return immediately
2015-01-12 03:56:17 +00:00
eb.Wait(func(events *util.Events) {
2015-01-01 19:49:30 +00:00
events.Clear()
})
// EventBox is cleared
2015-02-17 10:28:10 +00:00
if eb.Peek(EvtReadNew) {
2015-01-11 18:01:24 +00:00
t.Error("EvtReadNew should not be set yet")
2015-01-01 19:49:30 +00:00
}
// Make sure that event poller is finished
time.Sleep(readerPollIntervalMax)
// Restart event poller
reader.startEventPoller()
2015-01-01 19:49:30 +00:00
// Failing command
reader.fin(reader.readFromCommand(nil, `no-such-command`))
2015-01-01 19:49:30 +00:00
strs = []string{}
if len(strs) > 0 {
t.Errorf("%s", strs)
}
// Check EventBox again
2015-02-17 10:28:10 +00:00
if eb.Peek(EvtReadNew) {
t.Error("Command failed. EvtReadNew should not be set")
}
if !eb.Peek(EvtReadFin) {
t.Error("EvtReadFin should be set")
2015-01-01 19:49:30 +00:00
}
}