robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(), 2" matchaddpos(), matcharg(), matchdelete(), and setmatches(). 3 4source screendump.vim 5source check.vim 6 7function Test_match () 8highlight MyGroup1 term = bold ctermbg = red guibg = red 9highlight MyGroup2 term = italic ctermbg = green guibg = green 10highlight MyGroup3 term = underline ctermbg = blue guibg = blue 11 12" --- Check that "matcharg()" returns the correct group and pattern if a match 13" --- is defined. 14match MyGroup1 /TODO/ 152 match MyGroup2 /FIXME/ 163 match MyGroup3 /XXX/ 17call assert_equal ([ 'MyGroup1' , 'TODO' ], matcharg ( 1 )) 18call assert_equal ([ 'MyGroup2' , 'FIXME' ], 2 ->matcharg ()) 19call assert_equal ([ 'MyGroup3' , 'XXX' ], matcharg ( 3 )) 20 21" --- Check that "matcharg()" returns an empty list if the argument is not 1, 22" --- 2 or 3 (only 0 and 4 are tested). 23call assert_equal ([], matcharg ( 0 )) 24call assert_equal ([], matcharg ( 4 )) 25 26" --- Check that "matcharg()" returns ['', ''] if a match is not defined. 27match 282 match 293 match 30call assert_equal ([ '' , '' ], matcharg ( 1 )) 31call assert_equal ([ '' , '' ], matcharg ( 2 )) 32call assert_equal ([ '' , '' ], matcharg ( 3 )) 33 34" --- Check that "matchadd()" and "getmatches()" agree on added matches and 35" --- that default values apply. 36let m1 = matchadd ( "MyGroup1" , "TODO" ) 37let m2 = matchadd ( "MyGroup2" , "FIXME" , 42 ) 38let m3 = matchadd ( "MyGroup3" , "XXX" , 60 , 17 ) 39let ans = [{ 'group' : 'MyGroup1' , 'pattern' : 'TODO' , 'priority' : 10 , 'id' : 1000 }, 40 \{ 'group' : 'MyGroup2' , 'pattern' : 'FIXME' , 'priority' : 42 , 'id' : 1001 }, 41 \{ 'group' : 'MyGroup3' , 'pattern' : 'XXX' , 'priority' : 60 , 'id' : 17 }] 42call assert_equal ( ans , getmatches ()) 43 44" --- Check that "matchdelete()" deletes the matches defined in the previous 45" --- test correctly. 46call matchdelete ( m1 ) 47eval m2 ->matchdelete () 48call matchdelete ( m3 ) 49call assert_equal ([], getmatches ()) 50 51" --- Check that "matchdelete()" returns 0 if successful and otherwise -1. 52let m = matchadd ( "MyGroup1" , "TODO" ) 53call assert_equal ( 0 , matchdelete ( m )) 54call assert_fails ( 'call matchdelete(42)' , 'E803:' ) 55 56" --- Check that "clearmatches()" clears all matches defined by ":match" and 57" --- "matchadd()". 58let m1 = matchadd ( "MyGroup1" , "TODO" ) 59let m2 = "MyGroup2" ->matchadd ( "FIXME" , 42 ) 60let m3 = matchadd ( "MyGroup3" , "XXX" , 60 , 17 ) 61match MyGroup1 /COFFEE/ 622 match MyGroup2 /HUMPPA/ 633 match MyGroup3 /VIM/ 64call clearmatches () 65call assert_equal ([], getmatches ()) 66 67" --- Check that "setmatches()" restores a list of matches saved by 68" --- "getmatches()" without changes. (Matches with equal priority must also 69" --- remain in the same order.) 70let m1 = matchadd ( "MyGroup1" , "TODO" ) 71let m2 = matchadd ( "MyGroup2" , "FIXME" , 42 ) 72let m3 = matchadd ( "MyGroup3" , "XXX" , 60 , 17 ) 73match MyGroup1 /COFFEE/ 742 match MyGroup2 /HUMPPA/ 753 match MyGroup3 /VIM/ 76let ml = getmatches () 77call clearmatches () 78call setmatches ( ml ) 79call assert_equal ( ml , getmatches ()) 80call clearmatches () 81 82" --- Check that "setmatches()" will not add two matches with the same ID. The 83" --- expected behaviour (for now) is to add the first match but not the 84" --- second and to return 0 (even though it is a matter of debate whether 85" --- this can be considered successful behaviour). 86let data = [{ 'group' : 'MyGroup1' , 'pattern' : 'TODO' , 'priority' : 10 , 'id' : 1 }, 87 \{ 'group' : 'MyGroup2' , 'pattern' : 'FIXME' , 'priority' : 10 , 'id' : 1 }] 88call assert_fails ( 'call setmatches(data)' , 'E801:' ) 89call assert_equal ([ data [ 0 ]], getmatches ()) 90call clearmatches () 91 92" --- Check that "setmatches()" returns 0 if successful and otherwise -1. 93" --- (A range of valid and invalid input values are tried out to generate the 94" --- return values.) 95call assert_equal ( 0 , setmatches ([])) 96call assert_equal ( 0 , setmatches ([{ 'group' : 'MyGroup1' , 'pattern' : 'TODO' , 'priority' : 10 , 'id' : 1 }])) 97call clearmatches () 98call assert_fails ( 'call setmatches(0)' , 'E714:' ) 99call assert_fails ( 'call setmatches([0])' , 'E474:' ) 100call assert_fails ( "call setmatches([{'wrong key': 'wrong value'}])" , 'E474:' ) 101 102call setline ( 1 , 'abcdefghijklmnopq' ) 103call matchaddpos ( "MyGroup1" , [[ 1 , 5 ], [ 1 , 8 , 3 ]], 10 , 3 ) 1041 105redraw ! 106let v1 = screenattr ( 1 , 1 ) 107let v5 = screenattr ( 1 , 5 ) 108let v6 = screenattr ( 1 , 6 ) 109let v8 = screenattr ( 1 , 8 ) 110let v10 = screenattr ( 1 , 10 ) 111let v11 = screenattr ( 1 , 11 ) 112call assert_notequal ( v1 , v5 ) 113call assert_equal ( v6 , v1 ) 114call assert_equal ( v8 , v5 ) 115call assert_equal ( v10 , v5 ) 116call assert_equal ( v11 , v1 ) 117call assert_equal ([{ 'group' : 'MyGroup1' , 'id' : 3 , 'priority' : 10 , 'pos1' : [ 1 , 5 , 1 ], 'pos2' : [ 1 , 8 , 3 ]}], getmatches ()) 118call clearmatches () 119 120call setline ( 1 , 'abcdΣabcdef' ) 121eval "MyGroup1" ->matchaddpos ([[ 1 , 4 , 2 ], [ 1 , 9 , 2 ]], 10 , 42 ) 1221 123redraw ! 124let v1 = screenattr ( 1 , 1 ) 125let v4 = screenattr ( 1 , 4 ) 126let v5 = screenattr ( 1 , 5 ) 127let v6 = screenattr ( 1 , 6 ) 128let v7 = screenattr ( 1 , 7 ) 129let v8 = screenattr ( 1 , 8 ) 130let v9 = screenattr ( 1 , 9 ) 131let v10 = screenattr ( 1 , 10 ) 132call assert_equal ([{ 'group' : 'MyGroup1' , 'id' : 42 , 'priority' : 10 , 'pos1' : [ 1 , 4 , 2 ], 'pos2' : [ 1 , 9 , 2 ]}], getmatches ()) 133call assert_notequal ( v1 , v4 ) 134call assert_equal ( v5 , v4 ) 135call assert_equal ( v6 , v1 ) 136call assert_equal ( v7 , v1 ) 137call assert_equal ( v8 , v4 ) 138call assert_equal ( v9 , v4 ) 139call assert_equal ( v10 , v1 ) 140 141" Check, that setmatches() can correctly restore the matches from matchaddpos() 142call matchadd ( 'MyGroup1' , '\%2lmatchadd' ) 143let m = getmatches () 144call clearmatches () 145call setmatches ( m ) 146call assert_equal ([{ 'group' : 'MyGroup1' , 'id' : 42 , 'priority' : 10 , 'pos1' : [ 1 , 4 , 2 ], 'pos2' : [ 1 , 9 , 2 ]}, { 'group' : 'MyGroup1' , 'pattern' : '\%2lmatchadd' , 'priority' : 10 , 'id' : 1106 }], getmatches ()) 147 148highlight MyGroup1 NONE 149highlight MyGroup2 NONE 150highlight MyGroup3 NONE 151endfunc 152 153func Test_match_error () 154call assert_fails ( 'match Error' , 'E475:' ) 155call assert_fails ( 'match Error /' , 'E475:' ) 156call assert_fails ( '4match Error /x/' , 'E476:' ) 157call assert_fails ( 'match Error /x/ x' , 'E488:' ) 158endfunc 159 160func Test_matchadd_error () 161call clearmatches () 162" Nvim: not an error anymore: 163" call assert_fails("call matchadd('GroupDoesNotExist', 'X')", 'E28:') 164call matchadd ( 'GroupDoesNotExist' , 'X' ) 165call assert_equal ([{ 'group' : 'GroupDoesNotExist' , 'pattern' : 'X' , 'priority' : 10 , 'id' : 1206 }], getmatches ()) 166call assert_fails ( "call matchadd('Search', '\\(')" , 'E54:' ) 167call assert_fails ( "call matchadd('Search', 'XXX', 1, 123, 1)" , 'E715:' ) 168call assert_fails ( "call matchadd('Error', 'XXX', 1, 3)" , 'E798:' ) 169call assert_fails ( "call matchadd('Error', 'XXX', 1, 0)" , 'E799:' ) 170call assert_fails ( "call matchadd('Error', 'XXX', [], 0)" , 'E745:' ) 171endfunc 172 173func Test_matchaddpos () 174syntax on 175set hlsearch 176 177call setline ( 1 , [ '12345' , 'NP' ]) 178call matchaddpos ( 'Error' , [[ 1 , 2 ], [ 1 , 6 ], [ 2 , 2 ]]) 179redraw ! 180call assert_notequal ( screenattr ( 2 , 2 ), 0 ) 181call assert_equal ( screenattr ( 2 , 2 ), screenattr ( 1 , 2 )) 182call assert_notequal ( screenattr ( 2 , 2 ), screenattr ( 1 , 6 )) 1831 184call matchadd ( 'Search' , 'N\|\n' ) 185redraw ! 186call assert_notequal ( screenattr ( 2 , 1 ), 0 ) 187call assert_equal ( screenattr ( 2 , 1 ), screenattr ( 1 , 6 )) 188exec "norm! i0\<Esc>" 189redraw ! 190call assert_equal ( screenattr ( 2 , 2 ), screenattr ( 1 , 6 )) 191 192" Check overlapping pos 193call clearmatches () 194call setline ( 1 , [ '1234567890' , 'NH' ]) 195call matchaddpos ( 'Error' , [[ 1 , 1 , 5 ], [ 1 , 3 , 5 ], [ 2 , 2 ]]) 196redraw ! 197call assert_notequal ( screenattr ( 2 , 2 ), 0 ) 198call assert_equal ( screenattr ( 2 , 2 ), screenattr ( 1 , 5 )) 199call assert_equal ( screenattr ( 2 , 2 ), screenattr ( 1 , 7 )) 200call assert_notequal ( screenattr ( 2 , 2 ), screenattr ( 1 , 8 )) 201 202call clearmatches () 203call matchaddpos ( 'Error' , [[ 1 ], [ 2 , 2 ]]) 204redraw ! 205call assert_equal ( screenattr ( 2 , 2 ), screenattr ( 1 , 1 )) 206call assert_equal ( screenattr ( 2 , 2 ), screenattr ( 1 , 10 )) 207call assert_notequal ( screenattr ( 2 , 2 ), screenattr ( 1 , 11 )) 208 209" Check overlapping pos 210call clearmatches () 211call setline ( 1 , [ '1234567890' , 'NH' ]) 212call matchaddpos ( 'Error' , [[ 1 , 1 , 5 ], [ 1 , 3 , 5 ], [ 2 , 2 ]]) 213redraw ! 214call assert_notequal ( screenattr ( 2 , 2 ), 0 ) 215call assert_equal ( screenattr ( 2 , 2 ), screenattr ( 1 , 5 )) 216call assert_equal ( screenattr ( 2 , 2 ), screenattr ( 1 , 7 )) 217call assert_notequal ( screenattr ( 2 , 2 ), screenattr ( 1 , 8 )) 218 219nohl 220call clearmatches () 221syntax off 222set hlsearch & 223endfunc 224 225" Add 12 match positions (previously the limit was 8 positions). 226func Test_matchaddpos_dump () 227CheckScreendump 228 229let lines =<< trim END 230call setline(1, ['1234567890123']->repeat(14)) 231call matchaddpos('Search', range(1, 12)->map({i, v -> [v, v]})) 232END 233call writefile ( lines , 'Xmatchaddpos' , 'D' ) 234let buf = RunVimInTerminal ( '-S Xmatchaddpos' , #{ rows : 14 }) 235call VerifyScreenDump ( buf , 'Test_matchaddpos_1' , {}) 236 237call StopVimInTerminal ( buf ) 238endfunc 239 240func Test_matchaddpos_otherwin () 241syntax on 242new 243call setline ( 1 , [ '12345' , 'NP' ]) 244let winid = win_getid () 245 246wincmd w 247call matchadd ( 'Search' , '4' , 10 , -1 , { 'window' : winid }) 248call matchaddpos ( 'Error' , [[ 1 , 2 ], [ 2 , 2 ]], 10 , -1 , { 'window' : winid }) 249redraw ! 250call assert_notequal ( screenattr ( 1 , 2 ), 0 ) 251call assert_notequal ( screenattr ( 1 , 4 ), 0 ) 252call assert_notequal ( screenattr ( 2 , 2 ), 0 ) 253call assert_equal ( screenattr ( 1 , 2 ), screenattr ( 2 , 2 )) 254call assert_notequal ( screenattr ( 1 , 2 ), screenattr ( 1 , 4 )) 255 256let savematches = getmatches ( winid ) 257let expect = [ 258 \{ 'group' : 'Search' , 'pattern' : '4' , 'priority' : 10 , 'id' : 1000 }, 259 \{ 'group' : 'Error' , 'id' : 1001 , 'priority' : 10 , 'pos1' : [ 1 , 2 , 1 ], 'pos2' : [ 2 , 2 , 1 ]}, 260 \] 261call assert_equal ( expect , savematches ) 262 263eval winid ->clearmatches () 264call assert_equal ([], getmatches ( winid )) 265 266call setmatches ( savematches , winid ) 267call assert_equal ( expect , savematches ) 268 269wincmd w 270bwipe ! 271call clearmatches () 272syntax off 273endfunc 274 275func Test_matchaddpos_using_negative_priority () 276set hlsearch 277 278call clearmatches () 279 280call setline ( 1 , 'x' ) 281let @/= 'x' 282redraw ! 283let search_attr = screenattr ( 1 , 1 ) 284 285let @/= '' 286call matchaddpos ( 'Error' , [ 1 ], 10 ) 287redraw ! 288let error_attr = screenattr ( 1 , 1 ) 289 290call setline ( 2 , '-1 match priority' ) 291call matchaddpos ( 'Error' , [ 2 ], -1 ) 292redraw ! 293let negative_match_priority_attr = screenattr ( 2 , 1 ) 294 295call assert_notequal ( negative_match_priority_attr , search_attr , "Match with negative priority is incorrectly highlighted with Search highlight." ) 296call assert_equal ( negative_match_priority_attr , error_attr ) 297 298nohl 299set hlsearch & 300endfunc 301 302func Test_matchaddpos_error () 303call assert_fails ( "call matchaddpos('Error', 1)" , 'E686:' ) 304call assert_fails ( "call matchaddpos('Error', [1], 1, 1)" , 'E798:' ) 305call assert_fails ( "call matchaddpos('Error', [1], 1, 2)" , 'E798:' ) 306call assert_fails ( "call matchaddpos('Error', [1], 1, 0)" , 'E799:' ) 307call assert_fails ( "call matchaddpos('Error', [1], 1, 123, 1)" , 'E715:' ) 308call assert_fails ( "call matchaddpos('Error', [1], 1, 5, {'window':12345})" , 'E957:' ) 309" Why doesn't the following error have an error code E...? 310" call assert_fails("call matchaddpos('Error', [{}])", 'E290:') 311call assert_fails ( "call matchaddpos('Error', [{}])" , 'E5031:' ) 312call assert_equal ( -1 , matchaddpos ( 'Error' , v: _null_list )) 313call assert_equal ( -1 , matchaddpos ( 'Error' , [])) 314call assert_fails ( "call matchaddpos('Error', [1], [], 1)" , 'E745:' ) 315endfunc 316 317" setmatches() with more than one "posN" entry used to keep a reference to 318" every position list, so they were not released until the next garbage 319" collection. A "posN" value that is not a List made it bail out early 320" without releasing the list at all. 321func Test_setmatches_pos_refcount () 322throw 'Skipped: Nvim does not have test_refcount()' 323let p1 = [ 1 , 1 , 1 ] 324let p2 = [ 2 , 1 , 1 ] 325call assert_equal ( 1 , test_refcount ( p1 )) 326call assert_equal ( 1 , test_refcount ( p2 )) 327 328call setmatches ([#{ group : 'Search' , priority : 10 , id : 4 , pos1 : p1 , pos2 : p2 }]) 329call clearmatches () 330call assert_equal ( 1 , test_refcount ( p1 )) 331call assert_equal ( 1 , test_refcount ( p2 )) 332 333let p3 = [ 3 , 1 , 1 ] 334call assert_equal ( -1 , setmatches ([#{ group : 'Search' , priority : 10 , id : 4 , pos1 : p3 , pos2 : {}}])) 335call clearmatches () 336call assert_equal ( 1 , test_refcount ( p3 )) 337endfunc 338 339func OtherWindowCommon () 340let lines =<< trim END 341call setline(1, 'Hello Vim world') 342let mid = matchadd('Error', 'world', 1) 343let winid = win_getid() 344new 345END 346call writefile ( lines , 'XscriptMatchCommon' ) 347let buf = RunVimInTerminal ( '-S XscriptMatchCommon' , #{ rows : 12 }) 348call TermWait ( buf ) 349return buf 350endfunc 351 352func Test_matchdelete_other_window () 353CheckScreendump 354 355let buf = OtherWindowCommon () 356call term_sendkeys ( buf , ":call matchdelete(mid, winid)\<CR>" ) 357call VerifyScreenDump ( buf , 'Test_matchdelete_1' , {}) 358 359call StopVimInTerminal ( buf ) 360call delete ( 'XscriptMatchCommon' ) 361endfunc 362 363func Test_matchdelete_error () 364call assert_fails ( "call matchdelete(0)" , 'E802:' ) 365call assert_fails ( "call matchdelete(1, -1)" , 'E957:' ) 366endfunc 367 368func Test_matchclear_other_window () 369CheckScreendump 370CheckRunVimInTerminal 371let buf = OtherWindowCommon () 372call term_sendkeys ( buf , ":call clearmatches(winid)\<CR>" ) 373call VerifyScreenDump ( buf , 'Test_matchclear_1' , {}) 374 375call StopVimInTerminal ( buf ) 376call delete ( 'XscriptMatchCommon' ) 377endfunc 378 379func Test_matchadd_other_window () 380CheckScreendump 381CheckRunVimInTerminal 382let buf = OtherWindowCommon () 383call term_sendkeys ( buf , ":call matchadd('Search', 'Hello', 1, -1, #{window: winid})\<CR>" ) 384call term_sendkeys ( buf , ":\<CR>" ) 385call VerifyScreenDump ( buf , 'Test_matchadd_1' , {}) 386 387call StopVimInTerminal ( buf ) 388call delete ( 'XscriptMatchCommon' ) 389endfunc 390 391func Test_match_in_linebreak () 392CheckScreendump 393CheckRunVimInTerminal 394 395let lines =<< trim END 396set breakindent linebreak breakat+=] 397call printf('%s]%s', repeat('x', 50), repeat('x', 70))->setline(1) 398call matchaddpos('ErrorMsg', [[1, 51]]) 399END 400call writefile ( lines , 'XscriptMatchLinebreak' , 'D' ) 401let buf = RunVimInTerminal ( '-S XscriptMatchLinebreak' , #{ rows : 10 }) 402call VerifyScreenDump ( buf , 'Test_match_linebreak' , {}) 403 404call StopVimInTerminal ( buf ) 405endfunc 406 407func Test_match_with_incsearch () 408CheckScreendump 409CheckRunVimInTerminal 410 411let lines =<< trim END 412set incsearch 413call setline(1, range(20)) 414call matchaddpos('ErrorMsg', [3]) 415END 416call writefile ( lines , 'XmatchWithIncsearch' , 'D' ) 417let buf = RunVimInTerminal ( '-S XmatchWithIncsearch' , #{ rows : 6 }) 418call VerifyScreenDump ( buf , 'Test_match_with_incsearch_1' , {}) 419 420call term_sendkeys ( buf , ":s/0" ) 421call VerifyScreenDump ( buf , 'Test_match_with_incsearch_2' , {}) 422 423call term_sendkeys ( buf , "\<CR>" ) 424call StopVimInTerminal ( buf ) 425endfunc 426 427" Test for deleting matches outside of the screen redraw top/bottom lines 428" This should cause a redraw of those lines. 429func Test_matchdelete_redraw () 430new 431call setline ( 1 , range ( 1 , 500 )) 432call cursor ( 250 , 1 ) 433let m1 = matchaddpos ( 'Search' , [[ 250 ]]) 434let m2 = matchaddpos ( 'Search' , [[ 10 ], [ 450 ]]) 435redraw ! 436let m3 = matchaddpos ( 'Search' , [[ 240 ], [ 260 ]]) 437call matchdelete ( m2 ) 438let m = getmatches () 439call assert_equal ( 2 , len ( m )) 440call assert_equal ([ 250 ], m [ 0 ]. pos1 ) 441redraw ! 442call matchdelete ( m1 ) 443call assert_equal ( 1 , len ( getmatches ())) 444bw ! 445endfunc 446 447func Test_match_tab_with_linebreak () 448CheckScreendump 449CheckRunVimInTerminal 450 451let lines =<< trim END 452set linebreak 453call setline(1, "\tix") 454call matchadd('ErrorMsg', '\t') 455END 456call writefile ( lines , 'XscriptMatchTabLinebreak' , 'D' ) 457let buf = RunVimInTerminal ( '-S XscriptMatchTabLinebreak' , #{ rows : 10 }) 458call VerifyScreenDump ( buf , 'Test_match_tab_linebreak' , {}) 459 460call StopVimInTerminal ( buf ) 461endfunc 462 463" vim: shiftwidth=2 sts=2 expandtab