|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/walles/moor/v2/internal/linemetadata" |
| 7 | + "github.com/walles/twin" |
| 8 | + "gotest.tools/v3/assert" |
| 9 | +) |
| 10 | + |
| 11 | +// Ref: https://github.com/walles/moor/issues/466 |
| 12 | +func TestPagerModeFilter_UpArrowShowsMostRecentSearchHistoryEntry(t *testing.T) { |
| 13 | + pager := createThreeLinesPager(t) |
| 14 | + pager.searchHistory.entries = []string{"b", "d"} |
| 15 | + |
| 16 | + filterMode := NewPagerModeFilter(pager, pager.scrollPosition) |
| 17 | + filterMode.onKey(twin.KeyUp) |
| 18 | + |
| 19 | + assert.Equal(t, "d", filterMode.inputBox.text) |
| 20 | +} |
| 21 | + |
| 22 | +func TestPagerModeFilter_DownArrowStepsBackTowardsNewestEntry(t *testing.T) { |
| 23 | + pager := createThreeLinesPager(t) |
| 24 | + pager.searchHistory.entries = []string{"b", "d"} |
| 25 | + |
| 26 | + filterMode := NewPagerModeFilter(pager, pager.scrollPosition) |
| 27 | + filterMode.onKey(twin.KeyUp) |
| 28 | + filterMode.onKey(twin.KeyUp) |
| 29 | + filterMode.onKey(twin.KeyDown) |
| 30 | + |
| 31 | + assert.Equal(t, "d", filterMode.inputBox.text) |
| 32 | +} |
| 33 | + |
| 34 | +func TestPagerModeFilter_EnterPersistsTypedTextToSearchHistory(t *testing.T) { |
| 35 | + pager := createThreeLinesPager(t) |
| 36 | + pager.searchHistory = &SearchHistory{} // No file backing, keep this test disk-free |
| 37 | + |
| 38 | + filterMode := NewPagerModeFilter(pager, pager.scrollPosition) |
| 39 | + filterMode.inputBox.setText("abc") |
| 40 | + filterMode.onKey(twin.KeyEnter) |
| 41 | + |
| 42 | + assert.DeepEqual(t, []string{"abc"}, pager.searchHistory.entries) |
| 43 | +} |
| 44 | + |
| 45 | +// Search and filter modes each get their own HistoryNavigator, but both wrap |
| 46 | +// the same *SearchHistory on the pager, so an entry committed from one mode |
| 47 | +// must be visible when navigating history from the other. |
| 48 | +// |
| 49 | +// Ref: https://github.com/walles/moor/issues/466 |
| 50 | +func TestPagerModeFilter_SharesSearchHistoryWithSearchMode(t *testing.T) { |
| 51 | + pager := createThreeLinesPager(t) |
| 52 | + pager.searchHistory = &SearchHistory{} // No file backing, keep this test disk-free |
| 53 | + |
| 54 | + filterMode := NewPagerModeFilter(pager, pager.scrollPosition) |
| 55 | + filterMode.inputBox.setText("from filter") |
| 56 | + filterMode.onKey(twin.KeyEnter) |
| 57 | + |
| 58 | + searchMode := NewPagerModeSearch(pager, SearchDirectionForward, pager.scrollPosition) |
| 59 | + searchMode.onKey(twin.KeyUp) |
| 60 | + |
| 61 | + assert.Equal(t, "from filter", searchMode.inputBox.text) |
| 62 | +} |
| 63 | + |
| 64 | +// Pressing ESC should return the view to wherever it was scrolled to before |
| 65 | +// filtering started, the same way search's ESC does, even though filtering |
| 66 | +// out non-matching lines may have moved the pager's scroll position while |
| 67 | +// typing. |
| 68 | +// |
| 69 | +// Ref: https://github.com/walles/moor/issues/466 |
| 70 | +func TestPagerModeFilter_EscapeRestoresScrollPositionFromBeforeFiltering(t *testing.T) { |
| 71 | + pager := createThreeLinesPager(t) |
| 72 | + pager.scrollPosition = NewScrollPositionFromIndex(linemetadata.IndexFromZeroBased(4), "before filtering") |
| 73 | + initialIndex := pager.lineIndex().Index() |
| 74 | + |
| 75 | + filterMode := NewPagerModeFilter(pager, pager.scrollPosition) |
| 76 | + filterMode.onRune('a') // Filters down to only the line containing "a" |
| 77 | + |
| 78 | + // This mirrors what a real redraw does, and confirms filtering really did |
| 79 | + // move the scroll position as a side effect. |
| 80 | + assert.Assert(t, pager.lineIndex().Index() != initialIndex, "Filtering should have moved the scroll position") |
| 81 | + |
| 82 | + filterMode.onKey(twin.KeyEscape) |
| 83 | + |
| 84 | + assert.Equal(t, initialIndex, pager.lineIndex().Index()) |
| 85 | +} |
| 86 | + |
| 87 | +// Ref: https://github.com/walles/moor/issues/466 |
| 88 | +func TestPagerModeFilter_PgDownPersistsTypedTextAndSwitchesToViewing(t *testing.T) { |
| 89 | + pager := createThreeLinesPager(t) |
| 90 | + pager.searchHistory = &SearchHistory{} // No file backing, keep this test disk-free |
| 91 | + |
| 92 | + filterMode := NewPagerModeFilter(pager, pager.scrollPosition) |
| 93 | + pager.mode = filterMode |
| 94 | + filterMode.inputBox.setText("abc") |
| 95 | + filterMode.onKey(twin.KeyPgDown) |
| 96 | + |
| 97 | + assert.DeepEqual(t, []string{"abc"}, pager.searchHistory.entries) |
| 98 | + assert.Equal(t, "Viewing", modeName(pager)) |
| 99 | +} |
0 commit comments