1 -- major tests for text editing flows
2 -- Arguably this should be called source_edit_tests.lua,
3 -- but that would mess up the git blame at this point.
5 function test_initial_state()
6 App
.screen
.init
{width
=120, height
=60}
7 Editor_state
= edit
.initialize_test_state()
8 Editor_state
.lines
= load_array
{}
9 Text
.redraw_all(Editor_state
)
10 edit
.draw(Editor_state
)
11 check_eq(#Editor_state
.lines
, 1, '#lines')
12 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
13 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
14 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top:line')
15 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
18 function test_click_to_create_drawing()
19 App
.screen
.init
{width
=800, height
=600}
20 Editor_state
= edit
.initialize_test_state()
21 Editor_state
.lines
= load_array
{}
22 Text
.redraw_all(Editor_state
)
23 edit
.draw(Editor_state
)
24 edit
.run_after_mouse_click(Editor_state
, 8,Editor_state
.top
+8, 1)
25 -- cursor skips drawing to always remain on text
26 check_eq(#Editor_state
.lines
, 2, '#lines')
27 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor')
30 function test_backspace_to_delete_drawing()
31 -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
32 App
.screen
.init
{width
=120, height
=60}
33 Editor_state
= edit
.initialize_test_state()
34 Editor_state
.lines
= load_array
{'```lines', '```', ''}
35 Text
.redraw_all(Editor_state
)
36 -- cursor is on text as always (outside tests this will get initialized correctly)
37 Editor_state
.cursor1
.line
= 2
38 -- backspacing deletes the drawing
39 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
40 check_eq(#Editor_state
.lines
, 1, '#lines')
41 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
44 function test_backspace_from_start_of_final_line()
45 -- display final line of text with cursor at start of it
46 App
.screen
.init
{width
=120, height
=60}
47 Editor_state
= edit
.initialize_test_state()
48 Editor_state
.lines
= load_array
{'abc', 'def'}
49 Editor_state
.screen_top1
= {line
=2, pos
=1}
50 Editor_state
.cursor1
= {line
=2, pos
=1}
51 Text
.redraw_all(Editor_state
)
52 -- backspace scrolls up
53 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
54 check_eq(#Editor_state
.lines
, 1, '#lines')
55 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
56 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
59 function test_insert_first_character()
60 App
.screen
.init
{width
=120, height
=60}
61 Editor_state
= edit
.initialize_test_state()
62 Editor_state
.lines
= load_array
{}
63 Text
.redraw_all(Editor_state
)
64 edit
.draw(Editor_state
)
65 edit
.run_after_text_input(Editor_state
, 'a')
66 local y
= Editor_state
.top
67 App
.screen
.check(y
, 'a', 'screen:1')
70 function test_press_ctrl()
71 -- press ctrl while the cursor is on text
72 App
.screen
.init
{width
=50, height
=80}
73 Editor_state
= edit
.initialize_test_state()
74 Editor_state
.lines
= load_array
{''}
75 Text
.redraw_all(Editor_state
)
76 Editor_state
.cursor1
= {line
=1, pos
=1}
77 Editor_state
.screen_top1
= {line
=1, pos
=1}
78 edit
.run_after_keychord(Editor_state
, 'C-m', 'm')
81 function test_move_left()
82 App
.screen
.init
{width
=120, height
=60}
83 Editor_state
= edit
.initialize_test_state()
84 Editor_state
.lines
= load_array
{'a'}
85 Text
.redraw_all(Editor_state
)
86 Editor_state
.cursor1
= {line
=1, pos
=2}
87 edit
.draw(Editor_state
)
88 edit
.run_after_keychord(Editor_state
, 'left', 'left')
89 check_eq(Editor_state
.cursor1
.pos
, 1, 'check')
92 function test_move_right()
93 App
.screen
.init
{width
=120, height
=60}
94 Editor_state
= edit
.initialize_test_state()
95 Editor_state
.lines
= load_array
{'a'}
96 Text
.redraw_all(Editor_state
)
97 Editor_state
.cursor1
= {line
=1, pos
=1}
98 edit
.draw(Editor_state
)
99 edit
.run_after_keychord(Editor_state
, 'right', 'right')
100 check_eq(Editor_state
.cursor1
.pos
, 2, 'check')
103 function test_move_left_to_previous_line()
104 App
.screen
.init
{width
=120, height
=60}
105 Editor_state
= edit
.initialize_test_state()
106 Editor_state
.lines
= load_array
{'abc', 'def'}
107 Text
.redraw_all(Editor_state
)
108 Editor_state
.cursor1
= {line
=2, pos
=1}
109 edit
.draw(Editor_state
)
110 edit
.run_after_keychord(Editor_state
, 'left', 'left')
111 check_eq(Editor_state
.cursor1
.line
, 1, 'line')
112 check_eq(Editor_state
.cursor1
.pos
, 4, 'pos') -- past end of line
115 function test_move_right_to_next_line()
116 App
.screen
.init
{width
=120, height
=60}
117 Editor_state
= edit
.initialize_test_state()
118 Editor_state
.lines
= load_array
{'abc', 'def'}
119 Text
.redraw_all(Editor_state
)
120 Editor_state
.cursor1
= {line
=1, pos
=4} -- past end of line
121 edit
.draw(Editor_state
)
122 edit
.run_after_keychord(Editor_state
, 'right', 'right')
123 check_eq(Editor_state
.cursor1
.line
, 2, 'line')
124 check_eq(Editor_state
.cursor1
.pos
, 1, 'pos')
127 function test_move_to_start_of_word()
128 App
.screen
.init
{width
=120, height
=60}
129 Editor_state
= edit
.initialize_test_state()
130 Editor_state
.lines
= load_array
{'abc'}
131 Text
.redraw_all(Editor_state
)
132 Editor_state
.cursor1
= {line
=1, pos
=3}
133 edit
.draw(Editor_state
)
134 edit
.run_after_keychord(Editor_state
, 'M-left', 'left')
135 check_eq(Editor_state
.cursor1
.pos
, 1, 'check')
138 function test_move_to_start_of_previous_word()
139 App
.screen
.init
{width
=120, height
=60}
140 Editor_state
= edit
.initialize_test_state()
141 Editor_state
.lines
= load_array
{'abc def'}
142 Text
.redraw_all(Editor_state
)
143 Editor_state
.cursor1
= {line
=1, pos
=4} -- at the space between words
144 edit
.draw(Editor_state
)
145 edit
.run_after_keychord(Editor_state
, 'M-left', 'left')
146 check_eq(Editor_state
.cursor1
.pos
, 1, 'check')
149 function test_skip_to_previous_word()
150 App
.screen
.init
{width
=120, height
=60}
151 Editor_state
= edit
.initialize_test_state()
152 Editor_state
.lines
= load_array
{'abc def'}
153 Text
.redraw_all(Editor_state
)
154 Editor_state
.cursor1
= {line
=1, pos
=5} -- at the start of second word
155 edit
.draw(Editor_state
)
156 edit
.run_after_keychord(Editor_state
, 'M-left', 'left')
157 check_eq(Editor_state
.cursor1
.pos
, 1, 'check')
160 function test_skip_past_tab_to_previous_word()
161 App
.screen
.init
{width
=120, height
=60}
162 Editor_state
= edit
.initialize_test_state()
163 Editor_state
.lines
= load_array
{'abc def\tghi'}
164 Text
.redraw_all(Editor_state
)
165 Editor_state
.cursor1
= {line
=1, pos
=10} -- within third word
166 edit
.draw(Editor_state
)
167 edit
.run_after_keychord(Editor_state
, 'M-left', 'left')
168 check_eq(Editor_state
.cursor1
.pos
, 9, 'check')
171 function test_skip_multiple_spaces_to_previous_word()
172 App
.screen
.init
{width
=120, height
=60}
173 Editor_state
= edit
.initialize_test_state()
174 Editor_state
.lines
= load_array
{'abc def'}
175 Text
.redraw_all(Editor_state
)
176 Editor_state
.cursor1
= {line
=1, pos
=6} -- at the start of second word
177 edit
.draw(Editor_state
)
178 edit
.run_after_keychord(Editor_state
, 'M-left', 'left')
179 check_eq(Editor_state
.cursor1
.pos
, 1, 'check')
182 function test_move_to_start_of_word_on_previous_line()
183 App
.screen
.init
{width
=120, height
=60}
184 Editor_state
= edit
.initialize_test_state()
185 Editor_state
.lines
= load_array
{'abc def', 'ghi'}
186 Text
.redraw_all(Editor_state
)
187 Editor_state
.cursor1
= {line
=2, pos
=1}
188 edit
.draw(Editor_state
)
189 edit
.run_after_keychord(Editor_state
, 'M-left', 'left')
190 check_eq(Editor_state
.cursor1
.line
, 1, 'line')
191 check_eq(Editor_state
.cursor1
.pos
, 5, 'pos')
194 function test_move_past_end_of_word()
195 App
.screen
.init
{width
=120, height
=60}
196 Editor_state
= edit
.initialize_test_state()
197 Editor_state
.lines
= load_array
{'abc def'}
198 Text
.redraw_all(Editor_state
)
199 Editor_state
.cursor1
= {line
=1, pos
=1}
200 edit
.draw(Editor_state
)
201 edit
.run_after_keychord(Editor_state
, 'M-right', 'right')
202 check_eq(Editor_state
.cursor1
.pos
, 4, 'check')
205 function test_skip_to_next_word()
206 App
.screen
.init
{width
=120, height
=60}
207 Editor_state
= edit
.initialize_test_state()
208 Editor_state
.lines
= load_array
{'abc def'}
209 Text
.redraw_all(Editor_state
)
210 Editor_state
.cursor1
= {line
=1, pos
=4} -- at the space between words
211 edit
.draw(Editor_state
)
212 edit
.run_after_keychord(Editor_state
, 'M-right', 'right')
213 check_eq(Editor_state
.cursor1
.pos
, 8, 'check')
216 function test_skip_past_tab_to_next_word()
217 App
.screen
.init
{width
=120, height
=60}
218 Editor_state
= edit
.initialize_test_state()
219 Editor_state
.lines
= load_array
{'abc\tdef'}
220 Text
.redraw_all(Editor_state
)
221 Editor_state
.cursor1
= {line
=1, pos
=1} -- at the space between words
222 edit
.draw(Editor_state
)
223 edit
.run_after_keychord(Editor_state
, 'M-right', 'right')
224 check_eq(Editor_state
.cursor1
.pos
, 4, 'check')
227 function test_skip_multiple_spaces_to_next_word()
228 App
.screen
.init
{width
=120, height
=60}
229 Editor_state
= edit
.initialize_test_state()
230 Editor_state
.lines
= load_array
{'abc def'}
231 Text
.redraw_all(Editor_state
)
232 Editor_state
.cursor1
= {line
=1, pos
=4} -- at the start of second word
233 edit
.draw(Editor_state
)
234 edit
.run_after_keychord(Editor_state
, 'M-right', 'right')
235 check_eq(Editor_state
.cursor1
.pos
, 9, 'check')
238 function test_move_past_end_of_word_on_next_line()
239 App
.screen
.init
{width
=120, height
=60}
240 Editor_state
= edit
.initialize_test_state()
241 Editor_state
.lines
= load_array
{'abc def', 'ghi'}
242 Text
.redraw_all(Editor_state
)
243 Editor_state
.cursor1
= {line
=1, pos
=8}
244 edit
.draw(Editor_state
)
245 edit
.run_after_keychord(Editor_state
, 'M-right', 'right')
246 check_eq(Editor_state
.cursor1
.line
, 2, 'line')
247 check_eq(Editor_state
.cursor1
.pos
, 4, 'pos')
250 function test_click_moves_cursor()
251 App
.screen
.init
{width
=50, height
=60}
252 Editor_state
= edit
.initialize_test_state()
253 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
254 Text
.redraw_all(Editor_state
)
255 Editor_state
.cursor1
= {line
=1, pos
=1}
256 Editor_state
.screen_top1
= {line
=1, pos
=1}
257 Editor_state
.selection1
= {}
258 edit
.draw(Editor_state
) -- populate line_cache.startpos for each line
259 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
260 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
261 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
262 -- selection is empty to avoid perturbing future edits
263 check_nil(Editor_state
.selection1
.line
, 'selection:line')
264 check_nil(Editor_state
.selection1
.pos
, 'selection:pos')
267 function test_click_to_left_of_line()
268 -- display a line with the cursor in the middle
269 App
.screen
.init
{width
=50, height
=80}
270 Editor_state
= edit
.initialize_test_state()
271 Editor_state
.lines
= load_array
{'abc'}
272 Text
.redraw_all(Editor_state
)
273 Editor_state
.cursor1
= {line
=1, pos
=3}
274 Editor_state
.screen_top1
= {line
=1, pos
=1}
275 Editor_state
.selection1
= {}
276 -- click to the left of the line
277 edit
.draw(Editor_state
)
278 edit
.run_after_mouse_click(Editor_state
, Editor_state
.left
-4,Editor_state
.top
+5, 1)
279 -- cursor moves to start of line
280 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
281 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
282 check_nil(Editor_state
.selection1
.line
, 'selection is empty to avoid perturbing future edits')
285 function test_click_takes_margins_into_account()
286 -- display two lines with cursor on one of them
287 App
.screen
.init
{width
=100, height
=80}
288 Editor_state
= edit
.initialize_test_state()
289 Editor_state
.left
= 50 -- occupy only right side of screen
290 Editor_state
.lines
= load_array
{'abc', 'def'}
291 Text
.redraw_all(Editor_state
)
292 Editor_state
.cursor1
= {line
=2, pos
=1}
293 Editor_state
.screen_top1
= {line
=1, pos
=1}
294 Editor_state
.selection1
= {}
295 -- click on the other line
296 edit
.draw(Editor_state
)
297 edit
.run_after_mouse_click(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
299 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
300 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
301 check_nil(Editor_state
.selection1
.line
, 'selection is empty to avoid perturbing future edits')
304 function test_click_on_empty_line()
305 -- display two lines with the first one empty
306 App
.screen
.init
{width
=50, height
=80}
307 Editor_state
= edit
.initialize_test_state()
308 Editor_state
.lines
= load_array
{'', 'def'}
309 Text
.redraw_all(Editor_state
)
310 Editor_state
.cursor1
= {line
=2, pos
=1}
311 Editor_state
.screen_top1
= {line
=1, pos
=1}
312 Editor_state
.selection1
= {}
313 -- click on the empty line
314 edit
.draw(Editor_state
)
315 edit
.run_after_mouse_click(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
317 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
318 -- selection remains empty
319 check_nil(Editor_state
.selection1
.line
, 'selection is empty to avoid perturbing future edits')
322 function test_click_below_final_line_of_file()
324 App
.screen
.init
{width
=50, height
=80}
325 Editor_state
= edit
.initialize_test_state()
326 Editor_state
.lines
= load_array
{'abc'}
327 Text
.redraw_all(Editor_state
)
328 Editor_state
.cursor1
= {line
=1, pos
=1}
329 Editor_state
.screen_top1
= {line
=1, pos
=1}
330 Editor_state
.selection1
= {}
331 -- click below first line
332 edit
.draw(Editor_state
)
333 edit
.run_after_mouse_click(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+50, 1)
334 -- cursor goes to bottom
335 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
336 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
337 -- selection remains empty
338 check_nil(Editor_state
.selection1
.line
, 'selection is empty to avoid perturbing future edits')
341 function test_draw_text()
342 App
.screen
.init
{width
=120, height
=60}
343 Editor_state
= edit
.initialize_test_state()
344 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi'}
345 Text
.redraw_all(Editor_state
)
346 Editor_state
.cursor1
= {line
=1, pos
=1}
347 Editor_state
.screen_top1
= {line
=1, pos
=1}
348 edit
.draw(Editor_state
)
349 local y
= Editor_state
.top
350 App
.screen
.check(y
, 'abc', 'screen:1')
351 y
= y
+ Editor_state
.line_height
352 App
.screen
.check(y
, 'def', 'screen:2')
353 y
= y
+ Editor_state
.line_height
354 App
.screen
.check(y
, 'ghi', 'screen:3')
357 function test_draw_wrapping_text()
358 App
.screen
.init
{width
=50, height
=60}
359 Editor_state
= edit
.initialize_test_state()
360 Editor_state
.lines
= load_array
{'abc', 'defgh', 'xyz'}
361 Text
.redraw_all(Editor_state
)
362 Editor_state
.cursor1
= {line
=1, pos
=1}
363 Editor_state
.screen_top1
= {line
=1, pos
=1}
364 edit
.draw(Editor_state
)
365 local y
= Editor_state
.top
366 App
.screen
.check(y
, 'abc', 'screen:1')
367 y
= y
+ Editor_state
.line_height
368 App
.screen
.check(y
, 'de', 'screen:2')
369 y
= y
+ Editor_state
.line_height
370 App
.screen
.check(y
, 'fgh', 'screen:3')
373 function test_draw_word_wrapping_text()
374 App
.screen
.init
{width
=60, height
=60}
375 Editor_state
= edit
.initialize_test_state()
376 Editor_state
.lines
= load_array
{'abc def ghi', 'jkl'}
377 Text
.redraw_all(Editor_state
)
378 Editor_state
.cursor1
= {line
=1, pos
=1}
379 Editor_state
.screen_top1
= {line
=1, pos
=1}
380 edit
.draw(Editor_state
)
381 local y
= Editor_state
.top
382 App
.screen
.check(y
, 'abc ', 'screen:1')
383 y
= y
+ Editor_state
.line_height
384 App
.screen
.check(y
, 'def ', 'screen:2')
385 y
= y
+ Editor_state
.line_height
386 App
.screen
.check(y
, 'ghi', 'screen:3')
389 function test_click_on_wrapping_line()
390 -- display two screen lines with cursor on one of them
391 App
.screen
.init
{width
=50, height
=80}
392 Editor_state
= edit
.initialize_test_state()
393 Editor_state
.lines
= load_array
{'abc def ghi jkl mno pqr stu'}
394 Text
.redraw_all(Editor_state
)
395 Editor_state
.cursor1
= {line
=1, pos
=20}
396 Editor_state
.screen_top1
= {line
=1, pos
=1}
397 -- click on the other line
398 edit
.draw(Editor_state
)
399 edit
.run_after_mouse_click(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
401 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
402 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
403 check_nil(Editor_state
.selection1
.line
, 'selection is empty to avoid perturbing future edits')
406 function test_click_on_wrapping_line_takes_margins_into_account()
407 -- display two screen lines with cursor on one of them
408 App
.screen
.init
{width
=100, height
=80}
409 Editor_state
= edit
.initialize_test_state()
410 Editor_state
.left
= 50 -- occupy only right side of screen
411 Editor_state
.lines
= load_array
{'abc def ghi jkl mno pqr stu'}
412 Text
.redraw_all(Editor_state
)
413 Editor_state
.cursor1
= {line
=1, pos
=20}
414 Editor_state
.screen_top1
= {line
=1, pos
=1}
415 -- click on the other line
416 edit
.draw(Editor_state
)
417 edit
.run_after_mouse_click(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
419 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
420 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
421 check_nil(Editor_state
.selection1
.line
, 'selection is empty to avoid perturbing future edits')
424 function test_draw_text_wrapping_within_word()
425 -- arrange a screen line that needs to be split within a word
426 App
.screen
.init
{width
=60, height
=60}
427 Editor_state
= edit
.initialize_test_state()
428 Editor_state
.lines
= load_array
{'abcd e fghijk', 'xyz'}
429 Text
.redraw_all(Editor_state
)
430 Editor_state
.cursor1
= {line
=1, pos
=1}
431 Editor_state
.screen_top1
= {line
=1, pos
=1}
432 edit
.draw(Editor_state
)
433 local y
= Editor_state
.top
434 App
.screen
.check(y
, 'abcd ', 'screen:1')
435 y
= y
+ Editor_state
.line_height
436 App
.screen
.check(y
, 'e fgh', 'screen:2')
437 y
= y
+ Editor_state
.line_height
438 App
.screen
.check(y
, 'ijk', 'screen:3')
441 function test_draw_wrapping_text_containing_non_ascii()
442 -- draw a long line containing non-ASCII
443 App
.screen
.init
{width
=60, height
=60}
444 Editor_state
= edit
.initialize_test_state()
445 Editor_state
.lines
= load_array
{'madam I’m adam', 'xyz'} -- notice the non-ASCII apostrophe
446 Text
.redraw_all(Editor_state
)
447 Editor_state
.cursor1
= {line
=1, pos
=1}
448 Editor_state
.screen_top1
= {line
=1, pos
=1}
449 edit
.draw(Editor_state
)
450 local y
= Editor_state
.top
451 App
.screen
.check(y
, 'mad', 'screen:1')
452 y
= y
+ Editor_state
.line_height
453 App
.screen
.check(y
, 'am I', 'screen:2')
454 y
= y
+ Editor_state
.line_height
455 App
.screen
.check(y
, '’m a', 'screen:3')
458 function test_click_past_end_of_screen_line()
459 -- display a wrapping line
460 App
.screen
.init
{width
=75, height
=80}
461 Editor_state
= edit
.initialize_test_state()
463 Editor_state
.lines
= load_array
{"madam I'm adam"}
464 Text
.redraw_all(Editor_state
)
465 Editor_state
.cursor1
= {line
=1, pos
=1}
466 Editor_state
.screen_top1
= {line
=1, pos
=1}
467 edit
.draw(Editor_state
)
468 local y
= Editor_state
.top
469 App
.screen
.check(y
, 'madam ', 'baseline/screen:1')
470 y
= y
+ Editor_state
.line_height
471 App
.screen
.check(y
, "I'm ad", 'baseline/screen:2')
472 y
= y
+ Editor_state
.line_height
473 -- click past end of second screen line
474 edit
.run_after_mouse_click(Editor_state
, App
.screen
.width
-2,y
-2, 1)
475 -- cursor moves to end of screen line (one more than final character shown)
476 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
477 check_eq(Editor_state
.cursor1
.pos
, 13, 'cursor:pos')
480 function test_click_on_wrapping_line_rendered_from_partway_at_top_of_screen()
481 -- display a wrapping line from its second screen line
482 App
.screen
.init
{width
=75, height
=80}
483 Editor_state
= edit
.initialize_test_state()
485 Editor_state
.lines
= load_array
{"madam I'm adam"}
486 Text
.redraw_all(Editor_state
)
487 Editor_state
.cursor1
= {line
=1, pos
=8}
488 Editor_state
.screen_top1
= {line
=1, pos
=7}
489 edit
.draw(Editor_state
)
490 local y
= Editor_state
.top
491 App
.screen
.check(y
, "I'm ad", 'baseline/screen:2')
492 y
= y
+ Editor_state
.line_height
493 -- click past end of second screen line
494 edit
.run_after_mouse_click(Editor_state
, App
.screen
.width
-2,y
-2, 1)
495 -- cursor moves to end of screen line (one more than final character shown)
496 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
497 check_eq(Editor_state
.cursor1
.pos
, 13, 'cursor:pos')
500 function test_click_past_end_of_wrapping_line()
501 -- display a wrapping line
502 App
.screen
.init
{width
=75, height
=80}
503 Editor_state
= edit
.initialize_test_state()
505 Editor_state
.lines
= load_array
{"madam I'm adam"}
506 Text
.redraw_all(Editor_state
)
507 Editor_state
.cursor1
= {line
=1, pos
=1}
508 Editor_state
.screen_top1
= {line
=1, pos
=1}
509 edit
.draw(Editor_state
)
510 local y
= Editor_state
.top
511 App
.screen
.check(y
, 'madam ', 'baseline/screen:1')
512 y
= y
+ Editor_state
.line_height
513 App
.screen
.check(y
, "I'm ad", 'baseline/screen:2')
514 y
= y
+ Editor_state
.line_height
515 App
.screen
.check(y
, 'am', 'baseline/screen:3')
516 y
= y
+ Editor_state
.line_height
517 -- click past the end of it
518 edit
.run_after_mouse_click(Editor_state
, App
.screen
.width
-2,y
-2, 1)
519 -- cursor moves to end of line
520 check_eq(Editor_state
.cursor1
.pos
, 15, 'cursor') -- one more than the number of UTF-8 code-points
523 function test_click_past_end_of_wrapping_line_containing_non_ascii()
524 -- display a wrapping line containing non-ASCII
525 App
.screen
.init
{width
=75, height
=80}
526 Editor_state
= edit
.initialize_test_state()
528 Editor_state
.lines
= load_array
{'madam I’m adam'} -- notice the non-ASCII apostrophe
529 Text
.redraw_all(Editor_state
)
530 Editor_state
.cursor1
= {line
=1, pos
=1}
531 Editor_state
.screen_top1
= {line
=1, pos
=1}
532 edit
.draw(Editor_state
)
533 local y
= Editor_state
.top
534 App
.screen
.check(y
, 'madam ', 'baseline/screen:1')
535 y
= y
+ Editor_state
.line_height
536 App
.screen
.check(y
, 'I’m ad', 'baseline/screen:2')
537 y
= y
+ Editor_state
.line_height
538 App
.screen
.check(y
, 'am', 'baseline/screen:3')
539 y
= y
+ Editor_state
.line_height
540 -- click past the end of it
541 edit
.run_after_mouse_click(Editor_state
, App
.screen
.width
-2,y
-2, 1)
542 -- cursor moves to end of line
543 check_eq(Editor_state
.cursor1
.pos
, 15, 'cursor') -- one more than the number of UTF-8 code-points
546 function test_click_past_end_of_word_wrapping_line()
547 -- display a long line wrapping at a word boundary on a screen of more realistic length
548 App
.screen
.init
{width
=160, height
=80}
549 Editor_state
= edit
.initialize_test_state()
551 -- 123456789012345678901
552 Editor_state
.lines
= load_array
{'the quick brown fox jumped over the lazy dog'}
553 Text
.redraw_all(Editor_state
)
554 Editor_state
.cursor1
= {line
=1, pos
=1}
555 Editor_state
.screen_top1
= {line
=1, pos
=1}
556 edit
.draw(Editor_state
)
557 local y
= Editor_state
.top
558 App
.screen
.check(y
, 'the quick brown fox ', 'baseline/screen:1')
559 y
= y
+ Editor_state
.line_height
560 -- click past the end of the screen line
561 edit
.run_after_mouse_click(Editor_state
, App
.screen
.width
-2,y
-2, 1)
562 -- cursor moves to end of screen line (one more than final character shown)
563 check_eq(Editor_state
.cursor1
.pos
, 21, 'cursor')
566 function test_select_text()
567 -- display a line of text
568 App
.screen
.init
{width
=75, height
=80}
569 Editor_state
= edit
.initialize_test_state()
570 Editor_state
.lines
= load_array
{'abc def'}
571 Text
.redraw_all(Editor_state
)
572 Editor_state
.cursor1
= {line
=1, pos
=1}
573 Editor_state
.screen_top1
= {line
=1, pos
=1}
574 edit
.draw(Editor_state
)
576 App
.fake_key_press('lshift')
577 edit
.run_after_keychord(Editor_state
, 'S-right', 'right')
578 App
.fake_key_release('lshift')
579 edit
.key_release(Editor_state
, 'lshift')
580 -- selection persists even after shift is released
581 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
582 check_eq(Editor_state
.selection1
.pos
, 1, 'selection:pos')
583 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
584 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
587 function test_cursor_movement_without_shift_resets_selection()
588 -- display a line of text with some part selected
589 App
.screen
.init
{width
=75, height
=80}
590 Editor_state
= edit
.initialize_test_state()
591 Editor_state
.lines
= load_array
{'abc'}
592 Text
.redraw_all(Editor_state
)
593 Editor_state
.cursor1
= {line
=1, pos
=1}
594 Editor_state
.selection1
= {line
=1, pos
=2}
595 Editor_state
.screen_top1
= {line
=1, pos
=1}
596 edit
.draw(Editor_state
)
597 -- press an arrow key without shift
598 edit
.run_after_keychord(Editor_state
, 'right', 'right')
599 -- no change to data, selection is reset
600 check_nil(Editor_state
.selection1
.line
, 'check')
601 check_eq(Editor_state
.lines
[1].data
, 'abc', 'data')
604 function test_edit_deletes_selection()
605 -- display a line of text with some part selected
606 App
.screen
.init
{width
=75, height
=80}
607 Editor_state
= edit
.initialize_test_state()
608 Editor_state
.lines
= load_array
{'abc'}
609 Text
.redraw_all(Editor_state
)
610 Editor_state
.cursor1
= {line
=1, pos
=1}
611 Editor_state
.selection1
= {line
=1, pos
=2}
612 Editor_state
.screen_top1
= {line
=1, pos
=1}
613 edit
.draw(Editor_state
)
615 edit
.run_after_text_input(Editor_state
, 'x')
616 -- selected text is deleted and replaced with the key
617 check_eq(Editor_state
.lines
[1].data
, 'xbc', 'check')
620 function test_edit_with_shift_key_deletes_selection()
621 -- display a line of text with some part selected
622 App
.screen
.init
{width
=75, height
=80}
623 Editor_state
= edit
.initialize_test_state()
624 Editor_state
.lines
= load_array
{'abc'}
625 Text
.redraw_all(Editor_state
)
626 Editor_state
.cursor1
= {line
=1, pos
=1}
627 Editor_state
.selection1
= {line
=1, pos
=2}
628 Editor_state
.screen_top1
= {line
=1, pos
=1}
629 edit
.draw(Editor_state
)
630 -- mimic precise keypresses for a capital letter
631 App
.fake_key_press('lshift')
632 edit
.keychord_press(Editor_state
, 'd', 'd')
633 edit
.text_input(Editor_state
, 'D')
634 edit
.key_release(Editor_state
, 'd')
635 App
.fake_key_release('lshift')
636 -- selected text is deleted and replaced with the key
637 check_nil(Editor_state
.selection1
.line
, 'check')
638 check_eq(Editor_state
.lines
[1].data
, 'Dbc', 'data')
641 function test_copy_does_not_reset_selection()
642 -- display a line of text with a selection
643 App
.screen
.init
{width
=75, height
=80}
644 Editor_state
= edit
.initialize_test_state()
645 Editor_state
.lines
= load_array
{'abc'}
646 Text
.redraw_all(Editor_state
)
647 Editor_state
.cursor1
= {line
=1, pos
=1}
648 Editor_state
.selection1
= {line
=1, pos
=2}
649 Editor_state
.screen_top1
= {line
=1, pos
=1}
650 edit
.draw(Editor_state
)
652 edit
.run_after_keychord(Editor_state
, 'C-c', 'c')
653 check_eq(App
.clipboard
, 'a', 'clipboard')
654 -- selection is reset since shift key is not pressed
655 check(Editor_state
.selection1
.line
, 'check')
659 -- display a line of text with some part selected
660 App
.screen
.init
{width
=75, height
=80}
661 Editor_state
= edit
.initialize_test_state()
662 Editor_state
.lines
= load_array
{'abc'}
663 Text
.redraw_all(Editor_state
)
664 Editor_state
.cursor1
= {line
=1, pos
=1}
665 Editor_state
.selection1
= {line
=1, pos
=2}
666 Editor_state
.screen_top1
= {line
=1, pos
=1}
667 edit
.draw(Editor_state
)
669 edit
.run_after_keychord(Editor_state
, 'C-x', 'x')
670 check_eq(App
.clipboard
, 'a', 'clipboard')
671 -- selected text is deleted
672 check_eq(Editor_state
.lines
[1].data
, 'bc', 'data')
675 function test_paste_replaces_selection()
676 -- display a line of text with a selection
677 App
.screen
.init
{width
=75, height
=80}
678 Editor_state
= edit
.initialize_test_state()
679 Editor_state
.lines
= load_array
{'abc', 'def'}
680 Text
.redraw_all(Editor_state
)
681 Editor_state
.cursor1
= {line
=2, pos
=1}
682 Editor_state
.selection1
= {line
=1, pos
=1}
683 Editor_state
.screen_top1
= {line
=1, pos
=1}
684 edit
.draw(Editor_state
)
686 App
.clipboard
= 'xyz'
688 edit
.run_after_keychord(Editor_state
, 'C-v', 'v')
689 -- selection is reset since shift key is not pressed
690 -- selection includes the newline, so it's also deleted
691 check_eq(Editor_state
.lines
[1].data
, 'xyzdef', 'check')
694 function test_deleting_selection_may_scroll()
695 -- display lines 2/3/4
696 App
.screen
.init
{width
=120, height
=60}
697 Editor_state
= edit
.initialize_test_state()
698 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
699 Text
.redraw_all(Editor_state
)
700 Editor_state
.cursor1
= {line
=3, pos
=2}
701 Editor_state
.screen_top1
= {line
=2, pos
=1}
702 edit
.draw(Editor_state
)
703 local y
= Editor_state
.top
704 App
.screen
.check(y
, 'def', 'baseline/screen:1')
705 y
= y
+ Editor_state
.line_height
706 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
707 y
= y
+ Editor_state
.line_height
708 App
.screen
.check(y
, 'jkl', 'baseline/screen:3')
709 -- set up a selection starting above the currently displayed page
710 Editor_state
.selection1
= {line
=1, pos
=2}
712 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
714 check_eq(Editor_state
.screen_top1
.line
, 1, 'check')
715 check_eq(Editor_state
.lines
[1].data
, 'ahi', 'data')
718 function test_edit_wrapping_text()
719 App
.screen
.init
{width
=50, height
=60}
720 Editor_state
= edit
.initialize_test_state()
721 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
722 Text
.redraw_all(Editor_state
)
723 Editor_state
.cursor1
= {line
=2, pos
=4}
724 Editor_state
.screen_top1
= {line
=1, pos
=1}
725 edit
.draw(Editor_state
)
726 edit
.run_after_text_input(Editor_state
, 'g')
727 local y
= Editor_state
.top
728 App
.screen
.check(y
, 'abc', 'screen:1')
729 y
= y
+ Editor_state
.line_height
730 App
.screen
.check(y
, 'de', 'screen:2')
731 y
= y
+ Editor_state
.line_height
732 App
.screen
.check(y
, 'fg', 'screen:3')
735 function test_insert_newline()
736 -- display a few lines
737 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
738 Editor_state
= edit
.initialize_test_state()
739 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
740 Text
.redraw_all(Editor_state
)
741 Editor_state
.cursor1
= {line
=1, pos
=2}
742 Editor_state
.screen_top1
= {line
=1, pos
=1}
743 edit
.draw(Editor_state
)
744 local y
= Editor_state
.top
745 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
746 y
= y
+ Editor_state
.line_height
747 App
.screen
.check(y
, 'def', 'baseline/screen:2')
748 y
= y
+ Editor_state
.line_height
749 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
750 -- hitting the enter key splits the line
751 edit
.run_after_keychord(Editor_state
, 'return', 'return')
752 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
753 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
754 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
756 App
.screen
.check(y
, 'a', 'screen:1')
757 y
= y
+ Editor_state
.line_height
758 App
.screen
.check(y
, 'bc', 'screen:2')
759 y
= y
+ Editor_state
.line_height
760 App
.screen
.check(y
, 'def', 'screen:3')
763 function test_insert_newline_at_start_of_line()
765 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
766 Editor_state
= edit
.initialize_test_state()
767 Editor_state
.lines
= load_array
{'abc'}
768 Text
.redraw_all(Editor_state
)
769 Editor_state
.cursor1
= {line
=1, pos
=1}
770 Editor_state
.screen_top1
= {line
=1, pos
=1}
771 -- hitting the enter key splits the line
772 edit
.run_after_keychord(Editor_state
, 'return', 'return')
773 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
774 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
775 check_eq(Editor_state
.lines
[1].data
, '', 'data:1')
776 check_eq(Editor_state
.lines
[2].data
, 'abc', 'data:2')
779 function test_insert_from_clipboard()
780 -- display a few lines
781 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
782 Editor_state
= edit
.initialize_test_state()
783 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
784 Text
.redraw_all(Editor_state
)
785 Editor_state
.cursor1
= {line
=1, pos
=2}
786 Editor_state
.screen_top1
= {line
=1, pos
=1}
787 edit
.draw(Editor_state
)
788 local y
= Editor_state
.top
789 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
790 y
= y
+ Editor_state
.line_height
791 App
.screen
.check(y
, 'def', 'baseline/screen:2')
792 y
= y
+ Editor_state
.line_height
793 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
794 -- paste some text including a newline, check that new line is created
795 App
.clipboard
= 'xy\nz'
796 edit
.run_after_keychord(Editor_state
, 'C-v', 'v')
797 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
798 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
799 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
801 App
.screen
.check(y
, 'axy', 'screen:1')
802 y
= y
+ Editor_state
.line_height
803 App
.screen
.check(y
, 'zbc', 'screen:2')
804 y
= y
+ Editor_state
.line_height
805 App
.screen
.check(y
, 'def', 'screen:3')
808 function test_select_text_using_mouse()
809 App
.screen
.init
{width
=50, height
=60}
810 Editor_state
= edit
.initialize_test_state()
811 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
812 Text
.redraw_all(Editor_state
)
813 Editor_state
.cursor1
= {line
=1, pos
=1}
814 Editor_state
.screen_top1
= {line
=1, pos
=1}
815 Editor_state
.selection1
= {}
816 edit
.draw(Editor_state
) -- populate line_cache.startpos for each line
817 -- press and hold on first location
818 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
819 -- drag and release somewhere else
820 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+Editor_state
.line_height
+5, 1)
821 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
822 check_eq(Editor_state
.selection1
.pos
, 2, 'selection:pos')
823 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
824 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
827 function test_select_text_using_mouse_starting_above_text()
828 App
.screen
.init
{width
=50, height
=60}
829 Editor_state
= edit
.initialize_test_state()
830 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
831 Text
.redraw_all(Editor_state
)
832 Editor_state
.cursor1
= {line
=1, pos
=1}
833 Editor_state
.screen_top1
= {line
=1, pos
=1}
834 Editor_state
.selection1
= {}
835 edit
.draw(Editor_state
) -- populate line_cache.startpos for each line
836 -- press mouse above first line of text
837 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,5, 1)
838 check(Editor_state
.selection1
.line
~= nil, 'selection:line-not-nil')
839 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
840 check_eq(Editor_state
.selection1
.pos
, 1, 'selection:pos')
843 function test_select_text_using_mouse_starting_above_text_wrapping_line()
844 -- first screen line starts in the middle of a line
845 App
.screen
.init
{width
=50, height
=60}
846 Editor_state
= edit
.initialize_test_state()
847 Editor_state
.lines
= load_array
{'abc', 'defgh', 'xyz'}
848 Text
.redraw_all(Editor_state
)
849 Editor_state
.cursor1
= {line
=2, pos
=5}
850 Editor_state
.screen_top1
= {line
=2, pos
=3}
851 -- press mouse above first line of text
852 edit
.draw(Editor_state
)
853 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,5, 1)
854 -- selection is at screen top
855 check(Editor_state
.selection1
.line
~= nil, 'selection:line-not-nil')
856 check_eq(Editor_state
.selection1
.line
, 2, 'selection:line')
857 check_eq(Editor_state
.selection1
.pos
, 3, 'selection:pos')
860 function test_select_text_using_mouse_starting_below_text()
861 -- I'd like to test what happens when a mouse click is below some page of
862 -- text, potentially even in the middle of a line.
863 -- However, it's brittle to set up a text line boundary just right.
864 -- So I'm going to just check things below the bottom of the final line of
865 -- text when it's in the middle of the screen.
866 -- final screen line ends in the middle of screen
867 App
.screen
.init
{width
=50, height
=60}
868 Editor_state
= edit
.initialize_test_state()
869 Editor_state
.lines
= load_array
{'abcde'}
870 Text
.redraw_all(Editor_state
)
871 Editor_state
.cursor1
= {line
=1, pos
=1}
872 Editor_state
.screen_top1
= {line
=1, pos
=1}
873 edit
.draw(Editor_state
)
874 local y
= Editor_state
.top
875 App
.screen
.check(y
, 'ab', 'baseline:screen:1')
876 y
= y
+ Editor_state
.line_height
877 App
.screen
.check(y
, 'cde', 'baseline:screen:2')
878 -- press mouse above first line of text
879 edit
.run_after_mouse_press(Editor_state
, 5,App
.screen
.height
-5, 1)
880 -- selection is past bottom-most text in screen
881 check(Editor_state
.selection1
.line
~= nil, 'selection:line-not-nil')
882 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
883 check_eq(Editor_state
.selection1
.pos
, 6, 'selection:pos')
886 function test_select_text_using_mouse_and_shift()
887 App
.screen
.init
{width
=50, height
=60}
888 Editor_state
= edit
.initialize_test_state()
889 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
890 Text
.redraw_all(Editor_state
)
891 Editor_state
.cursor1
= {line
=1, pos
=1}
892 Editor_state
.screen_top1
= {line
=1, pos
=1}
893 Editor_state
.selection1
= {}
894 edit
.draw(Editor_state
) -- populate line_cache.startpos for each line
895 -- click on first location
896 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
897 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
898 -- hold down shift and click somewhere else
899 App
.fake_key_press('lshift')
900 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+5, 1)
901 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+Editor_state
.line_height
+5, 1)
902 App
.fake_key_release('lshift')
903 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
904 check_eq(Editor_state
.selection1
.pos
, 2, 'selection:pos')
905 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
906 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
909 function test_select_text_repeatedly_using_mouse_and_shift()
910 App
.screen
.init
{width
=50, height
=60}
911 Editor_state
= edit
.initialize_test_state()
912 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
913 Text
.redraw_all(Editor_state
)
914 Text
.redraw_all(Editor_state
)
915 Editor_state
.cursor1
= {line
=1, pos
=1}
916 Editor_state
.screen_top1
= {line
=1, pos
=1}
917 Editor_state
.selection1
= {}
918 edit
.draw(Editor_state
) -- populate line_cache.startpos for each line
919 -- click on first location
920 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
921 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
922 -- hold down shift and click on a second location
923 App
.fake_key_press('lshift')
924 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+5, 1)
925 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+Editor_state
.line_height
+5, 1)
926 -- hold down shift and click at a third location
927 App
.fake_key_press('lshift')
928 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+5, 1)
929 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+Editor_state
.line_height
+5, 1)
930 App
.fake_key_release('lshift')
931 -- selection is between first and third location. forget the second location, not the first.
932 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
933 check_eq(Editor_state
.selection1
.pos
, 2, 'selection:pos')
934 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
935 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
938 function test_select_all_text()
939 -- display a single line of text
940 App
.screen
.init
{width
=75, height
=80}
941 Editor_state
= edit
.initialize_test_state()
942 Editor_state
.lines
= load_array
{'abc def'}
943 Text
.redraw_all(Editor_state
)
944 Editor_state
.cursor1
= {line
=1, pos
=1}
945 Editor_state
.screen_top1
= {line
=1, pos
=1}
946 edit
.draw(Editor_state
)
948 App
.fake_key_press('lctrl')
949 edit
.run_after_keychord(Editor_state
, 'C-a', 'a')
950 App
.fake_key_release('lctrl')
951 edit
.key_release(Editor_state
, 'lctrl')
953 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
954 check_eq(Editor_state
.selection1
.pos
, 1, 'selection:pos')
955 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
956 check_eq(Editor_state
.cursor1
.pos
, 8, 'cursor:pos')
959 function test_cut_without_selection()
960 -- display a few lines
961 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
962 Editor_state
= edit
.initialize_test_state()
963 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
964 Text
.redraw_all(Editor_state
)
965 Editor_state
.cursor1
= {line
=1, pos
=2}
966 Editor_state
.screen_top1
= {line
=1, pos
=1}
967 Editor_state
.selection1
= {}
968 edit
.draw(Editor_state
)
969 -- try to cut without selecting text
970 edit
.run_after_keychord(Editor_state
, 'C-x', 'x')
972 check_nil(Editor_state
.selection1
.line
, 'check')
975 function test_pagedown()
976 App
.screen
.init
{width
=120, height
=45}
977 Editor_state
= edit
.initialize_test_state()
978 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi'}
979 Text
.redraw_all(Editor_state
)
980 Editor_state
.cursor1
= {line
=1, pos
=1}
981 Editor_state
.screen_top1
= {line
=1, pos
=1}
982 -- initially the first two lines are displayed
983 edit
.draw(Editor_state
)
984 local y
= Editor_state
.top
985 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
986 y
= y
+ Editor_state
.line_height
987 App
.screen
.check(y
, 'def', 'baseline/screen:2')
988 -- after pagedown the bottom line becomes the top
989 edit
.run_after_keychord(Editor_state
, 'pagedown', 'pagedown')
990 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
991 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor')
993 App
.screen
.check(y
, 'def', 'screen:1')
994 y
= y
+ Editor_state
.line_height
995 App
.screen
.check(y
, 'ghi', 'screen:2')
998 function test_pagedown_skips_drawings()
999 -- some lines of text with a drawing intermixed
1000 local drawing_width
= 50
1001 App
.screen
.init
{width
=Editor_state
.left
+drawing_width
, height
=80}
1002 Editor_state
= edit
.initialize_test_state()
1003 Editor_state
.lines
= load_array
{'abc', -- height 15
1004 '```lines', '```', -- height 25
1007 Text
.redraw_all(Editor_state
)
1008 check_eq(Editor_state
.lines
[2].mode
, 'drawing', 'baseline/lines')
1009 Editor_state
.cursor1
= {line
=1, pos
=1}
1010 Editor_state
.screen_top1
= {line
=1, pos
=1}
1011 local drawing_height
= Drawing_padding_height
+ drawing_width
/2 -- default
1012 -- initially the screen displays the first line and the drawing
1013 -- 15px margin + 15px line1 + 10px margin + 25px drawing + 10px margin = 75px < screen height 80px
1014 edit
.draw(Editor_state
)
1015 local y
= Editor_state
.top
1016 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1017 -- after pagedown the screen draws the drawing up top
1018 -- 15px margin + 10px margin + 25px drawing + 10px margin + 15px line3 = 75px < screen height 80px
1019 edit
.run_after_keychord(Editor_state
, 'pagedown', 'pagedown')
1020 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1021 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor')
1022 y
= Editor_state
.top
+ drawing_height
1023 App
.screen
.check(y
, 'def', 'screen:1')
1026 function test_pagedown_can_start_from_middle_of_long_wrapping_line()
1027 -- draw a few lines starting from a very long wrapping line
1028 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1029 Editor_state
= edit
.initialize_test_state()
1030 Editor_state
.lines
= load_array
{'abc def ghi jkl mno pqr stu vwx yza bcd efg hij', 'XYZ'}
1031 Text
.redraw_all(Editor_state
)
1032 Editor_state
.cursor1
= {line
=1, pos
=2}
1033 Editor_state
.screen_top1
= {line
=1, pos
=1}
1034 edit
.draw(Editor_state
)
1035 local y
= Editor_state
.top
1036 App
.screen
.check(y
, 'abc ', 'baseline/screen:1')
1037 y
= y
+ Editor_state
.line_height
1038 App
.screen
.check(y
, 'def ', 'baseline/screen:2')
1039 y
= y
+ Editor_state
.line_height
1040 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3')
1041 -- after pagedown we scroll down the very long wrapping line
1042 edit
.run_after_keychord(Editor_state
, 'pagedown', 'pagedown')
1043 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top:line')
1044 check_eq(Editor_state
.screen_top1
.pos
, 9, 'screen_top:pos')
1045 y
= Editor_state
.top
1046 App
.screen
.check(y
, 'ghi ', 'screen:1')
1047 y
= y
+ Editor_state
.line_height
1048 App
.screen
.check(y
, 'jkl ', 'screen:2')
1049 y
= y
+ Editor_state
.line_height
1050 if Version
== '12.0' then
1051 -- HACK: Maybe v12.0 uses a different font? Strange that it only causes
1052 -- issues in a couple of places.
1053 -- We'll need to rethink our tests if issues like this start to multiply.
1054 App
.screen
.check(y
, 'mno ', 'screen:3')
1056 App
.screen
.check(y
, 'mn', 'screen:3')
1060 function test_pagedown_never_moves_up()
1061 -- draw the final screen line of a wrapping line
1062 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1063 Editor_state
= edit
.initialize_test_state()
1064 Editor_state
.lines
= load_array
{'abc def ghi'}
1065 Text
.redraw_all(Editor_state
)
1066 Editor_state
.cursor1
= {line
=1, pos
=9}
1067 Editor_state
.screen_top1
= {line
=1, pos
=9}
1068 edit
.draw(Editor_state
)
1069 -- pagedown makes no change
1070 edit
.run_after_keychord(Editor_state
, 'pagedown', 'pagedown')
1071 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top:line')
1072 check_eq(Editor_state
.screen_top1
.pos
, 9, 'screen_top:pos')
1075 function test_down_arrow_moves_cursor()
1076 App
.screen
.init
{width
=120, height
=60}
1077 Editor_state
= edit
.initialize_test_state()
1078 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1079 Text
.redraw_all(Editor_state
)
1080 Editor_state
.cursor1
= {line
=1, pos
=1}
1081 Editor_state
.screen_top1
= {line
=1, pos
=1}
1082 -- initially the first three lines are displayed
1083 edit
.draw(Editor_state
)
1084 local y
= Editor_state
.top
1085 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1086 y
= y
+ Editor_state
.line_height
1087 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1088 y
= y
+ Editor_state
.line_height
1089 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1090 -- after hitting the down arrow, the cursor moves down by 1 line
1091 edit
.run_after_keychord(Editor_state
, 'down', 'down')
1092 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1093 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor')
1094 -- the screen is unchanged
1095 y
= Editor_state
.top
1096 App
.screen
.check(y
, 'abc', 'screen:1')
1097 y
= y
+ Editor_state
.line_height
1098 App
.screen
.check(y
, 'def', 'screen:2')
1099 y
= y
+ Editor_state
.line_height
1100 App
.screen
.check(y
, 'ghi', 'screen:3')
1103 function test_down_arrow_skips_drawing()
1104 -- some lines of text with a drawing intermixed
1105 local drawing_width
= 50
1106 App
.screen
.init
{width
=Editor_state
.left
+drawing_width
, height
=100}
1107 Editor_state
= edit
.initialize_test_state()
1108 Editor_state
.lines
= load_array
{'abc', -- height 15
1109 '```lines', '```', -- height 25
1111 Text
.redraw_all(Editor_state
)
1112 Editor_state
.cursor1
= {line
=1, pos
=1}
1113 Editor_state
.screen_top1
= {line
=1, pos
=1}
1114 edit
.draw(Editor_state
)
1115 local y
= Editor_state
.top
1116 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1117 y
= y
+ Editor_state
.line_height
1118 local drawing_height
= Drawing_padding_height
+ drawing_width
/2 -- default
1119 y
= y
+ drawing_height
1120 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1121 check(Editor_state
.cursor_x
, 'baseline/cursor_x')
1122 -- after hitting the down arrow the cursor moves down by 2 lines, skipping the drawing
1123 edit
.run_after_keychord(Editor_state
, 'down', 'down')
1124 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor')
1127 function test_down_arrow_scrolls_down_by_one_line()
1128 -- display the first three lines with the cursor on the bottom line
1129 App
.screen
.init
{width
=120, height
=60}
1130 Editor_state
= edit
.initialize_test_state()
1131 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1132 Text
.redraw_all(Editor_state
)
1133 Editor_state
.cursor1
= {line
=3, pos
=1}
1134 Editor_state
.screen_top1
= {line
=1, pos
=1}
1135 edit
.draw(Editor_state
)
1136 local y
= Editor_state
.top
1137 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1138 y
= y
+ Editor_state
.line_height
1139 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1140 y
= y
+ Editor_state
.line_height
1141 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1142 -- after hitting the down arrow the screen scrolls down by one line
1143 edit
.run_after_keychord(Editor_state
, 'down', 'down')
1144 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1145 check_eq(Editor_state
.cursor1
.line
, 4, 'cursor')
1146 y
= Editor_state
.top
1147 App
.screen
.check(y
, 'def', 'screen:1')
1148 y
= y
+ Editor_state
.line_height
1149 App
.screen
.check(y
, 'ghi', 'screen:2')
1150 y
= y
+ Editor_state
.line_height
1151 App
.screen
.check(y
, 'jkl', 'screen:3')
1154 function test_down_arrow_scrolls_down_by_one_screen_line()
1155 -- display the first three lines with the cursor on the bottom line
1156 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1157 Editor_state
= edit
.initialize_test_state()
1158 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1159 Text
.redraw_all(Editor_state
)
1160 Editor_state
.cursor1
= {line
=3, pos
=1}
1161 Editor_state
.screen_top1
= {line
=1, pos
=1}
1162 edit
.draw(Editor_state
)
1163 local y
= Editor_state
.top
1164 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1165 y
= y
+ Editor_state
.line_height
1166 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1167 y
= y
+ Editor_state
.line_height
1168 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1169 -- after hitting the down arrow the screen scrolls down by one line
1170 edit
.run_after_keychord(Editor_state
, 'down', 'down')
1171 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1172 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1173 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1174 y
= Editor_state
.top
1175 App
.screen
.check(y
, 'def', 'screen:1')
1176 y
= y
+ Editor_state
.line_height
1177 App
.screen
.check(y
, 'ghi ', 'screen:2')
1178 y
= y
+ Editor_state
.line_height
1179 App
.screen
.check(y
, 'jkl', 'screen:3')
1182 function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_word()
1183 -- display the first three lines with the cursor on the bottom line
1184 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1185 Editor_state
= edit
.initialize_test_state()
1186 Editor_state
.lines
= load_array
{'abc', 'def', 'ghijkl', 'mno'}
1187 Text
.redraw_all(Editor_state
)
1188 Editor_state
.cursor1
= {line
=3, pos
=1}
1189 Editor_state
.screen_top1
= {line
=1, pos
=1}
1190 edit
.draw(Editor_state
)
1191 local y
= Editor_state
.top
1192 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1193 y
= y
+ Editor_state
.line_height
1194 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1195 y
= y
+ Editor_state
.line_height
1196 App
.screen
.check(y
, 'ghij', 'baseline/screen:3')
1197 -- after hitting the down arrow the screen scrolls down by one line
1198 edit
.run_after_keychord(Editor_state
, 'down', 'down')
1199 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1200 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1201 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1202 y
= Editor_state
.top
1203 App
.screen
.check(y
, 'def', 'screen:1')
1204 y
= y
+ Editor_state
.line_height
1205 App
.screen
.check(y
, 'ghij', 'screen:2')
1206 y
= y
+ Editor_state
.line_height
1207 App
.screen
.check(y
, 'kl', 'screen:3')
1210 function test_pagedown_followed_by_down_arrow_does_not_scroll_screen_up()
1211 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1212 Editor_state
= edit
.initialize_test_state()
1213 Editor_state
.lines
= load_array
{'abc', 'def', 'ghijkl', 'mno'}
1214 Text
.redraw_all(Editor_state
)
1215 Editor_state
.cursor1
= {line
=3, pos
=1}
1216 Editor_state
.screen_top1
= {line
=1, pos
=1}
1217 edit
.draw(Editor_state
)
1218 local y
= Editor_state
.top
1219 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1220 y
= y
+ Editor_state
.line_height
1221 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1222 y
= y
+ Editor_state
.line_height
1223 App
.screen
.check(y
, 'ghij', 'baseline/screen:3')
1224 -- after hitting pagedown the screen scrolls down to start of a long line
1225 edit
.run_after_keychord(Editor_state
, 'pagedown', 'pagedown')
1226 check_eq(Editor_state
.screen_top1
.line
, 3, 'baseline2/screen_top')
1227 check_eq(Editor_state
.cursor1
.line
, 3, 'baseline2/cursor:line')
1228 check_eq(Editor_state
.cursor1
.pos
, 1, 'baseline2/cursor:pos')
1229 -- after hitting down arrow the screen doesn't scroll down further, and certainly doesn't scroll up
1230 edit
.run_after_keychord(Editor_state
, 'down', 'down')
1231 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top')
1232 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1233 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1234 y
= Editor_state
.top
1235 App
.screen
.check(y
, 'ghij', 'screen:1')
1236 y
= y
+ Editor_state
.line_height
1237 App
.screen
.check(y
, 'kl', 'screen:2')
1238 y
= y
+ Editor_state
.line_height
1239 App
.screen
.check(y
, 'mno', 'screen:3')
1242 function test_up_arrow_moves_cursor()
1243 -- display the first 3 lines with the cursor on the bottom line
1244 App
.screen
.init
{width
=120, height
=60}
1245 Editor_state
= edit
.initialize_test_state()
1246 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1247 Text
.redraw_all(Editor_state
)
1248 Editor_state
.cursor1
= {line
=3, pos
=1}
1249 Editor_state
.screen_top1
= {line
=1, pos
=1}
1250 edit
.draw(Editor_state
)
1251 local y
= Editor_state
.top
1252 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1253 y
= y
+ Editor_state
.line_height
1254 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1255 y
= y
+ Editor_state
.line_height
1256 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1257 -- after hitting the up arrow the cursor moves up by 1 line
1258 edit
.run_after_keychord(Editor_state
, 'up', 'up')
1259 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1260 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor')
1261 -- the screen is unchanged
1262 y
= Editor_state
.top
1263 App
.screen
.check(y
, 'abc', 'screen:1')
1264 y
= y
+ Editor_state
.line_height
1265 App
.screen
.check(y
, 'def', 'screen:2')
1266 y
= y
+ Editor_state
.line_height
1267 App
.screen
.check(y
, 'ghi', 'screen:3')
1270 function test_up_arrow_skips_drawing()
1271 -- some lines of text with a drawing intermixed
1272 local drawing_width
= 50
1273 App
.screen
.init
{width
=Editor_state
.left
+drawing_width
, height
=100}
1274 Editor_state
= edit
.initialize_test_state()
1275 Editor_state
.lines
= load_array
{'abc', -- height 15
1276 '```lines', '```', -- height 25
1278 Text
.redraw_all(Editor_state
)
1279 Editor_state
.cursor1
= {line
=3, pos
=1}
1280 Editor_state
.screen_top1
= {line
=1, pos
=1}
1281 edit
.draw(Editor_state
)
1282 local y
= Editor_state
.top
1283 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1284 y
= y
+ Editor_state
.line_height
1285 local drawing_height
= Drawing_padding_height
+ drawing_width
/2 -- default
1286 y
= y
+ drawing_height
1287 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1288 check(Editor_state
.cursor_x
, 'baseline/cursor_x')
1289 -- after hitting the up arrow the cursor moves up by 2 lines, skipping the drawing
1290 edit
.run_after_keychord(Editor_state
, 'up', 'up')
1291 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1294 function test_up_arrow_scrolls_up_by_one_line()
1295 -- display the lines 2/3/4 with the cursor on line 2
1296 App
.screen
.init
{width
=120, height
=60}
1297 Editor_state
= edit
.initialize_test_state()
1298 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1299 Text
.redraw_all(Editor_state
)
1300 Editor_state
.cursor1
= {line
=2, pos
=1}
1301 Editor_state
.screen_top1
= {line
=2, pos
=1}
1302 edit
.draw(Editor_state
)
1303 local y
= Editor_state
.top
1304 App
.screen
.check(y
, 'def', 'baseline/screen:1')
1305 y
= y
+ Editor_state
.line_height
1306 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
1307 y
= y
+ Editor_state
.line_height
1308 App
.screen
.check(y
, 'jkl', 'baseline/screen:3')
1309 -- after hitting the up arrow the screen scrolls up by one line
1310 edit
.run_after_keychord(Editor_state
, 'up', 'up')
1311 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1312 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1313 y
= Editor_state
.top
1314 App
.screen
.check(y
, 'abc', 'screen:1')
1315 y
= y
+ Editor_state
.line_height
1316 App
.screen
.check(y
, 'def', 'screen:2')
1317 y
= y
+ Editor_state
.line_height
1318 App
.screen
.check(y
, 'ghi', 'screen:3')
1321 function test_up_arrow_scrolls_up_by_one_line_skipping_drawing()
1322 -- display lines 3/4/5 with a drawing just off screen at line 2
1323 App
.screen
.init
{width
=120, height
=60}
1324 Editor_state
= edit
.initialize_test_state()
1325 Editor_state
.lines
= load_array
{'abc', '```lines', '```', 'def', 'ghi', 'jkl'}
1326 Text
.redraw_all(Editor_state
)
1327 Editor_state
.cursor1
= {line
=3, pos
=1}
1328 Editor_state
.screen_top1
= {line
=3, pos
=1}
1329 edit
.draw(Editor_state
)
1330 local y
= Editor_state
.top
1331 App
.screen
.check(y
, 'def', 'baseline/screen:1')
1332 y
= y
+ Editor_state
.line_height
1333 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
1334 y
= y
+ Editor_state
.line_height
1335 App
.screen
.check(y
, 'jkl', 'baseline/screen:3')
1336 -- after hitting the up arrow the screen scrolls up to previous text line
1337 edit
.run_after_keychord(Editor_state
, 'up', 'up')
1338 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1339 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1342 function test_up_arrow_scrolls_up_by_one_screen_line()
1343 -- display lines starting from second screen line of a line
1344 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1345 Editor_state
= edit
.initialize_test_state()
1346 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1347 Text
.redraw_all(Editor_state
)
1348 Editor_state
.cursor1
= {line
=3, pos
=6}
1349 Editor_state
.screen_top1
= {line
=3, pos
=5}
1350 edit
.draw(Editor_state
)
1351 local y
= Editor_state
.top
1352 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1353 y
= y
+ Editor_state
.line_height
1354 App
.screen
.check(y
, 'mno', 'baseline/screen:2')
1355 -- after hitting the up arrow the screen scrolls up to first screen line
1356 edit
.run_after_keychord(Editor_state
, 'up', 'up')
1357 y
= Editor_state
.top
1358 App
.screen
.check(y
, 'ghi ', 'screen:1')
1359 y
= y
+ Editor_state
.line_height
1360 App
.screen
.check(y
, 'jkl', 'screen:2')
1361 y
= y
+ Editor_state
.line_height
1362 App
.screen
.check(y
, 'mno', 'screen:3')
1363 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top:line')
1364 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1365 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1366 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1369 function test_up_arrow_scrolls_up_to_final_screen_line()
1370 -- display lines starting just after a long line
1371 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1372 Editor_state
= edit
.initialize_test_state()
1373 Editor_state
.lines
= load_array
{'abc def', 'ghi', 'jkl', 'mno'}
1374 Text
.redraw_all(Editor_state
)
1375 Editor_state
.cursor1
= {line
=2, pos
=1}
1376 Editor_state
.screen_top1
= {line
=2, pos
=1}
1377 edit
.draw(Editor_state
)
1378 local y
= Editor_state
.top
1379 App
.screen
.check(y
, 'ghi', 'baseline/screen:1')
1380 y
= y
+ Editor_state
.line_height
1381 App
.screen
.check(y
, 'jkl', 'baseline/screen:2')
1382 y
= y
+ Editor_state
.line_height
1383 App
.screen
.check(y
, 'mno', 'baseline/screen:3')
1384 -- after hitting the up arrow the screen scrolls up to final screen line of previous line
1385 edit
.run_after_keychord(Editor_state
, 'up', 'up')
1386 y
= Editor_state
.top
1387 App
.screen
.check(y
, 'def', 'screen:1')
1388 y
= y
+ Editor_state
.line_height
1389 App
.screen
.check(y
, 'ghi', 'screen:2')
1390 y
= y
+ Editor_state
.line_height
1391 App
.screen
.check(y
, 'jkl', 'screen:3')
1392 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top:line')
1393 check_eq(Editor_state
.screen_top1
.pos
, 5, 'screen_top:pos')
1394 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1395 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1398 function test_up_arrow_scrolls_up_to_empty_line()
1399 -- display a screenful of text with an empty line just above it outside the screen
1400 App
.screen
.init
{width
=120, height
=60}
1401 Editor_state
= edit
.initialize_test_state()
1402 Editor_state
.lines
= load_array
{'', 'abc', 'def', 'ghi', 'jkl'}
1403 Text
.redraw_all(Editor_state
)
1404 Editor_state
.cursor1
= {line
=2, pos
=1}
1405 Editor_state
.screen_top1
= {line
=2, pos
=1}
1406 edit
.draw(Editor_state
)
1407 local y
= Editor_state
.top
1408 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1409 y
= y
+ Editor_state
.line_height
1410 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1411 y
= y
+ Editor_state
.line_height
1412 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1413 -- after hitting the up arrow the screen scrolls up by one line
1414 edit
.run_after_keychord(Editor_state
, 'up', 'up')
1415 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1416 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1417 y
= Editor_state
.top
1419 y
= y
+ Editor_state
.line_height
1420 App
.screen
.check(y
, 'abc', 'screen:2')
1421 y
= y
+ Editor_state
.line_height
1422 App
.screen
.check(y
, 'def', 'screen:3')
1425 function test_pageup()
1426 App
.screen
.init
{width
=120, height
=45}
1427 Editor_state
= edit
.initialize_test_state()
1428 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi'}
1429 Text
.redraw_all(Editor_state
)
1430 Editor_state
.cursor1
= {line
=2, pos
=1}
1431 Editor_state
.screen_top1
= {line
=2, pos
=1}
1432 -- initially the last two lines are displayed
1433 edit
.draw(Editor_state
)
1434 local y
= Editor_state
.top
1435 App
.screen
.check(y
, 'def', 'baseline/screen:1')
1436 y
= y
+ Editor_state
.line_height
1437 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
1438 -- after pageup the cursor goes to first line
1439 edit
.run_after_keychord(Editor_state
, 'pageup', 'pageup')
1440 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1441 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1442 y
= Editor_state
.top
1443 App
.screen
.check(y
, 'abc', 'screen:1')
1444 y
= y
+ Editor_state
.line_height
1445 App
.screen
.check(y
, 'def', 'screen:2')
1448 function test_pageup_scrolls_up_by_screen_line()
1449 -- display the first three lines with the cursor on the bottom line
1450 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1451 Editor_state
= edit
.initialize_test_state()
1452 Editor_state
.lines
= load_array
{'abc def', 'ghi', 'jkl', 'mno'}
1453 Text
.redraw_all(Editor_state
)
1454 Editor_state
.cursor1
= {line
=2, pos
=1}
1455 Editor_state
.screen_top1
= {line
=2, pos
=1}
1456 edit
.draw(Editor_state
)
1457 local y
= Editor_state
.top
1458 App
.screen
.check(y
, 'ghi', 'baseline/screen:1')
1459 y
= y
+ Editor_state
.line_height
1460 App
.screen
.check(y
, 'jkl', 'baseline/screen:2')
1461 y
= y
+ Editor_state
.line_height
1462 App
.screen
.check(y
, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1463 -- after hitting the page-up key the screen scrolls up to top
1464 edit
.run_after_keychord(Editor_state
, 'pageup', 'pageup')
1465 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1466 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1467 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1468 y
= Editor_state
.top
1469 App
.screen
.check(y
, 'abc ', 'screen:1')
1470 y
= y
+ Editor_state
.line_height
1471 App
.screen
.check(y
, 'def', 'screen:2')
1472 y
= y
+ Editor_state
.line_height
1473 App
.screen
.check(y
, 'ghi', 'screen:3')
1476 function test_pageup_scrolls_up_from_middle_screen_line()
1477 -- display a few lines starting from the middle of a line (Editor_state.cursor1.pos > 1)
1478 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1479 Editor_state
= edit
.initialize_test_state()
1480 Editor_state
.lines
= load_array
{'abc def', 'ghi jkl', 'mno'}
1481 Text
.redraw_all(Editor_state
)
1482 Editor_state
.cursor1
= {line
=2, pos
=5}
1483 Editor_state
.screen_top1
= {line
=2, pos
=5}
1484 edit
.draw(Editor_state
)
1485 local y
= Editor_state
.top
1486 App
.screen
.check(y
, 'jkl', 'baseline/screen:2')
1487 y
= y
+ Editor_state
.line_height
1488 App
.screen
.check(y
, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1489 -- after hitting the page-up key the screen scrolls up to top
1490 edit
.run_after_keychord(Editor_state
, 'pageup', 'pageup')
1491 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1492 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1493 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1494 y
= Editor_state
.top
1495 App
.screen
.check(y
, 'abc ', 'screen:1')
1496 y
= y
+ Editor_state
.line_height
1497 App
.screen
.check(y
, 'def', 'screen:2')
1498 y
= y
+ Editor_state
.line_height
1499 App
.screen
.check(y
, 'ghi ', 'screen:3')
1502 function test_enter_on_bottom_line_scrolls_down()
1503 -- display a few lines with cursor on bottom line
1504 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1505 Editor_state
= edit
.initialize_test_state()
1506 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1507 Text
.redraw_all(Editor_state
)
1508 Editor_state
.cursor1
= {line
=3, pos
=2}
1509 Editor_state
.screen_top1
= {line
=1, pos
=1}
1510 edit
.draw(Editor_state
)
1511 local y
= Editor_state
.top
1512 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1513 y
= y
+ Editor_state
.line_height
1514 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1515 y
= y
+ Editor_state
.line_height
1516 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1517 -- after hitting the enter key the screen scrolls down
1518 edit
.run_after_keychord(Editor_state
, 'return', 'return')
1519 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1520 check_eq(Editor_state
.cursor1
.line
, 4, 'cursor:line')
1521 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1522 y
= Editor_state
.top
1523 App
.screen
.check(y
, 'def', 'screen:1')
1524 y
= y
+ Editor_state
.line_height
1525 App
.screen
.check(y
, 'g', 'screen:2')
1526 y
= y
+ Editor_state
.line_height
1527 App
.screen
.check(y
, 'hi', 'screen:3')
1530 function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1531 -- display just the bottom line on screen
1532 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1533 Editor_state
= edit
.initialize_test_state()
1534 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1535 Text
.redraw_all(Editor_state
)
1536 Editor_state
.cursor1
= {line
=4, pos
=2}
1537 Editor_state
.screen_top1
= {line
=4, pos
=1}
1538 edit
.draw(Editor_state
)
1539 local y
= Editor_state
.top
1540 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1541 -- after hitting the enter key the screen does not scroll down
1542 edit
.run_after_keychord(Editor_state
, 'return', 'return')
1543 check_eq(Editor_state
.screen_top1
.line
, 4, 'screen_top')
1544 check_eq(Editor_state
.cursor1
.line
, 5, 'cursor:line')
1545 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1546 y
= Editor_state
.top
1547 App
.screen
.check(y
, 'j', 'screen:1')
1548 y
= y
+ Editor_state
.line_height
1549 App
.screen
.check(y
, 'kl', 'screen:2')
1552 function test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1553 -- display just an empty bottom line on screen
1554 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1555 Editor_state
= edit
.initialize_test_state()
1556 Editor_state
.lines
= load_array
{'abc', ''}
1557 Text
.redraw_all(Editor_state
)
1558 Editor_state
.cursor1
= {line
=2, pos
=1}
1559 Editor_state
.screen_top1
= {line
=2, pos
=1}
1560 edit
.draw(Editor_state
)
1561 -- after hitting the inserting_text key the screen does not scroll down
1562 edit
.run_after_text_input(Editor_state
, 'a')
1563 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1564 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
1565 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
1566 local y
= Editor_state
.top
1567 App
.screen
.check(y
, 'a', 'screen:1')
1570 function test_typing_on_bottom_line_scrolls_down()
1571 -- display a few lines with cursor on bottom line
1572 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1573 Editor_state
= edit
.initialize_test_state()
1574 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1575 Text
.redraw_all(Editor_state
)
1576 Editor_state
.cursor1
= {line
=3, pos
=4}
1577 Editor_state
.screen_top1
= {line
=1, pos
=1}
1578 edit
.draw(Editor_state
)
1579 local y
= Editor_state
.top
1580 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1581 y
= y
+ Editor_state
.line_height
1582 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1583 y
= y
+ Editor_state
.line_height
1584 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1585 -- after typing something the line wraps and the screen scrolls down
1586 edit
.run_after_text_input(Editor_state
, 'j')
1587 edit
.run_after_text_input(Editor_state
, 'k')
1588 edit
.run_after_text_input(Editor_state
, 'l')
1589 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1590 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1591 check_eq(Editor_state
.cursor1
.pos
, 7, 'cursor:pos')
1592 y
= Editor_state
.top
1593 App
.screen
.check(y
, 'def', 'screen:1')
1594 y
= y
+ Editor_state
.line_height
1595 App
.screen
.check(y
, 'ghij', 'screen:2')
1596 y
= y
+ Editor_state
.line_height
1597 App
.screen
.check(y
, 'kl', 'screen:3')
1600 function test_left_arrow_scrolls_up_in_wrapped_line()
1601 -- display lines starting from second screen line of a line
1602 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1603 Editor_state
= edit
.initialize_test_state()
1604 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1605 Text
.redraw_all(Editor_state
)
1606 Editor_state
.screen_top1
= {line
=3, pos
=5}
1607 -- cursor is at top of screen
1608 Editor_state
.cursor1
= {line
=3, pos
=5}
1609 edit
.draw(Editor_state
)
1610 local y
= Editor_state
.top
1611 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1612 y
= y
+ Editor_state
.line_height
1613 App
.screen
.check(y
, 'mno', 'baseline/screen:2')
1614 -- after hitting the left arrow the screen scrolls up to first screen line
1615 edit
.run_after_keychord(Editor_state
, 'left', 'left')
1616 y
= Editor_state
.top
1617 App
.screen
.check(y
, 'ghi ', 'screen:1')
1618 y
= y
+ Editor_state
.line_height
1619 App
.screen
.check(y
, 'jkl', 'screen:2')
1620 y
= y
+ Editor_state
.line_height
1621 App
.screen
.check(y
, 'mno', 'screen:3')
1622 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top:line')
1623 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1624 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1625 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
1628 function test_right_arrow_scrolls_down_in_wrapped_line()
1629 -- display the first three lines with the cursor on the bottom line
1630 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1631 Editor_state
= edit
.initialize_test_state()
1632 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1633 Text
.redraw_all(Editor_state
)
1634 Editor_state
.screen_top1
= {line
=1, pos
=1}
1635 -- cursor is at bottom right of screen
1636 Editor_state
.cursor1
= {line
=3, pos
=5}
1637 edit
.draw(Editor_state
)
1638 local y
= Editor_state
.top
1639 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1640 y
= y
+ Editor_state
.line_height
1641 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1642 y
= y
+ Editor_state
.line_height
1643 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1644 -- after hitting the right arrow the screen scrolls down by one line
1645 edit
.run_after_keychord(Editor_state
, 'right', 'right')
1646 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1647 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1648 check_eq(Editor_state
.cursor1
.pos
, 6, 'cursor:pos')
1649 y
= Editor_state
.top
1650 App
.screen
.check(y
, 'def', 'screen:1')
1651 y
= y
+ Editor_state
.line_height
1652 App
.screen
.check(y
, 'ghi ', 'screen:2')
1653 y
= y
+ Editor_state
.line_height
1654 App
.screen
.check(y
, 'jkl', 'screen:3')
1657 function test_home_scrolls_up_in_wrapped_line()
1658 -- display lines starting from second screen line of a line
1659 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1660 Editor_state
= edit
.initialize_test_state()
1661 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1662 Text
.redraw_all(Editor_state
)
1663 Editor_state
.screen_top1
= {line
=3, pos
=5}
1664 -- cursor is at top of screen
1665 Editor_state
.cursor1
= {line
=3, pos
=5}
1666 edit
.draw(Editor_state
)
1667 local y
= Editor_state
.top
1668 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1669 y
= y
+ Editor_state
.line_height
1670 App
.screen
.check(y
, 'mno', 'baseline/screen:2')
1671 -- after hitting home the screen scrolls up to first screen line
1672 edit
.run_after_keychord(Editor_state
, 'home', 'home')
1673 y
= Editor_state
.top
1674 App
.screen
.check(y
, 'ghi ', 'screen:1')
1675 y
= y
+ Editor_state
.line_height
1676 App
.screen
.check(y
, 'jkl', 'screen:2')
1677 y
= y
+ Editor_state
.line_height
1678 App
.screen
.check(y
, 'mno', 'screen:3')
1679 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top:line')
1680 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1681 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1682 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1685 function test_end_scrolls_down_in_wrapped_line()
1686 -- display the first three lines with the cursor on the bottom line
1687 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1688 Editor_state
= edit
.initialize_test_state()
1689 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1690 Text
.redraw_all(Editor_state
)
1691 Editor_state
.screen_top1
= {line
=1, pos
=1}
1692 -- cursor is at bottom right of screen
1693 Editor_state
.cursor1
= {line
=3, pos
=5}
1694 edit
.draw(Editor_state
)
1695 local y
= Editor_state
.top
1696 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1697 y
= y
+ Editor_state
.line_height
1698 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1699 y
= y
+ Editor_state
.line_height
1700 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1701 -- after hitting end the screen scrolls down by one line
1702 edit
.run_after_keychord(Editor_state
, 'end', 'end')
1703 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1704 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1705 check_eq(Editor_state
.cursor1
.pos
, 8, 'cursor:pos')
1706 y
= Editor_state
.top
1707 App
.screen
.check(y
, 'def', 'screen:1')
1708 y
= y
+ Editor_state
.line_height
1709 App
.screen
.check(y
, 'ghi ', 'screen:2')
1710 y
= y
+ Editor_state
.line_height
1711 App
.screen
.check(y
, 'jkl', 'screen:3')
1714 function test_position_cursor_on_recently_edited_wrapping_line()
1715 -- draw a line wrapping over 2 screen lines
1716 App
.screen
.init
{width
=100, height
=200}
1717 Editor_state
= edit
.initialize_test_state()
1718 Editor_state
.lines
= load_array
{'abc def ghi jkl mno pqr ', 'xyz'}
1719 Text
.redraw_all(Editor_state
)
1720 Editor_state
.cursor1
= {line
=1, pos
=25}
1721 Editor_state
.screen_top1
= {line
=1, pos
=1}
1722 edit
.draw(Editor_state
)
1723 local y
= Editor_state
.top
1724 App
.screen
.check(y
, 'abc def ghi ', 'baseline1/screen:1')
1725 y
= y
+ Editor_state
.line_height
1726 App
.screen
.check(y
, 'jkl mno pqr ', 'baseline1/screen:2')
1727 y
= y
+ Editor_state
.line_height
1728 App
.screen
.check(y
, 'xyz', 'baseline1/screen:3')
1729 -- add to the line until it's wrapping over 3 screen lines
1730 edit
.run_after_text_input(Editor_state
, 's')
1731 edit
.run_after_text_input(Editor_state
, 't')
1732 edit
.run_after_text_input(Editor_state
, 'u')
1733 check_eq(Editor_state
.cursor1
.pos
, 28, 'cursor:pos')
1734 y
= Editor_state
.top
1735 App
.screen
.check(y
, 'abc def ghi ', 'baseline2/screen:1')
1736 y
= y
+ Editor_state
.line_height
1737 App
.screen
.check(y
, 'jkl mno pqr ', 'baseline2/screen:2')
1738 y
= y
+ Editor_state
.line_height
1739 App
.screen
.check(y
, 'stu', 'baseline2/screen:3')
1740 -- try to move the cursor earlier in the third screen line by clicking the mouse
1741 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+2,Editor_state
.top
+Editor_state
.line_height
*2+5, 1)
1742 -- cursor should move
1743 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1744 check_eq(Editor_state
.cursor1
.pos
, 25, 'cursor:pos')
1747 function test_backspace_can_scroll_up()
1748 -- display the lines 2/3/4 with the cursor on line 2
1749 App
.screen
.init
{width
=120, height
=60}
1750 Editor_state
= edit
.initialize_test_state()
1751 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1752 Text
.redraw_all(Editor_state
)
1753 Editor_state
.cursor1
= {line
=2, pos
=1}
1754 Editor_state
.screen_top1
= {line
=2, pos
=1}
1755 edit
.draw(Editor_state
)
1756 local y
= Editor_state
.top
1757 App
.screen
.check(y
, 'def', 'baseline/screen:1')
1758 y
= y
+ Editor_state
.line_height
1759 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
1760 y
= y
+ Editor_state
.line_height
1761 App
.screen
.check(y
, 'jkl', 'baseline/screen:3')
1762 -- after hitting backspace the screen scrolls up by one line
1763 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1764 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1765 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1766 y
= Editor_state
.top
1767 App
.screen
.check(y
, 'abcdef', 'screen:1')
1768 y
= y
+ Editor_state
.line_height
1769 App
.screen
.check(y
, 'ghi', 'screen:2')
1770 y
= y
+ Editor_state
.line_height
1771 App
.screen
.check(y
, 'jkl', 'screen:3')
1774 function test_backspace_can_scroll_up_screen_line()
1775 -- display lines starting from second screen line of a line
1776 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1777 Editor_state
= edit
.initialize_test_state()
1778 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1779 Text
.redraw_all(Editor_state
)
1780 Editor_state
.cursor1
= {line
=3, pos
=5}
1781 Editor_state
.screen_top1
= {line
=3, pos
=5}
1782 edit
.draw(Editor_state
)
1783 local y
= Editor_state
.top
1784 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1785 y
= y
+ Editor_state
.line_height
1786 App
.screen
.check(y
, 'mno', 'baseline/screen:2')
1787 -- after hitting backspace the screen scrolls up by one screen line
1788 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1789 y
= Editor_state
.top
1790 App
.screen
.check(y
, 'ghij', 'screen:1')
1791 y
= y
+ Editor_state
.line_height
1792 App
.screen
.check(y
, 'kl', 'screen:2')
1793 y
= y
+ Editor_state
.line_height
1794 App
.screen
.check(y
, 'mno', 'screen:3')
1795 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top:line')
1796 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1797 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1798 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
1801 function test_backspace_past_line_boundary()
1802 -- position cursor at start of a (non-first) line
1803 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1804 Editor_state
= edit
.initialize_test_state()
1805 Editor_state
.lines
= load_array
{'abc', 'def'}
1806 Text
.redraw_all(Editor_state
)
1807 Editor_state
.cursor1
= {line
=2, pos
=1}
1808 -- backspace joins with previous line
1809 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1810 check_eq(Editor_state
.lines
[1].data
, 'abcdef', 'check')
1813 -- some tests for operating over selections created using Shift- chords
1814 -- we're just testing delete_selection, and it works the same for all keys
1816 function test_backspace_over_selection()
1817 -- select just one character within a line with cursor before selection
1818 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1819 Editor_state
= edit
.initialize_test_state()
1820 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1821 Text
.redraw_all(Editor_state
)
1822 Editor_state
.cursor1
= {line
=1, pos
=1}
1823 Editor_state
.selection1
= {line
=1, pos
=2}
1824 -- backspace deletes the selected character, even though it's after the cursor
1825 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1826 check_eq(Editor_state
.lines
[1].data
, 'bc', 'data')
1827 -- cursor (remains) at start of selection
1828 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1829 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1830 -- selection is cleared
1831 check_nil(Editor_state
.selection1
.line
, 'selection')
1834 function test_backspace_over_selection_reverse()
1835 -- select just one character within a line with cursor after selection
1836 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1837 Editor_state
= edit
.initialize_test_state()
1838 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1839 Text
.redraw_all(Editor_state
)
1840 Editor_state
.cursor1
= {line
=1, pos
=2}
1841 Editor_state
.selection1
= {line
=1, pos
=1}
1842 -- backspace deletes the selected character
1843 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1844 check_eq(Editor_state
.lines
[1].data
, 'bc', 'data')
1845 -- cursor moves to start of selection
1846 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1847 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1848 -- selection is cleared
1849 check_nil(Editor_state
.selection1
.line
, 'selection')
1852 function test_backspace_over_multiple_lines()
1853 -- select just one character within a line with cursor after selection
1854 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1855 Editor_state
= edit
.initialize_test_state()
1856 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1857 Text
.redraw_all(Editor_state
)
1858 Editor_state
.cursor1
= {line
=1, pos
=2}
1859 Editor_state
.selection1
= {line
=4, pos
=2}
1860 -- backspace deletes the region and joins the remaining portions of lines on either side
1861 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1862 check_eq(Editor_state
.lines
[1].data
, 'akl', 'data:1')
1863 check_eq(Editor_state
.lines
[2].data
, 'mno', 'data:2')
1864 -- cursor remains at start of selection
1865 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1866 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
1867 -- selection is cleared
1868 check_nil(Editor_state
.selection1
.line
, 'selection')
1871 function test_backspace_to_end_of_line()
1872 -- select region from cursor to end of line
1873 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1874 Editor_state
= edit
.initialize_test_state()
1875 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1876 Text
.redraw_all(Editor_state
)
1877 Editor_state
.cursor1
= {line
=1, pos
=2}
1878 Editor_state
.selection1
= {line
=1, pos
=4}
1879 -- backspace deletes rest of line without joining to any other line
1880 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1881 check_eq(Editor_state
.lines
[1].data
, 'a', 'data:1')
1882 check_eq(Editor_state
.lines
[2].data
, 'def', 'data:2')
1883 -- cursor remains at start of selection
1884 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1885 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
1886 -- selection is cleared
1887 check_nil(Editor_state
.selection1
.line
, 'selection')
1890 function test_backspace_to_start_of_line()
1891 -- select region from cursor to start of line
1892 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1893 Editor_state
= edit
.initialize_test_state()
1894 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1895 Text
.redraw_all(Editor_state
)
1896 Editor_state
.cursor1
= {line
=2, pos
=1}
1897 Editor_state
.selection1
= {line
=2, pos
=3}
1898 -- backspace deletes beginning of line without joining to any other line
1899 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1900 check_eq(Editor_state
.lines
[1].data
, 'abc', 'data:1')
1901 check_eq(Editor_state
.lines
[2].data
, 'f', 'data:2')
1902 -- cursor remains at start of selection
1903 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
1904 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1905 -- selection is cleared
1906 check_nil(Editor_state
.selection1
.line
, 'selection')
1909 function test_undo_insert_text()
1910 App
.screen
.init
{width
=120, height
=60}
1911 Editor_state
= edit
.initialize_test_state()
1912 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
1913 Text
.redraw_all(Editor_state
)
1914 Editor_state
.cursor1
= {line
=2, pos
=4}
1915 Editor_state
.screen_top1
= {line
=1, pos
=1}
1916 -- insert a character
1917 edit
.draw(Editor_state
)
1918 edit
.run_after_text_input(Editor_state
, 'g')
1919 check_eq(Editor_state
.cursor1
.line
, 2, 'baseline/cursor:line')
1920 check_eq(Editor_state
.cursor1
.pos
, 5, 'baseline/cursor:pos')
1921 check_nil(Editor_state
.selection1
.line
, 'baseline/selection:line')
1922 check_nil(Editor_state
.selection1
.pos
, 'baseline/selection:pos')
1923 local y
= Editor_state
.top
1924 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1925 y
= y
+ Editor_state
.line_height
1926 App
.screen
.check(y
, 'defg', 'baseline/screen:2')
1927 y
= y
+ Editor_state
.line_height
1928 App
.screen
.check(y
, 'xyz', 'baseline/screen:3')
1930 edit
.run_after_keychord(Editor_state
, 'C-z', 'z')
1931 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
1932 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
1933 check_nil(Editor_state
.selection1
.line
, 'selection:line')
1934 check_nil(Editor_state
.selection1
.pos
, 'selection:pos')
1935 y
= Editor_state
.top
1936 App
.screen
.check(y
, 'abc', 'screen:1')
1937 y
= y
+ Editor_state
.line_height
1938 App
.screen
.check(y
, 'def', 'screen:2')
1939 y
= y
+ Editor_state
.line_height
1940 App
.screen
.check(y
, 'xyz', 'screen:3')
1943 function test_undo_delete_text()
1944 App
.screen
.init
{width
=120, height
=60}
1945 Editor_state
= edit
.initialize_test_state()
1946 Editor_state
.lines
= load_array
{'abc', 'defg', 'xyz'}
1947 Text
.redraw_all(Editor_state
)
1948 Editor_state
.cursor1
= {line
=2, pos
=5}
1949 Editor_state
.screen_top1
= {line
=1, pos
=1}
1950 -- delete a character
1951 edit
.run_after_keychord(Editor_state
, 'backspace', 'backspace')
1952 check_eq(Editor_state
.cursor1
.line
, 2, 'baseline/cursor:line')
1953 check_eq(Editor_state
.cursor1
.pos
, 4, 'baseline/cursor:pos')
1954 check_nil(Editor_state
.selection1
.line
, 'baseline/selection:line')
1955 check_nil(Editor_state
.selection1
.pos
, 'baseline/selection:pos')
1956 local y
= Editor_state
.top
1957 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1958 y
= y
+ Editor_state
.line_height
1959 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1960 y
= y
+ Editor_state
.line_height
1961 App
.screen
.check(y
, 'xyz', 'baseline/screen:3')
1963 --? -- after undo, the backspaced key is selected
1964 edit
.run_after_keychord(Editor_state
, 'C-z', 'z')
1965 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
1966 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1967 check_nil(Editor_state
.selection1
.line
, 'selection:line')
1968 check_nil(Editor_state
.selection1
.pos
, 'selection:pos')
1969 --? check_eq(Editor_state.selection1.line, 2, 'selection:line')
1970 --? check_eq(Editor_state.selection1.pos, 4, 'selection:pos')
1971 y
= Editor_state
.top
1972 App
.screen
.check(y
, 'abc', 'screen:1')
1973 y
= y
+ Editor_state
.line_height
1974 App
.screen
.check(y
, 'defg', 'screen:2')
1975 y
= y
+ Editor_state
.line_height
1976 App
.screen
.check(y
, 'xyz', 'screen:3')
1979 function test_undo_restores_selection()
1980 -- display a line of text with some part selected
1981 App
.screen
.init
{width
=75, height
=80}
1982 Editor_state
= edit
.initialize_test_state()
1983 Editor_state
.lines
= load_array
{'abc'}
1984 Text
.redraw_all(Editor_state
)
1985 Editor_state
.cursor1
= {line
=1, pos
=1}
1986 Editor_state
.selection1
= {line
=1, pos
=2}
1987 Editor_state
.screen_top1
= {line
=1, pos
=1}
1988 edit
.draw(Editor_state
)
1989 -- delete selected text
1990 edit
.run_after_text_input(Editor_state
, 'x')
1991 check_eq(Editor_state
.lines
[1].data
, 'xbc', 'baseline')
1992 check_nil(Editor_state
.selection1
.line
, 'baseline:selection')
1994 edit
.run_after_keychord(Editor_state
, 'C-z', 'z')
1995 edit
.run_after_keychord(Editor_state
, 'C-z', 'z')
1996 -- selection is restored
1997 check_eq(Editor_state
.selection1
.line
, 1, 'line')
1998 check_eq(Editor_state
.selection1
.pos
, 2, 'pos')
2001 function test_search()
2002 App
.screen
.init
{width
=120, height
=60}
2003 Editor_state
= edit
.initialize_test_state()
2004 Editor_state
.lines
= load_array
{'```lines', '```', 'def', 'ghi', '’deg'} -- contains unicode quote in final line
2005 Text
.redraw_all(Editor_state
)
2006 Editor_state
.cursor1
= {line
=1, pos
=1}
2007 Editor_state
.screen_top1
= {line
=1, pos
=1}
2008 edit
.draw(Editor_state
)
2009 -- search for a string
2010 edit
.run_after_keychord(Editor_state
, 'C-f', 'f')
2011 edit
.run_after_text_input(Editor_state
, 'd')
2012 edit
.run_after_keychord(Editor_state
, 'return', 'return')
2013 check_eq(Editor_state
.cursor1
.line
, 2, '1/cursor:line')
2014 check_eq(Editor_state
.cursor1
.pos
, 1, '1/cursor:pos')
2016 Editor_state
.cursor1
= {line
=1, pos
=1}
2017 Editor_state
.screen_top1
= {line
=1, pos
=1}
2018 -- search for second occurrence
2019 edit
.run_after_keychord(Editor_state
, 'C-f', 'f')
2020 edit
.run_after_text_input(Editor_state
, 'de')
2021 edit
.run_after_keychord(Editor_state
, 'down', 'down')
2022 edit
.run_after_keychord(Editor_state
, 'return', 'return')
2023 check_eq(Editor_state
.cursor1
.line
, 4, '2/cursor:line')
2024 check_eq(Editor_state
.cursor1
.pos
, 2, '2/cursor:pos')
2027 function test_search_upwards()
2028 App
.screen
.init
{width
=120, height
=60}
2029 Editor_state
= edit
.initialize_test_state()
2030 Editor_state
.lines
= load_array
{'’abc', 'abd'} -- contains unicode quote
2031 Text
.redraw_all(Editor_state
)
2032 Editor_state
.cursor1
= {line
=2, pos
=1}
2033 Editor_state
.screen_top1
= {line
=1, pos
=1}
2034 edit
.draw(Editor_state
)
2035 -- search for a string
2036 edit
.run_after_keychord(Editor_state
, 'C-f', 'f')
2037 edit
.run_after_text_input(Editor_state
, 'a')
2038 -- search for previous occurrence
2039 edit
.run_after_keychord(Editor_state
, 'up', 'up')
2040 check_eq(Editor_state
.cursor1
.line
, 1, '2/cursor:line')
2041 check_eq(Editor_state
.cursor1
.pos
, 2, '2/cursor:pos')
2044 function test_search_wrap()
2045 App
.screen
.init
{width
=120, height
=60}
2046 Editor_state
= edit
.initialize_test_state()
2047 Editor_state
.lines
= load_array
{'’abc', 'def'} -- contains unicode quote in first line
2048 Text
.redraw_all(Editor_state
)
2049 Editor_state
.cursor1
= {line
=2, pos
=1}
2050 Editor_state
.screen_top1
= {line
=1, pos
=1}
2051 edit
.draw(Editor_state
)
2052 -- search for a string
2053 edit
.run_after_keychord(Editor_state
, 'C-f', 'f')
2054 edit
.run_after_text_input(Editor_state
, 'a')
2055 edit
.run_after_keychord(Editor_state
, 'return', 'return')
2057 check_eq(Editor_state
.cursor1
.line
, 1, '1/cursor:line')
2058 check_eq(Editor_state
.cursor1
.pos
, 2, '1/cursor:pos')
2061 function test_search_wrap_upwards()
2062 App
.screen
.init
{width
=120, height
=60}
2063 Editor_state
= edit
.initialize_test_state()
2064 Editor_state
.lines
= load_array
{'abc ’abd'} -- contains unicode quote
2065 Text
.redraw_all(Editor_state
)
2066 Editor_state
.cursor1
= {line
=1, pos
=1}
2067 Editor_state
.screen_top1
= {line
=1, pos
=1}
2068 edit
.draw(Editor_state
)
2069 -- search upwards for a string
2070 edit
.run_after_keychord(Editor_state
, 'C-f', 'f')
2071 edit
.run_after_text_input(Editor_state
, 'a')
2072 edit
.run_after_keychord(Editor_state
, 'up', 'up')
2074 check_eq(Editor_state
.cursor1
.line
, 1, '1/cursor:line')
2075 check_eq(Editor_state
.cursor1
.pos
, 6, '1/cursor:pos')
2078 function test_search_downwards_from_end_of_line()
2079 App
.screen
.init
{width
=120, height
=60}
2080 Editor_state
= edit
.initialize_test_state()
2081 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi'}
2082 Text
.redraw_all(Editor_state
)
2083 Editor_state
.cursor1
= {line
=1, pos
=4}
2084 Editor_state
.screen_top1
= {line
=1, pos
=1}
2085 edit
.draw(Editor_state
)
2086 -- search for empty string
2087 edit
.run_after_keychord(Editor_state
, 'C-f', 'f')
2088 edit
.run_after_keychord(Editor_state
, 'down', 'down')
2092 function test_search_downwards_from_final_pos_of_line()
2093 App
.screen
.init
{width
=120, height
=60}
2094 Editor_state
= edit
.initialize_test_state()
2095 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi'}
2096 Text
.redraw_all(Editor_state
)
2097 Editor_state
.cursor1
= {line
=1, pos
=3}
2098 Editor_state
.screen_top1
= {line
=1, pos
=1}
2099 edit
.draw(Editor_state
)
2100 -- search for empty string
2101 edit
.run_after_keychord(Editor_state
, 'C-f', 'f')
2102 edit
.run_after_keychord(Editor_state
, 'down', 'down')