2022-09-22 21:45:13 +03:00
|
|
|
package m
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/walles/moar/twin"
|
2022-09-25 10:06:46 +03:00
|
|
|
"gotest.tools/v3/assert"
|
2022-09-22 21:45:13 +03:00
|
|
|
)
|
|
|
|
|
2024-01-09 15:52:59 +03:00
|
|
|
func modeName(pager *Pager) string {
|
|
|
|
switch pager.mode.(type) {
|
|
|
|
case PagerModeViewing:
|
|
|
|
return "Viewing"
|
|
|
|
case PagerModeNotFound:
|
|
|
|
return "NotFound"
|
|
|
|
case PagerModeSearch:
|
|
|
|
return "Search"
|
2024-01-09 19:28:49 +03:00
|
|
|
case *PagerModeGotoLine:
|
2024-01-09 15:52:59 +03:00
|
|
|
return "GotoLine"
|
|
|
|
default:
|
|
|
|
panic("Unknown pager mode")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 21:45:13 +03:00
|
|
|
// Create a pager with three screen lines reading from a six lines stream
|
|
|
|
func createThreeLinesPager(t *testing.T) *Pager {
|
2022-09-24 09:46:15 +03:00
|
|
|
reader := NewReaderFromText("", "a\nb\nc\nd\ne\nf\n")
|
2022-09-22 21:45:13 +03:00
|
|
|
|
|
|
|
screen := twin.NewFakeScreen(20, 3)
|
|
|
|
pager := NewPager(reader)
|
|
|
|
|
|
|
|
pager.screen = screen
|
|
|
|
|
2024-01-09 15:52:59 +03:00
|
|
|
assert.Equal(t, "Viewing", modeName(pager), "Initial pager state")
|
2022-09-22 21:45:13 +03:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2024-01-09 15:52:59 +03:00
|
|
|
assert.Equal(t, "NotFound", modeName(pager))
|
2022-09-22 21:45:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2024-01-09 15:52:59 +03:00
|
|
|
assert.Equal(t, "NotFound", modeName(pager))
|
2022-09-22 21:45:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2024-01-09 15:52:59 +03:00
|
|
|
assert.Equal(t, "NotFound", modeName(pager))
|
2022-09-22 21:45:13 +03:00
|
|
|
|
|
|
|
// Scroll to the next search hit, this should wrap the search and take us to
|
|
|
|
// the top
|
|
|
|
pager.scrollToNextSearchHit()
|
2024-01-09 15:52:59 +03:00
|
|
|
assert.Equal(t, "Viewing", modeName(pager))
|
2024-01-07 10:49:44 +03:00
|
|
|
assert.Assert(t, pager.lineNumber().IsZero())
|
2022-09-22 21:45:13 +03:00
|
|
|
}
|
2022-09-22 21:49:15 +03:00
|
|
|
|
|
|
|
func TestScrollToNextSearchHit_WrapAfterFound(t *testing.T) {
|
|
|
|
// Create a pager scrolled to the last line
|
|
|
|
pager := createThreeLinesPager(t)
|
|
|
|
pager.scrollToEnd()
|
|
|
|
|
|
|
|
// Search for "f", it's on the last line (ref createThreeLinesPager())
|
|
|
|
pager.searchString = "f"
|
|
|
|
pager.searchPattern = toPattern(pager.searchString)
|
|
|
|
|
|
|
|
// Scroll to the next search hit, this should take us into _NotFound
|
|
|
|
pager.scrollToNextSearchHit()
|
2024-01-09 15:52:59 +03:00
|
|
|
assert.Equal(t, "NotFound", modeName(pager))
|
2022-09-22 21:49:15 +03:00
|
|
|
|
|
|
|
// Scroll to the next search hit, this should wrap the search and take us
|
|
|
|
// back to the bottom again
|
|
|
|
pager.scrollToNextSearchHit()
|
2024-01-09 15:52:59 +03:00
|
|
|
assert.Equal(t, "Viewing", modeName(pager))
|
2024-01-07 10:56:02 +03:00
|
|
|
assert.Equal(t, 5, pager.lineNumber().AsOneBased())
|
2022-09-22 21:49:15 +03:00
|
|
|
}
|
2023-09-14 21:05:04 +03:00
|
|
|
|
|
|
|
// Ref: https://github.com/walles/moar/issues/152
|
|
|
|
func Test152(t *testing.T) {
|
|
|
|
// Show a pager on a five lines terminal
|
|
|
|
reader := NewReaderFromText("", "a\nab\nabc\nabcd\nabcde\nabcdef\n")
|
|
|
|
screen := twin.NewFakeScreen(20, 5)
|
|
|
|
pager := NewPager(reader)
|
|
|
|
pager.screen = screen
|
2024-01-09 15:52:59 +03:00
|
|
|
assert.Equal(t, "Viewing", modeName(pager), "Initial pager state")
|
2023-09-14 21:05:04 +03:00
|
|
|
|
|
|
|
// Search for the first not-visible hit
|
|
|
|
pager.searchString = "abcde"
|
2024-01-09 15:52:59 +03:00
|
|
|
searchMode := PagerModeSearch{pager: pager}
|
|
|
|
pager.mode = searchMode
|
2023-09-14 21:05:04 +03:00
|
|
|
|
|
|
|
// Scroll to the next search hit
|
2024-01-09 15:52:59 +03:00
|
|
|
searchMode.updateSearchPattern()
|
2023-09-14 21:05:04 +03:00
|
|
|
|
2024-01-09 16:40:40 +03:00
|
|
|
assert.Equal(t, "Search", modeName(pager))
|
2024-01-07 10:56:02 +03:00
|
|
|
assert.Equal(t, 3, pager.lineNumber().AsOneBased())
|
2023-09-14 21:05:04 +03:00
|
|
|
}
|