turbo-editors/turbo-corepublic Fork 0
v1.0.2
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

find_test.go · 129 lines · 3.3 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 21h ago1package buffer
2
3import "testing"
4
5func TestFindMovesForwards(t *testing.T) {
6 b := NewFromString("func one()\nfunc two()\nfunc three()")
7
8 found, ok := b.Find("func", Position{Line: 0, Col: 1}, true)
9
10 if !ok {
11 t.Fatal("Find() = false, want a match")
12 }
13 if found.Start != (Position{Line: 1, Col: 0}) {
14 t.Errorf("the match starts at %+v, want line 1", found.Start)
15 }
16 if found.End != (Position{Line: 1, Col: 4}) {
17 t.Errorf("the match ends at %+v, want column 4", found.End)
18 }
19}
20
21func TestFindWrapsRoundToTheStart(t *testing.T) {
22 b := NewFromString("needle\nhaystack")
23
24 found, ok := b.Find("needle", Position{Line: 1, Col: 3}, true)
25
26 if !ok {
27 t.Fatal("Find() = false, want the search to wrap round")
28 }
29 if found.Start != (Position{}) {
30 t.Errorf("the match starts at %+v, want the top of the buffer", found.Start)
31 }
32}
33
34func TestFindIsCaseInsensitiveOnRequest(t *testing.T) {
35 b := NewFromString("Package Main")
36
37 if _, ok := b.Find("package", Position{}, true); ok {
38 t.Error("a case-sensitive search matched a different case")
39 }
40 found, ok := b.Find("package", Position{}, false)
41 if !ok {
42 t.Fatal("a case-insensitive search found nothing")
43 }
44 if found.Start != (Position{}) {
45 t.Errorf("the match starts at %+v", found.Start)
46 }
47}
48
49func TestFindColumnsAreCountedInRunes(t *testing.T) {
50 b := NewFromString("héllo wörld")
51
52 found, ok := b.Find("wörld", Position{}, true)
53
54 if !ok {
55 t.Fatal("Find() = false")
56 }
57 if found.Start.Col != 6 {
58 t.Errorf("the match starts at column %d, want 6 counted in runes", found.Start.Col)
59 }
60 if found.End.Col != 11 {
61 t.Errorf("the match ends at column %d, want 11", found.End.Col)
62 }
63}
64
65func TestFindNothing(t *testing.T) {
66 b := NewFromString("abc")
67
68 if _, ok := b.Find("xyz", Position{}, true); ok {
69 t.Error("Find() = true for a needle that is not there")
70 }
71 if _, ok := b.Find("", Position{}, true); ok {
72 t.Error("Find() = true for an empty needle")
73 }
74 if _, ok := b.FindPrevious("", Position{}, true); ok {
75 t.Error("FindPrevious() = true for an empty needle")
76 }
77}
78
79func TestFindPreviousMovesBackwards(t *testing.T) {
80 b := NewFromString("func one()\nfunc two()\nfunc three()")
81
82 found, ok := b.FindPrevious("func", Position{Line: 2, Col: 0}, true)
83
84 if !ok {
85 t.Fatal("FindPrevious() = false, want a match")
86 }
87 if found.Start != (Position{Line: 1, Col: 0}) {
88 t.Errorf("the match starts at %+v, want line 1", found.Start)
89 }
90}
91
92func TestFindPreviousWrapsRoundToTheEnd(t *testing.T) {
93 b := NewFromString("first\nlast target")
94
95 found, ok := b.FindPrevious("target", Position{}, true)
96
97 if !ok {
98 t.Fatal("FindPrevious() = false, want the search to wrap round")
99 }
100 if found.Start.Line != 1 {
101 t.Errorf("the match is on line %d, want the last line", found.Start.Line)
102 }
103}
104
105func TestFindPreviousPicksTheLastMatchOnALine(t *testing.T) {
106 b := NewFromString("x x x")
107
108 found, ok := b.FindPrevious("x", Position{Line: 0, Col: 5}, true)
109
110 if !ok {
111 t.Fatal("FindPrevious() = false")
112 }
113 if found.Start.Col != 4 {
114 t.Errorf("the match is at column %d, want the last one before the cursor", found.Start.Col)
115 }
116}
117
118func TestFindFromAPositionPastTheEndOfALine(t *testing.T) {
119 b := NewFromString("ab\ntarget")
120
121 found, ok := b.Find("target", Position{Line: 0, Col: 99}, true)
122
123 if !ok {
124 t.Fatal("Find() = false")
125 }
126 if found.Start.Line != 1 {
127 t.Errorf("the match is on line %d, want line 1", found.Start.Line)
128 }
129}