-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisplay_unit_test.go
299 lines (236 loc) · 11.2 KB
/
display_unit_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
package main
import (
"testing"
"github.com/gdamore/tcell"
)
func TestArrow(t *testing.T) {
resp := make(chan bool)
h, e := initEditor(resp, 3, 3)
ctx := context{h: h, resp: resp, t: t, e: e}
/*
Right arrow test
*/
// Can go more right
setupScenario(ctx, [][]rune{{'a'}, {'b', 'c', 'd', 'e'}}, 1, 1, 2)
sendKey(ctx, tcell.KeyRight)
expectParams(ctx, 1, 1, 3)
// Cannot go any more right
setupScenario(ctx, [][]rune{{'a'}, {'b', 'c'}}, 1, 1, 2)
sendKey(ctx, tcell.KeyRight)
expectParams(ctx, 1, 1, 2)
// If on last char, can cause offsetY increase
setupScenario(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'b', 'c', 'd', 'e'}}, 0, 1, 2)
sendKey(ctx, tcell.KeyRight)
expectParams(ctx, 1, 1, 3)
/*
Left arrow test
*/
// Can go more left
setupScenario(ctx, [][]rune{{'a'}, {'b', 'c', 'd', 'e'}}, 1, 1, 2)
sendKey(ctx, tcell.KeyLeft)
expectParams(ctx, 1, 1, 1)
// Cannot go more left
setupScenario(ctx, [][]rune{{'a'}, {'b', 'c', 'd', 'e'}}, 1, 1, 0)
sendKey(ctx, tcell.KeyLeft)
expectParams(ctx, 1, 1, 0)
// If on first char on screen, can cause offsetY decrease
setupScenario(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'b', 'c', 'd', 'e'}}, 3, 1, 3)
sendKey(ctx, tcell.KeyLeft)
expectParams(ctx, 2, 1, 2)
/*
Down arrow test
*/
// Cannot go more down
setupScenario(ctx, [][]rune{{'a', 'b'}, {'b', 'c', 'd', 'e'}}, 1, 1, 2)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 1, 1, 2)
// Move to next longer line
setupScenario(ctx, [][]rune{{'a', 'b'}, {'b', 'c', 'd'}}, 0, 0, 2)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 0, 1, 2)
// Move to next shorter line
setupScenario(ctx, [][]rune{{'a', 'b', 'c'}, {'b', 'c'}}, 0, 0, 3)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 0, 1, 2)
// Move to next line: visible beginning, fits screen, visible part
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a', 'b', 'c', 'd'}}, 0, 1, 1)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 1, 2, 1)
// Move to next line: visible beginning, fits screen, visible part, longer
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g'}}, 0, 1, 1)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 2, 2, 1)
// Move to next line: visible beginning, fits screen, invisible part
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd'}, {'a', 'b', 'c', 'd'}}, 0, 1, 4)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 2, 2, 4)
// Move to next line: visible beginning, does not fit screen, visible part
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n'}}, 0, 1, 1)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 2, 2, 1)
// Move to next line: visible beginning, does not fit screen, invisible part
setupScenario(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n'}}, 0, 0, 4)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 2, 1, 4)
// Move to next line from line that's end is not visible, newline - 1 lines
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n'}, {'a'}}, 2, 2, 1)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 5, 3, 1)
// Move to next line from line that's end is not visible, newline - 2 lines, jump to beginning
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n'}, {'a', 'b', 'c', 'd'}}, 2, 2, 4)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 6, 3, 4)
// Move to next line: non-visible beginning, non-visible end, fits screen v1
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a'}, {'a', 'b'}}, 0, 2, 0)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 1, 3, 0)
// Move to next line: non-visible beginning, non-visible end, fits screen v2
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a'}, {'a', 'b', 'c', 'd'}}, 0, 2, 0)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 2, 3, 0)
// Move to next line: non-visible beginning, non-visible end, does not fit screen
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n'}}, 0, 2, 0)
sendKey(ctx, tcell.KeyDown)
expectParams(ctx, 3, 3, 0)
/*
Up arrow test
*/
// Cannot go more up
setupScenario(ctx, [][]rune{{'a', 'b'}, {'b', 'c', 'd', 'e'}}, 0, 0, 2)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 0, 0, 2)
// Move to previous longer line
setupScenario(ctx, [][]rune{{'a', 'b', 'c'}, {'b', 'c'}}, 0, 1, 1)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 0, 0, 1)
// Move to previous shorter line
setupScenario(ctx, [][]rune{{'a', 'b'}, {'b', 'c', 'd'}}, 0, 1, 3)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 0, 0, 2)
// Move to previous line: visible end, non-visible beginning, fits screen v1
setupScenario(ctx, [][]rune{{'a'}, {'a'}, {'a', 'b'}, {'a', 'b', 'c', 'd'}}, 3, 3, 1)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 2, 2, 1)
// Move to previous line: visible end, non-visible beginning, fits screen v2
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd'}, {'a', 'b', 'c', 'd'}}, 3, 2, 1)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 1, 1, 1)
// Move to previous line: visible end, non-visible beginning, fits screen v3
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd'}, {'a', 'b', 'c', 'd'}}, 3, 2, 4)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 1, 1, 4)
// Move to previous line: visible end, non-visible beginning, doesn't fit screen v1
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n'}, {'a'}}, 6, 2, 0)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 1, 1, 0)
// Move to previous line: visible end, non-visible beginning, doesn't fit screen v2
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n'}, {'a', 'b', 'c', 'd'}}, 6, 2, 4)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 1, 1, 4)
// Move to previous line: visible end, non-visible beginning, doesn't fit screen v2
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'm', 'n'}, {'a', 'b', 'c', 'd'}}, 7, 2, 4)
sendKey(ctx, tcell.KeyUp)
expectParams(ctx, 1, 1, 4)
}
func TestEnter(t *testing.T) {
resp := make(chan bool)
h, e := initEditor(resp, 3, 3)
ctx := context{h: h, resp: resp, t: t, e: e}
// beginning of document
setupScenario(ctx, [][]rune{{}, {'q'}, {'s'}}, 0, 0, 0)
sendKey(ctx, tcell.KeyEnter)
expectScenario(ctx, [][]rune{{}, {}, {'q'}, {'s'}}, 0, 1, 0)
// While coding in the middle
setupScenario(ctx, [][]rune{{'a'}, {'b'}, {'c'}, {'d'}, {'q'}, {'s'}}, 2, 3, 1)
sendKey(ctx, tcell.KeyEnter)
expectScenario(ctx, [][]rune{{'a'}, {'b'}, {'c'}, {'d'}, {}, {'q'}, {'s'}}, 2, 4, 0)
// After last line, v1
setupScenario(ctx, [][]rune{{'a'}, {'b'}, {'c', 'q'}, {'q'}, {'s'}}, 0, 2, 2)
sendKey(ctx, tcell.KeyEnter)
expectScenario(ctx, [][]rune{{'a'}, {'b'}, {'c', 'q'}, {}, {'q'}, {'s'}}, 1, 3, 0)
// After last line, v2
setupScenario(ctx, [][]rune{{'a'}, {'b', 'c', 'q', 'h', 'r'}, {'q'}, {'s'}}, 0, 1, 5)
sendKey(ctx, tcell.KeyEnter)
expectScenario(ctx, [][]rune{{'a'}, {'b', 'c', 'q', 'h', 'r'}, {}, {'q'}, {'s'}}, 1, 2, 0)
// Splitting the line. Mid screen, left 1-lines, all visible, no need to jump
setupScenario(ctx, [][]rune{{'a', 'b', 'c'}}, 0, 0, 1)
sendKey(ctx, tcell.KeyEnter)
setupScenario(ctx, [][]rune{{'a'}, {'b', 'c'}}, 0, 1, 0)
// Splitting the line of long, need to jump
setupScenario(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}}, 0, 0, 5)
sendKey(ctx, tcell.KeyEnter)
setupScenario(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e'}, {'f', 'g', 'h', 'i'}}, 1, 1, 0)
// Splitting the line of long, need to jump v2
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd', 'e', 'f'}}, 0, 1, 4)
sendKey(ctx, tcell.KeyEnter)
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd', 'e'}, {'e', 'f'}}, 2, 2, 0)
// Splitting too long line, above stays short part
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'}}, 0, 1, 4)
sendKey(ctx, tcell.KeyEnter)
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd'}, {'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'}}, 3, 2, 0)
// Splitting too long line, below stays short part
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'}}, 4, 1, 10)
sendKey(ctx, tcell.KeyEnter)
setupScenario(ctx, [][]rune{{'a'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}, {'k', 'l', 'm', 'n', 'o'}}, 5, 2, 0)
}
func TestTyping(t *testing.T) {
resp := make(chan bool)
h, e := initEditor(resp, 3, 3)
ctx := context{h: h, resp: resp, t: t, e: e}
// Only thing to test here is when offsetY changes while typing
setupScenario(ctx, [][]rune{{'a'}, {'b'}, {'c', 'd'}}, 0, 2, 2)
sendChar(ctx, 'e')
expectScenario(ctx, [][]rune{{'a'}, {'b'}, {'c', 'd', 'e'}}, 1, 2, 3)
}
func TestDel(t *testing.T) {
resp := make(chan bool)
h, e := initEditor(resp, 3, 3)
ctx := context{h: h, resp: resp, t: t, e: e}
// beginning of document
setupScenario(ctx, [][]rune{{}, {'q'}, {'s'}}, 0, 0, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{}, {'q'}, {'s'}}, 0, 0, 0)
// Test deleting characters
// Normal case
setupScenario(ctx, [][]rune{{'t'}, {'q', 's'}}, 0, 1, 2)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'q'}}, 0, 1, 1)
// Normal case v2
setupScenario(ctx, [][]rune{{'t'}, {'q', 's', 'r'}}, 0, 1, 3)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'q', 's'}}, 0, 1, 2)
// Shift
setupScenario(ctx, [][]rune{{'t'}, {'t'}, {'a', 'b', 'c'}}, 3, 2, 3)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t'}, {'a', 'b'}}, 2, 2, 2)
setupScenario(ctx, [][]rune{{'t'}, {'t'}, {'t'}, {'a', 'b', 'c'}}, 2, 3, 3)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t'}, {'t'}, {'a', 'b'}}, 2, 3, 2)
// Connect to previous line
setupScenario(ctx, [][]rune{{'t'}, {'t'}, {'a', 'b', 'c'}}, 1, 2, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t', 'a', 'b', 'c'}}, 1, 1, 1)
// Connect & shift together
setupScenario(ctx, [][]rune{{'t'}, {'t'}, {'a', 'b', 'c'}}, 2, 2, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t', 'a', 'b', 'c'}}, 1, 1, 1)
setupScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l'}, {'a', 'b', 'c'}}, 2, 2, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'a', 'b', 'c'}}, 2, 1, 3)
setupScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l'}, {'a', 'b', 'c'}}, 0, 2, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'a', 'b', 'c'}}, 0, 1, 3)
// More advanced connect & shift togethers
setupScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'm', 'n'}, {'a'}}, 3, 2, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'm', 'n', 'a'}}, 2, 1, 5)
setupScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'm', 'n'}, {'a'}}, 2, 2, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'm', 'n', 'a'}}, 2, 1, 5)
setupScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'm', 'n'}, {'a', 'b'}}, 3, 2, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'm', 'n', 'a', 'b'}}, 2, 1, 5)
setupScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'm', 'n'}, {'a', 'b', 'c', 'd', 'e', 'f'}}, 3, 2, 0)
sendKey(ctx, tcell.KeyDEL)
expectScenario(ctx, [][]rune{{'t'}, {'t', 's', 'l', 'm', 'n', 'a', 'b', 'c', 'd', 'e', 'f'}}, 2, 1, 5)
}