Merge lines.love
[view.love.git] / source_text_tests.lua
blob6376ec8aed2d76ec992df813087f3e3de305ac37
1 -- major tests for text editing flows
2 -- Arguably this should be called source_edit_tests.lua,
3 -- but that would mess up the git blame at this point.
5 function test_initial_state()
6 App.screen.init{width=120, height=60}
7 Editor_state = edit.initialize_test_state()
8 Editor_state.lines = load_array{}
9 Text.redraw_all(Editor_state)
10 edit.draw(Editor_state)
11 check_eq(#Editor_state.lines, 1, '#lines')
12 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
13 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
14 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
15 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
16 end
18 function test_click_to_create_drawing()
19 App.screen.init{width=120, height=60}
20 Editor_state = edit.initialize_test_state()
21 Editor_state.lines = load_array{}
22 Text.redraw_all(Editor_state)
23 edit.draw(Editor_state)
24 edit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1)
25 -- cursor skips drawing to always remain on text
26 check_eq(#Editor_state.lines, 2, '#lines')
27 check_eq(Editor_state.cursor1.line, 2, 'cursor')
28 end
30 function test_backspace_to_delete_drawing()
31 -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
32 App.screen.init{width=120, height=60}
33 Editor_state = edit.initialize_test_state()
34 Editor_state.lines = load_array{'```lines', '```', ''}
35 Text.redraw_all(Editor_state)
36 -- cursor is on text as always (outside tests this will get initialized correctly)
37 Editor_state.cursor1.line = 2
38 -- backspacing deletes the drawing
39 edit.run_after_keychord(Editor_state, 'backspace', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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', '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 Editor_state.selection1 = {}
279 -- click to the left of the line
280 edit.draw(Editor_state)
281 edit.run_after_mouse_click(Editor_state, Editor_state.left-4,Editor_state.top+5, 1)
282 -- cursor moves to start of line
283 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
284 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
285 check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
288 function test_click_takes_margins_into_account()
289 -- display two lines with cursor on one of them
290 App.screen.init{width=100, height=80}
291 Editor_state = edit.initialize_test_state()
292 Editor_state.left = 50 -- occupy only right side of screen
293 Editor_state.lines = load_array{'abc', 'def'}
294 Text.redraw_all(Editor_state)
295 Editor_state.cursor1 = {line=2, pos=1}
296 Editor_state.screen_top1 = {line=1, pos=1}
297 Editor_state.screen_bottom1 = {}
298 Editor_state.selection1 = {}
299 -- click on the other line
300 edit.draw(Editor_state)
301 edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
302 -- cursor moves
303 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
304 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
305 check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
308 function test_click_on_empty_line()
309 -- display two lines with the first one empty
310 App.screen.init{width=50, height=80}
311 Editor_state = edit.initialize_test_state()
312 Editor_state.lines = load_array{'', 'def'}
313 Text.redraw_all(Editor_state)
314 Editor_state.cursor1 = {line=2, pos=1}
315 Editor_state.screen_top1 = {line=1, pos=1}
316 Editor_state.screen_bottom1 = {}
317 Editor_state.selection1 = {}
318 -- click on the empty line
319 edit.draw(Editor_state)
320 edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
321 -- cursor moves
322 check_eq(Editor_state.cursor1.line, 1, 'cursor')
323 -- selection remains empty
324 check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
327 function test_click_below_all_lines()
328 -- display one line
329 App.screen.init{width=50, height=80}
330 Editor_state = edit.initialize_test_state()
331 Editor_state.lines = load_array{'abc'}
332 Text.redraw_all(Editor_state)
333 Editor_state.cursor1 = {line=1, pos=1}
334 Editor_state.screen_top1 = {line=1, pos=1}
335 Editor_state.screen_bottom1 = {}
336 Editor_state.selection1 = {}
337 -- click below first line
338 edit.draw(Editor_state)
339 edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+50, 1)
340 -- cursor doesn't move
341 check_eq(Editor_state.cursor1.line, 1, 'cursor')
342 -- selection remains empty
343 check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
346 function test_draw_text()
347 App.screen.init{width=120, height=60}
348 Editor_state = edit.initialize_test_state()
349 Editor_state.lines = load_array{'abc', 'def', 'ghi'}
350 Text.redraw_all(Editor_state)
351 Editor_state.cursor1 = {line=1, pos=1}
352 Editor_state.screen_top1 = {line=1, pos=1}
353 Editor_state.screen_bottom1 = {}
354 edit.draw(Editor_state)
355 local y = Editor_state.top
356 App.screen.check(y, 'abc', 'screen:1')
357 y = y + Editor_state.line_height
358 App.screen.check(y, 'def', 'screen:2')
359 y = y + Editor_state.line_height
360 App.screen.check(y, 'ghi', 'screen:3')
363 function test_draw_wrapping_text()
364 App.screen.init{width=50, height=60}
365 Editor_state = edit.initialize_test_state()
366 Editor_state.lines = load_array{'abc', 'defgh', 'xyz'}
367 Text.redraw_all(Editor_state)
368 Editor_state.cursor1 = {line=1, pos=1}
369 Editor_state.screen_top1 = {line=1, pos=1}
370 Editor_state.screen_bottom1 = {}
371 edit.draw(Editor_state)
372 local y = Editor_state.top
373 App.screen.check(y, 'abc', 'screen:1')
374 y = y + Editor_state.line_height
375 App.screen.check(y, 'de', 'screen:2')
376 y = y + Editor_state.line_height
377 App.screen.check(y, 'fgh', 'screen:3')
380 function test_draw_word_wrapping_text()
381 App.screen.init{width=60, height=60}
382 Editor_state = edit.initialize_test_state()
383 Editor_state.lines = load_array{'abc def ghi', 'jkl'}
384 Text.redraw_all(Editor_state)
385 Editor_state.cursor1 = {line=1, pos=1}
386 Editor_state.screen_top1 = {line=1, pos=1}
387 Editor_state.screen_bottom1 = {}
388 edit.draw(Editor_state)
389 local y = Editor_state.top
390 App.screen.check(y, 'abc ', 'screen:1')
391 y = y + Editor_state.line_height
392 App.screen.check(y, 'def ', 'screen:2')
393 y = y + Editor_state.line_height
394 App.screen.check(y, 'ghi', 'screen:3')
397 function test_click_on_wrapping_line()
398 -- display two screen lines with cursor on one of them
399 App.screen.init{width=50, height=80}
400 Editor_state = edit.initialize_test_state()
401 Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu'}
402 Text.redraw_all(Editor_state)
403 Editor_state.cursor1 = {line=1, pos=20}
404 Editor_state.screen_top1 = {line=1, pos=1}
405 Editor_state.screen_bottom1 = {}
406 -- click on the other line
407 edit.draw(Editor_state)
408 edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
409 -- cursor moves
410 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
411 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
412 check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
415 function test_click_on_wrapping_line_takes_margins_into_account()
416 -- display two screen lines with cursor on one of them
417 App.screen.init{width=100, height=80}
418 Editor_state = edit.initialize_test_state()
419 Editor_state.left = 50 -- occupy only right side of screen
420 Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu'}
421 Text.redraw_all(Editor_state)
422 Editor_state.cursor1 = {line=1, pos=20}
423 Editor_state.screen_top1 = {line=1, pos=1}
424 Editor_state.screen_bottom1 = {}
425 -- click on the other line
426 edit.draw(Editor_state)
427 edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
428 -- cursor moves
429 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
430 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
431 check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
434 function test_draw_text_wrapping_within_word()
435 -- arrange a screen line that needs to be split within a word
436 App.screen.init{width=60, height=60}
437 Editor_state = edit.initialize_test_state()
438 Editor_state.lines = load_array{'abcd e fghijk', 'xyz'}
439 Text.redraw_all(Editor_state)
440 Editor_state.cursor1 = {line=1, pos=1}
441 Editor_state.screen_top1 = {line=1, pos=1}
442 Editor_state.screen_bottom1 = {}
443 edit.draw(Editor_state)
444 local y = Editor_state.top
445 App.screen.check(y, 'abcd ', 'screen:1')
446 y = y + Editor_state.line_height
447 App.screen.check(y, 'e fgh', 'screen:2')
448 y = y + Editor_state.line_height
449 App.screen.check(y, 'ijk', 'screen:3')
452 function test_draw_wrapping_text_containing_non_ascii()
453 -- draw a long line containing non-ASCII
454 App.screen.init{width=60, height=60}
455 Editor_state = edit.initialize_test_state()
456 Editor_state.lines = load_array{'madam I’m adam', 'xyz'} -- notice the non-ASCII apostrophe
457 Text.redraw_all(Editor_state)
458 Editor_state.cursor1 = {line=1, pos=1}
459 Editor_state.screen_top1 = {line=1, pos=1}
460 Editor_state.screen_bottom1 = {}
461 edit.draw(Editor_state)
462 local y = Editor_state.top
463 App.screen.check(y, 'mad', 'screen:1')
464 y = y + Editor_state.line_height
465 App.screen.check(y, 'am I', 'screen:2')
466 y = y + Editor_state.line_height
467 App.screen.check(y, '’m a', 'screen:3')
470 function test_click_past_end_of_screen_line()
471 -- display a wrapping line
472 App.screen.init{width=75, height=80}
473 Editor_state = edit.initialize_test_state()
474 -- 12345678901234
475 Editor_state.lines = load_array{"madam I'm adam"}
476 Text.redraw_all(Editor_state)
477 Editor_state.cursor1 = {line=1, pos=1}
478 Editor_state.screen_top1 = {line=1, pos=1}
479 Editor_state.screen_bottom1 = {}
480 edit.draw(Editor_state)
481 local y = Editor_state.top
482 App.screen.check(y, 'madam ', 'baseline/screen:1')
483 y = y + Editor_state.line_height
484 App.screen.check(y, "I'm ad", 'baseline/screen:2')
485 y = y + Editor_state.line_height
486 -- click past end of second screen line
487 edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
488 -- cursor moves to end of screen line (one more than final character shown)
489 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
490 check_eq(Editor_state.cursor1.pos, 13, 'cursor:pos')
493 function test_click_on_wrapping_line_rendered_from_partway_at_top_of_screen()
494 -- display a wrapping line from its second screen line
495 App.screen.init{width=75, height=80}
496 Editor_state = edit.initialize_test_state()
497 -- 12345678901234
498 Editor_state.lines = load_array{"madam I'm adam"}
499 Text.redraw_all(Editor_state)
500 Editor_state.cursor1 = {line=1, pos=8}
501 Editor_state.screen_top1 = {line=1, pos=7}
502 Editor_state.screen_bottom1 = {}
503 edit.draw(Editor_state)
504 local y = Editor_state.top
505 App.screen.check(y, "I'm ad", 'baseline/screen:2')
506 y = y + Editor_state.line_height
507 -- click past end of second screen line
508 edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
509 -- cursor moves to end of screen line (one more than final character shown)
510 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
511 check_eq(Editor_state.cursor1.pos, 13, 'cursor:pos')
514 function test_click_past_end_of_wrapping_line()
515 -- display a wrapping line
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"}
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_wrapping_line_containing_non_ascii()
539 -- display a wrapping line containing non-ASCII
540 App.screen.init{width=75, height=80}
541 Editor_state = edit.initialize_test_state()
542 -- 12345678901234
543 Editor_state.lines = load_array{'madam I’m adam'} -- notice the non-ASCII apostrophe
544 Text.redraw_all(Editor_state)
545 Editor_state.cursor1 = {line=1, pos=1}
546 Editor_state.screen_top1 = {line=1, pos=1}
547 Editor_state.screen_bottom1 = {}
548 edit.draw(Editor_state)
549 local y = Editor_state.top
550 App.screen.check(y, 'madam ', 'baseline/screen:1')
551 y = y + Editor_state.line_height
552 App.screen.check(y, 'I’m ad', 'baseline/screen:2')
553 y = y + Editor_state.line_height
554 App.screen.check(y, 'am', 'baseline/screen:3')
555 y = y + Editor_state.line_height
556 -- click past the end of it
557 edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
558 -- cursor moves to end of line
559 check_eq(Editor_state.cursor1.pos, 15, 'cursor') -- one more than the number of UTF-8 code-points
562 function test_click_past_end_of_word_wrapping_line()
563 -- display a long line wrapping at a word boundary on a screen of more realistic length
564 App.screen.init{width=160, height=80}
565 Editor_state = edit.initialize_test_state()
566 -- 0 1 2
567 -- 123456789012345678901
568 Editor_state.lines = load_array{'the quick brown fox jumped over the lazy dog'}
569 Text.redraw_all(Editor_state)
570 Editor_state.cursor1 = {line=1, pos=1}
571 Editor_state.screen_top1 = {line=1, pos=1}
572 Editor_state.screen_bottom1 = {}
573 edit.draw(Editor_state)
574 local y = Editor_state.top
575 App.screen.check(y, 'the quick brown fox ', 'baseline/screen:1')
576 y = y + Editor_state.line_height
577 -- click past the end of the screen line
578 edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
579 -- cursor moves to end of screen line (one more than final character shown)
580 check_eq(Editor_state.cursor1.pos, 21, 'cursor')
583 function test_select_text()
584 -- display a line of text
585 App.screen.init{width=75, height=80}
586 Editor_state = edit.initialize_test_state()
587 Editor_state.lines = load_array{'abc def'}
588 Text.redraw_all(Editor_state)
589 Editor_state.cursor1 = {line=1, pos=1}
590 Editor_state.screen_top1 = {line=1, pos=1}
591 Editor_state.screen_bottom1 = {}
592 edit.draw(Editor_state)
593 -- select a letter
594 App.fake_key_press('lshift')
595 edit.run_after_keychord(Editor_state, 'S-right', 'right')
596 App.fake_key_release('lshift')
597 edit.key_release(Editor_state, 'lshift')
598 -- selection persists even after shift is released
599 check_eq(Editor_state.selection1.line, 1, 'selection:line')
600 check_eq(Editor_state.selection1.pos, 1, 'selection:pos')
601 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
602 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
605 function test_cursor_movement_without_shift_resets_selection()
606 -- display a line of text with some part selected
607 App.screen.init{width=75, height=80}
608 Editor_state = edit.initialize_test_state()
609 Editor_state.lines = load_array{'abc'}
610 Text.redraw_all(Editor_state)
611 Editor_state.cursor1 = {line=1, pos=1}
612 Editor_state.selection1 = {line=1, pos=2}
613 Editor_state.screen_top1 = {line=1, pos=1}
614 Editor_state.screen_bottom1 = {}
615 edit.draw(Editor_state)
616 -- press an arrow key without shift
617 edit.run_after_keychord(Editor_state, 'right', 'right')
618 -- no change to data, selection is reset
619 check_nil(Editor_state.selection1.line, 'check')
620 check_eq(Editor_state.lines[1].data, 'abc', 'data')
623 function test_edit_deletes_selection()
624 -- display a line of text with some part selected
625 App.screen.init{width=75, height=80}
626 Editor_state = edit.initialize_test_state()
627 Editor_state.lines = load_array{'abc'}
628 Text.redraw_all(Editor_state)
629 Editor_state.cursor1 = {line=1, pos=1}
630 Editor_state.selection1 = {line=1, pos=2}
631 Editor_state.screen_top1 = {line=1, pos=1}
632 Editor_state.screen_bottom1 = {}
633 edit.draw(Editor_state)
634 -- press a key
635 edit.run_after_text_input(Editor_state, 'x')
636 -- selected text is deleted and replaced with the key
637 check_eq(Editor_state.lines[1].data, 'xbc', 'check')
640 function test_edit_with_shift_key_deletes_selection()
641 -- display a line of text with some part selected
642 App.screen.init{width=75, height=80}
643 Editor_state = edit.initialize_test_state()
644 Editor_state.lines = load_array{'abc'}
645 Text.redraw_all(Editor_state)
646 Editor_state.cursor1 = {line=1, pos=1}
647 Editor_state.selection1 = {line=1, pos=2}
648 Editor_state.screen_top1 = {line=1, pos=1}
649 Editor_state.screen_bottom1 = {}
650 edit.draw(Editor_state)
651 -- mimic precise keypresses for a capital letter
652 App.fake_key_press('lshift')
653 edit.keychord_press(Editor_state, 'd', 'd')
654 edit.text_input(Editor_state, 'D')
655 edit.key_release(Editor_state, 'd')
656 App.fake_key_release('lshift')
657 -- selected text is deleted and replaced with the key
658 check_nil(Editor_state.selection1.line, 'check')
659 check_eq(Editor_state.lines[1].data, 'Dbc', 'data')
662 function test_copy_does_not_reset_selection()
663 -- display a line of text with a selection
664 App.screen.init{width=75, height=80}
665 Editor_state = edit.initialize_test_state()
666 Editor_state.lines = load_array{'abc'}
667 Text.redraw_all(Editor_state)
668 Editor_state.cursor1 = {line=1, pos=1}
669 Editor_state.selection1 = {line=1, pos=2}
670 Editor_state.screen_top1 = {line=1, pos=1}
671 Editor_state.screen_bottom1 = {}
672 edit.draw(Editor_state)
673 -- copy selection
674 edit.run_after_keychord(Editor_state, 'C-c', 'c')
675 check_eq(App.clipboard, 'a', 'clipboard')
676 -- selection is reset since shift key is not pressed
677 check(Editor_state.selection1.line, 'check')
680 function test_cut()
681 -- display a line of text with some part selected
682 App.screen.init{width=75, height=80}
683 Editor_state = edit.initialize_test_state()
684 Editor_state.lines = load_array{'abc'}
685 Text.redraw_all(Editor_state)
686 Editor_state.cursor1 = {line=1, pos=1}
687 Editor_state.selection1 = {line=1, pos=2}
688 Editor_state.screen_top1 = {line=1, pos=1}
689 Editor_state.screen_bottom1 = {}
690 edit.draw(Editor_state)
691 -- press a key
692 edit.run_after_keychord(Editor_state, 'C-x', 'x')
693 check_eq(App.clipboard, 'a', 'clipboard')
694 -- selected text is deleted
695 check_eq(Editor_state.lines[1].data, 'bc', 'data')
698 function test_paste_replaces_selection()
699 -- display a line of text with a selection
700 App.screen.init{width=75, height=80}
701 Editor_state = edit.initialize_test_state()
702 Editor_state.lines = load_array{'abc', 'def'}
703 Text.redraw_all(Editor_state)
704 Editor_state.cursor1 = {line=2, pos=1}
705 Editor_state.selection1 = {line=1, pos=1}
706 Editor_state.screen_top1 = {line=1, pos=1}
707 Editor_state.screen_bottom1 = {}
708 edit.draw(Editor_state)
709 -- set clipboard
710 App.clipboard = 'xyz'
711 -- paste selection
712 edit.run_after_keychord(Editor_state, 'C-v', 'v')
713 -- selection is reset since shift key is not pressed
714 -- selection includes the newline, so it's also deleted
715 check_eq(Editor_state.lines[1].data, 'xyzdef', 'check')
718 function test_deleting_selection_may_scroll()
719 -- display lines 2/3/4
720 App.screen.init{width=120, height=60}
721 Editor_state = edit.initialize_test_state()
722 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
723 Text.redraw_all(Editor_state)
724 Editor_state.cursor1 = {line=3, pos=2}
725 Editor_state.screen_top1 = {line=2, pos=1}
726 Editor_state.screen_bottom1 = {}
727 edit.draw(Editor_state)
728 local y = Editor_state.top
729 App.screen.check(y, 'def', 'baseline/screen:1')
730 y = y + Editor_state.line_height
731 App.screen.check(y, 'ghi', 'baseline/screen:2')
732 y = y + Editor_state.line_height
733 App.screen.check(y, 'jkl', 'baseline/screen:3')
734 -- set up a selection starting above the currently displayed page
735 Editor_state.selection1 = {line=1, pos=2}
736 -- delete selection
737 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
738 -- page scrolls up
739 check_eq(Editor_state.screen_top1.line, 1, 'check')
740 check_eq(Editor_state.lines[1].data, 'ahi', 'data')
743 function test_edit_wrapping_text()
744 App.screen.init{width=50, height=60}
745 Editor_state = edit.initialize_test_state()
746 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
747 Text.redraw_all(Editor_state)
748 Editor_state.cursor1 = {line=2, pos=4}
749 Editor_state.screen_top1 = {line=1, pos=1}
750 Editor_state.screen_bottom1 = {}
751 edit.draw(Editor_state)
752 edit.run_after_text_input(Editor_state, 'g')
753 local y = Editor_state.top
754 App.screen.check(y, 'abc', 'screen:1')
755 y = y + Editor_state.line_height
756 App.screen.check(y, 'de', 'screen:2')
757 y = y + Editor_state.line_height
758 App.screen.check(y, 'fg', 'screen:3')
761 function test_insert_newline()
762 -- display a few lines
763 App.screen.init{width=Editor_state.left+30, height=60}
764 Editor_state = edit.initialize_test_state()
765 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
766 Text.redraw_all(Editor_state)
767 Editor_state.cursor1 = {line=1, pos=2}
768 Editor_state.screen_top1 = {line=1, pos=1}
769 Editor_state.screen_bottom1 = {}
770 edit.draw(Editor_state)
771 local y = Editor_state.top
772 App.screen.check(y, 'abc', 'baseline/screen:1')
773 y = y + Editor_state.line_height
774 App.screen.check(y, 'def', 'baseline/screen:2')
775 y = y + Editor_state.line_height
776 App.screen.check(y, 'ghi', 'baseline/screen:3')
777 -- hitting the enter key splits the line
778 edit.run_after_keychord(Editor_state, 'return', 'return')
779 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
780 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
781 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
782 y = Editor_state.top
783 App.screen.check(y, 'a', 'screen:1')
784 y = y + Editor_state.line_height
785 App.screen.check(y, 'bc', 'screen:2')
786 y = y + Editor_state.line_height
787 App.screen.check(y, 'def', 'screen:3')
790 function test_insert_newline_at_start_of_line()
791 -- display a line
792 App.screen.init{width=Editor_state.left+30, height=60}
793 Editor_state = edit.initialize_test_state()
794 Editor_state.lines = load_array{'abc'}
795 Text.redraw_all(Editor_state)
796 Editor_state.cursor1 = {line=1, pos=1}
797 Editor_state.screen_top1 = {line=1, pos=1}
798 Editor_state.screen_bottom1 = {}
799 -- hitting the enter key splits the line
800 edit.run_after_keychord(Editor_state, 'return', 'return')
801 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
802 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
803 check_eq(Editor_state.lines[1].data, '', 'data:1')
804 check_eq(Editor_state.lines[2].data, 'abc', 'data:2')
807 function test_insert_from_clipboard()
808 -- display a few lines
809 App.screen.init{width=Editor_state.left+30, height=60}
810 Editor_state = edit.initialize_test_state()
811 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
812 Text.redraw_all(Editor_state)
813 Editor_state.cursor1 = {line=1, pos=2}
814 Editor_state.screen_top1 = {line=1, pos=1}
815 Editor_state.screen_bottom1 = {}
816 edit.draw(Editor_state)
817 local y = Editor_state.top
818 App.screen.check(y, 'abc', 'baseline/screen:1')
819 y = y + Editor_state.line_height
820 App.screen.check(y, 'def', 'baseline/screen:2')
821 y = y + Editor_state.line_height
822 App.screen.check(y, 'ghi', 'baseline/screen:3')
823 -- paste some text including a newline, check that new line is created
824 App.clipboard = 'xy\nz'
825 edit.run_after_keychord(Editor_state, 'C-v', 'v')
826 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
827 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
828 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
829 y = Editor_state.top
830 App.screen.check(y, 'axy', 'screen:1')
831 y = y + Editor_state.line_height
832 App.screen.check(y, 'zbc', 'screen:2')
833 y = y + Editor_state.line_height
834 App.screen.check(y, 'def', 'screen:3')
837 function test_select_text_using_mouse()
838 App.screen.init{width=50, height=60}
839 Editor_state = edit.initialize_test_state()
840 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
841 Text.redraw_all(Editor_state)
842 Editor_state.cursor1 = {line=1, pos=1}
843 Editor_state.screen_top1 = {line=1, pos=1}
844 Editor_state.screen_bottom1 = {}
845 Editor_state.selection1 = {}
846 edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
847 -- press and hold on first location
848 edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
849 -- drag and release somewhere else
850 edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)
851 check_eq(Editor_state.selection1.line, 1, 'selection:line')
852 check_eq(Editor_state.selection1.pos, 2, 'selection:pos')
853 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
854 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
857 function test_select_text_using_mouse_starting_above_text()
858 App.screen.init{width=50, height=60}
859 Editor_state = edit.initialize_test_state()
860 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
861 Text.redraw_all(Editor_state)
862 Editor_state.cursor1 = {line=1, pos=1}
863 Editor_state.screen_top1 = {line=1, pos=1}
864 Editor_state.screen_bottom1 = {}
865 Editor_state.selection1 = {}
866 edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
867 -- press mouse above first line of text
868 edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
869 check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
870 check_eq(Editor_state.selection1.line, 1, 'selection:line')
871 check_eq(Editor_state.selection1.pos, 1, 'selection:pos')
874 function test_select_text_using_mouse_starting_above_text_wrapping_line()
875 -- first screen line starts in the middle of a line
876 App.screen.init{width=50, height=60}
877 Editor_state = edit.initialize_test_state()
878 Editor_state.lines = load_array{'abc', 'defgh', 'xyz'}
879 Text.redraw_all(Editor_state)
880 Editor_state.cursor1 = {line=2, pos=5}
881 Editor_state.screen_top1 = {line=2, pos=3}
882 Editor_state.screen_bottom1 = {}
883 -- press mouse above first line of text
884 edit.draw(Editor_state)
885 edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
886 -- selection is at screen top
887 check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
888 check_eq(Editor_state.selection1.line, 2, 'selection:line')
889 check_eq(Editor_state.selection1.pos, 3, 'selection:pos')
892 function test_select_text_using_mouse_starting_below_text()
893 -- I'd like to test what happens when a mouse click is below some page of
894 -- text, potentially even in the middle of a line.
895 -- However, it's brittle to set up a text line boundary just right.
896 -- So I'm going to just check things below the bottom of the final line of
897 -- text when it's in the middle of the screen.
898 -- final screen line ends in the middle of screen
899 App.screen.init{width=50, height=60}
900 Editor_state = edit.initialize_test_state()
901 Editor_state.lines = load_array{'abcde'}
902 Text.redraw_all(Editor_state)
903 Editor_state.cursor1 = {line=1, pos=1}
904 Editor_state.screen_top1 = {line=1, pos=1}
905 Editor_state.screen_bottom1 = {}
906 edit.draw(Editor_state)
907 local y = Editor_state.top
908 App.screen.check(y, 'ab', 'baseline:screen:1')
909 y = y + Editor_state.line_height
910 App.screen.check(y, 'cde', 'baseline:screen:2')
911 -- press mouse above first line of text
912 edit.run_after_mouse_press(Editor_state, 5,App.screen.height-5, 1)
913 -- selection is past bottom-most text in screen
914 check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
915 check_eq(Editor_state.selection1.line, 1, 'selection:line')
916 check_eq(Editor_state.selection1.pos, 6, 'selection:pos')
919 function test_select_text_using_mouse_and_shift()
920 App.screen.init{width=50, height=60}
921 Editor_state = edit.initialize_test_state()
922 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
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 somewhere else
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 App.fake_key_release('lshift')
937 check_eq(Editor_state.selection1.line, 1, 'selection:line')
938 check_eq(Editor_state.selection1.pos, 2, 'selection:pos')
939 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
940 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
943 function test_select_text_repeatedly_using_mouse_and_shift()
944 App.screen.init{width=50, height=60}
945 Editor_state = edit.initialize_test_state()
946 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
947 Text.redraw_all(Editor_state)
948 Text.redraw_all(Editor_state)
949 Editor_state.cursor1 = {line=1, pos=1}
950 Editor_state.screen_top1 = {line=1, pos=1}
951 Editor_state.screen_bottom1 = {}
952 Editor_state.selection1 = {}
953 edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
954 -- click on first location
955 edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
956 edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
957 -- hold down shift and click on a second location
958 App.fake_key_press('lshift')
959 edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)
960 edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)
961 -- hold down shift and click at a third location
962 App.fake_key_press('lshift')
963 edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)
964 edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+Editor_state.line_height+5, 1)
965 App.fake_key_release('lshift')
966 -- selection is between first and third location. forget the second location, not the first.
967 check_eq(Editor_state.selection1.line, 1, 'selection:line')
968 check_eq(Editor_state.selection1.pos, 2, 'selection:pos')
969 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
970 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
973 function test_select_all_text()
974 -- display a single line of text
975 App.screen.init{width=75, height=80}
976 Editor_state = edit.initialize_test_state()
977 Editor_state.lines = load_array{'abc def'}
978 Text.redraw_all(Editor_state)
979 Editor_state.cursor1 = {line=1, pos=1}
980 Editor_state.screen_top1 = {line=1, pos=1}
981 Editor_state.screen_bottom1 = {}
982 edit.draw(Editor_state)
983 -- select all
984 App.fake_key_press('lctrl')
985 edit.run_after_keychord(Editor_state, 'C-a', 'a')
986 App.fake_key_release('lctrl')
987 edit.key_release(Editor_state, 'lctrl')
988 -- selection
989 check_eq(Editor_state.selection1.line, 1, 'selection:line')
990 check_eq(Editor_state.selection1.pos, 1, 'selection:pos')
991 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
992 check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
995 function test_cut_without_selection()
996 -- display a few lines
997 App.screen.init{width=Editor_state.left+30, height=60}
998 Editor_state = edit.initialize_test_state()
999 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1000 Text.redraw_all(Editor_state)
1001 Editor_state.cursor1 = {line=1, pos=2}
1002 Editor_state.screen_top1 = {line=1, pos=1}
1003 Editor_state.screen_bottom1 = {}
1004 Editor_state.selection1 = {}
1005 edit.draw(Editor_state)
1006 -- try to cut without selecting text
1007 edit.run_after_keychord(Editor_state, 'C-x', 'x')
1008 -- no crash
1009 check_nil(Editor_state.selection1.line, 'check')
1012 function test_pagedown()
1013 App.screen.init{width=120, height=45}
1014 Editor_state = edit.initialize_test_state()
1015 Editor_state.lines = load_array{'abc', 'def', 'ghi'}
1016 Text.redraw_all(Editor_state)
1017 Editor_state.cursor1 = {line=1, pos=1}
1018 Editor_state.screen_top1 = {line=1, pos=1}
1019 Editor_state.screen_bottom1 = {}
1020 -- initially the first two lines are displayed
1021 edit.draw(Editor_state)
1022 local y = Editor_state.top
1023 App.screen.check(y, 'abc', 'baseline/screen:1')
1024 y = y + Editor_state.line_height
1025 App.screen.check(y, 'def', 'baseline/screen:2')
1026 -- after pagedown the bottom line becomes the top
1027 edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
1028 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1029 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1030 y = Editor_state.top
1031 App.screen.check(y, 'def', 'screen:1')
1032 y = y + Editor_state.line_height
1033 App.screen.check(y, 'ghi', 'screen:2')
1036 function test_pagedown_skips_drawings()
1037 -- some lines of text with a drawing intermixed
1038 local drawing_width = 50
1039 App.screen.init{width=Editor_state.left+drawing_width, height=80}
1040 Editor_state = edit.initialize_test_state()
1041 Editor_state.lines = load_array{'abc', -- height 15
1042 '```lines', '```', -- height 25
1043 'def', -- height 15
1044 'ghi'} -- height 15
1045 Text.redraw_all(Editor_state)
1046 check_eq(Editor_state.lines[2].mode, 'drawing', 'baseline/lines')
1047 Editor_state.cursor1 = {line=1, pos=1}
1048 Editor_state.screen_top1 = {line=1, pos=1}
1049 Editor_state.screen_bottom1 = {}
1050 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1051 -- initially the screen displays the first line and the drawing
1052 -- 15px margin + 15px line1 + 10px margin + 25px drawing + 10px margin = 75px < screen height 80px
1053 edit.draw(Editor_state)
1054 local y = Editor_state.top
1055 App.screen.check(y, 'abc', 'baseline/screen:1')
1056 -- after pagedown the screen draws the drawing up top
1057 -- 15px margin + 10px margin + 25px drawing + 10px margin + 15px line3 = 75px < screen height 80px
1058 edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
1059 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1060 check_eq(Editor_state.cursor1.line, 3, 'cursor')
1061 y = Editor_state.top + drawing_height
1062 App.screen.check(y, 'def', 'screen:1')
1065 function test_pagedown_can_start_from_middle_of_long_wrapping_line()
1066 -- draw a few lines starting from a very long wrapping line
1067 App.screen.init{width=Editor_state.left+30, height=60}
1068 Editor_state = edit.initialize_test_state()
1069 Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu vwx yza bcd efg hij', 'XYZ'}
1070 Text.redraw_all(Editor_state)
1071 Editor_state.cursor1 = {line=1, pos=2}
1072 Editor_state.screen_top1 = {line=1, pos=1}
1073 Editor_state.screen_bottom1 = {}
1074 edit.draw(Editor_state)
1075 local y = Editor_state.top
1076 App.screen.check(y, 'abc ', 'baseline/screen:1')
1077 y = y + Editor_state.line_height
1078 App.screen.check(y, 'def ', 'baseline/screen:2')
1079 y = y + Editor_state.line_height
1080 App.screen.check(y, 'ghi ', 'baseline/screen:3')
1081 -- after pagedown we scroll down the very long wrapping line
1082 edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
1083 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1084 check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
1085 y = Editor_state.top
1086 App.screen.check(y, 'ghi ', 'screen:1')
1087 y = y + Editor_state.line_height
1088 App.screen.check(y, 'jkl ', 'screen:2')
1089 y = y + Editor_state.line_height
1090 if Version == '12.0' then
1091 -- HACK: Maybe v12.0 uses a different font? Strange that it only causes
1092 -- issues in a couple of places.
1093 -- We'll need to rethink our tests if issues like this start to multiply.
1094 App.screen.check(y, 'mno ', 'screen:3')
1095 else
1096 App.screen.check(y, 'mn', 'screen:3')
1100 function test_pagedown_never_moves_up()
1101 -- draw the final screen line of a wrapping line
1102 App.screen.init{width=Editor_state.left+30, height=60}
1103 Editor_state = edit.initialize_test_state()
1104 Editor_state.lines = load_array{'abc def ghi'}
1105 Text.redraw_all(Editor_state)
1106 Editor_state.cursor1 = {line=1, pos=9}
1107 Editor_state.screen_top1 = {line=1, pos=9}
1108 Editor_state.screen_bottom1 = {}
1109 edit.draw(Editor_state)
1110 -- pagedown makes no change
1111 edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
1112 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1113 check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
1116 function test_down_arrow_moves_cursor()
1117 App.screen.init{width=120, height=60}
1118 Editor_state = edit.initialize_test_state()
1119 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1120 Text.redraw_all(Editor_state)
1121 Editor_state.cursor1 = {line=1, pos=1}
1122 Editor_state.screen_top1 = {line=1, pos=1}
1123 Editor_state.screen_bottom1 = {}
1124 -- initially the first three lines are displayed
1125 edit.draw(Editor_state)
1126 local y = Editor_state.top
1127 App.screen.check(y, 'abc', 'baseline/screen:1')
1128 y = y + Editor_state.line_height
1129 App.screen.check(y, 'def', 'baseline/screen:2')
1130 y = y + Editor_state.line_height
1131 App.screen.check(y, 'ghi', 'baseline/screen:3')
1132 -- after hitting the down arrow, the cursor moves down by 1 line
1133 edit.run_after_keychord(Editor_state, 'down', 'down')
1134 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1135 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1136 -- the screen is unchanged
1137 y = Editor_state.top
1138 App.screen.check(y, 'abc', 'screen:1')
1139 y = y + Editor_state.line_height
1140 App.screen.check(y, 'def', 'screen:2')
1141 y = y + Editor_state.line_height
1142 App.screen.check(y, 'ghi', 'screen:3')
1145 function test_down_arrow_skips_drawing()
1146 -- some lines of text with a drawing intermixed
1147 local drawing_width = 50
1148 App.screen.init{width=Editor_state.left+drawing_width, height=100}
1149 Editor_state = edit.initialize_test_state()
1150 Editor_state.lines = load_array{'abc', -- height 15
1151 '```lines', '```', -- height 25
1152 'ghi'}
1153 Text.redraw_all(Editor_state)
1154 Editor_state.cursor1 = {line=1, pos=1}
1155 Editor_state.screen_top1 = {line=1, pos=1}
1156 Editor_state.screen_bottom1 = {}
1157 edit.draw(Editor_state)
1158 local y = Editor_state.top
1159 App.screen.check(y, 'abc', 'baseline/screen:1')
1160 y = y + Editor_state.line_height
1161 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1162 y = y + drawing_height
1163 App.screen.check(y, 'ghi', 'baseline/screen:3')
1164 check(Editor_state.cursor_x, 'baseline/cursor_x')
1165 -- after hitting the down arrow the cursor moves down by 2 lines, skipping the drawing
1166 edit.run_after_keychord(Editor_state, 'down', 'down')
1167 check_eq(Editor_state.cursor1.line, 3, 'cursor')
1170 function test_down_arrow_scrolls_down_by_one_line()
1171 -- display the first three lines with the cursor on the bottom line
1172 App.screen.init{width=120, height=60}
1173 Editor_state = edit.initialize_test_state()
1174 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1175 Text.redraw_all(Editor_state)
1176 Editor_state.cursor1 = {line=3, pos=1}
1177 Editor_state.screen_top1 = {line=1, pos=1}
1178 Editor_state.screen_bottom1 = {}
1179 edit.draw(Editor_state)
1180 local y = Editor_state.top
1181 App.screen.check(y, 'abc', 'baseline/screen:1')
1182 y = y + Editor_state.line_height
1183 App.screen.check(y, 'def', 'baseline/screen:2')
1184 y = y + Editor_state.line_height
1185 App.screen.check(y, 'ghi', 'baseline/screen:3')
1186 -- after hitting the down arrow the screen scrolls down by one line
1187 edit.run_after_keychord(Editor_state, 'down', 'down')
1188 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1189 check_eq(Editor_state.cursor1.line, 4, 'cursor')
1190 y = Editor_state.top
1191 App.screen.check(y, 'def', 'screen:1')
1192 y = y + Editor_state.line_height
1193 App.screen.check(y, 'ghi', 'screen:2')
1194 y = y + Editor_state.line_height
1195 App.screen.check(y, 'jkl', 'screen:3')
1198 function test_down_arrow_scrolls_down_by_one_screen_line()
1199 -- display the first three lines with the cursor on the bottom line
1200 App.screen.init{width=Editor_state.left+30, height=60}
1201 Editor_state = edit.initialize_test_state()
1202 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1203 Text.redraw_all(Editor_state)
1204 Editor_state.cursor1 = {line=3, pos=1}
1205 Editor_state.screen_top1 = {line=1, pos=1}
1206 Editor_state.screen_bottom1 = {}
1207 edit.draw(Editor_state)
1208 local y = Editor_state.top
1209 App.screen.check(y, 'abc', 'baseline/screen:1')
1210 y = y + Editor_state.line_height
1211 App.screen.check(y, 'def', 'baseline/screen:2')
1212 y = y + Editor_state.line_height
1213 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1214 -- after hitting the down arrow the screen scrolls down by one line
1215 edit.run_after_keychord(Editor_state, 'down', 'down')
1216 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1217 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1218 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1219 y = Editor_state.top
1220 App.screen.check(y, 'def', 'screen:1')
1221 y = y + Editor_state.line_height
1222 App.screen.check(y, 'ghi ', 'screen:2')
1223 y = y + Editor_state.line_height
1224 App.screen.check(y, 'jkl', 'screen:3')
1227 function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_word()
1228 -- display the first three lines with the cursor on the bottom line
1229 App.screen.init{width=Editor_state.left+30, height=60}
1230 Editor_state = edit.initialize_test_state()
1231 Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
1232 Text.redraw_all(Editor_state)
1233 Editor_state.cursor1 = {line=3, pos=1}
1234 Editor_state.screen_top1 = {line=1, pos=1}
1235 Editor_state.screen_bottom1 = {}
1236 edit.draw(Editor_state)
1237 local y = Editor_state.top
1238 App.screen.check(y, 'abc', 'baseline/screen:1')
1239 y = y + Editor_state.line_height
1240 App.screen.check(y, 'def', 'baseline/screen:2')
1241 y = y + Editor_state.line_height
1242 App.screen.check(y, 'ghij', 'baseline/screen:3')
1243 -- after hitting the down arrow the screen scrolls down by one line
1244 edit.run_after_keychord(Editor_state, 'down', 'down')
1245 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1246 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1247 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1248 y = Editor_state.top
1249 App.screen.check(y, 'def', 'screen:1')
1250 y = y + Editor_state.line_height
1251 App.screen.check(y, 'ghij', 'screen:2')
1252 y = y + Editor_state.line_height
1253 App.screen.check(y, 'kl', 'screen:3')
1256 function test_pagedown_followed_by_down_arrow_does_not_scroll_screen_up()
1257 App.screen.init{width=Editor_state.left+30, height=60}
1258 Editor_state = edit.initialize_test_state()
1259 Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
1260 Text.redraw_all(Editor_state)
1261 Editor_state.cursor1 = {line=3, pos=1}
1262 Editor_state.screen_top1 = {line=1, pos=1}
1263 Editor_state.screen_bottom1 = {}
1264 edit.draw(Editor_state)
1265 local y = Editor_state.top
1266 App.screen.check(y, 'abc', 'baseline/screen:1')
1267 y = y + Editor_state.line_height
1268 App.screen.check(y, 'def', 'baseline/screen:2')
1269 y = y + Editor_state.line_height
1270 App.screen.check(y, 'ghij', 'baseline/screen:3')
1271 -- after hitting pagedown the screen scrolls down to start of a long line
1272 edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
1273 check_eq(Editor_state.screen_top1.line, 3, 'baseline2/screen_top')
1274 check_eq(Editor_state.cursor1.line, 3, 'baseline2/cursor:line')
1275 check_eq(Editor_state.cursor1.pos, 1, 'baseline2/cursor:pos')
1276 -- after hitting down arrow the screen doesn't scroll down further, and certainly doesn't scroll up
1277 edit.run_after_keychord(Editor_state, 'down', 'down')
1278 check_eq(Editor_state.screen_top1.line, 3, 'screen_top')
1279 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1280 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1281 y = Editor_state.top
1282 App.screen.check(y, 'ghij', 'screen:1')
1283 y = y + Editor_state.line_height
1284 App.screen.check(y, 'kl', 'screen:2')
1285 y = y + Editor_state.line_height
1286 App.screen.check(y, 'mno', 'screen:3')
1289 function test_up_arrow_moves_cursor()
1290 -- display the first 3 lines with the cursor on the bottom line
1291 App.screen.init{width=120, height=60}
1292 Editor_state = edit.initialize_test_state()
1293 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1294 Text.redraw_all(Editor_state)
1295 Editor_state.cursor1 = {line=3, pos=1}
1296 Editor_state.screen_top1 = {line=1, pos=1}
1297 Editor_state.screen_bottom1 = {}
1298 edit.draw(Editor_state)
1299 local y = Editor_state.top
1300 App.screen.check(y, 'abc', 'baseline/screen:1')
1301 y = y + Editor_state.line_height
1302 App.screen.check(y, 'def', 'baseline/screen:2')
1303 y = y + Editor_state.line_height
1304 App.screen.check(y, 'ghi', 'baseline/screen:3')
1305 -- after hitting the up arrow the cursor moves up by 1 line
1306 edit.run_after_keychord(Editor_state, 'up', 'up')
1307 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1308 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1309 -- the screen is unchanged
1310 y = Editor_state.top
1311 App.screen.check(y, 'abc', 'screen:1')
1312 y = y + Editor_state.line_height
1313 App.screen.check(y, 'def', 'screen:2')
1314 y = y + Editor_state.line_height
1315 App.screen.check(y, 'ghi', 'screen:3')
1318 function test_up_arrow_skips_drawing()
1319 -- some lines of text with a drawing intermixed
1320 local drawing_width = 50
1321 App.screen.init{width=Editor_state.left+drawing_width, height=100}
1322 Editor_state = edit.initialize_test_state()
1323 Editor_state.lines = load_array{'abc', -- height 15
1324 '```lines', '```', -- height 25
1325 'ghi'}
1326 Text.redraw_all(Editor_state)
1327 Editor_state.cursor1 = {line=3, pos=1}
1328 Editor_state.screen_top1 = {line=1, pos=1}
1329 Editor_state.screen_bottom1 = {}
1330 edit.draw(Editor_state)
1331 local y = Editor_state.top
1332 App.screen.check(y, 'abc', 'baseline/screen:1')
1333 y = y + Editor_state.line_height
1334 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1335 y = y + drawing_height
1336 App.screen.check(y, 'ghi', 'baseline/screen:3')
1337 check(Editor_state.cursor_x, 'baseline/cursor_x')
1338 -- after hitting the up arrow the cursor moves up by 2 lines, skipping the drawing
1339 edit.run_after_keychord(Editor_state, 'up', 'up')
1340 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1343 function test_up_arrow_scrolls_up_by_one_line()
1344 -- display the lines 2/3/4 with the cursor on line 2
1345 App.screen.init{width=120, height=60}
1346 Editor_state = edit.initialize_test_state()
1347 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1348 Text.redraw_all(Editor_state)
1349 Editor_state.cursor1 = {line=2, pos=1}
1350 Editor_state.screen_top1 = {line=2, pos=1}
1351 Editor_state.screen_bottom1 = {}
1352 edit.draw(Editor_state)
1353 local y = Editor_state.top
1354 App.screen.check(y, 'def', 'baseline/screen:1')
1355 y = y + Editor_state.line_height
1356 App.screen.check(y, 'ghi', 'baseline/screen:2')
1357 y = y + Editor_state.line_height
1358 App.screen.check(y, 'jkl', 'baseline/screen:3')
1359 -- after hitting the up arrow the screen scrolls up by one line
1360 edit.run_after_keychord(Editor_state, 'up', 'up')
1361 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1362 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1363 y = Editor_state.top
1364 App.screen.check(y, 'abc', 'screen:1')
1365 y = y + Editor_state.line_height
1366 App.screen.check(y, 'def', 'screen:2')
1367 y = y + Editor_state.line_height
1368 App.screen.check(y, 'ghi', 'screen:3')
1371 function test_up_arrow_scrolls_up_by_one_line_skipping_drawing()
1372 -- display lines 3/4/5 with a drawing just off screen at line 2
1373 App.screen.init{width=120, height=60}
1374 Editor_state = edit.initialize_test_state()
1375 Editor_state.lines = load_array{'abc', '```lines', '```', 'def', 'ghi', 'jkl'}
1376 Text.redraw_all(Editor_state)
1377 Editor_state.cursor1 = {line=3, pos=1}
1378 Editor_state.screen_top1 = {line=3, pos=1}
1379 Editor_state.screen_bottom1 = {}
1380 edit.draw(Editor_state)
1381 local y = Editor_state.top
1382 App.screen.check(y, 'def', 'baseline/screen:1')
1383 y = y + Editor_state.line_height
1384 App.screen.check(y, 'ghi', 'baseline/screen:2')
1385 y = y + Editor_state.line_height
1386 App.screen.check(y, 'jkl', 'baseline/screen:3')
1387 -- after hitting the up arrow the screen scrolls up to previous text line
1388 edit.run_after_keychord(Editor_state, 'up', 'up')
1389 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1390 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1393 function test_up_arrow_scrolls_up_by_one_screen_line()
1394 -- display lines starting from second screen line of a line
1395 App.screen.init{width=Editor_state.left+30, height=60}
1396 Editor_state = edit.initialize_test_state()
1397 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1398 Text.redraw_all(Editor_state)
1399 Editor_state.cursor1 = {line=3, pos=6}
1400 Editor_state.screen_top1 = {line=3, pos=5}
1401 Editor_state.screen_bottom1 = {}
1402 edit.draw(Editor_state)
1403 local y = Editor_state.top
1404 App.screen.check(y, 'jkl', 'baseline/screen:1')
1405 y = y + Editor_state.line_height
1406 App.screen.check(y, 'mno', 'baseline/screen:2')
1407 -- after hitting the up arrow the screen scrolls up to first screen line
1408 edit.run_after_keychord(Editor_state, 'up', 'up')
1409 y = Editor_state.top
1410 App.screen.check(y, 'ghi ', 'screen:1')
1411 y = y + Editor_state.line_height
1412 App.screen.check(y, 'jkl', 'screen:2')
1413 y = y + Editor_state.line_height
1414 App.screen.check(y, 'mno', 'screen:3')
1415 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1416 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1417 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1418 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1421 function test_up_arrow_scrolls_up_to_final_screen_line()
1422 -- display lines starting just after a long line
1423 App.screen.init{width=Editor_state.left+30, height=60}
1424 Editor_state = edit.initialize_test_state()
1425 Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
1426 Text.redraw_all(Editor_state)
1427 Editor_state.cursor1 = {line=2, pos=1}
1428 Editor_state.screen_top1 = {line=2, pos=1}
1429 Editor_state.screen_bottom1 = {}
1430 edit.draw(Editor_state)
1431 local y = Editor_state.top
1432 App.screen.check(y, 'ghi', 'baseline/screen:1')
1433 y = y + Editor_state.line_height
1434 App.screen.check(y, 'jkl', 'baseline/screen:2')
1435 y = y + Editor_state.line_height
1436 App.screen.check(y, 'mno', 'baseline/screen:3')
1437 -- after hitting the up arrow the screen scrolls up to final screen line of previous line
1438 edit.run_after_keychord(Editor_state, 'up', 'up')
1439 y = Editor_state.top
1440 App.screen.check(y, 'def', 'screen:1')
1441 y = y + Editor_state.line_height
1442 App.screen.check(y, 'ghi', 'screen:2')
1443 y = y + Editor_state.line_height
1444 App.screen.check(y, 'jkl', 'screen:3')
1445 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1446 check_eq(Editor_state.screen_top1.pos, 5, 'screen_top:pos')
1447 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1448 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1451 function test_up_arrow_scrolls_up_to_empty_line()
1452 -- display a screenful of text with an empty line just above it outside the screen
1453 App.screen.init{width=120, height=60}
1454 Editor_state = edit.initialize_test_state()
1455 Editor_state.lines = load_array{'', 'abc', 'def', 'ghi', 'jkl'}
1456 Text.redraw_all(Editor_state)
1457 Editor_state.cursor1 = {line=2, pos=1}
1458 Editor_state.screen_top1 = {line=2, pos=1}
1459 Editor_state.screen_bottom1 = {}
1460 edit.draw(Editor_state)
1461 local y = Editor_state.top
1462 App.screen.check(y, 'abc', 'baseline/screen:1')
1463 y = y + Editor_state.line_height
1464 App.screen.check(y, 'def', 'baseline/screen:2')
1465 y = y + Editor_state.line_height
1466 App.screen.check(y, 'ghi', 'baseline/screen:3')
1467 -- after hitting the up arrow the screen scrolls up by one line
1468 edit.run_after_keychord(Editor_state, 'up', 'up')
1469 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1470 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1471 y = Editor_state.top
1472 -- empty first line
1473 y = y + Editor_state.line_height
1474 App.screen.check(y, 'abc', 'screen:2')
1475 y = y + Editor_state.line_height
1476 App.screen.check(y, 'def', 'screen:3')
1479 function test_pageup()
1480 App.screen.init{width=120, height=45}
1481 Editor_state = edit.initialize_test_state()
1482 Editor_state.lines = load_array{'abc', 'def', 'ghi'}
1483 Text.redraw_all(Editor_state)
1484 Editor_state.cursor1 = {line=2, pos=1}
1485 Editor_state.screen_top1 = {line=2, pos=1}
1486 Editor_state.screen_bottom1 = {}
1487 -- initially the last two lines are displayed
1488 edit.draw(Editor_state)
1489 local y = Editor_state.top
1490 App.screen.check(y, 'def', 'baseline/screen:1')
1491 y = y + Editor_state.line_height
1492 App.screen.check(y, 'ghi', 'baseline/screen:2')
1493 -- after pageup the cursor goes to first line
1494 edit.run_after_keychord(Editor_state, 'pageup', 'pageup')
1495 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1496 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1497 y = Editor_state.top
1498 App.screen.check(y, 'abc', 'screen:1')
1499 y = y + Editor_state.line_height
1500 App.screen.check(y, 'def', 'screen:2')
1503 function test_pageup_scrolls_up_by_screen_line()
1504 -- display the first three lines with the cursor on the bottom line
1505 App.screen.init{width=Editor_state.left+30, height=60}
1506 Editor_state = edit.initialize_test_state()
1507 Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
1508 Text.redraw_all(Editor_state)
1509 Editor_state.cursor1 = {line=2, pos=1}
1510 Editor_state.screen_top1 = {line=2, pos=1}
1511 Editor_state.screen_bottom1 = {}
1512 edit.draw(Editor_state)
1513 local y = Editor_state.top
1514 App.screen.check(y, 'ghi', 'baseline/screen:1')
1515 y = y + Editor_state.line_height
1516 App.screen.check(y, 'jkl', 'baseline/screen:2')
1517 y = y + Editor_state.line_height
1518 App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1519 -- after hitting the page-up key the screen scrolls up to top
1520 edit.run_after_keychord(Editor_state, 'pageup', 'pageup')
1521 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1522 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1523 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1524 y = Editor_state.top
1525 App.screen.check(y, 'abc ', 'screen:1')
1526 y = y + Editor_state.line_height
1527 App.screen.check(y, 'def', 'screen:2')
1528 y = y + Editor_state.line_height
1529 App.screen.check(y, 'ghi', 'screen:3')
1532 function test_pageup_scrolls_up_from_middle_screen_line()
1533 -- display a few lines starting from the middle of a line (Editor_state.cursor1.pos > 1)
1534 App.screen.init{width=Editor_state.left+30, height=60}
1535 Editor_state = edit.initialize_test_state()
1536 Editor_state.lines = load_array{'abc def', 'ghi jkl', 'mno'}
1537 Text.redraw_all(Editor_state)
1538 Editor_state.cursor1 = {line=2, pos=5}
1539 Editor_state.screen_top1 = {line=2, pos=5}
1540 Editor_state.screen_bottom1 = {}
1541 edit.draw(Editor_state)
1542 local y = Editor_state.top
1543 App.screen.check(y, 'jkl', 'baseline/screen:2')
1544 y = y + Editor_state.line_height
1545 App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1546 -- after hitting the page-up key the screen scrolls up to top
1547 edit.run_after_keychord(Editor_state, 'pageup', 'pageup')
1548 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1549 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1550 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1551 y = Editor_state.top
1552 App.screen.check(y, 'abc ', 'screen:1')
1553 y = y + Editor_state.line_height
1554 App.screen.check(y, 'def', 'screen:2')
1555 y = y + Editor_state.line_height
1556 App.screen.check(y, 'ghi ', 'screen:3')
1559 function test_enter_on_bottom_line_scrolls_down()
1560 -- display a few lines with cursor on bottom line
1561 App.screen.init{width=Editor_state.left+30, height=60}
1562 Editor_state = edit.initialize_test_state()
1563 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1564 Text.redraw_all(Editor_state)
1565 Editor_state.cursor1 = {line=3, pos=2}
1566 Editor_state.screen_top1 = {line=1, pos=1}
1567 Editor_state.screen_bottom1 = {}
1568 edit.draw(Editor_state)
1569 local y = Editor_state.top
1570 App.screen.check(y, 'abc', 'baseline/screen:1')
1571 y = y + Editor_state.line_height
1572 App.screen.check(y, 'def', 'baseline/screen:2')
1573 y = y + Editor_state.line_height
1574 App.screen.check(y, 'ghi', 'baseline/screen:3')
1575 -- after hitting the enter key the screen scrolls down
1576 edit.run_after_keychord(Editor_state, 'return', 'return')
1577 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1578 check_eq(Editor_state.cursor1.line, 4, 'cursor:line')
1579 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1580 y = Editor_state.top
1581 App.screen.check(y, 'def', 'screen:1')
1582 y = y + Editor_state.line_height
1583 App.screen.check(y, 'g', 'screen:2')
1584 y = y + Editor_state.line_height
1585 App.screen.check(y, 'hi', 'screen:3')
1588 function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1589 -- display just the bottom line on screen
1590 App.screen.init{width=Editor_state.left+30, height=60}
1591 Editor_state = edit.initialize_test_state()
1592 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1593 Text.redraw_all(Editor_state)
1594 Editor_state.cursor1 = {line=4, pos=2}
1595 Editor_state.screen_top1 = {line=4, pos=1}
1596 Editor_state.screen_bottom1 = {}
1597 edit.draw(Editor_state)
1598 local y = Editor_state.top
1599 App.screen.check(y, 'jkl', 'baseline/screen:1')
1600 -- after hitting the enter key the screen does not scroll down
1601 edit.run_after_keychord(Editor_state, 'return', 'return')
1602 check_eq(Editor_state.screen_top1.line, 4, 'screen_top')
1603 check_eq(Editor_state.cursor1.line, 5, 'cursor:line')
1604 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1605 y = Editor_state.top
1606 App.screen.check(y, 'j', 'screen:1')
1607 y = y + Editor_state.line_height
1608 App.screen.check(y, 'kl', 'screen:2')
1611 function test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1612 -- display just an empty bottom line on screen
1613 App.screen.init{width=Editor_state.left+30, height=60}
1614 Editor_state = edit.initialize_test_state()
1615 Editor_state.lines = load_array{'abc', ''}
1616 Text.redraw_all(Editor_state)
1617 Editor_state.cursor1 = {line=2, pos=1}
1618 Editor_state.screen_top1 = {line=2, pos=1}
1619 Editor_state.screen_bottom1 = {}
1620 edit.draw(Editor_state)
1621 -- after hitting the inserting_text key the screen does not scroll down
1622 edit.run_after_text_input(Editor_state, 'a')
1623 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1624 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1625 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1626 local y = Editor_state.top
1627 App.screen.check(y, 'a', 'screen:1')
1630 function test_typing_on_bottom_line_scrolls_down()
1631 -- display a few lines with cursor on bottom line
1632 App.screen.init{width=Editor_state.left+30, height=60}
1633 Editor_state = edit.initialize_test_state()
1634 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1635 Text.redraw_all(Editor_state)
1636 Editor_state.cursor1 = {line=3, pos=4}
1637 Editor_state.screen_top1 = {line=1, pos=1}
1638 Editor_state.screen_bottom1 = {}
1639 edit.draw(Editor_state)
1640 local y = Editor_state.top
1641 App.screen.check(y, 'abc', 'baseline/screen:1')
1642 y = y + Editor_state.line_height
1643 App.screen.check(y, 'def', 'baseline/screen:2')
1644 y = y + Editor_state.line_height
1645 App.screen.check(y, 'ghi', 'baseline/screen:3')
1646 -- after typing something the line wraps and the screen scrolls down
1647 edit.run_after_text_input(Editor_state, 'j')
1648 edit.run_after_text_input(Editor_state, 'k')
1649 edit.run_after_text_input(Editor_state, 'l')
1650 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1651 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1652 check_eq(Editor_state.cursor1.pos, 7, 'cursor:pos')
1653 y = Editor_state.top
1654 App.screen.check(y, 'def', 'screen:1')
1655 y = y + Editor_state.line_height
1656 App.screen.check(y, 'ghij', 'screen:2')
1657 y = y + Editor_state.line_height
1658 App.screen.check(y, 'kl', 'screen:3')
1661 function test_left_arrow_scrolls_up_in_wrapped_line()
1662 -- display lines starting from second screen line of a line
1663 App.screen.init{width=Editor_state.left+30, height=60}
1664 Editor_state = edit.initialize_test_state()
1665 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1666 Text.redraw_all(Editor_state)
1667 Editor_state.screen_top1 = {line=3, pos=5}
1668 Editor_state.screen_bottom1 = {}
1669 -- cursor is at top of screen
1670 Editor_state.cursor1 = {line=3, pos=5}
1671 edit.draw(Editor_state)
1672 local y = Editor_state.top
1673 App.screen.check(y, 'jkl', 'baseline/screen:1')
1674 y = y + Editor_state.line_height
1675 App.screen.check(y, 'mno', 'baseline/screen:2')
1676 -- after hitting the left arrow the screen scrolls up to first screen line
1677 edit.run_after_keychord(Editor_state, 'left', 'left')
1678 y = Editor_state.top
1679 App.screen.check(y, 'ghi ', 'screen:1')
1680 y = y + Editor_state.line_height
1681 App.screen.check(y, 'jkl', 'screen:2')
1682 y = y + Editor_state.line_height
1683 App.screen.check(y, 'mno', 'screen:3')
1684 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1685 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1686 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1687 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
1690 function test_right_arrow_scrolls_down_in_wrapped_line()
1691 -- display the first three lines with the cursor on the bottom line
1692 App.screen.init{width=Editor_state.left+30, height=60}
1693 Editor_state = edit.initialize_test_state()
1694 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1695 Text.redraw_all(Editor_state)
1696 Editor_state.screen_top1 = {line=1, pos=1}
1697 Editor_state.screen_bottom1 = {}
1698 -- cursor is at bottom right of screen
1699 Editor_state.cursor1 = {line=3, pos=5}
1700 edit.draw(Editor_state)
1701 local y = Editor_state.top
1702 App.screen.check(y, 'abc', 'baseline/screen:1')
1703 y = y + Editor_state.line_height
1704 App.screen.check(y, 'def', 'baseline/screen:2')
1705 y = y + Editor_state.line_height
1706 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1707 -- after hitting the right arrow the screen scrolls down by one line
1708 edit.run_after_keychord(Editor_state, 'right', 'right')
1709 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1710 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1711 check_eq(Editor_state.cursor1.pos, 6, 'cursor:pos')
1712 y = Editor_state.top
1713 App.screen.check(y, 'def', 'screen:1')
1714 y = y + Editor_state.line_height
1715 App.screen.check(y, 'ghi ', 'screen:2')
1716 y = y + Editor_state.line_height
1717 App.screen.check(y, 'jkl', 'screen:3')
1720 function test_home_scrolls_up_in_wrapped_line()
1721 -- display lines starting from second screen line of a line
1722 App.screen.init{width=Editor_state.left+30, height=60}
1723 Editor_state = edit.initialize_test_state()
1724 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1725 Text.redraw_all(Editor_state)
1726 Editor_state.screen_top1 = {line=3, pos=5}
1727 Editor_state.screen_bottom1 = {}
1728 -- cursor is at top of screen
1729 Editor_state.cursor1 = {line=3, pos=5}
1730 edit.draw(Editor_state)
1731 local y = Editor_state.top
1732 App.screen.check(y, 'jkl', 'baseline/screen:1')
1733 y = y + Editor_state.line_height
1734 App.screen.check(y, 'mno', 'baseline/screen:2')
1735 -- after hitting home the screen scrolls up to first screen line
1736 edit.run_after_keychord(Editor_state, 'home', 'home')
1737 y = Editor_state.top
1738 App.screen.check(y, 'ghi ', 'screen:1')
1739 y = y + Editor_state.line_height
1740 App.screen.check(y, 'jkl', 'screen:2')
1741 y = y + Editor_state.line_height
1742 App.screen.check(y, 'mno', 'screen:3')
1743 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1744 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1745 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1746 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1749 function test_end_scrolls_down_in_wrapped_line()
1750 -- display the first three lines with the cursor on the bottom line
1751 App.screen.init{width=Editor_state.left+30, height=60}
1752 Editor_state = edit.initialize_test_state()
1753 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1754 Text.redraw_all(Editor_state)
1755 Editor_state.screen_top1 = {line=1, pos=1}
1756 Editor_state.screen_bottom1 = {}
1757 -- cursor is at bottom right of screen
1758 Editor_state.cursor1 = {line=3, pos=5}
1759 edit.draw(Editor_state)
1760 local y = Editor_state.top
1761 App.screen.check(y, 'abc', 'baseline/screen:1')
1762 y = y + Editor_state.line_height
1763 App.screen.check(y, 'def', 'baseline/screen:2')
1764 y = y + Editor_state.line_height
1765 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1766 -- after hitting end the screen scrolls down by one line
1767 edit.run_after_keychord(Editor_state, 'end', 'end')
1768 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1769 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1770 check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
1771 y = Editor_state.top
1772 App.screen.check(y, 'def', 'screen:1')
1773 y = y + Editor_state.line_height
1774 App.screen.check(y, 'ghi ', 'screen:2')
1775 y = y + Editor_state.line_height
1776 App.screen.check(y, 'jkl', 'screen:3')
1779 function test_position_cursor_on_recently_edited_wrapping_line()
1780 -- draw a line wrapping over 2 screen lines
1781 App.screen.init{width=100, height=200}
1782 Editor_state = edit.initialize_test_state()
1783 Editor_state.lines = load_array{'abc def ghi jkl mno pqr ', 'xyz'}
1784 Text.redraw_all(Editor_state)
1785 Editor_state.cursor1 = {line=1, pos=25}
1786 Editor_state.screen_top1 = {line=1, pos=1}
1787 Editor_state.screen_bottom1 = {}
1788 edit.draw(Editor_state)
1789 local y = Editor_state.top
1790 App.screen.check(y, 'abc def ghi ', 'baseline1/screen:1')
1791 y = y + Editor_state.line_height
1792 App.screen.check(y, 'jkl mno pqr ', 'baseline1/screen:2')
1793 y = y + Editor_state.line_height
1794 App.screen.check(y, 'xyz', 'baseline1/screen:3')
1795 -- add to the line until it's wrapping over 3 screen lines
1796 edit.run_after_text_input(Editor_state, 's')
1797 edit.run_after_text_input(Editor_state, 't')
1798 edit.run_after_text_input(Editor_state, 'u')
1799 check_eq(Editor_state.cursor1.pos, 28, 'cursor:pos')
1800 y = Editor_state.top
1801 App.screen.check(y, 'abc def ghi ', 'baseline2/screen:1')
1802 y = y + Editor_state.line_height
1803 App.screen.check(y, 'jkl mno pqr ', 'baseline2/screen:2')
1804 y = y + Editor_state.line_height
1805 App.screen.check(y, 'stu', 'baseline2/screen:3')
1806 -- try to move the cursor earlier in the third screen line by clicking the mouse
1807 edit.run_after_mouse_release(Editor_state, Editor_state.left+2,Editor_state.top+Editor_state.line_height*2+5, 1)
1808 -- cursor should move
1809 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1810 check_eq(Editor_state.cursor1.pos, 25, 'cursor:pos')
1813 function test_backspace_can_scroll_up()
1814 -- display the lines 2/3/4 with the cursor on line 2
1815 App.screen.init{width=120, height=60}
1816 Editor_state = edit.initialize_test_state()
1817 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1818 Text.redraw_all(Editor_state)
1819 Editor_state.cursor1 = {line=2, pos=1}
1820 Editor_state.screen_top1 = {line=2, pos=1}
1821 Editor_state.screen_bottom1 = {}
1822 edit.draw(Editor_state)
1823 local y = Editor_state.top
1824 App.screen.check(y, 'def', 'baseline/screen:1')
1825 y = y + Editor_state.line_height
1826 App.screen.check(y, 'ghi', 'baseline/screen:2')
1827 y = y + Editor_state.line_height
1828 App.screen.check(y, 'jkl', 'baseline/screen:3')
1829 -- after hitting backspace the screen scrolls up by one line
1830 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
1831 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1832 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1833 y = Editor_state.top
1834 App.screen.check(y, 'abcdef', 'screen:1')
1835 y = y + Editor_state.line_height
1836 App.screen.check(y, 'ghi', 'screen:2')
1837 y = y + Editor_state.line_height
1838 App.screen.check(y, 'jkl', 'screen:3')
1841 function test_backspace_can_scroll_up_screen_line()
1842 -- display lines starting from second screen line of a line
1843 App.screen.init{width=Editor_state.left+30, height=60}
1844 Editor_state = edit.initialize_test_state()
1845 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1846 Text.redraw_all(Editor_state)
1847 Editor_state.cursor1 = {line=3, pos=5}
1848 Editor_state.screen_top1 = {line=3, pos=5}
1849 Editor_state.screen_bottom1 = {}
1850 edit.draw(Editor_state)
1851 local y = Editor_state.top
1852 App.screen.check(y, 'jkl', 'baseline/screen:1')
1853 y = y + Editor_state.line_height
1854 App.screen.check(y, 'mno', 'baseline/screen:2')
1855 -- after hitting backspace the screen scrolls up by one screen line
1856 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
1857 y = Editor_state.top
1858 App.screen.check(y, 'ghij', 'screen:1')
1859 y = y + Editor_state.line_height
1860 App.screen.check(y, 'kl', 'screen:2')
1861 y = y + Editor_state.line_height
1862 App.screen.check(y, 'mno', 'screen:3')
1863 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1864 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1865 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1866 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
1869 function test_backspace_past_line_boundary()
1870 -- position cursor at start of a (non-first) line
1871 App.screen.init{width=Editor_state.left+30, height=60}
1872 Editor_state = edit.initialize_test_state()
1873 Editor_state.lines = load_array{'abc', 'def'}
1874 Text.redraw_all(Editor_state)
1875 Editor_state.cursor1 = {line=2, pos=1}
1876 -- backspace joins with previous line
1877 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
1878 check_eq(Editor_state.lines[1].data, 'abcdef', 'check')
1881 -- some tests for operating over selections created using Shift- chords
1882 -- we're just testing delete_selection, and it works the same for all keys
1884 function test_backspace_over_selection()
1885 -- select just one character within a line with cursor before selection
1886 App.screen.init{width=Editor_state.left+30, height=60}
1887 Editor_state = edit.initialize_test_state()
1888 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1889 Text.redraw_all(Editor_state)
1890 Editor_state.cursor1 = {line=1, pos=1}
1891 Editor_state.selection1 = {line=1, pos=2}
1892 -- backspace deletes the selected character, even though it's after the cursor
1893 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
1894 check_eq(Editor_state.lines[1].data, 'bc', 'data')
1895 -- cursor (remains) at start of selection
1896 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1897 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1898 -- selection is cleared
1899 check_nil(Editor_state.selection1.line, 'selection')
1902 function test_backspace_over_selection_reverse()
1903 -- select just one character within a line with cursor after selection
1904 App.screen.init{width=Editor_state.left+30, height=60}
1905 Editor_state = edit.initialize_test_state()
1906 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1907 Text.redraw_all(Editor_state)
1908 Editor_state.cursor1 = {line=1, pos=2}
1909 Editor_state.selection1 = {line=1, pos=1}
1910 -- backspace deletes the selected character
1911 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
1912 check_eq(Editor_state.lines[1].data, 'bc', 'data')
1913 -- cursor moves to start of selection
1914 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1915 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1916 -- selection is cleared
1917 check_nil(Editor_state.selection1.line, 'selection')
1920 function test_backspace_over_multiple_lines()
1921 -- select just one character within a line with cursor after selection
1922 App.screen.init{width=Editor_state.left+30, height=60}
1923 Editor_state = edit.initialize_test_state()
1924 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1925 Text.redraw_all(Editor_state)
1926 Editor_state.cursor1 = {line=1, pos=2}
1927 Editor_state.selection1 = {line=4, pos=2}
1928 -- backspace deletes the region and joins the remaining portions of lines on either side
1929 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
1930 check_eq(Editor_state.lines[1].data, 'akl', 'data:1')
1931 check_eq(Editor_state.lines[2].data, 'mno', 'data:2')
1932 -- cursor remains at start of selection
1933 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1934 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1935 -- selection is cleared
1936 check_nil(Editor_state.selection1.line, 'selection')
1939 function test_backspace_to_end_of_line()
1940 -- select region from cursor to end of line
1941 App.screen.init{width=Editor_state.left+30, height=60}
1942 Editor_state = edit.initialize_test_state()
1943 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1944 Text.redraw_all(Editor_state)
1945 Editor_state.cursor1 = {line=1, pos=2}
1946 Editor_state.selection1 = {line=1, pos=4}
1947 -- backspace deletes rest of line without joining to any other line
1948 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
1949 check_eq(Editor_state.lines[1].data, 'a', 'data:1')
1950 check_eq(Editor_state.lines[2].data, 'def', 'data:2')
1951 -- cursor remains at start of selection
1952 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1953 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1954 -- selection is cleared
1955 check_nil(Editor_state.selection1.line, 'selection')
1958 function test_backspace_to_start_of_line()
1959 -- select region from cursor to start of line
1960 App.screen.init{width=Editor_state.left+30, height=60}
1961 Editor_state = edit.initialize_test_state()
1962 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1963 Text.redraw_all(Editor_state)
1964 Editor_state.cursor1 = {line=2, pos=1}
1965 Editor_state.selection1 = {line=2, pos=3}
1966 -- backspace deletes beginning of line without joining to any other line
1967 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
1968 check_eq(Editor_state.lines[1].data, 'abc', 'data:1')
1969 check_eq(Editor_state.lines[2].data, 'f', 'data:2')
1970 -- cursor remains at start of selection
1971 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1972 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1973 -- selection is cleared
1974 check_nil(Editor_state.selection1.line, 'selection')
1977 function test_undo_insert_text()
1978 App.screen.init{width=120, height=60}
1979 Editor_state = edit.initialize_test_state()
1980 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
1981 Text.redraw_all(Editor_state)
1982 Editor_state.cursor1 = {line=2, pos=4}
1983 Editor_state.screen_top1 = {line=1, pos=1}
1984 Editor_state.screen_bottom1 = {}
1985 -- insert a character
1986 edit.draw(Editor_state)
1987 edit.run_after_text_input(Editor_state, 'g')
1988 check_eq(Editor_state.cursor1.line, 2, 'baseline/cursor:line')
1989 check_eq(Editor_state.cursor1.pos, 5, 'baseline/cursor:pos')
1990 check_nil(Editor_state.selection1.line, 'baseline/selection:line')
1991 check_nil(Editor_state.selection1.pos, 'baseline/selection:pos')
1992 local y = Editor_state.top
1993 App.screen.check(y, 'abc', 'baseline/screen:1')
1994 y = y + Editor_state.line_height
1995 App.screen.check(y, 'defg', 'baseline/screen:2')
1996 y = y + Editor_state.line_height
1997 App.screen.check(y, 'xyz', 'baseline/screen:3')
1998 -- undo
1999 edit.run_after_keychord(Editor_state, 'C-z', 'z')
2000 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
2001 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
2002 check_nil(Editor_state.selection1.line, 'selection:line')
2003 check_nil(Editor_state.selection1.pos, 'selection:pos')
2004 y = Editor_state.top
2005 App.screen.check(y, 'abc', 'screen:1')
2006 y = y + Editor_state.line_height
2007 App.screen.check(y, 'def', 'screen:2')
2008 y = y + Editor_state.line_height
2009 App.screen.check(y, 'xyz', 'screen:3')
2012 function test_undo_delete_text()
2013 App.screen.init{width=120, height=60}
2014 Editor_state = edit.initialize_test_state()
2015 Editor_state.lines = load_array{'abc', 'defg', 'xyz'}
2016 Text.redraw_all(Editor_state)
2017 Editor_state.cursor1 = {line=2, pos=5}
2018 Editor_state.screen_top1 = {line=1, pos=1}
2019 Editor_state.screen_bottom1 = {}
2020 -- delete a character
2021 edit.run_after_keychord(Editor_state, 'backspace', 'backspace')
2022 check_eq(Editor_state.cursor1.line, 2, 'baseline/cursor:line')
2023 check_eq(Editor_state.cursor1.pos, 4, 'baseline/cursor:pos')
2024 check_nil(Editor_state.selection1.line, 'baseline/selection:line')
2025 check_nil(Editor_state.selection1.pos, 'baseline/selection:pos')
2026 local y = Editor_state.top
2027 App.screen.check(y, 'abc', 'baseline/screen:1')
2028 y = y + Editor_state.line_height
2029 App.screen.check(y, 'def', 'baseline/screen:2')
2030 y = y + Editor_state.line_height
2031 App.screen.check(y, 'xyz', 'baseline/screen:3')
2032 -- undo
2033 --? -- after undo, the backspaced key is selected
2034 edit.run_after_keychord(Editor_state, 'C-z', 'z')
2035 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
2036 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
2037 check_nil(Editor_state.selection1.line, 'selection:line')
2038 check_nil(Editor_state.selection1.pos, 'selection:pos')
2039 --? check_eq(Editor_state.selection1.line, 2, 'selection:line')
2040 --? check_eq(Editor_state.selection1.pos, 4, 'selection:pos')
2041 y = Editor_state.top
2042 App.screen.check(y, 'abc', 'screen:1')
2043 y = y + Editor_state.line_height
2044 App.screen.check(y, 'defg', 'screen:2')
2045 y = y + Editor_state.line_height
2046 App.screen.check(y, 'xyz', 'screen:3')
2049 function test_undo_restores_selection()
2050 -- display a line of text with some part selected
2051 App.screen.init{width=75, height=80}
2052 Editor_state = edit.initialize_test_state()
2053 Editor_state.lines = load_array{'abc'}
2054 Text.redraw_all(Editor_state)
2055 Editor_state.cursor1 = {line=1, pos=1}
2056 Editor_state.selection1 = {line=1, pos=2}
2057 Editor_state.screen_top1 = {line=1, pos=1}
2058 Editor_state.screen_bottom1 = {}
2059 edit.draw(Editor_state)
2060 -- delete selected text
2061 edit.run_after_text_input(Editor_state, 'x')
2062 check_eq(Editor_state.lines[1].data, 'xbc', 'baseline')
2063 check_nil(Editor_state.selection1.line, 'baseline:selection')
2064 -- undo
2065 edit.run_after_keychord(Editor_state, 'C-z', 'z')
2066 edit.run_after_keychord(Editor_state, 'C-z', 'z')
2067 -- selection is restored
2068 check_eq(Editor_state.selection1.line, 1, 'line')
2069 check_eq(Editor_state.selection1.pos, 2, 'pos')
2072 function test_search()
2073 App.screen.init{width=120, height=60}
2074 Editor_state = edit.initialize_test_state()
2075 Editor_state.lines = load_array{'```lines', '```', 'def', 'ghi', '’deg'} -- contains unicode quote in final line
2076 Text.redraw_all(Editor_state)
2077 Editor_state.cursor1 = {line=1, pos=1}
2078 Editor_state.screen_top1 = {line=1, pos=1}
2079 Editor_state.screen_bottom1 = {}
2080 edit.draw(Editor_state)
2081 -- search for a string
2082 edit.run_after_keychord(Editor_state, 'C-f', 'f')
2083 edit.run_after_text_input(Editor_state, 'd')
2084 edit.run_after_keychord(Editor_state, 'return', 'return')
2085 check_eq(Editor_state.cursor1.line, 2, '1/cursor:line')
2086 check_eq(Editor_state.cursor1.pos, 1, '1/cursor:pos')
2087 -- reset cursor
2088 Editor_state.cursor1 = {line=1, pos=1}
2089 Editor_state.screen_top1 = {line=1, pos=1}
2090 -- search for second occurrence
2091 edit.run_after_keychord(Editor_state, 'C-f', 'f')
2092 edit.run_after_text_input(Editor_state, 'de')
2093 edit.run_after_keychord(Editor_state, 'down', 'down')
2094 edit.run_after_keychord(Editor_state, 'return', 'return')
2095 check_eq(Editor_state.cursor1.line, 4, '2/cursor:line')
2096 check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
2099 function test_search_upwards()
2100 App.screen.init{width=120, height=60}
2101 Editor_state = edit.initialize_test_state()
2102 Editor_state.lines = load_array{'’abc', 'abd'} -- contains unicode quote
2103 Text.redraw_all(Editor_state)
2104 Editor_state.cursor1 = {line=2, pos=1}
2105 Editor_state.screen_top1 = {line=1, pos=1}
2106 Editor_state.screen_bottom1 = {}
2107 edit.draw(Editor_state)
2108 -- search for a string
2109 edit.run_after_keychord(Editor_state, 'C-f', 'f')
2110 edit.run_after_text_input(Editor_state, 'a')
2111 -- search for previous occurrence
2112 edit.run_after_keychord(Editor_state, 'up', 'up')
2113 check_eq(Editor_state.cursor1.line, 1, '2/cursor:line')
2114 check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
2117 function test_search_wrap()
2118 App.screen.init{width=120, height=60}
2119 Editor_state = edit.initialize_test_state()
2120 Editor_state.lines = load_array{'’abc', 'def'} -- contains unicode quote in first line
2121 Text.redraw_all(Editor_state)
2122 Editor_state.cursor1 = {line=2, pos=1}
2123 Editor_state.screen_top1 = {line=1, pos=1}
2124 Editor_state.screen_bottom1 = {}
2125 edit.draw(Editor_state)
2126 -- search for a string
2127 edit.run_after_keychord(Editor_state, 'C-f', 'f')
2128 edit.run_after_text_input(Editor_state, 'a')
2129 edit.run_after_keychord(Editor_state, 'return', 'return')
2130 -- cursor wraps
2131 check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
2132 check_eq(Editor_state.cursor1.pos, 2, '1/cursor:pos')
2135 function test_search_wrap_upwards()
2136 App.screen.init{width=120, height=60}
2137 Editor_state = edit.initialize_test_state()
2138 Editor_state.lines = load_array{'abc ’abd'} -- contains unicode quote
2139 Text.redraw_all(Editor_state)
2140 Editor_state.cursor1 = {line=1, pos=1}
2141 Editor_state.screen_top1 = {line=1, pos=1}
2142 Editor_state.screen_bottom1 = {}
2143 edit.draw(Editor_state)
2144 -- search upwards for a string
2145 edit.run_after_keychord(Editor_state, 'C-f', 'f')
2146 edit.run_after_text_input(Editor_state, 'a')
2147 edit.run_after_keychord(Editor_state, 'up', 'up')
2148 -- cursor wraps
2149 check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
2150 check_eq(Editor_state.cursor1.pos, 6, '1/cursor:pos')