test_global.vim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. " Test for :global and :vglobal
  2. source check.vim
  3. source term_util.vim
  4. func Test_yank_put_clipboard()
  5. new
  6. call setline(1, ['a', 'b', 'c'])
  7. set clipboard=unnamed
  8. g/^/normal yyp
  9. call assert_equal(['a', 'a', 'b', 'b', 'c', 'c'], getline(1, 6))
  10. set clipboard=unnamed,unnamedplus
  11. call setline(1, ['a', 'b', 'c'])
  12. g/^/normal yyp
  13. call assert_equal(['a', 'a', 'b', 'b', 'c', 'c'], getline(1, 6))
  14. set clipboard&
  15. bwipe!
  16. endfunc
  17. func Test_global_set_clipboard()
  18. CheckFeature clipboard_working
  19. new
  20. set clipboard=unnamedplus
  21. let @+='clipboard' | g/^/set cb= | let @" = 'unnamed' | put
  22. call assert_equal(['','unnamed'], getline(1, '$'))
  23. set clipboard&
  24. bwipe!
  25. endfunc
  26. func Test_nested_global()
  27. new
  28. call setline(1, ['nothing', 'found', 'found bad', 'bad'])
  29. call assert_fails('g/found/3v/bad/s/^/++/', 'E147')
  30. g/found/v/bad/s/^/++/
  31. call assert_equal(['nothing', '++found', 'found bad', 'bad'], getline(1, 4))
  32. bwipe!
  33. endfunc
  34. func Test_global_error()
  35. call assert_fails('g\\a', 'E10:')
  36. call assert_fails('g', 'E148:')
  37. call assert_fails('g/\(/y', 'E54:')
  38. endfunc
  39. " Test for printing lines using :g with different search patterns
  40. func Test_global_print()
  41. new
  42. call setline(1, ['foo', 'bar', 'foo', 'foo'])
  43. let @/ = 'foo'
  44. let t = execute("g/")->trim()->split("\n")
  45. call assert_equal(['foo', 'foo', 'foo'], t)
  46. " Test for Vi compatible patterns
  47. let @/ = 'bar'
  48. let t = execute('g\/')->trim()->split("\n")
  49. call assert_equal(['bar'], t)
  50. normal gg
  51. s/foo/foo/
  52. let t = execute('g\&')->trim()->split("\n")
  53. call assert_equal(['foo', 'foo', 'foo'], t)
  54. let @/ = 'bar'
  55. let t = execute('g?')->trim()->split("\n")
  56. call assert_equal(['bar'], t)
  57. " Test for the 'Pattern found in every line' message
  58. let v:statusmsg = ''
  59. v/foo\|bar/p
  60. call assert_notequal('', v:statusmsg)
  61. close!
  62. endfunc
  63. func Test_global_empty_pattern()
  64. " populate history
  65. silent g/hello/
  66. redir @a
  67. g//
  68. redir END
  69. call assert_match('Pattern not found: hello', @a)
  70. " ^~~~~ this was previously empty
  71. endfunc
  72. " Test for global command with newline character
  73. func Test_global_newline()
  74. new
  75. call setline(1, ['foo'])
  76. exe "g/foo/s/f/h/\<NL>s/o$/w/"
  77. call assert_equal('how', getline(1))
  78. call setline(1, ["foo\<NL>bar"])
  79. exe "g/foo/s/foo\\\<NL>bar/xyz/"
  80. call assert_equal('xyz', getline(1))
  81. close!
  82. endfunc
  83. " Test :g with ? as delimiter.
  84. func Test_global_question_delimiter()
  85. new
  86. call setline(1, ['aaaaa', 'b?bbb', 'ccccc', 'ddd?d', 'eeeee'])
  87. g?\??delete
  88. call assert_equal(['aaaaa', 'ccccc', 'eeeee'], getline(1, '$'))
  89. bwipe!
  90. endfunc
  91. func Test_global_wrong_delimiter()
  92. call assert_fails('g x^bxd', 'E146:')
  93. endfunc
  94. " Test for interrupting :global using Ctrl-C
  95. func Test_interrupt_global()
  96. CheckRunVimInTerminal
  97. let lines =<< trim END
  98. cnoremap ; <Cmd>sleep 10<CR>
  99. call setline(1, repeat(['foo'], 5))
  100. END
  101. call writefile(lines, 'Xtest_interrupt_global')
  102. let buf = RunVimInTerminal('-S Xtest_interrupt_global', {'rows': 6})
  103. call term_sendkeys(buf, ":g/foo/norm :\<C-V>;\<CR>")
  104. " Wait for :sleep to start
  105. call TermWait(buf, 100)
  106. call term_sendkeys(buf, "\<C-C>")
  107. call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 6))}, 1000)
  108. " Also test in Ex mode
  109. call term_sendkeys(buf, "gQg/foo/norm :\<C-V>;\<CR>")
  110. " Wait for :sleep to start
  111. call TermWait(buf, 100)
  112. call term_sendkeys(buf, "\<C-C>")
  113. call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 5))}, 1000)
  114. call StopVimInTerminal(buf)
  115. call delete('Xtest_interrupt_global')
  116. endfunc
  117. " vim: shiftwidth=2 sts=2 expandtab