package editor import ( "testing" "git.gophernest.net/azpect/TextEditor/internal/action" ) func TestPasteLinewiseBasic(t *testing.T) { t.Run("p pastes single line after cursor line", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.LineCount() != 3 { t.Errorf("LineCount() = %d, want 3", m.LineCount()) } if m.Line(0) != "line 1" { t.Errorf("Line(0) = %q, want 'line 1'", m.Line(0)) } if m.Line(1) != "inserted" { t.Errorf("Line(1) = %q, want 'inserted'", m.Line(1)) } if m.Line(2) != "line 2" { t.Errorf("Line(2) = %q, want 'line 2'", m.Line(2)) } }) t.Run("p moves cursor to first pasted line", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2", "line 3"}), WithCursorPos(action.Position{Line: 0, Col: 5}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.CursorY() != 1 { t.Errorf("CursorY() = %d, want 1", m.CursorY()) } }) t.Run("p from middle of file", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2", "line 3"}), WithCursorPos(action.Position{Line: 1, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.LineCount() != 4 { t.Errorf("LineCount() = %d, want 4", m.LineCount()) } if m.Line(2) != "inserted" { t.Errorf("Line(2) = %q, want 'inserted'", m.Line(2)) } if m.CursorY() != 2 { t.Errorf("CursorY() = %d, want 2", m.CursorY()) } }) t.Run("p at end of file", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 1, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.LineCount() != 3 { t.Errorf("LineCount() = %d, want 3", m.LineCount()) } if m.Line(2) != "inserted" { t.Errorf("Line(2) = %q, want 'inserted'", m.Line(2)) } }) } func TestPasteLinewiseMultipleLines(t *testing.T) { t.Run("p pastes multiple lines in correct order", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"first", "second", "third"}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.LineCount() != 5 { t.Errorf("LineCount() = %d, want 5", m.LineCount()) } if m.Line(0) != "line 1" { t.Errorf("Line(0) = %q, want 'line 1'", m.Line(0)) } if m.Line(1) != "first" { t.Errorf("Line(1) = %q, want 'first'", m.Line(1)) } if m.Line(2) != "second" { t.Errorf("Line(2) = %q, want 'second'", m.Line(2)) } if m.Line(3) != "third" { t.Errorf("Line(3) = %q, want 'third'", m.Line(3)) } if m.Line(4) != "line 2" { t.Errorf("Line(4) = %q, want 'line 2'", m.Line(4)) } }) t.Run("p with multiple lines moves cursor to first pasted line", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"first", "second"}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.CursorY() != 1 { t.Errorf("CursorY() = %d, want 1", m.CursorY()) } }) } func TestPasteLinewiseWithCount(t *testing.T) { t.Run("2p pastes content twice", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "2", "p") m := getFinalModel(t, tm) if m.LineCount() != 4 { t.Errorf("LineCount() = %d, want 4", m.LineCount()) } // Both "inserted" lines should appear after line 1 if m.Line(1) != "inserted" { t.Errorf("Line(1) = %q, want 'inserted'", m.Line(1)) } if m.Line(2) != "inserted" { t.Errorf("Line(2) = %q, want 'inserted'", m.Line(2)) } if m.Line(3) != "line 2" { t.Errorf("Line(3) = %q, want 'line 2'", m.Line(3)) } }) t.Run("3p pastes content three times", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"original"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"pasted"}), ) sendKeys(tm, "3", "p") m := getFinalModel(t, tm) if m.LineCount() != 4 { t.Errorf("LineCount() = %d, want 4", m.LineCount()) } for i := 1; i <= 3; i++ { if m.Line(i) != "pasted" { t.Errorf("Line(%d) = %q, want 'pasted'", i, m.Line(i)) } } }) t.Run("2p with multiple lines pastes all lines twice in order", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"original"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"first", "second"}), ) sendKeys(tm, "2", "p") m := getFinalModel(t, tm) // Should be: original, first, second, first, second if m.LineCount() != 5 { t.Errorf("LineCount() = %d, want 5", m.LineCount()) } if m.Line(0) != "original" { t.Errorf("Line(0) = %q, want 'original'", m.Line(0)) } if m.Line(1) != "first" { t.Errorf("Line(1) = %q, want 'first'", m.Line(1)) } if m.Line(2) != "second" { t.Errorf("Line(2) = %q, want 'second'", m.Line(2)) } if m.Line(3) != "first" { t.Errorf("Line(3) = %q, want 'first'", m.Line(3)) } if m.Line(4) != "second" { t.Errorf("Line(4) = %q, want 'second'", m.Line(4)) } }) t.Run("count paste moves cursor to first pasted line", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "3", "p") m := getFinalModel(t, tm) if m.CursorY() != 1 { t.Errorf("CursorY() = %d, want 1", m.CursorY()) } }) } // Tests for P (paste before) func TestPasteBeforeLinewiseBasic(t *testing.T) { t.Run("P pastes single line before cursor line", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 1, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.LineCount() != 3 { t.Errorf("LineCount() = %d, want 3", m.LineCount()) } if m.Line(0) != "line 1" { t.Errorf("Line(0) = %q, want 'line 1'", m.Line(0)) } if m.Line(1) != "inserted" { t.Errorf("Line(1) = %q, want 'inserted'", m.Line(1)) } if m.Line(2) != "line 2" { t.Errorf("Line(2) = %q, want 'line 2'", m.Line(2)) } }) t.Run("P moves cursor to first pasted line", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2", "line 3"}), WithCursorPos(action.Position{Line: 1, Col: 5}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.CursorY() != 1 { t.Errorf("CursorY() = %d, want 1", m.CursorY()) } }) t.Run("P at first line pastes at very top", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.LineCount() != 3 { t.Errorf("LineCount() = %d, want 3", m.LineCount()) } if m.Line(0) != "inserted" { t.Errorf("Line(0) = %q, want 'inserted'", m.Line(0)) } if m.Line(1) != "line 1" { t.Errorf("Line(1) = %q, want 'line 1'", m.Line(1)) } if m.CursorY() != 0 { t.Errorf("CursorY() = %d, want 0", m.CursorY()) } }) t.Run("P from middle of file", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2", "line 3"}), WithCursorPos(action.Position{Line: 1, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.LineCount() != 4 { t.Errorf("LineCount() = %d, want 4", m.LineCount()) } if m.Line(1) != "inserted" { t.Errorf("Line(1) = %q, want 'inserted'", m.Line(1)) } if m.Line(2) != "line 2" { t.Errorf("Line(2) = %q, want 'line 2'", m.Line(2)) } }) } func TestPasteBeforeLinewiseMultipleLines(t *testing.T) { t.Run("P pastes multiple lines in correct order", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 1, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"first", "second", "third"}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.LineCount() != 5 { t.Errorf("LineCount() = %d, want 5", m.LineCount()) } if m.Line(0) != "line 1" { t.Errorf("Line(0) = %q, want 'line 1'", m.Line(0)) } if m.Line(1) != "first" { t.Errorf("Line(1) = %q, want 'first'", m.Line(1)) } if m.Line(2) != "second" { t.Errorf("Line(2) = %q, want 'second'", m.Line(2)) } if m.Line(3) != "third" { t.Errorf("Line(3) = %q, want 'third'", m.Line(3)) } if m.Line(4) != "line 2" { t.Errorf("Line(4) = %q, want 'line 2'", m.Line(4)) } }) t.Run("P with multiple lines moves cursor to first pasted line", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 1, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"first", "second"}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.CursorY() != 1 { t.Errorf("CursorY() = %d, want 1", m.CursorY()) } }) } func TestPasteBeforeLinewiseWithCount(t *testing.T) { t.Run("2P pastes content twice", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 1, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "2", "P") m := getFinalModel(t, tm) if m.LineCount() != 4 { t.Errorf("LineCount() = %d, want 4", m.LineCount()) } // Both "inserted" lines should appear before line 2 if m.Line(1) != "inserted" { t.Errorf("Line(1) = %q, want 'inserted'", m.Line(1)) } if m.Line(2) != "inserted" { t.Errorf("Line(2) = %q, want 'inserted'", m.Line(2)) } if m.Line(3) != "line 2" { t.Errorf("Line(3) = %q, want 'line 2'", m.Line(3)) } }) t.Run("3P pastes content three times", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"original"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"pasted"}), ) sendKeys(tm, "3", "P") m := getFinalModel(t, tm) if m.LineCount() != 4 { t.Errorf("LineCount() = %d, want 4", m.LineCount()) } for i := 0; i < 3; i++ { if m.Line(i) != "pasted" { t.Errorf("Line(%d) = %q, want 'pasted'", i, m.Line(i)) } } if m.Line(3) != "original" { t.Errorf("Line(3) = %q, want 'original'", m.Line(3)) } }) t.Run("2P with multiple lines pastes all lines twice in order", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"original"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"first", "second"}), ) sendKeys(tm, "2", "P") m := getFinalModel(t, tm) // Should be: first, second, first, second, original if m.LineCount() != 5 { t.Errorf("LineCount() = %d, want 5", m.LineCount()) } if m.Line(0) != "first" { t.Errorf("Line(0) = %q, want 'first'", m.Line(0)) } if m.Line(1) != "second" { t.Errorf("Line(1) = %q, want 'second'", m.Line(1)) } if m.Line(2) != "first" { t.Errorf("Line(2) = %q, want 'first'", m.Line(2)) } if m.Line(3) != "second" { t.Errorf("Line(3) = %q, want 'second'", m.Line(3)) } if m.Line(4) != "original" { t.Errorf("Line(4) = %q, want 'original'", m.Line(4)) } }) t.Run("count paste before moves cursor to first pasted line", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 1, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "3", "P") m := getFinalModel(t, tm) if m.CursorY() != 1 { t.Errorf("CursorY() = %d, want 1", m.CursorY()) } }) } func TestPasteBeforeLinewiseEdgeCases(t *testing.T) { t.Run("P on single line buffer", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"only line"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.LineCount() != 2 { t.Errorf("LineCount() = %d, want 2", m.LineCount()) } if m.Line(0) != "inserted" { t.Errorf("Line(0) = %q, want 'inserted'", m.Line(0)) } if m.Line(1) != "only line" { t.Errorf("Line(1) = %q, want 'only line'", m.Line(1)) } }) t.Run("P with empty register content does nothing", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.LineCount() != 2 { t.Errorf("LineCount() = %d, want 2", m.LineCount()) } }) t.Run("P preserves indentation in pasted lines", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{" indented", "\ttabbed"}), ) sendKeys(tm, "P") m := getFinalModel(t, tm) if m.Line(0) != " indented" { t.Errorf("Line(0) = %q, want ' indented'", m.Line(0)) } if m.Line(1) != "\ttabbed" { t.Errorf("Line(1) = %q, want '\\ttabbed'", m.Line(1)) } }) t.Run("P with large count", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"original"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"x"}), ) sendKeys(tm, "1", "0", "P") // 10P m := getFinalModel(t, tm) if m.LineCount() != 11 { t.Errorf("LineCount() = %d, want 11", m.LineCount()) } // Original should be at the end if m.Line(10) != "original" { t.Errorf("Line(10) = %q, want 'original'", m.Line(10)) } }) } func TestPasteLinewiseEdgeCases(t *testing.T) { t.Run("p on single line buffer", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"only line"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"inserted"}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.LineCount() != 2 { t.Errorf("LineCount() = %d, want 2", m.LineCount()) } if m.Line(0) != "only line" { t.Errorf("Line(0) = %q, want 'only line'", m.Line(0)) } if m.Line(1) != "inserted" { t.Errorf("Line(1) = %q, want 'inserted'", m.Line(1)) } }) t.Run("p with empty register content does nothing", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1", "line 2"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.LineCount() != 2 { t.Errorf("LineCount() = %d, want 2", m.LineCount()) } }) t.Run("p preserves indentation in pasted lines", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"line 1"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{" indented", "\ttabbed"}), ) sendKeys(tm, "p") m := getFinalModel(t, tm) if m.Line(1) != " indented" { t.Errorf("Line(1) = %q, want ' indented'", m.Line(1)) } if m.Line(2) != "\ttabbed" { t.Errorf("Line(2) = %q, want '\\ttabbed'", m.Line(2)) } }) t.Run("p with large count", func(t *testing.T) { tm := newTestModel(t, WithLines([]string{"original"}), WithCursorPos(action.Position{Line: 0, Col: 0}), WithRegister('"', action.LinewiseRegister, []string{"x"}), ) sendKeys(tm, "1", "0", "p") // 10p m := getFinalModel(t, tm) if m.LineCount() != 11 { t.Errorf("LineCount() = %d, want 11", m.LineCount()) } }) }