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) } }