Gim/internal/editor/integration_operator_delete_test.go
Hayden Hargreaves 77374ba447 feat: implemented 'dd' and other 'd' for visual mode.
Tested 'dd' but not visual mode.
2026-02-11 17:56:06 -07:00

200 lines
5.4 KiB
Go

package editor
import (
"testing"
"git.gophernest.net/azpect/TextEditor/internal/action"
)
func TestDeleteLine(t *testing.T) {
t.Run("test 'dd' deletes first line", func(t *testing.T) {
lines := []string{"hello", "world"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 1 {
t.Errorf("LineCount() = %d, want '1'", m.LineCount())
}
if m.CursorX() != 0 {
t.Errorf("CursorX() = %d, want '0'", m.CursorX())
}
if m.CursorY() != 0 {
t.Errorf("CursorY() = %d, want '0'", m.CursorY())
}
if m.Line(0) != "world" {
t.Errorf("Line(0) = %s, want 'world'", m.Line(0))
}
})
t.Run("test 'dd' deletes middle line", func(t *testing.T) {
lines := []string{"hello", "world", "testing"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 1})
sendKeys(tm, "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 2 {
t.Errorf("LineCount() = %d, want '2'", m.LineCount())
}
if m.CursorX() != 0 {
t.Errorf("CursorX() = %d, want '0'", m.CursorX())
}
if m.CursorY() != 1 {
t.Errorf("CursorY() = %d, want '1'", m.CursorY())
}
if m.Line(0) != "hello" {
t.Errorf("Line(0) = %s, want 'hello'", m.Line(0))
}
if m.Line(1) != "testing" {
t.Errorf("Line(1) = %s, want 'testing'", m.Line(1))
}
})
t.Run("test 'dd' deletes last line", func(t *testing.T) {
lines := []string{"hello", "world"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 1})
sendKeys(tm, "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 1 {
t.Errorf("LineCount() = %d, want '1'", m.LineCount())
}
if m.CursorX() != 0 {
t.Errorf("CursorX() = %d, want '0'", m.CursorX())
}
if m.CursorY() != 0 {
t.Errorf("CursorY() = %d, want '0'", m.CursorY())
}
if m.Line(0) != "hello" {
t.Errorf("Line(0) = %s, want 'hello'", m.Line(0))
}
})
t.Run("test 'dd' deletes line and preserves column", func(t *testing.T) {
lines := []string{"hello", "world"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 3, Line: 0})
sendKeys(tm, "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 1 {
t.Errorf("LineCount() = %d, want '1'", m.LineCount())
}
if m.CursorX() != 3 {
t.Errorf("CursorX() = %d, want '3'", m.CursorX())
}
if m.CursorY() != 0 {
t.Errorf("CursorY() = %d, want '0'", m.CursorY())
}
if m.Line(0) != "world" {
t.Errorf("Line(0) = %s, want 'world'", m.Line(0))
}
})
t.Run("test '3dd' deletes three lines", func(t *testing.T) {
lines := []string{"hello", "world", "testing", "line", "another line"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 1})
sendKeys(tm, "3", "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 2 {
t.Errorf("LineCount() = %d, want '2'", m.LineCount())
}
if m.CursorX() != 0 {
t.Errorf("CursorX() = %d, want '0'", m.CursorX())
}
if m.CursorY() != 1 {
t.Errorf("CursorY() = %d, want '1'", m.CursorY())
}
if m.Line(0) != "hello" {
t.Errorf("Line(0) = %s, want 'hello'", m.Line(0))
}
if m.Line(1) != "another line" {
t.Errorf("Line(1) = %s, want 'another line'", m.Line(1))
}
})
t.Run("test 'dd' deletes only line and preserves content", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 1 {
t.Errorf("LineCount() = %d, want '1'", m.LineCount())
}
if m.CursorX() != 0 {
t.Errorf("CursorX() = %d, want '0'", m.CursorX())
}
if m.CursorY() != 0 {
t.Errorf("CursorY() = %d, want '0'", m.CursorY())
}
if m.Line(0) != "" {
t.Errorf("Line(0) = %s, want ''", m.Line(0))
}
})
t.Run("test 'dd' with no lines preserves content", func(t *testing.T) {
lines := []string{""}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 1 {
t.Errorf("LineCount() = %d, want '1'", m.LineCount())
}
if m.CursorX() != 0 {
t.Errorf("CursorX() = %d, want '0'", m.CursorX())
}
if m.CursorY() != 0 {
t.Errorf("CursorY() = %d, want '0'", m.CursorY())
}
if m.Line(0) != "" {
t.Errorf("Line(0) = %s, want ''", m.Line(0))
}
})
t.Run("test 'dd' clamps cursor when next line is shorter", func(t *testing.T) {
lines := []string{"hello world", "hi"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 7, Line: 0})
sendKeys(tm, "d", "d")
m := getFinalModel(t, tm)
if m.CursorX() != 2 {
t.Errorf("CursorX() = %d, want 2", m.CursorX())
}
})
t.Run("test '3dd' count exceeds remaining lines", func(t *testing.T) {
lines := []string{"hello", "world"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "3", "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 1 {
t.Errorf("LineCount() = %d, want 1", m.LineCount())
}
if m.Line(0) != "" {
t.Errorf("Line(0) = %q, want \"\"", m.Line(0))
}
})
t.Run("test '3dd' starting near end of file", func(t *testing.T) {
lines := []string{"hello", "world", "testing"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 1})
sendKeys(tm, "3", "d", "d")
m := getFinalModel(t, tm)
if m.LineCount() != 1 {
t.Errorf("LineCount() = %d, want 1", m.LineCount())
}
if m.Line(0) != "hello" {
t.Errorf("Line(0) = %q, want \"hello\"", m.Line(0))
}
if m.CursorY() != 0 {
t.Errorf("CursorY() = %d, want 0", m.CursorY())
}
})
}