From 37ba30463cf2d585fd017b077b9e2e391d6dad9a Mon Sep 17 00:00:00 2001 From: Caleb Foust Date: Tue, 14 Nov 2023 17:57:57 -0500 Subject: [PATCH] feat: scroll and clear tests --- pkg/sessions/search/search_test.go | 171 +++++++++++++++++++++++++++++ pkg/sessions/simulator.go | 6 - 2 files changed, 171 insertions(+), 6 deletions(-) diff --git a/pkg/sessions/search/search_test.go b/pkg/sessions/search/search_test.go index f8d07556..22aa16b6 100644 --- a/pkg/sessions/search/search_test.go +++ b/pkg/sessions/search/search_test.go @@ -3,10 +3,12 @@ package search import ( "testing" + "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom" "github.com/cfoust/cy/pkg/sessions" "github.com/stretchr/testify/require" + "github.com/xo/terminfo" ) func TestGetPartial(t *testing.T) { @@ -23,6 +25,11 @@ func TestGetPartial(t *testing.T) { require.Equal(t, "([ads]|[1-3])", re.String()) } +var TEST_SIZE = geom.Size{ + R: 4, + C: 10, +} + func TestBasic(t *testing.T) { sim := sessions.NewSimulator(). Add( @@ -64,3 +71,167 @@ func TestBasic(t *testing.T) { }, }, results[0]) } + +func TestClearScreen(t *testing.T) { + sim := sessions.NewSimulator(). + Add( + TEST_SIZE, + emu.LineFeedMode, + "foo\n", + "bar\n", + "baz", + ). + Term(terminfo.ClearScreen). + Add("test") + + results, err := Search(sim.Events(), "foo", nil) + require.NoError(t, err) + require.Equal(t, 1, len(results)) + require.Equal(t, SearchResult{ + Begin: Address{ + Index: 2, + Offset: 2, + }, + End: Address{ + Index: 5, + Offset: 6, + }, + Appearances: []Appearance{ + { + Begin: Address{ + Index: 2, + Offset: 2, + }, + End: Address{ + Index: 5, + Offset: 6, + }, + Selection: Selection{ + From: geom.Vec2{ + C: 0, + }, + To: geom.Vec2{ + C: 2, + }, + }, + }, + }, + }, results[0]) +} + +func TestClearLine(t *testing.T) { + sim := sessions.NewSimulator(). + Add( + TEST_SIZE, + emu.LineFeedMode, + "foo\n", + "bar\n", + "baz", + ). + Term(terminfo.CursorAddress, 0, 0). + Term(terminfo.DeleteLine) + + results, err := Search(sim.Events(), "foo", nil) + require.NoError(t, err) + require.Equal(t, 1, len(results)) + require.Equal(t, SearchResult{ + Begin: Address{ + Index: 2, + Offset: 2, + }, + End: Address{ + Index: 6, + Offset: 2, + }, + Appearances: []Appearance{ + { + Begin: Address{ + Index: 2, + Offset: 2, + }, + End: Address{ + Index: 6, + Offset: 2, + }, + Selection: Selection{ + From: geom.Vec2{ + C: 0, + }, + To: geom.Vec2{ + C: 2, + }, + }, + }, + }, + }, results[0]) +} + +func TestScroll(t *testing.T) { + sim := sessions.NewSimulator(). + Add( + TEST_SIZE, + emu.LineFeedMode, + "foo\n", + "bar\n", // 3 + "baz\n", + "\n", + "\n", // 6 + "test", + ) + + results, err := Search(sim.Events(), "bar", nil) + require.NoError(t, err) + require.Equal(t, 1, len(results)) + require.Equal(t, SearchResult{ + Begin: Address{ + Index: 3, + Offset: 2, + }, + End: Address{ + Index: 7, + Offset: 3, + }, + Appearances: []Appearance{ + { + Begin: Address{ + Index: 3, + Offset: 2, + }, + End: Address{ + Index: 5, + Offset: 0, + }, + Selection: Selection{ + From: geom.Vec2{ + R: 1, + C: 0, + }, + To: geom.Vec2{ + R: 1, + C: 2, + }, + }, + }, + { + Begin: Address{ + Index: 5, + Offset: 0, + }, + End: Address{ + Index: 7, + Offset: 3, + }, + Selection: Selection{ + From: geom.Vec2{ + R: 0, + C: 0, + }, + To: geom.Vec2{ + R: 0, + C: 2, + }, + }, + }, + }, + }, results[0]) +} diff --git a/pkg/sessions/simulator.go b/pkg/sessions/simulator.go index 0317adde..1b19ff15 100644 --- a/pkg/sessions/simulator.go +++ b/pkg/sessions/simulator.go @@ -3,7 +3,6 @@ package sessions import ( "time" - "github.com/cfoust/cy/pkg/emu" "github.com/cfoust/cy/pkg/geom" P "github.com/cfoust/cy/pkg/io/protocol" @@ -92,11 +91,6 @@ func (s *Simulator) Add(events ...interface{}) *Simulator { return s } -// LineFeed causes \n to return to the start of the next line. -func (s *Simulator) LineFeed() { - s.Add(emu.LineFeedMode) -} - func (s *Simulator) Events() []Event { return s.events }