robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
cc25b2f13c
master
1" Tests for the List and Dict types 2scriptencoding utf-8 3 4source vim9.vim 5 6func TearDown () 7" Run garbage collection after every test 8call test_garbagecollect_now () 9endfunc 10 11" Tests for List type 12 13" List creation 14func Test_list_create () 15" Creating List directly with different types 16let l = [ 1 , 'as''d' , [ 1 , 2 , function ( "strlen" )], { 'a' : 1 },] 17call assert_equal ( "[1, 'as''d', [1, 2, function('strlen')], {'a': 1}]" , string ( l )) 18call assert_equal ({ 'a' : 1 }, l [ -1 ]) 19call assert_equal ( 1 , l [ -4 ]) 20let x = 10 21try 22let x = l [ -5 ] 23catch 24call assert_match ( 'E684:' , v: exception ) 25endtry 26call assert_equal ( 10 , x ) 27endfunc 28 29" This was allowed in legacy Vim script 30let s: list_with_spaces = [ 1 , 2 , 3 ] 31 32" List slices 33func Test_list_slice () 34let l = [ 1 , 'as''d' , [ 1 , 2 , function ( "strlen" )], { 'a' : 1 },] 35call assert_equal ([ 1 , 'as''d' , [ 1 , 2 , function ( 'strlen' )], { 'a' : 1 }], l [:]) 36call assert_equal ([ 'as''d' , [ 1 , 2 , function ( 'strlen' )], { 'a' : 1 }], l [ 1 :]) 37call assert_equal ([ 1 , 'as''d' , [ 1 , 2 , function ( 'strlen' )]], l [: -2 ]) 38call assert_equal ([ 1 , 'as''d' , [ 1 , 2 , function ( 'strlen' )], { 'a' : 1 }], l [ 0 : 8 ]) 39call assert_equal ([], l [ 8 : -1 ]) 40call assert_equal ([], l [ 0 : -10 ]) 41" perform an operation on a list slice 42let l = [ 1 , 2 , 3 ] 43let l [: 1 ] += [ 1 , 2 ] 44let l [ 2 :] -= [ 1 ] 45call assert_equal ([ 2 , 4 , 2 ], l ) 46 47let lines =<< trim END 48VAR l = [1, 2] 49call assert_equal([1, 2], l[:]) 50call assert_equal([2], l[-1 : -1]) 51call assert_equal([1, 2], l[-2 : -1]) 52END 53call CheckLegacyAndVim9Success ( lines ) 54 55let l = [ 1 , 2 ] 56call assert_equal ([], l [ -3 : -1 ]) 57 58let lines =<< trim END 59var l = [1, 2] 60assert_equal([1, 2], l[-3 : -1]) 61END 62call CheckDefAndScriptSuccess ( lines ) 63 64call assert_fails ( 'let l[[]] = 1' , 'E730: Using a List as a String' ) 65call assert_fails ( 'let l[1 : []] = [1]' , 'E730: Using a List as a String' ) 66endfunc 67 68" List identity 69func Test_list_identity () 70let lines =<< trim END 71VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},] 72VAR ll = l 73VAR lx = copy(l) 74call assert_true(l == ll) 75call assert_false(l isnot ll) 76call assert_true(l is ll) 77call assert_true(l == lx) 78call assert_false(l is lx) 79call assert_true(l isnot lx) 80END 81call CheckLegacyAndVim9Success ( lines ) 82endfunc 83 84" removing items with :unlet 85func Test_list_unlet () 86let lines =<< trim END 87VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},] 88unlet l[2] 89call assert_equal([1, 'as''d', {'a': 1}], l) 90LET l = range(8) 91unlet l[: 3] 92unlet l[1 :] 93call assert_equal([4], l) 94 95#" removing items out of range: silently skip items that don't exist 96LET l = [0, 1, 2, 3] 97unlet l[2 : 2] 98call assert_equal([0, 1, 3], l) 99LET l = [0, 1, 2, 3] 100unlet l[2 : 3] 101call assert_equal([0, 1], l) 102LET l = [0, 1, 2, 3] 103unlet l[2 : 4] 104call assert_equal([0, 1], l) 105LET l = [0, 1, 2, 3] 106unlet l[2 : 5] 107call assert_equal([0, 1], l) 108LET l = [0, 1, 2, 3] 109unlet l[-2 : 2] 110call assert_equal([0, 1, 3], l) 111LET l = [0, 1, 2, 3] 112unlet l[-3 : 2] 113call assert_equal([0, 3], l) 114LET l = [0, 1, 2, 3] 115unlet l[-4 : 2] 116call assert_equal([3], l) 117LET l = [0, 1, 2, 3] 118unlet l[-5 : 2] 119call assert_equal([3], l) 120LET l = [0, 1, 2, 3] 121unlet l[-6 : 2] 122call assert_equal([3], l) 123END 124call CheckLegacyAndVim9Success ( lines ) 125 126let l = [ 0 , 1 , 2 , 3 ] 127unlet l [ 2 : 2 ] 128call assert_equal ([ 0 , 1 , 3 ], l ) 129let l = [ 0 , 1 , 2 , 3 ] 130unlet l [ 2 : 3 ] 131call assert_equal ([ 0 , 1 ], l ) 132 133let lines =<< trim END 134VAR l = [0, 1, 2, 3] 135unlet l[2 : 1] 136END 137call CheckLegacyAndVim9Failure ( lines , 'E684:' ) 138 139let lines =<< trim END 140VAR l = [0, 1, 2, 3] 141unlet l[-1 : 2] 142END 143call CheckLegacyAndVim9Failure ( lines , 'E684:' ) 144endfunc 145 146" assignment to a list 147func Test_list_assign () 148let lines =<< trim END 149VAR l = [0, 1, 2, 3] 150VAR va = 0 151VAR vb = 0 152LET [va, vb] = l[2 : 3] 153call assert_equal([2, 3], [va, vb]) 154END 155call CheckLegacyAndVim9Success ( lines ) 156 157let lines =<< trim END 158let l = [0, 1, 2, 3] 159let [va, vb] = l 160END 161call CheckScriptFailure ( lines , 'E687:' ) 162let lines =<< trim END 163var l = [0, 1, 2, 3] 164var va = 0 165var vb = 0 166[va, vb] = l 167END 168call CheckScriptFailure ([ 'vim9script' ] + lines , 'E687:' ) 169call CheckDefExecFailure ( lines , 'E1093: Expected 2 items but got 4' ) 170 171let lines =<< trim END 172let l = [0, 1, 2, 3] 173let [va, vb] = l[1:1] 174END 175call CheckScriptFailure ( lines , 'E688:' ) 176let lines =<< trim END 177var l = [0, 1, 2, 3] 178var va = 0 179var vb = 0 180[va, vb] = l[1 : 1] 181END 182call CheckScriptFailure ([ 'vim9script' ] + lines , 'E688:' ) 183call CheckDefExecFailure ( lines , 'E1093: Expected 2 items but got 1' ) 184 185let lines =<< trim END 186VAR l = [2] 187LET l += v:_null_list 188call assert_equal([2], l) 189LET l = v:_null_list 190LET l += [1] 191call assert_equal([1], l) 192END 193call CheckLegacyAndVim9Success ( lines ) 194 195let d = { 'abc' : [ 1 , 2 , 3 ]} 196call assert_fails ( 'let d.abc[0:0z10] = [10, 20]' , 'E976: Using a Blob as a String' ) 197endfunc 198 199" test for range assign 200func Test_list_range_assign () 201let lines =<< trim END 202VAR l = [0] 203LET l[:] = [1, 2] 204call assert_equal([1, 2], l) 205LET l[-4 : -1] = [5, 6] 206call assert_equal([5, 6], l) 207END 208call CheckLegacyAndVim9Success ( lines ) 209 210let lines =<< trim END 211var l = [7] 212l[:] = ['text'] 213END 214call CheckDefAndScriptFailure ( lines , 'E1012:' , 2 ) 215endfunc 216 217func Test_list_items () 218let r = [] 219let l = [ 'a' , 'b' , 'c' ] 220for [ idx , val ] in items ( l ) 221call extend ( r , [[ idx , val ]]) 222endfor 223call assert_equal ([[ 0 , 'a' ], [ 1 , 'b' ], [ 2 , 'c' ]], r ) 224 225call assert_fails ( 'call items(3)' , 'E1225:' ) 226endfunc 227 228func Test_string_items () 229let r = [] 230let s = 'ábツ' 231for [ idx , val ] in items ( s ) 232call extend ( r , [[ idx , val ]]) 233endfor 234call assert_equal ([[ 0 , 'á' ], [ 1 , 'b' ], [ 2 , 'ツ' ]], r ) 235endfunc 236 237" Test removing items in list 238func Test_list_func_remove () 239let lines =<< trim END 240#" Test removing 1 element 241VAR l = [1, 2, 3, 4] 242call assert_equal(1, remove(l, 0)) 243call assert_equal([2, 3, 4], l) 244 245LET l = [1, 2, 3, 4] 246call assert_equal(2, remove(l, 1)) 247call assert_equal([1, 3, 4], l) 248 249LET l = [1, 2, 3, 4] 250call assert_equal(4, remove(l, -1)) 251call assert_equal([1, 2, 3], l) 252 253#" Test removing range of element(s) 254LET l = [1, 2, 3, 4] 255call assert_equal([3], remove(l, 2, 2)) 256call assert_equal([1, 2, 4], l) 257 258LET l = [1, 2, 3, 4] 259call assert_equal([2, 3], remove(l, 1, 2)) 260call assert_equal([1, 4], l) 261 262LET l = [1, 2, 3, 4] 263call assert_equal([2, 3], remove(l, -3, -2)) 264call assert_equal([1, 4], l) 265END 266call CheckLegacyAndVim9Success ( lines ) 267 268" Test invalid cases 269let l = [ 1 , 2 , 3 , 4 ] 270call assert_fails ( "call remove(l, 5)" , 'E684:' ) 271call assert_fails ( "call remove(l, 1, 5)" , 'E684:' ) 272call assert_fails ( "call remove(l, 3, 2)" , 'E16:' ) 273call assert_fails ( "call remove(1, 0)" , 'E896:' ) 274call assert_fails ( "call remove(l, l)" , 'E745:' ) 275endfunc 276 277" List add() function 278func Test_list_add () 279let lines =<< trim END 280VAR l = [] 281call add(l, 1) 282call add(l, [2, 3]) 283call add(l, []) 284call add(l, v:_null_list) 285call add(l, {'k': 3}) 286call add(l, {}) 287call add(l, v:_null_dict) 288call assert_equal([1, [2, 3], [], [], {'k': 3}, {}, {}], l) 289END 290call CheckLegacyAndVim9Success ( lines ) 291 292" weird legacy behavior 293" call assert_equal(1, add(v:_null_list, 4)) 294endfunc 295 296" Tests for Dictionary type 297 298func Test_dict () 299" Creating Dictionary directly with different types 300let lines =<< trim END 301VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}, } 302call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d)) 303call assert_equal('asd', d.1) 304call assert_equal(['-1', '1', 'b'], sort(keys(d))) 305call assert_equal(['asd', [1, 2, function('strlen')], {'a': 1}], values(d)) 306call extend(d, {3: 33, 1: 99}) 307call extend(d, {'b': 'bbb', 'c': 'ccc'}, "keep") 308call assert_equal({'c': 'ccc', '1': 99, 'b': [1, 2, function('strlen')], '3': 33, '-1': {'a': 1}}, d) 309END 310call CheckLegacyAndVim9Success ( lines ) 311 312let d = { 001 : 'asd' , 'b' : [ 1 , 2 , function ( 'strlen' )], -1 : { 'a' : 1 },} 313call assert_equal ( "{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}" , string ( d )) 314 315let v = [] 316for [ key , val ] in items ( d ) 317call extend ( v , [ key , val ]) 318unlet key val 319endfor 320call assert_equal ([ '1' , 'asd' , 'b' ,[ 1 , 2 , function ( 'strlen' )], '-1' ,{ 'a' : 1 }], v ) 321 322call extend ( d , { 3 : 33 , 1 : 99 }) 323call assert_fails ( "call extend(d, {3:333,4:444}, 'error')" , 'E737:' ) 324 325" duplicate key 326call assert_fails ( "let d = {'k' : 10, 'k' : 20}" , 'E721:' ) 327" missing comma 328call assert_fails ( "let d = {'k' : 10 'k' : 20}" , 'E722:' ) 329" missing curly brace 330call assert_fails ( "let d = {'k' : 10," , 'E723:' ) 331" invalid key 332call assert_fails ( 'let d = #{++ : 10}' , 'E15:' ) 333" wrong type for key 334call assert_fails ( 'let d={[] : 10}' , 'E730:' ) 335" undefined variable as value 336call assert_fails ( "let d={'k' : i}" , 'E121:' ) 337 338" allow key starting with number at the start, not a curly expression 339call assert_equal ({ '1foo' : 77 }, #{ 1foo : 77 }) 340 341" #{expr} is not a curly expression 342let x = 'x' 343call assert_equal (#{ g : x }, #{ g : x }) 344endfunc 345 346" This was allowed in legacy Vim script 347let s: dict_with_spaces = { 'one' : 1 , 'two' : 2 , 'three' : 3 } 348let s: dict_with_spaces_lit = #{ one : 1 , two : 2 , three : 3 } 349 350" Dictionary identity 351func Test_dict_identity () 352let lines =<< trim END 353VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1}, } 354VAR dd = d 355VAR dx = copy(d) 356call assert_true(d == dd) 357call assert_false(d isnot dd) 358call assert_true(d is dd) 359call assert_true(d == dx) 360call assert_false(d is dx) 361call assert_true(d isnot dx) 362END 363call CheckLegacyAndVim9Success ( lines ) 364endfunc 365 366" removing items with :unlet 367func Test_dict_unlet () 368let lines =<< trim END 369VAR d = {'b': 'bbb', '1': 99, '3': 33, '-1': {'a': 1}} 370unlet d.b 371unlet d[-1] 372call assert_equal({'1': 99, '3': 33}, d) 373END 374call CheckLegacyAndVim9Success ( lines ) 375endfunc 376 377" manipulating a big Dictionary (hashtable.c has a border of 1000 entries) 378func Test_dict_big () 379let d = {} 380for i in range ( 1500 ) 381let d [ i ] = 3000 - i 382endfor 383call assert_equal ([ 3000 , 2900 , 2001 , 1600 , 1501 ], [ d [ 0 ], d [ 100 ], d [ 999 ], d [ 1400 ], d [ 1499 ]]) 384let str = '' 385try 386let n = d [ 1500 ] 387catch 388let str = substitute ( v: exception , '\v(.{14}).*( "\d{4}").*' , '\1\2' , '' ) 389endtry 390call assert_equal ( 'Vim(let):E716: "1500"' , str ) 391 392" lookup each item 393for i in range ( 1500 ) 394call assert_equal ( 3000 - i , d [ i ]) 395endfor 396let i += 1 397 398" delete even items 399while i >= 2 400let i -= 2 401unlet d [ i ] 402endwhile 403call assert_equal ( 'NONE' , get ( d , 1500 - 100 , 'NONE' )) 404call assert_equal ( 2999 , d [ 1 ]) 405 406" delete odd items, checking value, one intentionally wrong 407let d [ 33 ] = 999 408let i = 1 409while i < 1500 410if i != 33 411call assert_equal ( 3000 - i , d [ i ]) 412else 413call assert_equal ( 999 , d [ i ]) 414endif 415unlet d [ i ] 416let i += 2 417endwhile 418call assert_equal ({}, d ) 419unlet d 420endfunc 421 422" Dictionary function 423func Test_dict_func () 424let d = {} 425func d . func ( a ) dict 426return a: a . len ( self . data ) 427endfunc 428let d . data = [ 1 , 2 , 3 ] 429call assert_equal ( 'len: 3' , d . func ( 'len: ' )) 430let x = d . func ( 'again: ' ) 431call assert_equal ( 'again: 3' , x ) 432let Fn = d . func 433call assert_equal ( 'xxx3' , Fn ( 'xxx' )) 434endfunc 435 436func Test_dict_assign () 437let d = {} 438let d . 1 =1 439let d . _ = 2 440call assert_equal ({ '1' : 1 , '_' : 2 }, d ) 441 442let lines =<< trim END 443VAR d = {} 444LET d.a = 1 445LET d._ = 2 446call assert_equal({'a': 1, '_': 2}, d) 447END 448call CheckLegacyAndVim9Success ( lines ) 449 450let lines =<< trim END 451let n = 0 452let n.key = 3 453END 454call CheckScriptFailure ( lines , 'E1203: Dot can only be used on a dictionary: n.key = 3' ) 455let lines =<< trim END 456vim9script 457var n = 0 458n.key = 3 459END 460call CheckScriptFailure ( lines , 'E1203: Dot can only be used on a dictionary: n.key = 3' ) 461let lines =<< trim END 462var n = 0 463n.key = 3 464END 465call CheckDefFailure ( lines , 'E1141:' ) 466 467let d = { 'abc' : {}} 468call assert_fails ( "let d.abc[0z10] = 10" , 'E976: Using a Blob as a String' ) 469endfunc 470 471" Function in script-local List or Dict 472func Test_script_local_dict_func () 473let g: dict = {} 474function g: dict . func () dict 475return 'g:dict.func' . self . foo [ 1 ] . self . foo [ 0 ]( 'asdf' ) 476endfunc 477let g: dict . foo = [ '-' , 2 , 3 ] 478call insert ( g: dict . foo , function ( 'strlen' )) 479call assert_equal ( 'g:dict.func-4' , g: dict . func ()) 480unlet g: dict 481endfunc 482 483" Test removing items in a dictionary 484func Test_dict_func_remove () 485let lines =<< trim END 486VAR d = {1: 'a', 2: 'b', 3: 'c'} 487call assert_equal('b', remove(d, 2)) 488call assert_equal({1: 'a', 3: 'c'}, d) 489END 490call CheckLegacyAndVim9Success ( lines ) 491 492let lines =<< trim END 493VAR d = {1: 'a', 3: 'c'} 494call remove(d, 1, 2) 495END 496call CheckLegacyAndVim9Failure ( lines , 'E118:' ) 497 498let lines =<< trim END 499VAR d = {1: 'a', 3: 'c'} 500call remove(d, 'a') 501END 502call CheckLegacyAndVim9Failure ( lines , 'E716:' ) 503 504let lines =<< trim END 505let d = {'a-b': 55} 506echo d.a-b 507END 508call CheckScriptFailure ( lines , 'E716: Key not present in Dictionary: "a"' ) 509 510let lines =<< trim END 511vim9script 512var d = {'a-b': 55} 513echo d.a-b 514END 515call CheckScriptFailure ( lines , 'E716: Key not present in Dictionary: "a"' ) 516 517let lines =<< trim END 518var d = {'a-b': 55} 519echo d.a-b 520END 521call CheckDefFailure ( lines , 'E1004: White space required before and after ''-''' ) 522 523let lines =<< trim END 524let d = {1: 'a', 3: 'c'} 525call remove(d, []) 526END 527call CheckScriptFailure ( lines , 'E730:' ) 528let lines =<< trim END 529vim9script 530var d = {1: 'a', 3: 'c'} 531call remove(d, []) 532END 533call CheckScriptFailure ( lines , 'E1174: String required for argument 2' ) 534let lines =<< trim END 535var d = {1: 'a', 3: 'c'} 536call remove(d, []) 537END 538call CheckDefExecFailure ( lines , 'E1013: Argument 2: type mismatch, expected string but got list<unknown>' ) 539endfunc 540 541" Nasty: remove func from Dict that's being called (works) 542func Test_dict_func_remove_in_use () 543let d = { 1 : 1 } 544func d . func ( a ) 545return "a:" . a: a 546endfunc 547let expected = 'a:' . string ( get ( d , 'func' )) 548call assert_equal ( expected , d . func ( string ( remove ( d , 'func' )))) 549 550" similar, in a way it also works in Vim9 551let lines =<< trim END 552VAR d = {1: 1, 2: 'x'} 553func GetArg(a) 554return "a:" .. a:a 555endfunc 556LET d.func = function('GetArg') 557VAR expected = 'a:' .. string(get(d, 'func')) 558call assert_equal(expected, d.func(string(remove(d, 'func')))) 559END 560call CheckTransLegacySuccess ( lines ) 561call CheckTransVim9Success ( lines ) 562endfunc 563 564func Test_dict_literal_keys () 565call assert_equal ({ 'one' : 1 , 'two2' : 2 , '3three' : 3 , '44' : 4 }, #{ one : 1 , two2 : 2 , 3three : 3 , 44 : 4 }, ) 566 567" why *{} cannot be used for a literal dictionary 568let blue = 'blue' 569call assert_equal ( '6' , trim ( execute ( 'echo 2 *{blue: 3}.blue' ))) 570 571call assert_fails ( 'eval 1 || #{a:' , 'E15:' ) " used to leak 572endfunc 573 574" Nasty: deepcopy() dict that refers to itself (fails when noref used) 575func Test_dict_deepcopy () 576let lines =<< trim END 577VAR d = {1: 1, 2: '2'} 578VAR l = [4, d, 6] 579LET d[3] = l 580VAR dc = deepcopy(d) 581call deepcopy(d, 1) 582END 583call CheckLegacyAndVim9Failure ( lines , 'E698:' ) 584 585let lines =<< trim END 586VAR d = {1: 1, 2: '2'} 587VAR l = [4, d, 6] 588LET d[3] = l 589VAR l2 = [0, l, l, 3] 590LET l[1] = l2 591VAR l3 = deepcopy(l2) 592call assert_true(l3[1] is l3[2]) 593END 594call CheckLegacyAndVim9Success ( lines ) 595 596call assert_fails ( "call deepcopy([1, 2], 2)" , 'E1212:' ) 597endfunc 598 599" Locked variables 600func Test_list_locked_var () 601" Not tested with :def function, local vars cannot be locked. 602let lines =<< trim END 603VAR expected = [ 604\ [['1000-000', 'ppppppF'], 605\ ['0000-000', 'ppppppp'], 606\ ['0000-000', 'ppppppp']], 607\ [['1000-000', 'ppppppF'], 608\ ['0000-000', 'ppppppp'], 609\ ['0000-000', 'ppppppp']], 610\ [['1100-100', 'ppFppFF'], 611\ ['0000-000', 'ppppppp'], 612\ ['0000-000', 'ppppppp']], 613\ [['1110-110', 'pFFpFFF'], 614\ ['0010-010', 'pFppFpp'], 615\ ['0000-000', 'ppppppp']], 616\ [['1111-111', 'FFFFFFF'], 617\ ['0011-011', 'FFpFFpp'], 618\ ['0000-000', 'ppppppp']] 619\ ] 620for depth in range(5) 621for u in range(3) 622VAR l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}] 623exe "lockvar " .. depth .. " l" 624if u == 1 625exe "unlockvar l" 626elseif u == 2 627exe "unlockvar " .. depth .. " l" 628endif 629VAR ps = islocked("l") .. islocked("l[1]") .. islocked("l[1][1]") .. islocked("l[1][1][0]") .. '-' .. islocked("l[2]") .. islocked("l[2]['6']") .. islocked("l[2]['6'][7]") 630call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth) 631LET ps = '' 632try 633LET l[1][1][0] = 99 634LET ps ..= 'p' 635catch 636LET ps ..= 'F' 637endtry 638try 639LET l[1][1] = [99] 640LET ps ..= 'p' 641catch 642LET ps ..= 'F' 643endtry 644try 645LET l[1] = [99] 646LET ps ..= 'p' 647catch 648LET ps ..= 'F' 649endtry 650try 651LET l[2]['6'][7] = 99 652LET ps ..= 'p' 653catch 654LET ps ..= 'F' 655endtry 656try 657LET l[2][6] = {99: 99} 658LET ps ..= 'p' 659catch 660LET ps ..= 'F' 661endtry 662try 663LET l[2] = {99: 99} 664LET ps ..= 'p' 665catch 666LET ps ..= 'F' 667endtry 668try 669LET l = [99] 670LET ps ..= 'p' 671catch 672LET ps ..= 'F' 673endtry 674call assert_equal(expected[depth][u][1], ps, 'depth: ' .. depth) 675unlock! l 676endfor 677endfor 678END 679call CheckTransLegacySuccess ( lines ) 680call CheckTransVim9Success ( lines ) 681 682call assert_fails ( "let x=islocked('a b')" , 'E488:' ) 683let mylist = [ 1 , 2 , 3 ] 684call assert_fails ( "let x = islocked('mylist[1:2]')" , 'E786:' ) 685let mydict = { 'k' : 'v' } 686call assert_fails ( "let x = islocked('mydict.a')" , 'E716:' ) 687endfunc 688 689" Unletting locked variables 690func Test_list_locked_var_unlet () 691" Not tested with Vim9: script and local variables cannot be unlocked 692let expected = [ 693\ [[ '1000-000' , 'ppppppp' ], 694\ [ '0000-000' , 'ppppppp' ], 695\ [ '0000-000' , 'ppppppp' ]], 696\ [[ '1000-000' , 'ppFppFp' ], 697\ [ '0000-000' , 'ppppppp' ], 698\ [ '0000-000' , 'ppppppp' ]], 699\ [[ '1100-100' , 'pFFpFFp' ], 700\ [ '0000-000' , 'ppppppp' ], 701\ [ '0000-000' , 'ppppppp' ]], 702\ [[ '1110-110' , 'FFFFFFp' ], 703\ [ '0010-010' , 'FppFppp' ], 704\ [ '0000-000' , 'ppppppp' ]], 705\ [[ '1111-111' , 'FFFFFFp' ], 706\ [ '0011-011' , 'FppFppp' ], 707\ [ '0000-000' , 'ppppppp' ]] 708\ ] 709 710for depth in range ( 5 ) 711for u in range ( 3 ) 712unlet ! l 713let l = [ 0 , [ 1 , [ 2 , 3 ]], { 4 : 5 , 6 : { 7 : 8 }}] 714exe "lockvar " . depth . " l" 715if u == 1 716exe "unlockvar l" 717elseif u == 2 718exe "unlockvar " . depth . " l" 719endif 720let ps = islocked ( "l" ). islocked ( "l[1]" ). islocked ( "l[1][1]" ). islocked ( "l[1][1][0]" ) . '-' . islocked ( "l[2]" ). islocked ( "l[2]['6']" ). islocked ( "l[2]['6'][7]" ) 721call assert_equal ( expected [ depth ][ u ][ 0 ], ps , 'depth: ' .. depth ) 722let ps = '' 723try 724unlet l [ 2 ][ '6' ][ 7 ] 725let ps .= 'p' 726catch 727let ps .= 'F' 728endtry 729try 730unlet l [ 2 ][ 6 ] 731let ps .= 'p' 732catch 733let ps .= 'F' 734endtry 735try 736unlet l [ 2 ] 737let ps .= 'p' 738catch 739let ps .= 'F' 740endtry 741try 742unlet l [ 1 ][ 1 ][ 0 ] 743let ps .= 'p' 744catch 745let ps .= 'F' 746endtry 747try 748unlet l [ 1 ][ 1 ] 749let ps .= 'p' 750catch 751let ps .= 'F' 752endtry 753try 754unlet l [ 1 ] 755let ps .= 'p' 756catch 757let ps .= 'F' 758endtry 759try 760unlet l 761let ps .= 'p' 762catch 763let ps .= 'F' 764endtry 765call assert_equal ( expected [ depth ][ u ][ 1 ], ps ) 766endfor 767endfor 768 769" Deleting a list range with locked items works, but changing the items 770" fails. 771let l = [ 1 , 2 , 3 , 4 ] 772lockvar l[1:2] 773call assert_fails ( 'let l[1:2] = [8, 9]' , 'E741:' ) 774unlet l [ 1 : 2 ] 775call assert_equal ([ 1 , 4 ], l ) 776unlet l 777endfunc 778 779" Locked variables and :unlet or list / dict functions 780 781" No :unlet after lock on dict: 782func Test_dict_lock_unlet () 783let d = { 'a' : 99 , 'b' : 100 } 784lockvar 1 d 785call assert_fails ( 'unlet d.a' , 'E741:' ) 786endfunc 787 788" unlet after lock on dict item 789func Test_dict_item_lock_unlet () 790let lines =<< trim END 791VAR d = {'a': 99, 'b': 100} 792lockvar d.a 793unlet d.a 794call assert_equal({'b': 100}, d) 795END 796call CheckLegacyAndVim9Success ( lines ) 797endfunc 798 799" filter() after lock on dict item 800func Test_dict_lock_filter () 801let lines =<< trim END 802VAR d = {'a': 99, 'b': 100} 803lockvar d.a 804call filter(d, 'v:key != "a"') 805call assert_equal({'b': 100}, d) 806END 807call CheckLegacyAndVim9Success ( lines ) 808endfunc 809 810" map() after lock on dict 811func Test_dict_lock_map () 812let lines =<< trim END 813VAR d = {'a': 99, 'b': 100} 814lockvar 1 d 815call map(d, 'v:val + 200') 816call assert_equal({'a': 299, 'b': 300}, d) 817END 818" This won't work in a :def function 819call CheckTransLegacySuccess ( lines ) 820call CheckTransVim9Success ( lines ) 821 822" For a :def function use a global dict. 823let lines =<< trim END 824let g:thedict = {'a': 77, 'b': 88} 825lockvar 1 g:thedict 826def Delkey() 827unlet g:thedict.a 828enddef 829call Delkey() 830END 831" call CheckScriptFailure(lines, 'E741:') 832endfunc 833 834" No extend() after lock on dict item 835func Test_dict_lock_extend () 836let d = { 'a' : 99 , 'b' : 100 } 837lockvar d.a 838call assert_fails ( "call extend(d, {'a' : 123})" , 'E741:' ) 839call assert_equal ({ 'a' : 99 , 'b' : 100 }, d ) 840endfunc 841 842" Cannot use += with a locked dict 843func Test_dict_lock_operator () 844let d = {} 845lockvar d 846call assert_fails ( "let d += {'k' : 10}" , 'E741:' ) 847unlockvar d 848endfunc 849 850" No remove() of write-protected scope-level variable 851func Tfunc1 ( this_is_a_long_parameter_name ) 852call assert_fails ( "call remove(a:, 'this_is_a_long_parameter_name')" , 'E742:' ) 853endfunc 854func Test_dict_scope_var_remove () 855call Tfunc1 ( 'testval' ) 856endfunc 857 858" No extend() of write-protected scope-level variable 859func Test_dict_scope_var_extend () 860call assert_fails ( "call extend(a:, {'this_is_a_long_parameter_name': 1234})" , 'E742:' ) 861endfunc 862func Tfunc2 ( this_is_a_long_parameter_name ) 863call assert_fails ( "call extend(a:, {'this_is_a_long_parameter_name': 1234})" , 'E742:' ) 864endfunc 865func Test_dict_scope_var_extend_overwrite () 866call Tfunc2 ( 'testval' ) 867endfunc 868 869" No :unlet of variable in locked scope 870func Test_lock_var_unlet () 871let b: testvar = 123 872lockvar 1 b: 873call assert_fails ( 'unlet b:testvar' , 'E741:' ) 874unlockvar 1 b: 875unlet ! b: testvar 876endfunc 877 878" No :let += of locked list variable 879func Test_let_lock_list () 880let l = [ 'a' , 'b' , 3 ] 881lockvar 1 l 882call assert_fails ( "let l += ['x']" , 'E741:' ) 883call assert_equal ([ 'a' , 'b' , 3 ], l ) 884 885unlet l 886let l = [ 1 , 2 , 3 , 4 ] 887lockvar ! l 888call assert_equal ([ 1 , 2 , 3 , 4 ], l ) 889unlockvar l[1] 890call assert_fails ( 'unlet l[0:1]' , 'E741:' ) 891call assert_equal ([ 1 , 2 , 3 , 4 ], l ) 892call assert_fails ( 'unlet l[1:2]' , 'E741:' ) 893call assert_equal ([ 1 , 2 , 3 , 4 ], l ) 894unlockvar l[1] 895call assert_fails ( 'let l[0:1] = [0, 1]' , 'E741:' ) 896call assert_equal ([ 1 , 2 , 3 , 4 ], l ) 897call assert_fails ( 'let l[1:2] = [0, 1]' , 'E741:' ) 898call assert_equal ([ 1 , 2 , 3 , 4 ], l ) 899unlet l 900endfunc 901 902" Locking part of the list 903func Test_let_lock_list_items () 904let l = [ 1 , 2 , 3 , 4 ] 905lockvar l[2:] 906call assert_equal ( 0 , islocked ( 'l[0]' )) 907call assert_equal ( 1 , islocked ( 'l[2]' )) 908call assert_equal ( 1 , islocked ( 'l[3]' )) 909call assert_fails ( 'let l[2] = 10' , 'E741:' ) 910call assert_fails ( 'let l[3] = 20' , 'E741:' ) 911unlet l 912endfunc 913 914" lockvar/islocked() triggering script autoloading 915func Test_lockvar_script_autoload () 916let old_rtp = & rtp 917set rtp += ./sautest 918lockvar g:footest#x 919unlockvar g:footest#x 920call assert_equal ( -1 , 'g:footest#x' -> islocked ()) 921call assert_equal ( 0 , exists ( 'g:footest#x' )) 922call assert_equal ( 1 , g: footest#x ) 923let & rtp = old_rtp 924endfunc 925 926" a:000 function argument test 927func s: arg_list_test (...) 928call assert_fails ( 'let a:000 = [1, 2]' , 'E46:' ) 929call assert_fails ( 'let a:000[0] = 9' , 'E742:' ) 930call assert_fails ( 'let a:000[2] = [9, 10]' , 'E742:' ) 931call assert_fails ( 'let a:000[3] = {9 : 10}' , 'E742:' ) 932 933" now the tests that should pass 934let a: 000 [ 2 ][ 1 ] = 9 935call extend ( a: 000 [ 2 ], [ 5 , 6 ]) 936let a: 000 [ 3 ][ 5 ] = 8 937let a: 000 [ 3 ][ 'a' ] = 12 938call assert_equal ([ 1 , 2 , [ 3 , 9 , 5 , 6 ], { 'a' : 12 , '5' : 8 }], a: 000 ) 939endfunc 940 941func Test_func_arg_list () 942call s: arg_list_test ( 1 , 2 , [ 3 , 4 ], { 5 : 6 }) 943endfunc 944 945" Tests for reverse(), sort(), uniq() 946func Test_reverse_sort_uniq () 947let lines =<< trim END 948VAR l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5] 949call assert_equal(['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5], uniq(copy(l))) 950call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(l)) 951call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(reverse(l))) 952if has('float') 953call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(l)) 954call assert_equal([[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0'], reverse(sort(l))) 955call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(reverse(sort(l)))) 956call assert_equal(['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]], uniq(sort(l))) 957 958LET l = [7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four'] 959call assert_equal([-1, 'one', 'two', 'three', 'four', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255], sort(copy(l), 'n')) 960 961LET l = [7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []] 962call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 'i')) 963call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 'i')) 964call assert_equal(['BAR', 'Bar', 'FOO', 'FOOBAR', 'Foo', 'bar', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l))) 965endif 966END 967call CheckLegacyAndVim9Success ( lines ) 968 969call assert_fails ( 'call reverse({})' , 'E1252:' ) 970call assert_fails ( 'call uniq([1, 2], {x, y -> []})' , 'E745:' ) 971call assert_fails ( "call sort([1, 2], function('min'), 1)" , "E1206:" ) 972call assert_fails ( "call sort([1, 2], function('invalid_func'))" , "E700:" ) 973call assert_fails ( "call sort([1, 2], function('min'))" , "E118:" ) 974 975let lines =<< trim END 976call sort(['a', 'b'], 0) 977END 978call CheckDefAndScriptFailure ( lines , 'E1256: String or function required for argument 2' ) 979 980let lines =<< trim END 981call sort(['a', 'b'], 1) 982END 983call CheckDefAndScriptFailure ( lines , 'E1256: String or function required for argument 2' ) 984endfunc 985 986" reduce a list, blob or string 987func Test_reduce () 988let lines =<< trim END 989call assert_equal(1, reduce([], LSTART acc, val LMIDDLE acc + val LEND, 1)) 990call assert_equal(10, reduce([1, 3, 5], LSTART acc, val LMIDDLE acc + val LEND, 1)) 991call assert_equal(2 * (2 * ((2 * 1) + 2) + 3) + 4, reduce([2, 3, 4], LSTART acc, val LMIDDLE 2 * acc + val LEND, 1)) 992call assert_equal('a x y z', ['x', 'y', 'z']->reduce(LSTART acc, val LMIDDLE acc .. ' ' .. val LEND, 'a')) 993call assert_equal([0, 1, 2, 3], reduce([1, 2, 3], function('add'), [0])) 994 995VAR l = ['x', 'y', 'z'] 996call assert_equal(42, reduce(l, function('get'), {'x': {'y': {'z': 42 } } })) 997call assert_equal(['x', 'y', 'z'], l) 998 999call assert_equal(1, reduce([1], LSTART acc, val LMIDDLE acc + val LEND)) 1000call assert_equal('x y z', reduce(['x', 'y', 'z'], LSTART acc, val LMIDDLE acc .. ' ' .. val LEND)) 1001call assert_equal(120, range(1, 5)->reduce(LSTART acc, val LMIDDLE acc * val LEND)) 1002 1003call assert_equal(1, reduce(0z, LSTART acc, val LMIDDLE acc + val LEND, 1)) 1004call assert_equal(1 + 0xaf + 0xbf + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE acc + val LEND, 1)) 1005call assert_equal(2 * (2 * 1 + 0xaf) + 0xbf, 0zAFBF->reduce(LSTART acc, val LMIDDLE 2 * acc + val LEND, 1)) 1006 1007call assert_equal(0xff, reduce(0zff, LSTART acc, val LMIDDLE acc + val LEND)) 1008call assert_equal(2 * (2 * 0xaf + 0xbf) + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE 2 * acc + val LEND)) 1009 1010call assert_equal('x,y,z', 'xyz'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND)) 1011call assert_equal('', ''->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, '')) 1012call assert_equal('あ,い,う,え,お,😊,💕', 'あいうえお😊💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND)) 1013call assert_equal('😊,あ,い,う,え,お,💕', 'あいうえお💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, '😊')) 1014call assert_equal('ऊ,ॠ,ॡ', reduce('ऊॠॡ', LSTART acc, val LMIDDLE acc .. ',' .. val LEND)) 1015call assert_equal('c,à,t', reduce('càt', LSTART acc, val LMIDDLE acc .. ',' .. val LEND)) 1016call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND)) 1017call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND)) 1018call assert_equal(',a,b,c', reduce('abc', LSTART acc, val LMIDDLE acc .. ',' .. val LEND, v:_null_string)) 1019 1020call assert_equal(0x7d, reduce([0x30, 0x25, 0x08, 0x61], 'or')) 1021call assert_equal(0x7d, reduce(0z30250861, 'or')) 1022call assert_equal('β', reduce('ββββ', 'matchstr')) 1023END 1024call CheckLegacyAndVim9Success ( lines ) 1025 1026call assert_equal ({ 'x' : 1 , 'y' : 1 , 'z' : 1 }, [ 'x' , 'y' , 'z' ] -> reduce ({ acc , val -> extend ( acc , { val : 1 }) }, {})) 1027" vim9 assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce((acc, val) => extend(acc, {[val]: 1 }), {})) 1028 1029call assert_fails ( "call reduce([], { acc, val -> acc + val })" , 'E998: Reduce of an empty List with no initial value' ) 1030call assert_fails ( "call reduce(0z, { acc, val -> acc + val })" , 'E998: Reduce of an empty Blob with no initial value' ) 1031call assert_fails ( "call reduce(v:_null_blob, { acc, val -> acc + val })" , 'E998: Reduce of an empty Blob with no initial value' ) 1032call assert_fails ( "call reduce('', { acc, val -> acc + val })" , 'E998: Reduce of an empty String with no initial value' ) 1033call assert_fails ( "call reduce(v:_null_string, { acc, val -> acc + val })" , 'E998: Reduce of an empty String with no initial value' ) 1034 1035call assert_fails ( "call reduce({}, { acc, val -> acc + val }, 1)" , 'E1098:' ) 1036call assert_fails ( "call reduce(0, { acc, val -> acc + val }, 1)" , 'E1098:' ) 1037call assert_fails ( "call reduce([1, 2], 'Xdoes_not_exist')" , 'E117:' ) 1038call assert_fails ( "echo reduce(0z01, { acc, val -> 2 * acc + val }, '')" , 'E1210:' ) 1039 1040" call assert_fails("vim9 reduce(0, (acc, val) => (acc .. val), '')", 'E1252:') 1041" call assert_fails("vim9 reduce({}, (acc, val) => (acc .. val), '')", 'E1252:') 1042" call assert_fails("vim9 reduce(0.1, (acc, val) => (acc .. val), '')", 'E1252:') 1043" call assert_fails("vim9 reduce(function('tr'), (acc, val) => (acc .. val), '')", 'E1252:') 1044call assert_fails ( "call reduce('', { acc, val -> acc + val }, 1)" , 'E1174:' ) 1045call assert_fails ( "call reduce('', { acc, val -> acc + val }, {})" , 'E1174:' ) 1046call assert_fails ( "call reduce('', { acc, val -> acc + val }, 0.1)" , 'E1174:' ) 1047call assert_fails ( "call reduce('', { acc, val -> acc + val }, function('tr'))" , 'E1174:' ) 1048call assert_fails ( "call reduce('abc', { a, v -> a10}, '')" , 'E121:' ) 1049call assert_fails ( "call reduce(0z0102, { a, v -> a10}, 1)" , 'E121:' ) 1050call assert_fails ( "call reduce([1, 2], { a, v -> a10}, '')" , 'E121:' ) 1051 1052let g: lut = [ 1 , 2 , 3 , 4 ] 1053func EvilRemove () 1054call remove ( g: lut , 1 ) 1055return 1 1056endfunc 1057call assert_fails ( "call reduce(g:lut, { acc, val -> EvilRemove() }, 1)" , 'E742:' ) 1058unlet g: lut 1059delfunc EvilRemove 1060 1061call assert_equal ( 42 , reduce ( v: _null_list , function ( 'add' ), 42 )) 1062call assert_equal ( 42 , reduce ( v: _null_blob , function ( 'add' ), 42 )) 1063 1064" should not crash 1065" Nvim doesn't have null functions 1066" call assert_fails('echo reduce([1], test_null_function())', 'E1132:') 1067" Nvim doesn't have null partials 1068" call assert_fails('echo reduce([1], test_null_partial())', 'E1132:') 1069 1070" did cause double free 1071function ! OuterReduce () 1072vim 9 echo reduce('ab', 42) 1073endfunction 1074"call assert_fails('call OuterReduce()', 'E1024:') 1075call assert_fails ( "echo reduce('ab', 'NoSuchFunc')" , 'E117:' ) 1076delfunc OuterReduce 1077endfunc 1078 1079" splitting a string to a List using split() 1080func Test_str_split () 1081let lines =<< trim END 1082call assert_equal(['aa', 'bb'], split(' aa bb ')) 1083call assert_equal(['aa', 'bb'], split(' aa bb ', '\W\+', 0)) 1084call assert_equal(['', 'aa', 'bb', ''], split(' aa bb ', '\W\+', 1)) 1085call assert_equal(['', '', 'aa', '', 'bb', '', ''], split(' aa bb ', '\W', 1)) 1086call assert_equal(['aa', '', 'bb'], split(':aa::bb:', ':', 0)) 1087call assert_equal(['', 'aa', '', 'bb', ''], split(':aa::bb:', ':', 1)) 1088call assert_equal(['aa', '', 'bb', 'cc', ''], split('aa,,bb, cc,', ',\s*', 1)) 1089call assert_equal(['a', 'b', 'c'], split('abc', '\zs')) 1090call assert_equal(['', 'a', '', 'b', '', 'c', ''], split('abc', '\zs', 1)) 1091call assert_equal(['abc'], split('abc', '\\%(')) 1092END 1093call CheckLegacyAndVim9Success ( lines ) 1094 1095call assert_fails ( "call split('abc', [])" , 'E730:' ) 1096call assert_fails ( "call split('abc', 'b', [])" , 'E745:' ) 1097endfunc 1098 1099" compare recursively linked list and dict 1100func Test_listdict_compare () 1101let lines =<< trim END 1102VAR l = [1, 2, 3, '4'] 1103VAR d = {'1': 1, '2': l, '3': 3} 1104LET l[1] = d 1105call assert_true(l == l) 1106call assert_true(d == d) 1107call assert_false(l != deepcopy(l)) 1108call assert_false(d != deepcopy(d)) 1109END 1110call CheckLegacyAndVim9Success ( lines ) 1111 1112" comparison errors 1113call assert_fails ( 'echo [1, 2] =~ {}' , 'E691:' ) 1114call assert_fails ( 'echo [1, 2] =~ [1, 2]' , 'E692:' ) 1115call assert_fails ( 'echo {} =~ 5' , 'E735:' ) 1116call assert_fails ( 'echo {} =~ {}' , 'E736:' ) 1117endfunc 1118 1119func Test_recursive_listdict_compare () 1120let l1 = [ 0 , 1 ] 1121let l1 [ 0 ] = l1 1122let l2 = [ 0 , 1 ] 1123let l2 [ 0 ] = l2 1124call assert_true ( l1 == l2 ) 1125let d1 = { 0 : 0 , 1 : 1 } 1126let d1 [ 0 ] = d1 1127let d2 = { 0 : 0 , 1 : 1 } 1128let d2 [ 0 ] = d2 1129call assert_true ( d1 == d2 ) 1130endfunc 1131 1132" compare complex recursively linked list and dict 1133func Test_listdict_compare_complex () 1134let lines =<< trim END 1135VAR l = [] 1136call add(l, l) 1137VAR dict4 = {"l": l} 1138call add(dict4.l, dict4) 1139VAR lcopy = deepcopy(l) 1140VAR dict4copy = deepcopy(dict4) 1141call assert_true(l == lcopy) 1142call assert_true(dict4 == dict4copy) 1143END 1144call CheckLegacyAndVim9Success ( lines ) 1145endfunc 1146 1147" Test for extending lists and dictionaries 1148func Test_listdict_extend () 1149" Test extend() with lists 1150 1151" Pass the same List to extend() 1152let lines =<< trim END 1153VAR l = [1, 2, 3] 1154call assert_equal([1, 2, 3, 1, 2, 3], extend(l, l)) 1155call assert_equal([1, 2, 3, 1, 2, 3], l) 1156 1157LET l = [1, 2, 3] 1158call assert_equal([1, 2, 3, 4, 5, 6], extend(l, [4, 5, 6])) 1159call assert_equal([1, 2, 3, 4, 5, 6], l) 1160 1161LET l = [1, 2, 3] 1162call extend(l, [4, 5, 6], 0) 1163call assert_equal([4, 5, 6, 1, 2, 3], l) 1164 1165LET l = [1, 2, 3] 1166call extend(l, [4, 5, 6], 1) 1167call assert_equal([1, 4, 5, 6, 2, 3], l) 1168 1169LET l = [1, 2, 3] 1170call extend(l, [4, 5, 6], 3) 1171call assert_equal([1, 2, 3, 4, 5, 6], l) 1172 1173LET l = [1, 2, 3] 1174call extend(l, [4, 5, 6], -1) 1175call assert_equal([1, 2, 4, 5, 6, 3], l) 1176 1177LET l = [1, 2, 3] 1178call extend(l, [4, 5, 6], -3) 1179call assert_equal([4, 5, 6, 1, 2, 3], l) 1180 1181LET l = [1, 2, 3] 1182call assert_equal([1, 2, 3], l->extend([])) 1183call assert_equal([1, 2, 3], l->extend(v:_null_list)) 1184call assert_equal([1, 2, 3], l) 1185END 1186call CheckLegacyAndVim9Success ( lines ) 1187 1188let l = [ 1 , 2 , 3 ] 1189call assert_fails ( "call extend(l, [4, 5, 6], 4)" , 'E684:' ) 1190call assert_fails ( "call extend(l, [4, 5, 6], -4)" , 'E684:' ) 1191if has ( 'float' ) 1192call assert_fails ( "call extend(l, [4, 5, 6], 1.2)" , 'E805:' ) 1193endif 1194 1195" Test extend() with dictionaries. 1196 1197" Pass the same Dict to extend() 1198let lines =<< trim END 1199VAR d = {'a': {'b': 'B'}, 'x': 9} 1200call extend(d, d) 1201call assert_equal({'a': {'b': 'B'}, 'x': 9}, d) 1202 1203LET d = {'a': 'A', 'b': 9} 1204call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, extend(d, {'b': 0, 'c': 'C'})) 1205call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d) 1206 1207LET d = {'a': 'A', 'b': 9} 1208call extend(d, {'a': 'A', 'b': 0, 'c': 'C'}, "force") 1209call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d) 1210 1211LET d = {'a': 'A', 'b': 9} 1212call extend(d, {'b': 0, 'c': 'C'}, "keep") 1213call assert_equal({'a': 'A', 'b': 9, 'c': 'C'}, d) 1214 1215LET d = {'a': 'A', 'b': 9} 1216call assert_equal({'a': 'A', 'b': 9}, d->extend({})) 1217call assert_equal({'a': 'A', 'b': 9}, d->extend(v:_null_dict)) 1218call assert_equal({'a': 'A', 'b': 9}, d) 1219END 1220call CheckLegacyAndVim9Success ( lines ) 1221 1222let d = { 'a' : 'A' , 'b' : 'B' } 1223call assert_fails ( "call extend(d, {'b': 0, 'c':'C'}, 'error')" , 'E737:' ) 1224call assert_fails ( "call extend(d, {'b': 0}, [])" , 'E730:' ) 1225call assert_fails ( "call extend(d, {'b': 0, 'c':'C'}, 'xxx')" , 'E475:' ) 1226if has ( 'float' ) 1227call assert_fails ( "call extend(d, {'b': 0, 'c':'C'}, 1.2)" , 'E475:' ) 1228endif 1229call assert_equal ({ 'a' : 'A' , 'b' : 'B' }, d ) 1230 1231call assert_fails ( "call extend([1, 2], 1)" , 'E712:' ) 1232call assert_fails ( "call extend([1, 2], {})" , 'E712:' ) 1233 1234" Extend g: dictionary with an invalid variable name 1235call assert_fails ( "call extend(g:, {'-!' : 10})" , 'E461:' ) 1236 1237" Extend a list with itself. 1238let lines =<< trim END 1239VAR l = [1, 5, 7] 1240call extend(l, l, 0) 1241call assert_equal([1, 5, 7, 1, 5, 7], l) 1242LET l = [1, 5, 7] 1243call extend(l, l, 1) 1244call assert_equal([1, 1, 5, 7, 5, 7], l) 1245LET l = [1, 5, 7] 1246call extend(l, l, 2) 1247call assert_equal([1, 5, 1, 5, 7, 7], l) 1248LET l = [1, 5, 7] 1249call extend(l, l, 3) 1250call assert_equal([1, 5, 7, 1, 5, 7], l) 1251END 1252call CheckLegacyAndVim9Success ( lines ) 1253endfunc 1254 1255func Test_listdict_extendnew () 1256" Test extendnew() with lists 1257let l = [ 1 , 2 , 3 ] 1258call assert_equal ([ 1 , 2 , 3 , 4 , 5 ], extendnew ( l , [ 4 , 5 ])) 1259call assert_equal ([ 1 , 2 , 3 ], l ) 1260lockvar l 1261call assert_equal ([ 1 , 2 , 3 , 4 , 5 ], extendnew ( l , [ 4 , 5 ])) 1262let l2 = extendnew ( l , v: _null_list ) 1263call assert_equal ([ 1 , 2 , 3 ], l2 ) 1264let l2 += [ 4 ] 1265call assert_equal ([ 1 , 2 , 3 , 4 ], l2 ) 1266call assert_equal ([ 1 , 2 , 3 ], l ) 1267 1268" Test extendnew() with dictionaries. 1269let d = { 'a' : { 'b' : 'B' }} 1270call assert_equal ({ 'a' : { 'b' : 'B' }, 'c' : 'cc' }, extendnew ( d , { 'c' : 'cc' })) 1271call assert_equal ({ 'a' : { 'b' : 'B' }}, d ) 1272lockvar d 1273call assert_equal ({ 'a' : { 'b' : 'B' }, 'c' : 'cc' }, extendnew ( d , { 'c' : 'cc' })) 1274let d2 = extendnew ( d , v: _null_dict ) 1275call assert_equal ({ 'a' : { 'b' : 'B' }}, d2 ) 1276let d2 [ 'c' ] = 'C' 1277call assert_equal ({ 'a' : { 'b' : 'B' }, 'c' : 'C' }, d2 ) 1278call assert_equal ({ 'a' : { 'b' : 'B' }}, d ) 1279endfunc 1280 1281func s: check_scope_dict ( x , fixed ) 1282func s: gen_cmd ( cmd , x ) 1283return substitute ( a: cmd , '\<x\ze:' , a: x , 'g' ) 1284endfunc 1285 1286let cmd = s: gen_cmd ( 'let x:foo = 1' , a: x ) 1287if a: fixed 1288call assert_fails ( cmd , 'E461' ) 1289else 1290exe cmd 1291exe s: gen_cmd ( 'call assert_equal(1, x:foo)' , a: x ) 1292endif 1293 1294let cmd = s: gen_cmd ( 'let x:["bar"] = 2' , a: x ) 1295if a: fixed 1296call assert_fails ( cmd , 'E461' ) 1297else 1298exe cmd 1299exe s: gen_cmd ( 'call assert_equal(2, x:bar)' , a: x ) 1300endif 1301 1302let cmd = s: gen_cmd ( 'call extend(x:, {"baz": 3})' , a: x ) 1303if a: fixed 1304call assert_fails ( cmd , 'E742' ) 1305else 1306exe cmd 1307exe s: gen_cmd ( 'call assert_equal(3, x:baz)' , a: x ) 1308endif 1309 1310if a: fixed 1311if a: x ==# 'a' 1312call assert_fails ( 'unlet a:x' , 'E795' ) 1313call assert_fails ( 'call remove(a:, "x")' , 'E742' ) 1314elseif a: x ==# 'v' 1315call assert_fails ( 'unlet v:count' , 'E795' ) 1316call assert_fails ( 'call remove(v:, "count")' , 'E742' ) 1317endif 1318else 1319exe s: gen_cmd ( 'unlet x:foo' , a: x ) 1320exe s: gen_cmd ( 'unlet x:bar' , a: x ) 1321exe s: gen_cmd ( 'call remove(x:, "baz")' , a: x ) 1322endif 1323 1324delfunc s:gen_cmd 1325endfunc 1326 1327func Test_scope_dict () 1328" Test for g: 1329call s: check_scope_dict ( 'g' , v: false ) 1330 1331" Test for s: 1332call s: check_scope_dict ( 's' , v: false ) 1333 1334" Test for l: 1335call s: check_scope_dict ( 'l' , v: false ) 1336 1337" Test for a: 1338call s: check_scope_dict ( 'a' , v: true ) 1339 1340" Test for b: 1341call s: check_scope_dict ( 'b' , v: false ) 1342 1343" Test for w: 1344call s: check_scope_dict ( 'w' , v: false ) 1345 1346" Test for t: 1347call s: check_scope_dict ( 't' , v: false ) 1348 1349" Test for v: 1350call s: check_scope_dict ( 'v' , v: true ) 1351endfunc 1352 1353" Test for deep nesting of lists (> 100) 1354func Test_deep_nested_list () 1355let deep_list = [] 1356let l = deep_list 1357for i in range ( 102 ) 1358let newlist = [] 1359call add ( l , newlist ) 1360let l = newlist 1361endfor 1362call add ( l , 102 ) 1363 1364call assert_fails ( 'let m = deepcopy(deep_list)' , 'E698:' ) 1365call assert_fails ( 'lockvar 110 deep_list' , 'E743:' ) 1366call assert_fails ( 'unlockvar 110 deep_list' , 'E743:' ) 1367" Nvim implements :echo very differently 1368" call assert_fails('let x = execute("echo deep_list")', 'E724:') 1369call test_garbagecollect_now () 1370unlet deep_list 1371endfunc 1372 1373" Test for deep nesting of dicts (> 100) 1374func Test_deep_nested_dict () 1375let deep_dict = {} 1376let d = deep_dict 1377for i in range ( 102 ) 1378let newdict = {} 1379let d . k = newdict 1380let d = newdict 1381endfor 1382let d . k = 'v' 1383 1384call assert_fails ( 'let m = deepcopy(deep_dict)' , 'E698:' ) 1385call assert_fails ( 'lockvar 110 deep_dict' , 'E743:' ) 1386call assert_fails ( 'unlockvar 110 deep_dict' , 'E743:' ) 1387" Nvim implements :echo very differently 1388" call assert_fails('let x = execute("echo deep_dict")', 'E724:') 1389call test_garbagecollect_now () 1390unlet deep_dict 1391endfunc 1392 1393" List and dict indexing tests 1394func Test_listdict_index () 1395call CheckLegacyAndVim9Failure ([ 'echo function("min")[0]' ], 'E695:' ) 1396call CheckLegacyAndVim9Failure ([ 'echo v:true[0]' ], 'E909:' ) 1397call CheckLegacyAndVim9Failure ([ 'echo v:null[0]' ], 'E909:' ) 1398call CheckLegacyAndVim9Failure ([ 'VAR d = {"k": 10}' , 'echo d.' ], [ 'E15:' , 'E1127:' , 'E15:' ]) 1399call CheckLegacyAndVim9Failure ([ 'VAR d = {"k": 10}' , 'echo d[1 : 2]' ], 'E719:' ) 1400 1401call assert_fails ( "let v = [4, 6][{-> 1}]" , 'E729:' ) 1402call CheckDefAndScriptFailure ([ 'var v = [4, 6][() => 1]' ], [ 'E1012:' , 'E703:' ]) 1403 1404call CheckLegacyAndVim9Failure ([ 'VAR v = range(5)[2 : []]' ], [ 'E730:' , 'E1012:' , 'E730:' ]) 1405 1406call assert_fails ( "let v = range(5)[2:{-> 2}(]" , [ 'E15:' , 'E116:' ]) 1407call CheckDefAndScriptFailure ([ 'var v = range(5)[2 : () => 2(]' ], 'E15:' ) 1408 1409call CheckLegacyAndVim9Failure ([ 'VAR v = range(5)[2 : 3' ], [ 'E111:' , 'E1097:' , 'E111:' ]) 1410call CheckLegacyAndVim9Failure ([ 'VAR l = insert([1, 2, 3], 4, 10)' ], 'E684:' ) 1411call CheckLegacyAndVim9Failure ([ 'VAR l = insert([1, 2, 3], 4, -10)' ], 'E684:' ) 1412call CheckLegacyAndVim9Failure ([ 'VAR l = insert([1, 2, 3], 4, [])' ], [ 'E745:' , 'E1013:' , 'E1210:' ]) 1413 1414call CheckLegacyAndVim9Failure ([ 'VAR l = [1, 2, 3]' , 'LET l[i] = 3' ], [ 'E121:' , 'E1001:' , 'E121:' ]) 1415call CheckLegacyAndVim9Failure ([ 'VAR l = [1, 2, 3]' , 'LET l[1.1] = 4' ], [ 'E805:' , 'E1012:' , 'E805:' ]) 1416call CheckLegacyAndVim9Failure ([ 'VAR l = [1, 2, 3]' , 'LET l[: i] = [4, 5]' ], [ 'E121:' , 'E1001:' , 'E121:' ]) 1417call CheckLegacyAndVim9Failure ([ 'VAR l = [1, 2, 3]' , 'LET l[: 3.2] = [4, 5]' ], [ 'E805:' , 'E1012:' , 'E805:' ]) 1418" call CheckLegacyAndVim9Failure(['VAR t = test_unknown()', 'echo t[0]'], ['E685:', 'E909:', 'E685:']) 1419endfunc 1420 1421" Test for a null list 1422func Test_null_list () 1423let l = v: _null_list 1424call assert_equal ( '' , join ( l )) 1425call assert_equal ( 0 , len ( l )) 1426call assert_equal ( 1 , empty ( l )) 1427call assert_fails ( 'let s = join([1, 2], [])' , 'E730:' ) 1428call assert_equal ([], split ( v: _null_string )) 1429call assert_equal ([], l [: 2 ]) 1430call assert_true ([] == l ) 1431call assert_equal ( '[]' , string ( l )) 1432" call assert_equal(0, sort(l)) 1433" call assert_equal(0, sort(l)) 1434" call assert_equal(0, uniq(l)) 1435let k = [] + l 1436call assert_equal ([], k ) 1437let k = l + [] 1438call assert_equal ([], k ) 1439call assert_equal ( 0 , len ( copy ( l ))) 1440call assert_equal ( 0 , count ( l , 5 )) 1441call assert_equal ([], deepcopy ( l )) 1442call assert_equal ( 5 , get ( l , 2 , 5 )) 1443call assert_equal ( -1 , index ( l , 2 , 5 )) 1444" call assert_equal(0, insert(l, 2, -1)) 1445call assert_equal ( 0 , min ( l )) 1446call assert_equal ( 0 , max ( l )) 1447" call assert_equal(0, remove(l, 0, 2)) 1448call assert_equal ([], repeat ( l , 2 )) 1449" call assert_equal(0, reverse(l)) 1450" call assert_equal(0, sort(l)) 1451call assert_equal ( '[]' , string ( l )) 1452" call assert_equal(0, extend(l, l, 0)) 1453lockvar l 1454call assert_equal ( 1 , islocked ( 'l' )) 1455unlockvar l 1456endfunc 1457 1458" Test for a null dict 1459func Test_null_dict () 1460call assert_equal ( v: _null_dict , v: _null_dict ) 1461let d = v: _null_dict 1462call assert_equal ({}, d ) 1463call assert_equal ( 0 , len ( d )) 1464call assert_equal ( 1 , empty ( d )) 1465call assert_equal ([], items ( d )) 1466call assert_equal ([], keys ( d )) 1467call assert_equal ([], values ( d )) 1468call assert_false ( has_key ( d , 'k' )) 1469call assert_equal ( '{}' , string ( d )) 1470call assert_fails ( 'let x = d[10]' ) 1471call assert_equal ({}, {}) 1472call assert_equal ( 0 , len ( copy ( d ))) 1473call assert_equal ( 0 , count ( d , 'k' )) 1474call assert_equal ({}, deepcopy ( d )) 1475call assert_equal ( 20 , get ( d , 'k' , 20 )) 1476call assert_equal ( 0 , min ( d )) 1477call assert_equal ( 0 , max ( d )) 1478call assert_equal ( 0 , remove ( d , 'k' )) 1479call assert_equal ( '{}' , string ( d )) 1480" call assert_equal(0, extend(d, d, 0)) 1481lockvar d 1482call assert_equal ( 1 , islocked ( 'd' )) 1483unlockvar d 1484endfunc 1485 1486" Test for the indexof() function 1487func Test_indexof () 1488let l = [#{ color : 'red' }, #{ color : 'blue' }, #{ color : 'green' }] 1489call assert_equal ( 0 , indexof ( l , { i , v -> v . color == 'red' })) 1490call assert_equal ( 2 , indexof ( l , { i , v -> v . color == 'green' })) 1491call assert_equal ( -1 , indexof ( l , { i , v -> v . color == 'grey' })) 1492call assert_equal ( 1 , indexof ( l , "v:val.color == 'blue'" )) 1493call assert_equal ( -1 , indexof ( l , "v:val.color == 'cyan'" )) 1494 1495let l = [#{ n : 10 }, #{ n : 10 }, #{ n : 20 }] 1496call assert_equal ( 0 , indexof ( l , "v:val.n == 10" , #{ startidx : 0 })) 1497call assert_equal ( 1 , indexof ( l , "v:val.n == 10" , #{ startidx : -2 })) 1498call assert_equal ( -1 , indexof ( l , "v:val.n == 10" , #{ startidx : 4 })) 1499call assert_equal ( -1 , indexof ( l , "v:val.n == 10" , #{ startidx : -4 })) 1500call assert_equal ( 0 , indexof ( l , "v:val.n == 10" , v: _null_dict )) 1501 1502let s = [ "a" , "b" , "c" ] 1503call assert_equal ( 2 , indexof ( s , { _ , v -> v == 'c' })) 1504call assert_equal ( -1 , indexof ( s , { _ , v -> v == 'd' })) 1505call assert_equal ( -1 , indexof ( s , { _ , v -> "v == 'd'" })) 1506 1507call assert_equal ( -1 , indexof ([], { i , v -> v == 'a' })) 1508call assert_equal ( -1 , indexof ([ 1 , 2 , 3 ], { _ , v -> "v == 2" })) 1509call assert_equal ( -1 , indexof ( v: _null_list , { i , v -> v == 'a' })) 1510call assert_equal ( -1 , indexof ( l , v: _null_string )) 1511" Nvim doesn't have null functions 1512" call assert_equal(-1, indexof(l, test_null_function())) 1513call assert_equal ( -1 , indexof ( l , "" )) 1514call assert_fails ( 'let i = indexof(l, " ")' , 'E15:' ) 1515 1516" failure cases 1517call assert_fails ( 'let i = indexof(l, "v:val == ''cyan''")' , 'E735:' ) 1518call assert_fails ( 'let i = indexof(l, "color == ''cyan''")' , 'E121:' ) 1519call assert_fails ( 'let i = indexof(l, {})' , 'E1256:' ) 1520call assert_fails ( 'let i = indexof({}, "v:val == 2")' , 'E1226:' ) 1521call assert_fails ( 'let i = indexof([], "v:val == 2", [])' , 'E1206:' ) 1522 1523func TestIdx ( k , v ) 1524return a: v . n == 20 1525endfunc 1526call assert_equal ( 2 , indexof ( l , function ( "TestIdx" ))) 1527delfunc TestIdx 1528func TestIdx ( k , v ) 1529return {} 1530endfunc 1531call assert_fails ( 'let i = indexof(l, function("TestIdx"))' , 'E728:' ) 1532delfunc TestIdx 1533func TestIdx ( k , v ) 1534throw "IdxError" 1535endfunc 1536call assert_fails ( 'let i = indexof(l, function("TestIdx"))' , 'E605:' ) 1537delfunc TestIdx 1538endfunc 1539 1540func Test_extendnew_leak () 1541" This used to leak memory 1542for i in range ( 100 ) | silent ! call extendnew ([], [], []) | endfor 1543for i in range ( 100 ) | silent ! call extendnew ({}, {}, {}) | endfor 1544endfunc 1545 1546" Test for comparing deeply nested List/Dict values 1547func Test_deep_nested_listdict_compare () 1548let lines =<< trim END 1549func GetNestedList(sz) 1550let l = [] 1551let x = l 1552for i in range(a:sz) 1553let y = [1] 1554call add(x, y) 1555let x = y 1556endfor 1557return l 1558endfunc 1559 1560VAR l1 = GetNestedList(1000) 1561VAR l2 = GetNestedList(999) 1562call assert_false(l1 == l2) 1563 1564#" after 1000 nested items, the lists are considered to be equal 1565VAR l3 = GetNestedList(1001) 1566VAR l4 = GetNestedList(1002) 1567call assert_true(l3 == l4) 1568END 1569call CheckLegacyAndVim9Success ( lines ) 1570 1571let lines =<< trim END 1572func GetNestedDict(sz) 1573let d = {} 1574let x = d 1575for i in range(a:sz) 1576let y = {} 1577let x['a'] = y 1578let x = y 1579endfor 1580return d 1581endfunc 1582 1583VAR d1 = GetNestedDict(1000) 1584VAR d2 = GetNestedDict(999) 1585call assert_false(d1 == d2) 1586 1587#" after 1000 nested items, the Dicts are considered to be equal 1588VAR d3 = GetNestedDict(1001) 1589VAR d4 = GetNestedDict(1002) 1590call assert_true(d3 == d4) 1591END 1592call CheckLegacyAndVim9Success ( lines ) 1593endfunc 1594 1595" vim: shiftwidth=2 sts=2 expandtab