change section delimiters in log for OpenBSD
[view.love.git] / text_tests.lua
blob41fe2df39b5168df1665b998d5fb638d2aeaace0
1 -- major tests for text editing flows
2 -- Arguably this should be called edit_tests.lua,
3 -- but that would mess up the git blame at this point.
5 function test_initial_state()
6 App.screen.init{width=120, height=60}
7 Editor_state = edit.initialize_test_state()
8 Editor_state.lines = load_array{}
9 Text.redraw_all(Editor_state)
10 edit.draw(Editor_state)
11 check_eq(#Editor_state.lines, 1, '#lines')
12 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
13 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
14 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
15 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
16 end
18 function test_click_to_create_drawing()
19 App.screen.init{width=120, height=60}
20 Editor_state = edit.initialize_test_state()
21 Editor_state.lines = load_array{}
22 Text.redraw_all(Editor_state)
23 edit.draw(Editor_state)
24 edit.run_after_mouse_click(Editor_state, 8,Editor_state.top+8, 1)
25 -- cursor skips drawing to always remain on text
26 check_eq(#Editor_state.lines, 2, '#lines')
27 check_eq(Editor_state.cursor1.line, 2, 'cursor')
28 end
30 function test_backspace_to_delete_drawing()
31 -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end)
32 App.screen.init{width=120, height=60}
33 Editor_state = edit.initialize_test_state()
34 Editor_state.lines = load_array{'```lines', '```', ''}
35 Text.redraw_all(Editor_state)
36 -- cursor is on text as always (outside tests this will get initialized correctly)
37 Editor_state.cursor1.line = 2
38 -- backspacing deletes the drawing
39 edit.run_after_keychord(Editor_state, 'backspace')
40 check_eq(#Editor_state.lines, 1, '#lines')
41 check_eq(Editor_state.cursor1.line, 1, 'cursor')
42 end
44 function test_backspace_from_start_of_final_line()
45 -- display final line of text with cursor at start of it
46 App.screen.init{width=120, height=60}
47 Editor_state = edit.initialize_test_state()
48 Editor_state.lines = load_array{'abc', 'def'}
49 Editor_state.screen_top1 = {line=2, pos=1}
50 Editor_state.cursor1 = {line=2, pos=1}
51 Text.redraw_all(Editor_state)
52 -- backspace scrolls up
53 edit.run_after_keychord(Editor_state, 'backspace')
54 check_eq(#Editor_state.lines, 1, '#lines')
55 check_eq(Editor_state.cursor1.line, 1, 'cursor')
56 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
57 end
59 function test_insert_first_character()
60 App.screen.init{width=120, height=60}
61 Editor_state = edit.initialize_test_state()
62 Editor_state.lines = load_array{}
63 Text.redraw_all(Editor_state)
64 edit.draw(Editor_state)
65 edit.run_after_text_input(Editor_state, 'a')
66 local y = Editor_state.top
67 App.screen.check(y, 'a', 'screen:1')
68 end
70 function test_press_ctrl()
71 -- press ctrl while the cursor is on text
72 App.screen.init{width=50, height=80}
73 Editor_state = edit.initialize_test_state()
74 Editor_state.lines = load_array{''}
75 Text.redraw_all(Editor_state)
76 Editor_state.cursor1 = {line=1, pos=1}
77 Editor_state.screen_top1 = {line=1, pos=1}
78 Editor_state.screen_bottom1 = {}
79 edit.run_after_keychord(Editor_state, 'C-m')
80 end
82 function test_move_left()
83 App.screen.init{width=120, height=60}
84 Editor_state = edit.initialize_test_state()
85 Editor_state.lines = load_array{'a'}
86 Text.redraw_all(Editor_state)
87 Editor_state.cursor1 = {line=1, pos=2}
88 edit.draw(Editor_state)
89 edit.run_after_keychord(Editor_state, 'left')
90 check_eq(Editor_state.cursor1.pos, 1, 'check')
91 end
93 function test_move_right()
94 App.screen.init{width=120, height=60}
95 Editor_state = edit.initialize_test_state()
96 Editor_state.lines = load_array{'a'}
97 Text.redraw_all(Editor_state)
98 Editor_state.cursor1 = {line=1, pos=1}
99 edit.draw(Editor_state)
100 edit.run_after_keychord(Editor_state, 'right')
101 check_eq(Editor_state.cursor1.pos, 2, 'check')
104 function test_move_left_to_previous_line()
105 App.screen.init{width=120, height=60}
106 Editor_state = edit.initialize_test_state()
107 Editor_state.lines = load_array{'abc', 'def'}
108 Text.redraw_all(Editor_state)
109 Editor_state.cursor1 = {line=2, pos=1}
110 edit.draw(Editor_state)
111 edit.run_after_keychord(Editor_state, 'left')
112 check_eq(Editor_state.cursor1.line, 1, 'line')
113 check_eq(Editor_state.cursor1.pos, 4, 'pos') -- past end of line
116 function test_move_right_to_next_line()
117 App.screen.init{width=120, height=60}
118 Editor_state = edit.initialize_test_state()
119 Editor_state.lines = load_array{'abc', 'def'}
120 Text.redraw_all(Editor_state)
121 Editor_state.cursor1 = {line=1, pos=4} -- past end of line
122 edit.draw(Editor_state)
123 edit.run_after_keychord(Editor_state, 'right')
124 check_eq(Editor_state.cursor1.line, 2, 'line')
125 check_eq(Editor_state.cursor1.pos, 1, 'pos')
128 function test_move_to_start_of_word()
129 App.screen.init{width=120, height=60}
130 Editor_state = edit.initialize_test_state()
131 Editor_state.lines = load_array{'abc'}
132 Text.redraw_all(Editor_state)
133 Editor_state.cursor1 = {line=1, pos=3}
134 edit.draw(Editor_state)
135 edit.run_after_keychord(Editor_state, 'M-left')
136 check_eq(Editor_state.cursor1.pos, 1, 'check')
139 function test_move_to_start_of_previous_word()
140 App.screen.init{width=120, height=60}
141 Editor_state = edit.initialize_test_state()
142 Editor_state.lines = load_array{'abc def'}
143 Text.redraw_all(Editor_state)
144 Editor_state.cursor1 = {line=1, pos=4} -- at the space between words
145 edit.draw(Editor_state)
146 edit.run_after_keychord(Editor_state, 'M-left')
147 check_eq(Editor_state.cursor1.pos, 1, 'check')
150 function test_skip_to_previous_word()
151 App.screen.init{width=120, height=60}
152 Editor_state = edit.initialize_test_state()
153 Editor_state.lines = load_array{'abc def'}
154 Text.redraw_all(Editor_state)
155 Editor_state.cursor1 = {line=1, pos=5} -- at the start of second word
156 edit.draw(Editor_state)
157 edit.run_after_keychord(Editor_state, 'M-left')
158 check_eq(Editor_state.cursor1.pos, 1, 'check')
161 function test_skip_past_tab_to_previous_word()
162 App.screen.init{width=120, height=60}
163 Editor_state = edit.initialize_test_state()
164 Editor_state.lines = load_array{'abc def\tghi'}
165 Text.redraw_all(Editor_state)
166 Editor_state.cursor1 = {line=1, pos=10} -- within third word
167 edit.draw(Editor_state)
168 edit.run_after_keychord(Editor_state, 'M-left')
169 check_eq(Editor_state.cursor1.pos, 9, 'check')
172 function test_skip_multiple_spaces_to_previous_word()
173 App.screen.init{width=120, height=60}
174 Editor_state = edit.initialize_test_state()
175 Editor_state.lines = load_array{'abc def'}
176 Text.redraw_all(Editor_state)
177 Editor_state.cursor1 = {line=1, pos=6} -- at the start of second word
178 edit.draw(Editor_state)
179 edit.run_after_keychord(Editor_state, 'M-left')
180 check_eq(Editor_state.cursor1.pos, 1, 'check')
183 function test_move_to_start_of_word_on_previous_line()
184 App.screen.init{width=120, height=60}
185 Editor_state = edit.initialize_test_state()
186 Editor_state.lines = load_array{'abc def', 'ghi'}
187 Text.redraw_all(Editor_state)
188 Editor_state.cursor1 = {line=2, pos=1}
189 edit.draw(Editor_state)
190 edit.run_after_keychord(Editor_state, 'M-left')
191 check_eq(Editor_state.cursor1.line, 1, 'line')
192 check_eq(Editor_state.cursor1.pos, 5, 'pos')
195 function test_move_past_end_of_word()
196 App.screen.init{width=120, height=60}
197 Editor_state = edit.initialize_test_state()
198 Editor_state.lines = load_array{'abc def'}
199 Text.redraw_all(Editor_state)
200 Editor_state.cursor1 = {line=1, pos=1}
201 edit.draw(Editor_state)
202 edit.run_after_keychord(Editor_state, 'M-right')
203 check_eq(Editor_state.cursor1.pos, 4, 'check')
206 function test_skip_to_next_word()
207 App.screen.init{width=120, height=60}
208 Editor_state = edit.initialize_test_state()
209 Editor_state.lines = load_array{'abc def'}
210 Text.redraw_all(Editor_state)
211 Editor_state.cursor1 = {line=1, pos=4} -- at the space between words
212 edit.draw(Editor_state)
213 edit.run_after_keychord(Editor_state, 'M-right')
214 check_eq(Editor_state.cursor1.pos, 8, 'check')
217 function test_skip_past_tab_to_next_word()
218 App.screen.init{width=120, height=60}
219 Editor_state = edit.initialize_test_state()
220 Editor_state.lines = load_array{'abc\tdef'}
221 Text.redraw_all(Editor_state)
222 Editor_state.cursor1 = {line=1, pos=1} -- at the space between words
223 edit.draw(Editor_state)
224 edit.run_after_keychord(Editor_state, 'M-right')
225 check_eq(Editor_state.cursor1.pos, 4, 'check')
228 function test_skip_multiple_spaces_to_next_word()
229 App.screen.init{width=120, height=60}
230 Editor_state = edit.initialize_test_state()
231 Editor_state.lines = load_array{'abc def'}
232 Text.redraw_all(Editor_state)
233 Editor_state.cursor1 = {line=1, pos=4} -- at the start of second word
234 edit.draw(Editor_state)
235 edit.run_after_keychord(Editor_state, 'M-right')
236 check_eq(Editor_state.cursor1.pos, 9, 'check')
239 function test_move_past_end_of_word_on_next_line()
240 App.screen.init{width=120, height=60}
241 Editor_state = edit.initialize_test_state()
242 Editor_state.lines = load_array{'abc def', 'ghi'}
243 Text.redraw_all(Editor_state)
244 Editor_state.cursor1 = {line=1, pos=8}
245 edit.draw(Editor_state)
246 edit.run_after_keychord(Editor_state, 'M-right')
247 check_eq(Editor_state.cursor1.line, 2, 'line')
248 check_eq(Editor_state.cursor1.pos, 4, 'pos')
251 function test_click_moves_cursor()
252 App.screen.init{width=50, height=60}
253 Editor_state = edit.initialize_test_state()
254 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
255 Text.redraw_all(Editor_state)
256 Editor_state.cursor1 = {line=1, pos=1}
257 Editor_state.screen_top1 = {line=1, pos=1}
258 Editor_state.screen_bottom1 = {}
259 Editor_state.selection1 = {}
260 edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
261 edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
262 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
263 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
264 -- selection is empty to avoid perturbing future edits
265 check_nil(Editor_state.selection1.line, 'selection:line')
266 check_nil(Editor_state.selection1.pos, 'selection:pos')
269 function test_click_to_left_of_line()
270 -- display a line with the cursor in the middle
271 App.screen.init{width=50, height=80}
272 Editor_state = edit.initialize_test_state()
273 Editor_state.lines = load_array{'abc'}
274 Text.redraw_all(Editor_state)
275 Editor_state.cursor1 = {line=1, pos=3}
276 Editor_state.screen_top1 = {line=1, pos=1}
277 Editor_state.screen_bottom1 = {}
278 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_often_shows_start_of_wrapping_line()
1065 -- draw a few lines ending in part of a wrapping line
1066 App.screen.init{width=50, height=60}
1067 Editor_state = edit.initialize_test_state()
1068 Editor_state.lines = load_array{'abc', 'def ghi jkl', 'mno'}
1069 Text.redraw_all(Editor_state)
1070 Editor_state.cursor1 = {line=1, pos=1}
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 start drawing from the bottom _line_ (multiple screen lines)
1081 edit.run_after_keychord(Editor_state, 'pagedown')
1082 check_eq(Editor_state.screen_top1.line, 2, 'screen_top:line')
1083 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1084 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1085 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1086 y = Editor_state.top
1087 App.screen.check(y, 'def ', 'screen:1')
1088 y = y + Editor_state.line_height
1089 App.screen.check(y, 'ghi ', 'screen:2')
1090 y = y + Editor_state.line_height
1091 App.screen.check(y, 'jkl', 'screen:3')
1094 function test_pagedown_can_start_from_middle_of_long_wrapping_line()
1095 -- draw a few lines starting from a very long wrapping line
1096 App.screen.init{width=Editor_state.left+30, height=60}
1097 Editor_state = edit.initialize_test_state()
1098 Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu vwx yza bcd efg hij', 'XYZ'}
1099 Text.redraw_all(Editor_state)
1100 Editor_state.cursor1 = {line=1, pos=2}
1101 Editor_state.screen_top1 = {line=1, pos=1}
1102 Editor_state.screen_bottom1 = {}
1103 edit.draw(Editor_state)
1104 local y = Editor_state.top
1105 App.screen.check(y, 'abc ', 'baseline/screen:1')
1106 y = y + Editor_state.line_height
1107 App.screen.check(y, 'def ', 'baseline/screen:2')
1108 y = y + Editor_state.line_height
1109 App.screen.check(y, 'ghi ', 'baseline/screen:3')
1110 -- after pagedown we scroll down the very long wrapping line
1111 edit.run_after_keychord(Editor_state, 'pagedown')
1112 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1113 check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
1114 y = Editor_state.top
1115 App.screen.check(y, 'ghi ', 'screen:1')
1116 y = y + Editor_state.line_height
1117 App.screen.check(y, 'jkl ', 'screen:2')
1118 y = y + Editor_state.line_height
1119 if Version == '12.0' then
1120 -- HACK: Maybe v12.0 uses a different font? Strange that it only causes
1121 -- issues in a couple of places.
1122 -- We'll need to rethink our tests if issues like this start to multiply.
1123 App.screen.check(y, 'mno ', 'screen:3')
1124 else
1125 App.screen.check(y, 'mn', 'screen:3')
1129 function test_pagedown_never_moves_up()
1130 -- draw the final screen line of a wrapping line
1131 App.screen.init{width=Editor_state.left+30, height=60}
1132 Editor_state = edit.initialize_test_state()
1133 Editor_state.lines = load_array{'abc def ghi'}
1134 Text.redraw_all(Editor_state)
1135 Editor_state.cursor1 = {line=1, pos=9}
1136 Editor_state.screen_top1 = {line=1, pos=9}
1137 Editor_state.screen_bottom1 = {}
1138 edit.draw(Editor_state)
1139 -- pagedown makes no change
1140 edit.run_after_keychord(Editor_state, 'pagedown')
1141 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1142 check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
1145 function test_down_arrow_moves_cursor()
1146 App.screen.init{width=120, height=60}
1147 Editor_state = edit.initialize_test_state()
1148 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1149 Text.redraw_all(Editor_state)
1150 Editor_state.cursor1 = {line=1, pos=1}
1151 Editor_state.screen_top1 = {line=1, pos=1}
1152 Editor_state.screen_bottom1 = {}
1153 -- initially the first three lines are displayed
1154 edit.draw(Editor_state)
1155 local y = Editor_state.top
1156 App.screen.check(y, 'abc', 'baseline/screen:1')
1157 y = y + Editor_state.line_height
1158 App.screen.check(y, 'def', 'baseline/screen:2')
1159 y = y + Editor_state.line_height
1160 App.screen.check(y, 'ghi', 'baseline/screen:3')
1161 -- after hitting the down arrow, the cursor moves down by 1 line
1162 edit.run_after_keychord(Editor_state, 'down')
1163 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1164 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1165 -- the screen is unchanged
1166 y = Editor_state.top
1167 App.screen.check(y, 'abc', 'screen:1')
1168 y = y + Editor_state.line_height
1169 App.screen.check(y, 'def', 'screen:2')
1170 y = y + Editor_state.line_height
1171 App.screen.check(y, 'ghi', 'screen:3')
1174 function test_down_arrow_skips_drawing()
1175 -- some lines of text with a drawing intermixed
1176 local drawing_width = 50
1177 App.screen.init{width=Editor_state.left+drawing_width, height=100}
1178 Editor_state = edit.initialize_test_state()
1179 Editor_state.lines = load_array{'abc', -- height 15
1180 '```lines', '```', -- height 25
1181 'ghi'}
1182 Text.redraw_all(Editor_state)
1183 Editor_state.cursor1 = {line=1, pos=1}
1184 Editor_state.screen_top1 = {line=1, pos=1}
1185 Editor_state.screen_bottom1 = {}
1186 edit.draw(Editor_state)
1187 local y = Editor_state.top
1188 App.screen.check(y, 'abc', 'baseline/screen:1')
1189 y = y + Editor_state.line_height
1190 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1191 y = y + drawing_height
1192 App.screen.check(y, 'ghi', 'baseline/screen:3')
1193 check(Editor_state.cursor_x, 'baseline/cursor_x')
1194 -- after hitting the down arrow the cursor moves down by 2 lines, skipping the drawing
1195 edit.run_after_keychord(Editor_state, 'down')
1196 check_eq(Editor_state.cursor1.line, 3, 'cursor')
1199 function test_down_arrow_scrolls_down_by_one_line()
1200 -- display the first three lines with the cursor on the bottom line
1201 App.screen.init{width=120, height=60}
1202 Editor_state = edit.initialize_test_state()
1203 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1204 Text.redraw_all(Editor_state)
1205 Editor_state.cursor1 = {line=3, pos=1}
1206 Editor_state.screen_top1 = {line=1, pos=1}
1207 Editor_state.screen_bottom1 = {}
1208 edit.draw(Editor_state)
1209 local y = Editor_state.top
1210 App.screen.check(y, 'abc', 'baseline/screen:1')
1211 y = y + Editor_state.line_height
1212 App.screen.check(y, 'def', 'baseline/screen:2')
1213 y = y + Editor_state.line_height
1214 App.screen.check(y, 'ghi', 'baseline/screen:3')
1215 -- after hitting the down arrow the screen scrolls down by one line
1216 edit.run_after_keychord(Editor_state, 'down')
1217 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1218 check_eq(Editor_state.cursor1.line, 4, 'cursor')
1219 y = Editor_state.top
1220 App.screen.check(y, 'def', 'screen:1')
1221 y = y + Editor_state.line_height
1222 App.screen.check(y, 'ghi', 'screen:2')
1223 y = y + Editor_state.line_height
1224 App.screen.check(y, 'jkl', 'screen:3')
1227 function test_down_arrow_scrolls_down_by_one_screen_line()
1228 -- display the first three lines with the cursor on the bottom line
1229 App.screen.init{width=Editor_state.left+30, height=60}
1230 Editor_state = edit.initialize_test_state()
1231 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1232 Text.redraw_all(Editor_state)
1233 Editor_state.cursor1 = {line=3, pos=1}
1234 Editor_state.screen_top1 = {line=1, pos=1}
1235 Editor_state.screen_bottom1 = {}
1236 edit.draw(Editor_state)
1237 local y = Editor_state.top
1238 App.screen.check(y, 'abc', 'baseline/screen:1')
1239 y = y + Editor_state.line_height
1240 App.screen.check(y, 'def', 'baseline/screen:2')
1241 y = y + Editor_state.line_height
1242 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1243 -- after hitting the down arrow the screen scrolls down by one line
1244 edit.run_after_keychord(Editor_state, 'down')
1245 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1246 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1247 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1248 y = Editor_state.top
1249 App.screen.check(y, 'def', 'screen:1')
1250 y = y + Editor_state.line_height
1251 App.screen.check(y, 'ghi ', 'screen:2')
1252 y = y + Editor_state.line_height
1253 App.screen.check(y, 'jkl', 'screen:3')
1256 function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_word()
1257 -- display the first three lines with the cursor on the bottom line
1258 App.screen.init{width=Editor_state.left+30, height=60}
1259 Editor_state = edit.initialize_test_state()
1260 Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
1261 Text.redraw_all(Editor_state)
1262 Editor_state.cursor1 = {line=3, pos=1}
1263 Editor_state.screen_top1 = {line=1, pos=1}
1264 Editor_state.screen_bottom1 = {}
1265 edit.draw(Editor_state)
1266 local y = Editor_state.top
1267 App.screen.check(y, 'abc', 'baseline/screen:1')
1268 y = y + Editor_state.line_height
1269 App.screen.check(y, 'def', 'baseline/screen:2')
1270 y = y + Editor_state.line_height
1271 App.screen.check(y, 'ghij', 'baseline/screen:3')
1272 -- after hitting the down arrow the screen scrolls down by one line
1273 edit.run_after_keychord(Editor_state, 'down')
1274 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1275 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1276 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1277 y = Editor_state.top
1278 App.screen.check(y, 'def', 'screen:1')
1279 y = y + Editor_state.line_height
1280 App.screen.check(y, 'ghij', 'screen:2')
1281 y = y + Editor_state.line_height
1282 App.screen.check(y, 'kl', 'screen:3')
1285 function test_pagedown_followed_by_down_arrow_does_not_scroll_screen_up()
1286 App.screen.init{width=Editor_state.left+30, height=60}
1287 Editor_state = edit.initialize_test_state()
1288 Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
1289 Text.redraw_all(Editor_state)
1290 Editor_state.cursor1 = {line=3, pos=1}
1291 Editor_state.screen_top1 = {line=1, pos=1}
1292 Editor_state.screen_bottom1 = {}
1293 edit.draw(Editor_state)
1294 local y = Editor_state.top
1295 App.screen.check(y, 'abc', 'baseline/screen:1')
1296 y = y + Editor_state.line_height
1297 App.screen.check(y, 'def', 'baseline/screen:2')
1298 y = y + Editor_state.line_height
1299 App.screen.check(y, 'ghij', 'baseline/screen:3')
1300 -- after hitting pagedown the screen scrolls down to start of a long line
1301 edit.run_after_keychord(Editor_state, 'pagedown')
1302 check_eq(Editor_state.screen_top1.line, 3, 'baseline2/screen_top')
1303 check_eq(Editor_state.cursor1.line, 3, 'baseline2/cursor:line')
1304 check_eq(Editor_state.cursor1.pos, 1, 'baseline2/cursor:pos')
1305 -- after hitting down arrow the screen doesn't scroll down further, and certainly doesn't scroll up
1306 edit.run_after_keychord(Editor_state, 'down')
1307 check_eq(Editor_state.screen_top1.line, 3, 'screen_top')
1308 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1309 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1310 y = Editor_state.top
1311 App.screen.check(y, 'ghij', 'screen:1')
1312 y = y + Editor_state.line_height
1313 App.screen.check(y, 'kl', 'screen:2')
1314 y = y + Editor_state.line_height
1315 App.screen.check(y, 'mno', 'screen:3')
1318 function test_up_arrow_moves_cursor()
1319 -- display the first 3 lines with the cursor on the bottom line
1320 App.screen.init{width=120, height=60}
1321 Editor_state = edit.initialize_test_state()
1322 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1323 Text.redraw_all(Editor_state)
1324 Editor_state.cursor1 = {line=3, pos=1}
1325 Editor_state.screen_top1 = {line=1, pos=1}
1326 Editor_state.screen_bottom1 = {}
1327 edit.draw(Editor_state)
1328 local y = Editor_state.top
1329 App.screen.check(y, 'abc', 'baseline/screen:1')
1330 y = y + Editor_state.line_height
1331 App.screen.check(y, 'def', 'baseline/screen:2')
1332 y = y + Editor_state.line_height
1333 App.screen.check(y, 'ghi', 'baseline/screen:3')
1334 -- after hitting the up arrow the cursor moves up by 1 line
1335 edit.run_after_keychord(Editor_state, 'up')
1336 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1337 check_eq(Editor_state.cursor1.line, 2, 'cursor')
1338 -- the screen is unchanged
1339 y = Editor_state.top
1340 App.screen.check(y, 'abc', 'screen:1')
1341 y = y + Editor_state.line_height
1342 App.screen.check(y, 'def', 'screen:2')
1343 y = y + Editor_state.line_height
1344 App.screen.check(y, 'ghi', 'screen:3')
1347 function test_up_arrow_skips_drawing()
1348 -- some lines of text with a drawing intermixed
1349 local drawing_width = 50
1350 App.screen.init{width=Editor_state.left+drawing_width, height=100}
1351 Editor_state = edit.initialize_test_state()
1352 Editor_state.lines = load_array{'abc', -- height 15
1353 '```lines', '```', -- height 25
1354 'ghi'}
1355 Text.redraw_all(Editor_state)
1356 Editor_state.cursor1 = {line=3, pos=1}
1357 Editor_state.screen_top1 = {line=1, pos=1}
1358 Editor_state.screen_bottom1 = {}
1359 edit.draw(Editor_state)
1360 local y = Editor_state.top
1361 App.screen.check(y, 'abc', 'baseline/screen:1')
1362 y = y + Editor_state.line_height
1363 local drawing_height = Drawing_padding_height + drawing_width/2 -- default
1364 y = y + drawing_height
1365 App.screen.check(y, 'ghi', 'baseline/screen:3')
1366 check(Editor_state.cursor_x, 'baseline/cursor_x')
1367 -- after hitting the up arrow the cursor moves up by 2 lines, skipping the drawing
1368 edit.run_after_keychord(Editor_state, 'up')
1369 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1372 function test_up_arrow_scrolls_up_by_one_line()
1373 -- display the lines 2/3/4 with the cursor on line 2
1374 App.screen.init{width=120, height=60}
1375 Editor_state = edit.initialize_test_state()
1376 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1377 Text.redraw_all(Editor_state)
1378 Editor_state.cursor1 = {line=2, pos=1}
1379 Editor_state.screen_top1 = {line=2, pos=1}
1380 Editor_state.screen_bottom1 = {}
1381 edit.draw(Editor_state)
1382 local y = Editor_state.top
1383 App.screen.check(y, 'def', 'baseline/screen:1')
1384 y = y + Editor_state.line_height
1385 App.screen.check(y, 'ghi', 'baseline/screen:2')
1386 y = y + Editor_state.line_height
1387 App.screen.check(y, 'jkl', 'baseline/screen:3')
1388 -- after hitting the up arrow the screen scrolls up by one line
1389 edit.run_after_keychord(Editor_state, 'up')
1390 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1391 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1392 y = Editor_state.top
1393 App.screen.check(y, 'abc', 'screen:1')
1394 y = y + Editor_state.line_height
1395 App.screen.check(y, 'def', 'screen:2')
1396 y = y + Editor_state.line_height
1397 App.screen.check(y, 'ghi', 'screen:3')
1400 function test_up_arrow_scrolls_up_by_one_line_skipping_drawing()
1401 -- display lines 3/4/5 with a drawing just off screen at line 2
1402 App.screen.init{width=120, height=60}
1403 Editor_state = edit.initialize_test_state()
1404 Editor_state.lines = load_array{'abc', '```lines', '```', 'def', 'ghi', 'jkl'}
1405 Text.redraw_all(Editor_state)
1406 Editor_state.cursor1 = {line=3, pos=1}
1407 Editor_state.screen_top1 = {line=3, pos=1}
1408 Editor_state.screen_bottom1 = {}
1409 edit.draw(Editor_state)
1410 local y = Editor_state.top
1411 App.screen.check(y, 'def', 'baseline/screen:1')
1412 y = y + Editor_state.line_height
1413 App.screen.check(y, 'ghi', 'baseline/screen:2')
1414 y = y + Editor_state.line_height
1415 App.screen.check(y, 'jkl', 'baseline/screen:3')
1416 -- after hitting the up arrow the screen scrolls up to previous text line
1417 edit.run_after_keychord(Editor_state, 'up')
1418 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1419 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1422 function test_up_arrow_scrolls_up_by_one_screen_line()
1423 -- display lines starting from second screen line of a line
1424 App.screen.init{width=Editor_state.left+30, height=60}
1425 Editor_state = edit.initialize_test_state()
1426 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1427 Text.redraw_all(Editor_state)
1428 Editor_state.cursor1 = {line=3, pos=6}
1429 Editor_state.screen_top1 = {line=3, pos=5}
1430 Editor_state.screen_bottom1 = {}
1431 edit.draw(Editor_state)
1432 local y = Editor_state.top
1433 App.screen.check(y, 'jkl', 'baseline/screen:1')
1434 y = y + Editor_state.line_height
1435 App.screen.check(y, 'mno', 'baseline/screen:2')
1436 -- after hitting the up arrow the screen scrolls up to first screen line
1437 edit.run_after_keychord(Editor_state, 'up')
1438 y = Editor_state.top
1439 App.screen.check(y, 'ghi ', 'screen:1')
1440 y = y + Editor_state.line_height
1441 App.screen.check(y, 'jkl', 'screen:2')
1442 y = y + Editor_state.line_height
1443 App.screen.check(y, 'mno', 'screen:3')
1444 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1445 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1446 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1447 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1450 function test_up_arrow_scrolls_up_to_final_screen_line()
1451 -- display lines starting just after a long line
1452 App.screen.init{width=Editor_state.left+30, height=60}
1453 Editor_state = edit.initialize_test_state()
1454 Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
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, 'ghi', 'baseline/screen:1')
1462 y = y + Editor_state.line_height
1463 App.screen.check(y, 'jkl', 'baseline/screen:2')
1464 y = y + Editor_state.line_height
1465 App.screen.check(y, 'mno', 'baseline/screen:3')
1466 -- after hitting the up arrow the screen scrolls up to final screen line of previous line
1467 edit.run_after_keychord(Editor_state, 'up')
1468 y = Editor_state.top
1469 App.screen.check(y, 'def', 'screen:1')
1470 y = y + Editor_state.line_height
1471 App.screen.check(y, 'ghi', 'screen:2')
1472 y = y + Editor_state.line_height
1473 App.screen.check(y, 'jkl', 'screen:3')
1474 check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
1475 check_eq(Editor_state.screen_top1.pos, 5, 'screen_top:pos')
1476 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1477 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
1480 function test_up_arrow_scrolls_up_to_empty_line()
1481 -- display a screenful of text with an empty line just above it outside the screen
1482 App.screen.init{width=120, height=60}
1483 Editor_state = edit.initialize_test_state()
1484 Editor_state.lines = load_array{'', 'abc', 'def', 'ghi', 'jkl'}
1485 Text.redraw_all(Editor_state)
1486 Editor_state.cursor1 = {line=2, pos=1}
1487 Editor_state.screen_top1 = {line=2, pos=1}
1488 Editor_state.screen_bottom1 = {}
1489 edit.draw(Editor_state)
1490 local y = Editor_state.top
1491 App.screen.check(y, 'abc', 'baseline/screen:1')
1492 y = y + Editor_state.line_height
1493 App.screen.check(y, 'def', 'baseline/screen:2')
1494 y = y + Editor_state.line_height
1495 App.screen.check(y, 'ghi', 'baseline/screen:3')
1496 -- after hitting the up arrow the screen scrolls up by one line
1497 edit.run_after_keychord(Editor_state, 'up')
1498 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1499 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1500 y = Editor_state.top
1501 -- empty first line
1502 y = y + Editor_state.line_height
1503 App.screen.check(y, 'abc', 'screen:2')
1504 y = y + Editor_state.line_height
1505 App.screen.check(y, 'def', 'screen:3')
1508 function test_pageup()
1509 App.screen.init{width=120, height=45}
1510 Editor_state = edit.initialize_test_state()
1511 Editor_state.lines = load_array{'abc', 'def', 'ghi'}
1512 Text.redraw_all(Editor_state)
1513 Editor_state.cursor1 = {line=2, pos=1}
1514 Editor_state.screen_top1 = {line=2, pos=1}
1515 Editor_state.screen_bottom1 = {}
1516 -- initially the last two lines are displayed
1517 edit.draw(Editor_state)
1518 local y = Editor_state.top
1519 App.screen.check(y, 'def', 'baseline/screen:1')
1520 y = y + Editor_state.line_height
1521 App.screen.check(y, 'ghi', 'baseline/screen:2')
1522 -- after pageup the cursor goes to first line
1523 edit.run_after_keychord(Editor_state, 'pageup')
1524 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1525 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1526 y = Editor_state.top
1527 App.screen.check(y, 'abc', 'screen:1')
1528 y = y + Editor_state.line_height
1529 App.screen.check(y, 'def', 'screen:2')
1532 function test_pageup_scrolls_up_by_screen_line()
1533 -- display the first three lines with the cursor on the bottom line
1534 App.screen.init{width=Editor_state.left+30, height=60}
1535 Editor_state = edit.initialize_test_state()
1536 Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
1537 Text.redraw_all(Editor_state)
1538 Editor_state.cursor1 = {line=2, pos=1}
1539 Editor_state.screen_top1 = {line=2, pos=1}
1540 Editor_state.screen_bottom1 = {}
1541 edit.draw(Editor_state)
1542 local y = Editor_state.top
1543 App.screen.check(y, 'ghi', 'baseline/screen:1')
1544 y = y + Editor_state.line_height
1545 App.screen.check(y, 'jkl', 'baseline/screen:2')
1546 y = y + Editor_state.line_height
1547 App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1548 -- after hitting the page-up key the screen scrolls up to top
1549 edit.run_after_keychord(Editor_state, 'pageup')
1550 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1551 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1552 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1553 y = Editor_state.top
1554 App.screen.check(y, 'abc ', 'screen:1')
1555 y = y + Editor_state.line_height
1556 App.screen.check(y, 'def', 'screen:2')
1557 y = y + Editor_state.line_height
1558 App.screen.check(y, 'ghi', 'screen:3')
1561 function test_pageup_scrolls_up_from_middle_screen_line()
1562 -- display a few lines starting from the middle of a line (Editor_state.cursor1.pos > 1)
1563 App.screen.init{width=Editor_state.left+30, height=60}
1564 Editor_state = edit.initialize_test_state()
1565 Editor_state.lines = load_array{'abc def', 'ghi jkl', 'mno'}
1566 Text.redraw_all(Editor_state)
1567 Editor_state.cursor1 = {line=2, pos=5}
1568 Editor_state.screen_top1 = {line=2, pos=5}
1569 Editor_state.screen_bottom1 = {}
1570 edit.draw(Editor_state)
1571 local y = Editor_state.top
1572 App.screen.check(y, 'jkl', 'baseline/screen:2')
1573 y = y + Editor_state.line_height
1574 App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1575 -- after hitting the page-up key the screen scrolls up to top
1576 edit.run_after_keychord(Editor_state, 'pageup')
1577 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1578 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1579 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1580 y = Editor_state.top
1581 App.screen.check(y, 'abc ', 'screen:1')
1582 y = y + Editor_state.line_height
1583 App.screen.check(y, 'def', 'screen:2')
1584 y = y + Editor_state.line_height
1585 App.screen.check(y, 'ghi ', 'screen:3')
1588 function test_enter_on_bottom_line_scrolls_down()
1589 -- display a few lines with cursor on bottom line
1590 App.screen.init{width=Editor_state.left+30, height=60}
1591 Editor_state = edit.initialize_test_state()
1592 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1593 Text.redraw_all(Editor_state)
1594 Editor_state.cursor1 = {line=3, pos=2}
1595 Editor_state.screen_top1 = {line=1, pos=1}
1596 Editor_state.screen_bottom1 = {}
1597 edit.draw(Editor_state)
1598 local y = Editor_state.top
1599 App.screen.check(y, 'abc', 'baseline/screen:1')
1600 y = y + Editor_state.line_height
1601 App.screen.check(y, 'def', 'baseline/screen:2')
1602 y = y + Editor_state.line_height
1603 App.screen.check(y, 'ghi', 'baseline/screen:3')
1604 -- after hitting the enter key the screen scrolls down
1605 edit.run_after_keychord(Editor_state, 'return')
1606 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1607 check_eq(Editor_state.cursor1.line, 4, 'cursor:line')
1608 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1609 y = Editor_state.top
1610 App.screen.check(y, 'def', 'screen:1')
1611 y = y + Editor_state.line_height
1612 App.screen.check(y, 'g', 'screen:2')
1613 y = y + Editor_state.line_height
1614 App.screen.check(y, 'hi', 'screen:3')
1617 function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1618 -- display just the bottom line on screen
1619 App.screen.init{width=Editor_state.left+30, height=60}
1620 Editor_state = edit.initialize_test_state()
1621 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1622 Text.redraw_all(Editor_state)
1623 Editor_state.cursor1 = {line=4, pos=2}
1624 Editor_state.screen_top1 = {line=4, pos=1}
1625 Editor_state.screen_bottom1 = {}
1626 edit.draw(Editor_state)
1627 local y = Editor_state.top
1628 App.screen.check(y, 'jkl', 'baseline/screen:1')
1629 -- after hitting the enter key the screen does not scroll down
1630 edit.run_after_keychord(Editor_state, 'return')
1631 check_eq(Editor_state.screen_top1.line, 4, 'screen_top')
1632 check_eq(Editor_state.cursor1.line, 5, 'cursor:line')
1633 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1634 y = Editor_state.top
1635 App.screen.check(y, 'j', 'screen:1')
1636 y = y + Editor_state.line_height
1637 App.screen.check(y, 'kl', 'screen:2')
1640 function test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1641 -- display just an empty bottom line on screen
1642 App.screen.init{width=Editor_state.left+30, height=60}
1643 Editor_state = edit.initialize_test_state()
1644 Editor_state.lines = load_array{'abc', ''}
1645 Text.redraw_all(Editor_state)
1646 Editor_state.cursor1 = {line=2, pos=1}
1647 Editor_state.screen_top1 = {line=2, pos=1}
1648 Editor_state.screen_bottom1 = {}
1649 edit.draw(Editor_state)
1650 -- after hitting the inserting_text key the screen does not scroll down
1651 edit.run_after_text_input(Editor_state, 'a')
1652 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1653 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
1654 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1655 local y = Editor_state.top
1656 App.screen.check(y, 'a', 'screen:1')
1659 function test_typing_on_bottom_line_scrolls_down()
1660 -- display a few lines with cursor on bottom line
1661 App.screen.init{width=Editor_state.left+30, height=60}
1662 Editor_state = edit.initialize_test_state()
1663 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1664 Text.redraw_all(Editor_state)
1665 Editor_state.cursor1 = {line=3, pos=4}
1666 Editor_state.screen_top1 = {line=1, pos=1}
1667 Editor_state.screen_bottom1 = {}
1668 edit.draw(Editor_state)
1669 local y = Editor_state.top
1670 App.screen.check(y, 'abc', 'baseline/screen:1')
1671 y = y + Editor_state.line_height
1672 App.screen.check(y, 'def', 'baseline/screen:2')
1673 y = y + Editor_state.line_height
1674 App.screen.check(y, 'ghi', 'baseline/screen:3')
1675 -- after typing something the line wraps and the screen scrolls down
1676 edit.run_after_text_input(Editor_state, 'j')
1677 edit.run_after_text_input(Editor_state, 'k')
1678 edit.run_after_text_input(Editor_state, 'l')
1679 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1680 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1681 check_eq(Editor_state.cursor1.pos, 7, 'cursor:pos')
1682 y = Editor_state.top
1683 App.screen.check(y, 'def', 'screen:1')
1684 y = y + Editor_state.line_height
1685 App.screen.check(y, 'ghij', 'screen:2')
1686 y = y + Editor_state.line_height
1687 App.screen.check(y, 'kl', 'screen:3')
1690 function test_left_arrow_scrolls_up_in_wrapped_line()
1691 -- display lines starting from second screen line of a line
1692 App.screen.init{width=Editor_state.left+30, height=60}
1693 Editor_state = edit.initialize_test_state()
1694 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1695 Text.redraw_all(Editor_state)
1696 Editor_state.screen_top1 = {line=3, pos=5}
1697 Editor_state.screen_bottom1 = {}
1698 -- cursor is at top of screen
1699 Editor_state.cursor1 = {line=3, pos=5}
1700 edit.draw(Editor_state)
1701 local y = Editor_state.top
1702 App.screen.check(y, 'jkl', 'baseline/screen:1')
1703 y = y + Editor_state.line_height
1704 App.screen.check(y, 'mno', 'baseline/screen:2')
1705 -- after hitting the left arrow the screen scrolls up to first screen line
1706 edit.run_after_keychord(Editor_state, 'left')
1707 y = Editor_state.top
1708 App.screen.check(y, 'ghi ', 'screen:1')
1709 y = y + Editor_state.line_height
1710 App.screen.check(y, 'jkl', 'screen:2')
1711 y = y + Editor_state.line_height
1712 App.screen.check(y, 'mno', 'screen:3')
1713 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1714 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1715 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1716 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
1719 function test_right_arrow_scrolls_down_in_wrapped_line()
1720 -- display the first three lines with the cursor on the bottom 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=1, pos=1}
1726 Editor_state.screen_bottom1 = {}
1727 -- cursor is at bottom right 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, 'abc', 'baseline/screen:1')
1732 y = y + Editor_state.line_height
1733 App.screen.check(y, 'def', 'baseline/screen:2')
1734 y = y + Editor_state.line_height
1735 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1736 -- after hitting the right arrow the screen scrolls down by one line
1737 edit.run_after_keychord(Editor_state, 'right')
1738 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1739 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1740 check_eq(Editor_state.cursor1.pos, 6, 'cursor:pos')
1741 y = Editor_state.top
1742 App.screen.check(y, 'def', 'screen:1')
1743 y = y + Editor_state.line_height
1744 App.screen.check(y, 'ghi ', 'screen:2')
1745 y = y + Editor_state.line_height
1746 App.screen.check(y, 'jkl', 'screen:3')
1749 function test_home_scrolls_up_in_wrapped_line()
1750 -- display lines starting from second screen line of a line
1751 App.screen.init{width=Editor_state.left+30, height=60}
1752 Editor_state = edit.initialize_test_state()
1753 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1754 Text.redraw_all(Editor_state)
1755 Editor_state.screen_top1 = {line=3, pos=5}
1756 Editor_state.screen_bottom1 = {}
1757 -- cursor is at top of screen
1758 Editor_state.cursor1 = {line=3, pos=5}
1759 edit.draw(Editor_state)
1760 local y = Editor_state.top
1761 App.screen.check(y, 'jkl', 'baseline/screen:1')
1762 y = y + Editor_state.line_height
1763 App.screen.check(y, 'mno', 'baseline/screen:2')
1764 -- after hitting home the screen scrolls up to first screen line
1765 edit.run_after_keychord(Editor_state, 'home')
1766 y = Editor_state.top
1767 App.screen.check(y, 'ghi ', 'screen:1')
1768 y = y + Editor_state.line_height
1769 App.screen.check(y, 'jkl', 'screen:2')
1770 y = y + Editor_state.line_height
1771 App.screen.check(y, 'mno', 'screen:3')
1772 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1773 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1774 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1775 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1778 function test_end_scrolls_down_in_wrapped_line()
1779 -- display the first three lines with the cursor on the bottom line
1780 App.screen.init{width=Editor_state.left+30, height=60}
1781 Editor_state = edit.initialize_test_state()
1782 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1783 Text.redraw_all(Editor_state)
1784 Editor_state.screen_top1 = {line=1, pos=1}
1785 Editor_state.screen_bottom1 = {}
1786 -- cursor is at bottom right of screen
1787 Editor_state.cursor1 = {line=3, pos=5}
1788 edit.draw(Editor_state)
1789 local y = Editor_state.top
1790 App.screen.check(y, 'abc', 'baseline/screen:1')
1791 y = y + Editor_state.line_height
1792 App.screen.check(y, 'def', 'baseline/screen:2')
1793 y = y + Editor_state.line_height
1794 App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1795 -- after hitting end the screen scrolls down by one line
1796 edit.run_after_keychord(Editor_state, 'end')
1797 check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
1798 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1799 check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
1800 y = Editor_state.top
1801 App.screen.check(y, 'def', 'screen:1')
1802 y = y + Editor_state.line_height
1803 App.screen.check(y, 'ghi ', 'screen:2')
1804 y = y + Editor_state.line_height
1805 App.screen.check(y, 'jkl', 'screen:3')
1808 function test_position_cursor_on_recently_edited_wrapping_line()
1809 -- draw a line wrapping over 2 screen lines
1810 App.screen.init{width=100, height=200}
1811 Editor_state = edit.initialize_test_state()
1812 Editor_state.lines = load_array{'abc def ghi jkl mno pqr ', 'xyz'}
1813 Text.redraw_all(Editor_state)
1814 Editor_state.cursor1 = {line=1, pos=25}
1815 Editor_state.screen_top1 = {line=1, pos=1}
1816 Editor_state.screen_bottom1 = {}
1817 edit.draw(Editor_state)
1818 local y = Editor_state.top
1819 App.screen.check(y, 'abc def ghi ', 'baseline1/screen:1')
1820 y = y + Editor_state.line_height
1821 App.screen.check(y, 'jkl mno pqr ', 'baseline1/screen:2')
1822 y = y + Editor_state.line_height
1823 App.screen.check(y, 'xyz', 'baseline1/screen:3')
1824 -- add to the line until it's wrapping over 3 screen lines
1825 edit.run_after_text_input(Editor_state, 's')
1826 edit.run_after_text_input(Editor_state, 't')
1827 edit.run_after_text_input(Editor_state, 'u')
1828 check_eq(Editor_state.cursor1.pos, 28, 'cursor:pos')
1829 y = Editor_state.top
1830 App.screen.check(y, 'abc def ghi ', 'baseline2/screen:1')
1831 y = y + Editor_state.line_height
1832 App.screen.check(y, 'jkl mno pqr ', 'baseline2/screen:2')
1833 y = y + Editor_state.line_height
1834 App.screen.check(y, 'stu', 'baseline2/screen:3')
1835 -- try to move the cursor earlier in the third screen line by clicking the mouse
1836 edit.run_after_mouse_release(Editor_state, Editor_state.left+2,Editor_state.top+Editor_state.line_height*2+5, 1)
1837 -- cursor should move
1838 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1839 check_eq(Editor_state.cursor1.pos, 25, 'cursor:pos')
1842 function test_backspace_can_scroll_up()
1843 -- display the lines 2/3/4 with the cursor on line 2
1844 App.screen.init{width=120, height=60}
1845 Editor_state = edit.initialize_test_state()
1846 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
1847 Text.redraw_all(Editor_state)
1848 Editor_state.cursor1 = {line=2, pos=1}
1849 Editor_state.screen_top1 = {line=2, pos=1}
1850 Editor_state.screen_bottom1 = {}
1851 edit.draw(Editor_state)
1852 local y = Editor_state.top
1853 App.screen.check(y, 'def', 'baseline/screen:1')
1854 y = y + Editor_state.line_height
1855 App.screen.check(y, 'ghi', 'baseline/screen:2')
1856 y = y + Editor_state.line_height
1857 App.screen.check(y, 'jkl', 'baseline/screen:3')
1858 -- after hitting backspace the screen scrolls up by one line
1859 edit.run_after_keychord(Editor_state, 'backspace')
1860 check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
1861 check_eq(Editor_state.cursor1.line, 1, 'cursor')
1862 y = Editor_state.top
1863 App.screen.check(y, 'abcdef', 'screen:1')
1864 y = y + Editor_state.line_height
1865 App.screen.check(y, 'ghi', 'screen:2')
1866 y = y + Editor_state.line_height
1867 App.screen.check(y, 'jkl', 'screen:3')
1870 function test_backspace_can_scroll_up_screen_line()
1871 -- display lines starting from second screen line of a line
1872 App.screen.init{width=Editor_state.left+30, height=60}
1873 Editor_state = edit.initialize_test_state()
1874 Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
1875 Text.redraw_all(Editor_state)
1876 Editor_state.cursor1 = {line=3, pos=5}
1877 Editor_state.screen_top1 = {line=3, pos=5}
1878 Editor_state.screen_bottom1 = {}
1879 edit.draw(Editor_state)
1880 local y = Editor_state.top
1881 App.screen.check(y, 'jkl', 'baseline/screen:1')
1882 y = y + Editor_state.line_height
1883 App.screen.check(y, 'mno', 'baseline/screen:2')
1884 -- after hitting backspace the screen scrolls up by one screen line
1885 edit.run_after_keychord(Editor_state, 'backspace')
1886 y = Editor_state.top
1887 App.screen.check(y, 'ghij', 'screen:1')
1888 y = y + Editor_state.line_height
1889 App.screen.check(y, 'kl', 'screen:2')
1890 y = y + Editor_state.line_height
1891 App.screen.check(y, 'mno', 'screen:3')
1892 check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
1893 check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
1894 check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
1895 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
1898 function test_backspace_past_line_boundary()
1899 -- position cursor at start of a (non-first) line
1900 App.screen.init{width=Editor_state.left+30, height=60}
1901 Editor_state = edit.initialize_test_state()
1902 Editor_state.lines = load_array{'abc', 'def'}
1903 Text.redraw_all(Editor_state)
1904 Editor_state.cursor1 = {line=2, pos=1}
1905 -- backspace joins with previous line
1906 edit.run_after_keychord(Editor_state, 'backspace')
1907 check_eq(Editor_state.lines[1].data, 'abcdef', 'check')
1910 -- some tests for operating over selections created using Shift- chords
1911 -- we're just testing delete_selection, and it works the same for all keys
1913 function test_backspace_over_selection()
1914 -- select just one character within a line with cursor before selection
1915 App.screen.init{width=Editor_state.left+30, height=60}
1916 Editor_state = edit.initialize_test_state()
1917 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1918 Text.redraw_all(Editor_state)
1919 Editor_state.cursor1 = {line=1, pos=1}
1920 Editor_state.selection1 = {line=1, pos=2}
1921 -- backspace deletes the selected character, even though it's after the cursor
1922 edit.run_after_keychord(Editor_state, 'backspace')
1923 check_eq(Editor_state.lines[1].data, 'bc', 'data')
1924 -- cursor (remains) at start of selection
1925 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1926 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1927 -- selection is cleared
1928 check_nil(Editor_state.selection1.line, 'selection')
1931 function test_backspace_over_selection_reverse()
1932 -- select just one character within a line with cursor after selection
1933 App.screen.init{width=Editor_state.left+30, height=60}
1934 Editor_state = edit.initialize_test_state()
1935 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1936 Text.redraw_all(Editor_state)
1937 Editor_state.cursor1 = {line=1, pos=2}
1938 Editor_state.selection1 = {line=1, pos=1}
1939 -- backspace deletes the selected character
1940 edit.run_after_keychord(Editor_state, 'backspace')
1941 check_eq(Editor_state.lines[1].data, 'bc', 'data')
1942 -- cursor moves to start of selection
1943 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1944 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
1945 -- selection is cleared
1946 check_nil(Editor_state.selection1.line, 'selection')
1949 function test_backspace_over_multiple_lines()
1950 -- select just one character within a line with cursor after selection
1951 App.screen.init{width=Editor_state.left+30, height=60}
1952 Editor_state = edit.initialize_test_state()
1953 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1954 Text.redraw_all(Editor_state)
1955 Editor_state.cursor1 = {line=1, pos=2}
1956 Editor_state.selection1 = {line=4, pos=2}
1957 -- backspace deletes the region and joins the remaining portions of lines on either side
1958 edit.run_after_keychord(Editor_state, 'backspace')
1959 check_eq(Editor_state.lines[1].data, 'akl', 'data:1')
1960 check_eq(Editor_state.lines[2].data, 'mno', 'data:2')
1961 -- cursor remains at start of selection
1962 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1963 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1964 -- selection is cleared
1965 check_nil(Editor_state.selection1.line, 'selection')
1968 function test_backspace_to_end_of_line()
1969 -- select region from cursor to end of line
1970 App.screen.init{width=Editor_state.left+30, height=60}
1971 Editor_state = edit.initialize_test_state()
1972 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1973 Text.redraw_all(Editor_state)
1974 Editor_state.cursor1 = {line=1, pos=2}
1975 Editor_state.selection1 = {line=1, pos=4}
1976 -- backspace deletes rest of line without joining to any other line
1977 edit.run_after_keychord(Editor_state, 'backspace')
1978 check_eq(Editor_state.lines[1].data, 'a', 'data:1')
1979 check_eq(Editor_state.lines[2].data, 'def', 'data:2')
1980 -- cursor remains at start of selection
1981 check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
1982 check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
1983 -- selection is cleared
1984 check_nil(Editor_state.selection1.line, 'selection')
1987 function test_backspace_to_start_of_line()
1988 -- select region from cursor to start of line
1989 App.screen.init{width=Editor_state.left+30, height=60}
1990 Editor_state = edit.initialize_test_state()
1991 Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl', 'mno'}
1992 Text.redraw_all(Editor_state)
1993 Editor_state.cursor1 = {line=2, pos=1}
1994 Editor_state.selection1 = {line=2, pos=3}
1995 -- backspace deletes beginning of line without joining to any other line
1996 edit.run_after_keychord(Editor_state, 'backspace')
1997 check_eq(Editor_state.lines[1].data, 'abc', 'data:1')
1998 check_eq(Editor_state.lines[2].data, 'f', 'data:2')
1999 -- cursor remains at start of selection
2000 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
2001 check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
2002 -- selection is cleared
2003 check_nil(Editor_state.selection1.line, 'selection')
2006 function test_undo_insert_text()
2007 App.screen.init{width=120, height=60}
2008 Editor_state = edit.initialize_test_state()
2009 Editor_state.lines = load_array{'abc', 'def', 'xyz'}
2010 Text.redraw_all(Editor_state)
2011 Editor_state.cursor1 = {line=2, pos=4}
2012 Editor_state.screen_top1 = {line=1, pos=1}
2013 Editor_state.screen_bottom1 = {}
2014 -- insert a character
2015 edit.draw(Editor_state)
2016 edit.run_after_text_input(Editor_state, 'g')
2017 check_eq(Editor_state.cursor1.line, 2, 'baseline/cursor:line')
2018 check_eq(Editor_state.cursor1.pos, 5, 'baseline/cursor:pos')
2019 check_nil(Editor_state.selection1.line, 'baseline/selection:line')
2020 check_nil(Editor_state.selection1.pos, 'baseline/selection:pos')
2021 local y = Editor_state.top
2022 App.screen.check(y, 'abc', 'baseline/screen:1')
2023 y = y + Editor_state.line_height
2024 App.screen.check(y, 'defg', 'baseline/screen:2')
2025 y = y + Editor_state.line_height
2026 App.screen.check(y, 'xyz', 'baseline/screen:3')
2027 -- undo
2028 edit.run_after_keychord(Editor_state, 'C-z')
2029 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
2030 check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
2031 check_nil(Editor_state.selection1.line, 'selection:line')
2032 check_nil(Editor_state.selection1.pos, 'selection:pos')
2033 y = Editor_state.top
2034 App.screen.check(y, 'abc', 'screen:1')
2035 y = y + Editor_state.line_height
2036 App.screen.check(y, 'def', 'screen:2')
2037 y = y + Editor_state.line_height
2038 App.screen.check(y, 'xyz', 'screen:3')
2041 function test_undo_delete_text()
2042 App.screen.init{width=120, height=60}
2043 Editor_state = edit.initialize_test_state()
2044 Editor_state.lines = load_array{'abc', 'defg', 'xyz'}
2045 Text.redraw_all(Editor_state)
2046 Editor_state.cursor1 = {line=2, pos=5}
2047 Editor_state.screen_top1 = {line=1, pos=1}
2048 Editor_state.screen_bottom1 = {}
2049 -- delete a character
2050 edit.run_after_keychord(Editor_state, 'backspace')
2051 check_eq(Editor_state.cursor1.line, 2, 'baseline/cursor:line')
2052 check_eq(Editor_state.cursor1.pos, 4, 'baseline/cursor:pos')
2053 check_nil(Editor_state.selection1.line, 'baseline/selection:line')
2054 check_nil(Editor_state.selection1.pos, 'baseline/selection:pos')
2055 local y = Editor_state.top
2056 App.screen.check(y, 'abc', 'baseline/screen:1')
2057 y = y + Editor_state.line_height
2058 App.screen.check(y, 'def', 'baseline/screen:2')
2059 y = y + Editor_state.line_height
2060 App.screen.check(y, 'xyz', 'baseline/screen:3')
2061 -- undo
2062 --? -- after undo, the backspaced key is selected
2063 edit.run_after_keychord(Editor_state, 'C-z')
2064 check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
2065 check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
2066 check_nil(Editor_state.selection1.line, 'selection:line')
2067 check_nil(Editor_state.selection1.pos, 'selection:pos')
2068 --? check_eq(Editor_state.selection1.line, 2, 'selection:line')
2069 --? check_eq(Editor_state.selection1.pos, 4, 'selection:pos')
2070 y = Editor_state.top
2071 App.screen.check(y, 'abc', 'screen:1')
2072 y = y + Editor_state.line_height
2073 App.screen.check(y, 'defg', 'screen:2')
2074 y = y + Editor_state.line_height
2075 App.screen.check(y, 'xyz', 'screen:3')
2078 function test_undo_restores_selection()
2079 -- display a line of text with some part selected
2080 App.screen.init{width=75, height=80}
2081 Editor_state = edit.initialize_test_state()
2082 Editor_state.lines = load_array{'abc'}
2083 Text.redraw_all(Editor_state)
2084 Editor_state.cursor1 = {line=1, pos=1}
2085 Editor_state.selection1 = {line=1, pos=2}
2086 Editor_state.screen_top1 = {line=1, pos=1}
2087 Editor_state.screen_bottom1 = {}
2088 edit.draw(Editor_state)
2089 -- delete selected text
2090 edit.run_after_text_input(Editor_state, 'x')
2091 check_eq(Editor_state.lines[1].data, 'xbc', 'baseline')
2092 check_nil(Editor_state.selection1.line, 'baseline:selection')
2093 -- undo
2094 edit.run_after_keychord(Editor_state, 'C-z')
2095 edit.run_after_keychord(Editor_state, 'C-z')
2096 -- selection is restored
2097 check_eq(Editor_state.selection1.line, 1, 'line')
2098 check_eq(Editor_state.selection1.pos, 2, 'pos')
2101 function test_search()
2102 App.screen.init{width=120, height=60}
2103 Editor_state = edit.initialize_test_state()
2104 Editor_state.lines = load_array{'```lines', '```', 'def', 'ghi', '’deg'} -- contains unicode quote in final line
2105 Text.redraw_all(Editor_state)
2106 Editor_state.cursor1 = {line=1, pos=1}
2107 Editor_state.screen_top1 = {line=1, pos=1}
2108 Editor_state.screen_bottom1 = {}
2109 edit.draw(Editor_state)
2110 -- search for a string
2111 edit.run_after_keychord(Editor_state, 'C-f')
2112 edit.run_after_text_input(Editor_state, 'd')
2113 edit.run_after_keychord(Editor_state, 'return')
2114 check_eq(Editor_state.cursor1.line, 2, '1/cursor:line')
2115 check_eq(Editor_state.cursor1.pos, 1, '1/cursor:pos')
2116 -- reset cursor
2117 Editor_state.cursor1 = {line=1, pos=1}
2118 Editor_state.screen_top1 = {line=1, pos=1}
2119 -- search for second occurrence
2120 edit.run_after_keychord(Editor_state, 'C-f')
2121 edit.run_after_text_input(Editor_state, 'de')
2122 edit.run_after_keychord(Editor_state, 'down')
2123 edit.run_after_keychord(Editor_state, 'return')
2124 check_eq(Editor_state.cursor1.line, 4, '2/cursor:line')
2125 check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
2128 function test_search_upwards()
2129 App.screen.init{width=120, height=60}
2130 Editor_state = edit.initialize_test_state()
2131 Editor_state.lines = load_array{'’abc', 'abd'} -- contains unicode quote
2132 Text.redraw_all(Editor_state)
2133 Editor_state.cursor1 = {line=2, pos=1}
2134 Editor_state.screen_top1 = {line=1, pos=1}
2135 Editor_state.screen_bottom1 = {}
2136 edit.draw(Editor_state)
2137 -- search for a string
2138 edit.run_after_keychord(Editor_state, 'C-f')
2139 edit.run_after_text_input(Editor_state, 'a')
2140 -- search for previous occurrence
2141 edit.run_after_keychord(Editor_state, 'up')
2142 check_eq(Editor_state.cursor1.line, 1, '2/cursor:line')
2143 check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
2146 function test_search_wrap()
2147 App.screen.init{width=120, height=60}
2148 Editor_state = edit.initialize_test_state()
2149 Editor_state.lines = load_array{'’abc', 'def'} -- contains unicode quote in first line
2150 Text.redraw_all(Editor_state)
2151 Editor_state.cursor1 = {line=2, pos=1}
2152 Editor_state.screen_top1 = {line=1, pos=1}
2153 Editor_state.screen_bottom1 = {}
2154 edit.draw(Editor_state)
2155 -- search for a string
2156 edit.run_after_keychord(Editor_state, 'C-f')
2157 edit.run_after_text_input(Editor_state, 'a')
2158 edit.run_after_keychord(Editor_state, 'return')
2159 -- cursor wraps
2160 check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
2161 check_eq(Editor_state.cursor1.pos, 2, '1/cursor:pos')
2164 function test_search_wrap_upwards()
2165 App.screen.init{width=120, height=60}
2166 Editor_state = edit.initialize_test_state()
2167 Editor_state.lines = load_array{'abc ’abd'} -- contains unicode quote
2168 Text.redraw_all(Editor_state)
2169 Editor_state.cursor1 = {line=1, pos=1}
2170 Editor_state.screen_top1 = {line=1, pos=1}
2171 Editor_state.screen_bottom1 = {}
2172 edit.draw(Editor_state)
2173 -- search upwards for a string
2174 edit.run_after_keychord(Editor_state, 'C-f')
2175 edit.run_after_text_input(Editor_state, 'a')
2176 edit.run_after_keychord(Editor_state, 'up')
2177 -- cursor wraps
2178 check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
2179 check_eq(Editor_state.cursor1.pos, 6, '1/cursor:pos')