turbo-editors/turbo-corepublic Fork 0
v1.0.0
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.

📦 Turbo Core f3ade8d · on v1.0.0 · k33g · 8h ago
events_test.go · 471 lines · 13.4 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package editor

import (
	"strings"
	"testing"

	"github.com/gdamore/tcell/v2"

	"rickub.com/turbo-editors/turbo-core/buffer"
)

func TestArrowKeysMoveTheCursor(t *testing.T) {
	view, _, _ := newTestView(t, "abc\ndef", 40, 6)

	view.HandleKey(key(tcell.KeyRight, tcell.ModNone))
	view.HandleKey(key(tcell.KeyDown, tcell.ModNone))

	if got := view.Buffer().Cursor(); got != (buffer.Position{Line: 1, Col: 1}) {
		t.Errorf("Cursor() = %+v, want {Line:1 Col:1}", got)
	}
}

func TestCtrlArrowsMoveByWord(t *testing.T) {
	view, _, _ := newTestView(t, "alpha beta gamma", 40, 6)

	view.HandleKey(key(tcell.KeyRight, tcell.ModCtrl))

	if got := view.Buffer().Cursor().Col; got != 6 {
		t.Errorf("Cursor().Col = %d, want the start of the second word", got)
	}
}

func TestHomeAndEnd(t *testing.T) {
	view, _, _ := newTestView(t, "abc\ndefgh", 40, 6)
	view.Buffer().SetCursor(buffer.Position{Line: 1, Col: 2})

	view.HandleKey(key(tcell.KeyEnd, tcell.ModNone))
	if got := view.Buffer().Cursor().Col; got != 5 {
		t.Errorf("End left the cursor at column %d, want 5", got)
	}

	view.HandleKey(key(tcell.KeyHome, tcell.ModNone))
	if got := view.Buffer().Cursor().Col; got != 0 {
		t.Errorf("Home left the cursor at column %d, want 0", got)
	}

	view.HandleKey(key(tcell.KeyHome, tcell.ModCtrl))
	if got := view.Buffer().Cursor(); got != (buffer.Position{}) {
		t.Errorf("Ctrl-Home left the cursor at %+v, want the start of the buffer", got)
	}

	view.HandleKey(key(tcell.KeyEnd, tcell.ModCtrl))
	if got := view.Buffer().Cursor(); got != (buffer.Position{Line: 1, Col: 5}) {
		t.Errorf("Ctrl-End left the cursor at %+v, want the end of the buffer", got)
	}
}

func TestPageKeysMoveByAScreenful(t *testing.T) {
	view, _, _ := newTestView(t, strings.Repeat("line\n", 100), 40, 12)
	page := view.VisibleLines()

	view.HandleKey(key(tcell.KeyPgDn, tcell.ModNone))

	if got := view.Buffer().Cursor().Line; got != page {
		t.Errorf("Cursor().Line = %d, want %d", got, page)
	}

	view.HandleKey(key(tcell.KeyPgUp, tcell.ModNone))
	if got := view.Buffer().Cursor().Line; got != 0 {
		t.Errorf("Cursor().Line = %d, want 0", got)
	}
}

func TestShiftArrowsExtendTheSelection(t *testing.T) {
	view, _, _ := newTestView(t, "hello world", 40, 6)

	for range 5 {
		view.HandleKey(key(tcell.KeyRight, tcell.ModShift))
	}

	if got := view.SelectedText(); got != "hello" {
		t.Errorf("SelectedText() = %q, want %q", got, "hello")
	}
}

func TestAPlainArrowDropsTheSelection(t *testing.T) {
	view, _, _ := newTestView(t, "hello world", 40, 6)
	view.HandleKey(key(tcell.KeyRight, tcell.ModShift))

	view.HandleKey(key(tcell.KeyRight, tcell.ModNone))

	if got := view.SelectedText(); got != "" {
		t.Errorf("SelectedText() = %q, want the selection dropped", got)
	}
}

func TestTypingInsertsCharacters(t *testing.T) {
	view, _, _ := newTestView(t, "", 40, 6)
	changes := 0
	view.OnChange = func() { changes++ }

	for _, r := range "func" {
		view.HandleKey(typed(r))
	}

	if got := view.Buffer().Text(); got != "func" {
		t.Errorf("Text() = %q, want %q", got, "func")
	}
	if changes != 4 {
		t.Errorf("OnChange was called %d times, want 4", changes)
	}
}

func TestCtrlAndAltLettersAreNotTyped(t *testing.T) {
	view, _, _ := newTestView(t, "", 40, 6)

	view.HandleKey(tcell.NewEventKey(tcell.KeyRune, 'x', tcell.ModAlt))

	if got := view.Buffer().Text(); got != "" {
		t.Errorf("Text() = %q, want a modified key not to have been typed", got)
	}
}

