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 command =n . command 8local api =n . api 9local eval =n . eval 10local pcall_err =t . pcall_err 11local fn =n . fn 12local NIL =vim . NIL 13local source =n . source 14 15describe ( 'string() function' , function () 16setup ( clear ) 17 18describe ( 'used to represent floating-point values' , function () 19it ( 'dumps NaN values' , function () 20eq ( "str2float('nan')" , eval ( "string(str2float('nan'))" )) 21end ) 22 23it ( 'dumps infinite values' , function () 24eq ( "str2float('inf')" , eval ( "string(str2float('inf'))" )) 25eq ( "-str2float('inf')" , eval ( "string(str2float('-inf'))" )) 26end ) 27 28it ( 'dumps regular values' , function () 29eq ( '1.5' , fn . string ( 1.5 )) 30eq ( '1.56e-20' , fn . string ( 1.56000e-020 )) 31eq ( '0.0' , eval ( 'string(0.0)' )) 32end ) 33 34it ( 'dumps special v: values' , function () 35eq ( 'v:true' , eval ( 'string(v:true)' )) 36eq ( 'v:false' , eval ( 'string(v:false)' )) 37eq ( 'v:null' , eval ( 'string(v:null)' )) 38eq ( 'v:true' , fn . string ( true )) 39eq ( 'v:false' , fn . string ( false )) 40eq ( 'v:null' , fn . string ( NIL )) 41end ) 42 43it ( 'dumps values with at most six digits after the decimal point' , function () 44eq ( '1.234568e-20' , fn . string ( 1.23456789123456789123456789e-020 )) 45eq ( '1.234568' , fn . string ( 1.23456789123456789123456789 )) 46end ) 47 48it ( 'dumps values with at most seven digits before the decimal point' , function () 49eq ( '1234567.891235' , fn . string ( 1234567.89123456789123456789 )) 50eq ( '1.234568e7' , fn . string ( 12345678.9123456789123456789 )) 51end ) 52 53it ( 'dumps negative values' , function () 54eq ( '-1.5' , fn . string ( - 1.5 )) 55eq ( '-1.56e-20' , fn . string ( - 1.56000e-020 )) 56eq ( '-1.234568e-20' , fn . string ( - 1.23456789123456789123456789e-020 )) 57eq ( '-1.234568' , fn . string ( - 1.23456789123456789123456789 )) 58eq ( '-1234567.891235' , fn . string ( - 1234567.89123456789123456789 )) 59eq ( '-1.234568e7' , fn . string ( - 12345678.9123456789123456789 )) 60end ) 61end ) 62 63describe ( 'used to represent numbers' , function () 64it ( 'dumps regular values' , function () 65eq ( '0' , fn . string ( 0 )) 66eq ( '-1' , fn . string ( - 1 )) 67eq ( '1' , fn . string ( 1 )) 68end ) 69 70it ( 'dumps large values' , function () 71eq ( '2147483647' , fn . string ( 2 ^ 31 - 1 )) 72eq ( '-2147483648' , fn . string ( - 2 ^ 31 )) 73end ) 74end ) 75 76describe ( 'used to represent strings' , function () 77it ( 'dumps regular strings' , function () 78eq ( "'test'" , fn . string ( 'test' )) 79end ) 80 81it ( 'dumps empty strings' , function () 82eq ( "''" , fn . string ( '' )) 83end ) 84 85it ( "dumps strings with ' inside" , function () 86eq ( "''''''''" , fn . string ( "'''" )) 87eq ( "'a''b'''''" , fn . string ( "a'b''" )) 88eq ( "'''b''''d'" , fn . string ( "'b''d" )) 89eq ( "'a''b''c''d'" , fn . string ( "a'b'c'd" )) 90end ) 91 92it ( 'dumps NULL strings' , function () 93eq ( "''" , eval ( 'string($XXX_UNEXISTENT_VAR_XXX)' )) 94end ) 95 96it ( 'dumps NULL lists' , function () 97eq ( '[]' , eval ( 'string(v:_null_list)' )) 98end ) 99 100it ( 'dumps NULL dictionaries' , function () 101eq ( '{}' , eval ( 'string(v:_null_dict)' )) 102end ) 103end ) 104 105describe ( 'used to represent funcrefs' , function () 106setup ( function () 107source ( [[ 108function Test1() 109endfunction 110 111function s:Test2() dict 112endfunction 113 114function g:Test3() dict 115endfunction 116 117let g:Test2_f = function('s:Test2') 118]] ) 119end ) 120 121it ( 'dumps references to built-in functions' , function () 122eq ( "function('function')" , eval ( 'string(function("function"))' )) 123end ) 124 125it ( 'dumps references to user functions' , function () 126eq ( "function('Test1')" , eval ( 'string(function("Test1"))' )) 127eq ( "function('g:Test3')" , eval ( 'string(function("g:Test3"))' )) 128end ) 129 130it ( 'dumps references to script functions' , function () 131eq ( "function('<SNR>1_Test2')" , eval ( 'string(Test2_f)' )) 132end ) 133 134it ( 'dumps partials with self referencing a partial' , function () 135source ( [[ 136function TestDict() dict 137endfunction 138let d = {} 139let TestDictRef = function('TestDict', d) 140let d.tdr = TestDictRef 141]] ) 142eq ( 143'Vim(echo):E724: unable to correctly dump variable with self-referencing container' , 144pcall_err ( command , 'echo string(d.tdr)' ) 145) 146end ) 147 148it ( 'dumps automatically created partials' , function () 149eq ( 150"function('<SNR>1_Test2', {'f': function('<SNR>1_Test2')})" , 151eval ( 'string({"f": Test2_f}.f)' ) 152) 153eq ( 154"function('<SNR>1_Test2', [1], {'f': function('<SNR>1_Test2', [1])})" , 155eval ( 'string({"f": function(Test2_f, [1])}.f)' ) 156) 157end ) 158 159it ( 'dumps manually created partials' , function () 160eq ( "function('Test3', [1, 2], {})" , eval ( 'string(function("Test3", [1, 2], {}))' )) 161eq ( "function('Test3', {})" , eval ( 'string(function("Test3", {}))' )) 162eq ( "function('Test3', [1, 2])" , eval ( 'string(function("Test3", [1, 2]))' )) 163end ) 164 165it ( 'does not crash or halt when dumping partials with reference cycles in self' , function () 166api . nvim_set_var ( 'd' , { v =true } ) 167eq ( 168[[Vim(echo):E724: unable to correctly dump variable with self-referencing container]] , 169pcall_err ( command , 'echo string(extend(extend(g:d, {"f": g:Test2_f}), {"p": g:d.f}))' ) 170) 171end ) 172 173it ( 'does not show errors when dumping partials referencing the same dict' , function () 174command ( 'let d = {}' ) 175-- Regression for “eval/typval_encode: Dump empty dict before 176-- checking for refcycle”, results in error. 177eq ( 178"[function('tr', {}), function('tr', {})]" , 179eval ( 'string([function("tr", d), function("tr", d)])' ) 180) 181-- Regression for “eval: Work with reference cycles in partials (self) 182-- properly”, results in crash. 183eval ( 'extend(d, {"a": 1})' ) 184eq ( 185"[function('tr', {'a': 1}), function('tr', {'a': 1})]" , 186eval ( 'string([function("tr", d), function("tr", d)])' ) 187) 188end ) 189 190it ( 'does not crash or halt when dumping partials with reference cycles in arguments' , function () 191api . nvim_set_var ( 'l' , {} ) 192eval ( 'add(l, l)' ) 193-- Regression: the below line used to crash (add returns original list and 194-- there was error in dumping partials). Tested explicitly in 195-- test/unit/api/private_helpers_spec.lua. 196eval ( 'add(l, function("Test1", l))' ) 197eq ( 198[=[Vim(echo):E724: unable to correctly dump variable with self-referencing container]=] , 199pcall_err ( command , 'echo string(function("Test1", l))' ) 200) 201end ) 202 203it ( 204'does not crash or halt when dumping partials with reference cycles in self and arguments' , 205function () 206api . nvim_set_var ( 'd' , { v =true } ) 207api . nvim_set_var ( 'l' , {} ) 208eval ( 'add(l, l)' ) 209eval ( 'add(l, function("Test1", l))' ) 210eval ( 'add(l, function("Test1", d))' ) 211eq ( 212[=[Vim(echo):E724: unable to correctly dump variable with self-referencing container]=] , 213pcall_err ( 214command , 215'echo string(extend(extend(g:d, {"f": g:Test2_f}), {"p": function(g:d.f, l)}))' 216) 217) 218end 219) 220end ) 221 222describe ( 'used to represent lists' , function () 223it ( 'dumps empty list' , function () 224eq ( '[]' , fn . string ( {} )) 225end ) 226 227it ( 'dumps nested lists' , function () 228eq ( '[[[[[]]]]]' , fn . string ( { { { { {} } } } } )) 229end ) 230 231it ( 'dumps nested non-empty lists' , function () 232eq ( '[1, [[3, [[5], 4]], 2]]' , fn . string ( { 1 , { { 3 , { { 5 } , 4 } } , 2 } } )) 233end ) 234 235it ( 'errors when dumping recursive lists' , function () 236api . nvim_set_var ( 'l' , {} ) 237eval ( 'add(l, l)' ) 238eq ( 239'Vim(echo):E724: unable to correctly dump variable with self-referencing container' , 240pcall_err ( command , 'echo string(l)' ) 241) 242end ) 243 244it ( 'dumps recursive lists despite the error' , function () 245api . nvim_set_var ( 'l' , {} ) 246eval ( 'add(l, l)' ) 247eq ( 248'Vim(echo):E724: unable to correctly dump variable with self-referencing container' , 249pcall_err ( command , 'echo string(l)' ) 250) 251eq ( 252'Vim(echo):E724: unable to correctly dump variable with self-referencing container' , 253pcall_err ( command , 'echo string([l])' ) 254) 255end ) 256end ) 257 258describe ( 'used to represent dictionaries' , function () 259it ( 'dumps empty dict' , function () 260eq ( '{}' , eval ( 'string({})' )) 261end ) 262 263it ( 'dumps list with two same empty dictionaries, also in partials' , function () 264command ( 'let d = {}' ) 265eq ( '[{}, {}]' , eval ( 'string([d, d])' )) 266eq ( "[function('tr', {}), {}]" , eval ( 'string([function("tr", d), d])' )) 267eq ( "[{}, function('tr', {})]" , eval ( 'string([d, function("tr", d)])' )) 268end ) 269 270it ( 'dumps non-empty dict' , function () 271eq ( "{'t''est': 1}" , fn . string ( { [ "t'est" ] =1 } )) 272end ) 273 274it ( 'errors when dumping recursive dictionaries' , function () 275api . nvim_set_var ( 'd' , { d =1 } ) 276eval ( 'extend(d, {"d": d})' ) 277eq ( 278'Vim(echo):E724: unable to correctly dump variable with self-referencing container' , 279pcall_err ( command , 'echo string(d)' ) 280) 281end ) 282 283it ( 'dumps recursive dictionaries despite the error' , function () 284api . nvim_set_var ( 'd' , { d =1 } ) 285eval ( 'extend(d, {"d": d})' ) 286eq ( 287'Vim(echo):E724: unable to correctly dump variable with self-referencing container' , 288pcall_err ( command , 'echo string(d)' ) 289) 290eq ( 291'Vim(echo):E724: unable to correctly dump variable with self-referencing container' , 292pcall_err ( command , 'echo string({"out": d})' ) 293) 294end ) 295end ) 296end )