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.

selection_test.go · 137 lines · 3.9 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 4h ago1package buffer
2
3import "testing"
4
5func TestNoSelectionByDefault(t *testing.T) {
6 b := NewFromString("abc")
7
8 if _, ok := b.Selection(); ok {
9 t.Error("a fresh buffer must have no selection")
10 }
11 if got := b.SelectedText(); got != "" {
12 t.Errorf("SelectedText() = %q, want empty", got)
13 }
14}
15
16func TestSelectionExtendsFromTheAnchor(t *testing.T) {
17 b := NewFromString("hello world")
18 b.SetCursor(Position{Line: 0, Col: 6})
19 b.StartSelection()
20 b.SetCursorKeepingSelection(Position{Line: 0, Col: 11})
21
22 r, ok := b.Selection()
23 if !ok {
24 t.Fatal("Selection() reported none after extending from an anchor")
25 }
26 if r.Start != (Position{Line: 0, Col: 6}) || r.End != (Position{Line: 0, Col: 11}) {
27 t.Errorf("Selection() = %+v, want columns 6 to 11", r)
28 }
29 if got := b.SelectedText(); got != "world" {
30 t.Errorf("SelectedText() = %q, want %q", got, "world")
31 }
32}
33
34func TestSelectionIsOrderedWhenTheCursorGoesBackwards(t *testing.T) {
35 b := NewFromString("hello world")
36 b.SetCursor(Position{Line: 0, Col: 5})
37 b.StartSelection()
38 b.SetCursorKeepingSelection(Position{Line: 0, Col: 0})
39
40 if got := b.SelectedText(); got != "hello" {
41 t.Errorf("SelectedText() = %q, want %q even though the cursor moved left", got, "hello")
42 }
43}
44
45func TestSelectionSpanningLinesJoinsWithLineFeeds(t *testing.T) {
46 b := NewFromString("one\r\ntwo\r\nthree\r\n")
47 b.SetCursor(Position{Line: 0, Col: 1})
48 b.StartSelection()
49 b.SetCursorKeepingSelection(Position{Line: 2, Col: 2})
50
51 if got := b.SelectedText(); got != "ne\ntwo\nth" {
52 t.Errorf("SelectedText() = %q, want line feeds regardless of the file's CRLF endings", got)
53 }
54}
55
56func TestSelectionCollapsedOntoItsAnchorCountsAsAbsent(t *testing.T) {
57 b := NewFromString("abc")
58 b.SetCursor(Position{Line: 0, Col: 1})
59 b.StartSelection()
60 b.SetCursorKeepingSelection(Position{Line: 0, Col: 2})
61 b.SetCursorKeepingSelection(Position{Line: 0, Col: 1})
62
63 if _, ok := b.Selection(); ok {
64 t.Error("a selection reduced to nothing must report as absent")
65 }
66}
67
68func TestClearSelectionDropsTheAnchor(t *testing.T) {
69 b := NewFromString("abc")
70 b.StartSelection()
71 b.SetCursorKeepingSelection(Position{Line: 0, Col: 3})
72
73 b.ClearSelection()
74
75 if _, ok := b.Selection(); ok {
76 t.Error("ClearSelection left a selection behind")
77 }
78}
79
80func TestSetCursorDropsTheSelectionButSetCursorKeepingSelectionDoesNot(t *testing.T) {
81 b := NewFromString("abcdef")
82 b.StartSelection()
83
84 b.SetCursorKeepingSelection(Position{Line: 0, Col: 3})
85 if _, ok := b.Selection(); !ok {
86 t.Fatal("SetCursorKeepingSelection must keep the anchor")
87 }
88
89 b.SetCursor(Position{Line: 0, Col: 5})
90 if _, ok := b.Selection(); ok {
91 t.Error("SetCursor must drop the anchor")
92 }
93}
94
95func TestSelectAllCoversTheWholeBuffer(t *testing.T) {
96 b := NewFromString("one\ntwo")
97
98 b.SelectAll()
99
100 if got := b.SelectedText(); got != "one\ntwo" {
101 t.Errorf("SelectedText() = %q, want the whole text", got)
102 }
103 if got := b.Cursor(); got != (Position{Line: 1, Col: 3}) {
104 t.Errorf("Cursor() = %+v, want the end of the buffer", got)
105 }
106}
107
108func TestDeleteSelectionRemovesItAndReportsSo(t *testing.T) {
109 b := NewFromString("one\ntwo\nthree")
110 b.SetCursor(Position{Line: 0, Col: 1})
111 b.StartSelection()
112 b.SetCursorKeepingSelection(Position{Line: 2, Col: 2})
113
114 if !b.DeleteSelection() {
115 t.Fatal("DeleteSelection() = false, want true when something is selected")
116 }
117 if got := b.Text(); got != "oree" {
118 t.Errorf("Text() = %q, want %q", got, "oree")
119 }
120 if got := b.Cursor(); got != (Position{Line: 0, Col: 1}) {
121 t.Errorf("Cursor() = %+v, want the start of the deleted span", got)
122 }
123}
124
125func TestDeleteSelectionWithoutOneChangesNothing(t *testing.T) {
126 b := NewFromString("abc")
127
128 if b.DeleteSelection() {
129 t.Error("DeleteSelection() = true, want false when nothing is selected")
130 }
131 if got := b.Text(); got != "abc" {
132 t.Errorf("Text() = %q, want it unchanged", got)
133 }
134 if b.Modified() {
135 t.Error("DeleteSelection with no selection must not mark the buffer modified")
136 }
137}