robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each , pending =t . describe , t . it , t . before_each , t . pending 6local command , feed =n . command , n . feed 7local dedent =t . dedent 8local clear =n . clear 9local exec_lua =n . exec_lua 10local fn =n . fn 11local nvim_prog =n . nvim_prog 12local matches =t . matches 13local tmpname =t . tmpname 14local eq =t . eq 15local pesc =vim . pesc 16 17-- Collects all names passed to find_path() after attempting ":Man foo". 18local function get_search_history ( name ) 19return exec_lua ( function () 20local args =vim . split ( name , ' ' ) 21local man =require ( 'man' ) 22local res ={} 23--- @diagnostic disable-next-line:duplicate-set-field 24man . _find_path =function ( name0 , sect ) 25table . insert ( res , { sect , name0 } ) 26return nil 27end 28local err =man . open_page ( - 1 , { tab =0 } , args ) 29assert ( err and err : match ( 'no manual entry' )) 30return res 31end ) 32end 33 34clear () 35if fn . executable ( 'man' ) == 0 then 36pending ( 'N/A: missing "man" command' , function () end ) 37return 38end 39 40describe ( ':Man' , function () 41before_each ( function () 42clear () 43end ) 44 45describe ( 'man.lua: highlight_line()' , function () 46local screen --- @type test.functional.ui.screen 47 48before_each ( function () 49command ( 'syntax on' ) 50command ( 'set filetype=man' ) 51command ( 'syntax off' ) -- Ignore syntax groups 52screen =Screen . new ( 52 , 5 ) 53screen : set_default_attr_ids ( { 54b ={ bold =true } , 55i ={ italic =true } , 56u ={ underline =true } , 57bi ={ bold =true , italic =true } , 58biu ={ bold =true , italic =true , underline =true } , 59c ={ foreground =Screen . colors . Blue } , -- control chars 60eob ={ bold =true , foreground =Screen . colors . Blue } , -- empty line '~'s 61} ) 62end ) 63 64it ( 'clears backspaces from text and adds highlights' , function () 65feed ( 66dedent ( 67[[ 68ithis i<C-v><C-h>is<C-v><C-h>s a<C-v><C-h>a test 69with _<C-v><C-h>o_<C-v><C-h>v_<C-v><C-h>e_<C-v><C-h>r_<C-v><C-h>s_<C-v><C-h>t_<C-v><C-h>r_<C-v><C-h>u_<C-v><C-h>c_<C-v><C-h>k text<ESC>]] 70) 71) 72 73screen : expect { 74grid =[[ 75this i{c:^H}is{c:^H}s a{c:^H}a test | 76with _{c:^H}o_{c:^H}v_{c:^H}e_{c:^H}r_{c:^H}s_{c:^H}t_{c:^H}r_{c:^H}u_{c:^H}c_{c:^H}k tex^t | 77{eob:~ }|*2 78| 79]] , 80} 81 82exec_lua [[require'man'.init_pager()]] 83 84screen : expect ( [[ 85^this {b:is} {b:a} test | 86with {i:overstruck} text | 87{eob:~ }|*2 88| 89]] ) 90end ) 91 92it ( 'clears escape sequences from text and adds highlights' , function () 93feed ( 94dedent ( 95[[ 96ithis <C-v><ESC>[1mis <C-v><ESC>[3ma <C-v><ESC>[4mtest<C-v><ESC>[0m 97<C-v><ESC>[4mwith<C-v><ESC>[24m <C-v><ESC>[4mescaped<C-v><ESC>[24m <C-v><ESC>[4mtext<C-v><ESC>[24m<ESC>]] 98) 99) 100 101screen : expect { 102grid =[=[ 103this {c:^[}[1mis {c:^[}[3ma {c:^[}[4mtest{c:^[}[0m | 104{c:^[}[4mwith{c:^[}[24m {c:^[}[4mescaped{c:^[}[24m {c:^[}[4mtext{c:^[}[24^m | 105{eob:~ }|*2 106| 107]=] , 108} 109 110exec_lua [[require'man'.init_pager()]] 111 112screen : expect ( [[ 113^this {b:is }{bi:a }{biu:test} | 114{u:with} {u:escaped} {u:text} | 115{eob:~ }|*2 116| 117]] ) 118end ) 119 120it ( 'clears OSC 8 hyperlink markup from text' , function () 121feed ( 122dedent ( 123[[ 124ithis <C-v><ESC>]8;;http://example.com<C-v><ESC>\Link Title<C-v><ESC>]8;;<C-v><ESC>\<ESC>]] 125) 126) 127 128screen : expect { 129grid =[=[ 130this {c:^[}]8;;http://example.com{c:^[}\Link Title{c:^[}]8;;{c:^[}^\ | 131{eob:~ }|*3 132| 133]=] , 134} 135 136exec_lua [[require'man'.init_pager()]] 137 138screen : expect ( [[ 139^this Link Title | 140{eob:~ }|*3 141| 142]] ) 143end ) 144 145it ( 'highlights multibyte text' , function () 146feed ( 147dedent ( 148[[ 149ithis i<C-v><C-h>is<C-v><C-h>s あ<C-v><C-h>あ test 150with _<C-v><C-h>ö_<C-v><C-h>v_<C-v><C-h>e_<C-v><C-h>r_<C-v><C-h>s_<C-v><C-h>t_<C-v><C-h>r_<C-v><C-h>u_<C-v><C-h>̃_<C-v><C-h>c_<C-v><C-h>k te<C-v><ESC>[3mxt¶<C-v><ESC>[0m<ESC>]] 151) 152) 153exec_lua [[require'man'.init_pager()]] 154 155screen : expect ( [[ 156^this {b:is} {b:あ} test | 157with {i:överstrũck} te{i:xt¶} | 158{eob:~ }|*2 159| 160]] ) 161end ) 162 163it ( 'highlights underscores based on context' , function () 164feed ( 165dedent ( 166[[ 167i_<C-v><C-h>_b<C-v><C-h>be<C-v><C-h>eg<C-v><C-h>gi<C-v><C-h>in<C-v><C-h>ns<C-v><C-h>s 168m<C-v><C-h>mi<C-v><C-h>id<C-v><C-h>d_<C-v><C-h>_d<C-v><C-h>dl<C-v><C-h>le<C-v><C-h>e 169_<C-v><C-h>m_<C-v><C-h>i_<C-v><C-h>d_<C-v><C-h>__<C-v><C-h>d_<C-v><C-h>l_<C-v><C-h>e<ESC>]] 170) 171) 172exec_lua [[require'man'.init_pager()]] 173 174screen : expect ( [[ 175{b:^_begins} | 176{b:mid_dle} | 177{i:mid_dle} | 178{eob:~ }| 179| 180]] ) 181end ) 182 183it ( 'highlights various bullet formats' , function () 184feed ( dedent ( [[ 185i· ·<C-v><C-h>· 186+<C-v><C-h>o 187+<C-v><C-h>+<C-v><C-h>o<C-v><C-h>o double<ESC>]] )) 188exec_lua [[require'man'.init_pager()]] 189 190screen : expect ( [[ 191^· {b:·} | 192{b:·} | 193{b:·} double | 194{eob:~ }| 195| 196]] ) 197end ) 198 199it ( 'handles : characters in input' , function () 200feed ( dedent ( [[ 201i<C-v><C-[>[40m 0 <C-v><C-[>[41m 1 <C-v><C-[>[42m 2 <C-v><C-[>[43m 3 202<C-v><C-[>[44m 4 <C-v><C-[>[45m 5 <C-v><C-[>[46m 6 <C-v><C-[>[47m 7 <C-v><C-[>[100m 8 <C-v><C-[>[101m 9 203<C-v><C-[>[102m 10 <C-v><C-[>[103m 11 <C-v><C-[>[104m 12 <C-v><C-[>[105m 13 <C-v><C-[>[106m 14 <C-v><C-[>[107m 15 204<C-v><C-[>[48:5:16m 16 <ESC>]] )) 205exec_lua [[require'man'.init_pager()]] 206 207screen : expect ( [[ 208^ 0 1 2 3 | 2094 5 6 7 8 9 | 21010 11 12 13 14 15 | 21116 | 212| 213]] ) 214end ) 215end ) 216 217it ( 'q quits in "$MANPAGER mode" (:Man!) #18281' , function () 218-- This will hang if #18281 regresses. 219local args ={ 220nvim_prog , 221'--headless' , 222'+autocmd VimLeave * echo "quit works!!"' , 223'+Man!' , 224'+tag ls' , 225'+call nvim_input("q")' , 226} 227matches ( 'quit works!!' , fn . system ( args , { 'manpage contents' } )) 228end ) 229 230it ( 'raw manpage into (:Man!) creates a new buffer #30132' , function () 231local args ={ 232nvim_prog , 233'--headless' , 234'+Man! foo' , 235'+echo bufname()' , 236'+enew' , 237'+Man! foo' , 238'+echo bufname()' , 239'+enew' , 240'+Man! foo' , 241'+echo bufname()' , 242'+q' , 243} 244local out =fn . system ( args , { 'manpage contents' } ) 245assert ( out and out : match ( 'man://%?new=%d' )) 246end ) 247 248it ( 'reports non-existent man pages for absolute paths' , function () 249local actual_file =tmpname () 250-- actual_file must be an absolute path to an existent file for us to test against it 251matches ( '^/.+' , actual_file ) 252local args ={ nvim_prog , '--headless' , '+:Man ' .. actual_file , '+q' } 253matches ( 254( 'Error in command line:\n' .. 'man.lua: no manual entry for %s' ): format ( pesc ( actual_file )), 255fn . system ( args , { '' } ) 256) 257os . remove ( actual_file ) 258end ) 259 260-- if man -w returns multiple results, :Man should select the correct one. 261it ( 'matches correct manpage section and name' , function () 262-- mock the system function to return the results we want to test. 263local function _test ( results , name , sect ) 264return exec_lua ( function () 265local man =require 'man' 266return man . _match_manpage_path ( results , name , sect ) 267end ) 268end 269 270eq ( 271'/usr/share/man/man1/bash.1' , 272_test ( { '/usr/share/man/man1/bash.1' } , '/usr/share/man/man1/bash.1' ) 273) 274 275eq ( 276'/usr/share/man/man3/strlen.3.gz' , 277_test ( { '/usr/share/man/man3/strlen.3.gz' } , '/usr/share/man/man3/strlen.3.gz' ) 278) 279 280eq ( nil , _test ( { '/tmp/not-a-manpage' } , '/tmp/not-a-manpage' )) 281eq ( nil , _test ( { '/tmp/T10.1' } , '/tmp/T10.1' )) 282 283eq ( 284'/usr/share/man/man3/strcpy.3' , 285_test ( { 286'/usr/share/man/man3/strcpy.3' , 287'/usr/share/man/man3/string.3' , 288} , 'strcpy' ) 289) 290 291eq ( 292'/usr/share/man/man3/strcpy.3' , 293_test ( { 294'/usr/share/man/man3/string.3' , 295'/usr/share/man/man3/strcpy.3' , 296} , 'strcpy' ) 297) 298 299eq ( 300'/usr/share/man/man3/strcpy.3' , 301_test ( { 302'/usr/share/man/man3p/strcpy.3p' , 303'/usr/share/man/man3/strcpy.3' , 304'/usr/share/man/man3/string.3' , 305'/usr/share/man/man7/string_copying.7' , 306} , 'strcpy' , '3' ) 307) 308 309eq ( 310'/usr/share/man/man3/strcpy.3.gz' , 311_test ( { 312'/usr/share/man/man3p/strcpy.3p.gz' , 313'/usr/share/man/man3/strcpy.3.gz' , 314'/usr/share/man/man3/string.3.gz' , 315'/usr/share/man/man7/string_copying.7.gz' , 316} , 'strcpy' , '3' ) 317) 318end ) 319 320it ( 'uses direct manpage lookup if man directories cannot be determined #25919' , function () 321eq ( 322{ 323{ 324name ='open' , 325filename ='man://open(2)' , 326cmd ='1' , 327} , 328} , 329exec_lua ( function () 330local man =require ( 'man' ) 331vim . env . MANPATH =nil 332vim . npcall =function () 333return nil 334end 335man . _find_path =function ( name , sect ) 336if name == 'open' and sect == '2' then 337return '/usr/share/man/man2/open.2' 338end 339end 340return man . goto_tag ( 'open(2)' ) 341end ) 342) 343end ) 344 345it ( 'tries variants with spaces, underscores #22503' , function () 346eq ( { 347{ vim . NIL , 'NAME WITH SPACES' } , 348{ vim . NIL , 'NAME_WITH_SPACES' } , 349} , get_search_history ( 'NAME WITH SPACES' )) 350eq ( { 351{ '3' , 'some other man' } , 352{ '3' , 'some_other_man' } , 353} , get_search_history ( '3 some other man' )) 354eq ( { 355{ '3x' , 'some other man' } , 356{ '3x' , 'some_other_man' } , 357} , get_search_history ( '3X some other man' )) 358eq ( { 359{ '3tcl' , 'some other man' } , 360{ '3tcl' , 'some_other_man' } , 361} , get_search_history ( '3tcl some other man' )) 362eq ( { 363{ 'n' , 'some other man' } , 364{ 'n' , 'some_other_man' } , 365} , get_search_history ( 'n some other man' )) 366eq ( { 367{ vim . NIL , '123some other man' } , 368{ vim . NIL , '123some_other_man' } , 369} , get_search_history ( '123some other man' )) 370eq ( { 371{ '1' , 'other_man' } , 372{ '1' , 'other_man' } , 373} , get_search_history ( 'other_man(1)' )) 374end ) 375 376it ( 'can complete' , function () 377eq ( 378true , 379exec_lua ( function () 380return # require ( 'man' ). man_complete ( 'f' , 'Man f' ) > 0 381end ) 382) 383end ) 384end )