-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsearch_test.go
More file actions
237 lines (198 loc) · 5.91 KB
/
search_test.go
File metadata and controls
237 lines (198 loc) · 5.91 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package midterm
import (
"strings"
"testing"
)
func TestSearchBasic(t *testing.T) {
vt := NewTerminal(10, 40)
vt.Write([]byte("hello world\r\nfoo bar hello\r\nbaz\r\n"))
count := vt.Search("hello")
if count != 2 {
t.Fatalf("expected 2 matches, got %d", count)
}
// Check highlights exist for the right rows
if len(vt.SearchHighlights[0]) != 1 {
t.Fatalf("expected 1 highlight on row 0, got %d", len(vt.SearchHighlights[0]))
}
if len(vt.SearchHighlights[1]) != 1 {
t.Fatalf("expected 1 highlight on row 1, got %d", len(vt.SearchHighlights[1]))
}
// Row 0: "hello" at cols [0, 5)
hl := vt.SearchHighlights[0][0]
if hl.Col != 0 || hl.End != 5 {
t.Fatalf("row 0 highlight: got [%d, %d), want [0, 5)", hl.Col, hl.End)
}
// Row 1: "foo bar hello" → "hello" at col 8
hl = vt.SearchHighlights[1][0]
if hl.Col != 8 || hl.End != 13 {
t.Fatalf("row 1 highlight: got [%d, %d), want [8, 13)", hl.Col, hl.End)
}
}
func TestSearchCaseInsensitive(t *testing.T) {
vt := NewTerminal(10, 40)
vt.Write([]byte("Hello HELLO hElLo\r\n"))
count := vt.Search("hello")
if count != 3 {
t.Fatalf("expected 3 matches, got %d", count)
}
}
func TestSearchSetCurrent(t *testing.T) {
vt := NewTerminal(10, 40)
vt.Write([]byte("aaa\r\naaa\r\naaa\r\n"))
vt.Search("aaa")
row, col := vt.SearchSetCurrent(1)
if row != 1 || col != 0 {
t.Fatalf("expected (1, 0), got (%d, %d)", row, col)
}
// Check that match 1 is marked current
hl := vt.SearchHighlights[1][0]
if !hl.Current {
t.Fatal("expected match 1 to be current")
}
// And match 0 is not
hl = vt.SearchHighlights[0][0]
if hl.Current {
t.Fatal("expected match 0 to not be current")
}
// Move current to match 0
row, col = vt.SearchSetCurrent(0)
if row != 0 || col != 0 {
t.Fatalf("expected (0, 0), got (%d, %d)", row, col)
}
if !vt.SearchHighlights[0][0].Current {
t.Fatal("expected match 0 to be current after SetCurrent(0)")
}
if vt.SearchHighlights[1][0].Current {
t.Fatal("expected match 1 to not be current after SetCurrent(0)")
}
}
func TestSearchClear(t *testing.T) {
vt := NewTerminal(10, 40)
vt.Write([]byte("hello\r\n"))
vt.Search("hello")
if len(vt.SearchHighlights) == 0 {
t.Fatal("expected highlights after search")
}
vt.SearchClear()
if vt.SearchHighlights != nil {
t.Fatal("expected nil highlights after clear")
}
}
func TestSearchRenderHighlight(t *testing.T) {
vt := NewTerminal(5, 20)
vt.Write([]byte("hello world\r\n"))
vt.Search("world")
var buf strings.Builder
err := vt.RenderLine(&buf, 0)
if err != nil {
t.Fatal(err)
}
rendered := buf.String()
// The rendered output should contain the highlight styling.
// SearchMatchStyle uses ANSIYellow bg + ANSIBlack fg.
// Just check that the output differs from a non-search render.
vt.SearchClear()
var buf2 strings.Builder
vt.RenderLine(&buf2, 0)
plain := buf2.String()
if rendered == plain {
t.Fatal("expected rendered output with search highlights to differ from plain")
}
}
func TestSearchEmptyQuery(t *testing.T) {
vt := NewTerminal(5, 20)
vt.Write([]byte("hello\r\n"))
count := vt.Search("")
if count != 0 {
t.Fatalf("expected 0 matches for empty query, got %d", count)
}
if vt.SearchHighlights != nil {
t.Fatal("expected nil highlights for empty query")
}
}
func TestSearchNoMatches(t *testing.T) {
vt := NewTerminal(5, 20)
vt.Write([]byte("hello world\r\n"))
count := vt.Search("xyz")
if count != 0 {
t.Fatalf("expected 0 matches, got %d", count)
}
}
func TestSearchIncremental(t *testing.T) {
vt := NewTerminal(10, 40)
vt.Write([]byte("hello world\r\nfoo bar\r\n"))
// Initial search.
count := vt.Search("hello")
if count != 1 {
t.Fatalf("initial: expected 1 match, got %d", count)
}
// Same query, no changes — should be a no-op and return same count.
count = vt.Search("hello")
if count != 1 {
t.Fatalf("no-op: expected 1 match, got %d", count)
}
// Write new content with another match.
vt.Write([]byte("hello again\r\n"))
count = vt.Search("hello")
if count != 2 {
t.Fatalf("after append: expected 2 matches, got %d", count)
}
// Verify matches are in row order.
if vt.SearchMatches[0].Row != 0 || vt.SearchMatches[1].Row != 2 {
t.Fatalf("matches not in row order: %+v", vt.SearchMatches)
}
}
func TestSearchIncrementalModifiedRow(t *testing.T) {
vt := NewTerminal(10, 40)
vt.Write([]byte("aaa\r\nbbb\r\nccc\r\n"))
vt.Search("aaa")
if len(vt.SearchMatches) != 1 {
t.Fatalf("expected 1 match, got %d", len(vt.SearchMatches))
}
// Overwrite row 0 with different content.
vt.Write([]byte("\x1b[1;1H")) // move to row 0, col 0
vt.Write([]byte("xxx")) // overwrite "aaa" with "xxx"
count := vt.Search("aaa")
if count != 0 {
t.Fatalf("after overwrite: expected 0 matches, got %d", count)
}
// The old highlight should be gone.
if len(vt.SearchHighlights[0]) != 0 {
t.Fatalf("expected no highlights on row 0, got %d", len(vt.SearchHighlights[0]))
}
}
func TestSearchIncrementalNewQuery(t *testing.T) {
vt := NewTerminal(10, 40)
vt.Write([]byte("hello world\r\nfoo bar\r\n"))
vt.Search("hello")
if len(vt.SearchMatches) != 1 {
t.Fatalf("expected 1 match for hello, got %d", len(vt.SearchMatches))
}
// Different query — should do a full re-search.
count := vt.Search("foo")
if count != 1 {
t.Fatalf("expected 1 match for foo, got %d", count)
}
if vt.SearchMatches[0].Row != 1 {
t.Fatalf("expected match on row 1, got row %d", vt.SearchMatches[0].Row)
}
}
func TestSearchMultipleOnSameLine(t *testing.T) {
vt := NewTerminal(5, 40)
vt.Write([]byte("abcabcabc\r\n"))
count := vt.Search("abc")
if count != 3 {
t.Fatalf("expected 3 matches, got %d", count)
}
hls := vt.SearchHighlights[0]
if len(hls) != 3 {
t.Fatalf("expected 3 highlights on row 0, got %d", len(hls))
}
// Check positions
expected := [][2]int{{0, 3}, {3, 6}, {6, 9}}
for i, e := range expected {
if hls[i].Col != e[0] || hls[i].End != e[1] {
t.Fatalf("highlight %d: got [%d, %d), want [%d, %d)", i, hls[i].Col, hls[i].End, e[0], e[1])
}
}
}