func TestEnterInsertsALineAndKeepsTheIndent(t *testing.T) {
	view, _, _ := newTestView(t, "\tfoo()", 40, 6)
	view.Buffer().MoveLineEnd()

	view.HandleKey(key(tcell.KeyEnter, tcell.ModNone))

	if got := view.Buffer().Line(1); got != "\t" {
		t.Errorf("Line(1) = %q, want the indent carried over", got)
	}
}

func TestBackspaceAndDelete(t *testing.T) {
	view, _, _ := newTestView(t, "abcd", 40, 6)
	view.Buffer().SetCursor(buffer.Position{Line: 0, Col: 2})

	view.HandleKey(key(tcell.KeyBackspace2, tcell.ModNone))
	view.HandleKey(key(tcell.KeyDelete, tcell.ModNone))

	if got := view.Buffer().Text(); got != "ad" {
		t.Errorf("Text() = %q, want %q", got, "ad")
	}
}

func TestTabInsertsATabWhenNothingIsSelected(t *testing.T) {
	view, _, _ := newTestView(t, "x", 40, 6)

	view.HandleKey(key(tcell.KeyTab, tcell.ModNone))

	if got := view.Buffer().Text(); got != "\tx" {
		t.Errorf("Text() = %q, want a tab inserted", got)
	}
}

func TestTabIndentsEveryLineOfTheSelection(t *testing.T) {
	view, _, _ := newTestView(t, "one\ntwo\nthree", 40, 6)
	view.Buffer().SetCursor(buffer.Position{Line: 0, Col: 1})
	view.Buffer().StartSelection()
	view.Buffer().SetCursorKeepingSelection(buffer.Position{Line: 1, Col: 1})

	view.HandleKey(key(tcell.KeyTab, tcell.ModNone))

	if got := view.Buffer().Text(); got != "\tone\n\ttwo\nthree" {
		t.Errorf("Text() = %q, want the first two lines indented", got)
	}
}

func TestShiftTabRemovesOneIndentLevel(t *testing.T) {
	tests := []struct {
		name string
		give string
		want string
	}{
		{"a tab", "\tx", "x"},
		{"eight spaces", "        x", "x"},
		{"three spaces", "   x", "x"},
		{"nothing to remove", "x", "x"},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			view, _, _ := newTestView(t, tc.give, 40, 6)

			view.HandleKey(key(tcell.KeyBacktab, tcell.ModNone))

			if got := view.Buffer().Text(); got != tc.want {
				t.Errorf("Text() = %q, want %q", got, tc.want)
			}
		})
	}
}

func TestCutCopyAndPaste(t *testing.T) {
	clipboard := &Clipboard{}
	view, _, _ := newTestView(t, "hello world", 40, 6)
	view.clipboard = clipboard

	view.Buffer().SetCursor(buffer.Position{Line: 0, Col: 0})
	view.Buffer().StartSelection()
	view.Buffer().SetCursorKeepingSelection(buffer.Position{Line: 0, Col: 6})

	view.HandleKey(key(tcell.KeyCtrlX, tcell.ModNone))
	if got := view.Buffer().Text(); got != "world" {
		t.Fatalf("Text() = %q after a cut, want %q", got, "world")
	}
	if got := clipboard.Text(); got != "hello " {
		t.Errorf("the clipboard holds %q, want %q", got, "hello ")
	}

	view.Buffer().MoveBufferEnd()
	view.HandleKey(key(tcell.KeyCtrlV, tcell.ModNone))
	if got := view.Buffer().Text(); got != "worldhello " {
		t.Errorf("Text() = %q after a paste, want %q", got, "worldhello ")
	}
}

func TestTheTurboCClipboardKeysWorkToo(t *testing.T) {
	clipboard := &Clipboard{}
	view, _, _ := newTestView(t, "abc", 40, 6)
	view.clipboard = clipboard
	view.SelectAll()

	view.HandleKey(key(tcell.KeyInsert, tcell.ModCtrl)) // Ctrl-Ins copies
	if got := clipboard.Text(); got != "abc" {
		t.Errorf("the clipboard holds %q, want %q", got, "abc")
	}

	view.Buffer().MoveBufferEnd()
	view.HandleKey(key(tcell.KeyInsert, tcell.ModShift)) // Shift-Ins pastes
	if got := view.Buffer().Text(); got != "abcabc" {
		t.Errorf("Text() = %q, want %q", got, "abcabc")
	}

	view.SelectAll()
	view.HandleKey(key(tcell.KeyDelete, tcell.ModShift)) // Shift-Del cuts
	if got := view.Buffer().Text(); got != "" {
		t.Errorf("Text() = %q, want it emptied by the cut", got)
	}
}

