robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.unit.testutil' ) 2local describe =t . describe 3local itp =t . gen_itp ( t . it ) 4 5local ffi =t . ffi 6local eq =t . eq 7local to_cstr =t . to_cstr 8 9local cimp =t . cimport ( './src/nvim/message.h' , './src/nvim/memory.h' , './src/nvim/strings.h' ) 10 11describe ( 'trunc_string' , function () 12local buflen =40 13local function test_inplace ( s , expected , room ) 14room =room and room or 20 15local buf =cimp . xmalloc ( ffi . sizeof ( 'char' ) * buflen ) 16ffi . C . strcpy ( buf , s ) 17cimp . trunc_string ( buf , buf , room , buflen ) 18eq ( expected , ffi . string ( buf )) 19cimp . xfree ( buf ) 20end 21 22local function test_copy ( s , expected , room ) 23room =room and room or 20 24local buf =cimp . xmalloc ( ffi . sizeof ( 'char' ) * buflen ) 25local str =cimp . xstrdup ( to_cstr ( s )) 26cimp . trunc_string ( str , buf , room , buflen ) 27eq ( expected , ffi . string ( buf )) 28cimp . xfree ( buf ) 29cimp . xfree ( str ) 30end 31 32local permutations ={ 33{ [ 'desc' ] ='in-place' , [ 'func' ] =test_inplace } , 34{ [ 'desc' ] ='by copy' , [ 'func' ] =test_copy } , 35} 36 37for _ , q in ipairs ( permutations ) do 38describe ( 'populates buf ' .. q . desc , function () 39itp ( 'with a small string' , function () 40q . func ( 'text' , 'text' ) 41end ) 42 43itp ( 'with a medium string' , function () 44q . func ( 'a short text' , 'a short text' ) 45end ) 46 47itp ( 'with a string of length == 1/2 room' , function () 48q . func ( 'a text that fits' , 'a text that fits' , 34 ) 49end ) 50 51itp ( 'with a string exactly the truncate size' , function () 52q . func ( 'a text tha just fits' , 'a text tha just fits' ) 53end ) 54 55itp ( 'with a string that must be truncated' , function () 56q . func ( 'a text that nott fits' , 'a text t...nott fits' ) 57end ) 58end ) 59end 60end )