history_spec.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. -- ShaDa history saving/reading support
  2. local t = require('test.testutil')
  3. local n = require('test.functional.testnvim')()
  4. local t_shada = require('test.functional.shada.testutil')
  5. local nvim_command, fn, api, nvim_feed, eq = n.command, n.fn, n.api, n.feed, t.eq
  6. local assert_alive = n.assert_alive
  7. local expect_exit = n.expect_exit
  8. local reset, clear = t_shada.reset, t_shada.clear
  9. describe('ShaDa support code', function()
  10. before_each(reset)
  11. after_each(clear)
  12. it('is able to dump and read back command-line history', function()
  13. nvim_command("set shada='0")
  14. nvim_feed(':" Test\n')
  15. nvim_command('wshada')
  16. reset()
  17. nvim_command("set shada='0")
  18. nvim_command('rshada')
  19. eq('" Test', fn.histget(':', -1))
  20. end)
  21. it('is able to dump and read back 2 items in command-line history', function()
  22. nvim_command("set shada='0 history=2")
  23. nvim_feed(':" Test\n')
  24. nvim_feed(':" Test 2\n')
  25. expect_exit(nvim_command, 'qall')
  26. reset()
  27. nvim_command("set shada='0 history=2")
  28. nvim_command('rshada')
  29. eq('" Test 2', fn.histget(':', -1))
  30. eq('" Test', fn.histget(':', -2))
  31. end)
  32. it('respects &history when dumping', function()
  33. nvim_command("set shada='0 history=1")
  34. nvim_feed(':" Test\n')
  35. nvim_feed(':" Test 2\n')
  36. nvim_command('wshada')
  37. reset()
  38. nvim_command("set shada='0 history=2")
  39. nvim_command('rshada')
  40. eq('" Test 2', fn.histget(':', -1))
  41. eq('', fn.histget(':', -2))
  42. end)
  43. it('respects &history when loading', function()
  44. nvim_command("set shada='0 history=2")
  45. nvim_feed(':" Test\n')
  46. nvim_feed(':" Test 2\n')
  47. nvim_command('wshada')
  48. reset()
  49. nvim_command("set shada='0 history=1")
  50. nvim_command('rshada')
  51. eq('" Test 2', fn.histget(':', -1))
  52. eq('', fn.histget(':', -2))
  53. end)
  54. it('dumps only requested amount of command-line history items', function()
  55. nvim_command("set shada='0,:1")
  56. nvim_feed(':" Test\n')
  57. nvim_feed(':" Test 2\n')
  58. nvim_command('wshada')
  59. -- Regression test: :wshada should not alter or free history.
  60. eq('" Test 2', fn.histget(':', -1))
  61. eq('" Test', fn.histget(':', -2))
  62. reset()
  63. nvim_command("set shada='0")
  64. nvim_command('rshada')
  65. eq('" Test 2', fn.histget(':', -1))
  66. eq('', fn.histget(':', -2))
  67. end)
  68. it('does not respect number in &shada when loading history', function()
  69. nvim_command("set shada='0")
  70. nvim_feed(':" Test\n')
  71. nvim_feed(':" Test 2\n')
  72. nvim_command('wshada')
  73. reset()
  74. nvim_command("set shada='0,:1")
  75. nvim_command('rshada')
  76. eq('" Test 2', fn.histget(':', -1))
  77. eq('" Test', fn.histget(':', -2))
  78. end)
  79. it('dumps and loads all kinds of histories', function()
  80. nvim_command('debuggreedy')
  81. nvim_feed(':debug echo "Test"\n" Test 2\nc\n') -- Debug history.
  82. nvim_feed(':call input("")\nTest 2\n') -- Input history.
  83. nvim_feed('"="Test"\nyy') -- Expression history.
  84. nvim_feed('/Test\n') -- Search history
  85. nvim_feed(':" Test\n') -- Command-line history
  86. nvim_command('0debuggreedy')
  87. nvim_command('wshada')
  88. reset()
  89. nvim_command('rshada')
  90. eq('" Test', fn.histget(':', -1))
  91. eq('Test', fn.histget('/', -1))
  92. eq('"Test"', fn.histget('=', -1))
  93. eq('Test 2', fn.histget('@', -1))
  94. eq('c', fn.histget('>', -1))
  95. end)
  96. it('dumps and loads last search pattern with offset', function()
  97. api.nvim_set_option_value('wrapscan', false, {})
  98. fn.setline('.', { 'foo', 'bar--' })
  99. nvim_feed('gg0/a/e+1\n')
  100. eq({ 0, 2, 3, 0 }, fn.getpos('.'))
  101. nvim_command('wshada')
  102. reset()
  103. api.nvim_set_option_value('wrapscan', false, {})
  104. fn.setline('.', { 'foo', 'bar--' })
  105. nvim_feed('gg0n')
  106. eq({ 0, 2, 3, 0 }, fn.getpos('.'))
  107. eq(1, api.nvim_get_vvar('searchforward'))
  108. -- Autocommands shouldn't cause search pattern to change
  109. nvim_command('autocmd User * :')
  110. nvim_command('doautocmd User')
  111. nvim_feed('gg0n')
  112. eq({ 0, 2, 3, 0 }, fn.getpos('.'))
  113. eq(1, api.nvim_get_vvar('searchforward'))
  114. end)
  115. it('dumps and loads last search pattern with offset and backward direction', function()
  116. api.nvim_set_option_value('wrapscan', false, {})
  117. fn.setline('.', { 'foo', 'bar--' })
  118. nvim_feed('G$?a?e+1\n')
  119. eq({ 0, 2, 3, 0 }, fn.getpos('.'))
  120. nvim_command('wshada')
  121. reset()
  122. api.nvim_set_option_value('wrapscan', false, {})
  123. fn.setline('.', { 'foo', 'bar--' })
  124. nvim_feed('G$n')
  125. eq({ 0, 2, 3, 0 }, fn.getpos('.'))
  126. eq(0, api.nvim_get_vvar('searchforward'))
  127. -- Autocommands shouldn't cause search pattern to change
  128. nvim_command('autocmd User * :')
  129. nvim_command('doautocmd User')
  130. nvim_feed('G$n')
  131. eq({ 0, 2, 3, 0 }, fn.getpos('.'))
  132. eq(0, api.nvim_get_vvar('searchforward'))
  133. end)
  134. it('saves v:hlsearch=1', function()
  135. nvim_command('set hlsearch shada-=h')
  136. nvim_feed('/test\n')
  137. eq(1, api.nvim_get_vvar('hlsearch'))
  138. expect_exit(nvim_command, 'qall')
  139. reset()
  140. eq(1, api.nvim_get_vvar('hlsearch'))
  141. end)
  142. it('saves v:hlsearch=0 with :nohl', function()
  143. nvim_command('set hlsearch shada-=h')
  144. nvim_feed('/test\n')
  145. nvim_command('nohlsearch')
  146. expect_exit(nvim_command, 'qall')
  147. reset()
  148. eq(0, api.nvim_get_vvar('hlsearch'))
  149. end)
  150. it('saves v:hlsearch=0 with default &shada', function()
  151. nvim_command('set hlsearch')
  152. nvim_feed('/test\n')
  153. eq(1, api.nvim_get_vvar('hlsearch'))
  154. expect_exit(nvim_command, 'qall')
  155. reset()
  156. eq(0, api.nvim_get_vvar('hlsearch'))
  157. end)
  158. it('dumps and loads last substitute pattern and replacement string', function()
  159. fn.setline('.', { 'foo', 'bar' })
  160. nvim_command('%s/f/g/g')
  161. eq('goo', fn.getline(1))
  162. nvim_command('wshada')
  163. reset()
  164. fn.setline('.', { 'foo', 'bar' })
  165. nvim_command('&')
  166. eq('goo', fn.getline(1))
  167. end)
  168. it('dumps and loads history with UTF-8 characters', function()
  169. reset()
  170. nvim_feed(':echo "«"\n')
  171. expect_exit(nvim_command, 'qall')
  172. reset()
  173. eq('echo "«"', fn.histget(':', -1))
  174. end)
  175. it('dumps and loads replacement with UTF-8 characters', function()
  176. nvim_command('substitute/./«/ge')
  177. expect_exit(nvim_command, 'qall!')
  178. reset()
  179. fn.setline('.', { '.' })
  180. nvim_command('&')
  181. eq('«', fn.getline('.'))
  182. end)
  183. it('dumps and loads substitute pattern with UTF-8 characters', function()
  184. nvim_command('substitute/«/./ge')
  185. expect_exit(nvim_command, 'qall!')
  186. reset()
  187. fn.setline('.', { '«\171' })
  188. nvim_command('&')
  189. eq('.\171', fn.getline('.'))
  190. end)
  191. it('dumps and loads search pattern with UTF-8 characters', function()
  192. nvim_command('silent! /«/')
  193. nvim_command('set shada+=/0')
  194. expect_exit(nvim_command, 'qall!')
  195. reset()
  196. fn.setline('.', { '\171«' })
  197. nvim_command('~&')
  198. eq('\171', fn.getline('.'))
  199. eq('', fn.histget('/', -1))
  200. end)
  201. it('dumps and loads search pattern with 8-bit single-byte', function()
  202. -- \171 is U+00AB LEFT-POINTING DOUBLE ANGLE QUOTATION MARK in latin1
  203. nvim_command('silent! /\171/')
  204. nvim_command('set shada+=/0')
  205. expect_exit(nvim_command, 'qall!')
  206. reset()
  207. fn.setline('.', { '\171«' })
  208. nvim_command('~&')
  209. eq('«', fn.getline('.'))
  210. eq('', fn.histget('/', -1))
  211. end)
  212. it('does not crash when dumping last search pattern (#10945)', function()
  213. nvim_command('edit Xtest-functional-shada-history_spec')
  214. -- Save jump list
  215. nvim_command('wshada')
  216. -- Wipe out buffer list (jump list entry gets removed)
  217. nvim_command('%bwipeout')
  218. -- Restore jump list
  219. nvim_command('rshada')
  220. nvim_command('silent! /pat/')
  221. nvim_command('au BufNew * echo')
  222. nvim_command('wshada')
  223. end)
  224. it('does not crash when number of history save to zero (#11497)', function()
  225. nvim_command("set shada='10")
  226. nvim_feed(':" Test\n')
  227. nvim_command('wshada')
  228. nvim_command("set shada='10,:0")
  229. nvim_command('wshada')
  230. assert_alive()
  231. end)
  232. end)