change section delimiters in log for OpenBSD
[view.love.git] / source_text_tests.lua
blob0b45232d8f890dc8cbc357461aafb536a2199681
1 -- major tests for text editing flows
2 -- Arguably this should be called source_edit_tests.lua,
3 -- but that would mess up the git blame at this point.
5 function test_initial_state()
6 App.screen.init{width=120, height=60}
7 Editor_state = edit.initialize_test_state()
8 Editor_state.lines = load_array{}
9 Text.redraw_all(Editor_state)
10 edit.draw(Editor_state)
11 check_eq(#Editor_state.lines, 1, '#lines')
12 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
13 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
14 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
15 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
16 end
18 function test_click_to_create_drawing()
19 App.screen.init{width=120, height=60}
20 Editor_state = edit.initialize_test_state()
21 Editor_state.lines = load_array{}
22 Text.redraw_all(Editor_state)
23 edit.draw(Editor_state)
24 edit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1)
25 -- cursor skips drawing to always remain on text
26 check_eq(#Editor_state.lines, 2, '#lines')
27 check_eq(Editor_state.cursor1.line, 2, 'cursor')
28 end
30 function test_backspace_to_delete_drawing()
31 -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
32 App.screen.init{width=120, height=60}
33 Editor_state = edit.initialize_test_state()
34 Editor_state.lines = load_array{'```lines', '```', ''}
35 Text.redraw_all(Editor_state)
36 -- cursor is on text as always (outside tests this will get initialized correctly)
37 Editor_state.cursor1.line = 2
38 -- backspacing deletes the drawing
39 edit.run_after_keychord(Editor_state, 'backspace')
40 check_eq(#Editor_state.lines, 1, '#lines')
41 check_eq(Editor_state.cursor1.line, 1, 'cursor')
42 end
44 function test_backspace_from_start_of_final_line()
45 -- display final line of text with cursor at start of it
46 App.screen.init{width=120, height=60}
47 Editor_state = edit.initialize_test_state()
48 Editor_state.lines = load_array{'abc', 'def'}
49 Editor_state.screen_top1 = {line=2, pos=1}
50 Editor_state.cursor1 = {line=2, pos=1}
51 Text.redraw_all(Editor_state)
52 -- backspace scrolls up
53 edit.run_after_keychord(Editor_state, 'backspace')
54 check_eq(#Editor_state.lines, 1, '#lines')
55 check_eq(Editor_state.cursor1.line, 1, 'cursor')
56 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
57 end
59 function test_insert_first_character()
60 App.screen.init{width=120, height=60}
61 Editor_state = edit.initialize_test_state()
62 Editor_state.lines = load_array{}
63 Text.redraw_all(Editor_state)
64 edit.draw(Editor_state)
65 edit.run_after_text_input(Editor_state, 'a')
66 local y = Editor_state.top
67 App.screen.check(y, 'a', 'screen:1')
68 end
70 function test_press_ctrl()
71 -- press ctrl while the cursor is on text
72 App.screen.init{width=50, height=80}
73 Editor_state = edit.initialize_test_state()
74 Editor_state.lines = load_array{''}
75 Text.redraw_all(Editor_state)
76 Editor_state.cursor1 = {line=1, pos=1}
77 Editor_state.screen_top1 = {line=1, pos=1}
78 Editor_state.screen_bottom1 = {}
79 edit.run_after_keychord(Editor_state, 'C-m')
80 end
82 function test_move_left()
83 App.screen.init{width=120, height=60}
84 Editor_state = edit.initialize_test_state()
85 Editor_state.lines = load_array{'a'}
86 Text.redraw_all(Editor_state)
87 Editor_state.cursor1 = {line=1, pos=2}
88 edit.draw(Editor_state)
89 edit.run_after_keychord(Editor_state, 'left')
90 check_eq(Editor_state.cursor1.pos, 1, 'check')
91 end
93 function test_move_right()
94 App.screen.init{width=120, height=60}
95 Editor_state = edit.initialize_test_state()
96 Editor_state.lines = load_array{'a'}
97 Text.redraw_all(Editor_state)
98 Editor_state.cursor1 = {line=1, pos=1}
99 edit.draw(Editor_state)
100 edit.run_after_keychord(Editor_state, 'right')
101 check_eq(Editor_state.cursor1.pos, 2, 'check')
104 function test_move_left_to_previous_line()
105 App.screen.init{width=120, height=60}
106 Editor_state = edit.initialize_test_state()
107 Editor_state.lines = load_array{'abc', 'def'}
108 Text.redraw_all(Editor_state)
109 Editor_state.cursor1 = {line=2, pos=1}
110 edit.draw(Editor_state)
111 edit.run_after_keychord(Editor_state, 'left')
112 check_eq(Editor_state.cursor1.line, 1, 'line')
113 check_eq(Editor_state.cursor1.pos, 4, 'pos') -- past end of line
116 function test_move_right_to_next_line()
117 App.screen.init{width=120, height=60}
118 Editor_state = edit.initialize_test_state()
119 Editor_state.lines = load_array{'abc', 'def'}
120 Text.redraw_all(Editor_state)
121 Editor_state.cursor1 = {line=1, pos=4} -- past end of line
122 edit.draw(Editor_state)
123 edit.run_after_keychord(Editor_state, 'right')
124 check_eq(Editor_state.cursor1.line, 2, 'line')
125 check_eq(Editor_state.cursor1.pos, 1, 'pos')
128 function test_move_to_start_of_word()
129 App.screen.init{width=120, height=60}
130 Editor_state = edit.initialize_test_state()
131 Editor_state.lines = load_array{'abc'}
132 Text.redraw_all(Editor_state)
133 Editor_state.cursor1 = {line=1, pos=3}
134 edit.draw(Editor_state)
135 edit.run_after_keychord(Editor_state, 'M-left')
136 check_eq(Editor_state.cursor1.pos, 1, 'check')
139 function test_move_to_start_of_previous_word()
140 App.screen.init{width=120, height=60}
141 Editor_state = edit.initialize_test_state()
142 Editor_state.lines = load_array{'abc def'}
143 Text.redraw_all(Editor_state)
144 Editor_state.cursor1 = {line=1, pos=4} -- at the space between words
145 edit.draw(Editor_state)
146 edit.run_after_keychord(Editor_state, 'M-left')
147 check_eq(Editor_state.cursor1.pos, 1, 'check')
150 function test_skip_to_previous_word()
151 App.screen.init{width=120, height=60}
152 Editor_state = edit.initialize_test_state()
153 Editor_state.lines = load_array{'abc def'}
154 Text.redraw_all(Editor_state)
155 Editor_state.cursor1 = {line=1, pos=5} -- at the start of second word
156 edit.draw(Editor_state)
157 edit.run_after_keychord(Editor_state, 'M-left')
158 check_eq(Editor_state.cursor1.pos, 1, 'check')
161 function test_skip_past_tab_to_previous_word()
162 App.screen.init{width=120, height=60}
163 Editor_state = edit.initialize_test_state()
164 Editor_state.lines = load_array{'abc def\tghi'}
165 Text.redraw_all(Editor_state)
166 Editor_state.cursor1 = {line=1, pos=10} -- within third word
167 edit.draw(Editor_state)
168 edit.run_after_keychord(Editor_state, 'M-left')
169 check_eq(Editor_state.cursor1.pos, 9, 'check')
172 function test_skip_multiple_spaces_to_previous_word()
173 App.screen.init{width=120, height=60}
174 Editor_state = edit.initialize_test_state()
175 Editor_state.lines = load_array{'abc def'}
176 Text.redraw_all(Editor_state)
177 Editor_state.cursor1 = {line=1, pos=6} -- at the start of second word
178 edit.draw(Editor_state)
179 edit.run_after_keychord(Editor_state, 'M-left')
180 check_eq(Editor_state.cursor1.pos, 1, 'check')
183 function test_move_to_start_of_word_on_previous_line()
184 App.screen.init{width=120, height=60}
185 Editor_state = edit.initialize_test_state()
186 Editor_state.lines = load_array{'abc def', 'ghi'}
187 Text.redraw_all(Editor_state)
188 Editor_state.cursor1 = {line=2, pos=1}
189 edit.draw(Editor_state)
190 edit.run_after_keychord(Editor_state, 'M-left')
191 check_eq(Editor_state.cursor1.line, 1, 'line')
192 check_eq(Editor_state.cursor1.pos, 5, 'pos')
195 function test_move_past_end_of_word()
196 App.screen.init{width=120, height=60}
197 Editor_state = edit.initialize_test_state()
198 Editor_state.lines = load_array{'abc def'}
199 Text.redraw_all(Editor_state)
200 Editor_state.cursor1 = {line=1, pos=1}
201 edit.draw(Editor_state)
202 edit.run_after_keychord(Editor_state, 'M-right')
203 check_eq(Editor_state.cursor1.pos, 4, 'check')
206 function test_skip_to_next_word()
207 App.screen.init{width=120, height=60}
208 Editor_state = edit.initialize_test_state()
209 Editor_state.lines = load_array{'abc def'}
210 Text.redraw_all(Editor_state)
211 Editor_state.cursor1 = {line=1, pos=4} -- at the space between words
212 edit.draw(Editor_state)
213 edit.run_after_keychord(Editor_state, 'M-right')
214 check_eq(Editor_state.cursor1.pos, 8, 'check')
217 function test_skip_past_tab_to_next_word()
218 App.screen.init{width=120, height=60}
219 Editor_state = edit.initialize_test_state()
220 Editor_state.lines = load_array{'abc\tdef'}
221 Text.redraw_all(Editor_state)
222 Editor_state.cursor1 = {line=1, pos=1} -- at the space between words
223 edit.draw(Editor_state)
224 edit.run_after_keychord(Editor_state, 'M-right')
225 check_eq(Editor_state.cursor1.pos, 4, 'check')
228 function test_skip_multiple_spaces_to_next_word()
229 App.screen.init{width=120, height=60}
230 Editor_state = edit.initialize_test_state()
231 Editor_state.lines = load_array{'abc def'}
232 Text.redraw_all(Editor_state)
233 Editor_state.cursor1 = {line=1, pos=4} -- at the start of second word
234 edit.draw(Editor_state)
235 edit.run_after_keychord(Editor_state, 'M-right')
236 check_eq(Editor_state.cursor1.pos, 9, 'check')
239 function test_move_past_end_of_word_on_next_line()
240 App.screen.init{width=120, height=60}
241 Editor_state = edit.initialize_test_state()
242 Editor_state.lines = load_array{'abc def', 'ghi'}
243 Text.redraw_all(Editor_state)
244 Editor_state.cursor1 = {line=1, pos=8}
245 edit.draw(Editor_state)
246 edit.run_after_keychord(Editor_state, 'M-right')
247 check_eq(Editor_state.cursor1.line, 2, 'line')
248 check_eq(Editor_state.cursor1.pos, 4, 'pos')
251 function test_click_moves_cursor()
252 App.screen.init{width=50, height=60}
253 Editor_state = edit.initialize_test_state()
254 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
255 Text.redraw_all(Editor_state)
256 Editor_state.cursor1 = {line=1, pos=1}
257 Editor_state.screen_top1 = {line=1, pos=1}
258 Editor_state.screen_bottom1 = {}
259 Editor_state.selection1 = {}
260 edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
261 edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
262 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
263 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
264 -- selection is empty to avoid perturbing future edits
265 check_nil(Editor_state.selection1.line, 'selection:line')
266 check_nil(Editor_state.selection1.pos, 'selection:pos')
269 function test_click_to_left_of_line()
270 -- display a line with the cursor in the middle
271 App.screen.init{width=50, height=80}
272 Editor_state = edit.initialize_test_state()
273 Editor_state.lines = load_array{'abc'}
274 Text.redraw_all(Editor_state)
275 Editor_state.cursor1 = {line=1, pos=3}
276 Editor_state.screen_top1 = {line=1, pos=1}
277 Editor_state.screen_bottom1 = {}
278 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
489 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
490 check_eq(Editor_state.cursor1.pos, 12, '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
510 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
511 check_eq(Editor_state.cursor1.pos, 12, '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
580 check_eq(Editor_state.cursor1.pos, 20, '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')
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')
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')
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')
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')
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')
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')
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')
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')
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.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
885 -- selection is at screen top
886 check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
887 check_eq(Editor_state.selection1.line, 2, 'selection:line')
888 check_eq(Editor_state.selection1.pos, 3, 'selection:pos')
891 function test_select_text_using_mouse_starting_below_text()
892 -- I'd like to test what happens when a mouse click is below some page of
893 -- text, potentially even in the middle of a line.
894 -- However, it's brittle to set up a text line boundary just right.
895 -- So I'm going to just check things below the bottom of the final line of
896 -- text when it's in the middle of the screen.
897 -- final screen line ends in the middle of screen
898 App.screen.init{width=50, height=60}
899 Editor_state = edit.initialize_test_state()
900 Editor_state.lines = load_array{'abcde'}
901 Text.redraw_all(Editor_state)
902 Editor_state.cursor1 = {line=1, pos=1}
903 Editor_state.screen_top1 = {line=1, pos=1}
904 Editor_state.screen_bottom1 = {}
905 edit.draw(Editor_state)
906 local y = Editor_state.top
907 App.screen.check(y, 'ab', 'baseline:screen:1')
908 y = y + Editor_state.line_height
909 App.screen.check(y, 'cde', 'baseline:screen:2')
910 -- press mouse above first line of text
911 edit.run_after_mouse_press(Editor_state, 5,App.screen.height-5, 1)
912 -- selection is past bottom-most text in screen
913 check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
914 check_eq(Editor_state.selection1.line, 1, 'selection:line')
915 check_eq(Editor_state.selection1.pos, 6, 'selection:pos')
918 function test_select_text_using_mouse_and_shift()
919 App.screen.init{width=50, height=60}
920 Editor_state = edit.initialize_test_state()
921 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
922 Text.redraw_all(Editor_state)
923 Editor_state.cursor1 = {line=1, pos=1}
924 Editor_state.screen_top1 = {line=1, pos=1}
925 Editor_state.screen_bottom1 = {}
926 Editor_state.selection1 = {}
927 edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
928 -- click on first location
929 edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
930 edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
931 -- hold down shift and click somewhere else
932 App.fake_key_press('lshift')
933 edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)
934 edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)
935 App.fake_key_release('lshift')
936 check_eq(Editor_state.selection1.line, 1, 'selection:line')
937 check_eq(Editor_state.selection1.pos, 2, 'selection:pos')
938 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
939 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
942 function test_select_text_repeatedly_using_mouse_and_shift()
943 App.screen.init{width=50, height=60}
944 Editor_state = edit.initialize_test_state()
945 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
946 Text.redraw_all(Editor_state)
947 Text.redraw_all(Editor_state)
948 Editor_state.cursor1 = {line=1, pos=1}
949 Editor_state.screen_top1 = {line=1, pos=1}
950 Editor_state.screen_bottom1 = {}
951 Editor_state.selection1 = {}
952 edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
953 -- click on first location
954 edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
955 edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
956 -- hold down shift and click on a second location
957 App.fake_key_press('lshift')
958 edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)
959 edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)
960 -- hold down shift and click at a third location
961 App.fake_key_press('lshift')
962 edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)
963 edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+Editor_state.line_height+5, 1)
964 App.fake_key_release('lshift')
965 -- selection is between first and third location. forget the second location, not the first.
966 check_eq(Editor_state.selection1.line, 1, 'selection:line')
967 check_eq(Editor_state.selection1.pos, 2, 'selection:pos')
968 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
969 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
972 function test_select_all_text()
973 -- display a single line of text
974 App.screen.init{width=75, height=80}
975 Editor_state = edit.initialize_test_state()
976 Editor_state.lines = load_array{'abc def'}
977 Text.redraw_all(Editor_state)
978 Editor_state.cursor1 = {line=1, pos=1}
979 Editor_state.screen_top1 = {line=1, pos=1}
980 Editor_state.screen_bottom1 = {}
981 edit.draw(Editor_state)
982 -- select all
983 App.fake_key_press('lctrl')
984 edit.run_after_keychord(Editor_state, 'C-a')
985 App.fake_key_release('lctrl')
986 edit.key_release(Editor_state, 'lctrl')
987 -- selection
988 check_eq(Editor_state.selection1.line, 1, 'selection:line')
989 check_eq(Editor_state.selection1.pos, 1, 'selection:pos')
990 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
991 check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
994 function test_cut_without_selection()
995 -- display a few lines
996 App.screen.init{width=Editor_state.left+30, height=60}
997 Editor_state = edit.initialize_test_state()
998 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
999 Text.redraw_all(Editor_state)
1000 Editor_state.cursor1 = {line=1, pos=2}
1001 Editor_state.screen_top1 = {line=1, pos=1}
1002 Editor_state.screen_bottom1 = {}
1003 Editor_state.selection1 = {}
1004 edit.draw(Editor_state)
1005 -- try to cut without selecting text
1006 edit.run_after_keychord(Editor_state, 'C-x')
1007 -- no crash
1008 check_nil(Editor_state.selection1.line, 'check')
1011 function test_pagedown()
1012 App.screen.init{width=120, height=45}
1013 Editor_state = edit.initialize_test_state()
1014 Editor_state.lines = load_array{'abc', 'def', 'ghi'}
1015 Text.redraw_all(Editor_state)
1016 Editor_state.cursor1 = {line=1, pos=1}
1017 Editor_state.screen_top1 = {line=1, pos=1}
1018 Editor_state.screen_bottom1 = {}
1019 -- initially the first two lines are displayed
1020 edit.draw(Editor_state)
1021 local y = Editor_state.top
1022 App.screen.check(y, 'abc', 'baseline/screen:1')
1023 y = y + Editor_state.line_height
1024 App.screen.check(y, 'def', 'baseline/screen:2')
1025 -- after pagedown the bottom line becomes the top
1026 edit.run_after_keychord(Editor_state, 'pagedown')
1027 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1028 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1029 y = Editor_state.top
1030 App.screen.check(y, 'def', 'screen:1')
1031 y = y + Editor_state.line_height
1032 App.screen.check(y, 'ghi', 'screen:2')
1035 function test_pagedown_skips_drawings()
1036 -- some lines of text with a drawing intermixed
1037 local drawing_width = 50
1038 App.screen.init{width=Editor_state.left+drawing_width, height=80}
1039 Editor_state = edit.initialize_test_state()
1040 Editor_state.lines = load_array{'abc', -- height 15
1041 '```lines', '```', -- height 25
1042 'def', -- height 15
1043 'ghi'} -- height 15
1044 Text.redraw_all(Editor_state)
1045 check_eq(Editor_state.lines[2].mode, 'drawing', 'baseline/lines')
1046 Editor_state.cursor1 = {line=1, pos=1}
1047 Editor_state.screen_top1 = {line=1, pos=1}
1048 Editor_state.screen_bottom1 = {}
1049 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1050 -- initially the screen displays the first line and the drawing
1051 -- 15px margin + 15px line1 + 10px margin + 25px drawing + 10px margin = 75px < screen height 80px
1052 edit.draw(Editor_state)
1053 local y = Editor_state.top
1054 App.screen.check(y, 'abc', 'baseline/screen:1')
1055 -- after pagedown the screen draws the drawing up top
1056 -- 15px margin + 10px margin + 25px drawing + 10px margin + 15px line3 = 75px < screen height 80px
1057 edit.run_after_keychord(Editor_state, 'pagedown')
1058 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1059 check_eq(Editor_state.cursor1.line, 3, 'cursor')
1060 y = Editor_state.top + drawing_height
1061 App.screen.check(y, 'def', 'screen:1')
1064 function test_pagedown_can_start_from_middle_of_long_wrapping_line()
1065 -- draw a few lines starting from a very long wrapping line
1066 App.screen.init{width=Editor_state.left+30, height=60}
1067 Editor_state = edit.initialize_test_state()
1068 Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu vwx yza bcd efg hij', 'XYZ'}
1069 Text.redraw_all(Editor_state)
1070 Editor_state.cursor1 = {line=1, pos=2}
1071 Editor_state.screen_top1 = {line=1, pos=1}
1072 Editor_state.screen_bottom1 = {}
1073 edit.draw(Editor_state)
1074 local y = Editor_state.top
1075 App.screen.check(y, 'abc ', 'baseline/screen:1')
1076 y = y + Editor_state.line_height
1077 App.screen.check(y, 'def ', 'baseline/screen:2')
1078 y = y + Editor_state.line_height
1079 App.screen.check(y, 'ghi ', 'baseline/screen:3')
1080 -- after pagedown we scroll down the very long wrapping line
1081 edit.run_after_keychord(Editor_state, 'pagedown')
1082 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1083 check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
1084 y = Editor_state.top
1085 App.screen.check(y, 'ghi ', 'screen:1')
1086 y = y + Editor_state.line_height
1087 App.screen.check(y, 'jkl ', 'screen:2')
1088 y = y + Editor_state.line_height
1089 if Version == '12.0' then
1090 -- HACK: Maybe v12.0 uses a different font? Strange that it only causes
1091 -- issues in a couple of places.
1092 -- We'll need to rethink our tests if issues like this start to multiply.
1093 App.screen.check(y, 'mno ', 'screen:3')
1094 else
1095 App.screen.check(y, 'mn', 'screen:3')
1099 function test_pagedown_never_moves_up()
1100 -- draw the final screen line of a wrapping line
1101 App.screen.init{width=Editor_state.left+30, height=60}
1102 Editor_state = edit.initialize_test_state()
1103 Editor_state.lines = load_array{'abc def ghi'}
1104 Text.redraw_all(Editor_state)
1105 Editor_state.cursor1 = {line=1, pos=9}
1106 Editor_state.screen_top1 = {line=1, pos=9}
1107 Editor_state.screen_bottom1 = {}
1108 edit.draw(Editor_state)
1109 -- pagedown makes no change
1110 edit.run_after_keychord(Editor_state, 'pagedown')
1111 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1112 check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
1115 function test_down_arrow_moves_cursor()
1116 App.screen.init{width=120, height=60}
1117 Editor_state = edit.initialize_test_state()
1118 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1119 Text.redraw_all(Editor_state)
1120 Editor_state.cursor1 = {line=1, pos=1}
1121 Editor_state.screen_top1 = {line=1, pos=1}
1122 Editor_state.screen_bottom1 = {}
1123 -- initially the first three lines are displayed
1124 edit.draw(Editor_state)
1125 local y = Editor_state.top
1126 App.screen.check(y, 'abc', 'baseline/screen:1')
1127 y = y + Editor_state.line_height
1128 App.screen.check(y, 'def', 'baseline/screen:2')
1129 y = y + Editor_state.line_height
1130 App.screen.check(y, 'ghi', 'baseline/screen:3')
1131 -- after hitting the down arrow, the cursor moves down by 1 line
1132 edit.run_after_keychord(Editor_state, 'down')
1133 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1134 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1135 -- the screen is unchanged
1136 y = Editor_state.top
1137 App.screen.check(y, 'abc', 'screen:1')
1138 y = y + Editor_state.line_height
1139 App.screen.check(y, 'def', 'screen:2')
1140 y = y + Editor_state.line_height
1141 App.screen.check(y, 'ghi', 'screen:3')
1144 function test_down_arrow_skips_drawing()
1145 -- some lines of text with a drawing intermixed
1146 local drawing_width = 50
1147 App.screen.init{width=Editor_state.left+drawing_width, height=100}
1148 Editor_state = edit.initialize_test_state()
1149 Editor_state.lines = load_array{'abc', -- height 15
1150 '```lines', '```', -- height 25
1151 'ghi'}
1152 Text.redraw_all(Editor_state)
1153 Editor_state.cursor1 = {line=1, pos=1}
1154 Editor_state.screen_top1 = {line=1, pos=1}
1155 Editor_state.screen_bottom1 = {}
1156 edit.draw(Editor_state)
1157 local y = Editor_state.top
1158 App.screen.check(y, 'abc', 'baseline/screen:1')
1159 y = y + Editor_state.line_height
1160 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1161 y = y + drawing_height
1162 App.screen.check(y, 'ghi', 'baseline/screen:3')
1163 check(Editor_state.cursor_x, 'baseline/cursor_x')
1164 -- after hitting the down arrow the cursor moves down by 2 lines, skipping the drawing
1165 edit.run_after_keychord(Editor_state, 'down')
1166 check_eq(Editor_state.cursor1.line, 3, 'cursor')
1169 function test_down_arrow_scrolls_down_by_one_line()
1170 -- display the first three lines with the cursor on the bottom line
1171 App.screen.init{width=120, height=60}
1172 Editor_state = edit.initialize_test_state()
1173 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1174 Text.redraw_all(Editor_state)
1175 Editor_state.cursor1 = {line=3, pos=1}
1176 Editor_state.screen_top1 = {line=1, pos=1}
1177 Editor_state.screen_bottom1 = {}
1178 edit.draw(Editor_state)
1179 local y = Editor_state.top
1180 App.screen.check(y, 'abc', 'baseline/screen:1')
1181 y = y + Editor_state.line_height
1182 App.screen.check(y, 'def', 'baseline/screen:2')
1183 y = y + Editor_state.line_height
1184 App.screen.check(y, 'ghi', 'baseline/screen:3')
1185 -- after hitting the down arrow the screen scrolls down by one line
1186 edit.run_after_keychord(Editor_state, 'down')
1187 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1188 check_eq(Editor_state.cursor1.line, 4, 'cursor')
1189 y = Editor_state.top
1190 App.screen.check(y, 'def', 'screen:1')
1191 y = y + Editor_state.line_height
1192 App.screen.check(y, 'ghi', 'screen:2')
1193 y = y + Editor_state.line_height
1194 App.screen.check(y, 'jkl', 'screen:3')
1197 function test_down_arrow_scrolls_down_by_one_screen_line()
1198 -- display the first three lines with the cursor on the bottom line
1199 App.screen.init{width=Editor_state.left+30, height=60}
1200 Editor_state = edit.initialize_test_state()
1201 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1202 Text.redraw_all(Editor_state)
1203 Editor_state.cursor1 = {line=3, pos=1}
1204 Editor_state.screen_top1 = {line=1, pos=1}
1205 Editor_state.screen_bottom1 = {}
1206 edit.draw(Editor_state)
1207 local y = Editor_state.top
1208 App.screen.check(y, 'abc', 'baseline/screen:1')
1209 y = y + Editor_state.line_height
1210 App.screen.check(y, 'def', 'baseline/screen:2')
1211 y = y + Editor_state.line_height
1212 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1213 -- after hitting the down arrow the screen scrolls down by one line
1214 edit.run_after_keychord(Editor_state, 'down')
1215 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1216 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1217 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1218 y = Editor_state.top
1219 App.screen.check(y, 'def', 'screen:1')
1220 y = y + Editor_state.line_height
1221 App.screen.check(y, 'ghi ', 'screen:2')
1222 y = y + Editor_state.line_height
1223 App.screen.check(y, 'jkl', 'screen:3')
1226 function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_word()
1227 -- display the first three lines with the cursor on the bottom line
1228 App.screen.init{width=Editor_state.left+30, height=60}
1229 Editor_state = edit.initialize_test_state()
1230 Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
1231 Text.redraw_all(Editor_state)
1232 Editor_state.cursor1 = {line=3, pos=1}
1233 Editor_state.screen_top1 = {line=1, pos=1}
1234 Editor_state.screen_bottom1 = {}
1235 edit.draw(Editor_state)
1236 local y = Editor_state.top
1237 App.screen.check(y, 'abc', 'baseline/screen:1')
1238 y = y + Editor_state.line_height
1239 App.screen.check(y, 'def', 'baseline/screen:2')
1240 y = y + Editor_state.line_height
1241 App.screen.check(y, 'ghij', 'baseline/screen:3')
1242 -- after hitting the down arrow the screen scrolls down by one line
1243 edit.run_after_keychord(Editor_state, 'down')
1244 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1245 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1246 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1247 y = Editor_state.top
1248 App.screen.check(y, 'def', 'screen:1')
1249 y = y + Editor_state.line_height
1250 App.screen.check(y, 'ghij', 'screen:2')
1251 y = y + Editor_state.line_height
1252 App.screen.check(y, 'kl', 'screen:3')
1255 function test_pagedown_followed_by_down_arrow_does_not_scroll_screen_up()
1256 App.screen.init{width=Editor_state.left+30, height=60}
1257 Editor_state = edit.initialize_test_state()
1258 Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
1259 Text.redraw_all(Editor_state)
1260 Editor_state.cursor1 = {line=3, pos=1}
1261 Editor_state.screen_top1 = {line=1, pos=1}
1262 Editor_state.screen_bottom1 = {}
1263 edit.draw(Editor_state)
1264 local y = Editor_state.top
1265 App.screen.check(y, 'abc', 'baseline/screen:1')
1266 y = y + Editor_state.line_height
1267 App.screen.check(y, 'def', 'baseline/screen:2')
1268 y = y + Editor_state.line_height
1269 App.screen.check(y, 'ghij', 'baseline/screen:3')
1270 -- after hitting pagedown the screen scrolls down to start of a long line
1271 edit.run_after_keychord(Editor_state, 'pagedown')
1272 check_eq(Editor_state.screen_top1.line, 3, 'baseline2/screen_top')
1273 check_eq(Editor_state.cursor1.line, 3, 'baseline2/cursor:line')
1274 check_eq(Editor_state.cursor1.pos, 1, 'baseline2/cursor:pos')
1275 -- after hitting down arrow the screen doesn't scroll down further, and certainly doesn't scroll up
1276 edit.run_after_keychord(Editor_state, 'down')
1277 check_eq(Editor_state.screen_top1.line, 3, 'screen_top')
1278 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1279 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1280 y = Editor_state.top
1281 App.screen.check(y, 'ghij', 'screen:1')
1282 y = y + Editor_state.line_height
1283 App.screen.check(y, 'kl', 'screen:2')
1284 y = y + Editor_state.line_height
1285 App.screen.check(y, 'mno', 'screen:3')
1288 function test_up_arrow_moves_cursor()
1289 -- display the first 3 lines with the cursor on the bottom line
1290 App.screen.init{width=120, height=60}
1291 Editor_state = edit.initialize_test_state()
1292 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1293 Text.redraw_all(Editor_state)
1294 Editor_state.cursor1 = {line=3, pos=1}
1295 Editor_state.screen_top1 = {line=1, pos=1}
1296 Editor_state.screen_bottom1 = {}
1297 edit.draw(Editor_state)
1298 local y = Editor_state.top
1299 App.screen.check(y, 'abc', 'baseline/screen:1')
1300 y = y + Editor_state.line_height
1301 App.screen.check(y, 'def', 'baseline/screen:2')
1302 y = y + Editor_state.line_height
1303 App.screen.check(y, 'ghi', 'baseline/screen:3')
1304 -- after hitting the up arrow the cursor moves up by 1 line
1305 edit.run_after_keychord(Editor_state, 'up')
1306 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1307 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1308 -- the screen is unchanged
1309 y = Editor_state.top
1310 App.screen.check(y, 'abc', 'screen:1')
1311 y = y + Editor_state.line_height
1312 App.screen.check(y, 'def', 'screen:2')
1313 y = y + Editor_state.line_height
1314 App.screen.check(y, 'ghi', 'screen:3')
1317 function test_up_arrow_skips_drawing()
1318 -- some lines of text with a drawing intermixed
1319 local drawing_width = 50
1320 App.screen.init{width=Editor_state.left+drawing_width, height=100}
1321 Editor_state = edit.initialize_test_state()
1322 Editor_state.lines = load_array{'abc', -- height 15
1323 '```lines', '```', -- height 25
1324 'ghi'}
1325 Text.redraw_all(Editor_state)
1326 Editor_state.cursor1 = {line=3, pos=1}
1327 Editor_state.screen_top1 = {line=1, pos=1}
1328 Editor_state.screen_bottom1 = {}
1329 edit.draw(Editor_state)
1330 local y = Editor_state.top
1331 App.screen.check(y, 'abc', 'baseline/screen:1')
1332 y = y + Editor_state.line_height
1333 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1334 y = y + drawing_height
1335 App.screen.check(y, 'ghi', 'baseline/screen:3')
1336 check(Editor_state.cursor_x, 'baseline/cursor_x')
1337 -- after hitting the up arrow the cursor moves up by 2 lines, skipping the drawing
1338 edit.run_after_keychord(Editor_state, 'up')
1339 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1342 function test_up_arrow_scrolls_up_by_one_line()
1343 -- display the lines 2/3/4 with the cursor on line 2
1344 App.screen.init{width=120, height=60}
1345 Editor_state = edit.initialize_test_state()
1346 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1347 Text.redraw_all(Editor_state)
1348 Editor_state.cursor1 = {line=2, pos=1}
1349 Editor_state.screen_top1 = {line=2, pos=1}
1350 Editor_state.screen_bottom1 = {}
1351 edit.draw(Editor_state)
1352 local y = Editor_state.top
1353 App.screen.check(y, 'def', 'baseline/screen:1')
1354 y = y + Editor_state.line_height
1355 App.screen.check(y, 'ghi', 'baseline/screen:2')
1356 y = y + Editor_state.line_height
1357 App.screen.check(y, 'jkl', 'baseline/screen:3')
1358 -- after hitting the up arrow the screen scrolls up by one line
1359 edit.run_after_keychord(Editor_state, 'up')
1360 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1361 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1362 y = Editor_state.top
1363 App.screen.check(y, 'abc', 'screen:1')
1364 y = y + Editor_state.line_height
1365 App.screen.check(y, 'def', 'screen:2')
1366 y = y + Editor_state.line_height
1367 App.screen.check(y, 'ghi', 'screen:3')
1370 function test_up_arrow_scrolls_up_by_one_line_skipping_drawing()
1371 -- display lines 3/4/5 with a drawing just off screen at line 2
1372 App.screen.init{width=120, height=60}
1373 Editor_state = edit.initialize_test_state()
1374 Editor_state.lines = load_array{'abc', '```lines', '```', 'def', 'ghi', 'jkl'}
1375 Text.redraw_all(Editor_state)
1376 Editor_state.cursor1 = {line=3, pos=1}
1377 Editor_state.screen_top1 = {line=3, pos=1}
1378 Editor_state.screen_bottom1 = {}
1379 edit.draw(Editor_state)
1380 local y = Editor_state.top
1381 App.screen.check(y, 'def', 'baseline/screen:1')
1382 y = y + Editor_state.line_height
1383 App.screen.check(y, 'ghi', 'baseline/screen:2')
1384 y = y + Editor_state.line_height
1385 App.screen.check(y, 'jkl', 'baseline/screen:3')
1386 -- after hitting the up arrow the screen scrolls up to previous text line
1387 edit.run_after_keychord(Editor_state, 'up')
1388 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1389 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1392 function test_up_arrow_scrolls_up_by_one_screen_line()
1393 -- display lines starting from second screen line of a line
1394 App.screen.init{width=Editor_state.left+30, height=60}
1395 Editor_state = edit.initialize_test_state()
1396 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1397 Text.redraw_all(Editor_state)
1398 Editor_state.cursor1 = {line=3, pos=6}
1399 Editor_state.screen_top1 = {line=3, pos=5}
1400 Editor_state.screen_bottom1 = {}
1401 edit.draw(Editor_state)
1402 local y = Editor_state.top
1403 App.screen.check(y, 'jkl', 'baseline/screen:1')
1404 y = y + Editor_state.line_height
1405 App.screen.check(y, 'mno', 'baseline/screen:2')
1406 -- after hitting the up arrow the screen scrolls up to first screen line
1407 edit.run_after_keychord(Editor_state, 'up')
1408 y = Editor_state.top
1409 App.screen.check(y, 'ghi ', 'screen:1')
1410 y = y + Editor_state.line_height
1411 App.screen.check(y, 'jkl', 'screen:2')
1412 y = y + Editor_state.line_height
1413 App.screen.check(y, 'mno', 'screen:3')
1414 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1415 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1416 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1417 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1420 function test_up_arrow_scrolls_up_to_final_screen_line()
1421 -- display lines starting just after a long line
1422 App.screen.init{width=Editor_state.left+30, height=60}
1423 Editor_state = edit.initialize_test_state()
1424 Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
1425 Text.redraw_all(Editor_state)
1426 Editor_state.cursor1 = {line=2, pos=1}
1427 Editor_state.screen_top1 = {line=2, pos=1}
1428 Editor_state.screen_bottom1 = {}
1429 edit.draw(Editor_state)
1430 local y = Editor_state.top
1431 App.screen.check(y, 'ghi', 'baseline/screen:1')
1432 y = y + Editor_state.line_height
1433 App.screen.check(y, 'jkl', 'baseline/screen:2')
1434 y = y + Editor_state.line_height
1435 App.screen.check(y, 'mno', 'baseline/screen:3')
1436 -- after hitting the up arrow the screen scrolls up to final screen line of previous line
1437 edit.run_after_keychord(Editor_state, 'up')
1438 y = Editor_state.top
1439 App.screen.check(y, 'def', 'screen:1')
1440 y = y + Editor_state.line_height
1441 App.screen.check(y, 'ghi', 'screen:2')
1442 y = y + Editor_state.line_height
1443 App.screen.check(y, 'jkl', 'screen:3')
1444 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1445 check_eq(Editor_state.screen_top1.pos, 5, 'screen_top:pos')
1446 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1447 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1450 function test_up_arrow_scrolls_up_to_empty_line()
1451 -- display a screenful of text with an empty line just above it outside the screen
1452 App.screen.init{width=120, height=60}
1453 Editor_state = edit.initialize_test_state()
1454 Editor_state.lines = load_array{'', 'abc', 'def', 'ghi', 'jkl'}
1455 Text.redraw_all(Editor_state)
1456 Editor_state.cursor1 = {line=2, pos=1}
1457 Editor_state.screen_top1 = {line=2, pos=1}
1458 Editor_state.screen_bottom1 = {}
1459 edit.draw(Editor_state)
1460 local y = Editor_state.top
1461 App.screen.check(y, 'abc', 'baseline/screen:1')
1462 y = y + Editor_state.line_height
1463 App.screen.check(y, 'def', 'baseline/screen:2')
1464 y = y + Editor_state.line_height
1465 App.screen.check(y, 'ghi', 'baseline/screen:3')
1466 -- after hitting the up arrow the screen scrolls up by one line
1467 edit.run_after_keychord(Editor_state, 'up')
1468 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1469 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1470 y = Editor_state.top
1471 -- empty first line
1472 y = y + Editor_state.line_height
1473 App.screen.check(y, 'abc', 'screen:2')
1474 y = y + Editor_state.line_height
1475 App.screen.check(y, 'def', 'screen:3')
1478 function test_pageup()
1479 App.screen.init{width=120, height=45}
1480 Editor_state = edit.initialize_test_state()
1481 Editor_state.lines = load_array{'abc', 'def', 'ghi'}
1482 Text.redraw_all(Editor_state)
1483 Editor_state.cursor1 = {line=2, pos=1}
1484 Editor_state.screen_top1 = {line=2, pos=1}
1485 Editor_state.screen_bottom1 = {}
1486 -- initially the last two lines are displayed
1487 edit.draw(Editor_state)
1488 local y = Editor_state.top
1489 App.screen.check(y, 'def', 'baseline/screen:1')
1490 y = y + Editor_state.line_height
1491 App.screen.check(y, 'ghi', 'baseline/screen:2')
1492 -- after pageup the cursor goes to first line
1493 edit.run_after_keychord(Editor_state, 'pageup')
1494 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1495 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1496 y = Editor_state.top
1497 App.screen.check(y, 'abc', 'screen:1')
1498 y = y + Editor_state.line_height
1499 App.screen.check(y, 'def', 'screen:2')
1502 function test_pageup_scrolls_up_by_screen_line()
1503 -- display the first three lines with the cursor on the bottom line
1504 App.screen.init{width=Editor_state.left+30, height=60}
1505 Editor_state = edit.initialize_test_state()
1506 Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
1507 Text.redraw_all(Editor_state)
1508 Editor_state.cursor1 = {line=2, pos=1}
1509 Editor_state.screen_top1 = {line=2, pos=1}
1510 Editor_state.screen_bottom1 = {}
1511 edit.draw(Editor_state)
1512 local y = Editor_state.top
1513 App.screen.check(y, 'ghi', 'baseline/screen:1')
1514 y = y + Editor_state.line_height
1515 App.screen.check(y, 'jkl', 'baseline/screen:2')
1516 y = y + Editor_state.line_height
1517 App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1518 -- after hitting the page-up key the screen scrolls up to top
1519 edit.run_after_keychord(Editor_state, 'pageup')
1520 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1521 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1522 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1523 y = Editor_state.top
1524 App.screen.check(y, 'abc ', 'screen:1')
1525 y = y + Editor_state.line_height
1526 App.screen.check(y, 'def', 'screen:2')
1527 y = y + Editor_state.line_height
1528 App.screen.check(y, 'ghi', 'screen:3')
1531 function test_pageup_scrolls_up_from_middle_screen_line()
1532 -- display a few lines starting from the middle of a line (Editor_state.cursor1.pos > 1)
1533 App.screen.init{width=Editor_state.left+30, height=60}
1534 Editor_state = edit.initialize_test_state()
1535 Editor_state.lines = load_array{'abc def', 'ghi jkl', 'mno'}
1536 Text.redraw_all(Editor_state)
1537 Editor_state.cursor1 = {line=2, pos=5}
1538 Editor_state.screen_top1 = {line=2, pos=5}
1539 Editor_state.screen_bottom1 = {}
1540 edit.draw(Editor_state)
1541 local y = Editor_state.top
1542 App.screen.check(y, 'jkl', 'baseline/screen:2')
1543 y = y + Editor_state.line_height
1544 App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1545 -- after hitting the page-up key the screen scrolls up to top
1546 edit.run_after_keychord(Editor_state, 'pageup')
1547 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1548 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1549 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1550 y = Editor_state.top
1551 App.screen.check(y, 'abc ', 'screen:1')
1552 y = y + Editor_state.line_height
1553 App.screen.check(y, 'def', 'screen:2')
1554 y = y + Editor_state.line_height
1555 App.screen.check(y, 'ghi ', 'screen:3')
1558 function test_enter_on_bottom_line_scrolls_down()
1559 -- display a few lines with cursor on bottom line
1560 App.screen.init{width=Editor_state.left+30, height=60}
1561 Editor_state = edit.initialize_test_state()
1562 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1563 Text.redraw_all(Editor_state)
1564 Editor_state.cursor1 = {line=3, pos=2}
1565 Editor_state.screen_top1 = {line=1, pos=1}
1566 Editor_state.screen_bottom1 = {}
1567 edit.draw(Editor_state)
1568 local y = Editor_state.top
1569 App.screen.check(y, 'abc', 'baseline/screen:1')
1570 y = y + Editor_state.line_height
1571 App.screen.check(y, 'def', 'baseline/screen:2')
1572 y = y + Editor_state.line_height
1573 App.screen.check(y, 'ghi', 'baseline/screen:3')
1574 -- after hitting the enter key the screen scrolls down
1575 edit.run_after_keychord(Editor_state, 'return')
1576 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1577 check_eq(Editor_state.cursor1.line, 4, 'cursor:line')
1578 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1579 y = Editor_state.top
1580 App.screen.check(y, 'def', 'screen:1')
1581 y = y + Editor_state.line_height
1582 App.screen.check(y, 'g', 'screen:2')
1583 y = y + Editor_state.line_height
1584 App.screen.check(y, 'hi', 'screen:3')
1587 function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1588 -- display just the bottom line on screen
1589 App.screen.init{width=Editor_state.left+30, height=60}
1590 Editor_state = edit.initialize_test_state()
1591 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1592 Text.redraw_all(Editor_state)
1593 Editor_state.cursor1 = {line=4, pos=2}
1594 Editor_state.screen_top1 = {line=4, pos=1}
1595 Editor_state.screen_bottom1 = {}
1596 edit.draw(Editor_state)
1597 local y = Editor_state.top
1598 App.screen.check(y, 'jkl', 'baseline/screen:1')
1599 -- after hitting the enter key the screen does not scroll down
1600 edit.run_after_keychord(Editor_state, 'return')
1601 check_eq(Editor_state.screen_top1.line, 4, 'screen_top')
1602 check_eq(Editor_state.cursor1.line, 5, 'cursor:line')
1603 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1604 y = Editor_state.top
1605 App.screen.check(y, 'j', 'screen:1')
1606 y = y + Editor_state.line_height
1607 App.screen.check(y, 'kl', 'screen:2')
1610 function test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1611 -- display just an empty bottom line on screen
1612 App.screen.init{width=Editor_state.left+30, height=60}
1613 Editor_state = edit.initialize_test_state()
1614 Editor_state.lines = load_array{'abc', ''}
1615 Text.redraw_all(Editor_state)
1616 Editor_state.cursor1 = {line=2, pos=1}
1617 Editor_state.screen_top1 = {line=2, pos=1}
1618 Editor_state.screen_bottom1 = {}
1619 edit.draw(Editor_state)
1620 -- after hitting the inserting_text key the screen does not scroll down
1621 edit.run_after_text_input(Editor_state, 'a')
1622 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1623 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1624 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1625 local y = Editor_state.top
1626 App.screen.check(y, 'a', 'screen:1')
1629 function test_typing_on_bottom_line_scrolls_down()
1630 -- display a few lines with cursor on bottom line
1631 App.screen.init{width=Editor_state.left+30, height=60}
1632 Editor_state = edit.initialize_test_state()
1633 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1634 Text.redraw_all(Editor_state)
1635 Editor_state.cursor1 = {line=3, pos=4}
1636 Editor_state.screen_top1 = {line=1, pos=1}
1637 Editor_state.screen_bottom1 = {}
1638 edit.draw(Editor_state)
1639 local y = Editor_state.top
1640 App.screen.check(y, 'abc', 'baseline/screen:1')
1641 y = y + Editor_state.line_height
1642 App.screen.check(y, 'def', 'baseline/screen:2')
1643 y = y + Editor_state.line_height
1644 App.screen.check(y, 'ghi', 'baseline/screen:3')
1645 -- after typing something the line wraps and the screen scrolls down
1646 edit.run_after_text_input(Editor_state, 'j')
1647 edit.run_after_text_input(Editor_state, 'k')
1648 edit.run_after_text_input(Editor_state, 'l')
1649 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1650 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1651 check_eq(Editor_state.cursor1.pos, 7, 'cursor:pos')
1652 y = Editor_state.top
1653 App.screen.check(y, 'def', 'screen:1')
1654 y = y + Editor_state.line_height
1655 App.screen.check(y, 'ghij', 'screen:2')
1656 y = y + Editor_state.line_height
1657 App.screen.check(y, 'kl', 'screen:3')
1660 function test_left_arrow_scrolls_up_in_wrapped_line()
1661 -- display lines starting from second screen line of a line
1662 App.screen.init{width=Editor_state.left+30, height=60}
1663 Editor_state = edit.initialize_test_state()
1664 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1665 Text.redraw_all(Editor_state)
1666 Editor_state.screen_top1 = {line=3, pos=5}
1667 Editor_state.screen_bottom1 = {}
1668 -- cursor is at top of screen
1669 Editor_state.cursor1 = {line=3, pos=5}
1670 edit.draw(Editor_state)
1671 local y = Editor_state.top
1672 App.screen.check(y, 'jkl', 'baseline/screen:1')
1673 y = y + Editor_state.line_height
1674 App.screen.check(y, 'mno', 'baseline/screen:2')
1675 -- after hitting the left arrow the screen scrolls up to first screen line
1676 edit.run_after_keychord(Editor_state, 'left')
1677 y = Editor_state.top
1678 App.screen.check(y, 'ghi ', 'screen:1')
1679 y = y + Editor_state.line_height
1680 App.screen.check(y, 'jkl', 'screen:2')
1681 y = y + Editor_state.line_height
1682 App.screen.check(y, 'mno', 'screen:3')
1683 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1684 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1685 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1686 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
1689 function test_right_arrow_scrolls_down_in_wrapped_line()
1690 -- display the first three lines with the cursor on the bottom line
1691 App.screen.init{width=Editor_state.left+30, height=60}
1692 Editor_state = edit.initialize_test_state()
1693 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1694 Text.redraw_all(Editor_state)
1695 Editor_state.screen_top1 = {line=1, pos=1}
1696 Editor_state.screen_bottom1 = {}
1697 -- cursor is at bottom right of screen
1698 Editor_state.cursor1 = {line=3, pos=5}
1699 edit.draw(Editor_state)
1700 local y = Editor_state.top
1701 App.screen.check(y, 'abc', 'baseline/screen:1')
1702 y = y + Editor_state.line_height
1703 App.screen.check(y, 'def', 'baseline/screen:2')
1704 y = y + Editor_state.line_height
1705 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1706 -- after hitting the right arrow the screen scrolls down by one line
1707 edit.run_after_keychord(Editor_state, 'right')
1708 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1709 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1710 check_eq(Editor_state.cursor1.pos, 6, 'cursor:pos')
1711 y = Editor_state.top
1712 App.screen.check(y, 'def', 'screen:1')
1713 y = y + Editor_state.line_height
1714 App.screen.check(y, 'ghi ', 'screen:2')
1715 y = y + Editor_state.line_height
1716 App.screen.check(y, 'jkl', 'screen:3')
1719 function test_home_scrolls_up_in_wrapped_line()
1720 -- display lines starting from second screen line of a line
1721 App.screen.init{width=Editor_state.left+30, height=60}
1722 Editor_state = edit.initialize_test_state()
1723 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1724 Text.redraw_all(Editor_state)
1725 Editor_state.screen_top1 = {line=3, pos=5}
1726 Editor_state.screen_bottom1 = {}
1727 -- cursor is at top of screen
1728 Editor_state.cursor1 = {line=3, pos=5}
1729 edit.draw(Editor_state)
1730 local y = Editor_state.top
1731 App.screen.check(y, 'jkl', 'baseline/screen:1')
1732 y = y + Editor_state.line_height
1733 App.screen.check(y, 'mno', 'baseline/screen:2')
1734 -- after hitting home the screen scrolls up to first screen line
1735 edit.run_after_keychord(Editor_state, 'home')
1736 y = Editor_state.top
1737 App.screen.check(y, 'ghi ', 'screen:1')
1738 y = y + Editor_state.line_height
1739 App.screen.check(y, 'jkl', 'screen:2')
1740 y = y + Editor_state.line_height
1741 App.screen.check(y, 'mno', 'screen:3')
1742 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1743 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1744 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1745 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1748 function test_end_scrolls_down_in_wrapped_line()
1749 -- display the first three lines with the cursor on the bottom line
1750 App.screen.init{width=Editor_state.left+30, height=60}
1751 Editor_state = edit.initialize_test_state()
1752 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1753 Text.redraw_all(Editor_state)
1754 Editor_state.screen_top1 = {line=1, pos=1}
1755 Editor_state.screen_bottom1 = {}
1756 -- cursor is at bottom right of screen
1757 Editor_state.cursor1 = {line=3, pos=5}
1758 edit.draw(Editor_state)
1759 local y = Editor_state.top
1760 App.screen.check(y, 'abc', 'baseline/screen:1')
1761 y = y + Editor_state.line_height
1762 App.screen.check(y, 'def', 'baseline/screen:2')
1763 y = y + Editor_state.line_height
1764 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1765 -- after hitting end the screen scrolls down by one line
1766 edit.run_after_keychord(Editor_state, 'end')
1767 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1768 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1769 check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
1770 y = Editor_state.top
1771 App.screen.check(y, 'def', 'screen:1')
1772 y = y + Editor_state.line_height
1773 App.screen.check(y, 'ghi ', 'screen:2')
1774 y = y + Editor_state.line_height
1775 App.screen.check(y, 'jkl', 'screen:3')
1778 function test_position_cursor_on_recently_edited_wrapping_line()
1779 -- draw a line wrapping over 2 screen lines
1780 App.screen.init{width=100, height=200}
1781 Editor_state = edit.initialize_test_state()
1782 Editor_state.lines = load_array{'abc def ghi jkl mno pqr ', 'xyz'}
1783 Text.redraw_all(Editor_state)
1784 Editor_state.cursor1 = {line=1, pos=25}
1785 Editor_state.screen_top1 = {line=1, pos=1}
1786 Editor_state.screen_bottom1 = {}
1787 edit.draw(Editor_state)
1788 local y = Editor_state.top
1789 App.screen.check(y, 'abc def ghi ', 'baseline1/screen:1')
1790 y = y + Editor_state.line_height
1791 App.screen.check(y, 'jkl mno pqr ', 'baseline1/screen:2')
1792 y = y + Editor_state.line_height
1793 App.screen.check(y, 'xyz', 'baseline1/screen:3')
1794 -- add to the line until it's wrapping over 3 screen lines
1795 edit.run_after_text_input(Editor_state, 's')
1796 edit.run_after_text_input(Editor_state, 't')
1797 edit.run_after_text_input(Editor_state, 'u')
1798 check_eq(Editor_state.cursor1.pos, 28, 'cursor:pos')
1799 y = Editor_state.top
1800 App.screen.check(y, 'abc def ghi ', 'baseline2/screen:1')
1801 y = y + Editor_state.line_height
1802 App.screen.check(y, 'jkl mno pqr ', 'baseline2/screen:2')
1803 y = y + Editor_state.line_height
1804 App.screen.check(y, 'stu', 'baseline2/screen:3')
1805 -- try to move the cursor earlier in the third screen line by clicking the mouse
1806 edit.run_after_mouse_release(Editor_state, Editor_state.left+2,Editor_state.top+Editor_state.line_height*2+5, 1)
1807 -- cursor should move
1808 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1809 check_eq(Editor_state.cursor1.pos, 25, 'cursor:pos')
1812 function test_backspace_can_scroll_up()
1813 -- display the lines 2/3/4 with the cursor on line 2
1814 App.screen.init{width=120, height=60}
1815 Editor_state = edit.initialize_test_state()
1816 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1817 Text.redraw_all(Editor_state)
1818 Editor_state.cursor1 = {line=2, pos=1}
1819 Editor_state.screen_top1 = {line=2, pos=1}
1820 Editor_state.screen_bottom1 = {}
1821 edit.draw(Editor_state)
1822 local y = Editor_state.top
1823 App.screen.check(y, 'def', 'baseline/screen:1')
1824 y = y + Editor_state.line_height
1825 App.screen.check(y, 'ghi', 'baseline/screen:2')
1826 y = y + Editor_state.line_height
1827 App.screen.check(y, 'jkl', 'baseline/screen:3')
1828 -- after hitting backspace the screen scrolls up by one line
1829 edit.run_after_keychord(Editor_state, 'backspace')
1830 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1831 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1832 y = Editor_state.top
1833 App.screen.check(y, 'abcdef', 'screen:1')
1834 y = y + Editor_state.line_height
1835 App.screen.check(y, 'ghi', 'screen:2')
1836 y = y + Editor_state.line_height
1837 App.screen.check(y, 'jkl', 'screen:3')
1840 function test_backspace_can_scroll_up_screen_line()
1841 -- display lines starting from second screen line of a line
1842 App.screen.init{width=Editor_state.left+30, height=60}
1843 Editor_state = edit.initialize_test_state()
1844 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1845 Text.redraw_all(Editor_state)
1846 Editor_state.cursor1 = {line=3, pos=5}
1847 Editor_state.screen_top1 = {line=3, pos=5}
1848 Editor_state.screen_bottom1 = {}
1849 edit.draw(Editor_state)
1850 local y = Editor_state.top
1851 App.screen.check(y, 'jkl', 'baseline/screen:1')
1852 y = y + Editor_state.line_height
1853 App.screen.check(y, 'mno', 'baseline/screen:2')
1854 -- after hitting backspace the screen scrolls up by one screen line
1855 edit.run_after_keychord(Editor_state, 'backspace')
1856 y = Editor_state.top
1857 App.screen.check(y, 'ghij', 'screen:1')
1858 y = y + Editor_state.line_height
1859 App.screen.check(y, 'kl', 'screen:2')
1860 y = y + Editor_state.line_height
1861 App.screen.check(y, 'mno', 'screen:3')
1862 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1863 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1864 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1865 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
1868 function test_backspace_past_line_boundary()
1869 -- position cursor at start of a (non-first) line
1870 App.screen.init{width=Editor_state.left+30, height=60}
1871 Editor_state = edit.initialize_test_state()
1872 Editor_state.lines = load_array{'abc', 'def'}
1873 Text.redraw_all(Editor_state)
1874 Editor_state.cursor1 = {line=2, pos=1}
1875 -- backspace joins with previous line
1876 edit.run_after_keychord(Editor_state, 'backspace')
1877 check_eq(Editor_state.lines[1].data, 'abcdef', 'check')
1880 -- some tests for operating over selections created using Shift- chords
1881 -- we're just testing delete_selection, and it works the same for all keys
1883 function test_backspace_over_selection()
1884 -- select just one character within a line with cursor before selection
1885 App.screen.init{width=Editor_state.left+30, height=60}
1886 Editor_state = edit.initialize_test_state()
1887 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1888 Text.redraw_all(Editor_state)
1889 Editor_state.cursor1 = {line=1, pos=1}
1890 Editor_state.selection1 = {line=1, pos=2}
1891 -- backspace deletes the selected character, even though it's after the cursor
1892 edit.run_after_keychord(Editor_state, 'backspace')
1893 check_eq(Editor_state.lines[1].data, 'bc', 'data')
1894 -- cursor (remains) at start of selection
1895 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1896 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1897 -- selection is cleared
1898 check_nil(Editor_state.selection1.line, 'selection')
1901 function test_backspace_over_selection_reverse()
1902 -- select just one character within a line with cursor after selection
1903 App.screen.init{width=Editor_state.left+30, height=60}
1904 Editor_state = edit.initialize_test_state()
1905 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1906 Text.redraw_all(Editor_state)
1907 Editor_state.cursor1 = {line=1, pos=2}
1908 Editor_state.selection1 = {line=1, pos=1}
1909 -- backspace deletes the selected character
1910 edit.run_after_keychord(Editor_state, 'backspace')
1911 check_eq(Editor_state.lines[1].data, 'bc', 'data')
1912 -- cursor moves to start of selection
1913 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1914 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1915 -- selection is cleared
1916 check_nil(Editor_state.selection1.line, 'selection')
1919 function test_backspace_over_multiple_lines()
1920 -- select just one character within a line with cursor after selection
1921 App.screen.init{width=Editor_state.left+30, height=60}
1922 Editor_state = edit.initialize_test_state()
1923 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1924 Text.redraw_all(Editor_state)
1925 Editor_state.cursor1 = {line=1, pos=2}
1926 Editor_state.selection1 = {line=4, pos=2}
1927 -- backspace deletes the region and joins the remaining portions of lines on either side
1928 edit.run_after_keychord(Editor_state, 'backspace')
1929 check_eq(Editor_state.lines[1].data, 'akl', 'data:1')
1930 check_eq(Editor_state.lines[2].data, 'mno', 'data:2')
1931 -- cursor remains at start of selection
1932 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1933 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1934 -- selection is cleared
1935 check_nil(Editor_state.selection1.line, 'selection')
1938 function test_backspace_to_end_of_line()
1939 -- select region from cursor to end of line
1940 App.screen.init{width=Editor_state.left+30, height=60}
1941 Editor_state = edit.initialize_test_state()
1942 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1943 Text.redraw_all(Editor_state)
1944 Editor_state.cursor1 = {line=1, pos=2}
1945 Editor_state.selection1 = {line=1, pos=4}
1946 -- backspace deletes rest of line without joining to any other line
1947 edit.run_after_keychord(Editor_state, 'backspace')
1948 check_eq(Editor_state.lines[1].data, 'a', 'data:1')
1949 check_eq(Editor_state.lines[2].data, 'def', 'data:2')
1950 -- cursor remains at start of selection
1951 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1952 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1953 -- selection is cleared
1954 check_nil(Editor_state.selection1.line, 'selection')
1957 function test_backspace_to_start_of_line()
1958 -- select region from cursor to start of line
1959 App.screen.init{width=Editor_state.left+30, height=60}
1960 Editor_state = edit.initialize_test_state()
1961 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1962 Text.redraw_all(Editor_state)
1963 Editor_state.cursor1 = {line=2, pos=1}
1964 Editor_state.selection1 = {line=2, pos=3}
1965 -- backspace deletes beginning of line without joining to any other line
1966 edit.run_after_keychord(Editor_state, 'backspace')
1967 check_eq(Editor_state.lines[1].data, 'abc', 'data:1')
1968 check_eq(Editor_state.lines[2].data, 'f', 'data:2')
1969 -- cursor remains at start of selection
1970 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1971 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1972 -- selection is cleared
1973 check_nil(Editor_state.selection1.line, 'selection')
1976 function test_undo_insert_text()
1977 App.screen.init{width=120, height=60}
1978 Editor_state = edit.initialize_test_state()
1979 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
1980 Text.redraw_all(Editor_state)
1981 Editor_state.cursor1 = {line=2, pos=4}
1982 Editor_state.screen_top1 = {line=1, pos=1}
1983 Editor_state.screen_bottom1 = {}
1984 -- insert a character
1985 edit.draw(Editor_state)
1986 edit.run_after_text_input(Editor_state, 'g')
1987 check_eq(Editor_state.cursor1.line, 2, 'baseline/cursor:line')
1988 check_eq(Editor_state.cursor1.pos, 5, 'baseline/cursor:pos')
1989 check_nil(Editor_state.selection1.line, 'baseline/selection:line')
1990 check_nil(Editor_state.selection1.pos, 'baseline/selection:pos')
1991 local y = Editor_state.top
1992 App.screen.check(y, 'abc', 'baseline/screen:1')
1993 y = y + Editor_state.line_height
1994 App.screen.check(y, 'defg', 'baseline/screen:2')
1995 y = y + Editor_state.line_height
1996 App.screen.check(y, 'xyz', 'baseline/screen:3')
1997 -- undo
1998 edit.run_after_keychord(Editor_state, 'C-z')
1999 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
2000 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
2001 check_nil(Editor_state.selection1.line, 'selection:line')
2002 check_nil(Editor_state.selection1.pos, 'selection:pos')
2003 y = Editor_state.top
2004 App.screen.check(y, 'abc', 'screen:1')
2005 y = y + Editor_state.line_height
2006 App.screen.check(y, 'def', 'screen:2')
2007 y = y + Editor_state.line_height
2008 App.screen.check(y, 'xyz', 'screen:3')
2011 function test_undo_delete_text()
2012 App.screen.init{width=120, height=60}
2013 Editor_state = edit.initialize_test_state()
2014 Editor_state.lines = load_array{'abc', 'defg', 'xyz'}
2015 Text.redraw_all(Editor_state)
2016 Editor_state.cursor1 = {line=2, pos=5}
2017 Editor_state.screen_top1 = {line=1, pos=1}
2018 Editor_state.screen_bottom1 = {}
2019 -- delete a character
2020 edit.run_after_keychord(Editor_state, 'backspace')
2021 check_eq(Editor_state.cursor1.line, 2, 'baseline/cursor:line')
2022 check_eq(Editor_state.cursor1.pos, 4, 'baseline/cursor:pos')
2023 check_nil(Editor_state.selection1.line, 'baseline/selection:line')
2024 check_nil(Editor_state.selection1.pos, 'baseline/selection:pos')
2025 local y = Editor_state.top
2026 App.screen.check(y, 'abc', 'baseline/screen:1')
2027 y = y + Editor_state.line_height
2028 App.screen.check(y, 'def', 'baseline/screen:2')
2029 y = y + Editor_state.line_height
2030 App.screen.check(y, 'xyz', 'baseline/screen:3')
2031 -- undo
2032 --? -- after undo, the backspaced key is selected
2033 edit.run_after_keychord(Editor_state, 'C-z')
2034 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
2035 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
2036 check_nil(Editor_state.selection1.line, 'selection:line')
2037 check_nil(Editor_state.selection1.pos, 'selection:pos')
2038 --? check_eq(Editor_state.selection1.line, 2, 'selection:line')
2039 --? check_eq(Editor_state.selection1.pos, 4, 'selection:pos')
2040 y = Editor_state.top
2041 App.screen.check(y, 'abc', 'screen:1')
2042 y = y + Editor_state.line_height
2043 App.screen.check(y, 'defg', 'screen:2')
2044 y = y + Editor_state.line_height
2045 App.screen.check(y, 'xyz', 'screen:3')
2048 function test_undo_restores_selection()
2049 -- display a line of text with some part selected
2050 App.screen.init{width=75, height=80}
2051 Editor_state = edit.initialize_test_state()
2052 Editor_state.lines = load_array{'abc'}
2053 Text.redraw_all(Editor_state)
2054 Editor_state.cursor1 = {line=1, pos=1}
2055 Editor_state.selection1 = {line=1, pos=2}
2056 Editor_state.screen_top1 = {line=1, pos=1}
2057 Editor_state.screen_bottom1 = {}
2058 edit.draw(Editor_state)
2059 -- delete selected text
2060 edit.run_after_text_input(Editor_state, 'x')
2061 check_eq(Editor_state.lines[1].data, 'xbc', 'baseline')
2062 check_nil(Editor_state.selection1.line, 'baseline:selection')
2063 -- undo
2064 edit.run_after_keychord(Editor_state, 'C-z')
2065 edit.run_after_keychord(Editor_state, 'C-z')
2066 -- selection is restored
2067 check_eq(Editor_state.selection1.line, 1, 'line')
2068 check_eq(Editor_state.selection1.pos, 2, 'pos')
2071 function test_search()
2072 App.screen.init{width=120, height=60}
2073 Editor_state = edit.initialize_test_state()
2074 Editor_state.lines = load_array{'```lines', '```', 'def', 'ghi', '’deg'} -- contains unicode quote in final line
2075 Text.redraw_all(Editor_state)
2076 Editor_state.cursor1 = {line=1, pos=1}
2077 Editor_state.screen_top1 = {line=1, pos=1}
2078 Editor_state.screen_bottom1 = {}
2079 edit.draw(Editor_state)
2080 -- search for a string
2081 edit.run_after_keychord(Editor_state, 'C-f')
2082 edit.run_after_text_input(Editor_state, 'd')
2083 edit.run_after_keychord(Editor_state, 'return')
2084 check_eq(Editor_state.cursor1.line, 2, '1/cursor:line')
2085 check_eq(Editor_state.cursor1.pos, 1, '1/cursor:pos')
2086 -- reset cursor
2087 Editor_state.cursor1 = {line=1, pos=1}
2088 Editor_state.screen_top1 = {line=1, pos=1}
2089 -- search for second occurrence
2090 edit.run_after_keychord(Editor_state, 'C-f')
2091 edit.run_after_text_input(Editor_state, 'de')
2092 edit.run_after_keychord(Editor_state, 'down')
2093 edit.run_after_keychord(Editor_state, 'return')
2094 check_eq(Editor_state.cursor1.line, 4, '2/cursor:line')
2095 check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
2098 function test_search_upwards()
2099 App.screen.init{width=120, height=60}
2100 Editor_state = edit.initialize_test_state()
2101 Editor_state.lines = load_array{'’abc', 'abd'} -- contains unicode quote
2102 Text.redraw_all(Editor_state)
2103 Editor_state.cursor1 = {line=2, pos=1}
2104 Editor_state.screen_top1 = {line=1, pos=1}
2105 Editor_state.screen_bottom1 = {}
2106 edit.draw(Editor_state)
2107 -- search for a string
2108 edit.run_after_keychord(Editor_state, 'C-f')
2109 edit.run_after_text_input(Editor_state, 'a')
2110 -- search for previous occurrence
2111 edit.run_after_keychord(Editor_state, 'up')
2112 check_eq(Editor_state.cursor1.line, 1, '2/cursor:line')
2113 check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
2116 function test_search_wrap()
2117 App.screen.init{width=120, height=60}
2118 Editor_state = edit.initialize_test_state()
2119 Editor_state.lines = load_array{'’abc', 'def'} -- contains unicode quote in first line
2120 Text.redraw_all(Editor_state)
2121 Editor_state.cursor1 = {line=2, pos=1}
2122 Editor_state.screen_top1 = {line=1, pos=1}
2123 Editor_state.screen_bottom1 = {}
2124 edit.draw(Editor_state)
2125 -- search for a string
2126 edit.run_after_keychord(Editor_state, 'C-f')
2127 edit.run_after_text_input(Editor_state, 'a')
2128 edit.run_after_keychord(Editor_state, 'return')
2129 -- cursor wraps
2130 check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
2131 check_eq(Editor_state.cursor1.pos, 2, '1/cursor:pos')
2134 function test_search_wrap_upwards()
2135 App.screen.init{width=120, height=60}
2136 Editor_state = edit.initialize_test_state()
2137 Editor_state.lines = load_array{'abc ’abd'} -- contains unicode quote
2138 Text.redraw_all(Editor_state)
2139 Editor_state.cursor1 = {line=1, pos=1}
2140 Editor_state.screen_top1 = {line=1, pos=1}
2141 Editor_state.screen_bottom1 = {}
2142 edit.draw(Editor_state)
2143 -- search upwards for a string
2144 edit.run_after_keychord(Editor_state, 'C-f')
2145 edit.run_after_text_input(Editor_state, 'a')
2146 edit.run_after_keychord(Editor_state, 'up')
2147 -- cursor wraps
2148 check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
2149 check_eq(Editor_state.cursor1.pos, 6, '1/cursor:pos')