history_spec.lua 7.3 KB

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