-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisplay_scenario_test.go
251 lines (213 loc) · 8.69 KB
/
display_scenario_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 main
import (
"testing"
"github.com/gdamore/tcell"
)
func TestBasic1(t *testing.T) {
resp := make(chan bool)
h, e := initEditor(resp, 4, 6)
ctx := context{h: h, resp: resp, t: t, e: e}
expectPositionOnScreen(ctx, 0, 0)
// Single line overflows by typing, enough space on screen
sendChar(ctx, 'a')
expectScreen(ctx, [][]rune{{'a', '@', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a'}})
expectPositionOnScreen(ctx, 1, 0)
sendChar(ctx, 'b')
expectScreen(ctx, [][]rune{{'a', 'b', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b'}})
expectPositionOnScreen(ctx, 2, 0)
expectLineAndPosition(ctx, 0, 2)
sendChar(ctx, 'c')
expectScreen(ctx, [][]rune{{'a', 'b', 'c', '@'}, emptyRow, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c'}})
expectPositionOnScreen(ctx, 3, 0)
sendChar(ctx, 'd')
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'd'}, emptyRow, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'd'}})
expectPositionOnScreen(ctx, 0, 1)
sendChar(ctx, 'e')
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'e', '@', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e'}})
expectPositionOnScreen(ctx, 1, 1)
// Newline at last line, last character, enough space on screen
sendKey(ctx, tcell.KeyEnter)
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'e', '@', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e'}, {}})
expectPositionOnScreen(ctx, 0, 2)
sendChar(ctx, 'f')
sendChar(ctx, 'g')
sendChar(ctx, 'h')
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'e', '@', '@', '@'}, {'f', 'g', 'h', '@'}, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e'}, {'f', 'g', 'h'}})
expectPositionOnScreen(ctx, 3, 2)
expectLineAndPosition(ctx, 1, 3)
/*
T E S T A R R O W S
*/
// Up - takes us to previous, longer line
sendKey(ctx, tcell.KeyUp)
expectPositionOnScreen(ctx, 3, 0)
expectLineAndPosition(ctx, 0, 3)
// Up - first line, cannot go further
sendKey(ctx, tcell.KeyUp)
expectPositionOnScreen(ctx, 3, 0)
expectLineAndPosition(ctx, 0, 3)
// Right - jump to next line
sendKey(ctx, tcell.KeyRight)
expectPositionOnScreen(ctx, 0, 1)
// Right - regular
sendKey(ctx, tcell.KeyRight)
expectPositionOnScreen(ctx, 1, 1)
// Right - last char, cannot go further
sendKey(ctx, tcell.KeyRight)
expectPositionOnScreen(ctx, 1, 1)
expectLineAndPosition(ctx, 0, 5)
// Down - takes us to next, shorter line
sendKey(ctx, tcell.KeyDown)
expectPositionOnScreen(ctx, 3, 2)
expectLineAndPosition(ctx, 1, 3)
// Down - cannot go any more below, last line
sendKey(ctx, tcell.KeyDown)
expectPositionOnScreen(ctx, 3, 2)
expectLineAndPosition(ctx, 1, 3)
// Left - go to previus location
sendKey(ctx, tcell.KeyLeft)
expectPositionOnScreen(ctx, 2, 2)
expectLineAndPosition(ctx, 1, 2)
// 2 more times, go to beginning of line
sendKey(ctx, tcell.KeyLeft)
sendKey(ctx, tcell.KeyLeft)
expectPositionOnScreen(ctx, 0, 2)
expectLineAndPosition(ctx, 1, 0)
// Cannot go any more left
sendKey(ctx, tcell.KeyLeft)
expectPositionOnScreen(ctx, 0, 2)
expectLineAndPosition(ctx, 1, 0)
/*
T Y P I N G
*/
// Type at the beginning of line
sendChar(ctx, 'i')
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'e', '@', '@', '@'}, {'i', 'f', 'g', 'h'}, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e'}, {'i', 'f', 'g', 'h'}})
expectPositionOnScreen(ctx, 1, 2)
expectLineAndPosition(ctx, 1, 1)
sendChar(ctx, 'j')
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'e', '@', '@', '@'}, {'i', 'j', 'f', 'g'}, {'h', '@', '@', '@'}, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e'}, {'i', 'j', 'f', 'g', 'h'}})
expectPositionOnScreen(ctx, 2, 2)
expectLineAndPosition(ctx, 1, 2)
// Enter in the middle of line
sendKey(ctx, tcell.KeyEnter)
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'e', '@', '@', '@'}, {'i', 'j', '@', '@'}, {'f', 'g', 'h', '@'}, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e'}, {'i', 'j'}, {'f', 'g', 'h'}})
expectPositionOnScreen(ctx, 0, 3)
expectLineAndPosition(ctx, 2, 0)
// Enter for empty line
sendKey(ctx, tcell.KeyEnter)
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'd'}, {'e', '@', '@', '@'}, {'i', 'j', '@', '@'}, emptyRow, {'f', 'g', 'h', '@'}, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'd', 'e'}, {'i', 'j'}, {}, {'f', 'g', 'h'}})
expectPositionOnScreen(ctx, 0, 4)
expectLineAndPosition(ctx, 3, 0)
}
func TestDeletes(t *testing.T) {
resp := make(chan bool)
h, e := initEditor(resp, 4, 6)
ctx := context{h: h, resp: resp, t: t, e: e}
sendKey(ctx, tcell.KeyDEL)
expectPositionOnScreen(ctx, 0, 0)
// Delete in the middle of line
sendChar(ctx, 'a')
sendChar(ctx, 'b')
sendChar(ctx, 'c')
sendKey(ctx, tcell.KeyDEL)
expectScreen(ctx, [][]rune{{'a', 'b', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b'}})
expectPositionOnScreen(ctx, 2, 0)
// Delete in the middle of 2nd line, both chars
sendKey(ctx, tcell.KeyEnter)
sendChar(ctx, 'd')
sendChar(ctx, 'e')
expectScreen(ctx, [][]rune{{'a', 'b', '@', '@'}, {'d', 'e', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b'}, {'d', 'e'}})
expectPositionOnScreen(ctx, 2, 1)
sendKey(ctx, tcell.KeyDEL)
expectScreen(ctx, [][]rune{{'a', 'b', '@', '@'}, {'d', '@', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b'}, {'d'}})
expectPositionOnScreen(ctx, 1, 1)
sendKey(ctx, tcell.KeyDEL)
expectScreen(ctx, [][]rune{{'a', 'b', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b'}, {}})
expectPositionOnScreen(ctx, 0, 1)
sendKey(ctx, tcell.KeyDEL)
expectScreen(ctx, [][]rune{{'a', 'b', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b'}})
expectPositionOnScreen(ctx, 2, 0)
sendKey(ctx, tcell.KeyDEL)
sendKey(ctx, tcell.KeyDEL)
expectScreen(ctx, [][]rune{emptyRow, emptyRow, emptyRow, emptyRow, emptyRow, emptyRow})
expectData(ctx, [][]rune{{}})
expectPositionOnScreen(ctx, 0, 0)
}
func TestScreenShift(t *testing.T) {
resp := make(chan bool)
h, e := initEditor(resp, 4, 6)
ctx := context{h: h, resp: resp, t: t, e: e}
sendChar(ctx, 's')
sendKey(ctx, tcell.KeyEnter)
sendKey(ctx, tcell.KeyEnter)
sendKey(ctx, tcell.KeyEnter)
sendKey(ctx, tcell.KeyEnter)
sendKey(ctx, tcell.KeyEnter)
sendChar(ctx, 'a')
expectScreen(ctx, [][]rune{{'s', '@', '@', '@'}, emptyRow, emptyRow, emptyRow, emptyRow, {'a', '@', '@', '@'}})
// Screen shifts below by 1
sendKey(ctx, tcell.KeyEnter)
expectScreen(ctx, [][]rune{emptyRow, emptyRow, emptyRow, emptyRow, {'a', '@', '@', '@'}, emptyRow})
sendChar(ctx, 'b')
expectScreen(ctx, [][]rune{emptyRow, emptyRow, emptyRow, emptyRow, {'a', '@', '@', '@'}, {'b', '@', '@', '@'}})
sendChar(ctx, 'c')
sendChar(ctx, 'd')
sendChar(ctx, 'e')
expectScreen(ctx, [][]rune{emptyRow, emptyRow, emptyRow, {'a', '@', '@', '@'}, {'b', 'c', 'd', 'e'}, emptyRow})
}
func TestBasic2(t *testing.T) {
resp := make(chan bool)
h, e := initEditor(resp, 4, 6)
ctx := context{h: h, resp: resp, t: t, e: e}
// Test case when first line starts overflowiing => further lines get shifted
sendChar(ctx, 'a')
sendChar(ctx, 'b')
sendChar(ctx, 'c')
sendKey(ctx, tcell.KeyEnter)
sendChar(ctx, 'd')
sendKey(ctx, tcell.KeyEnter)
sendChar(ctx, 'e')
sendChar(ctx, 'f')
sendChar(ctx, 'g')
sendChar(ctx, 'h')
sendChar(ctx, 'i')
// Make sure we good
expectScreen(ctx, [][]rune{{'a', 'b', 'c', '@'}, {'d', '@', '@', '@'}, {'e', 'f', 'g', 'h'}, {'i', '@', '@', '@'}, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c'}, {'d'}, {'e', 'f', 'g', 'h', 'i'}})
expectPositionOnScreen(ctx, 1, 3)
expectLineAndPosition(ctx, 2, 5)
// Jump to the beginning and overflow by typing something
sendKey(ctx, tcell.KeyUp)
sendKey(ctx, tcell.KeyUp)
sendKey(ctx, tcell.KeyRight)
sendKey(ctx, tcell.KeyRight)
sendKey(ctx, tcell.KeyRight)
expectPositionOnScreen(ctx, 3, 0)
expectLineAndPosition(ctx, 0, 3)
// Text full, console newline but still no shift
sendChar(ctx, 'j')
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'j'}, {'d', '@', '@', '@'}, {'e', 'f', 'g', 'h'}, {'i', '@', '@', '@'}, emptyRow, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'j'}, {'d'}, {'e', 'f', 'g', 'h', 'i'}})
// Shift happening
sendChar(ctx, 'k')
expectScreen(ctx, [][]rune{{'a', 'b', 'c', 'j'}, {'k', '@', '@', '@'}, {'d', '@', '@', '@'}, {'e', 'f', 'g', 'h'}, {'i', '@', '@', '@'}, emptyRow})
expectData(ctx, [][]rune{{'a', 'b', 'c', 'j', 'k'}, {'d'}, {'e', 'f', 'g', 'h', 'i'}})
sendKey(ctx, tcell.KeyCtrlF)
}