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.

buffer_test.go · 174 lines · 4.5 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 5h ago1package buffer
2
3import "testing"
4
5func TestNewBufferHoldsOneEmptyLine(t *testing.T) {
6 b := New()
7
8 if got := b.LineCount(); got != 1 {
9 t.Fatalf("LineCount() = %d, want 1", got)
10 }
11 if got := b.Line(0); got != "" {
12 t.Errorf("Line(0) = %q, want empty", got)
13 }
14 if b.Modified() {
15 t.Error("a fresh buffer must not be marked modified")
16 }
17}
18
19func TestNewFromStringSplitsOnLineFeeds(t *testing.T) {
20 b := NewFromString("package main\n\nfunc main() {}\n")
21
22 if got := b.LineCount(); got != 3 {
23 t.Fatalf("LineCount() = %d, want 3 (the trailing newline is not a line)", got)
24 }
25 for i, want := range []string{"package main", "", "func main() {}"} {
26 if got := b.Line(i); got != want {
27 t.Errorf("Line(%d) = %q, want %q", i, got, want)
28 }
29 }
30 if b.Modified() {
31 t.Error("loading text must not mark the buffer modified")
32 }
33}
34
35func TestTextRoundTripsTheTrailingNewline(t *testing.T) {
36 tests := []struct {
37 name string
38 text string
39 }{
40 {"with trailing newline", "a\nb\n"},
41 {"without trailing newline", "a\nb"},
42 {"empty", ""},
43 {"only a newline", "\n"},
44 }
45
46 for _, tc := range tests {
47 t.Run(tc.name, func(t *testing.T) {
48 if got := NewFromString(tc.text).Text(); got != tc.text {
49 t.Errorf("Text() = %q, want %q", got, tc.text)
50 }
51 })
52 }
53}
54
55func TestTextPreservesWindowsLineEndings(t *testing.T) {
56 b := NewFromString("a\r\nb\r\n")
57
58 if got := b.LineCount(); got != 2 {
59 t.Fatalf("LineCount() = %d, want 2", got)
60 }
61 if got := b.Line(0); got != "a" {
62 t.Errorf("Line(0) = %q, want %q — the carriage return must be stripped", got, "a")
63 }
64 if got := b.Text(); got != "a\r\nb\r\n" {
65 t.Errorf("Text() = %q, want the original CRLF endings back", got)
66 }
67}
68
69func TestLineAccessorsToleratateOutOfRangeIndexes(t *testing.T) {
70 b := NewFromString("only")
71
72 for _, i := range []int{-1, 1, 99} {
73 if got := b.Line(i); got != "" {
74 t.Errorf("Line(%d) = %q, want empty", i, got)
75 }
76 if got := b.LineLen(i); got != 0 {
77 t.Errorf("LineLen(%d) = %d, want 0", i, got)
78 }
79 if got := b.LineRunes(i); got != nil {
80 t.Errorf("LineRunes(%d) = %v, want nil", i, got)
81 }
82 }
83}
84
85func TestLineRunesReturnsAnIndependentCopy(t *testing.T) {
86 b := NewFromString("abc")
87
88 runes := b.LineRunes(0)
89 runes[0] = 'X'
90
91 if got := b.Line(0); got != "abc" {
92 t.Errorf("Line(0) = %q after mutating the returned slice, want %q", got, "abc")
93 }
94}
95
96func TestSetTabWidthIgnoresNonPositiveValues(t *testing.T) {
97 b := New()
98
99 b.SetTabWidth(4)
100 if got := b.TabWidth(); got != 4 {
101 t.Fatalf("TabWidth() = %d, want 4", got)
102 }
103
104 b.SetTabWidth(0)
105 b.SetTabWidth(-2)
106 if got := b.TabWidth(); got != 4 {
107 t.Errorf("TabWidth() = %d, want the previous 4 to be kept", got)
108 }
109}
110
111func TestPositionOrdering(t *testing.T) {
112 first := Position{Line: 1, Col: 5}
113 sameLineLater := Position{Line: 1, Col: 6}
114 nextLine := Position{Line: 2, Col: 0}
115
116 if !first.Before(sameLineLater) {
117 t.Error("a smaller column on the same line must come first")
118 }
119 if !first.Before(nextLine) {
120 t.Error("a smaller line must come first whatever the columns")
121 }
122 if first.Before(first) {
123 t.Error("a position must not come before itself")
124 }
125 if !sameLineLater.After(first) {
126 t.Error("After must be the mirror of Before")
127 }
128}
129
130func TestNewRangeOrdersItsBounds(t *testing.T) {
131 low := Position{Line: 0, Col: 1}
132 high := Position{Line: 3, Col: 2}
133
134 if got := NewRange(high, low); got.Start != low || got.End != high {
135 t.Errorf("NewRange(high, low) = %+v, want the bounds swapped", got)
136 }
137 if got := NewRange(low, high); got.Start != low || got.End != high {
138 t.Errorf("NewRange(low, high) = %+v, want the bounds kept", got)
139 }
140}
141
142func TestRangeContainsExcludesItsEnd(t *testing.T) {
143 r := Range{Start: Position{Line: 1}, End: Position{Line: 1, Col: 3}}
144
145 if !r.Contains(Position{Line: 1}) {
146 t.Error("the start of a range is inside it")
147 }
148 if r.Contains(Position{Line: 1, Col: 3}) {
149 t.Error("the end of a range is outside it")
150 }
151 if empty := (Range{}); empty.Contains(Position{}) {
152 t.Error("an empty range contains nothing, not even its own position")
153 }
154}
155
156func TestAnEmptyBufferHoldsAnEmptyString(t *testing.T) {
157 // Not "\n": a buffer holds what was put into it, and nothing was.
158 if got := New().Text(); got != "" {
159 t.Errorf("New().Text() = %q, want empty", got)
160 }
161 if got := NewFromString("").Text(); got != "" {
162 t.Errorf("NewFromString(\"\").Text() = %q, want empty", got)
163 }
164}
165
166func TestPressingEnterGivesAFileItsTrailingNewline(t *testing.T) {
167 b := New()
168 b.Insert("package main")
169 b.InsertNewlineAndIndent()
170
171 if got := b.Text(); got != "package main\n" {
172 t.Errorf("Text() = %q, want %q", got, "package main\n")
173 }
174}