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 child_call_once =t . child_call_once 6local cimport =t . cimport 7local eq =t . eq 8local ffi =t . ffi 9local to_cstr =t . to_cstr 10 11local strings =cimport ( 'stdlib.h' , './src/nvim/strings.h' , './src/nvim/memory.h' ) 12 13local UVARNUM_TYPE 14 15child_call_once ( function () 16UVARNUM_TYPE =ffi . typeof ( 'uvarnumber_T' ) 17end ) 18 19describe ( 'vim_strsave_escaped()' , function () 20local vim_strsave_escaped =function ( s , chars ) 21local res =strings . vim_strsave_escaped ( to_cstr ( s ), to_cstr ( chars )) 22local ret =ffi . string ( res ) 23 24-- Explicitly free memory so we are sure it is allocated: if it was not it 25-- will crash. 26strings . xfree ( res ) 27return ret 28end 29 30itp ( 'precedes by a backslash all chars from second argument' , function () 31eq ( [[\a\b\c\d]] , vim_strsave_escaped ( 'abcd' , 'abcd' )) 32end ) 33 34itp ( 'precedes by a backslash chars only from second argument' , function () 35eq ( [[\a\bcd]] , vim_strsave_escaped ( 'abcd' , 'ab' )) 36end ) 37 38itp ( 'returns a copy of passed string if second argument is empty' , function () 39eq ( 'text \n text' , vim_strsave_escaped ( 'text \n text' , '' )) 40end ) 41 42itp ( 'returns an empty string if first argument is empty string' , function () 43eq ( '' , vim_strsave_escaped ( '' , '\r' )) 44end ) 45 46itp ( 'returns a copy of passed string if it does not contain chars from 2nd argument' , function () 47eq ( 'some text' , vim_strsave_escaped ( 'some text' , 'a' )) 48end ) 49end ) 50 51describe ( 'vim_strnsave_unquoted()' , function () 52local vim_strnsave_unquoted =function ( s , len ) 53local res =strings . vim_strnsave_unquoted ( to_cstr ( s ), len or # s ) 54local ret =ffi . string ( res ) 55-- Explicitly free memory so we are sure it is allocated: if it was not it 56-- will crash. 57strings . xfree ( res ) 58return ret 59end 60 61itp ( 'copies unquoted strings as-is' , function () 62eq ( '-c' , vim_strnsave_unquoted ( '-c' )) 63eq ( '' , vim_strnsave_unquoted ( '' )) 64end ) 65 66itp ( 'respects length argument' , function () 67eq ( '' , vim_strnsave_unquoted ( '-c' , 0 )) 68eq ( '-' , vim_strnsave_unquoted ( '-c' , 1 )) 69eq ( '-' , vim_strnsave_unquoted ( '"-c' , 2 )) 70end ) 71 72itp ( 'unquotes fully quoted word' , function () 73eq ( '/bin/sh' , vim_strnsave_unquoted ( '"/bin/sh"' )) 74end ) 75 76itp ( 'unquotes partially quoted word' , function () 77eq ( '/Program Files/sh' , vim_strnsave_unquoted ( '/Program" "Files/sh' )) 78end ) 79 80itp ( 'removes ""' , function () 81eq ( '/Program Files/sh' , vim_strnsave_unquoted ( '/""Program" "Files/sh' )) 82end ) 83 84itp ( 'performs unescaping of "' , function () 85eq ( '/"Program Files"/sh' , vim_strnsave_unquoted ( '/"\\""Program Files"\\""/sh' )) 86end ) 87 88itp ( 'performs unescaping of \\' , function () 89eq ( '/\\Program Files\\foo/sh' , vim_strnsave_unquoted ( '/"\\\\"Program Files"\\\\foo"/sh' )) 90end ) 91 92itp ( 'strips quote when there is no pair to it' , function () 93eq ( '/Program Files/sh' , vim_strnsave_unquoted ( '/Program" Files/sh' )) 94eq ( '' , vim_strnsave_unquoted ( '"' )) 95end ) 96 97itp ( 'allows string to end with one backslash unescaped' , function () 98eq ( '/Program Files/sh\\' , vim_strnsave_unquoted ( '/Program" Files/sh\\' )) 99end ) 100 101itp ( 'does not perform unescaping out of quotes' , function () 102eq ( '/Program\\ Files/sh\\' , vim_strnsave_unquoted ( '/Program\\ Files/sh\\' )) 103end ) 104 105itp ( 'does not unescape \\n' , function () 106eq ( '/Program\\nFiles/sh' , vim_strnsave_unquoted ( '/Program"\\n"Files/sh' )) 107end ) 108end ) 109 110describe ( 'vim_strchr()' , function () 111local vim_strchr =function ( s , c ) 112local str =to_cstr ( s ) 113local res =strings . vim_strchr ( str , c ) 114if res == nil then 115return nil 116else 117return res - str 118end 119end 120itp ( 'handles NUL and <0 correctly' , function () 121eq ( nil , vim_strchr ( 'abc' , 0 )) 122eq ( nil , vim_strchr ( 'abc' , - 1 )) 123end ) 124itp ( 'works' , function () 125eq ( 0 , vim_strchr ( 'abc' , ( 'a' ): byte ())) 126eq ( 1 , vim_strchr ( 'abc' , ( 'b' ): byte ())) 127eq ( 2 , vim_strchr ( 'abc' , ( 'c' ): byte ())) 128eq ( 0 , vim_strchr ( 'a«b»c' , ( 'a' ): byte ())) 129eq ( 3 , vim_strchr ( 'a«b»c' , ( 'b' ): byte ())) 130eq ( 6 , vim_strchr ( 'a«b»c' , ( 'c' ): byte ())) 131 132eq ( nil , vim_strchr ( '«»' , ( '«' ): byte ())) 133-- 0xAB == 171 == '«' 134eq ( nil , vim_strchr ( '\171' , 0xAB )) 135eq ( 0 , vim_strchr ( '«»' , 0xAB )) 136eq ( 3 , vim_strchr ( '„«»“' , 0xAB )) 137 138eq ( 7 , vim_strchr ( '„«»“' , 0x201C )) 139eq ( nil , vim_strchr ( '„«»“' , 0x201D )) 140eq ( 0 , vim_strchr ( '„«»“' , 0x201E )) 141 142eq ( 0 , vim_strchr ( '\244\143\188\128' , 0x10FF00 )) 143eq ( 2 , vim_strchr ( '«\244\143\188\128»' , 0x10FF00 )) 144-- |0xDBFF |0xDF00 - surrogate pair for 0x10FF00 145eq ( nil , vim_strchr ( '«\237\175\191\237\188\128»' , 0x10FF00 )) 146end ) 147end ) 148 149describe ( 'vim_snprintf()' , function () 150local function a ( expected , buf , bsize , fmt , ... ) 151local args ={ ... } 152local ctx =string . format ( 'snprintf(buf, %d, "%s"' , bsize , fmt ) 153for _ , x in ipairs ( args ) do 154ctx =ctx .. ', ' .. tostring ( x ) 155end 156ctx =ctx .. string . format ( ') = %s' , expected ) 157eq ( # expected , strings . vim_snprintf ( buf , bsize , fmt , ... ), ctx ) 158if bsize > 0 then 159local actual =ffi . string ( buf , math . min ( # expected + 1 , bsize )) 160eq ( expected : sub ( 1 , bsize - 1 ) .. '\0' , actual , ctx ) 161end 162end 163 164local function uv ( n ) 165return ffi . cast ( UVARNUM_TYPE , n ) 166end 167local function i ( n ) 168return ffi . cast ( 'int' , n ) 169end 170local function l ( n ) 171return ffi . cast ( 'long' , n ) 172end 173local function ll ( n ) 174return ffi . cast ( 'long long' , n ) 175end 176local function z ( n ) 177return ffi . cast ( 'ptrdiff_t' , n ) 178end 179local function u ( n ) 180return ffi . cast ( 'unsigned' , n ) 181end 182local function ul ( n ) 183return ffi . cast ( 'unsigned long' , n ) 184end 185local function ull ( n ) 186return ffi . cast ( 'unsigned long long' , n ) 187end 188local function uz ( n ) 189return ffi . cast ( 'size_t' , n ) 190end 191 192itp ( 'truncation' , function () 193for bsize =0 , 14 do 194local buf =ffi . gc ( strings . xmalloc ( bsize ), strings . xfree ) 195a ( '1.00000001e7' , buf , bsize , '%.8g' , 10000000.1 ) 196a ( '1234567' , buf , bsize , '%d' , i ( 1234567 )) 197a ( '1234567' , buf , bsize , '%ld' , l ( 1234567 )) 198a ( ' 1234567' , buf , bsize , '%9ld' , l ( 1234567 )) 199a ( '1234567 ' , buf , bsize , '%-9ld' , l ( 1234567 )) 200a ( 'deadbeef' , buf , bsize , '%x' , u ( 0xdeadbeef )) 201a ( '001100' , buf , bsize , '%06b' , uv ( 12 )) 202a ( 'one two' , buf , bsize , '%s %s' , 'one' , 'two' ) 203a ( '1.234000' , buf , bsize , '%f' , 1.234 ) 204a ( '1.234000e+00' , buf , bsize , '%e' , 1.234 ) 205a ( 'nan' , buf , bsize , '%f' , 0.0 / 0.0 ) 206a ( 'inf' , buf , bsize , '%f' , 1.0 / 0.0 ) 207a ( '-inf' , buf , bsize , '%f' , - 1.0 / 0.0 ) 208a ( '-0.000000' , buf , bsize , '%f' , tonumber ( '-0.0' )) 209a ( '漢語' , buf , bsize , '%s' , '漢語' ) 210a ( ' 漢語' , buf , bsize , '%8s' , '漢語' ) 211a ( '漢語 ' , buf , bsize , '%-8s' , '漢語' ) 212a ( '漢' , buf , bsize , '%.3s' , '漢語' ) 213a ( ' foo' , buf , bsize , '%5S' , 'foo' ) 214a ( '%%%' , buf , bsize , '%%%%%%' ) 215a ( '0x87654321' , buf , bsize , '%p' , ffi . cast ( 'char *' , 0x87654321 )) 216a ( '0x0087654321' , buf , bsize , '%012p' , ffi . cast ( 'char *' , 0x87654321 )) 217end 218end ) 219 220itp ( 'positional arguments' , function () 221for bsize =0 , 24 do 222local buf =ffi . gc ( strings . xmalloc ( bsize ), strings . xfree ) 223a ( '1234567 ' , buf , bsize , '%1$*2$ld' , l ( 1234567 ), i ( - 9 )) 224a ( '1234567 ' , buf , bsize , '%1$*2$.*3$ld' , l ( 1234567 ), i ( - 9 ), i ( 5 )) 225a ( '1234567 ' , buf , bsize , '%1$*3$.*2$ld' , l ( 1234567 ), i ( 5 ), i ( - 9 )) 226a ( '1234567 ' , buf , bsize , '%3$*1$.*2$ld' , i ( - 9 ), i ( 5 ), l ( 1234567 )) 227a ( '1234567' , buf , bsize , '%1$ld' , l ( 1234567 )) 228a ( ' 1234567' , buf , bsize , '%1$*2$ld' , l ( 1234567 ), i ( 9 )) 229a ( '9 12345 7654321' , buf , bsize , '%2$ld %1$d %3$lu' , i ( 12345 ), l ( 9 ), ul ( 7654321 )) 230a ( '9 1234567 7654321' , buf , bsize , '%2$d %1$ld %3$lu' , l ( 1234567 ), i ( 9 ), ul ( 7654321 )) 231a ( '9 1234567 7654321' , buf , bsize , '%2$d %1$lld %3$lu' , ll ( 1234567 ), i ( 9 ), ul ( 7654321 )) 232a ( '9 12345 7654321' , buf , bsize , '%2$ld %1$u %3$lu' , u ( 12345 ), l ( 9 ), ul ( 7654321 )) 233a ( '9 1234567 7654321' , buf , bsize , '%2$d %1$lu %3$lu' , ul ( 1234567 ), i ( 9 ), ul ( 7654321 )) 234a ( '9 1234567 7654321' , buf , bsize , '%2$d %1$llu %3$lu' , ull ( 1234567 ), i ( 9 ), ul ( 7654321 )) 235a ( '9 deadbeef 7654321' , buf , bsize , '%2$d %1$x %3$lu' , u ( 0xdeadbeef ), i ( 9 ), ul ( 7654321 )) 236a ( '9 c 7654321' , buf , bsize , '%2$ld %1$c %3$lu' , i (( 'c' ): byte ()), l ( 9 ), ul ( 7654321 )) 237a ( '9 hi 7654321' , buf , bsize , '%2$ld %1$s %3$lu' , 'hi' , l ( 9 ), ul ( 7654321 )) 238a ( '9 0.000000e+00 7654321' , buf , bsize , '%2$ld %1$e %3$lu' , 0.0 , l ( 9 ), ul ( 7654321 )) 239a ( 'two one two' , buf , bsize , '%2$s %1$s %2$s' , 'one' , 'two' , 'three' ) 240a ( 'three one two' , buf , bsize , '%3$s %1$s %2$s' , 'one' , 'two' , 'three' ) 241a ( '1234567' , buf , bsize , '%1$d' , i ( 1234567 )) 242a ( 'deadbeef' , buf , bsize , '%1$x' , u ( 0xdeadbeef )) 243a ( '001100' , buf , bsize , '%2$0*1$b' , i ( 6 ), uv ( 12 )) 244a ( '001100' , buf , bsize , '%1$0.*2$b' , uv ( 12 ), i ( 6 )) 245a ( 'one two' , buf , bsize , '%1$s %2$s' , 'one' , 'two' ) 246a ( '001100' , buf , bsize , '%06b' , uv ( 12 )) 247a ( 'two one' , buf , bsize , '%2$s %1$s' , 'one' , 'two' ) 248a ( '1.234000' , buf , bsize , '%1$f' , 1.234 ) 249a ( '1.234000e+00' , buf , bsize , '%1$e' , 1.234 ) 250a ( 'nan' , buf , bsize , '%1$f' , 0.0 / 0.0 ) 251a ( 'inf' , buf , bsize , '%1$f' , 1.0 / 0.0 ) 252a ( '-inf' , buf , bsize , '%1$f' , - 1.0 / 0.0 ) 253a ( '-0.000000' , buf , bsize , '%1$f' , tonumber ( '-0.0' )) 254end 255end ) 256 257itp ( '%zd and %zu' , function () 258local bsize =20 259local buf =ffi . gc ( strings . xmalloc ( bsize ), strings . xfree ) 260a ( '-1234567 -7654321' , buf , bsize , '%zd %zd' , z ( - 1234567 ), z ( - 7654321 )) 261a ( '-7654321 -1234567' , buf , bsize , '%2$zd %1$zd' , z ( - 1234567 ), z ( - 7654321 )) 262a ( '1234567 7654321' , buf , bsize , '%zu %zu' , uz ( 1234567 ), uz ( 7654321 )) 263a ( '7654321 1234567' , buf , bsize , '%2$zu %1$zu' , uz ( 1234567 ), uz ( 7654321 )) 264end ) 265end ) 266 267describe ( 'strcase_save()' , function () 268local strcase_save =function ( input_string , upper ) 269local res =strings . strcase_save ( to_cstr ( input_string ), upper ) 270return ffi . string ( res ) 271end 272 273itp ( 'decodes overlong encoded characters.' , function () 274eq ( 'A' , strcase_save ( '\xc1\x81' , true )) 275eq ( 'a' , strcase_save ( '\xc1\x81' , false )) 276end ) 277end ) 278 279describe ( 'reverse_text' , function () 280local reverse_text =function ( str ) 281return t . internalize ( strings . reverse_text ( to_cstr ( str ))) 282end 283 284itp ( 'handles empty string' , function () 285eq ( '' , reverse_text ( '' )) 286end ) 287 288itp ( 'handles simple cases' , function () 289eq ( 'a' , reverse_text ( 'a' )) 290eq ( 'ba' , reverse_text ( 'ab' )) 291end ) 292 293itp ( 'handles multibyte characters' , function () 294eq ( 'bα' , reverse_text ( 'αb' )) 295eq ( 'Yötön yö' , reverse_text ( 'öy nötöY' )) 296end ) 297 298itp ( 'handles combining chars' , function () 299local utf8_COMBINING_RING_ABOVE ='\204\138' 300local utf8_COMBINING_RING_BELOW ='\204\165' 301eq ( 302'bba' .. utf8_COMBINING_RING_ABOVE .. utf8_COMBINING_RING_BELOW .. 'aa' , 303reverse_text ( 'aaa' .. utf8_COMBINING_RING_ABOVE .. utf8_COMBINING_RING_BELOW .. 'bb' ) 304) 305end ) 306 307itp ( 'treats invalid utf as separate characters' , function () 308eq ( '\192ba' , reverse_text ( 'ab\192' )) 309end ) 310 311itp ( 'treats an incomplete utf continuation sequence as valid' , function () 312eq ( '\194ba' , reverse_text ( 'ab\194' )) 313end ) 314end )