robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
11.9 KiB495 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, setup, teardown, pending = t.describe, t.it, t.setup, t.teardown, t.pending
5local clear = n.clear
6local eq = t.eq
7local exec = n.exec
8local exec_capture = n.exec_capture
9local write_file = t.write_file
10local api = n.api
11local fn = n.fn
12
13--- @param cmd string
14--- @param v1 boolean
15local function last_set_lua_verbose_tests(cmd, v1)
16  local script_location, script_file
17  -- All test cases below use the same Nvim instance.
18  setup(function()
19    clear(v1 and { args = { '-V1' } } or nil)
20    script_file = 'test_verbose.lua'
21    local current_dir = fn.getcwd()
22    current_dir = fn.fnamemodify(current_dir, ':~')
23    script_location = current_dir .. '/' .. script_file
24
25    write_file(
26      script_file,
27      [=[
28vim.api.nvim_set_option_value('hlsearch', false, {})
29vim.bo.expandtab = true
30vim.opt.number = true
31vim.api.nvim_exec2('set numberwidth=2', {})
32vim.cmd('set colorcolumn=+1')
33
34local function cb()
35  -- This is a comment
36  -- This is another comment
37  vim.o.mouse = 'nv'
38end
39
40vim.api.nvim_set_keymap('n', '<leader>key1', ':echo "test"<cr>', {noremap = true})
41vim.keymap.set('n', '<leader>key2', ':echo "test"<cr>')
42
43vim.api.nvim_exec2("augroup test_group\
44                     autocmd!\
45                     autocmd FileType c setl cindent\
46                     augroup END\
47                  ", {})
48
49vim.api.nvim_create_autocmd('FileType', {
50  group = 'test_group',
51  pattern = 'cpp',
52  command = 'setl cindent',
53})
54
55vim.api.nvim_exec2(':highlight TestHL1 guibg=Blue', {})
56vim.api.nvim_set_hl(0, 'TestHL2', { bg = 'Green' })
57
58vim.api.nvim_command("command Bdelete :bd")
59vim.api.nvim_create_user_command("TestCommand", ":echo 'Hello'", {})
60
61vim.api.nvim_exec2 ("\
62function Close_Window() abort\
63  wincmd -\
64endfunction\
65", {})
66
67local ret = vim.api.nvim_exec2 ("\
68function! s:return80()\
69  return 80\
70endfunction\
71let &tw = s:return80()\
72", {})
73
74local set_list = ([[
75  func SetList()
76%s
77    set list
78  endfunc
79  call SetList()
80]]):format(('\n'):rep(1234))
81vim.api.nvim_exec2(set_list, {})
82
83vim.api.nvim_create_autocmd('User', { pattern = 'set_mouse', callback = cb })
84
85coroutine.wrap(function()
86  vim.o.busy = 2
87end)()
88]=]
89    )
90    exec(cmd .. ' ' .. script_file)
91    exec('doautocmd User set_mouse')
92  end)
93
94  local function get_last_set_location(linenr)
95    return ('%s %s'):format(
96      (cmd == 'source' or v1) and script_location or 'Lua',
97      v1 and ('line %d'):format(linenr) or '(run Nvim with -V1 for more details)'
98    )
99  end
100
101  local option_checks = {
102    { 'nvim_set_option_value', 'hlsearch', 'nohlsearch', 1 },
103    { 'vim.bo', 'expandtab', '  expandtab', 2 },
104    { 'vim.opt', 'number', '  number', 3 },
105    { 'nvim_exec2', 'numberwidth', '  numberwidth=2', 4 },
106    { 'vim.cmd', 'colorcolumn', '  colorcolumn=+1', 5 },
107    { 'Lua autocommand', 'mouse', '  mouse=nv', 10 },
108  }
109
110  teardown(function()
111    os.remove(script_file)
112  end)
113
114  for _, check in ipairs(option_checks) do
115    it(('for option set by %s'):format(check[1]), function()
116      local result = exec_capture((':verbose set %s?'):format(check[2]))
117      eq(
118        string.format(
119          [[
120%s
121	Last set from %s]],
122          check[3],
123          get_last_set_location(check[4])
124        ),
125        result
126      )
127    end)
128  end
129
130  it('for mapping set by nvim_set_keymap', function()
131    local result = exec_capture(':verbose map <leader>key1')
132    eq(
133      string.format(
134        [[
135
136n  \key1       * :echo "test"<CR>
137	Last set from %s]],
138        get_last_set_location(13)
139      ),
140      result
141    )
142  end)
143
144  it('for mapping set by vim.keymap.set', function()
145    local result = exec_capture(':verbose map <leader>key2')
146    eq(
147      string.format(
148        [[
149
150n  \key2       * :echo "test"<CR>
151	Last set from %s]],
152        get_last_set_location(14)
153      ),
154      result
155    )
156  end)
157
158  it('for autocmd set by nvim_exec2', function()
159    local result = exec_capture(':verbose autocmd test_group Filetype c')
160    eq(
161      string.format(
162        [[
163--- Autocommands ---
164test_group  FileType
165    c         setl cindent
166	Last set from %s]],
167        get_last_set_location(16)
168      ),
169      result
170    )
171  end)
172
173  it('for autocmd set by nvim_create_autocmd', function()
174    local result = exec_capture(':verbose autocmd test_group Filetype cpp')
175    eq(
176      string.format(
177        [[
178--- Autocommands ---
179test_group  FileType
180    cpp       setl cindent
181	Last set from %s]],
182        get_last_set_location(22)
183      ),
184      result
185    )
186  end)
187
188  it('for highlight group set by nvim_exec2', function()
189    local result = exec_capture(':verbose highlight TestHL1')
190    eq(
191      string.format(
192        [[
193TestHL1        xxx guibg=Blue
194	Last set from %s]],
195        get_last_set_location(28)
196      ),
197      result
198    )
199  end)
200
201  it('for highlight group set by nvim_set_hl', function()
202    local result = exec_capture(':verbose highlight TestHL2')
203    eq(
204      string.format(
205        [[
206TestHL2        xxx guibg=Green
207	Last set from %s]],
208        get_last_set_location(29)
209      ),
210      result
211    )
212  end)
213
214  it('for command defined by nvim_command', function()
215    if cmd == 'luafile' then
216      pending('nvim_command does not set the script context')
217    end
218    local result = exec_capture(':verbose command Bdelete')
219    eq(
220      string.format(
221        [[
222    Name              Args Address Complete    Definition
223    Bdelete           0                        :bd
224	Last set from %s]],
225        get_last_set_location(31)
226      ),
227      result
228    )
229  end)
230
231  it('for command defined by nvim_create_user_command', function()
232    local result = exec_capture(':verbose command TestCommand')
233    eq(
234      string.format(
235        [[
236    Name              Args Address Complete    Definition
237    TestCommand       0                        :echo 'Hello'
238	Last set from %s]],
239        get_last_set_location(32)
240      ),
241      result
242    )
243  end)
244
245  it('for function defined by nvim_exec2', function()
246    local result = exec_capture(':verbose function Close_Window')
247    eq(
248      string.format(
249        [[
250   function Close_Window() abort
251	Last set from %s
2521    wincmd -
253   endfunction]],
254        get_last_set_location(34)
255      ),
256      result
257    )
258  end)
259
260  it('for option set by nvim_exec2 with anonymous sid', function()
261    local result = exec_capture(':verbose set tw?')
262    local loc = get_last_set_location(40)
263    if loc == 'Lua (run Nvim with -V1 for more details)' then
264      loc = 'anonymous :source (script id 1) line 5'
265    end
266    eq(
267      string.format(
268        [[
269  textwidth=80
270	Last set from %s]],
271        loc
272      ),
273      result
274    )
275  end)
276
277  it('for option set by function in nvim_exec2', function()
278    local result = exec_capture(':verbose set list?')
279    eq(
280      string.format(
281        [[
282  list
283	Last set from %s]],
284        get_last_set_location(54)
285      ),
286      result
287    )
288  end)
289
290  it('for option set in coroutine', function()
291    local result = exec_capture(':verbose set busy?')
292    eq(
293      string.format(
294        [[
295  busy=2
296	Last set from %s]],
297        get_last_set_location(59)
298      ),
299      result
300    )
301  end)
302end
303
304describe('lua :verbose with -V1', function()
305  describe('"Last set" shows full location when using :source', function()
306    last_set_lua_verbose_tests('source', true)
307  end)
308
309  describe('"Last set" shows full location using :luafile', function()
310    last_set_lua_verbose_tests('luafile', true)
311  end)
312end)
313
314describe('lua :verbose without -V1', function()
315  describe('"Last set" shows file name when using :source', function()
316    last_set_lua_verbose_tests('source', false)
317  end)
318
319  describe('"Last set" suggests -V1 when using :luafile', function()
320    last_set_lua_verbose_tests('luafile', false)
321  end)
322end)
323
324describe(':verbose when using API from Vimscript', function()
325  local script_location, script_file
326  -- All test cases below use the same Nvim instance.
327  setup(function()
328    clear()
329    script_file = 'test_verbose.vim'
330    local current_dir = fn.getcwd()
331    current_dir = fn.fnamemodify(current_dir, ':~')
332    script_location = current_dir .. '/' .. script_file
333
334    write_file(
335      script_file,
336      [[
337call nvim_set_option_value('hlsearch', v:false, {})
338call nvim_set_keymap('n', '<leader>key1', ':echo "test"<cr>', #{noremap: v:true})
339
340call nvim_create_augroup('test_group', {})
341call nvim_create_autocmd('FileType', #{
342  \ group: 'test_group',
343  \ pattern: 'cpp',
344  \ command: 'setl cindent',
345\ })
346
347call nvim_set_hl(0, 'TestHL2', #{bg: 'Green'})
348call nvim_create_user_command("TestCommand", ":echo 'Hello'", {})
349]]
350    )
351    exec('source ' .. script_file)
352  end)
353
354  teardown(function()
355    os.remove(script_file)
356  end)
357
358  it('"Last set" for option set by nvim_set_option_value', function()
359    local result = exec_capture(':verbose set hlsearch?')
360    eq(
361      string.format(
362        [[
363nohlsearch
364	Last set from %s line 1]],
365        script_location
366      ),
367      result
368    )
369  end)
370
371  it('"Last set" for mapping set by nvim_set_keymap', function()
372    local result = exec_capture(':verbose map <leader>key1')
373    eq(
374      string.format(
375        [[
376
377n  \key1       * :echo "test"<CR>
378	Last set from %s line 2]],
379        script_location
380      ),
381      result
382    )
383  end)
384
385  it('"Last set" for autocmd set by nvim_create_autocmd', function()
386    local result = exec_capture(':verbose autocmd test_group Filetype cpp')
387    eq(
388      string.format(
389        [[
390--- Autocommands ---
391test_group  FileType
392    cpp       setl cindent
393	Last set from %s line 5]],
394        script_location
395      ),
396      result
397    )
398  end)
399
400  it('"Last set" for highlight group set by nvim_set_hl', function()
401    local result = exec_capture(':verbose highlight TestHL2')
402    eq(
403      string.format(
404        [[
405TestHL2        xxx guibg=Green
406	Last set from %s line 11]],
407        script_location
408      ),
409      result
410    )
411  end)
412
413  it('"Last set" for command defined by nvim_create_user_command', function()
414    local result = exec_capture(':verbose command TestCommand')
415    eq(
416      string.format(
417        [[
418    Name              Args Address Complete    Definition
419    TestCommand       0                        :echo 'Hello'
420	Last set from %s line 12]],
421        script_location
422      ),
423      result
424    )
425  end)
426end)
427
428describe(':verbose when using API from RPC', function()
429  -- All test cases below use the same Nvim instance.
430  setup(clear)
431
432  it('"Last set" for option set by nvim_set_option_value', function()
433    api.nvim_set_option_value('hlsearch', false, {})
434    local result = exec_capture(':verbose set hlsearch?')
435    eq(
436      [[
437nohlsearch
438	Last set from API client (channel id 1)]],
439      result
440    )
441  end)
442
443  it('"Last set" for mapping set by nvim_set_keymap', function()
444    api.nvim_set_keymap('n', '<leader>key1', ':echo "test"<cr>', { noremap = true })
445    local result = exec_capture(':verbose map <leader>key1')
446    eq(
447      [[
448
449n  \key1       * :echo "test"<CR>
450	Last set from API client (channel id 1)]],
451      result
452    )
453  end)
454
455  it('"Last set" for autocmd set by nvim_create_autocmd', function()
456    api.nvim_create_augroup('test_group', {})
457    api.nvim_create_autocmd('FileType', {
458      group = 'test_group',
459      pattern = 'cpp',
460      command = 'setl cindent',
461    })
462    local result = exec_capture(':verbose autocmd test_group Filetype cpp')
463    eq(
464      [[
465--- Autocommands ---
466test_group  FileType
467    cpp       setl cindent
468	Last set from API client (channel id 1)]],
469      result
470    )
471  end)
472
473  it('"Last set" for highlight group set by nvim_set_hl', function()
474    api.nvim_set_hl(0, 'TestHL2', { bg = 'Green' })
475    local result = exec_capture(':verbose highlight TestHL2')
476    eq(
477      [[
478TestHL2        xxx guibg=Green
479	Last set from API client (channel id 1)]],
480      result
481    )
482  end)
483
484  it('"Last set" for command defined by nvim_create_user_command', function()
485    api.nvim_create_user_command('TestCommand', ":echo 'Hello'", {})
486    local result = exec_capture(':verbose command TestCommand')
487    eq(
488      [[
489    Name              Args Address Complete    Definition
490    TestCommand       0                        :echo 'Hello'
491	Last set from API client (channel id 1)]],
492      result
493    )
494  end)
495end)