▍ humdrum codex / glint v1.0.2
license AGPL-3.0
3.2 KB raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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)
	}
}