Gim/internal/editor/integration_delete_test.go
2026-02-18 17:25:38 -07:00

178 lines
4.7 KiB
Go

package editor
import (
"testing"
"git.gophernest.net/azpect/TextEditor/internal/action"
)
func TestDeleteChar(t *testing.T) {
t.Run("test 'x' deletes character under cursor", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "x")
m := getFinalModel(t, tm)
if m.lines[0] != "ello" {
t.Errorf("lines[0] = %q, want 'ello'", m.lines[0])
}
})
t.Run("test 'x' in middle of line", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 2, Line: 0})
sendKeys(tm, "x")
m := getFinalModel(t, tm)
if m.lines[0] != "helo" {
t.Errorf("lines[0] = %q, want 'helo'", m.lines[0])
}
})
t.Run("test 'x' at end of line", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 4, Line: 0})
sendKeys(tm, "x")
m := getFinalModel(t, tm)
if m.lines[0] != "hell" {
t.Errorf("lines[0] = %q, want 'hell'", m.lines[0])
}
})
t.Run("test 'xx' deletes two characters", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "x", "x")
m := getFinalModel(t, tm)
if m.lines[0] != "llo" {
t.Errorf("lines[0] = %q, want 'llo'", m.lines[0])
}
})
}
func TestDeleteCharWithCount(t *testing.T) {
t.Run("test '3x' deletes three characters", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "3", "x")
m := getFinalModel(t, tm)
if m.lines[0] != "lo" {
t.Errorf("lines[0] = %q, want 'lo'", m.lines[0])
}
})
t.Run("test '10x' with overflow", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "1", "0", "x")
m := getFinalModel(t, tm)
if m.lines[0] != "" {
t.Errorf("lines[0] = %q, want ''", m.lines[0])
}
})
t.Run("test '2x' from middle", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 1, Line: 0})
sendKeys(tm, "2", "x")
m := getFinalModel(t, tm)
if m.lines[0] != "hlo" {
t.Errorf("lines[0] = %q, want 'hlo'", m.lines[0])
}
})
}
func TestDeleteToEndOfLine(t *testing.T) {
t.Run("test 'D' deletes to end of line", func(t *testing.T) {
lines := []string{"hello world"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 5, Line: 0})
sendKeys(tm, "D")
m := getFinalModel(t, tm)
if m.Line(0) != "hello" {
t.Errorf("Line(0) = %q, want 'hello'", m.Line(0))
}
})
t.Run("test 'D' from start of line deletes entire content", func(t *testing.T) {
lines := []string{"hello world"}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "D")
m := getFinalModel(t, tm)
if m.Line(0) != "" {
t.Errorf("Line(0) = %q, want ''", m.Line(0))
}
})
t.Run("test 'D' at last character", func(t *testing.T) {
lines := []string{"hello"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 4, Line: 0})
sendKeys(tm, "D")
m := getFinalModel(t, tm)
if m.Line(0) != "hell" {
t.Errorf("Line(0) = %q, want 'hell'", m.Line(0))
}
})
t.Run("test 'D' cursor position after delete", func(t *testing.T) {
lines := []string{"hello world"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 5, Line: 0})
sendKeys(tm, "D")
m := getFinalModel(t, tm)
// Cursor should move to last character of remaining text
if m.CursorX() != 4 {
t.Errorf("CursorX() = %d, want 4", m.CursorX())
}
})
t.Run("test 'D' deletes nothing on blank line", func(t *testing.T) {
lines := []string{""}
tm := newTestModelWithLines(t, lines)
sendKeys(tm, "D")
m := getFinalModel(t, tm)
if m.Line(0) != "" {
t.Errorf("Line(0) = %q, want ''", m.Line(0))
}
})
t.Run("test 'D' with count deletes following lines", func(t *testing.T) {
lines := []string{"hello", "world", "hi", "mom"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 2, Line: 0})
sendKeys(tm, "2", "D")
m := getFinalModel(t, tm)
if m.LineCount() != 3 {
t.Errorf("LineCount() = %q, want '3'", m.LineCount())
}
if m.Line(0) != "he" {
t.Errorf("Line(0) = %q, want 'he'", m.Line(0))
}
if m.Line(1) != "hi" {
t.Errorf("Line(1) = %q, want 'hi'", m.Line(1))
}
})
t.Run("test 'D' with count deletes following lines with overflow", func(t *testing.T) {
lines := []string{"hello", "world", "hi", "mom"}
tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 2, Line: 0})
sendKeys(tm, "8", "D")
m := getFinalModel(t, tm)
if m.LineCount() != 1 {
t.Errorf("LineCount() = %q, want '1'", m.LineCount())
}
if m.Line(0) != "he" {
t.Errorf("Line(0) = %q, want 'he'", m.Line(0))
}
})
}