Debian Codegraph @

src/reader_test.go

raw
1 package fzf
2
3 import (
4 "testing"
5 "time"
6
7 "github.com/junegunn/fzf/src/util"
8 )
9
10 func TestReadFromCommand(t *testing.T) {
11 strs := []string{}
12 eb := util.NewEventBox()
13 exec := util.NewExecutor("")
14 reader := NewReader(
15 func(s []byte) bool { strs = append(strs, string(s)); return true },
16 eb, exec, false, true)
17
18 reader.startEventPoller()
19
20 // Check EventBox
21 if eb.Peek(EvtReadNew) {
22 t.Error("EvtReadNew should not be set yet")
23 }
24
25 // Normal command
26 counter := 0
27 ready := func() {
28 counter++
29 }
30 reader.fin(reader.readFromCommand(`echo abc&&echo def`, nil, ready))
31 if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" || counter != 1 {
32 t.Errorf("%s", strs)
33 }
34
35 // Check EventBox again
36 eb.WaitFor(EvtReadFin)
37
38 // Wait should return immediately
39 eb.Wait(func(events *util.Events) {
40 events.Clear()
41 })
42
43 // EventBox is cleared
44 if eb.Peek(EvtReadNew) {
45 t.Error("EvtReadNew should not be set yet")
46 }
47
48 // Make sure that event poller is finished
49 time.Sleep(readerPollIntervalMax)
50
51 // Restart event poller
52 reader.startEventPoller()
53
54 // Failing command
55 reader.fin(reader.readFromCommand(`no-such-command`, nil, ready))
56 strs = []string{}
57 if len(strs) > 0 || counter != 2 {
58 t.Errorf("%s", strs)
59 }
60
61 // Check EventBox again
62 if eb.Peek(EvtReadNew) {
63 t.Error("Command failed. EvtReadNew should not be set")
64 }
65 if !eb.Peek(EvtReadFin) {
66 t.Error("EvtReadFin should be set")
67 }
68 }