-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch_test.go
112 lines (102 loc) · 2.72 KB
/
search_test.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import "testing"
func TestEnterSearchMode(t *testing.T) {
state := NewAppState([]string{"One"}, 10, 10)
state.CurrentMode = normal
state = KeyPress{Key: "?"}.Apply(state)
if state.CurrentMode != search {
t.Fail()
}
state.CurrentMode = normal
state = KeyPress{Key: "/"}.Apply(state)
if state.CurrentMode != search {
t.Fail()
}
}
func TestExitSearchModeEscape(t *testing.T) {
state := NewAppState([]string{"One"}, 10, 10)
state.CurrentMode = search
state = KeyPress{Key: "<escape>"}.Apply(state)
if state.CurrentMode != normal {
t.Fail()
}
}
func TestExitSearchModeEnter(t *testing.T) {
state := NewAppState([]string{"One"}, 10, 10)
state.CurrentMode = search
state = KeyPress{Key: "<enter>"}.Apply(state)
if state.CurrentMode != normal {
t.Fail()
}
}
func TestSearchTyping(t *testing.T) {
state := NewAppState([]string{"One"}, 10, 10)
state.CurrentMode = search
state = KeyPress{Key: "a"}.Apply(state)
if state.searchBuffer.text != "a" {
t.Fail()
}
}
func TestTypeKeySetsViewOffsetForSearch(t *testing.T) {
state := NewAppState([]string{"1"}, 2, 10)
state.Files["1"] = file{lines: lines{"abc1efg", "test", "other", "things", "out"}}
state.CurrentMode = search
state = state.typeKey("1")
if state.searchBuffer.text != "1" {
t.Fail()
}
if state.getSelectedView().offSet != 4 {
t.Fail()
}
}
func TestIncrementalSearch(t *testing.T) {
state := NewAppState([]string{"1"}, 2, 10)
state.Files["1"] = file{lines: lines{"banana", "test", "bana", "ban", "one"}}
state.CurrentMode = search
state.searchBuffer.text = "ba"
state = state.typeKey("n")
if state.getSelectedView().offSet != 1 {
t.Fail()
}
state = state.typeKey("a")
if state.getSelectedView().offSet != 2 {
t.Fail()
}
state = state.typeKey("n")
if state.getSelectedView().offSet != 4 {
t.Fail()
}
}
func TestFindNextMatch(t *testing.T) {
state := NewAppState([]string{"1"}, 1, 10)
state.Files["1"] = file{lines: lines{"banana", "test", "bana", "ban", "one"}}
state.CurrentMode = normal
state.searchBuffer.text = "ba"
state = state.findNext(up)
if state.searchIndex != 1 {
t.Error("incorrect search index")
}
if state.getSelectedView().offSet != 2 {
t.Error("incorrect view offset")
}
state = state.findNext(up)
if state.getSelectedView().offSet != 4 {
t.Error("incorrect view offset2")
}
if state.searchIndex != 2 {
t.Error("incorrect search index2")
}
}
func TestClearsSearchBufferAndIndexOnEnter(t *testing.T) {
state := NewAppState([]string{"One"}, 10, 10)
state.searchBuffer.text = "asdf"
state.searchIndex = 3
state.CurrentMode = normal
state = state.changeMode(search)
if state.searchBuffer.text != "" {
t.Error("Failed to clear buffer")
}
if state.searchIndex != 0 {
t.Error("Failed to clear searchIndex")
}
}