verbose_spec.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local eq = t.eq
  5. local exec = n.exec
  6. local exec_capture = n.exec_capture
  7. local write_file = t.write_file
  8. local call_viml_function = n.api.nvim_call_function
  9. local function last_set_tests(cmd)
  10. local script_location, script_file
  11. -- All test cases below use the same nvim instance.
  12. setup(function()
  13. clear { args = { '-V1' } }
  14. script_file = 'test_verbose.lua'
  15. local current_dir = call_viml_function('getcwd', {})
  16. current_dir = call_viml_function('fnamemodify', { current_dir, ':~' })
  17. script_location = table.concat { current_dir, n.get_pathsep(), script_file }
  18. write_file(
  19. script_file,
  20. [[
  21. vim.api.nvim_set_option_value('hlsearch', false, {})
  22. vim.bo.expandtab = true
  23. vim.opt.number = true
  24. vim.api.nvim_set_keymap('n', '<leader>key1', ':echo "test"<cr>', {noremap = true})
  25. vim.keymap.set('n', '<leader>key2', ':echo "test"<cr>')
  26. vim.api.nvim_exec2("augroup test_group\
  27. autocmd!\
  28. autocmd FileType c setl cindent\
  29. augroup END\
  30. ", {})
  31. vim.api.nvim_create_autocmd('FileType', {
  32. group = 'test_group',
  33. pattern = 'cpp',
  34. command = 'setl cindent',
  35. })
  36. vim.api.nvim_exec2(':highlight TestHL1 guibg=Blue', {})
  37. vim.api.nvim_set_hl(0, 'TestHL2', { bg = 'Green' })
  38. vim.api.nvim_command("command Bdelete :bd")
  39. vim.api.nvim_create_user_command("TestCommand", ":echo 'Hello'", {})
  40. vim.api.nvim_exec2 ("\
  41. function Close_Window() abort\
  42. wincmd -\
  43. endfunction\
  44. ", {})
  45. local ret = vim.api.nvim_exec2 ("\
  46. function! s:return80()\
  47. return 80\
  48. endfunction\
  49. let &tw = s:return80()\
  50. ", {})
  51. ]]
  52. )
  53. exec(cmd .. ' ' .. script_file)
  54. end)
  55. teardown(function()
  56. os.remove(script_file)
  57. end)
  58. it('"Last set" for option set by Lua', function()
  59. local result = exec_capture(':verbose set hlsearch?')
  60. eq(
  61. string.format(
  62. [[
  63. nohlsearch
  64. Last set from %s line 1]],
  65. script_location
  66. ),
  67. result
  68. )
  69. end)
  70. it('"Last set" for option set by vim.o', function()
  71. local result = exec_capture(':verbose set expandtab?')
  72. eq(
  73. string.format(
  74. [[
  75. expandtab
  76. Last set from %s line 2]],
  77. script_location
  78. ),
  79. result
  80. )
  81. end)
  82. it('"Last set" for option set by vim.opt', function()
  83. local result = exec_capture(':verbose set number?')
  84. eq(
  85. string.format(
  86. [[
  87. number
  88. Last set from %s line 3]],
  89. script_location
  90. ),
  91. result
  92. )
  93. end)
  94. it('"Last set" for mapping set by Lua', function()
  95. local result = exec_capture(':verbose map <leader>key1')
  96. eq(
  97. string.format(
  98. [[
  99. n \key1 * :echo "test"<CR>
  100. Last set from %s line 4]],
  101. script_location
  102. ),
  103. result
  104. )
  105. end)
  106. it('"Last set" for mapping set by vim.keymap.set', function()
  107. local result = exec_capture(':verbose map <leader>key2')
  108. eq(
  109. string.format(
  110. [[
  111. n \key2 * :echo "test"<CR>
  112. Last set from %s line 5]],
  113. script_location
  114. ),
  115. result
  116. )
  117. end)
  118. it('"Last set" for autocmd set by nvim_exec2', function()
  119. local result = exec_capture(':verbose autocmd test_group Filetype c')
  120. eq(
  121. string.format(
  122. [[
  123. --- Autocommands ---
  124. test_group FileType
  125. c setl cindent
  126. Last set from %s line 7]],
  127. script_location
  128. ),
  129. result
  130. )
  131. end)
  132. it('"Last set" for autocmd set by nvim_create_autocmd', function()
  133. local result = exec_capture(':verbose autocmd test_group Filetype cpp')
  134. eq(
  135. string.format(
  136. [[
  137. --- Autocommands ---
  138. test_group FileType
  139. cpp setl cindent
  140. Last set from %s line 13]],
  141. script_location
  142. ),
  143. result
  144. )
  145. end)
  146. it('"Last set" for highlight group set by nvim_exec2', function()
  147. local result = exec_capture(':verbose highlight TestHL1')
  148. eq(
  149. string.format(
  150. [[
  151. TestHL1 xxx guibg=Blue
  152. Last set from %s line 19]],
  153. script_location
  154. ),
  155. result
  156. )
  157. end)
  158. it('"Last set" for highlight group set by nvim_set_hl', function()
  159. local result = exec_capture(':verbose highlight TestHL2')
  160. eq(
  161. string.format(
  162. [[
  163. TestHL2 xxx guibg=Green
  164. Last set from %s line 20]],
  165. script_location
  166. ),
  167. result
  168. )
  169. end)
  170. it('"Last set" for command defined by nvim_command', function()
  171. if cmd == 'luafile' then
  172. pending('nvim_command does not set the script context')
  173. end
  174. local result = exec_capture(':verbose command Bdelete')
  175. eq(
  176. string.format(
  177. [[
  178. Name Args Address Complete Definition
  179. Bdelete 0 :bd
  180. Last set from %s line 22]],
  181. script_location
  182. ),
  183. result
  184. )
  185. end)
  186. it('"Last set" for command defined by nvim_create_user_command', function()
  187. local result = exec_capture(':verbose command TestCommand')
  188. eq(
  189. string.format(
  190. [[
  191. Name Args Address Complete Definition
  192. TestCommand 0 :echo 'Hello'
  193. Last set from %s line 23]],
  194. script_location
  195. ),
  196. result
  197. )
  198. end)
  199. it('"Last set" for function', function()
  200. local result = exec_capture(':verbose function Close_Window')
  201. eq(
  202. string.format(
  203. [[
  204. function Close_Window() abort
  205. Last set from %s line 25
  206. 1 wincmd -
  207. endfunction]],
  208. script_location
  209. ),
  210. result
  211. )
  212. end)
  213. it('"Last set" works with anonymous sid', function()
  214. local result = exec_capture(':verbose set tw?')
  215. eq(
  216. string.format(
  217. [[
  218. textwidth=80
  219. Last set from %s line 31]],
  220. script_location
  221. ),
  222. result
  223. )
  224. end)
  225. end
  226. describe('lua :verbose when using :source', function()
  227. last_set_tests('source')
  228. end)
  229. describe('lua :verbose when using :luafile', function()
  230. last_set_tests('luafile')
  231. end)
  232. describe('lua verbose:', function()
  233. local script_file
  234. setup(function()
  235. clear()
  236. script_file = 'test_luafile.lua'
  237. write_file(
  238. script_file,
  239. [[
  240. vim.api.nvim_set_option_value('hlsearch', false, {})
  241. ]]
  242. )
  243. exec(':source ' .. script_file)
  244. end)
  245. teardown(function()
  246. os.remove(script_file)
  247. end)
  248. it('is disabled when verbose = 0', function()
  249. local result = exec_capture(':verbose set hlsearch?')
  250. eq(
  251. [[
  252. nohlsearch
  253. Last set from Lua (run Nvim with -V1 for more details)]],
  254. result
  255. )
  256. end)
  257. end)