robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.unit.testutil' ) 2local describe =t . describe 3local itp =t . gen_itp ( t . it ) 4 5local to_cstr =t . to_cstr 6local get_str =t . ffi . string 7local eq =t . eq 8local NULL =t . NULL 9 10local buffer =t . cimport ( './src/nvim/buffer.h' ) 11local globals =t . cimport ( './src/nvim/globals.h' ) 12local stl =t . cimport ( './src/nvim/statusline.h' ) 13local grid =t . cimport ( './src/nvim/grid.h' ) 14 15describe ( 'build_stl_str_hl' , function () 16local buffer_byte_size =120 17local STL_INITIAL_ITEMS =20 18local output_buffer ='' 19 20-- This function builds the statusline 21-- 22-- @param arg Optional arguments are: 23-- .pat The statusline format string 24-- .fillchar The fill character used in the statusline 25-- .maximum_cell_count The number of cells available in the statusline 26local function build_stl_str_hl ( arg ) 27output_buffer =to_cstr ( string . rep ( ' ' , buffer_byte_size )) 28 29local pat =arg . pat or '' 30local fillchar =arg . fillchar or ' ' 31local maximum_cell_count =arg . maximum_cell_count or buffer_byte_size 32if type ( fillchar ) == type ( '' ) then 33fillchar =grid . schar_from_str ( fillchar ) 34end 35 36return stl . build_stl_str_hl ( 37globals . curwin , 38t . ffi . new ( 'CharBuf' , { output_buffer , buffer_byte_size } ), 39to_cstr ( pat ), 40- 1 , 410 , 42fillchar , 43maximum_cell_count , 44NULL , 45NULL , 46NULL , 47NULL 48) 49end 50 51-- Use this function to simplify testing the comparison between 52-- the format string and the resulting statusline. 53-- 54-- @param description The description of what the test should be doing 55-- @param statusline_cell_count The number of cells available in the statusline 56-- @param input_stl The format string for the statusline 57-- @param expected_stl The expected result string for the statusline 58-- 59-- @param arg Options can be placed in an optional dict as the last parameter 60-- .expected_cell_count The expected number of cells build_stl_str_hl will return 61-- .expected_byte_length The expected byte length of the string (defaults to byte length of expected_stl) 62-- .file_name The name of the file to be tested (useful in %f type tests) 63-- .fillchar The character that will be used to fill any 'extra' space in the stl 64local function statusline_test ( description , statusline_cell_count , input_stl , expected_stl , arg ) 65-- arg is the optional parameter 66-- so we either fill in option with arg or an empty dict 67local option =arg or {} 68 69local fillchar =option . fillchar or ' ' 70local expected_cell_count =option . expected_cell_count or statusline_cell_count 71local expected_byte_length =option . expected_byte_length or # expected_stl 72 73itp ( description , function () 74if option . file_name then 75buffer . setfname ( globals . curbuf , to_cstr ( option . file_name ), NULL , 1 ) 76else 77buffer . setfname ( globals . curbuf , nil , NULL , 1 ) 78end 79 80local result_cell_count =build_stl_str_hl { 81pat =input_stl , 82maximum_cell_count =statusline_cell_count , 83fillchar =fillchar , 84} 85 86local output_str =get_str ( output_buffer , expected_byte_length + 1 ) 87eq ( expected_stl , output_str : sub ( 1 , - 2 )) 88eq ( '\0' , output_str : sub ( - 1 ), 'output string must be NUL-terminated' ) 89eq ( expected_cell_count , result_cell_count ) 90end ) 91end 92 93-- expression testing 94statusline_test ( 'expands expression' , 2 , '%!expand(20+1)' , '21' ) 95statusline_test ( 'expands broken expression to itself' , 11 , '%!expand(20+1' , 'expand(20+1' ) 96 97-- file name testing 98statusline_test ( 'prints no file name' , 10 , '%f' , '[No Name]' , { expected_cell_count =9 } ) 99statusline_test ( 100'prints the relative file name' , 10130 , 102'%f' , 103'test/unit/buffer_spec.lua' , 104{ file_name ='test/unit/buffer_spec.lua' , expected_cell_count =25 } 105) 106statusline_test ( 107'prints the full file name' , 10840 , 109'%F' , 110'/test/unit/buffer_spec.lua' , 111{ file_name ='/test/unit/buffer_spec.lua' , expected_cell_count =26 } 112) 113 114-- fillchar testing 115statusline_test ( 'handles `!` as a fillchar' , 10 , 'abcde%=' , 'abcde!!!!!' , { fillchar ='!' } ) 116statusline_test ( 'handles `~` as a fillchar' , 10 , '%=abcde' , '~~~~~abcde' , { fillchar ='~' } ) 117statusline_test ( 118'puts fillchar `!` in between text' , 11910 , 120'abc%=def' , 121'abc!!!!def' , 122{ fillchar ='!' } 123) 124statusline_test ( 125'puts fillchar `~` in between text' , 12610 , 127'abc%=def' , 128'abc~~~~def' , 129{ fillchar ='~' } 130) 131statusline_test ( 132'puts fillchar `━` in between text' , 13310 , 134'abc%=def' , 135'abc━━━━def' , 136{ fillchar ='━' } 137) 138statusline_test ( 'handles zero-fillchar as a space' , 10 , 'abcde%=' , 'abcde ' , { fillchar =0 } ) 139statusline_test ( 140'prints the tail file name' , 14180 , 142'%t' , 143'buffer_spec.lua' , 144{ file_name ='test/unit/buffer_spec.lua' , expected_cell_count =15 } 145) 146 147-- standard text testing 148statusline_test ( 149'copies plain text' , 15080 , 151'this is a test' , 152'this is a test' , 153{ expected_cell_count =14 } 154) 155 156-- line number testing 157statusline_test ( 'prints the buffer number' , 80 , '%n' , '1' , { expected_cell_count =1 } ) 158statusline_test ( 159'prints the current line number in the buffer' , 16080 , 161'%l' , 162'0' , 163{ expected_cell_count =1 } 164) 165statusline_test ( 166'prints the number of lines in the buffer' , 16780 , 168'%L' , 169'1' , 170{ expected_cell_count =1 } 171) 172 173-- truncation testing 174statusline_test ( 175'truncates when standard text pattern is too long' , 17610 , 177'0123456789abcde' , 178'<6789abcde' 179) 180statusline_test ( 'truncates when using =' , 10 , 'abcdef%=ghijkl' , 'abcdef<jkl' ) 181statusline_test ( 'truncates centered text when using ==' , 10 , 'abcde%=gone%=fghij' , 'abcde<ghij' ) 182statusline_test ( 'respects the `<` marker' , 10 , 'abc%<defghijkl' , 'abc<ghijkl' ) 183statusline_test ( 'truncates at `<` with one `=`, test 1' , 10 , 'abc%<def%=ghijklmno' , 'abc<jklmno' ) 184statusline_test ( 'truncates at `<` with one `=`, test 2' , 10 , 'abcdef%=ghijkl%<mno' , 'abcdefghi>' ) 185statusline_test ( 'truncates at `<` with one `=`, test 3' , 10 , 'abc%<def%=ghij' , 'abcdefghij' ) 186statusline_test ( 'truncates at `<` with one `=`, test 4' , 10 , 'abc%<def%=ghijk' , 'abc<fghijk' ) 187statusline_test ( 'truncates at `<` with many `=`, test 5' , 10 , 'ab%<cdef%=g%=h%=ijk' , 'ab<efghijk' ) 188statusline_test ( 'truncates at the first `<`' , 10 , 'abc%<def%<ghijklm' , 'abc<hijklm' ) 189statusline_test ( 'ignores trailing %' , 3 , 'abc%' , 'abc' ) 190 191-- alignment testing with fillchar 192local function statusline_test_align ( 193description , 194statusline_cell_count , 195input_stl , 196expected_stl , 197arg 198) 199arg =arg or {} 200statusline_test ( 201description .. ' without fillchar' , 202statusline_cell_count , 203input_stl , 204expected_stl : gsub ( '%~' , ' ' ), 205arg 206) 207arg . fillchar ='!' 208statusline_test ( 209description .. ' with fillchar `!`' , 210statusline_cell_count , 211input_stl , 212expected_stl : gsub ( '%~' , '!' ), 213arg 214) 215arg . fillchar ='━' 216statusline_test ( 217description .. ' with fillchar `━`' , 218statusline_cell_count , 219input_stl , 220expected_stl : gsub ( '%~' , '━' ), 221arg 222) 223end 224 225statusline_test_align ( 'right aligns when using =' , 20 , 'neo%=vim' , 'neo~~~~~~~~~~~~~~vim' ) 226statusline_test_align ( 227'centers text when using %=text%=' , 22820 , 229'abc%=neovim%=def' , 230'abc~~~~neovim~~~~def' 231) 232statusline_test_align ( 233'handles uneven spacing in the buffer when using %=text%=' , 23420 , 235'abc%=neo_vim%=def' , 236'abc~~~neo_vim~~~~def' 237) 238statusline_test_align ( 239'produces approximately equal spaces even with non-equal sides when using =' , 24020 , 241'foobar%=test%=baz' , 242'foobar~~~test~~~~baz' 243) 244statusline_test_align ( 245'produces approximately equal spaces even with longer right side when using =' , 24620 , 247'a%=test%=longtext' , 248'a~~~test~~~~longtext' 249) 250statusline_test_align ( 251'handles an empty left side when using ==' , 25220 , 253'%=test%=baz' , 254'~~~~~~test~~~~~~~baz' 255) 256statusline_test_align ( 257'handles an empty right side when using ==' , 25820 , 259'foobar%=test%=' , 260'foobar~~~~~test~~~~~' 261) 262statusline_test_align ( 'handles consecutive empty ==' , 20 , '%=%=test%=' , '~~~~~~~~~~test~~~~~~' ) 263statusline_test_align ( 'handles an = alone' , 20 , '%=' , '~~~~~~~~~~~~~~~~~~~~' ) 264statusline_test_align ( 265'right aligns text when it is alone with =' , 26620 , 267'%=foo' , 268'~~~~~~~~~~~~~~~~~foo' 269) 270statusline_test_align ( 271'left aligns text when it is alone with =' , 27220 , 273'foo%=' , 274'foo~~~~~~~~~~~~~~~~~' 275) 276 277statusline_test_align ( 278'approximately centers text when using %=text%=' , 27921 , 280'abc%=neovim%=def' , 281'abc~~~~neovim~~~~~def' 282) 283statusline_test_align ( 284'completely fills the buffer when using %=text%=' , 28521 , 286'abc%=neo_vim%=def' , 287'abc~~~~neo_vim~~~~def' 288) 289statusline_test_align ( 290'produces equal spacing even with non-equal sides when using =' , 29121 , 292'foobar%=test%=baz' , 293'foobar~~~~test~~~~baz' 294) 295statusline_test_align ( 296'produces equal spacing even with longer right side when using =' , 29721 , 298'a%=test%=longtext' , 299'a~~~~test~~~~longtext' 300) 301statusline_test_align ( 302'handles an empty left side when using ==' , 30321 , 304'%=test%=baz' , 305'~~~~~~~test~~~~~~~baz' 306) 307statusline_test_align ( 308'handles an empty right side when using ==' , 30921 , 310'foobar%=test%=' , 311'foobar~~~~~test~~~~~~' 312) 313 314statusline_test_align ( 315'quadrants the text when using 3 %=' , 31640 , 317'abcd%=n%=eovim%=ef' , 318'abcd~~~~~~~~~n~~~~~~~~~eovim~~~~~~~~~~ef' 319) 320statusline_test_align ( 321'works well with %t' , 32240 , 323'%t%=right_aligned' , 324'buffer_spec.lua~~~~~~~~~~~~right_aligned' , 325{ file_name ='test/unit/buffer_spec.lua' } 326) 327statusline_test_align ( 328'works well with %t and regular text' , 32940 , 330'l%=m_l %t m_r%=r' , 331'l~~~~~~~m_l buffer_spec.lua m_r~~~~~~~~r' , 332{ file_name ='test/unit/buffer_spec.lua' } 333) 334statusline_test_align ( 335'works well with %=, %t, %L, and %l' , 33640 , 337'%t %= %L %= %l' , 338'buffer_spec.lua ~~~~~~~~~ 1 ~~~~~~~~~~ 0' , 339{ file_name ='test/unit/buffer_spec.lua' } 340) 341 342statusline_test_align ( 343'quadrants the text when using 3 %=' , 34441 , 345'abcd%=n%=eovim%=ef' , 346'abcd~~~~~~~~~n~~~~~~~~~eovim~~~~~~~~~~~ef' 347) 348statusline_test_align ( 349'works well with %t' , 35041 , 351'%t%=right_aligned' , 352'buffer_spec.lua~~~~~~~~~~~~~right_aligned' , 353{ file_name ='test/unit/buffer_spec.lua' } 354) 355statusline_test_align ( 356'works well with %t and regular text' , 35741 , 358'l%=m_l %t m_r%=r' , 359'l~~~~~~~~m_l buffer_spec.lua m_r~~~~~~~~r' , 360{ file_name ='test/unit/buffer_spec.lua' } 361) 362statusline_test_align ( 363'works well with %=, %t, %L, and %l' , 36441 , 365'%t %= %L %= %l' , 366'buffer_spec.lua ~~~~~~~~~~ 1 ~~~~~~~~~~ 0' , 367{ file_name ='test/unit/buffer_spec.lua' } 368) 369 370statusline_test_align ( 371'works with 10 %=' , 37250 , 373'aaaa%=b%=c%=d%=e%=fg%=hi%=jk%=lmnop%=qrstuv%=wxyz' , 374'aaaa~~b~~c~~d~~e~~fg~~hi~~jk~~lmnop~~qrstuv~~~wxyz' 375) 376 377-- item group testing 378 379statusline_test_align ( 380'right aligns in right aligned groups' , 38130 , 382'%5(%l%), %5.(%l%), %5.5(%l%), %5.10(%l%)' , 383'~~~~0, ~~~~0, ~~~~0, ~~~~0' , 384{ expected_cell_count =26 } 385) 386 387statusline_test_align ( 388'left aligns in left aligned groups' , 38930 , 390'%-5(%l%), %-5.(%l%), %-5.5(%l%), %-5.10(%l%)' , 391'0~~~~, 0~~~~, 0~~~~, 0~~~~' , 392{ expected_cell_count =26 } 393) 394 395statusline_test_align ( 396'expands at %= in item groups according to minwid' , 39760 , 398'%10(neo%=vim%), %10.(neo%=vim%), %10.10(neo%=vim%), %10.20(neo%=vim%)' , 399'neo~~~~vim, neo~~~~vim, neo~~~~vim, neo~~~~vim' , 400{ expected_cell_count =46 } 401) 402 403statusline_test_align ( 404'expands at %= in item groups with normal items' , 40560 , 406'%10(%L%=%l,%c%), %10(%=%L%=%l,%c%=%), %10(x%=%L%=%l,%c%=y%)' , 407'1~~~~~~0,0, ~~1~~0,0~~, x~1~0,0~~y' , 408{ expected_cell_count =34 } 409) 410 411statusline_test_align ( 412'expands at %= in nested item groups and top-level' , 41329 , 414'%21(foo%=%7(x%=y%=z%)%=bar%)%=%5(a%=b%=c%)' , 415'foo~~~~x~~y~~z~~~~bar~~~a~b~c' 416) 417 418statusline_test ( 419'ignores unexpanded %= in item groups when expanding at top-level' , 42020 , 421'%4(%L%=neo%)vim' , 422'1neovim' , 423{ expected_cell_count =7 } 424) 425 426statusline_test ( 427'ignores truncated %= in item groups when expanding at top-level' , 42820 , 429'%10.10(%<hidden%=%f%)TEST' , 430'<_spec.luaTEST' , 431{ file_name ='test/unit/buffer_spec.lua' , expected_cell_count =14 } 432) 433 434statusline_test ( 435'ignores %= in hidden item groups when expanding at top-level' , 43620 , 437'%(hidden%=%h%)TEST' , 438'TEST' , 439{ expected_cell_count =4 } 440) 441 442statusline_test ( 443'truncates item groups according to maxwid' , 44430 , 445'%.5(1234567%), %2.5(1234567%), %5.5(1234567%)' , 446'<4567, <4567, <4567' , 447{ expected_cell_count =19 } 448) 449 450statusline_test ( 451'truncates at first normal item in item groups' , 45230 , 453'%.15(path: %f%)' , 454'path: <spec.lua' , 455{ file_name ='test/unit/buffer_spec.lua' , expected_cell_count =15 } 456) 457 458statusline_test ( 459'truncates at %< in item groups' , 46060 , 461'%.15(%<path: %f%), %.15(path: %<%f%), %.15(path: %f%<%)' , 462'<uffer_spec.lua, path: <spec.lua, path: test/uni>' , 463{ file_name ='test/unit/buffer_spec.lua' , expected_cell_count =49 } 464) 465 466statusline_test ( 467'truncates at %< in nested item groups and top-level' , 46840 , 469'%.24(trim both ends: %<%.17(%f%<%)%), path: %<%f' , 470'trim both ends: <buffer>, path: <pec.lua' , 471{ file_name ='test/unit/buffer_spec.lua' } 472) 473 474statusline_test ( 475'ignores unused %< in item groups when truncating at top-level' , 47625 , 477'%.6(%L%< test%) file: %<%f' , 478'1 test file: <er_spec.lua' , 479{ file_name ='test/unit/buffer_spec.lua' } 480) 481 482statusline_test ( 483'ignores second %< in item groups when truncating at top-level' , 48432 , 485'%.15(file: %<%f%<%), file: %<%f' , 486'file: <spec.lua, file: <spec.lua' , 487{ file_name ='test/unit/buffer_spec.lua' } 488) 489 490statusline_test ( 491'ignores %< in hidden item groups when truncating at top-level' , 49215 , 493'%.15(hidden%<%)%f%<' , 494'test/unit/buff>' , 495{ file_name ='test/unit/buffer_spec.lua' } 496) 497 498statusline_test ( 499'hides auto-hiding item group with only empty normal items' , 50029 , 501'visible: %(pre-%h%f-post%), hidden: %(pre-%h%w-post%)' , 502'visible: pre-X-post, hidden: ' , 503{ file_name ='X' } 504) 505 506statusline_test ( 507'hides auto-hiding item group with expression evaluating to empty string' , 50829 , 509"visible: %(pre-%{'X'}-post%), hidden: %(pre-%{''}-post%)" , 510'visible: pre-X-post, hidden: ' 511) 512 513statusline_test ( 514'hides auto-hiding item group with re-evaluated expression evaluating to empty string' , 51529 , 516"visible: %(pre-%{%'%1*X%*'%}-post%), hidden: %(pre-%{%''%}-post%)" , 517'visible: pre-X-post, hidden: ' 518) 519 520statusline_test ( 521'hides auto-hiding item group with re-evaluated expression evaluating to only empty normal items' , 52229 , 523"visible: %(pre-%{%'%h%f'%}-post%), hidden: %(pre-%{%'%h%w'%}-post%)" , 524'visible: pre-X-post, hidden: ' , 525{ file_name ='X' } 526) 527 528statusline_test ( 529'hides auto-hiding item group with nested re-evaluated expressions evaluating to empty string' , 53029 , 531"visible: %(pre-%{%'%{%''%1*X%*''%'..nr2char(125)%}-post%), hidden: %(pre-%{%'%{%''''%'..nr2char(125)%}-post%)" , 532'visible: pre-X-post, hidden: ' 533) 534 535statusline_test ( 536'hides auto-hiding item group with nested re-evaluated expressions evaluating to only empty normal items' , 53729 , 538"visible: %(pre-%{%'%h%{%''%f''%'..nr2char(125)%}-post%), hidden: %(pre-%{%'%h%{%''%w''%'..nr2char(125)%}-post%)" , 539'visible: pre-X-post, hidden: ' , 540{ file_name ='X' } 541) 542 543-- stl item testing 544local tabline ='' 545for i =1 , 1000 do 546tabline =tabline .. ( i % 2 == 0 and '%#TabLineSel#' or '%#TabLineFill#' ) .. tostring ( i % 2 ) 547end 548statusline_test ( 'handles a large amount of any items' , 20 , tabline , '<1010101010101010101' ) -- Should not show any error 549statusline_test ( 550'handles a larger amount of = than stl initial item' , 55120 , 552( '%=' ): rep ( STL_INITIAL_ITEMS * 5 ), 553' ' 554) -- Should not show any error 555statusline_test ( 556'handles many extra characters' , 55720 , 558'a' .. ( 'a' ): rep ( STL_INITIAL_ITEMS * 5 ), 559'<aaaaaaaaaaaaaaaaaaa' 560) -- Does not show any error 561statusline_test ( 562'handles many extra characters and flags' , 56320 , 564'a' .. ( '%=a' ): rep ( STL_INITIAL_ITEMS * 2 ), 565'a<aaaaaaaaaaaaaaaaaa' 566) -- Should not show any error 567 568-- multi-cell character testing 569statusline_test ( 570'returns reduced width after truncating top-level from the left at multicell character' , 5715 , 572'12🙂345' , 573'<345' , 574{ expected_cell_count =4 } 575) 576statusline_test ( 577'returns reduced width after truncating top-level from the right at multicell character' , 5785 , 579'123🙂45%<' , 580'123>' , 581{ expected_cell_count =4 } 582) 583statusline_test_align ( 584'compensates with fillchar to reach minwid after truncating item group at multicell character' , 585100 , 586'%.5(12🙂345%), %4.5(12🙂345%), %5.5(12🙂345%), %-5.5(12🙂345%), %50.5(12🙂345%), %.5(123🙂%L%<%), %4.5(123🙂%L%<%), %5.5(123🙂%L%<%), %-5.5(123🙂%L%<%), %50.5(123🙂%L%<%)' , 587'<345, <345, <345~, <345~, <345~, 123>, 123>, 123>~, 123>~, 123>~' , 588{ expected_cell_count =64 } 589) 590 591-- multi-byte testing 592statusline_test ( 'handles multibyte characters' , 10 , 'Ĉ%=x' , 'Ĉ x' ) 593statusline_test ( 594'handles multibyte characters and different fillchars' , 59510 , 596'Ą%=mid%=end' , 597'Ą@mid@@end' , 598{ fillchar ='@' } 599) 600for _ , fillchar in ipairs ( { '-' , '±' , '━' , '𝕚' } ) do 601local minwid =math . floor ( 2147483647 / # fillchar ) + 10000 -- this supposes 4-byte integers 602itp ( "doesn't crash due to integer overflow with fillchar " .. fillchar , function () 603build_stl_str_hl { 604pat ='%' .. tostring ( minwid ) .. '(%)' , 605maximum_cell_count =10 , 606fillchar =fillchar , 607} 608end ) 609end 610 611-- escaping % testing 612statusline_test ( 'handles escape of %' , 4 , 'abc%%' , 'abc%' ) 613statusline_test ( 'case where escaped % does not fit' , 3 , 'abc%%abcabc' , '<bc' ) 614statusline_test ( 'escaped % is first' , 1 , '%%' , '%' ) 615end )