forked from limetext/commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsdel_test.go
338 lines (302 loc) · 7.69 KB
/
insdel_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
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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package commands
import (
"reflect"
"strings"
"testing"
"github.com/limetext/backend"
"github.com/limetext/text"
)
func TestBasic(t *testing.T) {
data := `Hello world
Test
Goodbye world
`
ed := backend.GetEditor()
w := ed.NewWindow()
defer w.Close()
v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()
e := v.BeginEdit()
v.Insert(e, 0, data)
v.EndEdit(e)
v.Sel().Clear()
v.Sel().Add(text.Region{11, 11})
v.Sel().Add(text.Region{16, 16})
v.Sel().Add(text.Region{30, 30})
ed.CommandHandler().RunTextCommand(v, "left_delete", nil)
if v.Substr(text.Region{0, v.Size()}) != `Hello worl
Tes
Goodbye worl
` {
t.Error(v.Substr(text.Region{0, v.Size()}))
}
ed.CommandHandler().RunTextCommand(v, "insert", backend.Args{"characters": "a"})
if d := v.Substr(text.Region{0, v.Size()}); d != "Hello worla\nTesa\nGoodbye worla\n" {
lines := strings.Split(v.Substr(text.Region{0, v.Size()}), "\n")
for _, l := range lines {
t.Errorf("%d: '%s'", len(l), l)
}
}
v.Settings().Set("translate_tabs_to_spaces", true)
ed.CommandHandler().RunTextCommand(v, "insert", backend.Args{"characters": "\t"})
if v.Substr(text.Region{0, v.Size()}) != "Hello worla \nTesa \nGoodbye worla \n" {
lines := strings.Split(v.Substr(text.Region{0, v.Size()}), "\n")
for _, l := range lines {
t.Errorf("%d: '%s'", len(l), l)
}
}
ed.CommandHandler().RunTextCommand(v, "left_delete", nil)
if d := v.Substr(text.Region{0, v.Size()}); d != "Hello worla\nTesa\nGoodbye worla\n" {
lines := strings.Split(v.Substr(text.Region{0, v.Size()}), "\n")
for _, l := range lines {
t.Errorf("%d: '%s'", len(l), l)
}
}
ed.CommandHandler().RunTextCommand(v, "left_delete", nil)
if d := v.Substr(text.Region{0, v.Size()}); d != "Hello worl\nTes\nGoodbye worl\n" {
lines := strings.Split(v.Substr(text.Region{0, v.Size()}), "\n")
for _, l := range lines {
t.Errorf("%d: '%s'", len(l), l)
}
}
ed.CommandHandler().RunTextCommand(v, "insert", backend.Args{"characters": "\t"})
if d := v.Substr(text.Region{0, v.Size()}); d != "Hello worl \nTes \nGoodbye worl \n" {
lines := strings.Split(v.Substr(text.Region{0, v.Size()}), "\n")
for _, l := range lines {
t.Errorf("%d: '%s'", len(l), l)
}
}
ed.CommandHandler().RunTextCommand(v, "left_delete", nil)
if v.Substr(text.Region{0, v.Size()}) != "Hello worl\nTes\nGoodbye worl\n" {
lines := strings.Split(v.Substr(text.Region{0, v.Size()}), "\n")
for _, l := range lines {
t.Errorf("%d: '%s'", len(l), l)
}
}
edit := v.BeginEdit()
v.Erase(edit, text.Region{0, v.Size()})
v.Insert(edit, 0, "€þıœəßðĸʒ×ŋµåäö𝄞")
v.EndEdit(edit)
orig := "€þıœəßðĸʒ×ŋµåäö𝄞"
if d := v.Substr(text.Region{0, v.Size()}); d != orig {
t.Errorf("%s\n\t%v\n\t%v", d, []byte(d), []byte(orig))
}
v.Sel().Clear()
v.Sel().Add(text.Region{3, 3})
v.Sel().Add(text.Region{6, 6})
v.Sel().Add(text.Region{9, 9})
ed.CommandHandler().RunTextCommand(v, "left_delete", nil)
exp := "€þœəðĸ×ŋµåäö𝄞"
if d := v.Substr(text.Region{0, v.Size()}); d != exp {
t.Errorf("%s\n\t%v\n\t%v", d, []byte(d), []byte(exp))
}
}
type deleteTest struct {
in, out []text.Region
text string
ins string
}
func runDeleteTest(command string, tests *[]deleteTest, t *testing.T) {
ed := backend.GetEditor()
w := ed.NewWindow()
for i, test := range *tests {
v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()
e := v.BeginEdit()
v.Insert(e, 0, test.ins)
v.EndEdit(e)
v.Sel().Clear()
for _, r := range test.in {
v.Sel().Add(r)
}
var s2 text.RegionSet
for _, r := range test.out {
s2.Add(r)
}
ed.CommandHandler().RunTextCommand(v, command, nil)
if d := v.Substr(text.Region{0, v.Size()}); d != test.text {
t.Errorf("Test %02d: Expected %s, but got %s", i, test.text, d)
} else if !reflect.DeepEqual(*v.Sel(), s2) {
t.Errorf("Test %02d: Expected %v, but have %v", i, s2, v.Sel())
}
}
}
func TestLeftDelete(t *testing.T) {
tests := []deleteTest{
{
[]text.Region{{1, 1}, {2, 2}, {3, 3}, {4, 4}},
[]text.Region{{0, 0}},
"5678",
"12345678",
},
{
[]text.Region{{1, 1}, {3, 3}, {5, 5}, {7, 7}},
[]text.Region{{0, 0}, {1, 1}, {2, 2}, {3, 3}},
"2468",
"12345678",
},
{
[]text.Region{{1, 3}},
[]text.Region{{1, 1}},
"145678",
"12345678",
},
{
[]text.Region{{3, 1}},
[]text.Region{{1, 1}},
"145678",
"12345678",
},
{
[]text.Region{{100, 5}},
[]text.Region{{93, 5}},
"abc\nd",
"abc\ndef\nghi\n",
}, // Yes, this is indeed what ST3 does too.
}
runDeleteTest("left_delete", &tests, t)
}
func TestRightDelete(t *testing.T) {
tests := []deleteTest{
{
[]text.Region{{0, 0}, {1, 1}, {2, 2}, {3, 3}},
[]text.Region{{0, 0}},
"5678",
"12345678",
},
{
[]text.Region{{1, 1}, {3, 3}, {5, 5}, {7, 7}},
[]text.Region{{1, 1}, {2, 2}, {3, 3}, {4, 4}},
"1357",
"12345678",
},
{
[]text.Region{{1, 3}},
[]text.Region{{1, 1}},
"145678",
"12345678",
},
{
[]text.Region{{3, 1}},
[]text.Region{{1, 1}},
"145678",
"12345678",
},
}
runDeleteTest("right_delete", &tests, t)
}
func TestInsert(t *testing.T) {
ed := backend.GetEditor()
ch := ed.CommandHandler()
w := ed.NewWindow()
defer w.Close()
v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()
e := v.BeginEdit()
v.Insert(e, 0, "Hello World!\nTest123123\nAbrakadabra\n")
v.EndEdit(e)
type Test struct {
in []text.Region
data string
expd string
expr []text.Region
}
tests := []Test{
{
[]text.Region{{1, 1}, {3, 3}, {6, 6}},
"a",
"Haelalo aWorld!\nTest123123\nAbrakadabra\n",
[]text.Region{{2, 2}, {5, 5}, {9, 9}},
},
{
[]text.Region{{1, 1}, {3, 3}, {6, 9}},
"a",
"Haelalo ald!\nTest123123\nAbrakadabra\n",
[]text.Region{{2, 2}, {5, 5}, {9, 9}},
},
{
[]text.Region{{1, 1}, {3, 3}, {6, 9}},
"€þıœəßðĸʒ×ŋµåäö𝄞",
"H€þıœəßðĸʒ×ŋµåäö𝄞el€þıœəßðĸʒ×ŋµåäö𝄞lo €þıœəßðĸʒ×ŋµåäö𝄞ld!\nTest123123\nAbrakadabra\n",
[]text.Region{{17, 17}, {35, 35}, {54, 54}},
},
}
for i, test := range tests {
v.Sel().Clear()
for _, r := range test.in {
v.Sel().Add(r)
}
ed.CommandHandler().RunTextCommand(v, "insert", backend.Args{"characters": test.data})
if d := v.Substr(text.Region{0, v.Size()}); d != test.expd {
t.Errorf("Insert test %d failed: %s", i, d)
}
if sr := v.Sel().Regions(); !reflect.DeepEqual(sr, test.expr) {
t.Errorf("Insert test %d failed: %v", i, sr)
}
ch.RunTextCommand(v, "undo", nil)
}
}
func TestDeleteWord(t *testing.T) {
tests := []struct {
text string
sel []text.Region
forward bool
expect string
}{
{
"word",
[]text.Region{{4, 4}},
false,
"",
},
{
"'(}[word",
[]text.Region{{7, 7}, {4, 4}},
false,
"d",
},
{
"testing forwar|d\ndelete word",
[]text.Region{{0, 2}, {11, 11}, {16, 16}},
true,
"sting for|ddelete word",
},
{
"simple test on outside",
[]text.Region{{-1, -1}, {6, 6}, {13, 13}, {54, 33}, {31, 31}},
true,
"simpletest outside",
},
}
ed := backend.GetEditor()
w := ed.NewWindow()
defer w.Close()
for i, test := range tests {
v := w.NewFile()
defer func() {
v.SetScratch(true)
v.Close()
}()
e := v.BeginEdit()
v.Insert(e, 0, test.text)
v.EndEdit(e)
v.Sel().Clear()
v.Sel().AddAll(test.sel)
ed.CommandHandler().RunTextCommand(v, "delete_word", backend.Args{"forward": test.forward})
if d := v.Substr(text.Region{0, v.Size()}); d != test.expect {
t.Errorf("Test %d:\nExcepted: '%s' but got: '%s'", i, test.expect, d)
}
}
}