marks_spec.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. -- ShaDa marks saving/reading support
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local meths, curwinmeths, curbufmeths, nvim_command, funcs, eq =
  4. helpers.meths, helpers.curwinmeths, helpers.curbufmeths, helpers.command,
  5. helpers.funcs, helpers.eq
  6. local exc_exec, exec_capture = helpers.exc_exec, helpers.exec_capture
  7. local shada_helpers = require('test.functional.shada.helpers')
  8. local reset, clear = shada_helpers.reset, shada_helpers.clear
  9. local nvim_current_line = function()
  10. return curwinmeths.get_cursor()[1]
  11. end
  12. describe('ShaDa support code', function()
  13. local testfilename = 'Xtestfile-functional-shada-marks'
  14. local testfilename_2 = 'Xtestfile-functional-shada-marks-2'
  15. local non_existent_testfilename = testfilename .. '.nonexistent'
  16. before_each(function()
  17. reset()
  18. os.remove(non_existent_testfilename)
  19. local fd = io.open(testfilename, 'w')
  20. fd:write('test\n')
  21. fd:write('test2\n')
  22. fd:close()
  23. fd = io.open(testfilename_2, 'w')
  24. fd:write('test3\n')
  25. fd:write('test4\n')
  26. fd:close()
  27. end)
  28. after_each(function()
  29. clear()
  30. os.remove(testfilename)
  31. os.remove(testfilename_2)
  32. end)
  33. it('is able to dump and read back global mark', function()
  34. nvim_command('edit ' .. testfilename)
  35. nvim_command('mark A')
  36. nvim_command('2')
  37. nvim_command('kB')
  38. nvim_command('wshada')
  39. reset()
  40. nvim_command('rshada')
  41. nvim_command('normal! `A')
  42. eq(testfilename, funcs.fnamemodify(curbufmeths.get_name(), ':t'))
  43. eq(1, nvim_current_line())
  44. nvim_command('normal! `B')
  45. eq(2, nvim_current_line())
  46. end)
  47. it('does not dump global mark with `f0` in shada', function()
  48. nvim_command('set shada+=f0')
  49. nvim_command('edit ' .. testfilename)
  50. nvim_command('mark A')
  51. nvim_command('2')
  52. nvim_command('kB')
  53. nvim_command('wshada')
  54. reset()
  55. nvim_command('language C')
  56. eq('Vim(normal):E20: Mark not set', exc_exec('normal! `A'))
  57. end)
  58. it('does read back global mark even with `\'0` and `f0` in shada', function()
  59. nvim_command('edit ' .. testfilename)
  60. nvim_command('mark A')
  61. nvim_command('2')
  62. nvim_command('kB')
  63. nvim_command('wshada')
  64. reset('set shada=\'0,f0')
  65. nvim_command('language C')
  66. nvim_command('normal! `A')
  67. eq(testfilename, funcs.fnamemodify(curbufmeths.get_name(), ':t'))
  68. eq(1, nvim_current_line())
  69. end)
  70. it('is able to dump and read back local mark', function()
  71. nvim_command('edit ' .. testfilename)
  72. nvim_command('mark a')
  73. nvim_command('2')
  74. nvim_command('kb')
  75. nvim_command('qall')
  76. reset()
  77. nvim_command('edit ' .. testfilename)
  78. nvim_command('normal! `a')
  79. eq(testfilename, funcs.fnamemodify(curbufmeths.get_name(), ':t'))
  80. eq(1, nvim_current_line())
  81. nvim_command('normal! `b')
  82. eq(2, nvim_current_line())
  83. end)
  84. it('is able to dump and read back mark "', function()
  85. nvim_command('edit ' .. testfilename)
  86. nvim_command('2')
  87. nvim_command('qall')
  88. reset()
  89. nvim_command('edit ' .. testfilename)
  90. nvim_command('normal! `"')
  91. eq(2, nvim_current_line())
  92. end)
  93. it('is able to dump and read back mark " from a closed tab', function()
  94. nvim_command('edit ' .. testfilename)
  95. nvim_command('tabedit ' .. testfilename_2)
  96. nvim_command('2')
  97. nvim_command('q!')
  98. nvim_command('qall')
  99. reset()
  100. nvim_command('edit ' .. testfilename_2)
  101. nvim_command('normal! `"')
  102. eq(2, nvim_current_line())
  103. end)
  104. it('is able to populate v:oldfiles', function()
  105. nvim_command('edit ' .. testfilename)
  106. local tf_full = curbufmeths.get_name()
  107. nvim_command('edit ' .. testfilename_2)
  108. local tf_full_2 = curbufmeths.get_name()
  109. nvim_command('qall')
  110. reset()
  111. local oldfiles = meths.get_vvar('oldfiles')
  112. table.sort(oldfiles)
  113. eq(2, #oldfiles)
  114. eq(testfilename, oldfiles[1]:sub(-#testfilename))
  115. eq(testfilename_2, oldfiles[2]:sub(-#testfilename_2))
  116. eq(tf_full, oldfiles[1])
  117. eq(tf_full_2, oldfiles[2])
  118. nvim_command('rshada!')
  119. oldfiles = meths.get_vvar('oldfiles')
  120. table.sort(oldfiles)
  121. eq(2, #oldfiles)
  122. eq(testfilename, oldfiles[1]:sub(-#testfilename))
  123. eq(testfilename_2, oldfiles[2]:sub(-#testfilename_2))
  124. eq(tf_full, oldfiles[1])
  125. eq(tf_full_2, oldfiles[2])
  126. end)
  127. it('is able to dump and restore jump list', function()
  128. nvim_command('edit ' .. testfilename_2)
  129. nvim_command('normal! G')
  130. nvim_command('normal! gg')
  131. nvim_command('edit ' .. testfilename)
  132. nvim_command('normal! G')
  133. nvim_command('normal! gg')
  134. nvim_command('enew')
  135. nvim_command('normal! gg')
  136. local saved = exec_capture('jumps')
  137. nvim_command('qall')
  138. reset()
  139. eq(saved, exec_capture('jumps'))
  140. end)
  141. it('when dumping jump list also dumps current position', function()
  142. nvim_command('edit ' .. testfilename)
  143. nvim_command('normal! G')
  144. nvim_command('split ' .. testfilename_2)
  145. nvim_command('normal! G')
  146. nvim_command('wshada')
  147. nvim_command('quit')
  148. nvim_command('rshada')
  149. nvim_command('normal! \15') -- <C-o>
  150. eq(testfilename_2, funcs.bufname('%'))
  151. eq({2, 0}, curwinmeths.get_cursor())
  152. end)
  153. it('is able to dump and restore jump list with different times (slow!)',
  154. function()
  155. nvim_command('edit ' .. testfilename_2)
  156. nvim_command('sleep 2')
  157. nvim_command('normal! G')
  158. nvim_command('sleep 2')
  159. nvim_command('normal! gg')
  160. nvim_command('sleep 2')
  161. nvim_command('edit ' .. testfilename)
  162. nvim_command('sleep 2')
  163. nvim_command('normal! G')
  164. nvim_command('sleep 2')
  165. nvim_command('normal! gg')
  166. nvim_command('qall')
  167. reset()
  168. nvim_command('redraw')
  169. nvim_command('edit ' .. testfilename)
  170. eq(testfilename, funcs.bufname('%'))
  171. eq(1, nvim_current_line())
  172. nvim_command('execute "normal! \\<C-o>"')
  173. eq(testfilename, funcs.bufname('%'))
  174. eq(2, nvim_current_line())
  175. nvim_command('execute "normal! \\<C-o>"')
  176. eq(testfilename_2, funcs.bufname('%'))
  177. eq(1, nvim_current_line())
  178. nvim_command('execute "normal! \\<C-o>"')
  179. eq(testfilename_2, funcs.bufname('%'))
  180. eq(2, nvim_current_line())
  181. nvim_command('execute "normal! \\<C-o>"')
  182. eq(testfilename_2, funcs.bufname('%'))
  183. eq(2, nvim_current_line())
  184. end)
  185. it('is able to dump and restore change list', function()
  186. nvim_command('edit ' .. testfilename)
  187. nvim_command('normal! Gra')
  188. nvim_command('normal! ggrb')
  189. nvim_command('qall!')
  190. reset()
  191. nvim_command('edit ' .. testfilename)
  192. nvim_command('normal! Gg;')
  193. -- Note: without “sync” “commands” test has good changes to fail for unknown
  194. -- reason (in first eq expected 1 is compared with 2). Any command inserted
  195. -- causes this to work properly.
  196. nvim_command('" sync')
  197. eq(1, nvim_current_line())
  198. nvim_command('normal! g;')
  199. nvim_command('" sync 2')
  200. eq(2, nvim_current_line())
  201. end)
  202. -- -c temporary sets lnum to zero to make `+/pat` work, so calling setpcmark()
  203. -- during -c used to add item with zero lnum to jump list.
  204. it('does not create incorrect file for non-existent buffers when writing from -c',
  205. function()
  206. local argv = helpers.new_argv{
  207. args_rm={
  208. '-i',
  209. '--embed', -- no --embed
  210. },
  211. args={
  212. '-i', meths.get_var('tmpname'), -- Use same shada file as parent.
  213. '--cmd', 'silent edit '..non_existent_testfilename,
  214. '-c', 'qall'},
  215. }
  216. eq('', funcs.system(argv))
  217. eq(0, exc_exec('rshada'))
  218. end)
  219. it('does not create incorrect file for non-existent buffers opened from -c',
  220. function()
  221. local argv = helpers.new_argv{
  222. args_rm={
  223. '-i',
  224. '--embed', -- no --embed
  225. },
  226. args={
  227. '-i', meths.get_var('tmpname'), -- Use same shada file as parent.
  228. '-c', 'silent edit '..non_existent_testfilename,
  229. '-c', 'autocmd VimEnter * qall'},
  230. }
  231. eq('', funcs.system(argv))
  232. eq(0, exc_exec('rshada'))
  233. end)
  234. end)