From 8d9c2be69f9ba32d0db470dcaaf5f1582c391dda Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Thu, 22 Sep 2022 20:45:13 +0200 Subject: [PATCH] Add initial search tests --- m/search_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 m/search_test.go diff --git a/m/search_test.go b/m/search_test.go new file mode 100644 index 0000000..9cc53e1 --- /dev/null +++ b/m/search_test.go @@ -0,0 +1,86 @@ +package m + +import ( + "strings" + "testing" + + "github.com/walles/moar/twin" +) + +// Create a pager with three screen lines reading from a six lines stream +func createThreeLinesPager(t *testing.T) *Pager { + reader := NewReaderFromStream("", strings.NewReader("a\nb\nc\nd\ne\nf\n")) + if err := reader._wait(); err != nil { + panic(err) + } + + screen := twin.NewFakeScreen(20, 3) + pager := NewPager(reader) + + pager.screen = screen + + if pager.mode != _Viewing { + t.Errorf("Expected initial pager state to be %v but got %v", _Viewing, pager.mode) + } + + return pager +} + +func TestScrollToNextSearchHit_StartAtBottom(t *testing.T) { + // Create a pager scrolled to the last line + pager := createThreeLinesPager(t) + pager.scrollToEnd() + + // Set the search to something that doesn't exist in this pager + pager.searchString = "xxx" + pager.searchPattern = toPattern(pager.searchString) + + // Scroll to the next search hit + pager.scrollToNextSearchHit() + + if pager.mode != _NotFound { + t.Errorf("Expected state %v but got %v", _NotFound, pager.mode) + } +} + +func TestScrollToNextSearchHit_StartAtTop(t *testing.T) { + // Create a pager scrolled to the first line + pager := createThreeLinesPager(t) + + // Set the search to something that doesn't exist in this pager + pager.searchString = "xxx" + pager.searchPattern = toPattern(pager.searchString) + + // Scroll to the next search hit + pager.scrollToNextSearchHit() + + if pager.mode != _NotFound { + t.Errorf("Expected state %v but got %v", _NotFound, pager.mode) + } +} + +func TestScrollToNextSearchHit_WrapAfterNotFound(t *testing.T) { + // Create a pager scrolled to the last line + pager := createThreeLinesPager(t) + pager.scrollToEnd() + + // Search for "a", it's on the first line (ref createThreeLinesPager()) + pager.searchString = "a" + pager.searchPattern = toPattern(pager.searchString) + + // Scroll to the next search hit, this should take us into _NotFound + pager.scrollToNextSearchHit() + if pager.mode != _NotFound { + t.Errorf("Expected state %v but got %v", _NotFound, pager.mode) + } + + // Scroll to the next search hit, this should wrap the search and take us to + // the top + pager.scrollToNextSearchHit() + if pager.mode != _Viewing { + t.Errorf("Expected state %v but got %v", _Viewing, pager.mode) + } + if pager.lineNumberOneBased() != 1 { + t.Errorf("Expected line number %v but got %v", 1, pager.lineNumberOneBased()) + } +}