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 =t . describe , t . it , t . setup 5local clear =n . clear 6local eq =t . eq 7local pcall_err =t . pcall_err 8local eval =n . eval 9local fn =n . fn 10local api =n . api 11local command =n . command 12 13describe ( 'printf()' , function () 14setup ( clear ) 15 16it ( 'works with zero and %b' , function () 17eq ( '0' , fn . printf ( '%lb' , 0 )) 18eq ( '0' , fn . printf ( '%llb' , 0 )) 19eq ( '0' , fn . printf ( '%zb' , 0 )) 20end ) 21it ( 'works with one and %b' , function () 22eq ( '1' , fn . printf ( '%b' , 1 )) 23eq ( '1' , fn . printf ( '%lb' , 1 )) 24eq ( '1' , fn . printf ( '%llb' , 1 )) 25eq ( '1' , fn . printf ( '%zb' , 1 )) 26end ) 27it ( 'works with 0xff and %b' , function () 28eq ( '11111111' , fn . printf ( '%b' , 0xff )) 29eq ( '11111111' , fn . printf ( '%lb' , 0xff )) 30eq ( '11111111' , fn . printf ( '%llb' , 0xff )) 31eq ( '11111111' , fn . printf ( '%zb' , 0xff )) 32end ) 33it ( 'accepts width modifier with %b' , function () 34eq ( ' 1' , fn . printf ( '%3b' , 1 )) 35end ) 36it ( 'accepts prefix modifier with %b' , function () 37eq ( '0b1' , fn . printf ( '%#b' , 1 )) 38end ) 39it ( 'writes capital B with %B' , function () 40eq ( '0B1' , fn . printf ( '%#B' , 1 )) 41end ) 42it ( 'accepts prefix, zero-fill and width modifiers with %b' , function () 43eq ( '0b001' , fn . printf ( '%#05b' , 1 )) 44end ) 45it ( 'accepts prefix and width modifiers with %b' , function () 46eq ( ' 0b1' , fn . printf ( '%#5b' , 1 )) 47end ) 48it ( 'does not write prefix for zero with prefix and width modifier used with %b' , function () 49eq ( ' 0' , fn . printf ( '%#5b' , 0 )) 50end ) 51it ( 'accepts precision modifier with %b' , function () 52eq ( '00000' , fn . printf ( '%.5b' , 0 )) 53end ) 54it ( 'accepts all modifiers with %b at once' , function () 55-- zero-fill modifier is ignored when used with left-align 56-- force-sign and add-blank are ignored 57-- use-grouping-characters modifier is ignored always 58eq ( '0b00011 ' , fn . printf ( "% '+#0-10.5b" , 3 )) 59end ) 60it ( 'errors out when %b modifier is used for a list' , function () 61eq ( 'Vim(call):E745: Using a List as a Number' , pcall_err ( command , 'call printf("%b", [])' )) 62end ) 63it ( 'errors out when %b modifier is used for a float' , function () 64eq ( 65'Vim(call):E805: Using a Float as a Number' , 66pcall_err ( command , 'call printf("%b", 3.1415926535)' ) 67) 68end ) 69it ( 'works with %p correctly' , function () 70local null_ret =nil 71local seen_rets ={} 72-- Collect all args in an array to avoid possible allocation of the same 73-- address after freeing unreferenced values. 74api . nvim_set_var ( '__args' , {} ) 75local function check_printf ( expr , is_null ) 76command ( 'call add(__args, ' .. expr .. ')' ) 77command ( 'let __result = printf("%p", __args[-1])' ) 78local id_ret =eval ( 'id(__args[-1])' ) 79eq ( id_ret , api . nvim_get_var ( '__result' )) 80if is_null then 81if null_ret then 82eq ( null_ret , id_ret ) 83else 84null_ret =id_ret 85end 86else 87eq ( nil , seen_rets [ id_ret ]) 88seen_rets [ id_ret ] =expr 89end 90api . nvim_del_var ( '__result' ) 91end 92check_printf ( 'v:_null_string' , true ) 93check_printf ( 'v:_null_list' , true ) 94check_printf ( 'v:_null_dict' , true ) 95check_printf ( 'v:_null_blob' , true ) 96check_printf ( '[]' ) 97check_printf ( '{}' ) 98check_printf ( '0z' ) 99check_printf ( 'function("tr", ["a"])' ) 100end ) 101end )