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

snippet_test.go · 158 lines · 4.4 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 17h ago1package editor
2
3import (
4 "strings"
5 "testing"
6
📦 Turbo Core f3ade8d k33g 9h ago7 "rickub.com/turbo-editors/turbo-core/buffer"
🛟 Updated. 28d5985 k33g 17h ago8)
9
10// viewOn returns a view on a buffer holding text, with the cursor placed.
11func viewOn(t *testing.T, text string, line, col int) *View {
12 t.Helper()
13
14 buf := buffer.New()
15 buf.SetText(text)
16 v := NewView(buf, &Clipboard{})
17 buf.SetCursor(buffer.Position{Line: line, Col: col})
18 return v
19}
20
21func TestInsertSnippetPutsASingleLineAtTheCursor(t *testing.T) {
22 v := viewOn(t, "abcd\n", 0, 2)
23
24 v.InsertSnippet("XY")
25
26 if got := v.Buffer().Text(); got != "abXYcd\n" {
27 t.Errorf("the buffer holds %q", got)
28 }
29}
30
31func TestInsertSnippetIndentsTheLinesAfterTheFirst(t *testing.T) {
32 // The case that decides the whole feature: a multi-line snippet dropped
33 // into indented code restarts at column zero without this.
34 v := viewOn(t, "func f() {\n\t\n}\n", 1, 1)
35
36 v.InsertSnippet("if err != nil {\n\treturn err\n}")
37
38 want := "func f() {\n\tif err != nil {\n\t\treturn err\n\t}\n}\n"
39 if got := v.Buffer().Text(); got != want {
40 t.Errorf("the buffer holds\n%q\nwant\n%q", got, want)
41 }
42}
43
44func TestInsertSnippetUsesSpacesWhenTheLineDoes(t *testing.T) {
45 v := viewOn(t, "x = [\n \n]\n", 1, 4)
46
47 v.InsertSnippet("a,\nb,")
48
49 want := "x = [\n a,\n b,\n]\n"
50 if got := v.Buffer().Text(); got != want {
51 t.Errorf("the buffer holds\n%q\nwant\n%q", got, want)
52 }
53}
54
55func TestInsertSnippetAtColumnZeroAddsNoIndent(t *testing.T) {
56 v := viewOn(t, "\n", 0, 0)
57
58 v.InsertSnippet("a\nb\nc")
59
60 if got := v.Buffer().Text(); got != "a\nb\nc\n" {
61 t.Errorf("the buffer holds %q", got)
62 }
63}
64
65func TestInsertSnippetLeavesBlankLinesBlank(t *testing.T) {
66 // Trailing whitespace on an empty line is something every formatter then
67 // removes, so putting it there is noise in the next diff.
68 v := viewOn(t, "func f() {\n\t\n}\n", 1, 1)
69
70 v.InsertSnippet("a\n\nb")
71
72 want := "func f() {\n\ta\n\n\tb\n}\n"
73 if got := v.Buffer().Text(); got != want {
74 t.Errorf("the buffer holds\n%q\nwant\n%q", got, want)
75 }
76}
77
78func TestInsertSnippetIsOneUndoStep(t *testing.T) {
79 v := viewOn(t, "func f() {\n\t\n}\n", 1, 1)
80 before := v.Buffer().Text()
81
82 v.InsertSnippet("if err != nil {\n\treturn err\n}")
83 v.Buffer().Undo()
84
85 if got := v.Buffer().Text(); got != before {
86 t.Errorf("one undo left\n%q\nwant\n%q", got, before)
87 }
88}
89
90func TestInsertSnippetLeavesTheCursorAfterTheText(t *testing.T) {
91 v := viewOn(t, "func f() {\n\t\n}\n", 1, 1)
92
93 v.InsertSnippet("a\nb")
94
95 cursor := v.Buffer().Cursor()
96 if cursor.Line != 2 {
97 t.Errorf("the cursor is on line %d, want the last line of the snippet", cursor.Line)
98 }
99 if got := v.Buffer().Line(cursor.Line); cursor.Col != len([]rune(got)) {
100 t.Errorf("the cursor is at column %d of %q, want the end", cursor.Col, got)
101 }
102}
103
104func TestInsertSnippetCountsAsAChange(t *testing.T) {
105 v := viewOn(t, "x\n", 0, 0)
106 before := v.Buffer().Revision()
107
108 v.InsertSnippet("y")
109
110 if v.Buffer().Revision() == before {
111 t.Error("inserting a snippet did not move the revision, so nothing will re-colour")
112 }
113 if !v.Buffer().Modified() {
114 t.Error("inserting a snippet left the buffer unmodified")
115 }
116}
117
118func TestInsertingAnEmptySnippetChangesNothing(t *testing.T) {
119 // The revision is what says nothing happened; Modified() cannot, because
120 // putting the text into the buffer in the first place already set it.
121 v := viewOn(t, "x\n", 0, 0)
122 before := v.Buffer().Revision()
123
124 v.InsertSnippet("")
125
126 if got := v.Buffer().Text(); got != "x\n" {
127 t.Errorf("the buffer holds %q", got)
128 }
129 if v.Buffer().Revision() != before {
130 t.Error("an empty snippet moved the revision")
131 }
132}
133
134func TestInsertSnippetHandlesAccentsInTheCurrentLine(t *testing.T) {
135 // The indent is taken as a byte prefix of the line, so a line whose text
136 // has accents must still give back only its whitespace.
137 if got := leadingWhitespace("\t évidemment"); got != "\t " {
138 t.Errorf("leadingWhitespace() = %q, want the tab and two spaces", got)
139 }
140}
141
142func TestLeadingWhitespaceOfABlankLineIsTheWholeLine(t *testing.T) {
143 if got := leadingWhitespace("\t\t"); got != "\t\t" {
144 t.Errorf("leadingWhitespace() = %q", got)
145 }
146 if got := leadingWhitespace(""); got != "" {
147 t.Errorf("leadingWhitespace() = %q", got)
148 }
149}
150
151func TestIndentContinuationLinesLeavesASingleLineAlone(t *testing.T) {
152 if got := indentContinuationLines("only", "\t"); got != "only" {
153 t.Errorf("got %q, want the text unchanged", got)
154 }
155 if strings.Contains(indentContinuationLines("only", "\t"), "\t") {
156 t.Error("a single-line snippet was indented")
157 }
158}