package editor import ( "testing" "git.gophernest.net/azpect/TextEditor/internal/action" ) // --- G and gg Tests --- func TestMoveToBottom(t *testing.T) { t.Run("test 'G' from top", func(t *testing.T) { tm := newTestModel(t) sendKeys(tm, "G") m := getFinalModel(t, tm) if m.CursorY() != 5 { t.Errorf("CursorY() = %d, want 5", m.CursorY()) } }) t.Run("test 'G' from middle", func(t *testing.T) { tm := newTestModelWithCursorPos(t, action.Position{Col: 0, Line: 2}) sendKeys(tm, "G") m := getFinalModel(t, tm) if m.CursorY() != 5 { t.Errorf("CursorY() = %d, want 5", m.CursorY()) } }) t.Run("test 'G' already at bottom", func(t *testing.T) { tm := newTestModelWithCursorPos(t, action.Position{Col: 0, Line: 5}) sendKeys(tm, "G") m := getFinalModel(t, tm) if m.CursorY() != 5 { t.Errorf("CursorY() = %d, want 5", m.CursorY()) } }) t.Run("test 'G' clamps CursorX()", func(t *testing.T) { lines := []string{"long line here", "short"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 10, Line: 0}) sendKeys(tm, "G") m := getFinalModel(t, tm) if m.CursorY() != 1 { t.Errorf("CursorY() = %d, want 1", m.CursorY()) } want := len(lines[1]) if m.CursorX() != want { t.Errorf("CursorX() = %d, want %d", m.CursorX(), want) } }) t.Run("test 'G' on single line file", func(t *testing.T) { lines := []string{"only line"} tm := newTestModelWithLines(t, lines) sendKeys(tm, "G") m := getFinalModel(t, tm) if m.CursorY() != 0 { t.Errorf("CursorY() = %d, want 0", m.CursorY()) } }) } func TestMoveToTop(t *testing.T) { t.Run("test 'gg' from bottom", func(t *testing.T) { tm := newTestModelWithCursorPos(t, action.Position{Col: 0, Line: 5}) sendKeys(tm, "g", "g") m := getFinalModel(t, tm) if m.CursorY() != 0 { t.Errorf("CursorY() = %d, want 0", m.CursorY()) } }) t.Run("test 'gg' from middle", func(t *testing.T) { tm := newTestModelWithCursorPos(t, action.Position{Col: 0, Line: 3}) sendKeys(tm, "g", "g") m := getFinalModel(t, tm) if m.CursorY() != 0 { t.Errorf("CursorY() = %d, want 0", m.CursorY()) } }) t.Run("test 'gg' already at top", func(t *testing.T) { tm := newTestModel(t) sendKeys(tm, "g", "g") m := getFinalModel(t, tm) if m.CursorY() != 0 { t.Errorf("CursorY() = %d, want 0", m.CursorY()) } }) t.Run("test 'gg' clamps CursorX()", func(t *testing.T) { lines := []string{"short", "long line here"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 10, Line: 1}) sendKeys(tm, "g", "g") m := getFinalModel(t, tm) if m.CursorY() != 0 { t.Errorf("CursorY() = %d, want 0", m.CursorY()) } want := len(lines[0]) if m.CursorX() != want { t.Errorf("CursorX() = %d, want %d", m.CursorX(), want) } }) } // --- 0, $ and _ Tests --- func TestMoveToLineStart(t *testing.T) { t.Run("test '0' from middle of line", func(t *testing.T) { tm := newTestModelWithCursorPos(t, action.Position{Col: 3, Line: 0}) sendKeys(tm, "0") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '0' from end of line", func(t *testing.T) { lines := []string{"hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: len(lines[0]), Line: 0}) sendKeys(tm, "0") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '0' already at start", func(t *testing.T) { tm := newTestModel(t) sendKeys(tm, "0") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '0' on empty line", func(t *testing.T) { lines := []string{""} tm := newTestModelWithLines(t, lines) sendKeys(tm, "0") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '0' preserves line", func(t *testing.T) { tm := newTestModelWithCursorPos(t, action.Position{Col: 3, Line: 2}) sendKeys(tm, "0") m := getFinalModel(t, tm) if m.CursorY() != 2 { t.Errorf("CursorY() = %d, want 2", m.CursorY()) } }) } func TestMoveToLineEnd(t *testing.T) { t.Run("test '$' from start of line", func(t *testing.T) { lines := []string{"hello"} tm := newTestModelWithLines(t, lines) sendKeys(tm, "$") m := getFinalModel(t, tm) want := len(lines[0]) if m.CursorX() != want { t.Errorf("CursorX() = %d, want %d", m.CursorX(), want) } }) t.Run("test '$' from middle of line", func(t *testing.T) { lines := []string{"hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 3, Line: 0}) sendKeys(tm, "$") m := getFinalModel(t, tm) want := len(lines[0]) if m.CursorX() != want { t.Errorf("CursorX() = %d, want %d", m.CursorX(), want) } }) t.Run("test '$' already at end", func(t *testing.T) { lines := []string{"hello"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: len(lines[0]), Line: 0}) sendKeys(tm, "$") m := getFinalModel(t, tm) want := len(lines[0]) if m.CursorX() != want { t.Errorf("CursorX() = %d, want %d", m.CursorX(), want) } }) t.Run("test '$' on empty line", func(t *testing.T) { lines := []string{""} tm := newTestModelWithLines(t, lines) sendKeys(tm, "$") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '$' preserves line", func(t *testing.T) { tm := newTestModelWithCursorPos(t, action.Position{Col: 0, Line: 2}) sendKeys(tm, "$") m := getFinalModel(t, tm) if m.CursorY() != 2 { t.Errorf("CursorY() = %d, want 2", m.CursorY()) } }) } func TestMoveToLineContentStart(t *testing.T) { t.Run("test '_' from middle of line with no leading whitespace", func(t *testing.T) { lines := []string{"hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 6, Line: 0}) sendKeys(tm, "_") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '_' from middle of line with leading whitespace", func(t *testing.T) { lines := []string{" hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 10, Line: 0}) sendKeys(tm, "_") m := getFinalModel(t, tm) if m.CursorX() != 4 { t.Errorf("CursorX() = %d, want 4", m.CursorX()) } }) t.Run("test '_' from start of line with leading whitespace", func(t *testing.T) { lines := []string{" hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 0}) sendKeys(tm, "_") m := getFinalModel(t, tm) if m.CursorX() != 4 { t.Errorf("CursorX() = %d, want 4", m.CursorX()) } }) t.Run("test '_' from start of line with no leading whitespace", func(t *testing.T) { lines := []string{"hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 0}) sendKeys(tm, "_") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '_' from middle of line with only whitespace", func(t *testing.T) { lines := []string{" "} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 2, Line: 0}) sendKeys(tm, "_") m := getFinalModel(t, tm) if m.CursorX() != 4 { t.Errorf("CursorX() = %d, want 4", m.CursorX()) } }) t.Run("test '_' from end of line with only whitespace", func(t *testing.T) { lines := []string{" "} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 4, Line: 0}) sendKeys(tm, "_") m := getFinalModel(t, tm) if m.CursorX() != 4 { t.Errorf("CursorX() = %d, want 4", m.CursorX()) } }) t.Run("test '_' on empty line", func(t *testing.T) { lines := []string{""} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 0}) sendKeys(tm, "_") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) } func TestMoveToLineContentStartAlias(t *testing.T) { t.Run("test '^' from middle of line with no leading whitespace", func(t *testing.T) { lines := []string{"hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 6, Line: 0}) sendKeys(tm, "^") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '^' from middle of line with leading whitespace", func(t *testing.T) { lines := []string{" hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 10, Line: 0}) sendKeys(tm, "^") m := getFinalModel(t, tm) if m.CursorX() != 4 { t.Errorf("CursorX() = %d, want 4", m.CursorX()) } }) t.Run("test '^' from start of line with leading whitespace", func(t *testing.T) { lines := []string{" hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 0}) sendKeys(tm, "^") m := getFinalModel(t, tm) if m.CursorX() != 4 { t.Errorf("CursorX() = %d, want 4", m.CursorX()) } }) t.Run("test '^' from start of line with no leading whitespace", func(t *testing.T) { lines := []string{"hello world"} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 0}) sendKeys(tm, "^") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) t.Run("test '^' from middle of line with only whitespace", func(t *testing.T) { lines := []string{" "} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 2, Line: 0}) sendKeys(tm, "^") m := getFinalModel(t, tm) if m.CursorX() != 4 { t.Errorf("CursorX() = %d, want 4", m.CursorX()) } }) t.Run("test '^' from end of line with only whitespace", func(t *testing.T) { lines := []string{" "} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 4, Line: 0}) sendKeys(tm, "^") m := getFinalModel(t, tm) if m.CursorX() != 4 { t.Errorf("CursorX() = %d, want 4", m.CursorX()) } }) t.Run("test '^' on empty line", func(t *testing.T) { lines := []string{""} tm := newTestModelWithLinesAndCursorPos(t, lines, action.Position{Col: 0, Line: 0}) sendKeys(tm, "^") m := getFinalModel(t, tm) if m.CursorX() != 0 { t.Errorf("CursorX() = %d, want 0", m.CursorX()) } }) }