package editor import ( "testing" tea "github.com/charmbracelet/bubbletea" ) func typeRune(e *Editor, r rune) { e.HandleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{r}}) } func TestAutoCloseInsertsPair(t *testing.T) { cases := []struct { open rune want string }{ {'(', "()"}, {'[', "[]"}, {'`', "``"}, } for _, c := range cases { e := newEditorWith("") typeRune(e, c.open) if e.Lines[0] != c.want { t.Errorf("typing %q: line = %q, want %q", c.open, e.Lines[0], c.want) } if e.Cursor.Col != 1 { t.Errorf("typing %q: cursor col = %d, want 1 (between pair)", c.open, e.Cursor.Col) } } } func TestAutoCloseStepsOverClosing(t *testing.T) { e := newEditorWith("") typeRune(e, '(') // -> "()", cursor 1 typeRune(e, ')') // should step over, not insert another ) if e.Lines[0] != "()" { t.Errorf("line = %q, want ()", e.Lines[0]) } if e.Cursor.Col != 2 { t.Errorf("cursor col = %d, want 2 (past closing)", e.Cursor.Col) } } func TestAutoCloseBacktickStepsOver(t *testing.T) { e := newEditorWith("") typeRune(e, '`') // -> "``", cursor 1 typeRune(e, '`') // step over if e.Lines[0] != "``" || e.Cursor.Col != 2 { t.Errorf("line=%q col=%d, want `` col 2", e.Lines[0], e.Cursor.Col) } } func TestClosingWithoutPairInsertsNormally(t *testing.T) { e := newEditorWith("") typeRune(e, ')') // no auto-pair to the right -> plain insert if e.Lines[0] != ")" || e.Cursor.Col != 1 { t.Errorf("line=%q col=%d, want ) col 1", e.Lines[0], e.Cursor.Col) } } func TestAutoCloseSkippedWithSelection(t *testing.T) { e := newEditorWith("word") e.setSelection(Position{0, 0}, Position{0, 4}) // select "word" typeRune(e, '(') // surround, not auto-close-empty if e.Lines[0] != "(word)" { t.Errorf("line = %q, want (word)", e.Lines[0]) } } func TestSmartHomeTogglesFirstNonBlank(t *testing.T) { e := newEditorWith(" - foo") e.Cursor = Position{Row: 0, Col: 7} // end of line e.MoveHome() if e.Cursor.Col != 2 { t.Errorf("first Home col = %d, want 2 (first non-blank)", e.Cursor.Col) } e.MoveHome() if e.Cursor.Col != 0 { t.Errorf("second Home col = %d, want 0", e.Cursor.Col) } e.MoveHome() if e.Cursor.Col != 2 { t.Errorf("third Home col = %d, want 2 (back to first non-blank)", e.Cursor.Col) } } func TestGotoLineClamps(t *testing.T) { e := newEditorWith("one", "two", "three") e.GotoLine(2) if e.Cursor.Row != 1 || e.Cursor.Col != 0 { t.Errorf("GotoLine(2) cursor = %+v, want {1 0}", e.Cursor) } e.GotoLine(99) if e.Cursor.Row != 2 { t.Errorf("GotoLine(99) row = %d, want 2 (last)", e.Cursor.Row) } e.GotoLine(0) if e.Cursor.Row != 0 { t.Errorf("GotoLine(0) row = %d, want 0", e.Cursor.Row) } } func TestSetCursorClamps(t *testing.T) { e := newEditorWith("ab", "cde") e.SetCursor(Position{Row: 1, Col: 2}) if e.Cursor != (Position{Row: 1, Col: 2}) { t.Errorf("cursor = %+v, want {1 2}", e.Cursor) } e.SetCursor(Position{Row: 9, Col: 9}) // out of range -> clamp to {1,3} if e.Cursor != (Position{Row: 1, Col: 3}) { t.Errorf("clamped cursor = %+v, want {1 3}", e.Cursor) } } func TestSmartHomeNoIndent(t *testing.T) { e := newEditorWith("abc") e.Cursor = Position{Row: 0, Col: 3} e.MoveHome() if e.Cursor.Col != 0 { t.Errorf("Home col = %d, want 0", e.Cursor.Col) } }