turbo-editors/turbo-corepublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on main · k33g · 3h ago
cursor_test.go · 266 lines · 6.7 KBGo Blame HistoryRaw
  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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package buffer

import "testing"

func TestSetCursorClampsIntoTheBuffer(t *testing.T) {
	b := NewFromString("ab\ncdef")

	tests := []struct {
		name string
		give Position
		want Position
	}{
		{"past the end of a line", Position{Line: 0, Col: 99}, Position{Line: 0, Col: 2}},
		{"past the last line", Position{Line: 99, Col: 0}, Position{Line: 1, Col: 4}},
		{"negative line", Position{Line: -1, Col: 3}, Position{Line: 0, Col: 0}},
		{"negative column", Position{Line: 1, Col: -3}, Position{Line: 1, Col: 0}},
		{"already valid", Position{Line: 1, Col: 2}, Position{Line: 1, Col: 2}},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			b.SetCursor(tc.give)
			if got := b.Cursor(); got != tc.want {
				t.Errorf("SetCursor(%+v) left the cursor at %+v, want %+v", tc.give, got, tc.want)
			}
		})
	}
}

func TestMoveLeftWrapsOntoThePreviousLine(t *testing.T) {
	b := NewFromString("ab\ncd")
	b.SetCursor(Position{Line: 1, Col: 0})

	b.MoveLeft()

	if got := b.Cursor(); got != (Position{Line: 0, Col: 2}) {
		t.Errorf("Cursor() = %+v, want the end of line 0", got)
	}
}

func TestMoveLeftStopsAtTheStartOfTheBuffer(t *testing.T) {
	b := NewFromString("ab")
	b.SetCursor(Position{})

	b.MoveLeft()

	if got := b.Cursor(); got != (Position{}) {
		t.Errorf("Cursor() = %+v, want it to stay at the origin", got)
	}
}

func TestMoveRightWrapsOntoTheNextLine(t *testing.T) {
	b := NewFromString("ab\ncd")
	b.SetCursor(Position{Line: 0, Col: 2})

	b.MoveRight()

	if got := b.Cursor(); got != (Position{Line: 1, Col: 0}) {
		t.Errorf("Cursor() = %+v, want the start of line 1", got)
	}
}

func TestMoveRightStopsAtTheEndOfTheBuffer(t *testing.T) {
	b := NewFromString("ab")
	b.MoveBufferEnd()

	b.MoveRight()

	if got := b.Cursor(); got != (Position{Line: 0, Col: 2}) {
		t.Errorf("Cursor() = %+v, want it to stay at the end", got)
	}
}

func TestMoveUpAndDownClampTheColumn(t *testing.T) {
	b := NewFromString("longer line\nab")
	b.SetCursor(Position{Line: 0, Col: 9})

	b.MoveDown()
	if got := b.Cursor(); got != (Position{Line: 1, Col: 2}) {
		t.Errorf("after MoveDown, Cursor() = %+v, want the column clamped to 2", got)
	}

	b.MoveUp()
	if got := b.Cursor(); got != (Position{Line: 0, Col: 2}) {
		t.Errorf("after MoveUp, Cursor() = %+v, want the clamped column kept", got)
	}
}

func TestMoveToLineAndBufferBounds(t *testing.T) {
	b := NewFromString("abc\ndefgh")
	b.SetCursor(Position{Line: 1, Col: 2})

	b.MoveLineStart()
	if got := b.Cursor(); got != (Position{Line: 1, Col: 0}) {
		t.Errorf("MoveLineStart left the cursor at %+v", got)
	}

	b.MoveLineEnd()
	if got := b.Cursor(); got != (Position{Line: 1, Col: 5}) {
		t.Errorf("MoveLineEnd left the cursor at %+v", got)
	}

	b.MoveBufferStart()
	if got := b.Cursor(); got != (Position{}) {
		t.Errorf("MoveBufferStart left the cursor at %+v", got)
	}

	b.MoveBufferEnd()
	if got := b.Cursor(); got != (Position{Line: 1, Col: 5}) {
		t.Errorf("MoveBufferEnd left the cursor at %+v", got)
	}
}

func TestMoveWordLeft(t *testing.T) {
	tests := []struct {
		name    string
		text    string
		from    int
		wantCol int
	}{
		{"from the end of a word", "foo bar", 7, 4},
		{"from the start of a word", "foo bar", 4, 0},
		{"over several separators", "foo   bar", 9, 6},
		{"from just after separators", "foo   bar", 6, 0},
		{"punctuation is a separator", "a.b", 3, 2},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			b := NewFromString(tc.text)
			b.SetCursor(Position{Line: 0, Col: tc.from})

			b.MoveWordLeft()

			if got := b.Cursor().Col; got != tc.wantCol {
				t.Errorf("Cursor().Col = %d, want %d", got, tc.wantCol)
			}
		})
	}
}

