summaryrefslogtreecommitdiffstats
path: root/src/reader_test.go
blob: bb68e51085b42b90875768dd2b83775a08525d57 (plain)
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
package fzf

import (
	"testing"

	"github.com/junegunn/fzf/src/util"
)

func TestReadFromCommand(t *testing.T) {
	strs := []string{}
	eb := util.NewEventBox()
	reader := Reader{
		pusher:   func(s []rune) bool { strs = append(strs, string(s)); return true },
		eventBox: eb}

	// Check EventBox
	if eb.Peek(EvtReadNew) {
		t.Error("EvtReadNew should not be set yet")
	}

	// Normal command
	reader.readFromCommand(`echo abc && echo def`)
	if len(strs) != 2 || strs[0] != "abc" || strs[1] != "def" {
		t.Errorf("%s", strs)
	}

	// Check EventBox again
	if !eb.Peek(EvtReadNew) {
		t.Error("EvtReadNew should be set yet")
	}

	// Wait should return immediately
	eb.Wait(func(events *util.Events) {
		if _, found := (*events)[EvtReadNew]; !found {
			t.Errorf("%s", events)
		}
		events.Clear()
	})

	// EventBox is cleared
	if eb.Peek(EvtReadNew) {
		t.Error("EvtReadNew should not be set yet")
	}

	// Failing command
	reader.readFromCommand(`no-such-command`)
	strs = []string{}
	if len(strs) > 0 {
		t.Errorf("%s", strs)
	}

	// Check EventBox again
	if eb.Peek(EvtReadNew) {
		t.Error("Command failed. EvtReadNew should be set")
	}
}