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 eq =t . eq 6local pcall_err =t . pcall_err 7local NIL =vim . NIL 8local eval =n . eval 9local clear =n . clear 10local api =n . api 11local fn =n . fn 12local source =n . source 13local dedent =t . dedent 14local command =n . command 15local exec_capture =n . exec_capture 16local matches =t . matches 17 18describe ( ':echo :echon :echomsg :echoerr' , function () 19local fn_tbl ={ 'String' , 'StringN' , 'StringMsg' , 'StringErr' } 20local function assert_same_echo_dump ( expected , input , use_eval ) 21for _ , v in pairs ( fn_tbl ) do 22eq ( expected , use_eval and eval ( v .. '(' .. input .. ')' ) or fn [ v ]( input )) 23end 24end 25local function assert_matches_echo_dump ( expected , input , use_eval ) 26for _ , v in pairs ( fn_tbl ) do 27matches ( expected , use_eval and eval ( v .. '(' .. input .. ')' ) or fn [ v ]( input )) 28end 29end 30 31setup ( function () 32clear () 33source ( [[ 34function String(s) 35return execute('echo a:s')[1:] 36endfunction 37function StringMsg(s) 38return execute('echomsg a:s')[1:] 39endfunction 40function StringN(s) 41return execute('echon a:s') 42endfunction 43function StringErr(s) 44try 45execute 'echoerr a:s' 46catch 47return substitute(v:exception, '^Vim(echoerr):', '', '') 48endtry 49endfunction 50]] ) 51end ) 52 53describe ( 'used to represent floating-point values' , function () 54it ( 'dumps NaN values' , function () 55assert_same_echo_dump ( "str2float('nan')" , "str2float('nan')" , true ) 56end ) 57 58it ( 'dumps infinite values' , function () 59assert_same_echo_dump ( "str2float('inf')" , "str2float('inf')" , true ) 60assert_same_echo_dump ( "-str2float('inf')" , "str2float('-inf')" , true ) 61end ) 62 63it ( 'dumps regular values' , function () 64assert_same_echo_dump ( '1.5' , 1.5 ) 65assert_same_echo_dump ( '1.56e-20' , 1.56000e-020 ) 66assert_same_echo_dump ( '0.0' , '0.0' , true ) 67end ) 68 69it ( 'dumps special v: values' , function () 70eq ( 'v:true' , eval ( 'String(v:true)' )) 71eq ( 'v:false' , eval ( 'String(v:false)' )) 72eq ( 'v:null' , eval ( 'String(v:null)' )) 73eq ( 'v:true' , fn . String ( true )) 74eq ( 'v:false' , fn . String ( false )) 75eq ( 'v:null' , fn . String ( NIL )) 76eq ( 'v:true' , eval ( 'StringMsg(v:true)' )) 77eq ( 'v:false' , eval ( 'StringMsg(v:false)' )) 78eq ( 'v:null' , eval ( 'StringMsg(v:null)' )) 79eq ( 'v:true' , fn . StringMsg ( true )) 80eq ( 'v:false' , fn . StringMsg ( false )) 81eq ( 'v:null' , fn . StringMsg ( NIL )) 82eq ( 'v:true' , eval ( 'StringErr(v:true)' )) 83eq ( 'v:false' , eval ( 'StringErr(v:false)' )) 84eq ( 'v:null' , eval ( 'StringErr(v:null)' )) 85eq ( 'v:true' , fn . StringErr ( true )) 86eq ( 'v:false' , fn . StringErr ( false )) 87eq ( 'v:null' , fn . StringErr ( NIL )) 88end ) 89 90it ( 'dumps values with at most six digits after the decimal point' , function () 91assert_same_echo_dump ( '1.234568e-20' , 1.23456789123456789123456789e-020 ) 92assert_same_echo_dump ( '1.234568' , 1.23456789123456789123456789 ) 93end ) 94 95it ( 'dumps values with at most seven digits before the decimal point' , function () 96assert_same_echo_dump ( '1234567.891235' , 1234567.89123456789123456789 ) 97assert_same_echo_dump ( '1.234568e7' , 12345678.9123456789123456789 ) 98end ) 99 100it ( 'dumps negative values' , function () 101assert_same_echo_dump ( '-1.5' , - 1.5 ) 102assert_same_echo_dump ( '-1.56e-20' , - 1.56000e-020 ) 103assert_same_echo_dump ( '-1.234568e-20' , - 1.23456789123456789123456789e-020 ) 104assert_same_echo_dump ( '-1.234568' , - 1.23456789123456789123456789 ) 105assert_same_echo_dump ( '-1234567.891235' , - 1234567.89123456789123456789 ) 106assert_same_echo_dump ( '-1.234568e7' , - 12345678.9123456789123456789 ) 107end ) 108end ) 109 110describe ( 'used to represent numbers' , function () 111it ( 'dumps regular values' , function () 112assert_same_echo_dump ( '0' , 0 ) 113assert_same_echo_dump ( '-1' , - 1 ) 114assert_same_echo_dump ( '1' , 1 ) 115end ) 116 117it ( 'dumps large values' , function () 118assert_same_echo_dump ( '2147483647' , 2 ^ 31 - 1 ) 119assert_same_echo_dump ( '-2147483648' , - 2 ^ 31 ) 120end ) 121end ) 122 123describe ( 'used to represent strings' , function () 124it ( 'dumps regular strings' , function () 125assert_same_echo_dump ( 'test' , 'test' ) 126end ) 127 128it ( 'dumps empty strings' , function () 129assert_same_echo_dump ( '' , '' ) 130end ) 131 132it ( "dumps strings with ' inside" , function () 133assert_same_echo_dump ( "'''" , "'''" ) 134assert_same_echo_dump ( "a'b''" , "a'b''" ) 135assert_same_echo_dump ( "'b''d" , "'b''d" ) 136assert_same_echo_dump ( "a'b'c'd" , "a'b'c'd" ) 137end ) 138 139it ( 'dumps NULL strings' , function () 140assert_same_echo_dump ( '' , '$XXX_UNEXISTENT_VAR_XXX' , true ) 141end ) 142 143it ( 'dumps NULL lists' , function () 144assert_same_echo_dump ( '[]' , 'v:_null_list' , true ) 145end ) 146 147it ( 'dumps NULL dictionaries' , function () 148assert_same_echo_dump ( '{}' , 'v:_null_dict' , true ) 149end ) 150end ) 151 152describe ( 'used to represent funcrefs' , function () 153setup ( function () 154source ( [[ 155function Test1() 156endfunction 157 158function s:Test2() dict 159endfunction 160 161function g:Test3() dict 162endfunction 163 164let g:Test2_f = function('s:Test2') 165]] ) 166end ) 167 168it ( 'dumps references to built-in functions' , function () 169eq ( 'function' , eval ( 'String(function("function"))' )) 170eq ( "function('function')" , eval ( 'StringMsg(function("function"))' )) 171eq ( "function('function')" , eval ( 'StringErr(function("function"))' )) 172end ) 173 174it ( 'dumps references to user functions' , function () 175eq ( 'Test1' , eval ( 'String(function("Test1"))' )) 176eq ( 'g:Test3' , eval ( 'String(function("g:Test3"))' )) 177eq ( "function('Test1')" , eval ( "StringMsg(function('Test1'))" )) 178eq ( "function('g:Test3')" , eval ( "StringMsg(function('g:Test3'))" )) 179eq ( "function('Test1')" , eval ( "StringErr(function('Test1'))" )) 180eq ( "function('g:Test3')" , eval ( "StringErr(function('g:Test3'))" )) 181end ) 182 183it ( 'dumps references to script functions' , function () 184eq ( '<SNR>1_Test2' , eval ( 'String(Test2_f)' )) 185eq ( "function('<SNR>1_Test2')" , eval ( 'StringMsg(Test2_f)' )) 186eq ( "function('<SNR>1_Test2')" , eval ( 'StringErr(Test2_f)' )) 187end ) 188 189it ( 'dump references to lambdas' , function () 190assert_matches_echo_dump ( "function%('<lambda>%d+'%)" , '{-> 1234}' , true ) 191end ) 192 193it ( 'dumps partials with self referencing a partial' , function () 194source ( [[ 195function TestDict() dict 196endfunction 197let d = {} 198let TestDictRef = function('TestDict', d) 199let d.tdr = TestDictRef 200]] ) 201eq ( 202dedent ( [[ 203function('TestDict', {'tdr': function('TestDict', {...@1})})]] ), 204exec_capture ( 'echo String(d.tdr)' ) 205) 206end ) 207 208it ( 'dumps automatically created partials' , function () 209assert_same_echo_dump ( 210"function('<SNR>1_Test2', {'f': function('<SNR>1_Test2')})" , 211'{"f": Test2_f}.f' , 212true 213) 214assert_same_echo_dump ( 215"function('<SNR>1_Test2', [1], {'f': function('<SNR>1_Test2', [1])})" , 216'{"f": function(Test2_f, [1])}.f' , 217true 218) 219end ) 220 221it ( 'dumps manually created partials' , function () 222assert_same_echo_dump ( "function('Test3', [1, 2], {})" , "function('Test3', [1, 2], {})" , true ) 223assert_same_echo_dump ( "function('Test3', [1, 2])" , "function('Test3', [1, 2])" , true ) 224assert_same_echo_dump ( "function('Test3', {})" , "function('Test3', {})" , true ) 225end ) 226 227it ( 'does not crash or halt when dumping partials with reference cycles in self' , function () 228api . nvim_set_var ( 'd' , { v =true } ) 229eq ( 230dedent ( 231[[ 232{'p': function('<SNR>1_Test2', {...@0}), 'f': function('<SNR>1_Test2'), 'v': v:true}]] 233), 234exec_capture ( 'echo String(extend(extend(g:d, {"f": g:Test2_f}), {"p": g:d.f}))' ) 235) 236end ) 237 238it ( 'does not show errors when dumping partials referencing the same dictionary' , function () 239command ( 'let d = {}' ) 240-- Regression for “eval/typval_encode: Dump empty dictionary before 241-- checking for refcycle”, results in error. 242eq ( 243"[function('tr', {}), function('tr', {})]" , 244eval ( 'String([function("tr", d), function("tr", d)])' ) 245) 246-- Regression for “eval: Work with reference cycles in partials (self) 247-- properly”, results in crash. 248eval ( 'extend(d, {"a": 1})' ) 249eq ( 250"[function('tr', {'a': 1}), function('tr', {'a': 1})]" , 251eval ( 'String([function("tr", d), function("tr", d)])' ) 252) 253end ) 254 255it ( 'does not crash or halt when dumping partials with reference cycles in arguments' , function () 256api . nvim_set_var ( 'l' , {} ) 257eval ( 'add(l, l)' ) 258-- Regression: the below line used to crash (add returns original list and 259-- there was error in dumping partials). Tested explicitly in 260-- test/unit/api/private_helpers_spec.lua. 261eval ( 'add(l, function("Test1", l))' ) 262eq ( 263dedent ( 264[=[ 265function('Test1', [[[...@2], function('Test1', [[...@2]])], function('Test1', [[[...@4], function('Test1', [[...@4]])]])])]=] 266), 267exec_capture ( 'echo String(function("Test1", l))' ) 268) 269end ) 270 271it ( 272'does not crash or halt when dumping partials with reference cycles in self and arguments' , 273function () 274api . nvim_set_var ( 'd' , { v =true } ) 275api . nvim_set_var ( 'l' , {} ) 276eval ( 'add(l, l)' ) 277eval ( 'add(l, function("Test1", l))' ) 278eval ( 'add(l, function("Test1", d))' ) 279eq ( 280dedent ( 281[=[ 282{'p': function('<SNR>1_Test2', [[[...@3], function('Test1', [[...@3]]), function('Test1', {...@0})], function('Test1', [[[...@5], function('Test1', [[...@5]]), function('Test1', {...@0})]]), function('Test1', {...@0})], {...@0}), 'f': function('<SNR>1_Test2'), 'v': v:true}]=] 283), 284exec_capture ( 285'echo String(extend(extend(g:d, {"f": g:Test2_f}), {"p": function(g:d.f, l)}))' 286) 287) 288end 289) 290end ) 291 292describe ( 'used to represent lists' , function () 293it ( 'dumps empty list' , function () 294assert_same_echo_dump ( '[]' , {} ) 295end ) 296 297it ( 'dumps non-empty list' , function () 298assert_same_echo_dump ( '[1, 2]' , { 1 , 2 } ) 299end ) 300 301it ( 'dumps nested lists' , function () 302assert_same_echo_dump ( '[[[[[]]]]]' , { { { { {} } } } } ) 303end ) 304 305it ( 'dumps nested non-empty lists' , function () 306assert_same_echo_dump ( '[1, [[3, [[5], 4]], 2]]' , { 1 , { { 3 , { { 5 } , 4 } } , 2 } } ) 307end ) 308 309it ( 'does not error when dumping recursive lists' , function () 310api . nvim_set_var ( 'l' , {} ) 311eval ( 'add(l, l)' ) 312command ( 'echo String(l)' ) 313end ) 314 315it ( 'dumps recursive lists without error' , function () 316api . nvim_set_var ( 'l' , {} ) 317eval ( 'add(l, l)' ) 318eq ( '[[...@0]]' , exec_capture ( 'echo String(l)' )) 319eq ( '[[[...@1]]]' , exec_capture ( 'echo String([l])' )) 320end ) 321end ) 322 323describe ( 'used to represent dictionaries' , function () 324it ( 'dumps empty dictionary' , function () 325assert_same_echo_dump ( '{}' , '{}' , true ) 326end ) 327 328it ( 'dumps list with two same empty dictionaries, also in partials' , function () 329command ( 'let d = {}' ) 330assert_same_echo_dump ( '[{}, {}]' , '[d, d]' , true ) 331eq ( "[function('tr', {}), {}]" , eval ( 'String([function("tr", d), d])' )) 332eq ( "[{}, function('tr', {})]" , eval ( 'String([d, function("tr", d)])' )) 333end ) 334 335it ( 'dumps non-empty dictionary' , function () 336assert_same_echo_dump ( "{'t''est': 1}" , { [ "t'est" ] =1 } ) 337end ) 338 339it ( 'does not error when dumping recursive dictionaries' , function () 340api . nvim_set_var ( 'd' , { d =1 } ) 341eval ( 'extend(d, {"d": d})' ) 342command ( 'echo String(d)' ) 343end ) 344 345it ( 'dumps recursive dictionaries without the error' , function () 346api . nvim_set_var ( 'd' , { d =1 } ) 347eval ( 'extend(d, {"d": d})' ) 348eq ( "{'d': {...@0}}" , exec_capture ( 'echo String(d)' )) 349eq ( "{'out': {'d': {...@1}}}" , exec_capture ( 'echo String({"out": d})' )) 350end ) 351end ) 352 353describe ( 'used to represent special values' , function () 354local function chr ( _n ) 355return ( '%c' ): format ( _n ) 356end 357local function ctrl ( c ) 358return ( '%c' ): format ( c : upper (): byte () - 0x40 ) 359end 360it ( 'displays hex as hex' , function () 361-- Regression: due to missing (uint8_t) cast \x80 was represented as 362-- ~@<80>. 363eq ( '<80>' , fn . String ( chr ( 0x80 ))) 364eq ( '<81>' , fn . String ( chr ( 0x81 ))) 365eq ( '<8e>' , fn . String ( chr ( 0x8e ))) 366eq ( '<c2>' , fn . String (( '«' ): sub ( 1 , 1 ))) 367eq ( '«' , fn . String (( '«' ): sub ( 1 , 2 ))) 368 369eq ( '<80>' , fn . StringMsg ( chr ( 0x80 ))) 370eq ( '<81>' , fn . StringMsg ( chr ( 0x81 ))) 371eq ( '<8e>' , fn . StringMsg ( chr ( 0x8e ))) 372eq ( '<c2>' , fn . StringMsg (( '«' ): sub ( 1 , 1 ))) 373eq ( '«' , fn . StringMsg (( '«' ): sub ( 1 , 2 ))) 374end ) 375it ( 'displays ASCII control characters using ^X notation' , function () 376eq ( '^C' , fn . String ( ctrl ( 'c' ))) 377eq ( '^A' , fn . String ( ctrl ( 'a' ))) 378eq ( '^F' , fn . String ( ctrl ( 'f' ))) 379eq ( '^C' , fn . StringMsg ( ctrl ( 'c' ))) 380eq ( '^A' , fn . StringMsg ( ctrl ( 'a' ))) 381eq ( '^F' , fn . StringMsg ( ctrl ( 'f' ))) 382end ) 383it ( 'prints CR, NL and tab as-is' , function () 384eq ( '\n' , fn . String ( '\n' )) 385eq ( '\r' , fn . String ( '\r' )) 386eq ( '\t' , fn . String ( '\t' )) 387end ) 388it ( 'prints non-printable UTF-8 in <> notation' , function () 389-- SINGLE SHIFT TWO, unicode control 390eq ( '<8e>' , fn . String ( fn . nr2char ( 0x8E ))) 391eq ( '<8e>' , fn . StringMsg ( fn . nr2char ( 0x8E ))) 392-- Surrogate pair: U+1F0A0 PLAYING CARD BACK is represented in UTF-16 as 393-- 0xD83C 0xDCA0. This is not valid in UTF-8. 394eq ( '<d83c>' , fn . String ( fn . nr2char ( 0xD83C ))) 395eq ( '<dca0>' , fn . String ( fn . nr2char ( 0xDCA0 ))) 396eq ( '<d83c><dca0>' , fn . String ( fn . nr2char ( 0xD83C ) .. fn . nr2char ( 0xDCA0 ))) 397eq ( '<d83c>' , fn . StringMsg ( fn . nr2char ( 0xD83C ))) 398eq ( '<dca0>' , fn . StringMsg ( fn . nr2char ( 0xDCA0 ))) 399eq ( '<d83c><dca0>' , fn . StringMsg ( fn . nr2char ( 0xD83C ) .. fn . nr2char ( 0xDCA0 ))) 400end ) 401end ) 402end )