assume starty can be nil in update
[view.love.git] / text_tests.lua
blob21a085af3eeb54ad89b0d5e8c27b21164f8ff6e0
1 -- major tests for text editing flows
2 -- Arguably this should be called 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_often_shows_start_of_wrapping_line()
1041 -- draw a few lines ending in part of a wrapping line
1042 App.screen.init{width=50, height=60}
1043 Editor_state = edit.initialize_test_state()
1044 Editor_state.lines = load_array{'abc', 'def ghi jkl', 'mno'}
1045 Text.redraw_all(Editor_state)
1046 Editor_state.cursor1 = {line=1, pos=1}
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 start drawing from the bottom _line_ (multiple screen lines)
1057 edit.run_after_keychord(Editor_state, 'pagedown')
1058 check_eq(Editor_state.screen_top1.line, 2, 'screen_top:line')
1059 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1060 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1061 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1062 y = Editor_state.top
1063 App.screen.check(y, 'def ', 'screen:1')
1064 y = y + Editor_state.line_height
1065 App.screen.check(y, 'ghi ', 'screen:2')
1066 y = y + Editor_state.line_height
1067 App.screen.check(y, 'jkl', 'screen:3')
1070 function test_pagedown_can_start_from_middle_of_long_wrapping_line()
1071 -- draw a few lines starting from a very long wrapping line
1072 App.screen.init{width=Editor_state.left+30, height=60}
1073 Editor_state = edit.initialize_test_state()
1074 Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu vwx yza bcd efg hij', 'XYZ'}
1075 Text.redraw_all(Editor_state)
1076 Editor_state.cursor1 = {line=1, pos=2}
1077 Editor_state.screen_top1 = {line=1, pos=1}
1078 Editor_state.screen_bottom1 = {}
1079 edit.draw(Editor_state)
1080 local y = Editor_state.top
1081 App.screen.check(y, 'abc ', 'baseline/screen:1')
1082 y = y + Editor_state.line_height
1083 App.screen.check(y, 'def ', 'baseline/screen:2')
1084 y = y + Editor_state.line_height
1085 App.screen.check(y, 'ghi ', 'baseline/screen:3')
1086 -- after pagedown we scroll down the very long wrapping line
1087 edit.run_after_keychord(Editor_state, 'pagedown')
1088 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1089 check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
1090 y = Editor_state.top
1091 App.screen.check(y, 'ghi ', 'screen:1')
1092 y = y + Editor_state.line_height
1093 App.screen.check(y, 'jkl ', 'screen:2')
1094 y = y + Editor_state.line_height
1095 App.screen.check(y, 'mn', 'screen:3')
1098 function test_pagedown_never_moves_up()
1099 -- draw the final screen line of a wrapping line
1100 App.screen.init{width=Editor_state.left+30, height=60}
1101 Editor_state = edit.initialize_test_state()
1102 Editor_state.lines = load_array{'abc def ghi'}
1103 Text.redraw_all(Editor_state)
1104 Editor_state.cursor1 = {line=1, pos=9}
1105 Editor_state.screen_top1 = {line=1, pos=9}
1106 Editor_state.screen_bottom1 = {}
1107 edit.draw(Editor_state)
1108 -- pagedown makes no change
1109 edit.run_after_keychord(Editor_state, 'pagedown')
1110 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1111 check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
1114 function test_down_arrow_moves_cursor()
1115 App.screen.init{width=120, height=60}
1116 Editor_state = edit.initialize_test_state()
1117 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1118 Text.redraw_all(Editor_state)
1119 Editor_state.cursor1 = {line=1, pos=1}
1120 Editor_state.screen_top1 = {line=1, pos=1}
1121 Editor_state.screen_bottom1 = {}
1122 -- initially the first three lines are displayed
1123 edit.draw(Editor_state)
1124 local y = Editor_state.top
1125 App.screen.check(y, 'abc', 'baseline/screen:1')
1126 y = y + Editor_state.line_height
1127 App.screen.check(y, 'def', 'baseline/screen:2')
1128 y = y + Editor_state.line_height
1129 App.screen.check(y, 'ghi', 'baseline/screen:3')
1130 -- after hitting the down arrow, the cursor moves down by 1 line
1131 edit.run_after_keychord(Editor_state, 'down')
1132 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1133 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1134 -- the screen is unchanged
1135 y = Editor_state.top
1136 App.screen.check(y, 'abc', 'screen:1')
1137 y = y + Editor_state.line_height
1138 App.screen.check(y, 'def', 'screen:2')
1139 y = y + Editor_state.line_height
1140 App.screen.check(y, 'ghi', 'screen:3')
1143 function test_down_arrow_skips_drawing()
1144 -- some lines of text with a drawing intermixed
1145 local drawing_width = 50
1146 App.screen.init{width=Editor_state.left+drawing_width, height=100}
1147 Editor_state = edit.initialize_test_state()
1148 Editor_state.lines = load_array{'abc', -- height 15
1149 '```lines', '```', -- height 25
1150 'ghi'}
1151 Text.redraw_all(Editor_state)
1152 Editor_state.cursor1 = {line=1, pos=1}
1153 Editor_state.screen_top1 = {line=1, pos=1}
1154 Editor_state.screen_bottom1 = {}
1155 edit.draw(Editor_state)
1156 local y = Editor_state.top
1157 App.screen.check(y, 'abc', 'baseline/screen:1')
1158 y = y + Editor_state.line_height
1159 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1160 y = y + drawing_height
1161 App.screen.check(y, 'ghi', 'baseline/screen:3')
1162 check(Editor_state.cursor_x, 'baseline/cursor_x')
1163 -- after hitting the down arrow the cursor moves down by 2 lines, skipping the drawing
1164 edit.run_after_keychord(Editor_state, 'down')
1165 check_eq(Editor_state.cursor1.line, 3, 'cursor')
1168 function test_down_arrow_scrolls_down_by_one_line()
1169 -- display the first three lines with the cursor on the bottom line
1170 App.screen.init{width=120, height=60}
1171 Editor_state = edit.initialize_test_state()
1172 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1173 Text.redraw_all(Editor_state)
1174 Editor_state.cursor1 = {line=3, pos=1}
1175 Editor_state.screen_top1 = {line=1, pos=1}
1176 Editor_state.screen_bottom1 = {}
1177 edit.draw(Editor_state)
1178 local y = Editor_state.top
1179 App.screen.check(y, 'abc', 'baseline/screen:1')
1180 y = y + Editor_state.line_height
1181 App.screen.check(y, 'def', 'baseline/screen:2')
1182 y = y + Editor_state.line_height
1183 App.screen.check(y, 'ghi', 'baseline/screen:3')
1184 -- after hitting the down arrow the screen scrolls down by one line
1185 edit.run_after_keychord(Editor_state, 'down')
1186 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1187 check_eq(Editor_state.cursor1.line, 4, 'cursor')
1188 y = Editor_state.top
1189 App.screen.check(y, 'def', 'screen:1')
1190 y = y + Editor_state.line_height
1191 App.screen.check(y, 'ghi', 'screen:2')
1192 y = y + Editor_state.line_height
1193 App.screen.check(y, 'jkl', 'screen:3')
1196 function test_down_arrow_scrolls_down_by_one_screen_line()
1197 -- display the first three lines with the cursor on the bottom line
1198 App.screen.init{width=Editor_state.left+30, height=60}
1199 Editor_state = edit.initialize_test_state()
1200 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1201 Text.redraw_all(Editor_state)
1202 Editor_state.cursor1 = {line=3, pos=1}
1203 Editor_state.screen_top1 = {line=1, pos=1}
1204 Editor_state.screen_bottom1 = {}
1205 edit.draw(Editor_state)
1206 local y = Editor_state.top
1207 App.screen.check(y, 'abc', 'baseline/screen:1')
1208 y = y + Editor_state.line_height
1209 App.screen.check(y, 'def', 'baseline/screen:2')
1210 y = y + Editor_state.line_height
1211 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1212 -- after hitting the down arrow the screen scrolls down by one line
1213 edit.run_after_keychord(Editor_state, 'down')
1214 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1215 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1216 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1217 y = Editor_state.top
1218 App.screen.check(y, 'def', 'screen:1')
1219 y = y + Editor_state.line_height
1220 App.screen.check(y, 'ghi ', 'screen:2')
1221 y = y + Editor_state.line_height
1222 App.screen.check(y, 'jkl', 'screen:3')
1225 function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_word()
1226 -- display the first three lines with the cursor on the bottom line
1227 App.screen.init{width=Editor_state.left+30, height=60}
1228 Editor_state = edit.initialize_test_state()
1229 Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
1230 Text.redraw_all(Editor_state)
1231 Editor_state.cursor1 = {line=3, pos=1}
1232 Editor_state.screen_top1 = {line=1, pos=1}
1233 Editor_state.screen_bottom1 = {}
1234 edit.draw(Editor_state)
1235 local y = Editor_state.top
1236 App.screen.check(y, 'abc', 'baseline/screen:1')
1237 y = y + Editor_state.line_height
1238 App.screen.check(y, 'def', 'baseline/screen:2')
1239 y = y + Editor_state.line_height
1240 App.screen.check(y, 'ghij', 'baseline/screen:3')
1241 -- after hitting the down arrow the screen scrolls down by one line
1242 edit.run_after_keychord(Editor_state, 'down')
1243 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1244 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1245 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1246 y = Editor_state.top
1247 App.screen.check(y, 'def', 'screen:1')
1248 y = y + Editor_state.line_height
1249 App.screen.check(y, 'ghij', 'screen:2')
1250 y = y + Editor_state.line_height
1251 App.screen.check(y, 'kl', 'screen:3')
1254 function test_pagedown_followed_by_down_arrow_does_not_scroll_screen_up()
1255 App.screen.init{width=Editor_state.left+30, height=60}
1256 Editor_state = edit.initialize_test_state()
1257 Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
1258 Text.redraw_all(Editor_state)
1259 Editor_state.cursor1 = {line=3, pos=1}
1260 Editor_state.screen_top1 = {line=1, pos=1}
1261 Editor_state.screen_bottom1 = {}
1262 edit.draw(Editor_state)
1263 local y = Editor_state.top
1264 App.screen.check(y, 'abc', 'baseline/screen:1')
1265 y = y + Editor_state.line_height
1266 App.screen.check(y, 'def', 'baseline/screen:2')
1267 y = y + Editor_state.line_height
1268 App.screen.check(y, 'ghij', 'baseline/screen:3')
1269 -- after hitting pagedown the screen scrolls down to start of a long line
1270 edit.run_after_keychord(Editor_state, 'pagedown')
1271 check_eq(Editor_state.screen_top1.line, 3, 'baseline2/screen_top')
1272 check_eq(Editor_state.cursor1.line, 3, 'baseline2/cursor:line')
1273 check_eq(Editor_state.cursor1.pos, 1, 'baseline2/cursor:pos')
1274 -- after hitting down arrow the screen doesn't scroll down further, and certainly doesn't scroll up
1275 edit.run_after_keychord(Editor_state, 'down')
1276 check_eq(Editor_state.screen_top1.line, 3, 'screen_top')
1277 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1278 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1279 y = Editor_state.top
1280 App.screen.check(y, 'ghij', 'screen:1')
1281 y = y + Editor_state.line_height
1282 App.screen.check(y, 'kl', 'screen:2')
1283 y = y + Editor_state.line_height
1284 App.screen.check(y, 'mno', 'screen:3')
1287 function test_up_arrow_moves_cursor()
1288 -- display the first 3 lines with the cursor on the bottom line
1289 App.screen.init{width=120, height=60}
1290 Editor_state = edit.initialize_test_state()
1291 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1292 Text.redraw_all(Editor_state)
1293 Editor_state.cursor1 = {line=3, pos=1}
1294 Editor_state.screen_top1 = {line=1, pos=1}
1295 Editor_state.screen_bottom1 = {}
1296 edit.draw(Editor_state)
1297 local y = Editor_state.top
1298 App.screen.check(y, 'abc', 'baseline/screen:1')
1299 y = y + Editor_state.line_height
1300 App.screen.check(y, 'def', 'baseline/screen:2')
1301 y = y + Editor_state.line_height
1302 App.screen.check(y, 'ghi', 'baseline/screen:3')
1303 -- after hitting the up arrow the cursor moves up by 1 line
1304 edit.run_after_keychord(Editor_state, 'up')
1305 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1306 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1307 -- the screen is unchanged
1308 y = Editor_state.top
1309 App.screen.check(y, 'abc', 'screen:1')
1310 y = y + Editor_state.line_height
1311 App.screen.check(y, 'def', 'screen:2')
1312 y = y + Editor_state.line_height
1313 App.screen.check(y, 'ghi', 'screen:3')
1316 function test_up_arrow_skips_drawing()
1317 -- some lines of text with a drawing intermixed
1318 local drawing_width = 50
1319 App.screen.init{width=Editor_state.left+drawing_width, height=100}
1320 Editor_state = edit.initialize_test_state()
1321 Editor_state.lines = load_array{'abc', -- height 15
1322 '```lines', '```', -- height 25
1323 'ghi'}
1324 Text.redraw_all(Editor_state)
1325 Editor_state.cursor1 = {line=3, pos=1}
1326 Editor_state.screen_top1 = {line=1, pos=1}
1327 Editor_state.screen_bottom1 = {}
1328 edit.draw(Editor_state)
1329 local y = Editor_state.top
1330 App.screen.check(y, 'abc', 'baseline/screen:1')
1331 y = y + Editor_state.line_height
1332 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1333 y = y + drawing_height
1334 App.screen.check(y, 'ghi', 'baseline/screen:3')
1335 check(Editor_state.cursor_x, 'baseline/cursor_x')
1336 -- after hitting the up arrow the cursor moves up by 2 lines, skipping the drawing
1337 edit.run_after_keychord(Editor_state, 'up')
1338 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1341 function test_up_arrow_scrolls_up_by_one_line()
1342 -- display the lines 2/3/4 with the cursor on line 2
1343 App.screen.init{width=120, height=60}
1344 Editor_state = edit.initialize_test_state()
1345 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1346 Text.redraw_all(Editor_state)
1347 Editor_state.cursor1 = {line=2, pos=1}
1348 Editor_state.screen_top1 = {line=2, pos=1}
1349 Editor_state.screen_bottom1 = {}
1350 edit.draw(Editor_state)
1351 local y = Editor_state.top
1352 App.screen.check(y, 'def', 'baseline/screen:1')
1353 y = y + Editor_state.line_height
1354 App.screen.check(y, 'ghi', 'baseline/screen:2')
1355 y = y + Editor_state.line_height
1356 App.screen.check(y, 'jkl', 'baseline/screen:3')
1357 -- after hitting the up arrow the screen scrolls up by one line
1358 edit.run_after_keychord(Editor_state, 'up')
1359 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1360 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1361 y = Editor_state.top
1362 App.screen.check(y, 'abc', 'screen:1')
1363 y = y + Editor_state.line_height
1364 App.screen.check(y, 'def', 'screen:2')
1365 y = y + Editor_state.line_height
1366 App.screen.check(y, 'ghi', 'screen:3')
1369 function test_up_arrow_scrolls_up_by_one_line_skipping_drawing()
1370 -- display lines 3/4/5 with a drawing just off screen at line 2
1371 App.screen.init{width=120, height=60}
1372 Editor_state = edit.initialize_test_state()
1373 Editor_state.lines = load_array{'abc', '```lines', '```', 'def', 'ghi', 'jkl'}
1374 Text.redraw_all(Editor_state)
1375 Editor_state.cursor1 = {line=3, pos=1}
1376 Editor_state.screen_top1 = {line=3, pos=1}
1377 Editor_state.screen_bottom1 = {}
1378 edit.draw(Editor_state)
1379 local y = Editor_state.top
1380 App.screen.check(y, 'def', 'baseline/screen:1')
1381 y = y + Editor_state.line_height
1382 App.screen.check(y, 'ghi', 'baseline/screen:2')
1383 y = y + Editor_state.line_height
1384 App.screen.check(y, 'jkl', 'baseline/screen:3')
1385 -- after hitting the up arrow the screen scrolls up to previous text line
1386 edit.run_after_keychord(Editor_state, 'up')
1387 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1388 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1391 function test_up_arrow_scrolls_up_by_one_screen_line()
1392 -- display lines starting from second screen line of a line
1393 App.screen.init{width=Editor_state.left+30, height=60}
1394 Editor_state = edit.initialize_test_state()
1395 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1396 Text.redraw_all(Editor_state)
1397 Editor_state.cursor1 = {line=3, pos=6}
1398 Editor_state.screen_top1 = {line=3, pos=5}
1399 Editor_state.screen_bottom1 = {}
1400 edit.draw(Editor_state)
1401 local y = Editor_state.top
1402 App.screen.check(y, 'jkl', 'baseline/screen:1')
1403 y = y + Editor_state.line_height
1404 App.screen.check(y, 'mno', 'baseline/screen:2')
1405 -- after hitting the up arrow the screen scrolls up to first screen line
1406 edit.run_after_keychord(Editor_state, 'up')
1407 y = Editor_state.top
1408 App.screen.check(y, 'ghi ', 'screen:1')
1409 y = y + Editor_state.line_height
1410 App.screen.check(y, 'jkl', 'screen:2')
1411 y = y + Editor_state.line_height
1412 App.screen.check(y, 'mno', 'screen:3')
1413 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1414 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1415 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1416 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1419 function test_up_arrow_scrolls_up_to_final_screen_line()
1420 -- display lines starting just after a long line
1421 App.screen.init{width=Editor_state.left+30, height=60}
1422 Editor_state = edit.initialize_test_state()
1423 Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
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, 'ghi', 'baseline/screen:1')
1431 y = y + Editor_state.line_height
1432 App.screen.check(y, 'jkl', 'baseline/screen:2')
1433 y = y + Editor_state.line_height
1434 App.screen.check(y, 'mno', 'baseline/screen:3')
1435 -- after hitting the up arrow the screen scrolls up to final screen line of previous line
1436 edit.run_after_keychord(Editor_state, 'up')
1437 y = Editor_state.top
1438 App.screen.check(y, 'def', 'screen:1')
1439 y = y + Editor_state.line_height
1440 App.screen.check(y, 'ghi', 'screen:2')
1441 y = y + Editor_state.line_height
1442 App.screen.check(y, 'jkl', 'screen:3')
1443 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1444 check_eq(Editor_state.screen_top1.pos, 5, 'screen_top:pos')
1445 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1446 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1449 function test_up_arrow_scrolls_up_to_empty_line()
1450 -- display a screenful of text with an empty line just above it outside the screen
1451 App.screen.init{width=120, height=60}
1452 Editor_state = edit.initialize_test_state()
1453 Editor_state.lines = load_array{'', 'abc', 'def', 'ghi', 'jkl'}
1454 Text.redraw_all(Editor_state)
1455 Editor_state.cursor1 = {line=2, pos=1}
1456 Editor_state.screen_top1 = {line=2, pos=1}
1457 Editor_state.screen_bottom1 = {}
1458 edit.draw(Editor_state)
1459 local y = Editor_state.top
1460 App.screen.check(y, 'abc', 'baseline/screen:1')
1461 y = y + Editor_state.line_height
1462 App.screen.check(y, 'def', 'baseline/screen:2')
1463 y = y + Editor_state.line_height
1464 App.screen.check(y, 'ghi', 'baseline/screen:3')
1465 -- after hitting the up arrow the screen scrolls up by one line
1466 edit.run_after_keychord(Editor_state, 'up')
1467 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1468 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1469 y = Editor_state.top
1470 -- empty first line
1471 y = y + Editor_state.line_height
1472 App.screen.check(y, 'abc', 'screen:2')
1473 y = y + Editor_state.line_height
1474 App.screen.check(y, 'def', 'screen:3')
1477 function test_pageup()
1478 App.screen.init{width=120, height=45}
1479 Editor_state = edit.initialize_test_state()
1480 Editor_state.lines = load_array{'abc', 'def', 'ghi'}
1481 Text.redraw_all(Editor_state)
1482 Editor_state.cursor1 = {line=2, pos=1}
1483 Editor_state.screen_top1 = {line=2, pos=1}
1484 Editor_state.screen_bottom1 = {}
1485 -- initially the last two lines are displayed
1486 edit.draw(Editor_state)
1487 local y = Editor_state.top
1488 App.screen.check(y, 'def', 'baseline/screen:1')
1489 y = y + Editor_state.line_height
1490 App.screen.check(y, 'ghi', 'baseline/screen:2')
1491 -- after pageup the cursor goes to first line
1492 edit.run_after_keychord(Editor_state, 'pageup')
1493 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1494 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1495 y = Editor_state.top
1496 App.screen.check(y, 'abc', 'screen:1')
1497 y = y + Editor_state.line_height
1498 App.screen.check(y, 'def', 'screen:2')
1501 function test_pageup_scrolls_up_by_screen_line()
1502 -- display the first three lines with the cursor on the bottom line
1503 App.screen.init{width=Editor_state.left+30, height=60}
1504 Editor_state = edit.initialize_test_state()
1505 Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
1506 Text.redraw_all(Editor_state)
1507 Editor_state.cursor1 = {line=2, pos=1}
1508 Editor_state.screen_top1 = {line=2, pos=1}
1509 Editor_state.screen_bottom1 = {}
1510 edit.draw(Editor_state)
1511 local y = Editor_state.top
1512 App.screen.check(y, 'ghi', 'baseline/screen:1')
1513 y = y + Editor_state.line_height
1514 App.screen.check(y, 'jkl', 'baseline/screen:2')
1515 y = y + Editor_state.line_height
1516 App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1517 -- after hitting the page-up key the screen scrolls up to top
1518 edit.run_after_keychord(Editor_state, 'pageup')
1519 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1520 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1521 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1522 y = Editor_state.top
1523 App.screen.check(y, 'abc ', 'screen:1')
1524 y = y + Editor_state.line_height
1525 App.screen.check(y, 'def', 'screen:2')
1526 y = y + Editor_state.line_height
1527 App.screen.check(y, 'ghi', 'screen:3')
1530 function test_pageup_scrolls_up_from_middle_screen_line()
1531 -- display a few lines starting from the middle of a line (Editor_state.cursor1.pos > 1)
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', 'mno'}
1535 Text.redraw_all(Editor_state)
1536 Editor_state.cursor1 = {line=2, pos=5}
1537 Editor_state.screen_top1 = {line=2, pos=5}
1538 Editor_state.screen_bottom1 = {}
1539 edit.draw(Editor_state)
1540 local y = Editor_state.top
1541 App.screen.check(y, 'jkl', 'baseline/screen:2')
1542 y = y + Editor_state.line_height
1543 App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1544 -- after hitting the page-up key the screen scrolls up to top
1545 edit.run_after_keychord(Editor_state, 'pageup')
1546 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1547 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1548 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1549 y = Editor_state.top
1550 App.screen.check(y, 'abc ', 'screen:1')
1551 y = y + Editor_state.line_height
1552 App.screen.check(y, 'def', 'screen:2')
1553 y = y + Editor_state.line_height
1554 App.screen.check(y, 'ghi ', 'screen:3')
1557 function test_enter_on_bottom_line_scrolls_down()
1558 -- display a few lines with cursor on bottom line
1559 App.screen.init{width=Editor_state.left+30, height=60}
1560 Editor_state = edit.initialize_test_state()
1561 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1562 Text.redraw_all(Editor_state)
1563 Editor_state.cursor1 = {line=3, pos=2}
1564 Editor_state.screen_top1 = {line=1, pos=1}
1565 Editor_state.screen_bottom1 = {}
1566 edit.draw(Editor_state)
1567 local y = Editor_state.top
1568 App.screen.check(y, 'abc', 'baseline/screen:1')
1569 y = y + Editor_state.line_height
1570 App.screen.check(y, 'def', 'baseline/screen:2')
1571 y = y + Editor_state.line_height
1572 App.screen.check(y, 'ghi', 'baseline/screen:3')
1573 -- after hitting the enter key the screen scrolls down
1574 edit.run_after_keychord(Editor_state, 'return')
1575 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1576 check_eq(Editor_state.cursor1.line, 4, 'cursor:line')
1577 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1578 y = Editor_state.top
1579 App.screen.check(y, 'def', 'screen:1')
1580 y = y + Editor_state.line_height
1581 App.screen.check(y, 'g', 'screen:2')
1582 y = y + Editor_state.line_height
1583 App.screen.check(y, 'hi', 'screen:3')
1586 function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1587 -- display just the bottom line on screen
1588 App.screen.init{width=Editor_state.left+30, height=60}
1589 Editor_state = edit.initialize_test_state()
1590 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1591 Text.redraw_all(Editor_state)
1592 Editor_state.cursor1 = {line=4, pos=2}
1593 Editor_state.screen_top1 = {line=4, pos=1}
1594 Editor_state.screen_bottom1 = {}
1595 edit.draw(Editor_state)
1596 local y = Editor_state.top
1597 App.screen.check(y, 'jkl', 'baseline/screen:1')
1598 -- after hitting the enter key the screen does not scroll down
1599 edit.run_after_keychord(Editor_state, 'return')
1600 check_eq(Editor_state.screen_top1.line, 4, 'screen_top')
1601 check_eq(Editor_state.cursor1.line, 5, 'cursor:line')
1602 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1603 y = Editor_state.top
1604 App.screen.check(y, 'j', 'screen:1')
1605 y = y + Editor_state.line_height
1606 App.screen.check(y, 'kl', 'screen:2')
1609 function test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1610 -- display just an empty bottom line on screen
1611 App.screen.init{width=Editor_state.left+30, height=60}
1612 Editor_state = edit.initialize_test_state()
1613 Editor_state.lines = load_array{'abc', ''}
1614 Text.redraw_all(Editor_state)
1615 Editor_state.cursor1 = {line=2, pos=1}
1616 Editor_state.screen_top1 = {line=2, pos=1}
1617 Editor_state.screen_bottom1 = {}
1618 edit.draw(Editor_state)
1619 -- after hitting the inserting_text key the screen does not scroll down
1620 edit.run_after_text_input(Editor_state, 'a')
1621 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1622 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1623 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1624 local y = Editor_state.top
1625 App.screen.check(y, 'a', 'screen:1')
1628 function test_typing_on_bottom_line_scrolls_down()
1629 -- display a few lines with cursor on 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'}
1633 Text.redraw_all(Editor_state)
1634 Editor_state.cursor1 = {line=3, pos=4}
1635 Editor_state.screen_top1 = {line=1, pos=1}
1636 Editor_state.screen_bottom1 = {}
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')
1644 -- after typing something the line wraps and the screen scrolls down
1645 edit.run_after_text_input(Editor_state, 'j')
1646 edit.run_after_text_input(Editor_state, 'k')
1647 edit.run_after_text_input(Editor_state, 'l')
1648 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1649 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1650 check_eq(Editor_state.cursor1.pos, 7, 'cursor:pos')
1651 y = Editor_state.top
1652 App.screen.check(y, 'def', 'screen:1')
1653 y = y + Editor_state.line_height
1654 App.screen.check(y, 'ghij', 'screen:2')
1655 y = y + Editor_state.line_height
1656 App.screen.check(y, 'kl', 'screen:3')
1659 function test_left_arrow_scrolls_up_in_wrapped_line()
1660 -- display lines starting from second screen line of a line
1661 App.screen.init{width=Editor_state.left+30, height=60}
1662 Editor_state = edit.initialize_test_state()
1663 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1664 Text.redraw_all(Editor_state)
1665 Editor_state.screen_top1 = {line=3, pos=5}
1666 Editor_state.screen_bottom1 = {}
1667 -- cursor is at top of screen
1668 Editor_state.cursor1 = {line=3, pos=5}
1669 edit.draw(Editor_state)
1670 local y = Editor_state.top
1671 App.screen.check(y, 'jkl', 'baseline/screen:1')
1672 y = y + Editor_state.line_height
1673 App.screen.check(y, 'mno', 'baseline/screen:2')
1674 -- after hitting the left arrow the screen scrolls up to first screen line
1675 edit.run_after_keychord(Editor_state, 'left')
1676 y = Editor_state.top
1677 App.screen.check(y, 'ghi ', 'screen:1')
1678 y = y + Editor_state.line_height
1679 App.screen.check(y, 'jkl', 'screen:2')
1680 y = y + Editor_state.line_height
1681 App.screen.check(y, 'mno', 'screen:3')
1682 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1683 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1684 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1685 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
1688 function test_right_arrow_scrolls_down_in_wrapped_line()
1689 -- display the first three lines with the cursor on the bottom 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=1, pos=1}
1695 Editor_state.screen_bottom1 = {}
1696 -- cursor is at bottom right 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, 'abc', 'baseline/screen:1')
1701 y = y + Editor_state.line_height
1702 App.screen.check(y, 'def', 'baseline/screen:2')
1703 y = y + Editor_state.line_height
1704 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1705 -- after hitting the right arrow the screen scrolls down by one line
1706 edit.run_after_keychord(Editor_state, 'right')
1707 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1708 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1709 check_eq(Editor_state.cursor1.pos, 6, 'cursor:pos')
1710 y = Editor_state.top
1711 App.screen.check(y, 'def', 'screen:1')
1712 y = y + Editor_state.line_height
1713 App.screen.check(y, 'ghi ', 'screen:2')
1714 y = y + Editor_state.line_height
1715 App.screen.check(y, 'jkl', 'screen:3')
1718 function test_home_scrolls_up_in_wrapped_line()
1719 -- display lines starting from second screen line of a line
1720 App.screen.init{width=Editor_state.left+30, height=60}
1721 Editor_state = edit.initialize_test_state()
1722 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1723 Text.redraw_all(Editor_state)
1724 Editor_state.screen_top1 = {line=3, pos=5}
1725 Editor_state.screen_bottom1 = {}
1726 -- cursor is at top of screen
1727 Editor_state.cursor1 = {line=3, pos=5}
1728 edit.draw(Editor_state)
1729 local y = Editor_state.top
1730 App.screen.check(y, 'jkl', 'baseline/screen:1')
1731 y = y + Editor_state.line_height
1732 App.screen.check(y, 'mno', 'baseline/screen:2')
1733 -- after hitting home the screen scrolls up to first screen line
1734 edit.run_after_keychord(Editor_state, 'home')
1735 y = Editor_state.top
1736 App.screen.check(y, 'ghi ', 'screen:1')
1737 y = y + Editor_state.line_height
1738 App.screen.check(y, 'jkl', 'screen:2')
1739 y = y + Editor_state.line_height
1740 App.screen.check(y, 'mno', 'screen:3')
1741 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1742 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1743 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1744 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1747 function test_end_scrolls_down_in_wrapped_line()
1748 -- display the first three lines with the cursor on the bottom line
1749 App.screen.init{width=Editor_state.left+30, height=60}
1750 Editor_state = edit.initialize_test_state()
1751 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1752 Text.redraw_all(Editor_state)
1753 Editor_state.screen_top1 = {line=1, pos=1}
1754 Editor_state.screen_bottom1 = {}
1755 -- cursor is at bottom right of screen
1756 Editor_state.cursor1 = {line=3, pos=5}
1757 edit.draw(Editor_state)
1758 local y = Editor_state.top
1759 App.screen.check(y, 'abc', 'baseline/screen:1')
1760 y = y + Editor_state.line_height
1761 App.screen.check(y, 'def', 'baseline/screen:2')
1762 y = y + Editor_state.line_height
1763 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1764 -- after hitting end the screen scrolls down by one line
1765 edit.run_after_keychord(Editor_state, 'end')
1766 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1767 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1768 check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
1769 y = Editor_state.top
1770 App.screen.check(y, 'def', 'screen:1')
1771 y = y + Editor_state.line_height
1772 App.screen.check(y, 'ghi ', 'screen:2')
1773 y = y + Editor_state.line_height
1774 App.screen.check(y, 'jkl', 'screen:3')
1777 function test_position_cursor_on_recently_edited_wrapping_line()
1778 -- draw a line wrapping over 2 screen lines
1779 App.screen.init{width=100, height=200}
1780 Editor_state = edit.initialize_test_state()
1781 Editor_state.lines = load_array{'abc def ghi jkl mno pqr ', 'xyz'}
1782 Text.redraw_all(Editor_state)
1783 Editor_state.cursor1 = {line=1, pos=25}
1784 Editor_state.screen_top1 = {line=1, pos=1}
1785 Editor_state.screen_bottom1 = {}
1786 edit.draw(Editor_state)
1787 local y = Editor_state.top
1788 App.screen.check(y, 'abc def ghi ', 'baseline1/screen:1')
1789 y = y + Editor_state.line_height
1790 App.screen.check(y, 'jkl mno pqr ', 'baseline1/screen:2')
1791 y = y + Editor_state.line_height
1792 App.screen.check(y, 'xyz', 'baseline1/screen:3')
1793 -- add to the line until it's wrapping over 3 screen lines
1794 edit.run_after_text_input(Editor_state, 's')
1795 edit.run_after_text_input(Editor_state, 't')
1796 edit.run_after_text_input(Editor_state, 'u')
1797 check_eq(Editor_state.cursor1.pos, 28, 'cursor:pos')
1798 y = Editor_state.top
1799 App.screen.check(y, 'abc def ghi ', 'baseline2/screen:1')
1800 y = y + Editor_state.line_height
1801 App.screen.check(y, 'jkl mno pqr ', 'baseline2/screen:2')
1802 y = y + Editor_state.line_height
1803 App.screen.check(y, 'stu', 'baseline2/screen:3')
1804 -- try to move the cursor earlier in the third screen line by clicking the mouse
1805 edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+Editor_state.line_height*2+5, 1)
1806 -- cursor should move
1807 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1808 check_eq(Editor_state.cursor1.pos, 26, 'cursor:pos')
1811 function test_backspace_can_scroll_up()
1812 -- display the lines 2/3/4 with the cursor on line 2
1813 App.screen.init{width=120, height=60}
1814 Editor_state = edit.initialize_test_state()
1815 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1816 Text.redraw_all(Editor_state)
1817 Editor_state.cursor1 = {line=2, pos=1}
1818 Editor_state.screen_top1 = {line=2, pos=1}
1819 Editor_state.screen_bottom1 = {}
1820 edit.draw(Editor_state)
1821 local y = Editor_state.top
1822 App.screen.check(y, 'def', 'baseline/screen:1')
1823 y = y + Editor_state.line_height
1824 App.screen.check(y, 'ghi', 'baseline/screen:2')
1825 y = y + Editor_state.line_height
1826 App.screen.check(y, 'jkl', 'baseline/screen:3')
1827 -- after hitting backspace the screen scrolls up by one line
1828 edit.run_after_keychord(Editor_state, 'backspace')
1829 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1830 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1831 y = Editor_state.top
1832 App.screen.check(y, 'abcdef', 'screen:1')
1833 y = y + Editor_state.line_height
1834 App.screen.check(y, 'ghi', 'screen:2')
1835 y = y + Editor_state.line_height
1836 App.screen.check(y, 'jkl', 'screen:3')
1839 function test_backspace_can_scroll_up_screen_line()
1840 -- display lines starting from second screen line of a line
1841 App.screen.init{width=Editor_state.left+30, height=60}
1842 Editor_state = edit.initialize_test_state()
1843 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1844 Text.redraw_all(Editor_state)
1845 Editor_state.cursor1 = {line=3, pos=5}
1846 Editor_state.screen_top1 = {line=3, pos=5}
1847 Editor_state.screen_bottom1 = {}
1848 edit.draw(Editor_state)
1849 local y = Editor_state.top
1850 App.screen.check(y, 'jkl', 'baseline/screen:1')
1851 y = y + Editor_state.line_height
1852 App.screen.check(y, 'mno', 'baseline/screen:2')
1853 -- after hitting backspace the screen scrolls up by one screen line
1854 edit.run_after_keychord(Editor_state, 'backspace')
1855 y = Editor_state.top
1856 App.screen.check(y, 'ghij', 'screen:1')
1857 y = y + Editor_state.line_height
1858 App.screen.check(y, 'kl', 'screen:2')
1859 y = y + Editor_state.line_height
1860 App.screen.check(y, 'mno', 'screen:3')
1861 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1862 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1863 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1864 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
1867 function test_backspace_past_line_boundary()
1868 -- position cursor at start of a (non-first) line
1869 App.screen.init{width=Editor_state.left+30, height=60}
1870 Editor_state = edit.initialize_test_state()
1871 Editor_state.lines = load_array{'abc', 'def'}
1872 Text.redraw_all(Editor_state)
1873 Editor_state.cursor1 = {line=2, pos=1}
1874 -- backspace joins with previous line
1875 edit.run_after_keychord(Editor_state, 'backspace')
1876 check_eq(Editor_state.lines[1].data, 'abcdef', 'check')
1879 -- some tests for operating over selections created using Shift- chords
1880 -- we're just testing delete_selection, and it works the same for all keys
1882 function test_backspace_over_selection()
1883 -- select just one character within a line with cursor before selection
1884 App.screen.init{width=Editor_state.left+30, height=60}
1885 Editor_state = edit.initialize_test_state()
1886 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1887 Text.redraw_all(Editor_state)
1888 Editor_state.cursor1 = {line=1, pos=1}
1889 Editor_state.selection1 = {line=1, pos=2}
1890 -- backspace deletes the selected character, even though it's after the cursor
1891 edit.run_after_keychord(Editor_state, 'backspace')
1892 check_eq(Editor_state.lines[1].data, 'bc', 'data')
1893 -- cursor (remains) at start of selection
1894 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1895 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1896 -- selection is cleared
1897 check_nil(Editor_state.selection1.line, 'selection')
1900 function test_backspace_over_selection_reverse()
1901 -- select just one character within a line with cursor after selection
1902 App.screen.init{width=Editor_state.left+30, height=60}
1903 Editor_state = edit.initialize_test_state()
1904 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1905 Text.redraw_all(Editor_state)
1906 Editor_state.cursor1 = {line=1, pos=2}
1907 Editor_state.selection1 = {line=1, pos=1}
1908 -- backspace deletes the selected character
1909 edit.run_after_keychord(Editor_state, 'backspace')
1910 check_eq(Editor_state.lines[1].data, 'bc', 'data')
1911 -- cursor moves to start of selection
1912 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1913 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1914 -- selection is cleared
1915 check_nil(Editor_state.selection1.line, 'selection')
1918 function test_backspace_over_multiple_lines()
1919 -- select just one character within a line with cursor after selection
1920 App.screen.init{width=Editor_state.left+30, height=60}
1921 Editor_state = edit.initialize_test_state()
1922 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1923 Text.redraw_all(Editor_state)
1924 Editor_state.cursor1 = {line=1, pos=2}
1925 Editor_state.selection1 = {line=4, pos=2}
1926 -- backspace deletes the region and joins the remaining portions of lines on either side
1927 edit.run_after_keychord(Editor_state, 'backspace')
1928 check_eq(Editor_state.lines[1].data, 'akl', 'data:1')
1929 check_eq(Editor_state.lines[2].data, 'mno', 'data:2')
1930 -- cursor remains at start of selection
1931 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1932 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1933 -- selection is cleared
1934 check_nil(Editor_state.selection1.line, 'selection')
1937 function test_backspace_to_end_of_line()
1938 -- select region from cursor to end of line
1939 App.screen.init{width=Editor_state.left+30, height=60}
1940 Editor_state = edit.initialize_test_state()
1941 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1942 Text.redraw_all(Editor_state)
1943 Editor_state.cursor1 = {line=1, pos=2}
1944 Editor_state.selection1 = {line=1, pos=4}
1945 -- backspace deletes rest of line without joining to any other line
1946 edit.run_after_keychord(Editor_state, 'backspace')
1947 check_eq(Editor_state.lines[1].data, 'a', 'data:1')
1948 check_eq(Editor_state.lines[2].data, 'def', 'data:2')
1949 -- cursor remains at start of selection
1950 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1951 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1952 -- selection is cleared
1953 check_nil(Editor_state.selection1.line, 'selection')
1956 function test_backspace_to_start_of_line()
1957 -- select region from cursor to start of line
1958 App.screen.init{width=Editor_state.left+30, height=60}
1959 Editor_state = edit.initialize_test_state()
1960 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1961 Text.redraw_all(Editor_state)
1962 Editor_state.cursor1 = {line=2, pos=1}
1963 Editor_state.selection1 = {line=2, pos=3}
1964 -- backspace deletes beginning of line without joining to any other line
1965 edit.run_after_keychord(Editor_state, 'backspace')
1966 check_eq(Editor_state.lines[1].data, 'abc', 'data:1')
1967 check_eq(Editor_state.lines[2].data, 'f', 'data:2')
1968 -- cursor remains at start of selection
1969 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1970 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1971 -- selection is cleared
1972 check_nil(Editor_state.selection1.line, 'selection')
1975 function test_undo_insert_text()
1976 App.screen.init{width=120, height=60}
1977 Editor_state = edit.initialize_test_state()
1978 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
1979 Text.redraw_all(Editor_state)
1980 Editor_state.cursor1 = {line=2, pos=4}
1981 Editor_state.screen_top1 = {line=1, pos=1}
1982 Editor_state.screen_bottom1 = {}
1983 -- insert a character
1984 edit.draw(Editor_state)
1985 edit.run_after_text_input(Editor_state, 'g')
1986 check_eq(Editor_state.cursor1.line, 2, 'baseline/cursor:line')
1987 check_eq(Editor_state.cursor1.pos, 5, 'baseline/cursor:pos')
1988 check_nil(Editor_state.selection1.line, 'baseline/selection:line')
1989 check_nil(Editor_state.selection1.pos, 'baseline/selection:pos')
1990 local y = Editor_state.top
1991 App.screen.check(y, 'abc', 'baseline/screen:1')
1992 y = y + Editor_state.line_height
1993 App.screen.check(y, 'defg', 'baseline/screen:2')
1994 y = y + Editor_state.line_height
1995 App.screen.check(y, 'xyz', 'baseline/screen:3')
1996 -- undo
1997 edit.run_after_keychord(Editor_state, 'C-z')
1998 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1999 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
2000 check_nil(Editor_state.selection1.line, 'selection:line')
2001 check_nil(Editor_state.selection1.pos, 'selection:pos')
2002 y = Editor_state.top
2003 App.screen.check(y, 'abc', 'screen:1')
2004 y = y + Editor_state.line_height
2005 App.screen.check(y, 'def', 'screen:2')
2006 y = y + Editor_state.line_height
2007 App.screen.check(y, 'xyz', 'screen:3')
2010 function test_undo_delete_text()
2011 App.screen.init{width=120, height=60}
2012 Editor_state = edit.initialize_test_state()
2013 Editor_state.lines = load_array{'abc', 'defg', 'xyz'}
2014 Text.redraw_all(Editor_state)
2015 Editor_state.cursor1 = {line=2, pos=5}
2016 Editor_state.screen_top1 = {line=1, pos=1}
2017 Editor_state.screen_bottom1 = {}
2018 -- delete a character
2019 edit.run_after_keychord(Editor_state, 'backspace')
2020 check_eq(Editor_state.cursor1.line, 2, 'baseline/cursor:line')
2021 check_eq(Editor_state.cursor1.pos, 4, 'baseline/cursor:pos')
2022 check_nil(Editor_state.selection1.line, 'baseline/selection:line')
2023 check_nil(Editor_state.selection1.pos, 'baseline/selection:pos')
2024 local y = Editor_state.top
2025 App.screen.check(y, 'abc', 'baseline/screen:1')
2026 y = y + Editor_state.line_height
2027 App.screen.check(y, 'def', 'baseline/screen:2')
2028 y = y + Editor_state.line_height
2029 App.screen.check(y, 'xyz', 'baseline/screen:3')
2030 -- undo
2031 --? -- after undo, the backspaced key is selected
2032 edit.run_after_keychord(Editor_state, 'C-z')
2033 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
2034 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
2035 check_nil(Editor_state.selection1.line, 'selection:line')
2036 check_nil(Editor_state.selection1.pos, 'selection:pos')
2037 --? check_eq(Editor_state.selection1.line, 2, 'selection:line')
2038 --? check_eq(Editor_state.selection1.pos, 4, 'selection:pos')
2039 y = Editor_state.top
2040 App.screen.check(y, 'abc', 'screen:1')
2041 y = y + Editor_state.line_height
2042 App.screen.check(y, 'defg', 'screen:2')
2043 y = y + Editor_state.line_height
2044 App.screen.check(y, 'xyz', 'screen:3')
2047 function test_undo_restores_selection()
2048 -- display a line of text with some part selected
2049 App.screen.init{width=75, height=80}
2050 Editor_state = edit.initialize_test_state()
2051 Editor_state.lines = load_array{'abc'}
2052 Text.redraw_all(Editor_state)
2053 Editor_state.cursor1 = {line=1, pos=1}
2054 Editor_state.selection1 = {line=1, pos=2}
2055 Editor_state.screen_top1 = {line=1, pos=1}
2056 Editor_state.screen_bottom1 = {}
2057 edit.draw(Editor_state)
2058 -- delete selected text
2059 edit.run_after_text_input(Editor_state, 'x')
2060 check_eq(Editor_state.lines[1].data, 'xbc', 'baseline')
2061 check_nil(Editor_state.selection1.line, 'baseline:selection')
2062 -- undo
2063 edit.run_after_keychord(Editor_state, 'C-z')
2064 edit.run_after_keychord(Editor_state, 'C-z')
2065 -- selection is restored
2066 check_eq(Editor_state.selection1.line, 1, 'line')
2067 check_eq(Editor_state.selection1.pos, 2, 'pos')
2070 function test_search()
2071 App.screen.init{width=120, height=60}
2072 Editor_state = edit.initialize_test_state()
2073 Editor_state.lines = load_array{'```lines', '```', 'def', 'ghi', '’deg'} -- contains unicode quote in final line
2074 Text.redraw_all(Editor_state)
2075 Editor_state.cursor1 = {line=1, pos=1}
2076 Editor_state.screen_top1 = {line=1, pos=1}
2077 Editor_state.screen_bottom1 = {}
2078 edit.draw(Editor_state)
2079 -- search for a string
2080 edit.run_after_keychord(Editor_state, 'C-f')
2081 edit.run_after_text_input(Editor_state, 'd')
2082 edit.run_after_keychord(Editor_state, 'return')
2083 check_eq(Editor_state.cursor1.line, 2, '1/cursor:line')
2084 check_eq(Editor_state.cursor1.pos, 1, '1/cursor:pos')
2085 -- reset cursor
2086 Editor_state.cursor1 = {line=1, pos=1}
2087 Editor_state.screen_top1 = {line=1, pos=1}
2088 -- search for second occurrence
2089 edit.run_after_keychord(Editor_state, 'C-f')
2090 edit.run_after_text_input(Editor_state, 'de')
2091 edit.run_after_keychord(Editor_state, 'down')
2092 edit.run_after_keychord(Editor_state, 'return')
2093 check_eq(Editor_state.cursor1.line, 4, '2/cursor:line')
2094 check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
2097 function test_search_upwards()
2098 App.screen.init{width=120, height=60}
2099 Editor_state = edit.initialize_test_state()
2100 Editor_state.lines = load_array{'’abc', 'abd'} -- contains unicode quote
2101 Text.redraw_all(Editor_state)
2102 Editor_state.cursor1 = {line=2, pos=1}
2103 Editor_state.screen_top1 = {line=1, pos=1}
2104 Editor_state.screen_bottom1 = {}
2105 edit.draw(Editor_state)
2106 -- search for a string
2107 edit.run_after_keychord(Editor_state, 'C-f')
2108 edit.run_after_text_input(Editor_state, 'a')
2109 -- search for previous occurrence
2110 edit.run_after_keychord(Editor_state, 'up')
2111 check_eq(Editor_state.cursor1.line, 1, '2/cursor:line')
2112 check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
2115 function test_search_wrap()
2116 App.screen.init{width=120, height=60}
2117 Editor_state = edit.initialize_test_state()
2118 Editor_state.lines = load_array{'’abc', 'def'} -- contains unicode quote in first line
2119 Text.redraw_all(Editor_state)
2120 Editor_state.cursor1 = {line=2, pos=1}
2121 Editor_state.screen_top1 = {line=1, pos=1}
2122 Editor_state.screen_bottom1 = {}
2123 edit.draw(Editor_state)
2124 -- search for a string
2125 edit.run_after_keychord(Editor_state, 'C-f')
2126 edit.run_after_text_input(Editor_state, 'a')
2127 edit.run_after_keychord(Editor_state, 'return')
2128 -- cursor wraps
2129 check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
2130 check_eq(Editor_state.cursor1.pos, 2, '1/cursor:pos')
2133 function test_search_wrap_upwards()
2134 App.screen.init{width=120, height=60}
2135 Editor_state = edit.initialize_test_state()
2136 Editor_state.lines = load_array{'abc ’abd'} -- contains unicode quote
2137 Text.redraw_all(Editor_state)
2138 Editor_state.cursor1 = {line=1, pos=1}
2139 Editor_state.screen_top1 = {line=1, pos=1}
2140 Editor_state.screen_bottom1 = {}
2141 edit.draw(Editor_state)
2142 -- search upwards for a string
2143 edit.run_after_keychord(Editor_state, 'C-f')
2144 edit.run_after_text_input(Editor_state, 'a')
2145 edit.run_after_keychord(Editor_state, 'up')
2146 -- cursor wraps
2147 check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
2148 check_eq(Editor_state.cursor1.pos, 6, '1/cursor:pos')