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')
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')
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')
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')
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')
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')
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')
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)
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)
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()
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)
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)
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()
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()
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()
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()
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()
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
)
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
)
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
)
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')
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
)
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
)
710 App
.clipboard
= 'xyz'
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}
737 edit
.run_after_keychord(Editor_state
, 'backspace')
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')
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()
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')
830 App
.screen
.check(y
, 'axy', 'screen:1')
831 y
= y
+ Editor_state
.line_height
832 App
.screen
.check(y
, 'zbc', 'screen:2')
833 y
= y
+ Editor_state
.line_height
834 App
.screen
.check(y
, 'def', 'screen:3')
837 function test_select_text_using_mouse()
838 App
.screen
.init
{width
=50, height
=60}
839 Editor_state
= edit
.initialize_test_state()
840 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
841 Text
.redraw_all(Editor_state
)
842 Editor_state
.cursor1
= {line
=1, pos
=1}
843 Editor_state
.screen_top1
= {line
=1, pos
=1}
844 Editor_state
.screen_bottom1
= {}
845 Editor_state
.selection1
= {}
846 edit
.draw(Editor_state
) -- populate line_cache.starty for each line Editor_state.line_cache
847 -- press and hold on first location
848 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
849 -- drag and release somewhere else
850 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+Editor_state
.line_height
+5, 1)
851 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
852 check_eq(Editor_state
.selection1
.pos
, 2, 'selection:pos')
853 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
854 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
857 function test_select_text_using_mouse_starting_above_text()
858 App
.screen
.init
{width
=50, height
=60}
859 Editor_state
= edit
.initialize_test_state()
860 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
861 Text
.redraw_all(Editor_state
)
862 Editor_state
.cursor1
= {line
=1, pos
=1}
863 Editor_state
.screen_top1
= {line
=1, pos
=1}
864 Editor_state
.screen_bottom1
= {}
865 Editor_state
.selection1
= {}
866 edit
.draw(Editor_state
) -- populate line_cache.starty for each line Editor_state.line_cache
867 -- press mouse above first line of text
868 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,5, 1)
869 check(Editor_state
.selection1
.line
~= nil, 'selection:line-not-nil')
870 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
871 check_eq(Editor_state
.selection1
.pos
, 1, 'selection:pos')
874 function test_select_text_using_mouse_starting_above_text_wrapping_line()
875 -- first screen line starts in the middle of a line
876 App
.screen
.init
{width
=50, height
=60}
877 Editor_state
= edit
.initialize_test_state()
878 Editor_state
.lines
= load_array
{'abc', 'defgh', 'xyz'}
879 Text
.redraw_all(Editor_state
)
880 Editor_state
.cursor1
= {line
=2, pos
=5}
881 Editor_state
.screen_top1
= {line
=2, pos
=3}
882 Editor_state
.screen_bottom1
= {}
883 -- press mouse above first line of text
884 edit
.draw(Editor_state
)
885 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,5, 1)
886 -- selection is at screen top
887 check(Editor_state
.selection1
.line
~= nil, 'selection:line-not-nil')
888 check_eq(Editor_state
.selection1
.line
, 2, 'selection:line')
889 check_eq(Editor_state
.selection1
.pos
, 3, 'selection:pos')
892 function test_select_text_using_mouse_starting_below_text()
893 -- I'd like to test what happens when a mouse click is below some page of
894 -- text, potentially even in the middle of a line.
895 -- However, it's brittle to set up a text line boundary just right.
896 -- So I'm going to just check things below the bottom of the final line of
897 -- text when it's in the middle of the screen.
898 -- final screen line ends in the middle of screen
899 App
.screen
.init
{width
=50, height
=60}
900 Editor_state
= edit
.initialize_test_state()
901 Editor_state
.lines
= load_array
{'abcde'}
902 Text
.redraw_all(Editor_state
)
903 Editor_state
.cursor1
= {line
=1, pos
=1}
904 Editor_state
.screen_top1
= {line
=1, pos
=1}
905 Editor_state
.screen_bottom1
= {}
906 edit
.draw(Editor_state
)
907 local y
= Editor_state
.top
908 App
.screen
.check(y
, 'ab', 'baseline:screen:1')
909 y
= y
+ Editor_state
.line_height
910 App
.screen
.check(y
, 'cde', 'baseline:screen:2')
911 -- press mouse above first line of text
912 edit
.run_after_mouse_press(Editor_state
, 5,App
.screen
.height
-5, 1)
913 -- selection is past bottom-most text in screen
914 check(Editor_state
.selection1
.line
~= nil, 'selection:line-not-nil')
915 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
916 check_eq(Editor_state
.selection1
.pos
, 6, 'selection:pos')
919 function test_select_text_using_mouse_and_shift()
920 App
.screen
.init
{width
=50, height
=60}
921 Editor_state
= edit
.initialize_test_state()
922 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
923 Text
.redraw_all(Editor_state
)
924 Editor_state
.cursor1
= {line
=1, pos
=1}
925 Editor_state
.screen_top1
= {line
=1, pos
=1}
926 Editor_state
.screen_bottom1
= {}
927 Editor_state
.selection1
= {}
928 edit
.draw(Editor_state
) -- populate line_cache.starty for each line Editor_state.line_cache
929 -- click on first location
930 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
931 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
932 -- hold down shift and click somewhere else
933 App
.fake_key_press('lshift')
934 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+5, 1)
935 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+Editor_state
.line_height
+5, 1)
936 App
.fake_key_release('lshift')
937 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
938 check_eq(Editor_state
.selection1
.pos
, 2, 'selection:pos')
939 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
940 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
943 function test_select_text_repeatedly_using_mouse_and_shift()
944 App
.screen
.init
{width
=50, height
=60}
945 Editor_state
= edit
.initialize_test_state()
946 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
947 Text
.redraw_all(Editor_state
)
948 Text
.redraw_all(Editor_state
)
949 Editor_state
.cursor1
= {line
=1, pos
=1}
950 Editor_state
.screen_top1
= {line
=1, pos
=1}
951 Editor_state
.screen_bottom1
= {}
952 Editor_state
.selection1
= {}
953 edit
.draw(Editor_state
) -- populate line_cache.starty for each line Editor_state.line_cache
954 -- click on first location
955 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
956 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+5, 1)
957 -- hold down shift and click on a second location
958 App
.fake_key_press('lshift')
959 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+5, 1)
960 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+Editor_state
.line_height
+5, 1)
961 -- hold down shift and click at a third location
962 App
.fake_key_press('lshift')
963 edit
.run_after_mouse_press(Editor_state
, Editor_state
.left
+20,Editor_state
.top
+5, 1)
964 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+8,Editor_state
.top
+Editor_state
.line_height
+5, 1)
965 App
.fake_key_release('lshift')
966 -- selection is between first and third location. forget the second location, not the first.
967 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
968 check_eq(Editor_state
.selection1
.pos
, 2, 'selection:pos')
969 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
970 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
973 function test_select_all_text()
974 -- display a single line of text
975 App
.screen
.init
{width
=75, height
=80}
976 Editor_state
= edit
.initialize_test_state()
977 Editor_state
.lines
= load_array
{'abc def'}
978 Text
.redraw_all(Editor_state
)
979 Editor_state
.cursor1
= {line
=1, pos
=1}
980 Editor_state
.screen_top1
= {line
=1, pos
=1}
981 Editor_state
.screen_bottom1
= {}
982 edit
.draw(Editor_state
)
984 App
.fake_key_press('lctrl')
985 edit
.run_after_keychord(Editor_state
, 'C-a')
986 App
.fake_key_release('lctrl')
987 edit
.key_release(Editor_state
, 'lctrl')
989 check_eq(Editor_state
.selection1
.line
, 1, 'selection:line')
990 check_eq(Editor_state
.selection1
.pos
, 1, 'selection:pos')
991 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
992 check_eq(Editor_state
.cursor1
.pos
, 8, 'cursor:pos')
995 function test_cut_without_selection()
996 -- display a few lines
997 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
998 Editor_state
= edit
.initialize_test_state()
999 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1000 Text
.redraw_all(Editor_state
)
1001 Editor_state
.cursor1
= {line
=1, pos
=2}
1002 Editor_state
.screen_top1
= {line
=1, pos
=1}
1003 Editor_state
.screen_bottom1
= {}
1004 Editor_state
.selection1
= {}
1005 edit
.draw(Editor_state
)
1006 -- try to cut without selecting text
1007 edit
.run_after_keychord(Editor_state
, 'C-x')
1009 check_nil(Editor_state
.selection1
.line
, 'check')
1012 function test_pagedown()
1013 App
.screen
.init
{width
=120, height
=45}
1014 Editor_state
= edit
.initialize_test_state()
1015 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi'}
1016 Text
.redraw_all(Editor_state
)
1017 Editor_state
.cursor1
= {line
=1, pos
=1}
1018 Editor_state
.screen_top1
= {line
=1, pos
=1}
1019 Editor_state
.screen_bottom1
= {}
1020 -- initially the first two lines are displayed
1021 edit
.draw(Editor_state
)
1022 local y
= Editor_state
.top
1023 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1024 y
= y
+ Editor_state
.line_height
1025 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1026 -- after pagedown the bottom line becomes the top
1027 edit
.run_after_keychord(Editor_state
, 'pagedown')
1028 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1029 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor')
1030 y
= Editor_state
.top
1031 App
.screen
.check(y
, 'def', 'screen:1')
1032 y
= y
+ Editor_state
.line_height
1033 App
.screen
.check(y
, 'ghi', 'screen:2')
1036 function test_pagedown_skips_drawings()
1037 -- some lines of text with a drawing intermixed
1038 local drawing_width
= 50
1039 App
.screen
.init
{width
=Editor_state
.left
+drawing_width
, height
=80}
1040 Editor_state
= edit
.initialize_test_state()
1041 Editor_state
.lines
= load_array
{'abc', -- height 15
1042 '```lines', '```', -- height 25
1045 Text
.redraw_all(Editor_state
)
1046 check_eq(Editor_state
.lines
[2].mode
, 'drawing', 'baseline/lines')
1047 Editor_state
.cursor1
= {line
=1, pos
=1}
1048 Editor_state
.screen_top1
= {line
=1, pos
=1}
1049 Editor_state
.screen_bottom1
= {}
1050 local drawing_height
= Drawing_padding_height
+ drawing_width
/2 -- default
1051 -- initially the screen displays the first line and the drawing
1052 -- 15px margin + 15px line1 + 10px margin + 25px drawing + 10px margin = 75px < screen height 80px
1053 edit
.draw(Editor_state
)
1054 local y
= Editor_state
.top
1055 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1056 -- after pagedown the screen draws the drawing up top
1057 -- 15px margin + 10px margin + 25px drawing + 10px margin + 15px line3 = 75px < screen height 80px
1058 edit
.run_after_keychord(Editor_state
, 'pagedown')
1059 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1060 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor')
1061 y
= Editor_state
.top
+ drawing_height
1062 App
.screen
.check(y
, 'def', 'screen:1')
1065 function test_pagedown_often_shows_start_of_wrapping_line()
1066 -- draw a few lines ending in part of a wrapping line
1067 App
.screen
.init
{width
=50, height
=60}
1068 Editor_state
= edit
.initialize_test_state()
1069 Editor_state
.lines
= load_array
{'abc', 'def ghi jkl', 'mno'}
1070 Text
.redraw_all(Editor_state
)
1071 Editor_state
.cursor1
= {line
=1, pos
=1}
1072 Editor_state
.screen_top1
= {line
=1, pos
=1}
1073 Editor_state
.screen_bottom1
= {}
1074 edit
.draw(Editor_state
)
1075 local y
= Editor_state
.top
1076 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1077 y
= y
+ Editor_state
.line_height
1078 App
.screen
.check(y
, 'def ', 'baseline/screen:2')
1079 y
= y
+ Editor_state
.line_height
1080 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3')
1081 -- after pagedown we start drawing from the bottom _line_ (multiple screen lines)
1082 edit
.run_after_keychord(Editor_state
, 'pagedown')
1083 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top:line')
1084 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1085 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
1086 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1087 y
= Editor_state
.top
1088 App
.screen
.check(y
, 'def ', 'screen:1')
1089 y
= y
+ Editor_state
.line_height
1090 App
.screen
.check(y
, 'ghi ', 'screen:2')
1091 y
= y
+ Editor_state
.line_height
1092 App
.screen
.check(y
, 'jkl', 'screen:3')
1095 function test_pagedown_can_start_from_middle_of_long_wrapping_line()
1096 -- draw a few lines starting from a very long wrapping line
1097 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1098 Editor_state
= edit
.initialize_test_state()
1099 Editor_state
.lines
= load_array
{'abc def ghi jkl mno pqr stu vwx yza bcd efg hij', 'XYZ'}
1100 Text
.redraw_all(Editor_state
)
1101 Editor_state
.cursor1
= {line
=1, pos
=2}
1102 Editor_state
.screen_top1
= {line
=1, pos
=1}
1103 Editor_state
.screen_bottom1
= {}
1104 edit
.draw(Editor_state
)
1105 local y
= Editor_state
.top
1106 App
.screen
.check(y
, 'abc ', 'baseline/screen:1')
1107 y
= y
+ Editor_state
.line_height
1108 App
.screen
.check(y
, 'def ', 'baseline/screen:2')
1109 y
= y
+ Editor_state
.line_height
1110 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3')
1111 -- after pagedown we scroll down the very long wrapping line
1112 edit
.run_after_keychord(Editor_state
, 'pagedown')
1113 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top:line')
1114 check_eq(Editor_state
.screen_top1
.pos
, 9, 'screen_top:pos')
1115 y
= Editor_state
.top
1116 App
.screen
.check(y
, 'ghi ', 'screen:1')
1117 y
= y
+ Editor_state
.line_height
1118 App
.screen
.check(y
, 'jkl ', 'screen:2')
1119 y
= y
+ Editor_state
.line_height
1120 if Version
== '12.0' then
1121 -- HACK: Maybe v12.0 uses a different font? Strange that it only causes
1122 -- issues in a couple of places.
1123 -- We'll need to rethink our tests if issues like this start to multiply.
1124 App
.screen
.check(y
, 'mno ', 'screen:3')
1126 App
.screen
.check(y
, 'mn', 'screen:3')
1130 function test_pagedown_never_moves_up()
1131 -- draw the final screen line of a wrapping line
1132 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1133 Editor_state
= edit
.initialize_test_state()
1134 Editor_state
.lines
= load_array
{'abc def ghi'}
1135 Text
.redraw_all(Editor_state
)
1136 Editor_state
.cursor1
= {line
=1, pos
=9}
1137 Editor_state
.screen_top1
= {line
=1, pos
=9}
1138 Editor_state
.screen_bottom1
= {}
1139 edit
.draw(Editor_state
)
1140 -- pagedown makes no change
1141 edit
.run_after_keychord(Editor_state
, 'pagedown')
1142 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top:line')
1143 check_eq(Editor_state
.screen_top1
.pos
, 9, 'screen_top:pos')
1146 function test_down_arrow_moves_cursor()
1147 App
.screen
.init
{width
=120, height
=60}
1148 Editor_state
= edit
.initialize_test_state()
1149 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1150 Text
.redraw_all(Editor_state
)
1151 Editor_state
.cursor1
= {line
=1, pos
=1}
1152 Editor_state
.screen_top1
= {line
=1, pos
=1}
1153 Editor_state
.screen_bottom1
= {}
1154 -- initially the first three lines are displayed
1155 edit
.draw(Editor_state
)
1156 local y
= Editor_state
.top
1157 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1158 y
= y
+ Editor_state
.line_height
1159 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1160 y
= y
+ Editor_state
.line_height
1161 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1162 -- after hitting the down arrow, the cursor moves down by 1 line
1163 edit
.run_after_keychord(Editor_state
, 'down')
1164 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1165 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor')
1166 -- the screen is unchanged
1167 y
= Editor_state
.top
1168 App
.screen
.check(y
, 'abc', 'screen:1')
1169 y
= y
+ Editor_state
.line_height
1170 App
.screen
.check(y
, 'def', 'screen:2')
1171 y
= y
+ Editor_state
.line_height
1172 App
.screen
.check(y
, 'ghi', 'screen:3')
1175 function test_down_arrow_skips_drawing()
1176 -- some lines of text with a drawing intermixed
1177 local drawing_width
= 50
1178 App
.screen
.init
{width
=Editor_state
.left
+drawing_width
, height
=100}
1179 Editor_state
= edit
.initialize_test_state()
1180 Editor_state
.lines
= load_array
{'abc', -- height 15
1181 '```lines', '```', -- height 25
1183 Text
.redraw_all(Editor_state
)
1184 Editor_state
.cursor1
= {line
=1, pos
=1}
1185 Editor_state
.screen_top1
= {line
=1, pos
=1}
1186 Editor_state
.screen_bottom1
= {}
1187 edit
.draw(Editor_state
)
1188 local y
= Editor_state
.top
1189 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1190 y
= y
+ Editor_state
.line_height
1191 local drawing_height
= Drawing_padding_height
+ drawing_width
/2 -- default
1192 y
= y
+ drawing_height
1193 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1194 check(Editor_state
.cursor_x
, 'baseline/cursor_x')
1195 -- after hitting the down arrow the cursor moves down by 2 lines, skipping the drawing
1196 edit
.run_after_keychord(Editor_state
, 'down')
1197 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor')
1200 function test_down_arrow_scrolls_down_by_one_line()
1201 -- display the first three lines with the cursor on the bottom line
1202 App
.screen
.init
{width
=120, height
=60}
1203 Editor_state
= edit
.initialize_test_state()
1204 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1205 Text
.redraw_all(Editor_state
)
1206 Editor_state
.cursor1
= {line
=3, pos
=1}
1207 Editor_state
.screen_top1
= {line
=1, pos
=1}
1208 Editor_state
.screen_bottom1
= {}
1209 edit
.draw(Editor_state
)
1210 local y
= Editor_state
.top
1211 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1212 y
= y
+ Editor_state
.line_height
1213 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1214 y
= y
+ Editor_state
.line_height
1215 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1216 -- after hitting the down arrow the screen scrolls down by one line
1217 edit
.run_after_keychord(Editor_state
, 'down')
1218 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1219 check_eq(Editor_state
.cursor1
.line
, 4, 'cursor')
1220 y
= Editor_state
.top
1221 App
.screen
.check(y
, 'def', 'screen:1')
1222 y
= y
+ Editor_state
.line_height
1223 App
.screen
.check(y
, 'ghi', 'screen:2')
1224 y
= y
+ Editor_state
.line_height
1225 App
.screen
.check(y
, 'jkl', 'screen:3')
1228 function test_down_arrow_scrolls_down_by_one_screen_line()
1229 -- display the first three lines with the cursor on the bottom line
1230 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1231 Editor_state
= edit
.initialize_test_state()
1232 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1233 Text
.redraw_all(Editor_state
)
1234 Editor_state
.cursor1
= {line
=3, pos
=1}
1235 Editor_state
.screen_top1
= {line
=1, pos
=1}
1236 Editor_state
.screen_bottom1
= {}
1237 edit
.draw(Editor_state
)
1238 local y
= Editor_state
.top
1239 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1240 y
= y
+ Editor_state
.line_height
1241 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1242 y
= y
+ Editor_state
.line_height
1243 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1244 -- after hitting the down arrow the screen scrolls down by one line
1245 edit
.run_after_keychord(Editor_state
, 'down')
1246 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1247 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1248 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1249 y
= Editor_state
.top
1250 App
.screen
.check(y
, 'def', 'screen:1')
1251 y
= y
+ Editor_state
.line_height
1252 App
.screen
.check(y
, 'ghi ', 'screen:2')
1253 y
= y
+ Editor_state
.line_height
1254 App
.screen
.check(y
, 'jkl', 'screen:3')
1257 function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_word()
1258 -- display the first three lines with the cursor on the bottom line
1259 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1260 Editor_state
= edit
.initialize_test_state()
1261 Editor_state
.lines
= load_array
{'abc', 'def', 'ghijkl', 'mno'}
1262 Text
.redraw_all(Editor_state
)
1263 Editor_state
.cursor1
= {line
=3, pos
=1}
1264 Editor_state
.screen_top1
= {line
=1, pos
=1}
1265 Editor_state
.screen_bottom1
= {}
1266 edit
.draw(Editor_state
)
1267 local y
= Editor_state
.top
1268 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1269 y
= y
+ Editor_state
.line_height
1270 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1271 y
= y
+ Editor_state
.line_height
1272 App
.screen
.check(y
, 'ghij', 'baseline/screen:3')
1273 -- after hitting the down arrow the screen scrolls down by one line
1274 edit
.run_after_keychord(Editor_state
, 'down')
1275 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1276 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1277 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1278 y
= Editor_state
.top
1279 App
.screen
.check(y
, 'def', 'screen:1')
1280 y
= y
+ Editor_state
.line_height
1281 App
.screen
.check(y
, 'ghij', 'screen:2')
1282 y
= y
+ Editor_state
.line_height
1283 App
.screen
.check(y
, 'kl', 'screen:3')
1286 function test_pagedown_followed_by_down_arrow_does_not_scroll_screen_up()
1287 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1288 Editor_state
= edit
.initialize_test_state()
1289 Editor_state
.lines
= load_array
{'abc', 'def', 'ghijkl', 'mno'}
1290 Text
.redraw_all(Editor_state
)
1291 Editor_state
.cursor1
= {line
=3, pos
=1}
1292 Editor_state
.screen_top1
= {line
=1, pos
=1}
1293 Editor_state
.screen_bottom1
= {}
1294 edit
.draw(Editor_state
)
1295 local y
= Editor_state
.top
1296 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1297 y
= y
+ Editor_state
.line_height
1298 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1299 y
= y
+ Editor_state
.line_height
1300 App
.screen
.check(y
, 'ghij', 'baseline/screen:3')
1301 -- after hitting pagedown the screen scrolls down to start of a long line
1302 edit
.run_after_keychord(Editor_state
, 'pagedown')
1303 check_eq(Editor_state
.screen_top1
.line
, 3, 'baseline2/screen_top')
1304 check_eq(Editor_state
.cursor1
.line
, 3, 'baseline2/cursor:line')
1305 check_eq(Editor_state
.cursor1
.pos
, 1, 'baseline2/cursor:pos')
1306 -- after hitting down arrow the screen doesn't scroll down further, and certainly doesn't scroll up
1307 edit
.run_after_keychord(Editor_state
, 'down')
1308 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top')
1309 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1310 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1311 y
= Editor_state
.top
1312 App
.screen
.check(y
, 'ghij', 'screen:1')
1313 y
= y
+ Editor_state
.line_height
1314 App
.screen
.check(y
, 'kl', 'screen:2')
1315 y
= y
+ Editor_state
.line_height
1316 App
.screen
.check(y
, 'mno', 'screen:3')
1319 function test_up_arrow_moves_cursor()
1320 -- display the first 3 lines with the cursor on the bottom line
1321 App
.screen
.init
{width
=120, height
=60}
1322 Editor_state
= edit
.initialize_test_state()
1323 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1324 Text
.redraw_all(Editor_state
)
1325 Editor_state
.cursor1
= {line
=3, pos
=1}
1326 Editor_state
.screen_top1
= {line
=1, pos
=1}
1327 Editor_state
.screen_bottom1
= {}
1328 edit
.draw(Editor_state
)
1329 local y
= Editor_state
.top
1330 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1331 y
= y
+ Editor_state
.line_height
1332 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1333 y
= y
+ Editor_state
.line_height
1334 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1335 -- after hitting the up arrow the cursor moves up by 1 line
1336 edit
.run_after_keychord(Editor_state
, 'up')
1337 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1338 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor')
1339 -- the screen is unchanged
1340 y
= Editor_state
.top
1341 App
.screen
.check(y
, 'abc', 'screen:1')
1342 y
= y
+ Editor_state
.line_height
1343 App
.screen
.check(y
, 'def', 'screen:2')
1344 y
= y
+ Editor_state
.line_height
1345 App
.screen
.check(y
, 'ghi', 'screen:3')
1348 function test_up_arrow_skips_drawing()
1349 -- some lines of text with a drawing intermixed
1350 local drawing_width
= 50
1351 App
.screen
.init
{width
=Editor_state
.left
+drawing_width
, height
=100}
1352 Editor_state
= edit
.initialize_test_state()
1353 Editor_state
.lines
= load_array
{'abc', -- height 15
1354 '```lines', '```', -- height 25
1356 Text
.redraw_all(Editor_state
)
1357 Editor_state
.cursor1
= {line
=3, pos
=1}
1358 Editor_state
.screen_top1
= {line
=1, pos
=1}
1359 Editor_state
.screen_bottom1
= {}
1360 edit
.draw(Editor_state
)
1361 local y
= Editor_state
.top
1362 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1363 y
= y
+ Editor_state
.line_height
1364 local drawing_height
= Drawing_padding_height
+ drawing_width
/2 -- default
1365 y
= y
+ drawing_height
1366 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1367 check(Editor_state
.cursor_x
, 'baseline/cursor_x')
1368 -- after hitting the up arrow the cursor moves up by 2 lines, skipping the drawing
1369 edit
.run_after_keychord(Editor_state
, 'up')
1370 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1373 function test_up_arrow_scrolls_up_by_one_line()
1374 -- display the lines 2/3/4 with the cursor on line 2
1375 App
.screen
.init
{width
=120, height
=60}
1376 Editor_state
= edit
.initialize_test_state()
1377 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1378 Text
.redraw_all(Editor_state
)
1379 Editor_state
.cursor1
= {line
=2, pos
=1}
1380 Editor_state
.screen_top1
= {line
=2, pos
=1}
1381 Editor_state
.screen_bottom1
= {}
1382 edit
.draw(Editor_state
)
1383 local y
= Editor_state
.top
1384 App
.screen
.check(y
, 'def', 'baseline/screen:1')
1385 y
= y
+ Editor_state
.line_height
1386 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
1387 y
= y
+ Editor_state
.line_height
1388 App
.screen
.check(y
, 'jkl', 'baseline/screen:3')
1389 -- after hitting the up arrow the screen scrolls up by one line
1390 edit
.run_after_keychord(Editor_state
, 'up')
1391 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1392 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1393 y
= Editor_state
.top
1394 App
.screen
.check(y
, 'abc', 'screen:1')
1395 y
= y
+ Editor_state
.line_height
1396 App
.screen
.check(y
, 'def', 'screen:2')
1397 y
= y
+ Editor_state
.line_height
1398 App
.screen
.check(y
, 'ghi', 'screen:3')
1401 function test_up_arrow_scrolls_up_by_one_line_skipping_drawing()
1402 -- display lines 3/4/5 with a drawing just off screen at line 2
1403 App
.screen
.init
{width
=120, height
=60}
1404 Editor_state
= edit
.initialize_test_state()
1405 Editor_state
.lines
= load_array
{'abc', '```lines', '```', 'def', 'ghi', 'jkl'}
1406 Text
.redraw_all(Editor_state
)
1407 Editor_state
.cursor1
= {line
=3, pos
=1}
1408 Editor_state
.screen_top1
= {line
=3, pos
=1}
1409 Editor_state
.screen_bottom1
= {}
1410 edit
.draw(Editor_state
)
1411 local y
= Editor_state
.top
1412 App
.screen
.check(y
, 'def', 'baseline/screen:1')
1413 y
= y
+ Editor_state
.line_height
1414 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
1415 y
= y
+ Editor_state
.line_height
1416 App
.screen
.check(y
, 'jkl', 'baseline/screen:3')
1417 -- after hitting the up arrow the screen scrolls up to previous text line
1418 edit
.run_after_keychord(Editor_state
, 'up')
1419 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1420 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1423 function test_up_arrow_scrolls_up_by_one_screen_line()
1424 -- display lines starting from second screen line of a line
1425 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1426 Editor_state
= edit
.initialize_test_state()
1427 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1428 Text
.redraw_all(Editor_state
)
1429 Editor_state
.cursor1
= {line
=3, pos
=6}
1430 Editor_state
.screen_top1
= {line
=3, pos
=5}
1431 Editor_state
.screen_bottom1
= {}
1432 edit
.draw(Editor_state
)
1433 local y
= Editor_state
.top
1434 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1435 y
= y
+ Editor_state
.line_height
1436 App
.screen
.check(y
, 'mno', 'baseline/screen:2')
1437 -- after hitting the up arrow the screen scrolls up to first screen line
1438 edit
.run_after_keychord(Editor_state
, 'up')
1439 y
= Editor_state
.top
1440 App
.screen
.check(y
, 'ghi ', 'screen:1')
1441 y
= y
+ Editor_state
.line_height
1442 App
.screen
.check(y
, 'jkl', 'screen:2')
1443 y
= y
+ Editor_state
.line_height
1444 App
.screen
.check(y
, 'mno', 'screen:3')
1445 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top:line')
1446 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1447 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1448 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1451 function test_up_arrow_scrolls_up_to_final_screen_line()
1452 -- display lines starting just after a long line
1453 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1454 Editor_state
= edit
.initialize_test_state()
1455 Editor_state
.lines
= load_array
{'abc def', 'ghi', 'jkl', 'mno'}
1456 Text
.redraw_all(Editor_state
)
1457 Editor_state
.cursor1
= {line
=2, pos
=1}
1458 Editor_state
.screen_top1
= {line
=2, pos
=1}
1459 Editor_state
.screen_bottom1
= {}
1460 edit
.draw(Editor_state
)
1461 local y
= Editor_state
.top
1462 App
.screen
.check(y
, 'ghi', 'baseline/screen:1')
1463 y
= y
+ Editor_state
.line_height
1464 App
.screen
.check(y
, 'jkl', 'baseline/screen:2')
1465 y
= y
+ Editor_state
.line_height
1466 App
.screen
.check(y
, 'mno', 'baseline/screen:3')
1467 -- after hitting the up arrow the screen scrolls up to final screen line of previous line
1468 edit
.run_after_keychord(Editor_state
, 'up')
1469 y
= Editor_state
.top
1470 App
.screen
.check(y
, 'def', 'screen:1')
1471 y
= y
+ Editor_state
.line_height
1472 App
.screen
.check(y
, 'ghi', 'screen:2')
1473 y
= y
+ Editor_state
.line_height
1474 App
.screen
.check(y
, 'jkl', 'screen:3')
1475 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top:line')
1476 check_eq(Editor_state
.screen_top1
.pos
, 5, 'screen_top:pos')
1477 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1478 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
1481 function test_up_arrow_scrolls_up_to_empty_line()
1482 -- display a screenful of text with an empty line just above it outside the screen
1483 App
.screen
.init
{width
=120, height
=60}
1484 Editor_state
= edit
.initialize_test_state()
1485 Editor_state
.lines
= load_array
{'', 'abc', 'def', 'ghi', 'jkl'}
1486 Text
.redraw_all(Editor_state
)
1487 Editor_state
.cursor1
= {line
=2, pos
=1}
1488 Editor_state
.screen_top1
= {line
=2, pos
=1}
1489 Editor_state
.screen_bottom1
= {}
1490 edit
.draw(Editor_state
)
1491 local y
= Editor_state
.top
1492 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1493 y
= y
+ Editor_state
.line_height
1494 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1495 y
= y
+ Editor_state
.line_height
1496 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1497 -- after hitting the up arrow the screen scrolls up by one line
1498 edit
.run_after_keychord(Editor_state
, 'up')
1499 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1500 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1501 y
= Editor_state
.top
1503 y
= y
+ Editor_state
.line_height
1504 App
.screen
.check(y
, 'abc', 'screen:2')
1505 y
= y
+ Editor_state
.line_height
1506 App
.screen
.check(y
, 'def', 'screen:3')
1509 function test_pageup()
1510 App
.screen
.init
{width
=120, height
=45}
1511 Editor_state
= edit
.initialize_test_state()
1512 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi'}
1513 Text
.redraw_all(Editor_state
)
1514 Editor_state
.cursor1
= {line
=2, pos
=1}
1515 Editor_state
.screen_top1
= {line
=2, pos
=1}
1516 Editor_state
.screen_bottom1
= {}
1517 -- initially the last two lines are displayed
1518 edit
.draw(Editor_state
)
1519 local y
= Editor_state
.top
1520 App
.screen
.check(y
, 'def', 'baseline/screen:1')
1521 y
= y
+ Editor_state
.line_height
1522 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
1523 -- after pageup the cursor goes to first line
1524 edit
.run_after_keychord(Editor_state
, 'pageup')
1525 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1526 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1527 y
= Editor_state
.top
1528 App
.screen
.check(y
, 'abc', 'screen:1')
1529 y
= y
+ Editor_state
.line_height
1530 App
.screen
.check(y
, 'def', 'screen:2')
1533 function test_pageup_scrolls_up_by_screen_line()
1534 -- display the first three lines with the cursor on the bottom line
1535 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1536 Editor_state
= edit
.initialize_test_state()
1537 Editor_state
.lines
= load_array
{'abc def', 'ghi', 'jkl', 'mno'}
1538 Text
.redraw_all(Editor_state
)
1539 Editor_state
.cursor1
= {line
=2, pos
=1}
1540 Editor_state
.screen_top1
= {line
=2, pos
=1}
1541 Editor_state
.screen_bottom1
= {}
1542 edit
.draw(Editor_state
)
1543 local y
= Editor_state
.top
1544 App
.screen
.check(y
, 'ghi', 'baseline/screen:1')
1545 y
= y
+ Editor_state
.line_height
1546 App
.screen
.check(y
, 'jkl', 'baseline/screen:2')
1547 y
= y
+ Editor_state
.line_height
1548 App
.screen
.check(y
, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1549 -- after hitting the page-up key the screen scrolls up to top
1550 edit
.run_after_keychord(Editor_state
, 'pageup')
1551 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1552 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1553 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1554 y
= Editor_state
.top
1555 App
.screen
.check(y
, 'abc ', 'screen:1')
1556 y
= y
+ Editor_state
.line_height
1557 App
.screen
.check(y
, 'def', 'screen:2')
1558 y
= y
+ Editor_state
.line_height
1559 App
.screen
.check(y
, 'ghi', 'screen:3')
1562 function test_pageup_scrolls_up_from_middle_screen_line()
1563 -- display a few lines starting from the middle of a line (Editor_state.cursor1.pos > 1)
1564 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1565 Editor_state
= edit
.initialize_test_state()
1566 Editor_state
.lines
= load_array
{'abc def', 'ghi jkl', 'mno'}
1567 Text
.redraw_all(Editor_state
)
1568 Editor_state
.cursor1
= {line
=2, pos
=5}
1569 Editor_state
.screen_top1
= {line
=2, pos
=5}
1570 Editor_state
.screen_bottom1
= {}
1571 edit
.draw(Editor_state
)
1572 local y
= Editor_state
.top
1573 App
.screen
.check(y
, 'jkl', 'baseline/screen:2')
1574 y
= y
+ Editor_state
.line_height
1575 App
.screen
.check(y
, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1576 -- after hitting the page-up key the screen scrolls up to top
1577 edit
.run_after_keychord(Editor_state
, 'pageup')
1578 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1579 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1580 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1581 y
= Editor_state
.top
1582 App
.screen
.check(y
, 'abc ', 'screen:1')
1583 y
= y
+ Editor_state
.line_height
1584 App
.screen
.check(y
, 'def', 'screen:2')
1585 y
= y
+ Editor_state
.line_height
1586 App
.screen
.check(y
, 'ghi ', 'screen:3')
1589 function test_enter_on_bottom_line_scrolls_down()
1590 -- display a few lines with cursor on bottom line
1591 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1592 Editor_state
= edit
.initialize_test_state()
1593 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1594 Text
.redraw_all(Editor_state
)
1595 Editor_state
.cursor1
= {line
=3, pos
=2}
1596 Editor_state
.screen_top1
= {line
=1, pos
=1}
1597 Editor_state
.screen_bottom1
= {}
1598 edit
.draw(Editor_state
)
1599 local y
= Editor_state
.top
1600 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1601 y
= y
+ Editor_state
.line_height
1602 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1603 y
= y
+ Editor_state
.line_height
1604 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1605 -- after hitting the enter key the screen scrolls down
1606 edit
.run_after_keychord(Editor_state
, 'return')
1607 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1608 check_eq(Editor_state
.cursor1
.line
, 4, 'cursor:line')
1609 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1610 y
= Editor_state
.top
1611 App
.screen
.check(y
, 'def', 'screen:1')
1612 y
= y
+ Editor_state
.line_height
1613 App
.screen
.check(y
, 'g', 'screen:2')
1614 y
= y
+ Editor_state
.line_height
1615 App
.screen
.check(y
, 'hi', 'screen:3')
1618 function test_enter_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1619 -- display just the bottom line on screen
1620 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1621 Editor_state
= edit
.initialize_test_state()
1622 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1623 Text
.redraw_all(Editor_state
)
1624 Editor_state
.cursor1
= {line
=4, pos
=2}
1625 Editor_state
.screen_top1
= {line
=4, pos
=1}
1626 Editor_state
.screen_bottom1
= {}
1627 edit
.draw(Editor_state
)
1628 local y
= Editor_state
.top
1629 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1630 -- after hitting the enter key the screen does not scroll down
1631 edit
.run_after_keychord(Editor_state
, 'return')
1632 check_eq(Editor_state
.screen_top1
.line
, 4, 'screen_top')
1633 check_eq(Editor_state
.cursor1
.line
, 5, 'cursor:line')
1634 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1635 y
= Editor_state
.top
1636 App
.screen
.check(y
, 'j', 'screen:1')
1637 y
= y
+ Editor_state
.line_height
1638 App
.screen
.check(y
, 'kl', 'screen:2')
1641 function test_inserting_text_on_final_line_avoids_scrolling_down_when_not_at_bottom()
1642 -- display just an empty bottom line on screen
1643 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1644 Editor_state
= edit
.initialize_test_state()
1645 Editor_state
.lines
= load_array
{'abc', ''}
1646 Text
.redraw_all(Editor_state
)
1647 Editor_state
.cursor1
= {line
=2, pos
=1}
1648 Editor_state
.screen_top1
= {line
=2, pos
=1}
1649 Editor_state
.screen_bottom1
= {}
1650 edit
.draw(Editor_state
)
1651 -- after hitting the inserting_text key the screen does not scroll down
1652 edit
.run_after_text_input(Editor_state
, 'a')
1653 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1654 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
1655 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
1656 local y
= Editor_state
.top
1657 App
.screen
.check(y
, 'a', 'screen:1')
1660 function test_typing_on_bottom_line_scrolls_down()
1661 -- display a few lines with cursor on bottom line
1662 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1663 Editor_state
= edit
.initialize_test_state()
1664 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1665 Text
.redraw_all(Editor_state
)
1666 Editor_state
.cursor1
= {line
=3, pos
=4}
1667 Editor_state
.screen_top1
= {line
=1, pos
=1}
1668 Editor_state
.screen_bottom1
= {}
1669 edit
.draw(Editor_state
)
1670 local y
= Editor_state
.top
1671 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1672 y
= y
+ Editor_state
.line_height
1673 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1674 y
= y
+ Editor_state
.line_height
1675 App
.screen
.check(y
, 'ghi', 'baseline/screen:3')
1676 -- after typing something the line wraps and the screen scrolls down
1677 edit
.run_after_text_input(Editor_state
, 'j')
1678 edit
.run_after_text_input(Editor_state
, 'k')
1679 edit
.run_after_text_input(Editor_state
, 'l')
1680 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1681 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1682 check_eq(Editor_state
.cursor1
.pos
, 7, 'cursor:pos')
1683 y
= Editor_state
.top
1684 App
.screen
.check(y
, 'def', 'screen:1')
1685 y
= y
+ Editor_state
.line_height
1686 App
.screen
.check(y
, 'ghij', 'screen:2')
1687 y
= y
+ Editor_state
.line_height
1688 App
.screen
.check(y
, 'kl', 'screen:3')
1691 function test_left_arrow_scrolls_up_in_wrapped_line()
1692 -- display lines starting from second screen line of a line
1693 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1694 Editor_state
= edit
.initialize_test_state()
1695 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1696 Text
.redraw_all(Editor_state
)
1697 Editor_state
.screen_top1
= {line
=3, pos
=5}
1698 Editor_state
.screen_bottom1
= {}
1699 -- cursor is at top of screen
1700 Editor_state
.cursor1
= {line
=3, pos
=5}
1701 edit
.draw(Editor_state
)
1702 local y
= Editor_state
.top
1703 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1704 y
= y
+ Editor_state
.line_height
1705 App
.screen
.check(y
, 'mno', 'baseline/screen:2')
1706 -- after hitting the left arrow the screen scrolls up to first screen line
1707 edit
.run_after_keychord(Editor_state
, 'left')
1708 y
= Editor_state
.top
1709 App
.screen
.check(y
, 'ghi ', 'screen:1')
1710 y
= y
+ Editor_state
.line_height
1711 App
.screen
.check(y
, 'jkl', 'screen:2')
1712 y
= y
+ Editor_state
.line_height
1713 App
.screen
.check(y
, 'mno', 'screen:3')
1714 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top:line')
1715 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1716 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1717 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
1720 function test_right_arrow_scrolls_down_in_wrapped_line()
1721 -- display the first three lines with the cursor on the bottom line
1722 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1723 Editor_state
= edit
.initialize_test_state()
1724 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1725 Text
.redraw_all(Editor_state
)
1726 Editor_state
.screen_top1
= {line
=1, pos
=1}
1727 Editor_state
.screen_bottom1
= {}
1728 -- cursor is at bottom right of screen
1729 Editor_state
.cursor1
= {line
=3, pos
=5}
1730 edit
.draw(Editor_state
)
1731 local y
= Editor_state
.top
1732 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1733 y
= y
+ Editor_state
.line_height
1734 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1735 y
= y
+ Editor_state
.line_height
1736 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1737 -- after hitting the right arrow the screen scrolls down by one line
1738 edit
.run_after_keychord(Editor_state
, 'right')
1739 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1740 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1741 check_eq(Editor_state
.cursor1
.pos
, 6, 'cursor:pos')
1742 y
= Editor_state
.top
1743 App
.screen
.check(y
, 'def', 'screen:1')
1744 y
= y
+ Editor_state
.line_height
1745 App
.screen
.check(y
, 'ghi ', 'screen:2')
1746 y
= y
+ Editor_state
.line_height
1747 App
.screen
.check(y
, 'jkl', 'screen:3')
1750 function test_home_scrolls_up_in_wrapped_line()
1751 -- display lines starting from second screen line of a line
1752 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1753 Editor_state
= edit
.initialize_test_state()
1754 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1755 Text
.redraw_all(Editor_state
)
1756 Editor_state
.screen_top1
= {line
=3, pos
=5}
1757 Editor_state
.screen_bottom1
= {}
1758 -- cursor is at top of screen
1759 Editor_state
.cursor1
= {line
=3, pos
=5}
1760 edit
.draw(Editor_state
)
1761 local y
= Editor_state
.top
1762 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1763 y
= y
+ Editor_state
.line_height
1764 App
.screen
.check(y
, 'mno', 'baseline/screen:2')
1765 -- after hitting home the screen scrolls up to first screen line
1766 edit
.run_after_keychord(Editor_state
, 'home')
1767 y
= Editor_state
.top
1768 App
.screen
.check(y
, 'ghi ', 'screen:1')
1769 y
= y
+ Editor_state
.line_height
1770 App
.screen
.check(y
, 'jkl', 'screen:2')
1771 y
= y
+ Editor_state
.line_height
1772 App
.screen
.check(y
, 'mno', 'screen:3')
1773 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top:line')
1774 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1775 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1776 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1779 function test_end_scrolls_down_in_wrapped_line()
1780 -- display the first three lines with the cursor on the bottom line
1781 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1782 Editor_state
= edit
.initialize_test_state()
1783 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1784 Text
.redraw_all(Editor_state
)
1785 Editor_state
.screen_top1
= {line
=1, pos
=1}
1786 Editor_state
.screen_bottom1
= {}
1787 -- cursor is at bottom right of screen
1788 Editor_state
.cursor1
= {line
=3, pos
=5}
1789 edit
.draw(Editor_state
)
1790 local y
= Editor_state
.top
1791 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
1792 y
= y
+ Editor_state
.line_height
1793 App
.screen
.check(y
, 'def', 'baseline/screen:2')
1794 y
= y
+ Editor_state
.line_height
1795 App
.screen
.check(y
, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
1796 -- after hitting end the screen scrolls down by one line
1797 edit
.run_after_keychord(Editor_state
, 'end')
1798 check_eq(Editor_state
.screen_top1
.line
, 2, 'screen_top')
1799 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1800 check_eq(Editor_state
.cursor1
.pos
, 8, 'cursor:pos')
1801 y
= Editor_state
.top
1802 App
.screen
.check(y
, 'def', 'screen:1')
1803 y
= y
+ Editor_state
.line_height
1804 App
.screen
.check(y
, 'ghi ', 'screen:2')
1805 y
= y
+ Editor_state
.line_height
1806 App
.screen
.check(y
, 'jkl', 'screen:3')
1809 function test_position_cursor_on_recently_edited_wrapping_line()
1810 -- draw a line wrapping over 2 screen lines
1811 App
.screen
.init
{width
=100, height
=200}
1812 Editor_state
= edit
.initialize_test_state()
1813 Editor_state
.lines
= load_array
{'abc def ghi jkl mno pqr ', 'xyz'}
1814 Text
.redraw_all(Editor_state
)
1815 Editor_state
.cursor1
= {line
=1, pos
=25}
1816 Editor_state
.screen_top1
= {line
=1, pos
=1}
1817 Editor_state
.screen_bottom1
= {}
1818 edit
.draw(Editor_state
)
1819 local y
= Editor_state
.top
1820 App
.screen
.check(y
, 'abc def ghi ', 'baseline1/screen:1')
1821 y
= y
+ Editor_state
.line_height
1822 App
.screen
.check(y
, 'jkl mno pqr ', 'baseline1/screen:2')
1823 y
= y
+ Editor_state
.line_height
1824 App
.screen
.check(y
, 'xyz', 'baseline1/screen:3')
1825 -- add to the line until it's wrapping over 3 screen lines
1826 edit
.run_after_text_input(Editor_state
, 's')
1827 edit
.run_after_text_input(Editor_state
, 't')
1828 edit
.run_after_text_input(Editor_state
, 'u')
1829 check_eq(Editor_state
.cursor1
.pos
, 28, 'cursor:pos')
1830 y
= Editor_state
.top
1831 App
.screen
.check(y
, 'abc def ghi ', 'baseline2/screen:1')
1832 y
= y
+ Editor_state
.line_height
1833 App
.screen
.check(y
, 'jkl mno pqr ', 'baseline2/screen:2')
1834 y
= y
+ Editor_state
.line_height
1835 App
.screen
.check(y
, 'stu', 'baseline2/screen:3')
1836 -- try to move the cursor earlier in the third screen line by clicking the mouse
1837 edit
.run_after_mouse_release(Editor_state
, Editor_state
.left
+2,Editor_state
.top
+Editor_state
.line_height
*2+5, 1)
1838 -- cursor should move
1839 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1840 check_eq(Editor_state
.cursor1
.pos
, 25, 'cursor:pos')
1843 function test_backspace_can_scroll_up()
1844 -- display the lines 2/3/4 with the cursor on line 2
1845 App
.screen
.init
{width
=120, height
=60}
1846 Editor_state
= edit
.initialize_test_state()
1847 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl'}
1848 Text
.redraw_all(Editor_state
)
1849 Editor_state
.cursor1
= {line
=2, pos
=1}
1850 Editor_state
.screen_top1
= {line
=2, pos
=1}
1851 Editor_state
.screen_bottom1
= {}
1852 edit
.draw(Editor_state
)
1853 local y
= Editor_state
.top
1854 App
.screen
.check(y
, 'def', 'baseline/screen:1')
1855 y
= y
+ Editor_state
.line_height
1856 App
.screen
.check(y
, 'ghi', 'baseline/screen:2')
1857 y
= y
+ Editor_state
.line_height
1858 App
.screen
.check(y
, 'jkl', 'baseline/screen:3')
1859 -- after hitting backspace the screen scrolls up by one line
1860 edit
.run_after_keychord(Editor_state
, 'backspace')
1861 check_eq(Editor_state
.screen_top1
.line
, 1, 'screen_top')
1862 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor')
1863 y
= Editor_state
.top
1864 App
.screen
.check(y
, 'abcdef', 'screen:1')
1865 y
= y
+ Editor_state
.line_height
1866 App
.screen
.check(y
, 'ghi', 'screen:2')
1867 y
= y
+ Editor_state
.line_height
1868 App
.screen
.check(y
, 'jkl', 'screen:3')
1871 function test_backspace_can_scroll_up_screen_line()
1872 -- display lines starting from second screen line of a line
1873 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1874 Editor_state
= edit
.initialize_test_state()
1875 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi jkl', 'mno'}
1876 Text
.redraw_all(Editor_state
)
1877 Editor_state
.cursor1
= {line
=3, pos
=5}
1878 Editor_state
.screen_top1
= {line
=3, pos
=5}
1879 Editor_state
.screen_bottom1
= {}
1880 edit
.draw(Editor_state
)
1881 local y
= Editor_state
.top
1882 App
.screen
.check(y
, 'jkl', 'baseline/screen:1')
1883 y
= y
+ Editor_state
.line_height
1884 App
.screen
.check(y
, 'mno', 'baseline/screen:2')
1885 -- after hitting backspace the screen scrolls up by one screen line
1886 edit
.run_after_keychord(Editor_state
, 'backspace')
1887 y
= Editor_state
.top
1888 App
.screen
.check(y
, 'ghij', 'screen:1')
1889 y
= y
+ Editor_state
.line_height
1890 App
.screen
.check(y
, 'kl', 'screen:2')
1891 y
= y
+ Editor_state
.line_height
1892 App
.screen
.check(y
, 'mno', 'screen:3')
1893 check_eq(Editor_state
.screen_top1
.line
, 3, 'screen_top:line')
1894 check_eq(Editor_state
.screen_top1
.pos
, 1, 'screen_top:pos')
1895 check_eq(Editor_state
.cursor1
.line
, 3, 'cursor:line')
1896 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
1899 function test_backspace_past_line_boundary()
1900 -- position cursor at start of a (non-first) line
1901 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1902 Editor_state
= edit
.initialize_test_state()
1903 Editor_state
.lines
= load_array
{'abc', 'def'}
1904 Text
.redraw_all(Editor_state
)
1905 Editor_state
.cursor1
= {line
=2, pos
=1}
1906 -- backspace joins with previous line
1907 edit
.run_after_keychord(Editor_state
, 'backspace')
1908 check_eq(Editor_state
.lines
[1].data
, 'abcdef', 'check')
1911 -- some tests for operating over selections created using Shift- chords
1912 -- we're just testing delete_selection, and it works the same for all keys
1914 function test_backspace_over_selection()
1915 -- select just one character within a line with cursor before selection
1916 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1917 Editor_state
= edit
.initialize_test_state()
1918 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1919 Text
.redraw_all(Editor_state
)
1920 Editor_state
.cursor1
= {line
=1, pos
=1}
1921 Editor_state
.selection1
= {line
=1, pos
=2}
1922 -- backspace deletes the selected character, even though it's after the cursor
1923 edit
.run_after_keychord(Editor_state
, 'backspace')
1924 check_eq(Editor_state
.lines
[1].data
, 'bc', 'data')
1925 -- cursor (remains) at start of selection
1926 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1927 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1928 -- selection is cleared
1929 check_nil(Editor_state
.selection1
.line
, 'selection')
1932 function test_backspace_over_selection_reverse()
1933 -- select just one character within a line with cursor after selection
1934 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1935 Editor_state
= edit
.initialize_test_state()
1936 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1937 Text
.redraw_all(Editor_state
)
1938 Editor_state
.cursor1
= {line
=1, pos
=2}
1939 Editor_state
.selection1
= {line
=1, pos
=1}
1940 -- backspace deletes the selected character
1941 edit
.run_after_keychord(Editor_state
, 'backspace')
1942 check_eq(Editor_state
.lines
[1].data
, 'bc', 'data')
1943 -- cursor moves to start of selection
1944 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1945 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
1946 -- selection is cleared
1947 check_nil(Editor_state
.selection1
.line
, 'selection')
1950 function test_backspace_over_multiple_lines()
1951 -- select just one character within a line with cursor after selection
1952 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1953 Editor_state
= edit
.initialize_test_state()
1954 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1955 Text
.redraw_all(Editor_state
)
1956 Editor_state
.cursor1
= {line
=1, pos
=2}
1957 Editor_state
.selection1
= {line
=4, pos
=2}
1958 -- backspace deletes the region and joins the remaining portions of lines on either side
1959 edit
.run_after_keychord(Editor_state
, 'backspace')
1960 check_eq(Editor_state
.lines
[1].data
, 'akl', 'data:1')
1961 check_eq(Editor_state
.lines
[2].data
, 'mno', 'data:2')
1962 -- cursor remains at start of selection
1963 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1964 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
1965 -- selection is cleared
1966 check_nil(Editor_state
.selection1
.line
, 'selection')
1969 function test_backspace_to_end_of_line()
1970 -- select region from cursor to end of line
1971 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1972 Editor_state
= edit
.initialize_test_state()
1973 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1974 Text
.redraw_all(Editor_state
)
1975 Editor_state
.cursor1
= {line
=1, pos
=2}
1976 Editor_state
.selection1
= {line
=1, pos
=4}
1977 -- backspace deletes rest of line without joining to any other line
1978 edit
.run_after_keychord(Editor_state
, 'backspace')
1979 check_eq(Editor_state
.lines
[1].data
, 'a', 'data:1')
1980 check_eq(Editor_state
.lines
[2].data
, 'def', 'data:2')
1981 -- cursor remains at start of selection
1982 check_eq(Editor_state
.cursor1
.line
, 1, 'cursor:line')
1983 check_eq(Editor_state
.cursor1
.pos
, 2, 'cursor:pos')
1984 -- selection is cleared
1985 check_nil(Editor_state
.selection1
.line
, 'selection')
1988 function test_backspace_to_start_of_line()
1989 -- select region from cursor to start of line
1990 App
.screen
.init
{width
=Editor_state
.left
+30, height
=60}
1991 Editor_state
= edit
.initialize_test_state()
1992 Editor_state
.lines
= load_array
{'abc', 'def', 'ghi', 'jkl', 'mno'}
1993 Text
.redraw_all(Editor_state
)
1994 Editor_state
.cursor1
= {line
=2, pos
=1}
1995 Editor_state
.selection1
= {line
=2, pos
=3}
1996 -- backspace deletes beginning of line without joining to any other line
1997 edit
.run_after_keychord(Editor_state
, 'backspace')
1998 check_eq(Editor_state
.lines
[1].data
, 'abc', 'data:1')
1999 check_eq(Editor_state
.lines
[2].data
, 'f', 'data:2')
2000 -- cursor remains at start of selection
2001 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
2002 check_eq(Editor_state
.cursor1
.pos
, 1, 'cursor:pos')
2003 -- selection is cleared
2004 check_nil(Editor_state
.selection1
.line
, 'selection')
2007 function test_undo_insert_text()
2008 App
.screen
.init
{width
=120, height
=60}
2009 Editor_state
= edit
.initialize_test_state()
2010 Editor_state
.lines
= load_array
{'abc', 'def', 'xyz'}
2011 Text
.redraw_all(Editor_state
)
2012 Editor_state
.cursor1
= {line
=2, pos
=4}
2013 Editor_state
.screen_top1
= {line
=1, pos
=1}
2014 Editor_state
.screen_bottom1
= {}
2015 -- insert a character
2016 edit
.draw(Editor_state
)
2017 edit
.run_after_text_input(Editor_state
, 'g')
2018 check_eq(Editor_state
.cursor1
.line
, 2, 'baseline/cursor:line')
2019 check_eq(Editor_state
.cursor1
.pos
, 5, 'baseline/cursor:pos')
2020 check_nil(Editor_state
.selection1
.line
, 'baseline/selection:line')
2021 check_nil(Editor_state
.selection1
.pos
, 'baseline/selection:pos')
2022 local y
= Editor_state
.top
2023 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
2024 y
= y
+ Editor_state
.line_height
2025 App
.screen
.check(y
, 'defg', 'baseline/screen:2')
2026 y
= y
+ Editor_state
.line_height
2027 App
.screen
.check(y
, 'xyz', 'baseline/screen:3')
2029 edit
.run_after_keychord(Editor_state
, 'C-z')
2030 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
2031 check_eq(Editor_state
.cursor1
.pos
, 4, 'cursor:pos')
2032 check_nil(Editor_state
.selection1
.line
, 'selection:line')
2033 check_nil(Editor_state
.selection1
.pos
, 'selection:pos')
2034 y
= Editor_state
.top
2035 App
.screen
.check(y
, 'abc', 'screen:1')
2036 y
= y
+ Editor_state
.line_height
2037 App
.screen
.check(y
, 'def', 'screen:2')
2038 y
= y
+ Editor_state
.line_height
2039 App
.screen
.check(y
, 'xyz', 'screen:3')
2042 function test_undo_delete_text()
2043 App
.screen
.init
{width
=120, height
=60}
2044 Editor_state
= edit
.initialize_test_state()
2045 Editor_state
.lines
= load_array
{'abc', 'defg', 'xyz'}
2046 Text
.redraw_all(Editor_state
)
2047 Editor_state
.cursor1
= {line
=2, pos
=5}
2048 Editor_state
.screen_top1
= {line
=1, pos
=1}
2049 Editor_state
.screen_bottom1
= {}
2050 -- delete a character
2051 edit
.run_after_keychord(Editor_state
, 'backspace')
2052 check_eq(Editor_state
.cursor1
.line
, 2, 'baseline/cursor:line')
2053 check_eq(Editor_state
.cursor1
.pos
, 4, 'baseline/cursor:pos')
2054 check_nil(Editor_state
.selection1
.line
, 'baseline/selection:line')
2055 check_nil(Editor_state
.selection1
.pos
, 'baseline/selection:pos')
2056 local y
= Editor_state
.top
2057 App
.screen
.check(y
, 'abc', 'baseline/screen:1')
2058 y
= y
+ Editor_state
.line_height
2059 App
.screen
.check(y
, 'def', 'baseline/screen:2')
2060 y
= y
+ Editor_state
.line_height
2061 App
.screen
.check(y
, 'xyz', 'baseline/screen:3')
2063 --? -- after undo, the backspaced key is selected
2064 edit
.run_after_keychord(Editor_state
, 'C-z')
2065 check_eq(Editor_state
.cursor1
.line
, 2, 'cursor:line')
2066 check_eq(Editor_state
.cursor1
.pos
, 5, 'cursor:pos')
2067 check_nil(Editor_state
.selection1
.line
, 'selection:line')
2068 check_nil(Editor_state
.selection1
.pos
, 'selection:pos')
2069 --? check_eq(Editor_state.selection1.line, 2, 'selection:line')
2070 --? check_eq(Editor_state.selection1.pos, 4, 'selection:pos')
2071 y
= Editor_state
.top
2072 App
.screen
.check(y
, 'abc', 'screen:1')
2073 y
= y
+ Editor_state
.line_height
2074 App
.screen
.check(y
, 'defg', 'screen:2')
2075 y
= y
+ Editor_state
.line_height
2076 App
.screen
.check(y
, 'xyz', 'screen:3')
2079 function test_undo_restores_selection()
2080 -- display a line of text with some part selected
2081 App
.screen
.init
{width
=75, height
=80}
2082 Editor_state
= edit
.initialize_test_state()
2083 Editor_state
.lines
= load_array
{'abc'}
2084 Text
.redraw_all(Editor_state
)
2085 Editor_state
.cursor1
= {line
=1, pos
=1}
2086 Editor_state
.selection1
= {line
=1, pos
=2}
2087 Editor_state
.screen_top1
= {line
=1, pos
=1}
2088 Editor_state
.screen_bottom1
= {}
2089 edit
.draw(Editor_state
)
2090 -- delete selected text
2091 edit
.run_after_text_input(Editor_state
, 'x')
2092 check_eq(Editor_state
.lines
[1].data
, 'xbc', 'baseline')
2093 check_nil(Editor_state
.selection1
.line
, 'baseline:selection')
2095 edit
.run_after_keychord(Editor_state
, 'C-z')
2096 edit
.run_after_keychord(Editor_state
, 'C-z')
2097 -- selection is restored
2098 check_eq(Editor_state
.selection1
.line
, 1, 'line')
2099 check_eq(Editor_state
.selection1
.pos
, 2, 'pos')
2102 function test_search()
2103 App
.screen
.init
{width
=120, height
=60}
2104 Editor_state
= edit
.initialize_test_state()
2105 Editor_state
.lines
= load_array
{'```lines', '```', 'def', 'ghi', '’deg'} -- contains unicode quote in final line
2106 Text
.redraw_all(Editor_state
)
2107 Editor_state
.cursor1
= {line
=1, pos
=1}
2108 Editor_state
.screen_top1
= {line
=1, pos
=1}
2109 Editor_state
.screen_bottom1
= {}
2110 edit
.draw(Editor_state
)
2111 -- search for a string
2112 edit
.run_after_keychord(Editor_state
, 'C-f')
2113 edit
.run_after_text_input(Editor_state
, 'd')
2114 edit
.run_after_keychord(Editor_state
, 'return')
2115 check_eq(Editor_state
.cursor1
.line
, 2, '1/cursor:line')
2116 check_eq(Editor_state
.cursor1
.pos
, 1, '1/cursor:pos')
2118 Editor_state
.cursor1
= {line
=1, pos
=1}
2119 Editor_state
.screen_top1
= {line
=1, pos
=1}
2120 -- search for second occurrence
2121 edit
.run_after_keychord(Editor_state
, 'C-f')
2122 edit
.run_after_text_input(Editor_state
, 'de')
2123 edit
.run_after_keychord(Editor_state
, 'down')
2124 edit
.run_after_keychord(Editor_state
, 'return')
2125 check_eq(Editor_state
.cursor1
.line
, 4, '2/cursor:line')
2126 check_eq(Editor_state
.cursor1
.pos
, 2, '2/cursor:pos')
2129 function test_search_upwards()
2130 App
.screen
.init
{width
=120, height
=60}
2131 Editor_state
= edit
.initialize_test_state()
2132 Editor_state
.lines
= load_array
{'’abc', 'abd'} -- contains unicode quote
2133 Text
.redraw_all(Editor_state
)
2134 Editor_state
.cursor1
= {line
=2, pos
=1}
2135 Editor_state
.screen_top1
= {line
=1, pos
=1}
2136 Editor_state
.screen_bottom1
= {}
2137 edit
.draw(Editor_state
)
2138 -- search for a string
2139 edit
.run_after_keychord(Editor_state
, 'C-f')
2140 edit
.run_after_text_input(Editor_state
, 'a')
2141 -- search for previous occurrence
2142 edit
.run_after_keychord(Editor_state
, 'up')
2143 check_eq(Editor_state
.cursor1
.line
, 1, '2/cursor:line')
2144 check_eq(Editor_state
.cursor1
.pos
, 2, '2/cursor:pos')
2147 function test_search_wrap()
2148 App
.screen
.init
{width
=120, height
=60}
2149 Editor_state
= edit
.initialize_test_state()
2150 Editor_state
.lines
= load_array
{'’abc', 'def'} -- contains unicode quote in first line
2151 Text
.redraw_all(Editor_state
)
2152 Editor_state
.cursor1
= {line
=2, pos
=1}
2153 Editor_state
.screen_top1
= {line
=1, pos
=1}
2154 Editor_state
.screen_bottom1
= {}
2155 edit
.draw(Editor_state
)
2156 -- search for a string
2157 edit
.run_after_keychord(Editor_state
, 'C-f')
2158 edit
.run_after_text_input(Editor_state
, 'a')
2159 edit
.run_after_keychord(Editor_state
, 'return')
2161 check_eq(Editor_state
.cursor1
.line
, 1, '1/cursor:line')
2162 check_eq(Editor_state
.cursor1
.pos
, 2, '1/cursor:pos')
2165 function test_search_wrap_upwards()
2166 App
.screen
.init
{width
=120, height
=60}
2167 Editor_state
= edit
.initialize_test_state()
2168 Editor_state
.lines
= load_array
{'abc ’abd'} -- contains unicode quote
2169 Text
.redraw_all(Editor_state
)
2170 Editor_state
.cursor1
= {line
=1, pos
=1}
2171 Editor_state
.screen_top1
= {line
=1, pos
=1}
2172 Editor_state
.screen_bottom1
= {}
2173 edit
.draw(Editor_state
)
2174 -- search upwards for a string
2175 edit
.run_after_keychord(Editor_state
, 'C-f')
2176 edit
.run_after_text_input(Editor_state
, 'a')
2177 edit
.run_after_keychord(Editor_state
, 'up')
2179 check_eq(Editor_state
.cursor1
.line
, 1, '1/cursor:line')
2180 check_eq(Editor_state
.cursor1
.pos
, 6, '1/cursor:pos')