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 , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 5local assert_alive =n . assert_alive 6local clear , source =n . clear , n . source 7local api =n . api 8local insert =n . insert 9local eq , next_msg =t . eq , n . next_msg 10local matches =t . matches 11local exec_lua =n . exec_lua 12local command =n . command 13local eval =n . eval 14local pcall_err =t . pcall_err 15 16describe ( 'Vimscript dictionary notifications' , function () 17local channel 18 19before_each ( function () 20clear () 21channel =api . nvim_get_chan_info ( 0 ). id 22api . nvim_set_var ( 'channel' , channel ) 23end ) 24 25-- the same set of tests are applied to top-level dictionaries(g:, b:, w: and 26-- t:) and a dictionary variable, so we generate them in the following 27-- function. 28local function gentests ( dict_expr , dict_init ) 29local is_g =dict_expr == 'g:' 30 31local function update ( opval , key ) 32if not key then 33key ='watched' 34end 35if opval == '' then 36command (( "unlet %s['%s']" ): format ( dict_expr , key )) 37else 38command (( "let %s['%s'] %s" ): format ( dict_expr , key , opval )) 39end 40end 41 42local function update_with_api ( opval , key ) 43if not key then 44key ='watched' 45end 46if opval == '' then 47exec_lua (( "vim.api.nvim_del_var('%s')" ): format ( key )) 48else 49exec_lua (( "vim.api.nvim_set_var('%s', %s)" ): format ( key , opval )) 50end 51end 52 53local function update_with_vim_g ( opval , key ) 54if not key then 55key ='watched' 56end 57if opval == '' then 58exec_lua (( 'vim.g.%s = nil' ): format ( key )) 59else 60exec_lua (( 'vim.g.%s %s' ): format ( key , opval )) 61end 62end 63 64local function verify_echo () 65-- helper to verify that no notifications are sent after certain change 66-- to a dict 67command ( "call rpcnotify(g:channel, 'echo')" ) 68eq ( { 'notification' , 'echo' , {} } , next_msg ()) 69end 70 71local function verify_value ( vals , key ) 72if not key then 73key ='watched' 74end 75eq ( { 'notification' , 'values' , { key , vals } } , next_msg ()) 76end 77 78describe ( dict_expr .. ' watcher' , function () 79if dict_init then 80before_each ( function () 81source ( dict_init ) 82end ) 83end 84 85before_each ( function () 86source ( [[ 87function! g:Changed(dict, key, value) 88if a:dict isnot ]] .. dict_expr .. [[ | 89throw 'invalid dict' 90endif 91call rpcnotify(g:channel, 'values', a:key, a:value) 92endfunction 93call dictwatcheradd(]] .. dict_expr .. [[, "watched", "g:Changed") 94call dictwatcheradd(]] .. dict_expr .. [[, "watched2", "g:Changed") 95]] ) 96end ) 97 98after_each ( function () 99source ( [[ 100call dictwatcherdel(]] .. dict_expr .. [[, "watched", "g:Changed") 101call dictwatcherdel(]] .. dict_expr .. [[, "watched2", "g:Changed") 102]] ) 103update ( '= "test"' ) 104update ( '= "test2"' , 'watched2' ) 105update ( '' , 'watched2' ) 106update ( '' ) 107verify_echo () 108if is_g then 109update_with_api ( '"test"' ) 110update_with_api ( '"test2"' , 'watched2' ) 111update_with_api ( '' , 'watched2' ) 112update_with_api ( '' ) 113verify_echo () 114update_with_vim_g ( '= "test"' ) 115update_with_vim_g ( '= "test2"' , 'watched2' ) 116update_with_vim_g ( '' , 'watched2' ) 117update_with_vim_g ( '' ) 118verify_echo () 119end 120end ) 121 122it ( 'is not triggered when unwatched keys are updated' , function () 123update ( '= "noop"' , 'unwatched' ) 124update ( '.= "noop2"' , 'unwatched' ) 125update ( '' , 'unwatched' ) 126verify_echo () 127if is_g then 128update_with_api ( '"noop"' , 'unwatched' ) 129update_with_api ( 'vim.g.unwatched .. "noop2"' , 'unwatched' ) 130update_with_api ( '' , 'unwatched' ) 131verify_echo () 132update_with_vim_g ( '= "noop"' , 'unwatched' ) 133update_with_vim_g ( '= vim.g.unwatched .. "noop2"' , 'unwatched' ) 134update_with_vim_g ( '' , 'unwatched' ) 135verify_echo () 136end 137end ) 138 139it ( 'is triggered by remove()' , function () 140update ( '= "test"' ) 141verify_value ( { new ='test' } ) 142command ( 'call remove(' .. dict_expr .. ', "watched")' ) 143verify_value ( { old ='test' } ) 144end ) 145 146if is_g then 147it ( 'is triggered by remove() when updated with nvim_*_var' , function () 148update_with_api ( '"test"' ) 149verify_value ( { new ='test' } ) 150command ( 'call remove(' .. dict_expr .. ', "watched")' ) 151verify_value ( { old ='test' } ) 152end ) 153 154it ( 'is triggered by remove() when updated with vim.g' , function () 155update_with_vim_g ( '= "test"' ) 156verify_value ( { new ='test' } ) 157command ( 'call remove(' .. dict_expr .. ', "watched")' ) 158verify_value ( { old ='test' } ) 159end ) 160end 161 162it ( 'is triggered by extend()' , function () 163update ( '= "xtend"' ) 164verify_value ( { new ='xtend' } ) 165command ( [[ 166call extend(]] .. dict_expr .. [[, {'watched': 'xtend2', 'watched2': 5, 'watched3': 'a'}) 167]] ) 168verify_value ( { old ='xtend' , new ='xtend2' } ) 169verify_value ( { new =5 } , 'watched2' ) 170update ( '' ) 171verify_value ( { old ='xtend2' } ) 172update ( '' , 'watched2' ) 173verify_value ( { old =5 } , 'watched2' ) 174update ( '' , 'watched3' ) 175verify_echo () 176end ) 177 178it ( 'is triggered with key patterns' , function () 179source ( [[ 180call dictwatcheradd(]] .. dict_expr .. [[, "wat*", "g:Changed") 181]] ) 182update ( '= 1' ) 183verify_value ( { new =1 } ) 184verify_value ( { new =1 } ) 185update ( '= 3' , 'watched2' ) 186verify_value ( { new =3 } , 'watched2' ) 187verify_value ( { new =3 } , 'watched2' ) 188verify_echo () 189source ( [[ 190call dictwatcherdel(]] .. dict_expr .. [[, "wat*", "g:Changed") 191]] ) 192-- watch every key pattern 193source ( [[ 194call dictwatcheradd(]] .. dict_expr .. [[, "*", "g:Changed") 195]] ) 196update ( '= 3' , 'another_key' ) 197update ( '= 4' , 'another_key' ) 198update ( '' , 'another_key' ) 199update ( '= 2' ) 200verify_value ( { new =3 } , 'another_key' ) 201verify_value ( { old =3 , new =4 } , 'another_key' ) 202verify_value ( { old =4 } , 'another_key' ) 203verify_value ( { old =1 , new =2 } ) 204verify_value ( { old =1 , new =2 } ) 205verify_echo () 206source ( [[ 207call dictwatcherdel(]] .. dict_expr .. [[, "*", "g:Changed") 208]] ) 209end ) 210 211it ( 'is triggered for empty keys' , function () 212command ( [[ 213call dictwatcheradd(]] .. dict_expr .. [[, "", "g:Changed") 214]] ) 215update ( '= 1' , '' ) 216verify_value ( { new =1 } , '' ) 217update ( '= 2' , '' ) 218verify_value ( { old =1 , new =2 } , '' ) 219command ( [[ 220call dictwatcherdel(]] .. dict_expr .. [[, "", "g:Changed") 221]] ) 222end ) 223 224it ( 'is triggered for empty keys when using catch-all *' , function () 225command ( [[ 226call dictwatcheradd(]] .. dict_expr .. [[, "*", "g:Changed") 227]] ) 228update ( '= 1' , '' ) 229verify_value ( { new =1 } , '' ) 230update ( '= 2' , '' ) 231verify_value ( { old =1 , new =2 } , '' ) 232command ( [[ 233call dictwatcherdel(]] .. dict_expr .. [[, "*", "g:Changed") 234]] ) 235end ) 236 237-- test a sequence of updates of different types to ensure proper memory 238-- management(with ASAN) 239local function test_updates ( tests ) 240it ( 'test change sequence' , function () 241local input , output 242for i =1 , # tests do 243input , output =unpack ( tests [ i ]) 244update ( input ) 245verify_value ( output ) 246end 247end ) 248end 249 250test_updates ( { 251{ '= 3' , { new =3 } } , 252{ '= 6' , { old =3 , new =6 } } , 253{ '+= 3' , { old =6 , new =9 } } , 254{ '' , { old =9 } } , 255} ) 256 257test_updates ( { 258{ '= "str"' , { new ='str' } } , 259{ '= "str2"' , { old ='str' , new ='str2' } } , 260{ '.= "2str"' , { old ='str2' , new ='str22str' } } , 261{ '' , { old ='str22str' } } , 262} ) 263 264test_updates ( { 265{ '= [1, 2]' , { new ={ 1 , 2 } } } , 266{ '= [1, 2, 3]' , { old ={ 1 , 2 } , new ={ 1 , 2 , 3 } } } , 267-- the += will update the list in place, so old and new are the same 268{ '+= [4, 5]' , { old ={ 1 , 2 , 3 , 4 , 5 } , new ={ 1 , 2 , 3 , 4 , 5 } } } , 269{ '' , { old ={ 1 , 2 , 3 , 4 , 5 } } } , 270} ) 271 272test_updates ( { 273{ '= {"k": "v"}' , { new ={ k ='v' } } } , 274{ '= {"k1": 2}' , { old ={ k ='v' } , new ={ k1 =2 } } } , 275{ '' , { old ={ k1 =2 } } } , 276} ) 277end ) 278end 279 280gentests ( 'g:' ) 281gentests ( 'b:' ) 282gentests ( 'w:' ) 283gentests ( 't:' ) 284gentests ( 'g:dict_var' , 'let g:dict_var = {}' ) 285 286describe ( 'multiple watchers on the same dict/key' , function () 287before_each ( function () 288source ( [[ 289function! g:Watcher1(dict, key, value) 290call rpcnotify(g:channel, '1', a:key, a:value) 291endfunction 292function! g:Watcher2(dict, key, value) 293call rpcnotify(g:channel, '2', a:key, a:value) 294endfunction 295call dictwatcheradd(g:, "key", "g:Watcher1") 296call dictwatcheradd(g:, "key", "g:Watcher2") 297]] ) 298end ) 299 300it ( 'invokes all callbacks when the key is changed' , function () 301command ( 'let g:key = "value"' ) 302eq ( { 'notification' , '1' , { 'key' , { new ='value' } } } , next_msg ()) 303eq ( { 'notification' , '2' , { 'key' , { new ='value' } } } , next_msg ()) 304end ) 305 306it ( 'only removes watchers that fully match dict, key and callback' , function () 307command ( 'let g:key = "value"' ) 308eq ( { 'notification' , '1' , { 'key' , { new ='value' } } } , next_msg ()) 309eq ( { 'notification' , '2' , { 'key' , { new ='value' } } } , next_msg ()) 310command ( 'call dictwatcherdel(g:, "key", "g:Watcher1")' ) 311command ( 'let g:key = "v2"' ) 312eq ( { 'notification' , '2' , { 'key' , { old ='value' , new ='v2' } } } , next_msg ()) 313end ) 314end ) 315 316it ( 'errors out when adding to v:_null_dict' , function () 317command ( [[ 318function! g:Watcher1(dict, key, value) 319call rpcnotify(g:channel, '1', a:key, a:value) 320endfunction 321]] ) 322eq ( 323'Vim(call):E46: Cannot change read-only variable "dictwatcheradd() argument"' , 324pcall_err ( command , 'call dictwatcheradd(v:_null_dict, "x", "g:Watcher1")' ) 325) 326end ) 327 328describe ( 'errors' , function () 329before_each ( function () 330source ( [[ 331function! g:Watcher1(dict, key, value) 332call rpcnotify(g:channel, '1', a:key, a:value) 333endfunction 334function! g:Watcher2(dict, key, value) 335call rpcnotify(g:channel, '2', a:key, a:value) 336endfunction 337]] ) 338end ) 339 340-- WARNING: This suite depends on the above tests 341it ( 'fails to remove if no watcher with matching callback is found' , function () 342eq ( 343"Vim(call):Couldn't find a watcher matching key and callback" , 344pcall_err ( command , 'call dictwatcherdel(g:, "key", "g:Watcher1")' ) 345) 346end ) 347 348it ( 'fails to remove if no watcher with matching key is found' , function () 349eq ( 350"Vim(call):Couldn't find a watcher matching key and callback" , 351pcall_err ( command , 'call dictwatcherdel(g:, "invalid_key", "g:Watcher2")' ) 352) 353end ) 354 355it ( "does not fail to add/remove if the callback doesn't exist" , function () 356command ( 'call dictwatcheradd(g:, "key", "g:InvalidCb")' ) 357command ( 'call dictwatcherdel(g:, "key", "g:InvalidCb")' ) 358end ) 359 360it ( 'fails to remove watcher from v:_null_dict' , function () 361eq ( 362"Vim(call):Couldn't find a watcher matching key and callback" , 363pcall_err ( command , 'call dictwatcherdel(v:_null_dict, "x", "g:Watcher2")' ) 364) 365end ) 366 367--[[ 368[ it("fails to add/remove if the callback doesn't exist", function() 369[ eq("Vim(call):Function g:InvalidCb doesn't exist", 370[ pcall_err(command, 'call dictwatcheradd(g:, "key", "g:InvalidCb")')) 371[ eq("Vim(call):Function g:InvalidCb doesn't exist", 372[ pcall_err(command, 'call dictwatcherdel(g:, "key", "g:InvalidCb")')) 373[ end) 374]] 375 376it ( 'does not fail to replace a watcher function' , function () 377source ( [[ 378let g:key = 'v2' 379call dictwatcheradd(g:, "key", "g:Watcher2") 380function! g:ReplaceWatcher2() 381function! g:Watcher2(dict, key, value) 382call rpcnotify(g:channel, '2b', a:key, a:value) 383endfunction 384endfunction 385]] ) 386command ( 'call g:ReplaceWatcher2()' ) 387command ( 'let g:key = "value"' ) 388eq ( { 'notification' , '2b' , { 'key' , { old ='v2' , new ='value' } } } , next_msg ()) 389end ) 390 391it ( 'does not crash when freeing a watched dictionary' , function () 392source ( [[ 393function! Watcher(dict, key, value) 394echo a:key string(a:value) 395endfunction 396 397function! MakeWatch() 398let d = {'foo': 'bar'} 399call dictwatcheradd(d, 'foo', function('Watcher')) 400endfunction 401]] ) 402 403command ( 'call MakeWatch()' ) 404assert_alive () 405end ) 406end ) 407 408describe ( 'with lambdas' , function () 409it ( 'works correctly' , function () 410source ( [[ 411let d = {'foo': 'baz'} 412call dictwatcheradd(d, 'foo', {dict, key, value -> rpcnotify(g:channel, '2', key, value)}) 413let d.foo = 'bar' 414]] ) 415eq ( { 'notification' , '2' , { 'foo' , { old ='baz' , new ='bar' } } } , next_msg ()) 416end ) 417end ) 418 419it ( 'for b:changedtick' , function () 420source ( [[ 421function! OnTickChanged(dict, key, value) 422call rpcnotify(g:channel, 'SendChangeTick', a:key, a:value) 423endfunction 424call dictwatcheradd(b:, 'changedtick', 'OnTickChanged') 425]] ) 426 427insert ( 't' ) 428eq ( { 'notification' , 'SendChangeTick' , { 'changedtick' , { old =2 , new =3 } } } , next_msg ()) 429 430command ( [[call dictwatcherdel(b:, 'changedtick', 'OnTickChanged')]] ) 431insert ( 't' ) 432assert_alive () 433 434command ( [[call dictwatcheradd(b:, 'changedtick', {-> execute('bwipe!')})]] ) 435insert ( 't' ) 436eq ( 'E937: Attempt to delete a buffer that is in use: [No Name]' , api . nvim_get_vvar ( 'errmsg' )) 437assert_alive () 438 439command ( [[enew | set modified | call dictwatcheradd(b:, 'changedtick', {-> execute('split')})]] ) 440-- Used to instead leave a window open to a NULL buffer. 441matches ( 442'E565: Not allowed to change text or change window: split$' , 443pcall_err ( command , 'bdelete!' ) 444) 445end ) 446 447it ( 'does not cause use-after-free when unletting from callback' , function () 448source ( [[ 449let g:called = 0 450function W(...) abort 451unlet g:d 452let g:called = 1 453endfunction 454let g:d = {} 455call dictwatcheradd(g:d, '*', function('W')) 456let g:d.foo = 123 457]] ) 458eq ( 1 , eval ( 'g:called' )) 459end ) 460 461it ( 'does not crash when using dictwatcherdel in callback' , function () 462source ( [[ 463let g:d = {} 464 465function! W1(...) 466" Delete current and following watcher. 467call dictwatcherdel(g:d, '*', function('W1')) 468call dictwatcherdel(g:d, '*', function('W2')) 469try 470call dictwatcherdel({}, 'meh', function('tr')) 471catch 472let g:exc = v:exception 473endtry 474endfunction 475call dictwatcheradd(g:d, '*', function('W1')) 476 477function! W2(...) 478endfunction 479call dictwatcheradd(g:d, '*', function('W2')) 480 481let g:d.foo = 23 482]] ) 483eq ( 23 , eval ( 'g:d.foo' )) 484eq ( "Vim(call):Couldn't find a watcher matching key and callback" , eval ( 'g:exc' )) 485end ) 486 487it ( 'does not call watcher added in callback' , function () 488source ( [[ 489let g:d = {} 490let g:calls = [] 491 492function! W1(...) abort 493call add(g:calls, 'W1') 494call dictwatcheradd(g:d, '*', function('W2')) 495endfunction 496 497function! W2(...) abort 498call add(g:calls, 'W2') 499endfunction 500 501call dictwatcheradd(g:d, '*', function('W1')) 502let g:d.foo = 23 503]] ) 504eq ( 23 , eval ( 'g:d.foo' )) 505eq ( { 'W1' } , eval ( 'g:calls' )) 506end ) 507 508it ( 'calls watcher deleted in callback' , function () 509source ( [[ 510let g:d = {} 511let g:calls = [] 512 513function! W1(...) abort 514call add(g:calls, "W1") 515call dictwatcherdel(g:d, '*', function('W2')) 516endfunction 517 518function! W2(...) abort 519call add(g:calls, "W2") 520endfunction 521 522call dictwatcheradd(g:d, '*', function('W1')) 523call dictwatcheradd(g:d, '*', function('W2')) 524let g:d.foo = 123 525 526unlet g:d 527let g:d = {} 528call dictwatcheradd(g:d, '*', function('W2')) 529call dictwatcheradd(g:d, '*', function('W1')) 530let g:d.foo = 123 531]] ) 532eq ( 123 , eval ( 'g:d.foo' )) 533eq ( { 'W1' , 'W2' , 'W2' , 'W1' } , eval ( 'g:calls' )) 534end ) 535end ) 536describe ( 'tabpagebuflist() with dict watcher during buffer close/wipe' , function () 537before_each ( function () 538clear () 539end ) 540 541it ( 542'does not segfault when called from dict watcher on b:changedtick (bufhidden=unload)' , 543function () 544command ( [[ 545new 546set bufhidden=unload 547call dictwatcheradd(b:, 'changedtick', {-> tabpagebuflist()}) 548close 549]] ) 550 551assert_alive () 552end 553) 554 555it ( 'does not segfault when wiping buffer with dict watcher' , function () 556command ( [[ 557new 558call setline(1, 'test') 559call dictwatcheradd(b:, 'changedtick', {-> tabpagebuflist()}) 560bwipeout! 561]] ) 562 563assert_alive () 564end ) 565 566it ( 'does not segfault with multiple windows in the tabpage' , function () 567command ( [[ 568" create two windows in the current tab 569edit foo 570vnew 571call setline(1, 'bar') 572 573" attach watcher to the current buffer in the split 574call dictwatcheradd(b:, 'changedtick', {-> tabpagebuflist()}) 575 576" close the split window (triggers close_buffer on this buffer) 577close 578]] ) 579 580assert_alive () 581end ) 582end )