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