robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8c0bf18374
master
1-- Tests for vim.ui.select(), including integration with builtins (:tselect, z=). 2 3local t =require ( 'test.testutil' ) 4local describe , it , before_each , finally =t . describe , t . it , t . before_each , t . finally 5local retry =t . retry 6local n =require ( 'test.functional.testnvim' )() 7local clear =n . clear 8local exec_lua =n . exec_lua 9local api =n . api 10local eq =t . eq 11local neq =t . neq 12local write_file =t . write_file 13 14before_each ( clear ) 15 16--- Mock async vim.ui.select impl. Imitates fzf-lua/telescope/snacks: opens a transient floating 17--- window, then schedules on_choice to fire on the next event-loop tick. 18--- 19--- Sets `_G._captured` so tests can assert the user choice. 20--- @param pick integer|nil 1-based index to "pick" (nil cancels). 21local function setup_async_picker ( pick ) 22exec_lua ( function () 23_G . _captured =nil 24--- @diagnostic disable-next-line: duplicate-set-field 25vim . ui . select =function ( items , opts , on_choice ) 26_G . _captured ={ items =items , opts =opts } 27-- Open a floating window like a real picker would. 28local buf =vim . api . nvim_create_buf ( false , true ) 29local win =vim . api . nvim_open_win ( buf , false , { 30relative ='editor' , 31row =1 , 32col =1 , 33width =30 , 34height =math . min ( # items , 5 ), 35} ) 36_G . _captured . win =win 37-- Defer the choice so the wait actually has to pump events. 38vim . defer_fn ( function () 39if vim . api . nvim_win_is_valid ( win ) then 40vim . api . nvim_win_close ( win , true ) 41end 42if pick then 43on_choice ( items [ pick ], pick ) 44else 45on_choice ( nil , nil ) 46end 47end , 30 ) 48end 49end , pick ) 50end 51 52--- Mock fzf-lua-style picker: opens a floating window with a *terminal* buffer running a small 53--- shell command. When the command exits we treat the user as having "picked" `pick`. 54local function setup_term_picker ( pick ) 55exec_lua ( function ( pick_ , prog ) 56_G . _captured =nil 57--- @diagnostic disable-next-line: duplicate-set-field 58vim . ui . select =function ( items , opts , on_choice ) 59_G . _captured ={ items =items , opts =opts } 60local buf =vim . api . nvim_create_buf ( false , true ) 61local win =vim . api . nvim_open_win ( buf , true , { 62relative ='editor' , 63row =1 , 64col =1 , 65width =30 , 66height =math . min ( # items , 5 ), 67} ) 68vim . fn . jobstart ( { prog } , { 69term =true , 70on_exit =function () 71if vim . api . nvim_win_is_valid ( win ) then 72vim . api . nvim_win_close ( win , true ) 73end 74if pick_ then 75on_choice ( items [ pick_ ], pick_ ) 76else 77on_choice ( nil , nil ) 78end 79end , 80} ) 81end 82end , pick , n . testprg ( 'shell-test' )) 83end 84 85describe ( 'vim.ui.select()' , function () 86it ( 'can select an item' , function () 87local result =exec_lua [[ 88local items = { 89{ name = 'Item 1' }, 90{ name = 'Item 2' }, 91} 92local opts = { 93format_item = function(entry) 94return entry.name 95end 96} 97local selected 98local cb = function(item) 99selected = item 100end 101-- inputlist would require input and block the test; 102local choices 103vim.fn.inputlist = function(x) 104choices = x 105return 1 106end 107vim.ui.select(items, opts, cb) 108vim.wait(100, function() return selected ~= nil end) 109return {selected, choices} 110]] 111eq ( { name ='Item 1' } , result [ 1 ]) 112eq ( { 113'Select one of:' , 114'1: Item 1' , 115'2: Item 2' , 116} , result [ 2 ]) 117end ) 118 119describe ( 'via :tselect' , function () 120local function prepare_test () 121-- Create dummy source files so the jump succeeds. 122write_file ( 'XselTagA.c' , 'int foo;\n' ) 123write_file ( 'XselTagB.c' , 'int foo = 1;\n' ) 124finally ( function () 125os . remove ( 'XselTagA.c' ) 126os . remove ( 'XselTagB.c' ) 127os . remove ( 'XselTags' ) 128end ) 129write_file ( 130'XselTags' , 131'!_TAG_FILE_FORMAT\t2\t/extended format/\n' 132.. 'foo\tXselTagA.c\t/^int foo;$/;"\tv\n' 133.. 'foo\tXselTagB.c\t/^int foo = 1;$/;"\tv\n' 134) 135api . nvim_set_option_value ( 'tags' , 'XselTags' , {} ) 136end 137 138it ( 'passes items, gets user choice' , function () 139prepare_test () 140 141local got =exec_lua ( function () 142--- @diagnostic disable-next-line: duplicate-set-field 143vim . ui . select =function ( items , opts , on_choice ) 144_G . _captured ={ items =items , kind =opts . kind } 145-- Pick the second match. 146on_choice ( items [ 2 ], 2 ) 147end 148vim . cmd ( 'tselect foo' ) 149end ) 150-- on_choice queues `:[idx]tag` via feedkeys; let typeahead drain. 151retry ( nil , 1000 , function () 152eq ( 'XselTagB.c' , api . nvim_eval ( 'expand("%:t")' )) 153end ) 154got =exec_lua ( function () 155return _G . _captured 156end ) 157 158eq ( 'tag' , got . kind ) 159eq ( 2 , # got . items ) 160eq ( 'foo' , got . items [ 1 ]. tag ) 161eq ( 'XselTagB.c' , got . items [ 2 ]. file ) 162end ) 163 164it ( 'does nothing when the user cancels' , function () 165prepare_test () 166 167local before =api . nvim_buf_get_name ( 0 ) 168exec_lua ( function () 169vim . ui . select =function ( _ , _ , on_choice ) 170on_choice ( nil , nil ) 171end 172vim . cmd ( 'tselect foo' ) 173end ) 174 175eq ( before , api . nvim_buf_get_name ( 0 )) 176end ) 177 178it ( '+ async picker' , function () 179prepare_test () 180 181setup_async_picker ( 2 ) 182exec_lua ( [[vim.cmd('tselect foo')]] ) 183retry ( nil , 1000 , function () 184eq ( 'XselTagB.c' , api . nvim_eval ( 'expand("%:t")' )) 185end ) 186eq ( 'tag' , exec_lua ( [[return _G._captured and _G._captured.opts.kind]] )) 187end ) 188 189it ( '+ async terminal-based picker' , function () 190prepare_test () 191 192setup_term_picker ( 2 ) 193exec_lua ( [[vim.cmd('tselect foo')]] ) 194retry ( nil , 1000 , function () 195eq ( 'XselTagB.c' , api . nvim_eval ( 'expand("%:t")' )) 196end ) 197end ) 198end ) 199 200describe ( 'via z=' , function () 201local function prepare_test () 202api . nvim_set_option_value ( 'spell' , true , {} ) 203api . nvim_set_option_value ( 'spelllang' , 'en_us' , {} ) 204api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'helo' } ) 205end 206 207it ( 'passes items, gets user choice' , function () 208prepare_test () 209 210exec_lua ( function () 211vim . cmd ( 'normal! gg0' ) 212--- @diagnostic disable-next-line: duplicate-set-field 213vim . ui . select =function ( items , opts , on_choice ) 214_G . _captured ={ items =items , kind =opts . kind , prompt =opts . prompt } 215-- Pick the first suggestion. 216on_choice ( items [ 1 ], 1 ) 217end 218vim . cmd ( 'normal! z=' ) 219end ) 220-- z= delegates to vim.ui.select, see `_core/spell:select_suggest`. on_choice queues 221-- `:normal! [idx]z=` via feedkeys; let typeahead drain. 222retry ( nil , 1000 , function () 223t . neq ( 'helo' , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )[ 1 ]) 224end ) 225local got =exec_lua ( [[return _G._captured]] ) 226 227eq ( 'spell' , got . kind ) 228t . matches ( 'helo' , got . prompt ) 229eq ( got . items [ 1 ]. word , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )[ 1 ]) 230end ) 231 232it ( 'does not produce E315 when picker changes the current window' , function () 233prepare_test () 234 235api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 236'Praesent enim diam,' , 237'Praesent enim diam,' , 238'Praesent enim diam,' , 239'Praesent enim diam,' , 240'Praesent enim diam,' , 241} ) 242-- Keep the cursor beyond the popup's single line to reproduce E315. 243api . nvim_win_set_cursor ( 0 , { 5 , 0 } ) 244 245exec_lua ( function () 246vim . ui . select =function () 247local buf =vim . api . nvim_create_buf ( false , true ) 248 249vim . api . nvim_open_win ( buf , true , { 250row =10 , 251col =10 , 252height =10 , 253width =10 , 254relative ='editor' , 255style ='minimal' , 256} ) 257end 258 259vim . cmd ( 'normal! wz=' ) 260end ) 261end ) 262 263it ( 'does nothing when the user cancels' , function () 264prepare_test () 265 266exec_lua ( function () 267vim . cmd ( 'normal! gg0' ) 268vim . ui . select =function ( _ , _ , on_choice ) 269on_choice ( nil , nil ) 270end 271vim . cmd ( 'normal! z=' ) 272end ) 273 274eq ( 'helo' , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )[ 1 ]) 275end ) 276 277it ( '+ async picker' , function () 278prepare_test () 279 280setup_async_picker ( 1 ) 281exec_lua ( [[vim.cmd('normal! gg0z=')]] ) 282retry ( nil , 1000 , function () 283neq ( 'helo' , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )[ 1 ]) 284end ) 285eq ( 'spell' , exec_lua ( [[return _G._captured and _G._captured.opts.kind]] )) 286end ) 287 288it ( '+ async terminal-based picker' , function () 289prepare_test () 290 291setup_term_picker ( 1 ) 292exec_lua ( [[vim.cmd('normal! gg0z=')]] ) 293retry ( nil , 1000 , function () 294neq ( 'helo' , api . nvim_buf_get_lines ( 0 , 0 , - 1 , false )[ 1 ]) 295end ) 296end ) 297end ) 298 299describe ( 'via ":browse oldfiles"' , function () 300it ( '+ async picker' , function () 301finally ( function () 302os . remove ( 'XselOldA' ) 303os . remove ( 'XselOldB' ) 304end ) 305write_file ( 'XselOldA' , 'a\n' ) 306write_file ( 'XselOldB' , 'b\n' ) 307local cwd =exec_lua ( [[return vim.uv.cwd()]] ) 308 309setup_async_picker ( 2 ) 310exec_lua ( function ( cwd_ ) 311-- v:oldfiles is normally populated via shada; inject directly for the test. 312vim . v . oldfiles ={ cwd_ .. '/XselOldA' , cwd_ .. '/XselOldB' } 313vim . cmd ( 'browse oldfiles' ) 314end , cwd ) 315retry ( nil , 1000 , function () 316eq ( 'XselOldB' , api . nvim_eval ( 'expand("%:t")' )) 317end ) 318eq ( 'oldfiles' , exec_lua ( [[return _G._captured and _G._captured.opts.kind]] )) 319end ) 320end ) 321end )