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