test_gn.vim 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. " Test for gn command
  2. func Test_gn_command()
  3. noautocmd new
  4. " replace a single char by itself quoted:
  5. call setline('.', 'abc x def x ghi x jkl')
  6. let @/ = 'x'
  7. exe "norm! cgn'x'\<esc>.."
  8. call assert_equal("abc 'x' def 'x' ghi 'x' jkl", getline('.'))
  9. sil! %d_
  10. " simple search match
  11. call setline('.', 'foobar')
  12. let @/ = 'foobar'
  13. exe "norm! gncsearchmatch"
  14. call assert_equal('searchmatch', getline('.'))
  15. sil! %d _
  16. " replace a multi-line match
  17. call setline('.', ['', 'one', 'two'])
  18. let @/ = 'one\_s*two\_s'
  19. exe "norm! gnceins\<CR>zwei"
  20. call assert_equal(['','eins','zwei'], getline(1,'$'))
  21. sil! %d _
  22. " test count argument
  23. call setline('.', ['', 'abcdx | abcdx | abcdx'])
  24. let @/ = '[a]bcdx'
  25. exe "norm! 2gnd"
  26. call assert_equal(['','abcdx | | abcdx'], getline(1,'$'))
  27. sil! %d _
  28. " join lines
  29. call setline('.', ['join ', 'lines'])
  30. let @/ = '$'
  31. exe "norm! 0gnd"
  32. call assert_equal(['join lines'], getline(1,'$'))
  33. sil! %d _
  34. " zero-width match
  35. call setline('.', ['', 'zero width pattern'])
  36. let @/ = '\>\zs'
  37. exe "norm! 0gnd"
  38. call assert_equal(['', 'zerowidth pattern'], getline(1,'$'))
  39. sil! %d _
  40. " delete first and last chars
  41. call setline('.', ['delete first and last chars'])
  42. let @/ = '^'
  43. exe "norm! 0gnd$"
  44. let @/ = '\zs'
  45. exe "norm! gnd"
  46. call assert_equal(['elete first and last char'], getline(1,'$'))
  47. sil! %d _
  48. " using visual mode
  49. call setline('.', ['', 'uniquepattern uniquepattern'])
  50. exe "norm! /[u]niquepattern/s\<cr>vlgnd"
  51. call assert_equal(['', ' uniquepattern'], getline(1,'$'))
  52. sil! %d _
  53. " backwards search
  54. call setline('.', ['my very excellent mother just served us nachos'])
  55. let @/ = 'mother'
  56. exe "norm! $cgNmongoose"
  57. call assert_equal(['my very excellent mongoose just served us nachos'], getline(1,'$'))
  58. sil! %d _
  59. " search for single char
  60. call setline('.', ['','for (i=0; i<=10; i++)'])
  61. let @/ = 'i'
  62. exe "norm! cgnj"
  63. call assert_equal(['','for (j=0; i<=10; i++)'], getline(1,'$'))
  64. sil! %d _
  65. " search hex char
  66. call setline('.', ['','Y'])
  67. set noignorecase
  68. let @/ = '\%x59'
  69. exe "norm! gnd"
  70. call assert_equal(['',''], getline(1,'$'))
  71. sil! %d _
  72. " test repeating gdn
  73. call setline('.', ['', '1', 'Johnny', '2', 'Johnny', '3'])
  74. let @/ = 'Johnny'
  75. exe "norm! dgn."
  76. call assert_equal(['','1', '', '2', '', '3'], getline(1,'$'))
  77. sil! %d _
  78. " test repeating gUgn
  79. call setline('.', ['', '1', 'Depp', '2', 'Depp', '3'])
  80. let @/ = 'Depp'
  81. exe "norm! gUgn."
  82. call assert_equal(['', '1', 'DEPP', '2', 'DEPP', '3'], getline(1,'$'))
  83. sil! %d _
  84. " test using look-ahead assertions
  85. call setline('.', ['a:10', '', 'a:1', '', 'a:20'])
  86. let @/ = 'a:0\@!\zs\d\+'
  87. exe "norm! 2nygno\<esc>p"
  88. call assert_equal(['a:10', '', 'a:1', '1', '', 'a:20'], getline(1,'$'))
  89. sil! %d _
  90. " test using nowrapscan
  91. set nowrapscan
  92. call setline(1, 'foo bar baz')
  93. exe "norm! /bar/e\<cr>"
  94. exe "norm! gnd"
  95. call assert_equal(['foo baz'], getline(1,'$'))
  96. sil! %d_
  97. " search upwards with nowrapscan set
  98. call setline('.', ['foo', 'bar', 'foo', 'baz'])
  99. set nowrapscan
  100. let @/ = 'foo'
  101. $
  102. norm! dgN
  103. call assert_equal(['foo', 'bar', '', 'baz'], getline(1,'$'))
  104. sil! %d_
  105. " search using the \zs atom
  106. call setline(1, [' nnoremap', '' , 'nnoremap'])
  107. set wrapscan&vim
  108. let @/ = '\_s\zsnnoremap'
  109. $
  110. norm! cgnmatch
  111. call assert_equal([' nnoremap', '', 'match'], getline(1,'$'))
  112. sil! %d_
  113. " make sure it works correctly for one-char wide search items
  114. call setline('.', ['abcdefghi'])
  115. let @/ = 'a'
  116. exe "norm! 0fhvhhgNgU"
  117. call assert_equal(['ABCDEFGHi'], getline(1,'$'))
  118. call setline('.', ['abcdefghi'])
  119. let @/ = 'b'
  120. " this gn wraps around the end of the file
  121. exe "norm! 0fhvhhgngU"
  122. call assert_equal(['aBCDEFGHi'], getline(1,'$'))
  123. sil! %d _
  124. call setline('.', ['abcdefghi'])
  125. let @/ = 'f'
  126. exe "norm! 0vllgngU"
  127. call assert_equal(['ABCDEFghi'], getline(1,'$'))
  128. sil! %d _
  129. call setline('.', ['12345678'])
  130. let @/ = '5'
  131. norm! gg0f7vhhhhgnd
  132. call assert_equal(['12348'], getline(1,'$'))
  133. sil! %d _
  134. call setline('.', ['12345678'])
  135. let @/ = '5'
  136. norm! gg0f2vf7gNd
  137. call assert_equal(['1678'], getline(1,'$'))
  138. sil! %d _
  139. set wrapscan&vim
  140. " Without 'wrapscan', in visual mode, running gn without a match should fail
  141. " but the visual mode should be kept.
  142. set nowrapscan
  143. call setline('.', 'one two')
  144. let @/ = 'one'
  145. call assert_beeps('normal 0wvlgn')
  146. exe "normal y"
  147. call assert_equal('tw', @")
  148. " with exclusive selection, run gn and gN
  149. set selection=exclusive
  150. normal 0gny
  151. call assert_equal('one', @")
  152. normal 0wgNy
  153. call assert_equal('one', @")
  154. set selection&
  155. endfunc
  156. func Test_gN_repeat()
  157. new
  158. call setline(1, 'this list is a list with a list of a list.')
  159. /list
  160. normal $gNgNgNx
  161. call assert_equal('list with a list of a list', @")
  162. bwipe!
  163. endfunc
  164. func Test_gN_then_gn()
  165. new
  166. call setline(1, 'this list is a list with a list of a last.')
  167. /l.st
  168. normal $gNgNgnx
  169. call assert_equal('last', @")
  170. call setline(1, 'this list is a list with a lust of a last.')
  171. /l.st
  172. normal $gNgNgNgnx
  173. call assert_equal('lust of a last', @")
  174. bwipe!
  175. endfunc
  176. func Test_gn_multi_line()
  177. new
  178. call setline(1, [
  179. \ 'func Tm1()',
  180. \ ' echo "one"',
  181. \ 'endfunc',
  182. \ 'func Tm2()',
  183. \ ' echo "two"',
  184. \ 'endfunc',
  185. \ 'func Tm3()',
  186. \ ' echo "three"',
  187. \ 'endfunc',
  188. \])
  189. /\v^func Tm\d\(\)\n.*\zs".*"\ze$
  190. normal jgnrx
  191. call assert_equal(' echo xxxxx', getline(5))
  192. bwipe!
  193. endfunc
  194. " vim: shiftwidth=2 sts=2 expandtab