neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

verbose_spec.lua (12187B)


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