func TestCopyingNothingDoesNotTouchTheClipboard(t *testing.T) {
	clipboard := &Clipboard{}
	clipboard.SetText("kept")
	view, _, _ := newTestView(t, "abc", 40, 6)
	view.clipboard = clipboard

	if view.Copy() || view.Cut() {
		t.Error("Copy or Cut reported success with nothing selected")
	}
	if got := clipboard.Text(); got != "kept" {
		t.Errorf("the clipboard holds %q, want it untouched", got)
	}
}

func TestPastingAnEmptyClipboardDoesNothing(t *testing.T) {
	view, _, _ := newTestView(t, "abc", 40, 6)

	if view.Paste() {
		t.Error("Paste reported success with an empty clipboard")
	}
	if got := view.Buffer().Text(); got != "abc" {
		t.Errorf("Text() = %q, want it unchanged", got)
	}
}

func TestUndoAndRedoKeys(t *testing.T) {
	view, _, _ := newTestView(t, "abc", 40, 6)
	view.Buffer().MoveBufferEnd()
	view.HandleKey(typed('d'))

	view.HandleKey(key(tcell.KeyCtrlZ, tcell.ModNone))
	if got := view.Buffer().Text(); got != "abc" {
		t.Errorf("Text() = %q after undo, want %q", got, "abc")
	}

	// Ctrl-R, not Ctrl-Y: Ctrl-Y is Turbo C's delete-line, and that claim won.
	view.HandleKey(key(tcell.KeyCtrlR, tcell.ModNone))
	if got := view.Buffer().Text(); got != "abcd" {
		t.Errorf("Text() = %q after redo, want %q", got, "abcd")
	}
}

func TestCtrlYNoLongerRedoes(t *testing.T) {
	// The half of the change that a user notices: the old key must not
	// quietly keep working, or nobody learns the new one until it bites.
	view, _, _ := newTestView(t, "abc", 40, 6)
	view.Buffer().MoveBufferEnd()
	view.HandleKey(typed('d'))
	view.HandleKey(key(tcell.KeyCtrlZ, tcell.ModNone))

	view.HandleKey(key(tcell.KeyCtrlY, tcell.ModNone))

	if got := view.Buffer().Text(); got == "abcd" {
		t.Error("Ctrl-Y redid the change; it deletes a line now")
	}
}

func TestCtrlNInsertsALineAndCtrlYDeletesOne(t *testing.T) {
	view, _, _ := newTestView(t, "one\ntwo\nthree\n", 40, 6)
	view.Buffer().SetCursor(buffer.Position{Line: 1, Col: 1})

	view.HandleKey(key(tcell.KeyCtrlN, tcell.ModNone))
	if got := view.Buffer().Text(); got != "one\n\ntwo\nthree\n" {
		t.Fatalf("after Ctrl-N Text() = %q", got)
	}

	view.HandleKey(key(tcell.KeyCtrlY, tcell.ModNone))
	if got := view.Buffer().Text(); got != "one\n\nthree\n" {
		t.Errorf("after Ctrl-Y Text() = %q, want the line the cursor was on gone", got)
	}
}

func TestCtrlASelectsEverything(t *testing.T) {
	view, _, _ := newTestView(t, "one\ntwo", 40, 6)

	view.HandleKey(key(tcell.KeyCtrlA, tcell.ModNone))

	if got := view.SelectedText(); got != "one\ntwo" {
		t.Errorf("SelectedText() = %q, want the whole buffer", got)
	}
}

func TestCtrlSpaceAsksForCompletion(t *testing.T) {
	view, _, _ := newTestView(t, "fmt.", 40, 6)
	asked := 0
	view.OnCompletionRequest = func() { asked++ }

	view.HandleKey(key(tcell.KeyCtrlSpace, tcell.ModNone))

	if asked != 1 {
		t.Errorf("completion was asked for %d times, want 1", asked)
	}
}

func TestTypingADotAsksForCompletion(t *testing.T) {
	view, _, _ := newTestView(t, "fmt", 40, 6)
	view.Buffer().MoveBufferEnd()
	asked := 0
	view.OnCompletionRequest = func() { asked++ }

	view.HandleKey(typed('.'))
	view.HandleKey(typed('P'))

	if asked != 1 {
		t.Errorf("completion was asked for %d times, want 1 — only the dot triggers it", asked)
	}
}

