robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
598fd77d95
master
1" Test for lambda and closure 2 3func Test_lambda_feature () 4call assert_equal ( 1 , has ( 'lambda' )) 5endfunc 6 7func Test_lambda_with_filter () 8let s: x = 2 9call assert_equal ([ 2 , 3 ], filter ([ 1 , 2 , 3 ], { i , v ->v >= s: x })) 10endfunc 11 12func Test_lambda_with_map () 13let s: x = 1 14call assert_equal ([ 2 , 3 , 4 ], map ([ 1 , 2 , 3 ], { i , v ->v + s: x })) 15endfunc 16 17func Test_lambda_with_sort () 18call assert_equal ([ 1 , 2 , 3 , 4 , 7 ], sort ([ 3 , 7 , 2 , 1 , 4 ], { a , b ->a - b })) 19endfunc 20 21func Test_lambda_with_timer () 22if ! has ( 'timers' ) 23return 24endif 25 26let s: n = 0 27let s: timer_id = 0 28func ! s: Foo () 29let s: timer_id = timer_start ( 10 , { ->execute ( "let s:n += 1 | echo s:n" , "" )}, { "repeat" : -1 }) 30endfunc 31 32call s: Foo () 33" check timer works 34for i in range ( 0 , 10 ) 35if s: n > 0 36break 37endif 38sleep 10m 39endfor 40 41" do not collect lambda 42call test_garbagecollect_now () 43 44" check timer still works 45let m = s: n 46for i in range ( 0 , 10 ) 47if s: n > m 48break 49endif 50sleep 10m 51endfor 52 53call timer_stop ( s: timer_id ) 54call assert_true ( s: n > m ) 55endfunc 56 57func Test_lambda_with_partial () 58let l: Cb = function ({ ... -> ['zero', a:1, a:2, a:3]}, ['one', 'two']) 59call assert_equal ([ 'zero' , 'one' , 'two' , 'three' ], l: Cb ( 'three' )) 60endfunc 61 62function Test_lambda_fails () 63call assert_equal ( 3 , { a , b ->a + b }( 1 , 2 )) 64call assert_fails ( 'echo {a, a -> a + a}(1, 2)' , 'E853:' ) 65call assert_fails ( 'echo {a, b -> a + b)}(1, 2)' , 'E451:' ) 66echo assert_fails ( 'echo 10->{a -> a + 2}' , 'E107:' ) 67endfunc 68 69func Test_not_lambda () 70let x = { '>' : 'foo' } 71call assert_equal ( 'foo' , x [ '>' ]) 72endfunc 73 74func Test_lambda_capture_by_reference () 75let v = 1 76let l: F = { x ->x + v } 77let v = 2 78call assert_equal ( 12 , l: F ( 10 )) 79endfunc 80 81func Test_lambda_side_effect () 82func ! s: update_and_return ( arr ) 83let a: arr [ 1 ] = 5 84return a: arr 85endfunc 86 87func ! s: foo ( arr ) 88return { ->s: update_and_return ( a: arr )} 89endfunc 90 91let arr = [ 3 , 2 , 1 ] 92call assert_equal ([ 3 , 5 , 1 ], s: foo ( arr )()) 93endfunc 94 95func Test_lambda_refer_local_variable_from_other_scope () 96func ! s: foo ( X ) 97return a: X () " refer l:x in s:bar() 98endfunc 99 100func ! s: bar () 101let x = 123 102return s: foo ({ ->x }) 103endfunc 104 105call assert_equal ( 123 , s: bar ()) 106endfunc 107 108func Test_lambda_do_not_share_local_variable () 109func ! s: define_funcs () 110let l: One = { ->split ( execute ( "let a = 'abc' | echo a" ))[ 0 ]} 111let l: Two = { ->exists ( "a" ) ? a : "no" } 112return [ l: One , l: Two ] 113endfunc 114 115let l: F = s: define_funcs () 116 117call assert_equal ( 'no' , l: F [ 1 ]()) 118call assert_equal ( 'abc' , l: F [ 0 ]()) 119call assert_equal ( 'no' , l: F [ 1 ]()) 120endfunc 121 122func Test_lambda_closure_counter () 123func ! s: foo () 124let x = 0 125return { ->[ execute ( "let x += 1" ), x ][ -1 ]} 126endfunc 127 128let l: F = s: foo () 129call test_garbagecollect_now () 130call assert_equal ( 1 , l: F ()) 131call assert_equal ( 2 , l: F ()) 132call assert_equal ( 3 , l: F ()) 133call assert_equal ( 4 , l: F ()) 134endfunc 135 136func Test_lambda_with_a_var () 137func ! s: foo () 138let x = 2 139return { ... -> a:000 + [x]} 140endfunc 141func ! s: bar () 142return s: foo ()( 1 ) 143endfunc 144 145call assert_equal ([ 1 , 2 ], s: bar ()) 146endfunc 147 148func Test_lambda_call_lambda_from_lambda () 149func ! s: foo ( x ) 150let l: F1 = { ->{ ->a: x }} 151return { ->l: F1 ()} 152endfunc 153 154let l: F = s: foo ( 1 ) 155call assert_equal ( 1 , l: F ()()) 156endfunc 157 158func Test_lambda_delfunc () 159func ! s: gen () 160let pl = l: 161let l: Foo = { ->get ( pl , "Foo" , get ( pl , "Bar" , { ->0 }))} 162let l: Bar = l: Foo 163delfunction l:Foo 164return l: Bar 165endfunc 166 167let l: F = s: gen () 168call assert_fails ( ':call l:F()' , 'E933:' ) 169endfunc 170 171func Test_lambda_scope () 172func ! s: NewCounter () 173let c = 0 174return { ->[ execute ( 'let c += 1' ), c ][ -1 ]} 175endfunc 176 177func ! s: NewCounter2 () 178return { ->[ execute ( 'let c += 100' ), c ][ -1 ]} 179endfunc 180 181let l: C = s: NewCounter () 182let l: D = s: NewCounter2 () 183 184call assert_equal ( 1 , l: C ()) 185call assert_fails ( ':call l:D()' , 'E121:' ) 186call assert_equal ( 2 , l: C ()) 187endfunc 188 189func Test_lambda_share_scope () 190func ! s: New () 191let c = 0 192let l: Inc0 = { ->[ execute ( 'let c += 1' ), c ][ -1 ]} 193let l: Dec0 = { ->[ execute ( 'let c -= 1' ), c ][ -1 ]} 194return [ l: Inc0 , l: Dec0 ] 195endfunc 196 197let [ l: Inc , l: Dec ] = s: New () 198 199call assert_equal ( 1 , l: Inc ()) 200call assert_equal ( 2 , l: Inc ()) 201call assert_equal ( 1 , l: Dec ()) 202endfunc 203 204func Test_lambda_circular_reference () 205func ! s: Foo () 206let d = {} 207let d . f = { ->d } 208return d . f 209endfunc 210 211call s: Foo () 212call test_garbagecollect_now () 213let i = 0 |while i < 10000 |call s: Foo () |let i += 1 |endwhile 214call test_garbagecollect_now () 215endfunc 216 217func Test_lambda_combination () 218call assert_equal ( 2 , { x ->{ x ->x }}( 1 )( 2 )) 219call assert_equal ( 10 , { y ->{ x ->x ( y )( 10 )}({ y ->y })}({ z ->z })) 220if has ( 'float' ) 221call assert_equal ( 5.0 , { x ->{ y ->x / y }}( 10 )( 2.0 )) 222endif 223call assert_equal ( 6 , { x ->{ y ->{ z ->x + y + z }}}( 1 )( 2 )( 3 )) 224 225call assert_equal ( 6 , { x ->{ f ->f ( x )}}( 3 )({ x ->x * 2 })) 226call assert_equal ( 6 , { f ->{ x ->f ( x )}}({ x ->x * 2 })( 3 )) 227 228" Z combinator 229let Z = { f ->{ x ->f ({ y ->x ( x )( y )})}({ x ->f ({ y ->x ( x )( y )})})} 230let Fact = { f ->{ x ->x == 0 ? 1 : x * f ( x - 1 )}} 231call assert_equal ( 120 , Z ( Fact )( 5 )) 232endfunc 233 234func Test_closure_counter () 235func ! s: foo () 236let x = 0 237func ! s: bar () closure 238let x += 1 239return x 240endfunc 241return function ( 's:bar' ) 242endfunc 243 244let l: F = s: foo () 245call test_garbagecollect_now () 246call assert_equal ( 1 , l: F ()) 247call assert_equal ( 2 , l: F ()) 248call assert_equal ( 3 , l: F ()) 249call assert_equal ( 4 , l: F ()) 250 251call assert_match ( "^\n function <SNR>\\d\\+_bar() closure" 252 \.. "\n1 let x += 1" 253 \.. "\n2 return x" 254 \.. "\n endfunction$" , execute ( 'func s:bar' )) 255endfunc 256 257func Test_closure_unlet () 258func ! s: foo () 259let x = 1 260func ! s: bar () closure 261unlet x 262endfunc 263call s: bar () 264return l: 265endfunc 266 267call assert_false ( has_key ( s: foo (), 'x' )) 268call test_garbagecollect_now () 269endfunc 270 271func LambdaFoo () 272let x = 0 273func ! LambdaBar () closure 274let x += 1 275return x 276endfunc 277return function ( 'LambdaBar' ) 278endfunc 279 280func Test_closure_refcount () 281let g: Count = LambdaFoo () 282call test_garbagecollect_now () 283call assert_equal ( 1 , g: Count ()) 284let g: Count2 = LambdaFoo () 285call test_garbagecollect_now () 286call assert_equal ( 1 , g: Count2 ()) 287call assert_equal ( 2 , g: Count ()) 288call assert_equal ( 3 , g: Count2 ()) 289 290delfunc LambdaFoo 291delfunc LambdaBar 292endfunc 293 294" This test is causing a use-after-free on shutdown. 295func Test_named_function_closure () 296func ! Afoo () 297let x = 14 298func ! s: Abar () closure 299return x 300endfunc 301call assert_equal ( 14 , s: Abar ()) 302endfunc 303call Afoo () 304call assert_equal ( 14 , s: Abar ()) 305call test_garbagecollect_now () 306call assert_equal ( 14 , s: Abar ()) 307endfunc 308 309func Test_lambda_with_index () 310let List = { x ->[ x ]} 311let Extract = { ->function ( List , [ 'foobar' ])()[ 0 ]} 312call assert_equal ( 'foobar' , Extract ()) 313endfunc 314 315func Test_lambda_error () 316" This was causing a crash 317call assert_fails ( 'ec{@{->{d->()()' , 'E15' ) 318endfunc 319 320func Test_closure_error () 321let l =<< trim END 322func F1() closure 323return 1 324endfunc 325END 326call writefile ( l , 'Xscript' ) 327let caught_932 = 0 328try 329source Xscript 330catch /E932:/ 331let caught_932 = 1 332endtry 333call assert_equal ( 1 , caught_932 ) 334call delete ( 'Xscript' ) 335endfunc 336 337" vim: shiftwidth=2 sts=2 expandtab