test_execute_func.vim 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. " test execute()
  2. source view_util.vim
  3. source check.vim
  4. source vim9.vim
  5. source term_util.vim
  6. func NestedEval()
  7. let nested = execute('echo "nested\nlines"')
  8. echo 'got: "' . nested . '"'
  9. endfunc
  10. func NestedRedir()
  11. redir => var
  12. echo 'broken'
  13. redir END
  14. endfunc
  15. func Test_execute_string()
  16. call assert_equal("\nnocompatible", execute('set compatible?'))
  17. call assert_equal("\nsomething\nnice", execute('echo "something\nnice"'))
  18. call assert_equal("noendofline", execute('echon "noendofline"'))
  19. call assert_equal("", execute(123))
  20. call assert_equal("\ngot: \"\nnested\nlines\"", execute('call NestedEval()'))
  21. redir => redired
  22. echo 'this'
  23. let evaled = execute('echo "that"')
  24. echo 'theend'
  25. redir END
  26. " Nvim supports execute('... :redir ...'), so this test is intentionally
  27. " disabled.
  28. " call assert_equal("\nthis\ntheend", redired)
  29. call assert_equal("\nthat", evaled)
  30. call assert_fails('call execute("doesnotexist")', 'E492:')
  31. " Nvim supports execute('... :redir ...'), so this test is intentionally
  32. " disabled.
  33. " call assert_fails('call execute("call NestedRedir()")', 'E930:')
  34. call assert_equal("\nsomething", execute('echo "something"', ''))
  35. call assert_equal("\nsomething", execute('echo "something"', 'silent'))
  36. call assert_equal("\nsomething", execute('echo "something"', 'silent!'))
  37. call assert_equal("", execute('burp', 'silent!'))
  38. if has('float')
  39. call assert_fails('call execute(3.4)', 'E492:')
  40. call assert_equal("\nx", execute("echo \"x\"", 3.4))
  41. call CheckDefExecAndScriptFailure(['execute("echo \"x\"", 3.4)'], 'E806:')
  42. endif
  43. endfunc
  44. func Test_execute_list()
  45. call assert_equal("\nsomething\nnice", execute(['echo "something"', 'echo "nice"']))
  46. let l = ['for n in range(0, 3)',
  47. \ 'echo n',
  48. \ 'endfor']
  49. call assert_equal("\n0\n1\n2\n3", execute(l))
  50. call assert_equal("", execute([]))
  51. endfunc
  52. func Test_execute_does_not_change_col()
  53. echo ''
  54. echon 'abcd'
  55. let x = execute('silent echo 234343')
  56. echon 'xyz'
  57. let text = ''
  58. for col in range(1, 7)
  59. let text .= nr2char(screenchar(&lines, col))
  60. endfor
  61. call assert_equal('abcdxyz', text)
  62. endfunc
  63. func Test_execute_not_silent()
  64. echo ''
  65. echon 'abcd'
  66. let x = execute('echon 234', '')
  67. echo 'xyz'
  68. let text1 = ''
  69. for col in range(1, 8)
  70. let text1 .= nr2char(screenchar(&lines - 1, col))
  71. endfor
  72. call assert_equal('abcd234 ', text1)
  73. let text2 = ''
  74. for col in range(1, 4)
  75. let text2 .= nr2char(screenchar(&lines, col))
  76. endfor
  77. call assert_equal('xyz ', text2)
  78. endfunc
  79. func Test_win_execute()
  80. let thiswin = win_getid()
  81. new
  82. let otherwin = win_getid()
  83. call setline(1, 'the new window')
  84. call win_gotoid(thiswin)
  85. let line = win_execute(otherwin, 'echo getline(1)')
  86. call assert_match('the new window', line)
  87. let line = win_execute(134343, 'echo getline(1)')
  88. call assert_equal('', line)
  89. if has('textprop')
  90. let popupwin = popup_create('the popup win', {'line': 2, 'col': 3})
  91. redraw
  92. let line = 'echo getline(1)'->win_execute(popupwin)
  93. call assert_match('the popup win', line)
  94. call popup_close(popupwin)
  95. endif
  96. call win_gotoid(otherwin)
  97. bwipe!
  98. " check :lcd in another window does not change directory
  99. let curid = win_getid()
  100. let curdir = getcwd()
  101. split Xother
  102. lcd ..
  103. " Use :pwd to get the actual current directory
  104. let otherdir = execute('pwd')
  105. call win_execute(curid, 'lcd testdir')
  106. call assert_equal(otherdir, execute('pwd'))
  107. bwipe!
  108. execute 'cd ' .. curdir
  109. endfunc
  110. func Test_win_execute_update_ruler()
  111. CheckFeature quickfix
  112. enew
  113. call setline(1, range(500))
  114. 20
  115. split
  116. let winid = win_getid()
  117. set ruler
  118. wincmd w
  119. let height = winheight(winid)
  120. redraw
  121. call assert_match('20,1', Screenline(height + 1))
  122. let line = win_execute(winid, 'call cursor(100, 1)')
  123. redraw
  124. call assert_match('100,1', Screenline(height + 1))
  125. bwipe!
  126. endfunc
  127. func Test_win_execute_other_tab()
  128. let thiswin = win_getid()
  129. tabnew
  130. call win_execute(thiswin, 'let xyz = 1')
  131. call assert_equal(1, xyz)
  132. tabclose
  133. unlet xyz
  134. endfunc
  135. func Test_win_execute_visual_redraw()
  136. call setline(1, ['a', 'b', 'c'])
  137. new
  138. wincmd p
  139. " start Visual in current window, redraw in other window with fewer lines
  140. call feedkeys("G\<C-V>", 'txn')
  141. call win_execute(winnr('#')->win_getid(), 'redraw')
  142. call feedkeys("\<Esc>", 'txn')
  143. bwipe!
  144. bwipe!
  145. enew
  146. new
  147. call setline(1, ['a', 'b', 'c'])
  148. let winid = win_getid()
  149. wincmd p
  150. " start Visual in current window, extend it in other window with more lines
  151. call feedkeys("\<C-V>", 'txn')
  152. call win_execute(winid, 'call feedkeys("G\<C-V>", ''txn'')')
  153. redraw
  154. bwipe!
  155. bwipe!
  156. endfunc
  157. func Test_win_execute_on_startup()
  158. CheckRunVimInTerminal
  159. let lines =<< trim END
  160. vim9script
  161. [repeat('x', &columns)]->writefile('Xfile1')
  162. silent tabedit Xfile2
  163. var id = win_getid()
  164. silent tabedit Xfile3
  165. autocmd VimEnter * win_execute(id, 'close')
  166. END
  167. call writefile(lines, 'XwinExecute')
  168. let buf = RunVimInTerminal('-p Xfile1 -Nu XwinExecute', {})
  169. " this was crashing on exit with EXITFREE defined
  170. call StopVimInTerminal(buf)
  171. call delete('XwinExecute')
  172. call delete('Xfile1')
  173. endfunc
  174. func Test_execute_cmd_with_null()
  175. call assert_equal("", execute(v:_null_string))
  176. call assert_equal("", execute(v:_null_list))
  177. call assert_fails('call execute(v:_null_dict)', 'E731:')
  178. call assert_fails('call execute(v:_null_blob)', 'E976:')
  179. " Nvim doesn't have null partials
  180. " call assert_fails('call execute(test_null_partial())','E729:')
  181. if has('job')
  182. call assert_fails('call execute(test_null_job())', 'E908:')
  183. call assert_fails('call execute(test_null_channel())', 'E908:')
  184. endif
  185. endfunc
  186. func Test_win_execute_tabpagewinnr()
  187. belowright split
  188. tab split
  189. belowright split
  190. call assert_equal(2, tabpagewinnr(1))
  191. tabprevious
  192. wincmd p
  193. call assert_equal(1, tabpagenr())
  194. call assert_equal(1, tabpagewinnr(1))
  195. call assert_equal(2, tabpagewinnr(2))
  196. call win_execute(win_getid(1, 2),
  197. \ 'call assert_equal(2, tabpagenr())'
  198. \ .. '| call assert_equal(1, tabpagewinnr(1))'
  199. \ .. '| call assert_equal(1, tabpagewinnr(2))')
  200. call assert_equal(1, tabpagenr())
  201. call assert_equal(1, tabpagewinnr(1))
  202. call assert_equal(2, tabpagewinnr(2))
  203. %bwipe!
  204. endfunc
  205. " vim: shiftwidth=2 sts=2 expandtab