func TestAKeyTheViewHasNoUseForIsPassedOn(t *testing.T) {
	view, _, _ := newTestView(t, "abc", 40, 6)

	if view.HandleKey(key(tcell.KeyF9, tcell.ModNone)) {
		t.Error("the view claimed a function key it does nothing with")
	}
}

func TestWordBeforeCursor(t *testing.T) {
	tests := []struct {
		name string
		text string
		col  int
		want string
	}{
		{"mid-identifier", "fmt.Print", 9, "Print"},
		{"just after a dot", "fmt.", 4, ""},
		{"at the start of a line", "abc", 0, ""},
		{"a whole word", "hello", 5, "hello"},
		{"with digits and underscores", "a_1b", 4, "a_1b"},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			view, _, _ := newTestView(t, tc.text, 40, 6)
			view.Buffer().SetCursor(buffer.Position{Line: 0, Col: tc.col})

			if got := view.WordBeforeCursor(); got != tc.want {
				t.Errorf("WordBeforeCursor() = %q, want %q", got, tc.want)
			}
		})
	}
}

func TestReplaceWordBeforeCursorAcceptsACompletion(t *testing.T) {
	view, _, _ := newTestView(t, "fmt.Prin", 40, 6)
	view.Buffer().MoveBufferEnd()

	view.ReplaceWordBeforeCursor("Println")

	if got := view.Buffer().Text(); got != "fmt.Println" {
		t.Errorf("Text() = %q, want %q", got, "fmt.Println")
	}
}

func TestGoToLine(t *testing.T) {
	view, _, _ := newTestView(t, strings.Repeat("x\n", 50), 40, 8)

	view.GoToLine(30)

	if got := view.Buffer().Cursor().Line; got != 29 {
		t.Errorf("Cursor().Line = %d, want 29 — GoToLine counts from one", got)
	}
	if top := view.TopLine(); 29 < top || 29 >= top+view.VisibleLines() {
		t.Error("the view did not scroll to the requested line")
	}
}

func TestClickingPlacesTheCursor(t *testing.T) {
	view, _, _ := newTestView(t, "package main\nfunc main() {}", 40, 8)
	gutter := view.gutterWidth()

	view.HandleMouse(tcell.NewEventMouse(gutter+4, 1, tcell.Button1, tcell.ModNone))

	if got := view.Buffer().Cursor(); got != (buffer.Position{Line: 1, Col: 4}) {
		t.Errorf("Cursor() = %+v, want {Line:1 Col:4}", got)
	}
}

func TestDraggingSelects(t *testing.T) {
	view, _, _ := newTestView(t, "hello world", 40, 8)
	gutter := view.gutterWidth()

	view.HandleMouse(tcell.NewEventMouse(gutter, 0, tcell.Button1, tcell.ModNone))
	view.HandleMouse(tcell.NewEventMouse(gutter+5, 0, tcell.Button1, tcell.ModNone))

	if got := view.SelectedText(); got != "hello" {
		t.Errorf("SelectedText() = %q, want %q", got, "hello")
	}

	view.HandleMouse(tcell.NewEventMouse(gutter+5, 0, tcell.ButtonNone, tcell.ModNone))
	if view.selecting {
		t.Error("the drag did not end when the button came back up")
	}
}

func TestTheWheelScrolls(t *testing.T) {
	view, _, _ := newTestView(t, strings.Repeat("line\n", 100), 40, 10)

	view.HandleMouse(tcell.NewEventMouse(5, 5, tcell.WheelDown, tcell.ModNone))

	if got := view.TopLine(); got != wheelStep {
		t.Errorf("TopLine() = %d, want %d", got, wheelStep)
	}

	view.HandleMouse(tcell.NewEventMouse(5, 5, tcell.WheelUp, tcell.ModNone))
	if got := view.TopLine(); got != 0 {
		t.Errorf("TopLine() = %d, want 0", got)
	}
}

func TestAMouseEventTheViewHasNoUseForIsPassedOn(t *testing.T) {
	view, _, _ := newTestView(t, "abc", 40, 6)

	if view.HandleMouse(tcell.NewEventMouse(1, 1, tcell.Button3, tcell.ModNone)) {
		t.Error("the view claimed a middle-button event it does nothing with")
	}
}

func TestTheCursorIsReportedWhenItMoves(t *testing.T) {
	view, _, _ := newTestView(t, "abc", 40, 6)
	moves := 0
	view.OnCursorMove = func() { moves++ }

	view.HandleKey(key(tcell.KeyRight, tcell.ModNone))
	view.HandleKey(typed('x')) // an edit moves the cursor too

	if moves != 2 {
		t.Errorf("OnCursorMove was called %d times, want 2", moves)
	}
}