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
|
package buffer
import "testing"
func TestFindMovesForwards(t *testing.T) {
b := NewFromString("func one()\nfunc two()\nfunc three()")
found, ok := b.Find("func", Position{Line: 0, Col: 1}, true)
if !ok {
t.Fatal("Find() = false, want a match")
}
if found.Start != (Position{Line: 1, Col: 0}) {
t.Errorf("the match starts at %+v, want line 1", found.Start)
}
if found.End != (Position{Line: 1, Col: 4}) {
t.Errorf("the match ends at %+v, want column 4", found.End)
}
}
func TestFindWrapsRoundToTheStart(t *testing.T) {
b := NewFromString("needle\nhaystack")
found, ok := b.Find("needle", Position{Line: 1, Col: 3}, true)
if !ok {
t.Fatal("Find() = false, want the search to wrap round")
}
if found.Start != (Position{}) {
t.Errorf("the match starts at %+v, want the top of the buffer", found.Start)
}
}
func TestFindIsCaseInsensitiveOnRequest(t *testing.T) {
b := NewFromString("Package Main")
if _, ok := b.Find("package", Position{}, true); ok {
t.Error("a case-sensitive search matched a different case")
}
found, ok := b.Find("package", Position{}, false)
if !ok {
t.Fatal("a case-insensitive search found nothing")
}
if found.Start != (Position{}) {
t.Errorf("the match starts at %+v", found.Start)
}
}
func TestFindColumnsAreCountedInRunes(t *testing.T) {
b := NewFromString("héllo wörld")
found, ok := b.Find("wörld", Position{}, true)
if !ok {
t.Fatal("Find() = false")
}
if found.Start.Col != 6 {
t.Errorf("the match starts at column %d, want 6 counted in runes", found.Start.Col)
}
if found.End.Col != 11 {
t.Errorf("the match ends at column %d, want 11", found.End.Col)
}
}
func TestFindNothing(t *testing.T) {
b := NewFromString("abc")
if _, ok := b.Find("xyz", Position{}, true); ok {
t.Error("Find() = true for a needle that is not there")
}
if _, ok := b.Find("", Position{}, true); ok {
t.Error("Find() = true for an empty needle")
}
if _, ok := b.FindPrevious("", Position{}, true); ok {
t.Error("FindPrevious() = true for an empty needle")
}
}
func TestFindPreviousMovesBackwards(t *testing.T) {
b := NewFromString("func one()\nfunc two()\nfunc three()")
found, ok := b.FindPrevious("func", Position{Line: 2, Col: 0}, true)
if !ok {
t.Fatal("FindPrevious() = false, want a match")
}
if found.Start != (Position{Line: 1, Col: 0}) {
t.Errorf("the match starts at %+v, want line 1", found.Start)
}
}
func TestFindPreviousWrapsRoundToTheEnd(t *testing.T) {
b := NewFromString("first\nlast target")
found, ok := b.FindPrevious("target", Position{}, true)
if !ok {
t.Fatal("FindPrevious() = false, want the search to wrap round")
}
if found.Start.Line != 1 {
t.Errorf("the match is on line %d, want the last line", found.Start.Line)
}
}
func TestFindPreviousPicksTheLastMatchOnALine(t *testing.T) {
b := NewFromString("x x x")
found, ok := b.FindPrevious("x", Position{Line: 0, Col: 5}, true)
if !ok {
t.Fatal("FindPrevious() = false")
}
if found.Start.Col != 4 {
t.Errorf("the match is at column %d, want the last one before the cursor", found.Start.Col)
}
}
func TestFindFromAPositionPastTheEndOfALine(t *testing.T) {
b := NewFromString("ab\ntarget")
found, ok := b.Find("target", Position{Line: 0, Col: 99}, true)
if !ok {
t.Fatal("Find() = false")
}
if found.Start.Line != 1 {
t.Errorf("the match is on line %d, want line 1", found.Start.Line)
}
}
|