text_tests.lua 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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.
  4. function test_initial_state()
  5. App.screen.init{width=120, height=60}
  6. Editor_state = edit.initialize_test_state()
  7. Editor_state.lines = load_array{}
  8. Text.redraw_all(Editor_state)
  9. edit.draw(Editor_state)
  10. check_eq(#Editor_state.lines, 1, '#lines')
  11. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  12. check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
  13. check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
  14. check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
  15. end
  16. function test_move_left()
  17. App.screen.init{width=120, height=60}
  18. Editor_state = edit.initialize_test_state()
  19. Editor_state.lines = load_array{'a'}
  20. Text.redraw_all(Editor_state)
  21. Editor_state.cursor1 = {line=1, pos=2}
  22. edit.draw(Editor_state)
  23. edit.run_after_keychord(Editor_state, 'left', 'left')
  24. check_eq(Editor_state.cursor1.pos, 1, 'check')
  25. end
  26. function test_move_right()
  27. App.screen.init{width=120, height=60}
  28. Editor_state = edit.initialize_test_state()
  29. Editor_state.lines = load_array{'a'}
  30. Text.redraw_all(Editor_state)
  31. Editor_state.cursor1 = {line=1, pos=1}
  32. edit.draw(Editor_state)
  33. edit.run_after_keychord(Editor_state, 'right', 'right')
  34. check_eq(Editor_state.cursor1.pos, 2, 'check')
  35. end
  36. function test_move_left_to_previous_line()
  37. App.screen.init{width=120, height=60}
  38. Editor_state = edit.initialize_test_state()
  39. Editor_state.lines = load_array{'abc', 'def'}
  40. Text.redraw_all(Editor_state)
  41. Editor_state.cursor1 = {line=2, pos=1}
  42. edit.draw(Editor_state)
  43. edit.run_after_keychord(Editor_state, 'left', 'left')
  44. check_eq(Editor_state.cursor1.line, 1, 'line')
  45. check_eq(Editor_state.cursor1.pos, 4, 'pos') -- past end of line
  46. end
  47. function test_move_right_to_next_line()
  48. App.screen.init{width=120, height=60}
  49. Editor_state = edit.initialize_test_state()
  50. Editor_state.lines = load_array{'abc', 'def'}
  51. Text.redraw_all(Editor_state)
  52. Editor_state.cursor1 = {line=1, pos=4} -- past end of line
  53. edit.draw(Editor_state)
  54. edit.run_after_keychord(Editor_state, 'right', 'right')
  55. check_eq(Editor_state.cursor1.line, 2, 'line')
  56. check_eq(Editor_state.cursor1.pos, 1, 'pos')
  57. end
  58. function test_move_to_start_of_word()
  59. App.screen.init{width=120, height=60}
  60. Editor_state = edit.initialize_test_state()
  61. Editor_state.lines = load_array{'abc'}
  62. Text.redraw_all(Editor_state)
  63. Editor_state.cursor1 = {line=1, pos=3}
  64. edit.draw(Editor_state)
  65. edit.run_after_keychord(Editor_state, 'M-left', 'left')
  66. check_eq(Editor_state.cursor1.pos, 1, 'check')
  67. end
  68. function test_move_to_start_of_previous_word()
  69. App.screen.init{width=120, height=60}
  70. Editor_state = edit.initialize_test_state()
  71. Editor_state.lines = load_array{'abc def'}
  72. Text.redraw_all(Editor_state)
  73. Editor_state.cursor1 = {line=1, pos=4} -- at the space between words
  74. edit.draw(Editor_state)
  75. edit.run_after_keychord(Editor_state, 'M-left', 'left')
  76. check_eq(Editor_state.cursor1.pos, 1, 'check')
  77. end
  78. function test_skip_to_previous_word()
  79. App.screen.init{width=120, height=60}
  80. Editor_state = edit.initialize_test_state()
  81. Editor_state.lines = load_array{'abc def'}
  82. Text.redraw_all(Editor_state)
  83. Editor_state.cursor1 = {line=1, pos=5} -- at the start of second word
  84. edit.draw(Editor_state)
  85. edit.run_after_keychord(Editor_state, 'M-left', 'left')
  86. check_eq(Editor_state.cursor1.pos, 1, 'check')
  87. end
  88. function test_skip_past_tab_to_previous_word()
  89. App.screen.init{width=120, height=60}
  90. Editor_state = edit.initialize_test_state()
  91. Editor_state.lines = load_array{'abc def\tghi'}
  92. Text.redraw_all(Editor_state)
  93. Editor_state.cursor1 = {line=1, pos=10} -- within third word
  94. edit.draw(Editor_state)
  95. edit.run_after_keychord(Editor_state, 'M-left', 'left')
  96. check_eq(Editor_state.cursor1.pos, 9, 'check')
  97. end
  98. function test_skip_multiple_spaces_to_previous_word()
  99. App.screen.init{width=120, height=60}
  100. Editor_state = edit.initialize_test_state()
  101. Editor_state.lines = load_array{'abc def'}
  102. Text.redraw_all(Editor_state)
  103. Editor_state.cursor1 = {line=1, pos=6} -- at the start of second word
  104. edit.draw(Editor_state)
  105. edit.run_after_keychord(Editor_state, 'M-left', 'left')
  106. check_eq(Editor_state.cursor1.pos, 1, 'check')
  107. end
  108. function test_move_to_start_of_word_on_previous_line()
  109. App.screen.init{width=120, height=60}
  110. Editor_state = edit.initialize_test_state()
  111. Editor_state.lines = load_array{'abc def', 'ghi'}
  112. Text.redraw_all(Editor_state)
  113. Editor_state.cursor1 = {line=2, pos=1}
  114. edit.draw(Editor_state)
  115. edit.run_after_keychord(Editor_state, 'M-left', 'left')
  116. check_eq(Editor_state.cursor1.line, 1, 'line')
  117. check_eq(Editor_state.cursor1.pos, 5, 'pos')
  118. end
  119. function test_move_past_end_of_word()
  120. App.screen.init{width=120, height=60}
  121. Editor_state = edit.initialize_test_state()
  122. Editor_state.lines = load_array{'abc def'}
  123. Text.redraw_all(Editor_state)
  124. Editor_state.cursor1 = {line=1, pos=1}
  125. edit.draw(Editor_state)
  126. edit.run_after_keychord(Editor_state, 'M-right', 'right')
  127. check_eq(Editor_state.cursor1.pos, 4, 'check')
  128. end
  129. function test_skip_to_next_word()
  130. App.screen.init{width=120, height=60}
  131. Editor_state = edit.initialize_test_state()
  132. Editor_state.lines = load_array{'abc def'}
  133. Text.redraw_all(Editor_state)
  134. Editor_state.cursor1 = {line=1, pos=4} -- at the space between words
  135. edit.draw(Editor_state)
  136. edit.run_after_keychord(Editor_state, 'M-right', 'right')
  137. check_eq(Editor_state.cursor1.pos, 8, 'check')
  138. end
  139. function test_skip_past_tab_to_next_word()
  140. App.screen.init{width=120, height=60}
  141. Editor_state = edit.initialize_test_state()
  142. Editor_state.lines = load_array{'abc\tdef'}
  143. Text.redraw_all(Editor_state)
  144. Editor_state.cursor1 = {line=1, pos=1} -- at the space between words
  145. edit.draw(Editor_state)
  146. edit.run_after_keychord(Editor_state, 'M-right', 'right')
  147. check_eq(Editor_state.cursor1.pos, 4, 'check')
  148. end
  149. function test_skip_multiple_spaces_to_next_word()
  150. App.screen.init{width=120, height=60}
  151. Editor_state = edit.initialize_test_state()
  152. Editor_state.lines = load_array{'abc def'}
  153. Text.redraw_all(Editor_state)
  154. Editor_state.cursor1 = {line=1, pos=4} -- at the start of second word
  155. edit.draw(Editor_state)
  156. edit.run_after_keychord(Editor_state, 'M-right', 'right')
  157. check_eq(Editor_state.cursor1.pos, 9, 'check')
  158. end
  159. function test_move_past_end_of_word_on_next_line()
  160. App.screen.init{width=120, height=60}
  161. Editor_state = edit.initialize_test_state()
  162. Editor_state.lines = load_array{'abc def', 'ghi'}
  163. Text.redraw_all(Editor_state)
  164. Editor_state.cursor1 = {line=1, pos=8}
  165. edit.draw(Editor_state)
  166. edit.run_after_keychord(Editor_state, 'M-right', 'right')
  167. check_eq(Editor_state.cursor1.line, 2, 'line')
  168. check_eq(Editor_state.cursor1.pos, 4, 'pos')
  169. end
  170. function test_click_moves_cursor()
  171. App.screen.init{width=50, height=60}
  172. Editor_state = edit.initialize_test_state()
  173. Editor_state.lines = load_array{'abc', 'def', 'xyz'}
  174. Text.redraw_all(Editor_state)
  175. Editor_state.cursor1 = {line=1, pos=1}
  176. Editor_state.screen_top1 = {line=1, pos=1}
  177. Editor_state.screen_bottom1 = {}
  178. Editor_state.selection1 = {}
  179. edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
  180. edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  181. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  182. check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
  183. -- selection is empty to avoid perturbing future edits
  184. check_nil(Editor_state.selection1.line, 'selection:line')
  185. check_nil(Editor_state.selection1.pos, 'selection:pos')
  186. end
  187. function test_click_to_left_of_line()
  188. -- display a line with the cursor in the middle
  189. App.screen.init{width=50, height=80}
  190. Editor_state = edit.initialize_test_state()
  191. Editor_state.lines = load_array{'abc'}
  192. Text.redraw_all(Editor_state)
  193. Editor_state.cursor1 = {line=1, pos=3}
  194. Editor_state.screen_top1 = {line=1, pos=1}
  195. Editor_state.screen_bottom1 = {}
  196. Editor_state.selection1 = {}
  197. -- click to the left of the line
  198. edit.draw(Editor_state)
  199. edit.run_after_mouse_click(Editor_state, Editor_state.left-4,Editor_state.top+5, 1)
  200. -- cursor moves to start of line
  201. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  202. check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
  203. check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
  204. end
  205. function test_click_takes_margins_into_account()
  206. -- display two lines with cursor on one of them
  207. App.screen.init{width=100, height=80}
  208. Editor_state = edit.initialize_test_state()
  209. Editor_state.left = 50 -- occupy only right side of screen
  210. Editor_state.lines = load_array{'abc', 'def'}
  211. Text.redraw_all(Editor_state)
  212. Editor_state.cursor1 = {line=2, pos=1}
  213. Editor_state.screen_top1 = {line=1, pos=1}
  214. Editor_state.screen_bottom1 = {}
  215. Editor_state.selection1 = {}
  216. -- click on the other line
  217. edit.draw(Editor_state)
  218. edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  219. -- cursor moves
  220. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  221. check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
  222. check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
  223. end
  224. function test_click_on_empty_line()
  225. -- display two lines with the first one empty
  226. App.screen.init{width=50, height=80}
  227. Editor_state = edit.initialize_test_state()
  228. Editor_state.lines = load_array{'', 'def'}
  229. Text.redraw_all(Editor_state)
  230. Editor_state.cursor1 = {line=2, pos=1}
  231. Editor_state.screen_top1 = {line=1, pos=1}
  232. Editor_state.screen_bottom1 = {}
  233. Editor_state.selection1 = {}
  234. -- click on the empty line
  235. edit.draw(Editor_state)
  236. edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  237. -- cursor moves
  238. check_eq(Editor_state.cursor1.line, 1, 'cursor')
  239. -- selection remains empty
  240. check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
  241. end
  242. function test_click_below_all_lines()
  243. -- display one line
  244. App.screen.init{width=50, height=80}
  245. Editor_state = edit.initialize_test_state()
  246. Editor_state.lines = load_array{'abc'}
  247. Text.redraw_all(Editor_state)
  248. Editor_state.cursor1 = {line=1, pos=1}
  249. Editor_state.screen_top1 = {line=1, pos=1}
  250. Editor_state.screen_bottom1 = {}
  251. Editor_state.selection1 = {}
  252. -- click below first line
  253. edit.draw(Editor_state)
  254. edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+50, 1)
  255. -- cursor doesn't move
  256. check_eq(Editor_state.cursor1.line, 1, 'cursor')
  257. -- selection remains empty
  258. check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
  259. end
  260. function test_draw_text()
  261. App.screen.init{width=120, height=60}
  262. Editor_state = edit.initialize_test_state()
  263. Editor_state.lines = load_array{'abc', 'def', 'ghi'}
  264. Text.redraw_all(Editor_state)
  265. Editor_state.cursor1 = {line=1, pos=1}
  266. Editor_state.screen_top1 = {line=1, pos=1}
  267. Editor_state.screen_bottom1 = {}
  268. edit.draw(Editor_state)
  269. local y = Editor_state.top
  270. App.screen.check(y, 'abc', 'screen:1')
  271. y = y + Editor_state.line_height
  272. App.screen.check(y, 'def', 'screen:2')
  273. y = y + Editor_state.line_height
  274. App.screen.check(y, 'ghi', 'screen:3')
  275. end
  276. function test_draw_wrapping_text()
  277. App.screen.init{width=50, height=60}
  278. Editor_state = edit.initialize_test_state()
  279. Editor_state.lines = load_array{'abc', 'defgh', 'xyz'}
  280. Text.redraw_all(Editor_state)
  281. Editor_state.cursor1 = {line=1, pos=1}
  282. Editor_state.screen_top1 = {line=1, pos=1}
  283. Editor_state.screen_bottom1 = {}
  284. edit.draw(Editor_state)
  285. local y = Editor_state.top
  286. App.screen.check(y, 'abc', 'screen:1')
  287. y = y + Editor_state.line_height
  288. App.screen.check(y, 'de', 'screen:2')
  289. y = y + Editor_state.line_height
  290. App.screen.check(y, 'fgh', 'screen:3')
  291. end
  292. function test_draw_word_wrapping_text()
  293. App.screen.init{width=60, height=60}
  294. Editor_state = edit.initialize_test_state()
  295. Editor_state.lines = load_array{'abc def ghi', 'jkl'}
  296. Text.redraw_all(Editor_state)
  297. Editor_state.cursor1 = {line=1, pos=1}
  298. Editor_state.screen_top1 = {line=1, pos=1}
  299. Editor_state.screen_bottom1 = {}
  300. edit.draw(Editor_state)
  301. local y = Editor_state.top
  302. App.screen.check(y, 'abc ', 'screen:1')
  303. y = y + Editor_state.line_height
  304. App.screen.check(y, 'def ', 'screen:2')
  305. y = y + Editor_state.line_height
  306. App.screen.check(y, 'ghi', 'screen:3')
  307. end
  308. function test_click_on_wrapping_line()
  309. -- display two screen lines with cursor on one of them
  310. App.screen.init{width=50, height=80}
  311. Editor_state = edit.initialize_test_state()
  312. Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu'}
  313. Text.redraw_all(Editor_state)
  314. Editor_state.cursor1 = {line=1, pos=20}
  315. Editor_state.screen_top1 = {line=1, pos=1}
  316. Editor_state.screen_bottom1 = {}
  317. -- click on the other line
  318. edit.draw(Editor_state)
  319. edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  320. -- cursor moves
  321. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  322. check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
  323. check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
  324. end
  325. function test_click_on_wrapping_line_takes_margins_into_account()
  326. -- display two screen lines with cursor on one of them
  327. App.screen.init{width=100, height=80}
  328. Editor_state = edit.initialize_test_state()
  329. Editor_state.left = 50 -- occupy only right side of screen
  330. Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu'}
  331. Text.redraw_all(Editor_state)
  332. Editor_state.cursor1 = {line=1, pos=20}
  333. Editor_state.screen_top1 = {line=1, pos=1}
  334. Editor_state.screen_bottom1 = {}
  335. -- click on the other line
  336. edit.draw(Editor_state)
  337. edit.run_after_mouse_click(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  338. -- cursor moves
  339. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  340. check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
  341. check_nil(Editor_state.selection1.line, 'selection is empty to avoid perturbing future edits')
  342. end
  343. function test_draw_text_wrapping_within_word()
  344. -- arrange a screen line that needs to be split within a word
  345. App.screen.init{width=60, height=60}
  346. Editor_state = edit.initialize_test_state()
  347. Editor_state.lines = load_array{'abcd e fghijk', 'xyz'}
  348. Text.redraw_all(Editor_state)
  349. Editor_state.cursor1 = {line=1, pos=1}
  350. Editor_state.screen_top1 = {line=1, pos=1}
  351. Editor_state.screen_bottom1 = {}
  352. edit.draw(Editor_state)
  353. local y = Editor_state.top
  354. App.screen.check(y, 'abcd ', 'screen:1')
  355. y = y + Editor_state.line_height
  356. App.screen.check(y, 'e fgh', 'screen:2')
  357. y = y + Editor_state.line_height
  358. App.screen.check(y, 'ijk', 'screen:3')
  359. end
  360. function test_draw_wrapping_text_containing_non_ascii()
  361. -- draw a long line containing non-ASCII
  362. App.screen.init{width=60, height=60}
  363. Editor_state = edit.initialize_test_state()
  364. Editor_state.lines = load_array{'madam I’m adam', 'xyz'} -- notice the non-ASCII apostrophe
  365. Text.redraw_all(Editor_state)
  366. Editor_state.cursor1 = {line=1, pos=1}
  367. Editor_state.screen_top1 = {line=1, pos=1}
  368. Editor_state.screen_bottom1 = {}
  369. edit.draw(Editor_state)
  370. local y = Editor_state.top
  371. App.screen.check(y, 'mad', 'screen:1')
  372. y = y + Editor_state.line_height
  373. App.screen.check(y, 'am I', 'screen:2')
  374. y = y + Editor_state.line_height
  375. App.screen.check(y, '’m a', 'screen:3')
  376. end
  377. function test_click_past_end_of_screen_line()
  378. -- display a wrapping line
  379. App.screen.init{width=75, height=80}
  380. Editor_state = edit.initialize_test_state()
  381. -- 12345678901234
  382. Editor_state.lines = load_array{"madam I'm adam"}
  383. Text.redraw_all(Editor_state)
  384. Editor_state.cursor1 = {line=1, pos=1}
  385. Editor_state.screen_top1 = {line=1, pos=1}
  386. Editor_state.screen_bottom1 = {}
  387. edit.draw(Editor_state)
  388. local y = Editor_state.top
  389. App.screen.check(y, 'madam ', 'baseline/screen:1')
  390. y = y + Editor_state.line_height
  391. App.screen.check(y, "I'm ad", 'baseline/screen:2')
  392. y = y + Editor_state.line_height
  393. -- click past end of second screen line
  394. edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
  395. -- cursor moves to end of screen line (one more than final character shown)
  396. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  397. check_eq(Editor_state.cursor1.pos, 13, 'cursor:pos')
  398. end
  399. function test_click_on_wrapping_line_rendered_from_partway_at_top_of_screen()
  400. -- display a wrapping line from its second screen line
  401. App.screen.init{width=75, height=80}
  402. Editor_state = edit.initialize_test_state()
  403. -- 12345678901234
  404. Editor_state.lines = load_array{"madam I'm adam"}
  405. Text.redraw_all(Editor_state)
  406. Editor_state.cursor1 = {line=1, pos=8}
  407. Editor_state.screen_top1 = {line=1, pos=7}
  408. Editor_state.screen_bottom1 = {}
  409. edit.draw(Editor_state)
  410. local y = Editor_state.top
  411. App.screen.check(y, "I'm ad", 'baseline/screen:2')
  412. y = y + Editor_state.line_height
  413. -- click past end of second screen line
  414. edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
  415. -- cursor moves to end of screen line (one more than final character shown)
  416. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  417. check_eq(Editor_state.cursor1.pos, 13, 'cursor:pos')
  418. end
  419. function test_click_past_end_of_wrapping_line()
  420. -- display a wrapping line
  421. App.screen.init{width=75, height=80}
  422. Editor_state = edit.initialize_test_state()
  423. -- 12345678901234
  424. Editor_state.lines = load_array{"madam I'm adam"}
  425. Text.redraw_all(Editor_state)
  426. Editor_state.cursor1 = {line=1, pos=1}
  427. Editor_state.screen_top1 = {line=1, pos=1}
  428. Editor_state.screen_bottom1 = {}
  429. edit.draw(Editor_state)
  430. local y = Editor_state.top
  431. App.screen.check(y, 'madam ', 'baseline/screen:1')
  432. y = y + Editor_state.line_height
  433. App.screen.check(y, "I'm ad", 'baseline/screen:2')
  434. y = y + Editor_state.line_height
  435. App.screen.check(y, 'am', 'baseline/screen:3')
  436. y = y + Editor_state.line_height
  437. -- click past the end of it
  438. edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
  439. -- cursor moves to end of line
  440. check_eq(Editor_state.cursor1.pos, 15, 'cursor') -- one more than the number of UTF-8 code-points
  441. end
  442. function test_click_past_end_of_wrapping_line_containing_non_ascii()
  443. -- display a wrapping line containing non-ASCII
  444. App.screen.init{width=75, height=80}
  445. Editor_state = edit.initialize_test_state()
  446. -- 12345678901234
  447. Editor_state.lines = load_array{'madam I’m adam'} -- notice the non-ASCII apostrophe
  448. Text.redraw_all(Editor_state)
  449. Editor_state.cursor1 = {line=1, pos=1}
  450. Editor_state.screen_top1 = {line=1, pos=1}
  451. Editor_state.screen_bottom1 = {}
  452. edit.draw(Editor_state)
  453. local y = Editor_state.top
  454. App.screen.check(y, 'madam ', 'baseline/screen:1')
  455. y = y + Editor_state.line_height
  456. App.screen.check(y, 'I’m ad', 'baseline/screen:2')
  457. y = y + Editor_state.line_height
  458. App.screen.check(y, 'am', 'baseline/screen:3')
  459. y = y + Editor_state.line_height
  460. -- click past the end of it
  461. edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
  462. -- cursor moves to end of line
  463. check_eq(Editor_state.cursor1.pos, 15, 'cursor') -- one more than the number of UTF-8 code-points
  464. end
  465. function test_click_past_end_of_word_wrapping_line()
  466. -- display a long line wrapping at a word boundary on a screen of more realistic length
  467. App.screen.init{width=160, height=80}
  468. Editor_state = edit.initialize_test_state()
  469. -- 0 1 2
  470. -- 123456789012345678901
  471. Editor_state.lines = load_array{'the quick brown fox jumped over the lazy dog'}
  472. Text.redraw_all(Editor_state)
  473. Editor_state.cursor1 = {line=1, pos=1}
  474. Editor_state.screen_top1 = {line=1, pos=1}
  475. Editor_state.screen_bottom1 = {}
  476. edit.draw(Editor_state)
  477. local y = Editor_state.top
  478. App.screen.check(y, 'the quick brown fox ', 'baseline/screen:1')
  479. y = y + Editor_state.line_height
  480. -- click past the end of the screen line
  481. edit.run_after_mouse_click(Editor_state, App.screen.width-2,y-2, 1)
  482. -- cursor moves to end of screen line (one more than final character shown)
  483. check_eq(Editor_state.cursor1.pos, 21, 'cursor')
  484. end
  485. function test_select_text()
  486. -- display a line of text
  487. App.screen.init{width=75, height=80}
  488. Editor_state = edit.initialize_test_state()
  489. Editor_state.lines = load_array{'abc def'}
  490. Text.redraw_all(Editor_state)
  491. Editor_state.cursor1 = {line=1, pos=1}
  492. Editor_state.screen_top1 = {line=1, pos=1}
  493. Editor_state.screen_bottom1 = {}
  494. edit.draw(Editor_state)
  495. -- select a letter
  496. App.fake_key_press('lshift')
  497. edit.run_after_keychord(Editor_state, 'S-right', 'right')
  498. App.fake_key_release('lshift')
  499. edit.key_release(Editor_state, 'lshift')
  500. -- selection persists even after shift is released
  501. check_eq(Editor_state.selection1.line, 1, 'selection:line')
  502. check_eq(Editor_state.selection1.pos, 1, 'selection:pos')
  503. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  504. check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
  505. end
  506. function test_cursor_movement_without_shift_resets_selection()
  507. -- display a line of text with some part selected
  508. App.screen.init{width=75, height=80}
  509. Editor_state = edit.initialize_test_state()
  510. Editor_state.lines = load_array{'abc'}
  511. Text.redraw_all(Editor_state)
  512. Editor_state.cursor1 = {line=1, pos=1}
  513. Editor_state.selection1 = {line=1, pos=2}
  514. Editor_state.screen_top1 = {line=1, pos=1}
  515. Editor_state.screen_bottom1 = {}
  516. edit.draw(Editor_state)
  517. -- press an arrow key without shift
  518. edit.run_after_keychord(Editor_state, 'right', 'right')
  519. -- no change to data, selection is reset
  520. check_nil(Editor_state.selection1.line, 'check')
  521. check_eq(Editor_state.lines[1].data, 'abc', 'data')
  522. end
  523. function test_copy_does_not_reset_selection()
  524. -- display a line of text with a selection
  525. App.screen.init{width=75, height=80}
  526. Editor_state = edit.initialize_test_state()
  527. Editor_state.lines = load_array{'abc'}
  528. Text.redraw_all(Editor_state)
  529. Editor_state.cursor1 = {line=1, pos=1}
  530. Editor_state.selection1 = {line=1, pos=2}
  531. Editor_state.screen_top1 = {line=1, pos=1}
  532. Editor_state.screen_bottom1 = {}
  533. edit.draw(Editor_state)
  534. -- copy selection
  535. edit.run_after_keychord(Editor_state, 'C-c', 'c')
  536. check_eq(App.clipboard, 'a', 'clipboard')
  537. -- selection is reset since shift key is not pressed
  538. check(Editor_state.selection1.line, 'check')
  539. end
  540. function test_move_cursor_using_mouse()
  541. App.screen.init{width=50, height=60}
  542. Editor_state = edit.initialize_test_state()
  543. Editor_state.lines = load_array{'abc', 'def', 'xyz'}
  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. Editor_state.selection1 = {}
  549. edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
  550. edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  551. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  552. check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
  553. check_nil(Editor_state.selection1.line, 'selection:line')
  554. check_nil(Editor_state.selection1.pos, 'selection:pos')
  555. end
  556. function test_select_text_using_mouse()
  557. App.screen.init{width=50, height=60}
  558. Editor_state = edit.initialize_test_state()
  559. Editor_state.lines = load_array{'abc', 'def', 'xyz'}
  560. Text.redraw_all(Editor_state)
  561. Editor_state.cursor1 = {line=1, pos=1}
  562. Editor_state.screen_top1 = {line=1, pos=1}
  563. Editor_state.screen_bottom1 = {}
  564. Editor_state.selection1 = {}
  565. edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
  566. -- press and hold on first location
  567. edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  568. -- drag and release somewhere else
  569. edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)
  570. check_eq(Editor_state.selection1.line, 1, 'selection:line')
  571. check_eq(Editor_state.selection1.pos, 2, 'selection:pos')
  572. check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
  573. check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
  574. end
  575. function test_select_text_using_mouse_starting_above_text()
  576. App.screen.init{width=50, height=60}
  577. Editor_state = edit.initialize_test_state()
  578. Editor_state.lines = load_array{'abc', 'def', 'xyz'}
  579. Text.redraw_all(Editor_state)
  580. Editor_state.cursor1 = {line=1, pos=1}
  581. Editor_state.screen_top1 = {line=1, pos=1}
  582. Editor_state.screen_bottom1 = {}
  583. Editor_state.selection1 = {}
  584. edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
  585. -- press mouse above first line of text
  586. edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
  587. check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
  588. check_eq(Editor_state.selection1.line, 1, 'selection:line')
  589. check_eq(Editor_state.selection1.pos, 1, 'selection:pos')
  590. end
  591. function test_select_text_using_mouse_starting_above_text_wrapping_line()
  592. -- first screen line starts in the middle of a line
  593. App.screen.init{width=50, height=60}
  594. Editor_state = edit.initialize_test_state()
  595. Editor_state.lines = load_array{'abc', 'defgh', 'xyz'}
  596. Text.redraw_all(Editor_state)
  597. Editor_state.cursor1 = {line=2, pos=5}
  598. Editor_state.screen_top1 = {line=2, pos=3}
  599. Editor_state.screen_bottom1 = {}
  600. -- press mouse above first line of text
  601. edit.draw(Editor_state)
  602. edit.run_after_mouse_press(Editor_state, Editor_state.left+8,5, 1)
  603. -- selection is at screen top
  604. check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
  605. check_eq(Editor_state.selection1.line, 2, 'selection:line')
  606. check_eq(Editor_state.selection1.pos, 3, 'selection:pos')
  607. end
  608. function test_select_text_using_mouse_starting_below_text()
  609. -- I'd like to test what happens when a mouse click is below some page of
  610. -- text, potentially even in the middle of a line.
  611. -- However, it's brittle to set up a text line boundary just right.
  612. -- So I'm going to just check things below the bottom of the final line of
  613. -- text when it's in the middle of the screen.
  614. -- final screen line ends in the middle of screen
  615. App.screen.init{width=50, height=60}
  616. Editor_state = edit.initialize_test_state()
  617. Editor_state.lines = load_array{'abcde'}
  618. Text.redraw_all(Editor_state)
  619. Editor_state.cursor1 = {line=1, pos=1}
  620. Editor_state.screen_top1 = {line=1, pos=1}
  621. Editor_state.screen_bottom1 = {}
  622. edit.draw(Editor_state)
  623. local y = Editor_state.top
  624. App.screen.check(y, 'ab', 'baseline:screen:1')
  625. y = y + Editor_state.line_height
  626. App.screen.check(y, 'cde', 'baseline:screen:2')
  627. -- press mouse above first line of text
  628. edit.run_after_mouse_press(Editor_state, 5,App.screen.height-5, 1)
  629. -- selection is past bottom-most text in screen
  630. check(Editor_state.selection1.line ~= nil, 'selection:line-not-nil')
  631. check_eq(Editor_state.selection1.line, 1, 'selection:line')
  632. check_eq(Editor_state.selection1.pos, 6, 'selection:pos')
  633. end
  634. function test_select_text_using_mouse_and_shift()
  635. App.screen.init{width=50, height=60}
  636. Editor_state = edit.initialize_test_state()
  637. Editor_state.lines = load_array{'abc', 'def', 'xyz'}
  638. Text.redraw_all(Editor_state)
  639. Editor_state.cursor1 = {line=1, pos=1}
  640. Editor_state.screen_top1 = {line=1, pos=1}
  641. Editor_state.screen_bottom1 = {}
  642. Editor_state.selection1 = {}
  643. edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
  644. -- click on first location
  645. edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  646. edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  647. -- hold down shift and click somewhere else
  648. App.fake_key_press('lshift')
  649. edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)
  650. edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)
  651. App.fake_key_release('lshift')
  652. check_eq(Editor_state.selection1.line, 1, 'selection:line')
  653. check_eq(Editor_state.selection1.pos, 2, 'selection:pos')
  654. check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
  655. check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
  656. end
  657. function test_select_text_repeatedly_using_mouse_and_shift()
  658. App.screen.init{width=50, height=60}
  659. Editor_state = edit.initialize_test_state()
  660. Editor_state.lines = load_array{'abc', 'def', 'xyz'}
  661. Text.redraw_all(Editor_state)
  662. Text.redraw_all(Editor_state)
  663. Editor_state.cursor1 = {line=1, pos=1}
  664. Editor_state.screen_top1 = {line=1, pos=1}
  665. Editor_state.screen_bottom1 = {}
  666. Editor_state.selection1 = {}
  667. edit.draw(Editor_state) -- populate line_cache.starty for each line Editor_state.line_cache
  668. -- click on first location
  669. edit.run_after_mouse_press(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  670. edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+5, 1)
  671. -- hold down shift and click on a second location
  672. App.fake_key_press('lshift')
  673. edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)
  674. edit.run_after_mouse_release(Editor_state, Editor_state.left+20,Editor_state.top+Editor_state.line_height+5, 1)
  675. -- hold down shift and click at a third location
  676. App.fake_key_press('lshift')
  677. edit.run_after_mouse_press(Editor_state, Editor_state.left+20,Editor_state.top+5, 1)
  678. edit.run_after_mouse_release(Editor_state, Editor_state.left+8,Editor_state.top+Editor_state.line_height+5, 1)
  679. App.fake_key_release('lshift')
  680. -- selection is between first and third location. forget the second location, not the first.
  681. check_eq(Editor_state.selection1.line, 1, 'selection:line')
  682. check_eq(Editor_state.selection1.pos, 2, 'selection:pos')
  683. check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
  684. check_eq(Editor_state.cursor1.pos, 2, 'cursor:pos')
  685. end
  686. function test_select_all_text()
  687. -- display a single line of text
  688. App.screen.init{width=75, height=80}
  689. Editor_state = edit.initialize_test_state()
  690. Editor_state.lines = load_array{'abc def'}
  691. Text.redraw_all(Editor_state)
  692. Editor_state.cursor1 = {line=1, pos=1}
  693. Editor_state.screen_top1 = {line=1, pos=1}
  694. Editor_state.screen_bottom1 = {}
  695. edit.draw(Editor_state)
  696. -- select all
  697. App.fake_key_press('lctrl')
  698. edit.run_after_keychord(Editor_state, 'C-a', 'a')
  699. App.fake_key_release('lctrl')
  700. edit.key_release(Editor_state, 'lctrl')
  701. -- selection
  702. check_eq(Editor_state.selection1.line, 1, 'selection:line')
  703. check_eq(Editor_state.selection1.pos, 1, 'selection:pos')
  704. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  705. check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
  706. end
  707. function test_pagedown()
  708. App.screen.init{width=120, height=45}
  709. Editor_state = edit.initialize_test_state()
  710. Editor_state.lines = load_array{'abc', 'def', 'ghi'}
  711. Text.redraw_all(Editor_state)
  712. Editor_state.cursor1 = {line=1, pos=1}
  713. Editor_state.screen_top1 = {line=1, pos=1}
  714. Editor_state.screen_bottom1 = {}
  715. -- initially the first two lines are displayed
  716. edit.draw(Editor_state)
  717. local y = Editor_state.top
  718. App.screen.check(y, 'abc', 'baseline/screen:1')
  719. y = y + Editor_state.line_height
  720. App.screen.check(y, 'def', 'baseline/screen:2')
  721. -- after pagedown the bottom line becomes the top
  722. edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
  723. check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
  724. check_eq(Editor_state.cursor1.line, 2, 'cursor')
  725. y = Editor_state.top
  726. App.screen.check(y, 'def', 'screen:1')
  727. y = y + Editor_state.line_height
  728. App.screen.check(y, 'ghi', 'screen:2')
  729. end
  730. function test_pagedown_often_shows_start_of_wrapping_line()
  731. -- draw a few lines ending in part of a wrapping line
  732. App.screen.init{width=50, height=60}
  733. Editor_state = edit.initialize_test_state()
  734. Editor_state.lines = load_array{'abc', 'def ghi jkl', 'mno'}
  735. Text.redraw_all(Editor_state)
  736. Editor_state.cursor1 = {line=1, pos=1}
  737. Editor_state.screen_top1 = {line=1, pos=1}
  738. Editor_state.screen_bottom1 = {}
  739. edit.draw(Editor_state)
  740. local y = Editor_state.top
  741. App.screen.check(y, 'abc', 'baseline/screen:1')
  742. y = y + Editor_state.line_height
  743. App.screen.check(y, 'def ', 'baseline/screen:2')
  744. y = y + Editor_state.line_height
  745. App.screen.check(y, 'ghi ', 'baseline/screen:3')
  746. -- after pagedown we start drawing from the bottom _line_ (multiple screen lines)
  747. edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
  748. check_eq(Editor_state.screen_top1.line, 2, 'screen_top:line')
  749. check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
  750. check_eq(Editor_state.cursor1.line, 2, 'cursor:line')
  751. check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
  752. y = Editor_state.top
  753. App.screen.check(y, 'def ', 'screen:1')
  754. y = y + Editor_state.line_height
  755. App.screen.check(y, 'ghi ', 'screen:2')
  756. y = y + Editor_state.line_height
  757. App.screen.check(y, 'jkl', 'screen:3')
  758. end
  759. function test_pagedown_can_start_from_middle_of_long_wrapping_line()
  760. -- draw a few lines starting from a very long wrapping line
  761. App.screen.init{width=Editor_state.left+30, height=60}
  762. Editor_state = edit.initialize_test_state()
  763. Editor_state.lines = load_array{'abc def ghi jkl mno pqr stu vwx yza bcd efg hij', 'XYZ'}
  764. Text.redraw_all(Editor_state)
  765. Editor_state.cursor1 = {line=1, pos=2}
  766. Editor_state.screen_top1 = {line=1, pos=1}
  767. Editor_state.screen_bottom1 = {}
  768. edit.draw(Editor_state)
  769. local y = Editor_state.top
  770. App.screen.check(y, 'abc ', 'baseline/screen:1')
  771. y = y + Editor_state.line_height
  772. App.screen.check(y, 'def ', 'baseline/screen:2')
  773. y = y + Editor_state.line_height
  774. App.screen.check(y, 'ghi ', 'baseline/screen:3')
  775. -- after pagedown we scroll down the very long wrapping line
  776. edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
  777. check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
  778. check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
  779. y = Editor_state.top
  780. App.screen.check(y, 'ghi ', 'screen:1')
  781. y = y + Editor_state.line_height
  782. App.screen.check(y, 'jkl ', 'screen:2')
  783. y = y + Editor_state.line_height
  784. if Version == '12.0' then
  785. -- HACK: Maybe v12.0 uses a different font? Strange that it only causes
  786. -- issues in a couple of places.
  787. -- We'll need to rethink our tests if issues like this start to multiply.
  788. App.screen.check(y, 'mno ', 'screen:3')
  789. else
  790. App.screen.check(y, 'mn', 'screen:3')
  791. end
  792. end
  793. function test_pagedown_never_moves_up()
  794. -- draw the final screen line of a wrapping line
  795. App.screen.init{width=Editor_state.left+30, height=60}
  796. Editor_state = edit.initialize_test_state()
  797. Editor_state.lines = load_array{'abc def ghi'}
  798. Text.redraw_all(Editor_state)
  799. Editor_state.cursor1 = {line=1, pos=9}
  800. Editor_state.screen_top1 = {line=1, pos=9}
  801. Editor_state.screen_bottom1 = {}
  802. edit.draw(Editor_state)
  803. -- pagedown makes no change
  804. edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
  805. check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
  806. check_eq(Editor_state.screen_top1.pos, 9, 'screen_top:pos')
  807. end
  808. function test_down_arrow_moves_cursor()
  809. App.screen.init{width=120, 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=1}
  814. Editor_state.screen_top1 = {line=1, pos=1}
  815. Editor_state.screen_bottom1 = {}
  816. -- initially the first three lines are displayed
  817. edit.draw(Editor_state)
  818. local y = Editor_state.top
  819. App.screen.check(y, 'abc', 'baseline/screen:1')
  820. y = y + Editor_state.line_height
  821. App.screen.check(y, 'def', 'baseline/screen:2')
  822. y = y + Editor_state.line_height
  823. App.screen.check(y, 'ghi', 'baseline/screen:3')
  824. -- after hitting the down arrow, the cursor moves down by 1 line
  825. edit.run_after_keychord(Editor_state, 'down', 'down')
  826. check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
  827. check_eq(Editor_state.cursor1.line, 2, 'cursor')
  828. -- the screen is unchanged
  829. y = Editor_state.top
  830. App.screen.check(y, 'abc', 'screen:1')
  831. y = y + Editor_state.line_height
  832. App.screen.check(y, 'def', 'screen:2')
  833. y = y + Editor_state.line_height
  834. App.screen.check(y, 'ghi', 'screen:3')
  835. end
  836. function test_down_arrow_scrolls_down_by_one_line()
  837. -- display the first three lines with the cursor on the bottom line
  838. App.screen.init{width=120, height=60}
  839. Editor_state = edit.initialize_test_state()
  840. Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
  841. Text.redraw_all(Editor_state)
  842. Editor_state.cursor1 = {line=3, pos=1}
  843. Editor_state.screen_top1 = {line=1, pos=1}
  844. Editor_state.screen_bottom1 = {}
  845. edit.draw(Editor_state)
  846. local y = Editor_state.top
  847. App.screen.check(y, 'abc', 'baseline/screen:1')
  848. y = y + Editor_state.line_height
  849. App.screen.check(y, 'def', 'baseline/screen:2')
  850. y = y + Editor_state.line_height
  851. App.screen.check(y, 'ghi', 'baseline/screen:3')
  852. -- after hitting the down arrow the screen scrolls down by one line
  853. edit.run_after_keychord(Editor_state, 'down', 'down')
  854. check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
  855. check_eq(Editor_state.cursor1.line, 4, 'cursor')
  856. y = Editor_state.top
  857. App.screen.check(y, 'def', 'screen:1')
  858. y = y + Editor_state.line_height
  859. App.screen.check(y, 'ghi', 'screen:2')
  860. y = y + Editor_state.line_height
  861. App.screen.check(y, 'jkl', 'screen:3')
  862. end
  863. function test_down_arrow_scrolls_down_by_one_screen_line()
  864. -- display the first three lines with the cursor on the bottom line
  865. App.screen.init{width=Editor_state.left+30, height=60}
  866. Editor_state = edit.initialize_test_state()
  867. Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
  868. Text.redraw_all(Editor_state)
  869. Editor_state.cursor1 = {line=3, pos=1}
  870. Editor_state.screen_top1 = {line=1, pos=1}
  871. Editor_state.screen_bottom1 = {}
  872. edit.draw(Editor_state)
  873. local y = Editor_state.top
  874. App.screen.check(y, 'abc', 'baseline/screen:1')
  875. y = y + Editor_state.line_height
  876. App.screen.check(y, 'def', 'baseline/screen:2')
  877. y = y + Editor_state.line_height
  878. App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
  879. -- after hitting the down arrow the screen scrolls down by one line
  880. edit.run_after_keychord(Editor_state, 'down', 'down')
  881. check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
  882. check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
  883. check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
  884. y = Editor_state.top
  885. App.screen.check(y, 'def', 'screen:1')
  886. y = y + Editor_state.line_height
  887. App.screen.check(y, 'ghi ', 'screen:2')
  888. y = y + Editor_state.line_height
  889. App.screen.check(y, 'jkl', 'screen:3')
  890. end
  891. function test_down_arrow_scrolls_down_by_one_screen_line_after_splitting_within_word()
  892. -- display the first three lines with the cursor on the bottom line
  893. App.screen.init{width=Editor_state.left+30, height=60}
  894. Editor_state = edit.initialize_test_state()
  895. Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
  896. Text.redraw_all(Editor_state)
  897. Editor_state.cursor1 = {line=3, pos=1}
  898. Editor_state.screen_top1 = {line=1, pos=1}
  899. Editor_state.screen_bottom1 = {}
  900. edit.draw(Editor_state)
  901. local y = Editor_state.top
  902. App.screen.check(y, 'abc', 'baseline/screen:1')
  903. y = y + Editor_state.line_height
  904. App.screen.check(y, 'def', 'baseline/screen:2')
  905. y = y + Editor_state.line_height
  906. App.screen.check(y, 'ghij', 'baseline/screen:3')
  907. -- after hitting the down arrow the screen scrolls down by one line
  908. edit.run_after_keychord(Editor_state, 'down', 'down')
  909. check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
  910. check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
  911. check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
  912. y = Editor_state.top
  913. App.screen.check(y, 'def', 'screen:1')
  914. y = y + Editor_state.line_height
  915. App.screen.check(y, 'ghij', 'screen:2')
  916. y = y + Editor_state.line_height
  917. App.screen.check(y, 'kl', 'screen:3')
  918. end
  919. function test_pagedown_followed_by_down_arrow_does_not_scroll_screen_up()
  920. App.screen.init{width=Editor_state.left+30, height=60}
  921. Editor_state = edit.initialize_test_state()
  922. Editor_state.lines = load_array{'abc', 'def', 'ghijkl', 'mno'}
  923. Text.redraw_all(Editor_state)
  924. Editor_state.cursor1 = {line=3, pos=1}
  925. Editor_state.screen_top1 = {line=1, pos=1}
  926. Editor_state.screen_bottom1 = {}
  927. edit.draw(Editor_state)
  928. local y = Editor_state.top
  929. App.screen.check(y, 'abc', 'baseline/screen:1')
  930. y = y + Editor_state.line_height
  931. App.screen.check(y, 'def', 'baseline/screen:2')
  932. y = y + Editor_state.line_height
  933. App.screen.check(y, 'ghij', 'baseline/screen:3')
  934. -- after hitting pagedown the screen scrolls down to start of a long line
  935. edit.run_after_keychord(Editor_state, 'pagedown', 'pagedown')
  936. check_eq(Editor_state.screen_top1.line, 3, 'baseline2/screen_top')
  937. check_eq(Editor_state.cursor1.line, 3, 'baseline2/cursor:line')
  938. check_eq(Editor_state.cursor1.pos, 1, 'baseline2/cursor:pos')
  939. -- after hitting down arrow the screen doesn't scroll down further, and certainly doesn't scroll up
  940. edit.run_after_keychord(Editor_state, 'down', 'down')
  941. check_eq(Editor_state.screen_top1.line, 3, 'screen_top')
  942. check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
  943. check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
  944. y = Editor_state.top
  945. App.screen.check(y, 'ghij', 'screen:1')
  946. y = y + Editor_state.line_height
  947. App.screen.check(y, 'kl', 'screen:2')
  948. y = y + Editor_state.line_height
  949. App.screen.check(y, 'mno', 'screen:3')
  950. end
  951. function test_up_arrow_moves_cursor()
  952. -- display the first 3 lines with the cursor on the bottom line
  953. App.screen.init{width=120, height=60}
  954. Editor_state = edit.initialize_test_state()
  955. Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
  956. Text.redraw_all(Editor_state)
  957. Editor_state.cursor1 = {line=3, pos=1}
  958. Editor_state.screen_top1 = {line=1, pos=1}
  959. Editor_state.screen_bottom1 = {}
  960. edit.draw(Editor_state)
  961. local y = Editor_state.top
  962. App.screen.check(y, 'abc', 'baseline/screen:1')
  963. y = y + Editor_state.line_height
  964. App.screen.check(y, 'def', 'baseline/screen:2')
  965. y = y + Editor_state.line_height
  966. App.screen.check(y, 'ghi', 'baseline/screen:3')
  967. -- after hitting the up arrow the cursor moves up by 1 line
  968. edit.run_after_keychord(Editor_state, 'up', 'up')
  969. check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
  970. check_eq(Editor_state.cursor1.line, 2, 'cursor')
  971. -- the screen is unchanged
  972. y = Editor_state.top
  973. App.screen.check(y, 'abc', 'screen:1')
  974. y = y + Editor_state.line_height
  975. App.screen.check(y, 'def', 'screen:2')
  976. y = y + Editor_state.line_height
  977. App.screen.check(y, 'ghi', 'screen:3')
  978. end
  979. function test_up_arrow_scrolls_up_by_one_line()
  980. -- display the lines 2/3/4 with the cursor on line 2
  981. App.screen.init{width=120, height=60}
  982. Editor_state = edit.initialize_test_state()
  983. Editor_state.lines = load_array{'abc', 'def', 'ghi', 'jkl'}
  984. Text.redraw_all(Editor_state)
  985. Editor_state.cursor1 = {line=2, pos=1}
  986. Editor_state.screen_top1 = {line=2, pos=1}
  987. Editor_state.screen_bottom1 = {}
  988. edit.draw(Editor_state)
  989. local y = Editor_state.top
  990. App.screen.check(y, 'def', 'baseline/screen:1')
  991. y = y + Editor_state.line_height
  992. App.screen.check(y, 'ghi', 'baseline/screen:2')
  993. y = y + Editor_state.line_height
  994. App.screen.check(y, 'jkl', 'baseline/screen:3')
  995. -- after hitting the up arrow the screen scrolls up by one line
  996. edit.run_after_keychord(Editor_state, 'up', 'up')
  997. check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
  998. check_eq(Editor_state.cursor1.line, 1, 'cursor')
  999. y = Editor_state.top
  1000. App.screen.check(y, 'abc', 'screen:1')
  1001. y = y + Editor_state.line_height
  1002. App.screen.check(y, 'def', 'screen:2')
  1003. y = y + Editor_state.line_height
  1004. App.screen.check(y, 'ghi', 'screen:3')
  1005. end
  1006. function test_up_arrow_scrolls_up_by_one_screen_line()
  1007. -- display lines starting from second screen line of a line
  1008. App.screen.init{width=Editor_state.left+30, height=60}
  1009. Editor_state = edit.initialize_test_state()
  1010. Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
  1011. Text.redraw_all(Editor_state)
  1012. Editor_state.cursor1 = {line=3, pos=6}
  1013. Editor_state.screen_top1 = {line=3, pos=5}
  1014. Editor_state.screen_bottom1 = {}
  1015. edit.draw(Editor_state)
  1016. local y = Editor_state.top
  1017. App.screen.check(y, 'jkl', 'baseline/screen:1')
  1018. y = y + Editor_state.line_height
  1019. App.screen.check(y, 'mno', 'baseline/screen:2')
  1020. -- after hitting the up arrow the screen scrolls up to first screen line
  1021. edit.run_after_keychord(Editor_state, 'up', 'up')
  1022. y = Editor_state.top
  1023. App.screen.check(y, 'ghi ', 'screen:1')
  1024. y = y + Editor_state.line_height
  1025. App.screen.check(y, 'jkl', 'screen:2')
  1026. y = y + Editor_state.line_height
  1027. App.screen.check(y, 'mno', 'screen:3')
  1028. check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
  1029. check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
  1030. check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
  1031. check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
  1032. end
  1033. function test_up_arrow_scrolls_up_to_final_screen_line()
  1034. -- display lines starting just after a long line
  1035. App.screen.init{width=Editor_state.left+30, height=60}
  1036. Editor_state = edit.initialize_test_state()
  1037. Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
  1038. Text.redraw_all(Editor_state)
  1039. Editor_state.cursor1 = {line=2, pos=1}
  1040. Editor_state.screen_top1 = {line=2, pos=1}
  1041. Editor_state.screen_bottom1 = {}
  1042. edit.draw(Editor_state)
  1043. local y = Editor_state.top
  1044. App.screen.check(y, 'ghi', 'baseline/screen:1')
  1045. y = y + Editor_state.line_height
  1046. App.screen.check(y, 'jkl', 'baseline/screen:2')
  1047. y = y + Editor_state.line_height
  1048. App.screen.check(y, 'mno', 'baseline/screen:3')
  1049. -- after hitting the up arrow the screen scrolls up to final screen line of previous line
  1050. edit.run_after_keychord(Editor_state, 'up', 'up')
  1051. y = Editor_state.top
  1052. App.screen.check(y, 'def', 'screen:1')
  1053. y = y + Editor_state.line_height
  1054. App.screen.check(y, 'ghi', 'screen:2')
  1055. y = y + Editor_state.line_height
  1056. App.screen.check(y, 'jkl', 'screen:3')
  1057. check_eq(Editor_state.screen_top1.line, 1, 'screen_top:line')
  1058. check_eq(Editor_state.screen_top1.pos, 5, 'screen_top:pos')
  1059. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  1060. check_eq(Editor_state.cursor1.pos, 5, 'cursor:pos')
  1061. end
  1062. function test_up_arrow_scrolls_up_to_empty_line()
  1063. -- display a screenful of text with an empty line just above it outside the screen
  1064. App.screen.init{width=120, height=60}
  1065. Editor_state = edit.initialize_test_state()
  1066. Editor_state.lines = load_array{'', 'abc', 'def', 'ghi', 'jkl'}
  1067. Text.redraw_all(Editor_state)
  1068. Editor_state.cursor1 = {line=2, pos=1}
  1069. Editor_state.screen_top1 = {line=2, pos=1}
  1070. Editor_state.screen_bottom1 = {}
  1071. edit.draw(Editor_state)
  1072. local y = Editor_state.top
  1073. App.screen.check(y, 'abc', 'baseline/screen:1')
  1074. y = y + Editor_state.line_height
  1075. App.screen.check(y, 'def', 'baseline/screen:2')
  1076. y = y + Editor_state.line_height
  1077. App.screen.check(y, 'ghi', 'baseline/screen:3')
  1078. -- after hitting the up arrow the screen scrolls up by one line
  1079. edit.run_after_keychord(Editor_state, 'up', 'up')
  1080. check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
  1081. check_eq(Editor_state.cursor1.line, 1, 'cursor')
  1082. y = Editor_state.top
  1083. -- empty first line
  1084. y = y + Editor_state.line_height
  1085. App.screen.check(y, 'abc', 'screen:2')
  1086. y = y + Editor_state.line_height
  1087. App.screen.check(y, 'def', 'screen:3')
  1088. end
  1089. function test_pageup()
  1090. App.screen.init{width=120, height=45}
  1091. Editor_state = edit.initialize_test_state()
  1092. Editor_state.lines = load_array{'abc', 'def', 'ghi'}
  1093. Text.redraw_all(Editor_state)
  1094. Editor_state.cursor1 = {line=2, pos=1}
  1095. Editor_state.screen_top1 = {line=2, pos=1}
  1096. Editor_state.screen_bottom1 = {}
  1097. -- initially the last two lines are displayed
  1098. edit.draw(Editor_state)
  1099. local y = Editor_state.top
  1100. App.screen.check(y, 'def', 'baseline/screen:1')
  1101. y = y + Editor_state.line_height
  1102. App.screen.check(y, 'ghi', 'baseline/screen:2')
  1103. -- after pageup the cursor goes to first line
  1104. edit.run_after_keychord(Editor_state, 'pageup', 'pageup')
  1105. check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
  1106. check_eq(Editor_state.cursor1.line, 1, 'cursor')
  1107. y = Editor_state.top
  1108. App.screen.check(y, 'abc', 'screen:1')
  1109. y = y + Editor_state.line_height
  1110. App.screen.check(y, 'def', 'screen:2')
  1111. end
  1112. function test_pageup_scrolls_up_by_screen_line()
  1113. -- display the first three lines with the cursor on the bottom line
  1114. App.screen.init{width=Editor_state.left+30, height=60}
  1115. Editor_state = edit.initialize_test_state()
  1116. Editor_state.lines = load_array{'abc def', 'ghi', 'jkl', 'mno'}
  1117. Text.redraw_all(Editor_state)
  1118. Editor_state.cursor1 = {line=2, pos=1}
  1119. Editor_state.screen_top1 = {line=2, pos=1}
  1120. Editor_state.screen_bottom1 = {}
  1121. edit.draw(Editor_state)
  1122. local y = Editor_state.top
  1123. App.screen.check(y, 'ghi', 'baseline/screen:1')
  1124. y = y + Editor_state.line_height
  1125. App.screen.check(y, 'jkl', 'baseline/screen:2')
  1126. y = y + Editor_state.line_height
  1127. App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
  1128. -- after hitting the page-up key the screen scrolls up to top
  1129. edit.run_after_keychord(Editor_state, 'pageup', 'pageup')
  1130. check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
  1131. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  1132. check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
  1133. y = Editor_state.top
  1134. App.screen.check(y, 'abc ', 'screen:1')
  1135. y = y + Editor_state.line_height
  1136. App.screen.check(y, 'def', 'screen:2')
  1137. y = y + Editor_state.line_height
  1138. App.screen.check(y, 'ghi', 'screen:3')
  1139. end
  1140. function test_pageup_scrolls_up_from_middle_screen_line()
  1141. -- display a few lines starting from the middle of a line (Editor_state.cursor1.pos > 1)
  1142. App.screen.init{width=Editor_state.left+30, height=60}
  1143. Editor_state = edit.initialize_test_state()
  1144. Editor_state.lines = load_array{'abc def', 'ghi jkl', 'mno'}
  1145. Text.redraw_all(Editor_state)
  1146. Editor_state.cursor1 = {line=2, pos=5}
  1147. Editor_state.screen_top1 = {line=2, pos=5}
  1148. Editor_state.screen_bottom1 = {}
  1149. edit.draw(Editor_state)
  1150. local y = Editor_state.top
  1151. App.screen.check(y, 'jkl', 'baseline/screen:2')
  1152. y = y + Editor_state.line_height
  1153. App.screen.check(y, 'mno', 'baseline/screen:3') -- line wrapping includes trailing whitespace
  1154. -- after hitting the page-up key the screen scrolls up to top
  1155. edit.run_after_keychord(Editor_state, 'pageup', 'pageup')
  1156. check_eq(Editor_state.screen_top1.line, 1, 'screen_top')
  1157. check_eq(Editor_state.cursor1.line, 1, 'cursor:line')
  1158. check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
  1159. y = Editor_state.top
  1160. App.screen.check(y, 'abc ', 'screen:1')
  1161. y = y + Editor_state.line_height
  1162. App.screen.check(y, 'def', 'screen:2')
  1163. y = y + Editor_state.line_height
  1164. App.screen.check(y, 'ghi ', 'screen:3')
  1165. end
  1166. function test_left_arrow_scrolls_up_in_wrapped_line()
  1167. -- display lines starting from second screen line of a line
  1168. App.screen.init{width=Editor_state.left+30, height=60}
  1169. Editor_state = edit.initialize_test_state()
  1170. Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
  1171. Text.redraw_all(Editor_state)
  1172. Editor_state.screen_top1 = {line=3, pos=5}
  1173. Editor_state.screen_bottom1 = {}
  1174. -- cursor is at top of screen
  1175. Editor_state.cursor1 = {line=3, pos=5}
  1176. edit.draw(Editor_state)
  1177. local y = Editor_state.top
  1178. App.screen.check(y, 'jkl', 'baseline/screen:1')
  1179. y = y + Editor_state.line_height
  1180. App.screen.check(y, 'mno', 'baseline/screen:2')
  1181. -- after hitting the left arrow the screen scrolls up to first screen line
  1182. edit.run_after_keychord(Editor_state, 'left', 'left')
  1183. y = Editor_state.top
  1184. App.screen.check(y, 'ghi ', 'screen:1')
  1185. y = y + Editor_state.line_height
  1186. App.screen.check(y, 'jkl', 'screen:2')
  1187. y = y + Editor_state.line_height
  1188. App.screen.check(y, 'mno', 'screen:3')
  1189. check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
  1190. check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
  1191. check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
  1192. check_eq(Editor_state.cursor1.pos, 4, 'cursor:pos')
  1193. end
  1194. function test_right_arrow_scrolls_down_in_wrapped_line()
  1195. -- display the first three lines with the cursor on the bottom line
  1196. App.screen.init{width=Editor_state.left+30, height=60}
  1197. Editor_state = edit.initialize_test_state()
  1198. Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
  1199. Text.redraw_all(Editor_state)
  1200. Editor_state.screen_top1 = {line=1, pos=1}
  1201. Editor_state.screen_bottom1 = {}
  1202. -- cursor is at bottom right of screen
  1203. Editor_state.cursor1 = {line=3, pos=5}
  1204. edit.draw(Editor_state)
  1205. local y = Editor_state.top
  1206. App.screen.check(y, 'abc', 'baseline/screen:1')
  1207. y = y + Editor_state.line_height
  1208. App.screen.check(y, 'def', 'baseline/screen:2')
  1209. y = y + Editor_state.line_height
  1210. App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
  1211. -- after hitting the right arrow the screen scrolls down by one line
  1212. edit.run_after_keychord(Editor_state, 'right', 'right')
  1213. check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
  1214. check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
  1215. check_eq(Editor_state.cursor1.pos, 6, 'cursor:pos')
  1216. y = Editor_state.top
  1217. App.screen.check(y, 'def', 'screen:1')
  1218. y = y + Editor_state.line_height
  1219. App.screen.check(y, 'ghi ', 'screen:2')
  1220. y = y + Editor_state.line_height
  1221. App.screen.check(y, 'jkl', 'screen:3')
  1222. end
  1223. function test_home_scrolls_up_in_wrapped_line()
  1224. -- display lines starting from second screen line of a line
  1225. App.screen.init{width=Editor_state.left+30, height=60}
  1226. Editor_state = edit.initialize_test_state()
  1227. Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
  1228. Text.redraw_all(Editor_state)
  1229. Editor_state.screen_top1 = {line=3, pos=5}
  1230. Editor_state.screen_bottom1 = {}
  1231. -- cursor is at top of screen
  1232. Editor_state.cursor1 = {line=3, pos=5}
  1233. edit.draw(Editor_state)
  1234. local y = Editor_state.top
  1235. App.screen.check(y, 'jkl', 'baseline/screen:1')
  1236. y = y + Editor_state.line_height
  1237. App.screen.check(y, 'mno', 'baseline/screen:2')
  1238. -- after hitting home the screen scrolls up to first screen line
  1239. edit.run_after_keychord(Editor_state, 'home', 'home')
  1240. y = Editor_state.top
  1241. App.screen.check(y, 'ghi ', 'screen:1')
  1242. y = y + Editor_state.line_height
  1243. App.screen.check(y, 'jkl', 'screen:2')
  1244. y = y + Editor_state.line_height
  1245. App.screen.check(y, 'mno', 'screen:3')
  1246. check_eq(Editor_state.screen_top1.line, 3, 'screen_top:line')
  1247. check_eq(Editor_state.screen_top1.pos, 1, 'screen_top:pos')
  1248. check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
  1249. check_eq(Editor_state.cursor1.pos, 1, 'cursor:pos')
  1250. end
  1251. function test_end_scrolls_down_in_wrapped_line()
  1252. -- display the first three lines with the cursor on the bottom line
  1253. App.screen.init{width=Editor_state.left+30, height=60}
  1254. Editor_state = edit.initialize_test_state()
  1255. Editor_state.lines = load_array{'abc', 'def', 'ghi jkl', 'mno'}
  1256. Text.redraw_all(Editor_state)
  1257. Editor_state.screen_top1 = {line=1, pos=1}
  1258. Editor_state.screen_bottom1 = {}
  1259. -- cursor is at bottom right of screen
  1260. Editor_state.cursor1 = {line=3, pos=5}
  1261. edit.draw(Editor_state)
  1262. local y = Editor_state.top
  1263. App.screen.check(y, 'abc', 'baseline/screen:1')
  1264. y = y + Editor_state.line_height
  1265. App.screen.check(y, 'def', 'baseline/screen:2')
  1266. y = y + Editor_state.line_height
  1267. App.screen.check(y, 'ghi ', 'baseline/screen:3') -- line wrapping includes trailing whitespace
  1268. -- after hitting end the screen scrolls down by one line
  1269. edit.run_after_keychord(Editor_state, 'end', 'end')
  1270. check_eq(Editor_state.screen_top1.line, 2, 'screen_top')
  1271. check_eq(Editor_state.cursor1.line, 3, 'cursor:line')
  1272. check_eq(Editor_state.cursor1.pos, 8, 'cursor:pos')
  1273. y = Editor_state.top
  1274. App.screen.check(y, 'def', 'screen:1')
  1275. y = y + Editor_state.line_height
  1276. App.screen.check(y, 'ghi ', 'screen:2')
  1277. y = y + Editor_state.line_height
  1278. App.screen.check(y, 'jkl', 'screen:3')
  1279. end
  1280. function test_search()
  1281. App.screen.init{width=120, height=60}
  1282. Editor_state = edit.initialize_test_state()
  1283. Editor_state.lines = load_array{'abc', 'def', 'ghi', '’deg'} -- contains unicode quote in final line
  1284. Text.redraw_all(Editor_state)
  1285. Editor_state.cursor1 = {line=1, pos=1}
  1286. Editor_state.screen_top1 = {line=1, pos=1}
  1287. Editor_state.screen_bottom1 = {}
  1288. edit.draw(Editor_state)
  1289. -- search for a string
  1290. edit.run_after_keychord(Editor_state, 'C-f', 'f')
  1291. edit.run_after_text_input(Editor_state, 'd')
  1292. edit.run_after_keychord(Editor_state, 'return', 'return')
  1293. check_eq(Editor_state.cursor1.line, 2, '1/cursor:line')
  1294. check_eq(Editor_state.cursor1.pos, 1, '1/cursor:pos')
  1295. -- reset cursor
  1296. Editor_state.cursor1 = {line=1, pos=1}
  1297. Editor_state.screen_top1 = {line=1, pos=1}
  1298. -- search for second occurrence
  1299. edit.run_after_keychord(Editor_state, 'C-f', 'f')
  1300. edit.run_after_text_input(Editor_state, 'de')
  1301. edit.run_after_keychord(Editor_state, 'down', 'down')
  1302. edit.run_after_keychord(Editor_state, 'return', 'return')
  1303. check_eq(Editor_state.cursor1.line, 4, '2/cursor:line')
  1304. check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
  1305. end
  1306. function test_search_upwards()
  1307. App.screen.init{width=120, height=60}
  1308. Editor_state = edit.initialize_test_state()
  1309. Editor_state.lines = load_array{'’abc', 'abd'} -- contains unicode quote
  1310. Text.redraw_all(Editor_state)
  1311. Editor_state.cursor1 = {line=2, pos=1}
  1312. Editor_state.screen_top1 = {line=1, pos=1}
  1313. Editor_state.screen_bottom1 = {}
  1314. edit.draw(Editor_state)
  1315. -- search for a string
  1316. edit.run_after_keychord(Editor_state, 'C-f', 'f')
  1317. edit.run_after_text_input(Editor_state, 'a')
  1318. -- search for previous occurrence
  1319. edit.run_after_keychord(Editor_state, 'up', 'up')
  1320. check_eq(Editor_state.cursor1.line, 1, '2/cursor:line')
  1321. check_eq(Editor_state.cursor1.pos, 2, '2/cursor:pos')
  1322. end
  1323. function test_search_wrap()
  1324. App.screen.init{width=120, height=60}
  1325. Editor_state = edit.initialize_test_state()
  1326. Editor_state.lines = load_array{'’abc', 'def'} -- contains unicode quote in first line
  1327. Text.redraw_all(Editor_state)
  1328. Editor_state.cursor1 = {line=2, pos=1}
  1329. Editor_state.screen_top1 = {line=1, pos=1}
  1330. Editor_state.screen_bottom1 = {}
  1331. edit.draw(Editor_state)
  1332. -- search for a string
  1333. edit.run_after_keychord(Editor_state, 'C-f', 'f')
  1334. edit.run_after_text_input(Editor_state, 'a')
  1335. edit.run_after_keychord(Editor_state, 'return', 'return')
  1336. -- cursor wraps
  1337. check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
  1338. check_eq(Editor_state.cursor1.pos, 2, '1/cursor:pos')
  1339. end
  1340. function test_search_wrap_upwards()
  1341. App.screen.init{width=120, height=60}
  1342. Editor_state = edit.initialize_test_state()
  1343. Editor_state.lines = load_array{'abc ’abd'} -- contains unicode quote
  1344. Text.redraw_all(Editor_state)
  1345. Editor_state.cursor1 = {line=1, pos=1}
  1346. Editor_state.screen_top1 = {line=1, pos=1}
  1347. Editor_state.screen_bottom1 = {}
  1348. edit.draw(Editor_state)
  1349. -- search upwards for a string
  1350. edit.run_after_keychord(Editor_state, 'C-f', 'f')
  1351. edit.run_after_text_input(Editor_state, 'a')
  1352. edit.run_after_keychord(Editor_state, 'up', 'up')
  1353. -- cursor wraps
  1354. check_eq(Editor_state.cursor1.line, 1, '1/cursor:line')
  1355. check_eq(Editor_state.cursor1.pos, 6, '1/cursor:pos')
  1356. end