func TestMoveWordLeftAtTheStartOfTheBufferDoesNothing(t *testing.T) {
	b := NewFromString("foo")
	b.SetCursor(Position{})

	b.MoveWordLeft()

	if got := b.Cursor(); got != (Position{}) {
		t.Errorf("Cursor() = %+v, want the origin", got)
	}
}

func TestMoveWordRight(t *testing.T) {
	tests := []struct {
		name    string
		text    string
		from    int
		wantCol int
	}{
		{"from the start of a word", "foo bar", 0, 4},
		{"from inside a word", "foo bar", 1, 4},
		{"over several separators", "foo   bar", 0, 6},
		{"no word left, stops at the end", "foo bar", 4, 7},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			b := NewFromString(tc.text)
			b.SetCursor(Position{Line: 0, Col: tc.from})

			b.MoveWordRight()

			if got := b.Cursor().Col; got != tc.wantCol {
				t.Errorf("Cursor().Col = %d, want %d", got, tc.wantCol)
			}
		})
	}
}

func TestMoveWordRightAtTheEndOfALineWrapsOver(t *testing.T) {
	b := NewFromString("foo\nbar")
	b.SetCursor(Position{Line: 0, Col: 3})

	b.MoveWordRight()

	if got := b.Cursor(); got != (Position{Line: 1, Col: 0}) {
		t.Errorf("Cursor() = %+v, want the start of the next line", got)
	}
}

func TestDisplayColumnExpandsTabs(t *testing.T) {
	b := NewFromString("\tab\tc")
	b.SetTabWidth(4)

	tests := []struct {
		runeCol   int
		screenCol int
	}{
		{0, 0},
		{1, 4}, // just after the leading tab
		{2, 5},
		{3, 6},
		{4, 8}, // the second tab fills columns 6 and 7
		{5, 9},
	}

	for _, tc := range tests {
		if got := b.DisplayColumn(0, tc.runeCol); got != tc.screenCol {
			t.Errorf("DisplayColumn(0, %d) = %d, want %d", tc.runeCol, got, tc.screenCol)
		}
	}
}

func TestDisplayColumnClampsItsArguments(t *testing.T) {
	b := NewFromString("ab")

	if got := b.DisplayColumn(0, 99); got != 2 {
		t.Errorf("DisplayColumn(0, 99) = %d, want 2", got)
	}
	if got := b.DisplayColumn(0, -1); got != 0 {
		t.Errorf("DisplayColumn(0, -1) = %d, want 0", got)
	}
	if got := b.DisplayColumn(99, 0); got != 0 {
		t.Errorf("DisplayColumn(99, 0) = %d, want 0", got)
	}
}

func TestRuneColumnIsTheInverseOfDisplayColumn(t *testing.T) {
	b := NewFromString("\tab\tc")
	b.SetTabWidth(4)

	for runeCol := 0; runeCol <= b.LineLen(0); runeCol++ {
		screenCol := b.DisplayColumn(0, runeCol)
		if got := b.RuneColumn(0, screenCol); got != runeCol {
			t.Errorf("RuneColumn(0, DisplayColumn(0, %d)=%d) = %d, want %d",
				runeCol, screenCol, got, runeCol)
		}
	}
}

func TestRuneColumnInsideATabPicksThatTab(t *testing.T) {
	b := NewFromString("\tx")
	b.SetTabWidth(4)

	for screenCol := 0; screenCol < 4; screenCol++ {
		if got := b.RuneColumn(0, screenCol); got != 0 {
			t.Errorf("RuneColumn(0, %d) = %d, want 0 — a click inside a tab lands on it", screenCol, got)
		}
	}
	if got := b.RuneColumn(0, 99); got != 2 {
		t.Errorf("RuneColumn(0, 99) = %d, want 2 — past the end lands after the last rune", got)
	}
}

func TestIsWordRuneAcceptsIdentifierCharacters(t *testing.T) {
	for _, r := range []rune{'a', 'Z', '0', '_', 'é', 'π'} {
		if !IsWordRune(r) {
			t.Errorf("IsWordRune(%q) = false, want true", r)
		}
	}
	for _, r := range []rune{' ', '\t', '.', '(', '-'} {
		if IsWordRune(r) {
			t.Errorf("IsWordRune(%q) = true, want false", r)
		}
	}
}