fzf/src/reader_test.go

64 lines
1.2 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
2024-02-19 03:39:04 +00:00
reader.fin(reader.readFromCommand(`echo abc&&echo def`, nil))
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
2024-02-19 03:39:04 +00:00
reader.fin(reader.readFromCommand(`no-such-command`, nil))
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
}
}