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