robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local eq =t . eq 6local ok =t . ok 7local exec_lua =n . exec_lua 8local clear =n . clear 9local feed =n . feed 10local eval =n . eval 11local is_ci =t . is_ci 12local is_os =t . is_os 13local poke_eventloop =n . poke_eventloop 14 15describe ( 'vim.ui' , function () 16before_each ( function () 17clear ( { args_rm ={ '-u' } , args ={ '--clean' } } ) 18end ) 19 20describe ( 'input()' , function () 21it ( 'can input text' , function () 22local result =exec_lua [[ 23local opts = { 24prompt = 'Input: ', 25} 26local input 27local cb = function(item) 28input = item 29end 30-- input would require input and block the test; 31local prompt 32vim.fn.input = function(opts) 33prompt = opts.prompt 34return "Inputted text" 35end 36vim.ui.input(opts, cb) 37vim.wait(100, function() return input ~= nil end) 38return {input, prompt} 39]] 40eq ( 'Inputted text' , result [ 1 ]) 41eq ( 'Input: ' , result [ 2 ]) 42end ) 43 44it ( 'can input text on nil opt' , function () 45feed ( ':lua vim.ui.input(nil, function(input) result = input end)<cr>' ) 46eq ( '' , eval ( 'v:errmsg' )) 47feed ( 'Inputted text<cr>' ) 48eq ( 'Inputted text' , exec_lua ( 'return result' )) 49end ) 50 51it ( 'can input text on {} opt' , function () 52feed ( ':lua vim.ui.input({}, function(input) result = input end)<cr>' ) 53eq ( '' , eval ( 'v:errmsg' )) 54feed ( 'abcdefg<cr>' ) 55eq ( 'abcdefg' , exec_lua ( 'return result' )) 56end ) 57 58it ( 'can input empty text #18144' , function () 59feed ( ':lua vim.ui.input({}, function(input) result = input end)<cr>' ) 60feed ( '<cr>' ) 61eq ( '' , exec_lua ( 'return result' )) 62end ) 63 64it ( 'can input empty text with cancelreturn opt #18144' , function () 65feed ( ':lua vim.ui.input({ cancelreturn = "CANCEL" }, function(input) result = input end)<cr>' ) 66feed ( '<cr>' ) 67eq ( '' , exec_lua ( 'return result' )) 68end ) 69 70it ( 'can return nil when aborted with ESC #18144' , function () 71feed ( ':lua result = "on_confirm not called"<cr>' ) 72feed ( ':lua vim.ui.input({}, function(input) result = input end)<cr>' ) 73feed ( 'Inputted Text<esc>' ) 74-- Note: When `result == nil`, exec_lua('returns result') returns vim.NIL 75eq ( true , exec_lua ( 'return (nil == result)' )) 76end ) 77 78it ( 'can return opts.cancelreturn when aborted with ESC with cancelreturn opt #18144' , function () 79feed ( ':lua result = "on_confirm not called"<cr>' ) 80feed ( ':lua vim.ui.input({ cancelreturn = "CANCEL" }, function(input) result = input end)<cr>' ) 81feed ( 'Inputted Text<esc>' ) 82eq ( 'CANCEL' , exec_lua ( 'return result' )) 83end ) 84 85it ( 'can return nil when interrupted with Ctrl-C #18144' , function () 86feed ( ':lua result = "on_confirm not called"<cr>' ) 87feed ( ':lua vim.ui.input({}, function(input) result = input end)<cr>' ) 88poke_eventloop () -- This is needed because Ctrl-C flushes input 89feed ( 'Inputted Text<c-c>' ) 90eq ( true , exec_lua ( 'return (nil == result)' )) 91end ) 92 93it ( 94'can return the identical object when an arbitrary opts.cancelreturn object is given' , 95function () 96feed ( ':lua fn = function() return 42 end<CR>' ) 97eq ( 42 , exec_lua ( 'return fn()' )) 98feed ( ':lua vim.ui.input({ cancelreturn = fn }, function(input) result = input end)<cr>' ) 99feed ( 'cancel<esc>' ) 100eq ( true , exec_lua ( 'return (result == fn)' )) 101eq ( 42 , exec_lua ( 'return result()' )) 102end 103) 104end ) 105 106describe ( 'open()' , function () 107it ( 'validation' , function () 108if is_os ( 'win' ) or not is_ci ( 'github' ) then 109exec_lua [[vim.system = function() return { wait=function() return { code=3 } end } end]] 110end 111if not is_os ( 'bsd' ) then 112local rv =exec_lua ( [[ 113local cmd, err = vim.ui.open('non-existent-file') 114if err and err:find('no handler found') then 115return -1 116end 117return cmd:wait(100).code 118]] ) 119ok ( type ( rv ) == 'number' and rv ~= 0 , 'nonzero exit code' , rv ) 120end 121 122exec_lua [[ 123vim.fn.has = function() return 0 end 124vim.fn.executable = function() return 0 end 125]] 126eq ( 127'vim.ui.open: no handler found (tried: wslview, explorer.exe, xdg-open, lemonade)' , 128exec_lua [[local _, err = vim.ui.open('foo') ; return err]] 129) 130end ) 131 132it ( 'escapes cmd.exe metacharacters in URIs #41337' , function () 133eq ( 134{ 'cmd.exe' , '/c' , 'start' , '' , 'https://example.com/?q=^&^|^<^>^^^%^!' } , 135exec_lua ( function () 136vim . fn . has =function ( feat ) 137return feat == 'win32' and 1 or 0 138end 139local captured --- @type string[] 140vim . system =function ( cmd ) 141captured =cmd 142return { 143wait =function () 144return { code =0 } 145end , 146} 147end 148vim . ui . open ( 'https://example.com/?q=&|<>^%!' ) 149return captured 150end ) 151) 152end ) 153 154it ( 'opt.cmd #29490' , function () 155t . matches ( 156'ENOENT: no such file or directory' , 157t . pcall_err ( exec_lua , function () 158vim . ui . open ( 'foo' , { cmd ={ 'non-existent-tool' } } ) 159end ) 160) 161 162eq ( 163{ 164code =0 , 165signal =0 , 166stderr ='' , 167stdout ='arg1=arg1;arg2=https://example.com;' , 168} , 169exec_lua ( function ( cmd_ ) 170local cmd , err =vim . ui . open ( 'https://example.com' , { cmd =cmd_ } ) 171assert ( cmd and not err ) 172return cmd : wait () 173end , { n . testprg ( 'printargs-test' ), 'arg1' } ) 174) 175end ) 176 177it ( 'can reuse opt.cmd without accumulating paths' , function () 178eq ( 179'arg1=arg1;arg2=https://example.com/second;' , 180exec_lua ( function ( opener ) 181local opts ={ cmd ={ opener , 'arg1' } } 182vim . ui . open ( 'https://example.com/first' , opts ): wait () 183return vim . ui . open ( 'https://example.com/second' , opts ): wait (). stdout 184end , n . testprg ( 'printargs-test' )) 185) 186end ) 187 188it ( 'gx on a help tag opens URL' , function () 189n . command ( 'helptags $VIMRUNTIME/doc' ) 190n . command ( 'help nvim.txt' ) 191 192local link_ns =n . api . nvim_create_namespace ( 'nvim.help.urls' ) 193local tag =n . api . nvim_buf_get_extmarks ( 0 , link_ns , 0 , - 1 , { 194limit =1 , 195details =true , 196} )[ 1 ] 197 198local url =tag [ 4 ]. url 199assert ( url ) 200 201--- points to the neovim.io site 202eq ( true , vim . startswith ( url , 'https://neovim.io/doc' )) 203 204-- tag is URI encoded 205local param =url : match ( '%?tag=(.*)' ) 206local tagname = 207n . api . nvim_buf_get_text ( 0 , tag [ 2 ], tag [ 3 ], tag [ 4 ]. end_row , tag [ 4 ]. end_col , {} )[ 1 ] 208eq ( vim . uri_encode ( tagname ), param ) 209 210-- non-nvim tags are ignored 211local buf =n . api . nvim_create_buf ( false , false ) 212n . api . nvim_buf_set_lines ( buf , 0 , 0 , false , { 213'|nonexisting|' , 214} ) 215n . api . nvim_set_option_value ( 'filetype' , 'help' , { buf =buf , scope ='local' } ) 216local tags =n . api . nvim_buf_get_extmarks ( buf , link_ns , 0 , - 1 , {} ) 217eq ( 0 , # tags ) 218end ) 219end ) 220end )