forked from c-bata/go-prompt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuffer_test.go
251 lines (213 loc) · 7.73 KB
/
buffer_test.go
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
package prompt
import (
"reflect"
"testing"
istrings "github.com/joeycumines/go-prompt/strings"
)
func TestNewBuffer(t *testing.T) {
b := NewBuffer()
if b.workingIndex != 0 {
t.Errorf("workingIndex should be %#v, got %#v", 0, b.workingIndex)
}
if !reflect.DeepEqual(b.workingLines, []string{""}) {
t.Errorf("workingLines should be %#v, got %#v", []string{""}, b.workingLines)
}
}
func TestBuffer_InsertText(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("some_text", DefColCount, DefRowCount, false)
if b.Text() != "some_text" {
t.Errorf("Text should be %#v, got %#v", "some_text", b.Text())
}
if b.cursorPosition != istrings.RuneCountInString("some_text") {
t.Errorf("cursorPosition should be %#v, got %#v", istrings.RuneCountInString("some_text"), b.cursorPosition)
}
}
func TestBuffer_InsertText_Overwrite(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("ABC", DefColCount, DefRowCount, false)
if b.Text() != "ABC" {
t.Errorf("Text should be %#v, got %#v", "ABC", b.Text())
}
if b.cursorPosition != istrings.RuneCountInString("ABC") {
t.Errorf("cursorPosition should be %#v, got %#v", istrings.RuneCountInString("ABC"), b.cursorPosition)
}
b.CursorLeft(1, DefColCount, DefRowCount)
// Replace C with DEF in ABC
b.InsertTextMoveCursor("DEF", DefColCount, DefRowCount, true)
if b.Text() != "ABDEF" {
t.Errorf("Text should be %#v, got %#v", "ABDEF", b.Text())
}
b.CursorLeft(100, DefColCount, DefRowCount)
// Replace ABD with GHI in ABDEF
b.InsertTextMoveCursor("GHI", DefColCount, DefRowCount, true)
if b.Text() != "GHIEF" {
t.Errorf("Text should be %#v, got %#v", "GHIEF", b.Text())
}
b.CursorLeft(100, DefColCount, DefRowCount)
// Replace GHI with J\nK in GHIEF
b.InsertTextMoveCursor("J\nK", DefColCount, DefRowCount, true)
if b.Text() != "J\nKEF" {
t.Errorf("Text should be %#v, got %#v", "J\nKEF", b.Text())
}
b.CursorUp(100, DefColCount, DefRowCount)
b.CursorLeft(100, DefColCount, DefRowCount)
// Replace J with LMN in J\nKEF test end of line
b.InsertTextMoveCursor("LMN", DefColCount, DefRowCount, true)
if b.Text() != "LMN\nKEF" {
t.Errorf("Text should be %#v, got %#v", "LMN\nKEF", b.Text())
}
}
func TestBuffer_CursorMovement(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("some_text", DefColCount, DefRowCount, false)
b.CursorLeft(1, DefColCount, DefRowCount)
b.CursorLeft(2, DefColCount, DefRowCount)
b.CursorRight(1, DefColCount, DefRowCount)
b.InsertTextMoveCursor("A", DefColCount, DefRowCount, false)
if b.Text() != "some_teAxt" {
t.Errorf("Text should be %#v, got %#v", "some_teAxt", b.Text())
}
if b.cursorPosition != istrings.RuneCountInString("some_teA") {
t.Errorf("Text should be %#v, got %#v", istrings.RuneCountInString("some_teA"), b.cursorPosition)
}
// Moving over left character counts.
b.CursorLeft(100, DefColCount, DefRowCount)
b.InsertTextMoveCursor("A", DefColCount, DefRowCount, false)
if b.Text() != "Asome_teAxt" {
t.Errorf("Text should be %#v, got %#v", "some_teAxt", b.Text())
}
if b.cursorPosition != istrings.RuneCountInString("A") {
t.Errorf("Text should be %#v, got %#v", istrings.RuneCountInString("some_teA"), b.cursorPosition)
}
// TODO: Going right already at right end.
}
func TestBuffer_CursorMovement_WithMultiByte(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("あいうえお", DefColCount, DefRowCount, false)
b.CursorLeft(1, DefColCount, DefRowCount)
if l := b.Document().TextAfterCursor(); l != "お" {
t.Errorf("Should be 'お', but got %s", l)
}
b.InsertTextMoveCursor("żółć", DefColCount, DefRowCount, true)
if b.Text() != "あいうえżółć" {
t.Errorf("Text should be %#v, got %#v", "あいうえżółć", b.Text())
}
}
func TestBuffer_CursorUp(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("long line1\nline2", DefColCount, DefRowCount, false)
b.CursorUp(1, DefColCount, DefRowCount)
if b.Document().cursorPosition != 5 {
t.Errorf("Should be %#v, got %#v", 5, b.Document().cursorPosition)
}
// Going up when already at the top.
b.CursorUp(1, DefColCount, DefRowCount)
if b.Document().cursorPosition != 5 {
t.Errorf("Should be %#v, got %#v", 5, b.Document().cursorPosition)
}
// Going up to a line that's shorter.
b.setDocument(&Document{}, DefColCount, DefRowCount)
b.InsertTextMoveCursor("line1\nlong line2", DefColCount, DefRowCount, false)
b.CursorUp(1, DefColCount, DefRowCount)
if b.Document().cursorPosition != 5 {
t.Errorf("Should be %#v, got %#v", 5, b.Document().cursorPosition)
}
}
func TestBuffer_CursorDown(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("line1\nline2", DefColCount, DefRowCount, false)
b.cursorPosition = 3
b.preferredColumn = -1
// Normally going down
b.CursorDown(1, DefColCount, DefRowCount)
if b.Document().cursorPosition != istrings.RuneCountInString("line1\nlin") {
t.Errorf("Should be %#v, got %#v", istrings.RuneCountInString("line1\nlin"), b.Document().cursorPosition)
}
// Going down to a line that's storter.
b = NewBuffer()
b.InsertTextMoveCursor("long line1\na\nb", DefColCount, DefRowCount, false)
b.cursorPosition = 3
b.CursorDown(1, DefColCount, DefRowCount)
if b.Document().cursorPosition != istrings.RuneCountInString("long line1\na") {
t.Errorf("Should be %#v, got %#v", istrings.RuneCountInString("long line1\na"), b.Document().cursorPosition)
}
}
func TestBuffer_DeleteBeforeCursor(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("some_text", DefColCount, DefRowCount, false)
b.CursorLeft(2, DefColCount, DefRowCount)
deleted := b.DeleteBeforeCursor(1, DefColCount, DefRowCount)
if b.Text() != "some_txt" {
t.Errorf("Should be %#v, got %#v", "some_txt", b.Text())
}
if deleted != "e" {
t.Errorf("Should be %#v, got %#v", deleted, "e")
}
if b.cursorPosition != istrings.RuneCountInString("some_t") {
t.Errorf("Should be %#v, got %#v", istrings.RuneCountInString("some_t"), b.cursorPosition)
}
// Delete over the characters length before cursor.
deleted = b.DeleteBeforeCursor(100, DefColCount, DefRowCount)
if deleted != "some_t" {
t.Errorf("Should be %#v, got %#v", "some_t", deleted)
}
if b.Text() != "xt" {
t.Errorf("Should be %#v, got %#v", "xt", b.Text())
}
// If cursor position is a beginning of line, it has no effect.
deleted = b.DeleteBeforeCursor(1, DefColCount, DefRowCount)
if deleted != "" {
t.Errorf("Should be empty, got %#v", deleted)
}
}
func TestBuffer_NewLine(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor(" hello", DefColCount, DefRowCount, false)
b.NewLine(DefColCount, DefRowCount, false)
ac := b.Text()
ex := " hello\n"
if ac != ex {
t.Errorf("Should be %#v, got %#v", ex, ac)
}
b = NewBuffer()
b.InsertTextMoveCursor(" hello", DefColCount, DefRowCount, false)
b.NewLine(DefColCount, DefRowCount, true)
ac = b.Text()
ex = " hello\n "
if ac != ex {
t.Errorf("Should be %#v, got %#v", ex, ac)
}
}
func TestBuffer_JoinNextLine(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("line1\nline2\nline3", DefColCount, DefRowCount, false)
b.CursorUp(1, DefColCount, DefRowCount)
b.JoinNextLine(" ", DefColCount, DefRowCount)
ac := b.Text()
ex := "line1\nline2 line3"
if ac != ex {
t.Errorf("Should be %#v, got %#v", ex, ac)
}
// Test when there is no '\n' in the text
b = NewBuffer()
b.InsertTextMoveCursor("line1", DefColCount, DefRowCount, false)
b.cursorPosition = 0
b.JoinNextLine(" ", DefColCount, DefRowCount)
ac = b.Text()
ex = "line1"
if ac != ex {
t.Errorf("Should be %#v, got %#v", ex, ac)
}
}
func TestBuffer_SwapCharactersBeforeCursor(t *testing.T) {
b := NewBuffer()
b.InsertTextMoveCursor("hello world", DefColCount, DefRowCount, false)
b.CursorLeft(2, DefColCount, DefRowCount)
b.SwapCharactersBeforeCursor(DefColCount, DefRowCount)
ac := b.Text()
ex := "hello wrold"
if ac != ex {
t.Errorf("Should be %#v, got %#v", ex, ac)
}
}