| 🛟 Updated. 28d5985 k33g 17h ago | 1 | package buffer |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func 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 | |
| 21 | func 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 | |
| 34 | func 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 | |
| 49 | func 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 | |
| 65 | func 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 | |
| 79 | func 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 | |
| 92 | func 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 | |
| 105 | func 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 | |
| 118 | func 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 | } |