robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Tests for various functions. 2 3source shared.vim 4source check.vim 5source term_util.vim 6source screendump.vim 7source vim9.vim 8 9" Must be done first, since the alternate buffer must be unset. 10func Test_00_bufexists () 11call assert_equal ( 0 , bufexists ( 'does_not_exist' )) 12call assert_equal ( 1 , bufexists ( bufnr ( '%' ))) 13call assert_equal ( 0 , bufexists ( 0 )) 14new Xfoo 15let bn = bufnr ( '%' ) 16call assert_equal ( 1 , bufexists ( bn )) 17call assert_equal ( 1 , bufexists ( 'Xfoo' )) 18call assert_equal ( 1 , bufexists ( getcwd () . '/Xfoo' )) 19call assert_equal ( 1 , bufexists ( 0 )) 20bw 21call assert_equal ( 0 , bufexists ( bn )) 22call assert_equal ( 0 , bufexists ( 'Xfoo' )) 23endfunc 24 25func Test_has () 26throw 'Skipped: Nvim has removed some features' 27call assert_equal ( 1 , has ( 'eval' )) 28call assert_equal ( 1 , has ( 'eval' , 1 )) 29 30if has ( 'unix' ) 31call assert_equal ( 1 , or ( has ( 'ttyin' ), 1 )) 32call assert_equal ( 0 , and ( has ( 'ttyout' ), 0 )) 33call assert_equal ( 1 , has ( 'multi_byte_encoding' )) 34call assert_equal ( 0 , has ( ':tearoff' )) 35endif 36call assert_equal ( 1 , has ( 'vcon' , 1 )) 37call assert_equal ( 1 , has ( 'mouse_gpm_enabled' , 1 )) 38 39call assert_equal ( has ( 'gui_win32' ) && has ( 'menu' ), has ( ':tearoff' )) 40 41call assert_equal ( 0 , has ( 'nonexistent' )) 42call assert_equal ( 0 , has ( 'nonexistent' , 1 )) 43 44" Will we ever have patch 9999? 45let ver = 'patch-' .. v: version / 100 .. '.' .. v: version % 100 .. '.9999' 46call assert_equal ( 0 , has ( ver )) 47 48" There actually isn't a patch 9.0.0, but this is more consistent. 49call assert_equal ( 1 , has ( 'patch-9.0.0' )) 50endfunc 51 52func Test_empty () 53call assert_equal ( 1 , empty ( '' )) 54call assert_equal ( 0 , empty ( 'a' )) 55 56call assert_equal ( 1 , empty ( 0 )) 57call assert_equal ( 1 , empty ( -0 )) 58call assert_equal ( 0 , empty ( 1 )) 59call assert_equal ( 0 , empty ( -1 )) 60 61if has ( 'float' ) 62call assert_equal ( 1 , empty ( 0.0 )) 63call assert_equal ( 1 , empty ( -0.0 )) 64call assert_equal ( 0 , empty ( 1.0 )) 65call assert_equal ( 0 , empty ( -1.0 )) 66call assert_equal ( 0 , empty ( 1.0 / 0.0 )) 67call assert_equal ( 0 , empty ( 0.0 / 0.0 )) 68endif 69 70call assert_equal ( 1 , empty ([])) 71call assert_equal ( 0 , empty ([ 'a' ])) 72 73call assert_equal ( 1 , empty ({})) 74call assert_equal ( 0 , empty ({ 'a' : 1 })) 75 76call assert_equal ( 1 , empty ( v: null )) 77" call assert_equal(1, empty(v:none)) 78call assert_equal ( 1 , empty ( v: false )) 79call assert_equal ( 0 , empty ( v: true )) 80 81if has ( 'channel' ) 82call assert_equal ( 1 , empty ( test_null_channel ())) 83endif 84if has ( 'job' ) 85call assert_equal ( 1 , empty ( test_null_job ())) 86endif 87 88call assert_equal ( 0 , empty ( function ( 'Test_empty' ))) 89call assert_equal ( 0 , empty ( function ( 'Test_empty' , [ 0 ]))) 90endfunc 91 92func Test_err_teapot () 93throw 'Skipped: Nvim does not have err_teapot()' 94call assert_fails ( 'call err_teapot()' , "E418: I'm a teapot" ) 95call assert_fails ( 'call err_teapot(0)' , "E418: I'm a teapot" ) 96call assert_fails ( 'call err_teapot(v:false)' , "E418: I'm a teapot" ) 97 98call assert_fails ( 'call err_teapot("1")' , "E503: Coffee is currently not available" ) 99call assert_fails ( 'call err_teapot(v:true)' , "E503: Coffee is currently not available" ) 100let expr = 1 101call assert_fails ( 'call err_teapot(expr)' , "E503: Coffee is currently not available" ) 102endfunc 103 104func Test_islocked () 105call assert_fails ( 'call islocked(99)' , 'E475:' ) 106call assert_fails ( 'call islocked("s: x")' , 'E488:' ) 107endfunc 108 109func Test_len () 110call assert_equal ( 1 , len ( 0 )) 111call assert_equal ( 2 , len ( 12 )) 112 113call assert_equal ( 0 , len ( '' )) 114call assert_equal ( 2 , len ( 'ab' )) 115 116call assert_equal ( 0 , len ([])) 117call assert_equal ( 0 , len ( v: _null_list )) 118call assert_equal ( 2 , len ([ 2 , 1 ])) 119 120call assert_equal ( 0 , len ({})) 121call assert_equal ( 0 , len ( v: _null_dict )) 122call assert_equal ( 2 , len ({ 'a' : 1 , 'b' : 2 })) 123 124" call assert_fails('call len(v:none)', 'E701:') 125call assert_fails ( 'call len({-> 0})' , 'E701:' ) 126endfunc 127 128func Test_max () 129call assert_equal ( 0 , max ([])) 130call assert_equal ( 2 , max ([ 2 ])) 131call assert_equal ( 2 , max ([ 1 , 2 ])) 132call assert_equal ( 2 , max ([ 1 , 2 , v: null ])) 133 134call assert_equal ( 0 , max ({})) 135call assert_equal ( 2 , max ({ 'a' : 1 , 'b' : 2 })) 136 137call assert_fails ( 'call max(1)' , 'E712:' ) 138" call assert_fails('call max(v:none)', 'E712:') 139 140" check we only get one error 141call assert_fails ( 'call max([#{}, [1]])' , [ 'E728:' , 'E728:' ]) 142call assert_fails ( 'call max(#{a: {}, b: [1]})' , [ 'E728:' , 'E728:' ]) 143endfunc 144 145func Test_min () 146call assert_equal ( 0 , min ([])) 147call assert_equal ( 2 , min ([ 2 ])) 148call assert_equal ( 1 , min ([ 1 , 2 ])) 149call assert_equal ( 0 , min ([ 1 , 2 , v: null ])) 150 151call assert_equal ( 0 , min ({})) 152call assert_equal ( 1 , min ({ 'a' : 1 , 'b' : 2 })) 153 154call assert_fails ( 'call min(1)' , 'E712:' ) 155" call assert_fails('call min(v:none)', 'E712:') 156call assert_fails ( 'call min([1, {}])' , 'E728:' ) 157 158" check we only get one error 159call assert_fails ( 'call min([[1], #{}])' , [ 'E745:' , 'E745:' ]) 160call assert_fails ( 'call min(#{a: [1], b: #{}})' , [ 'E745:' , 'E745:' ]) 161endfunc 162 163func Test_strwidth () 164for aw in [ 'single' , 'double' ] 165exe 'set ambiwidth=' . aw 166call assert_equal ( 0 , strwidth ( '' )) 167call assert_equal ( 1 , strwidth ( "\t" )) 168call assert_equal ( 3 , strwidth ( 'Vim' )) 169call assert_equal ( 4 , strwidth ( 1234 )) 170call assert_equal ( 5 , strwidth ( -1234 )) 171 172call assert_equal ( 2 , strwidth ( '😉' )) 173call assert_equal ( 17 , strwidth ( 'Eĥoŝanĝo ĉiuĵaŭde' )) 174call assert_equal (( aw == 'single' ) ? 6 : 7 , strwidth ( 'Straße' )) 175 176call assert_fails ( 'call strwidth({->0})' , 'E729:' ) 177call assert_fails ( 'call strwidth([])' , 'E730:' ) 178call assert_fails ( 'call strwidth({})' , 'E731:' ) 179endfor 180 181if has ( 'float' ) 182call assert_equal ( 3 , strwidth ( 1.2 )) 183call CheckDefExecAndScriptFailure ([ 'echo strwidth(1.2)' ], 'E806:' ) 184endif 185 186set ambiwidth & 187endfunc 188 189func Test_strtrans () 190" The default of 'isprint' is platform-dependent: 0x7f and 0x9f are 191" printable on Win32 and VMS. Set it so the expectations below hold 192" everywhere. 193let save_isprint = & isprint 194set isprint = @,161-255 195 196" printable ASCII is unchanged 197call assert_equal ( '' , strtrans ( '' )) 198call assert_equal ( 'abc' , strtrans ( 'abc' )) 199 200" control characters are displayed as ^X 201call assert_equal ( '^I' , strtrans ( "\t" )) 202call assert_equal ( 'a^Mb^[c' , strtrans ( "a\rb\ec" )) 203call assert_equal ( '^A^_^?' , strtrans ( "\x01\x1f\x7f" )) 204 205" printable multibyte characters are unchanged, including composing 206" characters and characters above 0xffff 207call assert_equal ( 'héllo 你好' , strtrans ( 'héllo 你好' )) 208let s = 'e' .. nr2char ( 0x301 ) .. 'x' 209call assert_equal ( s , strtrans ( s )) 210call assert_equal ( nr2char ( 0x1d11e ), strtrans ( nr2char ( 0x1d11e ))) 211 212" unprintable multibyte characters are displayed in <xx> hex form 213call assert_equal ( '<9f>' , strtrans ( nr2char ( 0x9f ))) 214call assert_equal ( '<200b>' , strtrans ( nr2char ( 0x200b ))) 215call assert_equal ( '<feff>' , strtrans ( nr2char ( 0xfeff ))) 216 217" illegal bytes are displayed in <xx> hex form 218call assert_equal ( 'A<ff>B' , strtrans ( "A\xffB" )) 219 220" a long string mixing all kinds of characters 221call assert_equal ( repeat ( 'a^Bé<9f>' , 100 ), 222 \strtrans ( repeat ( "a\x02é" .. nr2char ( 0x9f ), 100 ))) 223 224" the non-multi-byte code path 225"set encoding=latin1 226set isprint = @,161-255 227call assert_equal ( 'a^Mb^[c' , strtrans ( "a\rb\ec" )) 228call assert_equal ( '^A^_^?' , strtrans ( "\x01\x1f\x7f" )) 229" an unprintable byte above 0x7f uses the meta notation 230"call assert_equal('| ', strtrans("\xa0")) 231"call assert_equal("\xe9", strtrans("\xe9")) 232"call assert_equal("x^B\xe9| y", strtrans("x\x02\xe9\xa0y")) 233set encoding = utf-8 234 235let & isprint = save_isprint 236endfunc 237 238func Test_str2nr () 239call assert_equal ( 0 , str2nr ( '' )) 240call assert_equal ( 1 , str2nr ( '1' )) 241call assert_equal ( 1 , str2nr ( ' 1 ' )) 242 243call assert_equal ( 1 , str2nr ( '+1' )) 244call assert_equal ( 1 , str2nr ( '+ 1' )) 245call assert_equal ( 1 , str2nr ( ' + 1 ' )) 246 247call assert_equal ( -1 , str2nr ( '-1' )) 248call assert_equal ( -1 , str2nr ( '- 1' )) 249call assert_equal ( -1 , str2nr ( ' - 1 ' )) 250 251call assert_equal ( 123456789 , str2nr ( '123456789' )) 252call assert_equal ( -123456789 , str2nr ( '-123456789' )) 253 254call assert_equal ( 5 , str2nr ( '101' , 2 )) 255call assert_equal ( 5 , '0b101' ->str2nr ( 2 )) 256call assert_equal ( 5 , str2nr ( '0B101' , 2 )) 257call assert_equal ( -5 , str2nr ( '-101' , 2 )) 258call assert_equal ( -5 , str2nr ( '-0b101' , 2 )) 259call assert_equal ( -5 , str2nr ( '-0B101' , 2 )) 260 261call assert_equal ( 65 , str2nr ( '101' , 8 )) 262call assert_equal ( 65 , str2nr ( '0101' , 8 )) 263call assert_equal ( -65 , str2nr ( '-101' , 8 )) 264call assert_equal ( -65 , str2nr ( '-0101' , 8 )) 265call assert_equal ( 65 , str2nr ( '0o101' , 8 )) 266call assert_equal ( 65 , str2nr ( '0O0101' , 8 )) 267call assert_equal ( -65 , str2nr ( '-0O101' , 8 )) 268call assert_equal ( -65 , str2nr ( '-0o0101' , 8 )) 269 270call assert_equal ( 11259375 , str2nr ( 'abcdef' , 16 )) 271call assert_equal ( 11259375 , str2nr ( 'ABCDEF' , 16 )) 272call assert_equal ( -11259375 , str2nr ( '-ABCDEF' , 16 )) 273call assert_equal ( 11259375 , str2nr ( '0xabcdef' , 16 )) 274call assert_equal ( 11259375 , str2nr ( '0Xabcdef' , 16 )) 275call assert_equal ( 11259375 , str2nr ( '0XABCDEF' , 16 )) 276call assert_equal ( -11259375 , str2nr ( '-0xABCDEF' , 16 )) 277 278call assert_equal ( 1 , str2nr ( "1'000'000" , 10 , 0 )) 279call assert_equal ( 256 , str2nr ( "1'0000'0000" , 2 , 1 )) 280call assert_equal ( 262144 , str2nr ( "1'000'000" , 8 , 1 )) 281call assert_equal ( 1000000 , str2nr ( "1'000'000" , 10 , 1 )) 282call assert_equal ( 1000 , str2nr ( "1'000''000" , 10 , 1 )) 283call assert_equal ( 65536 , str2nr ( "1'00'00" , 16 , 1 )) 284 285call assert_equal ( 0 , str2nr ( '0x10' )) 286call assert_equal ( 0 , str2nr ( '0b10' )) 287call assert_equal ( 0 , str2nr ( '0o10' )) 288call assert_equal ( 1 , str2nr ( '12' , 2 )) 289call assert_equal ( 1 , str2nr ( '18' , 8 )) 290call assert_equal ( 1 , str2nr ( '1g' , 16 )) 291 292call assert_equal ( 0 , str2nr ( v: null )) 293" call assert_equal(0, str2nr(v:none)) 294 295call assert_fails ( 'call str2nr([])' , 'E730:' ) 296call assert_fails ( 'call str2nr({->2})' , 'E729:' ) 297if has ( 'float' ) 298call assert_equal ( 1 , str2nr ( 1.2 )) 299call CheckDefExecFailure ([ 'echo str2nr(1.2)' ], 'E1013:' ) 300call CheckScriptFailure ([ 'vim9script' , 'echo str2nr(1.2)' ], 'E806:' ) 301endif 302call assert_fails ( 'call str2nr(10, [])' , 'E745:' ) 303endfunc 304 305func Test_strftime () 306CheckFunction strftime 307 308" Format of strftime() depends on system. We assume 309" that basic formats tested here are available and 310" identical on all systems which support strftime(). 311" 312" The 2nd parameter of strftime() is a local time, so the output day 313" of strftime() can be 17 or 18, depending on timezone. 314call assert_match ( '^2017-01-1[78]$' , strftime ( '%Y-%m-%d' , 1484695512 )) 315" 316call assert_match ( '^\d\d\d\d-\(0\d\|1[012]\)-\([012]\d\|3[01]\) \([01]\d\|2[0-3]\):[0-5]\d:\([0-5]\d\|60\)$' , '%Y-%m-%d %H:%M:%S' ->strftime ()) 317 318call assert_fails ( 'call strftime([])' , 'E730:' ) 319call assert_fails ( 'call strftime("%Y", [])' , 'E745:' ) 320 321" Check that the time changes after we change the timezone 322" Save previous timezone value, if any 323if exists ( '$TZ' ) 324let tz = $ TZ 325endif 326 327" Force different time zones, save the current hour (24-hour clock) for each 328let $ TZ = 'GMT+1' |let one = strftime ( '%H' ) 329let $ TZ = 'GMT+2' |let two = strftime ( '%H' ) 330 331" Those hours should be two bytes long, and should not be the same; if they 332" are, a tzset(3) call may have failed somewhere 333call assert_equal ( strlen ( one ), 2 ) 334call assert_equal ( strlen ( two ), 2 ) 335" TODO: this fails on MS-Windows 336if has ( 'unix' ) 337call assert_notequal ( one , two ) 338endif 339 340" If we cached a timezone value, put it back, otherwise clear it 341if exists ( 'tz' ) 342let $ TZ = tz 343else 344unlet $ TZ 345endif 346endfunc 347 348func Test_strptime () 349CheckFunction strptime 350CheckNotBSD 351CheckNotMSWindows 352 353if exists ( '$TZ' ) 354let tz = $ TZ 355endif 356let $ TZ = 'UTC' 357 358call assert_equal ( 1484653763 , strptime ( '%Y-%m-%d %T' , '2017-01-17 11:49:23' )) 359 360" Force DST and check that it's considered 361let $ TZ = 'WINTER0SUMMER,J1,J365' 362call assert_equal ( 1484653763 - 3600 , strptime ( '%Y-%m-%d %T' , '2017-01-17 11:49:23' )) 363 364call assert_fails ( 'call strptime()' , 'E119:' ) 365call assert_fails ( 'call strptime("xxx")' , 'E119:' ) 366" This fails on BSD 14 and returns 367" -2209078800 instead of 0 368call assert_equal ( 0 , strptime ( "%Y" , '' )) 369call assert_equal ( 0 , strptime ( "%Y" , "xxx" )) 370 371if exists ( 'tz' ) 372let $ TZ = tz 373else 374unlet $ TZ 375endif 376endfunc 377 378func Test_repeat_overflow () 379call assert_fails ( "call repeat('abcdef', 715827883)" , 'E1510:' ) 380call assert_fails ( "call repeat('ab', 4294967297)" , 'E1510:' ) 381call assert_fails ( 'call repeat([1, 2, 3, 4, 5, 6], 715827883)' , 'E1510:' ) 382endfunc 383 384func Test_resolve_unix () 385CheckUnix 386 387" Xlink1 -> Xlink2 388" Xlink2 -> Xlink3 389silent !ln -s -fXlink2 Xlink1 390silent !ln -s -fXlink3 Xlink2 391call assert_equal ( 'Xlink3' , resolve ( 'Xlink1' )) 392call assert_equal ( './Xlink3' , resolve ( './Xlink1' )) 393call assert_equal ( 'Xlink3/' , resolve ( 'Xlink2/' )) 394" FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?! 395"call assert_equal('Xlink3/', resolve('Xlink1/')) 396"call assert_equal('./Xlink3/', resolve('./Xlink1/')) 397"call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/')) 398call assert_equal ( getcwd () . '/Xlink3' , resolve ( getcwd () . '/Xlink1' )) 399 400" Test resolve() with a symlink cycle. 401" Xlink1 -> Xlink2 402" Xlink2 -> Xlink3 403" Xlink3 -> Xlink1 404silent !ln -s -fXlink1 Xlink3 405call assert_fails ( 'call resolve("Xlink1")' , 'E655:' ) 406call assert_fails ( 'call resolve("./Xlink1")' , 'E655:' ) 407call assert_fails ( 'call resolve("Xlink2")' , 'E655:' ) 408call assert_fails ( 'call resolve("Xlink3")' , 'E655:' ) 409call delete ( 'Xlink1' ) 410call delete ( 'Xlink2' ) 411call delete ( 'Xlink3' ) 412 413silent !ln -s -fXdir//Xfile Xlink 414call assert_equal ( 'Xdir/Xfile' , resolve ( 'Xlink' )) 415call delete ( 'Xlink' ) 416 417silent !ln -s -fXlink2/ Xlink1 418call assert_equal ( 'Xlink2' , 'Xlink1' ->resolve ()) 419call assert_equal ( 'Xlink2/' , resolve ( 'Xlink1/' )) 420call delete ( 'Xlink1' ) 421 422silent !ln -s -f ./Xlink2 Xlink1 423call assert_equal ( 'Xlink2' , resolve ( 'Xlink1' )) 424call assert_equal ( './Xlink2' , resolve ( './Xlink1' )) 425call delete ( 'Xlink1' ) 426 427call assert_equal ( '/' , resolve ( '/' )) 428endfunc 429 430func s: normalize_fname ( fname ) 431let ret = substitute ( a: fname , '\' , '/' , 'g' ) 432let ret = substitute ( ret , '//' , '/' , 'g' ) 433return ret ->tolower () 434endfunc 435 436func Test_simplify () 437call assert_equal ( '' , simplify ( '' )) 438call assert_equal ( '/' , simplify ( '/' )) 439call assert_equal ( '/' , simplify ( '/.' )) 440call assert_equal ( '/' , simplify ( '/..' )) 441call assert_equal ( '/...' , simplify ( '/...' )) 442call assert_equal ( '//path' , simplify ( '//path' )) 443if has ( 'unix' ) 444call assert_equal ( '/path' , simplify ( '///path' )) 445call assert_equal ( '/path' , simplify ( '////path' )) 446endif 447 448call assert_equal ( './dir/file' , './dir/file' ->simplify ()) 449call assert_equal ( './dir/file' , simplify ( './//dir//file' )) 450call assert_equal ( './dir/file' , simplify ( './dir/./file' )) 451call assert_equal ( './file' , simplify ( './dir/../file' )) 452call assert_equal ( '../dir/file' , simplify ( 'dir/../../dir/file' )) 453call assert_equal ( './file' , simplify ( 'dir/.././file' )) 454call assert_equal ( '../dir' , simplify ( './../dir' )) 455call assert_equal ( '..' , simplify ( '../testdir/..' )) 456call mkdir ( 'Xdir' ) 457call assert_equal ( '.' , simplify ( 'Xdir/../.' )) 458call delete ( 'Xdir' , 'd' ) 459 460call assert_fails ( 'call simplify({->0})' , 'E729:' ) 461call assert_fails ( 'call simplify([])' , 'E730:' ) 462call assert_fails ( 'call simplify({})' , 'E731:' ) 463if has ( 'float' ) 464call assert_equal ( '1.2' , simplify ( 1.2 )) 465call CheckDefExecAndScriptFailure ([ 'echo simplify(1.2)' ], 'E806:' ) 466endif 467endfunc 468 469func Test_pathshorten () 470call assert_equal ( '' , pathshorten ( '' )) 471call assert_equal ( 'foo' , pathshorten ( 'foo' )) 472call assert_equal ( '/foo' , '/foo' ->pathshorten ()) 473call assert_equal ( 'f/' , pathshorten ( 'foo/' )) 474call assert_equal ( 'f/bar' , pathshorten ( 'foo/bar' )) 475call assert_equal ( 'f/b/foobar' , 'foo/bar/foobar' ->pathshorten ()) 476call assert_equal ( '/f/b/foobar' , pathshorten ( '/foo/bar/foobar' )) 477call assert_equal ( '.f/bar' , pathshorten ( '.foo/bar' )) 478call assert_equal ( '~f/bar' , pathshorten ( '~foo/bar' )) 479call assert_equal ( '~.f/bar' , pathshorten ( '~.foo/bar' )) 480call assert_equal ( '.~f/bar' , pathshorten ( '.~foo/bar' )) 481call assert_equal ( '~/f/bar' , pathshorten ( '~/foo/bar' )) 482call assert_fails ( 'call pathshorten([])' , 'E730:' ) 483 484" test pathshorten with optional variable to set preferred size of shortening 485call assert_equal ( '' , pathshorten ( '' , 2 )) 486call assert_equal ( 'foo' , pathshorten ( 'foo' , 2 )) 487call assert_equal ( '/foo' , pathshorten ( '/foo' , 2 )) 488call assert_equal ( 'fo/' , pathshorten ( 'foo/' , 2 )) 489call assert_equal ( 'fo/bar' , pathshorten ( 'foo/bar' , 2 )) 490call assert_equal ( 'fo/ba/foobar' , pathshorten ( 'foo/bar/foobar' , 2 )) 491call assert_equal ( '/fo/ba/foobar' , pathshorten ( '/foo/bar/foobar' , 2 )) 492call assert_equal ( '.fo/bar' , pathshorten ( '.foo/bar' , 2 )) 493call assert_equal ( '~fo/bar' , pathshorten ( '~foo/bar' , 2 )) 494call assert_equal ( '~.fo/bar' , pathshorten ( '~.foo/bar' , 2 )) 495call assert_equal ( '.~fo/bar' , pathshorten ( '.~foo/bar' , 2 )) 496call assert_equal ( '~/fo/bar' , pathshorten ( '~/foo/bar' , 2 )) 497call assert_fails ( 'call pathshorten([],2)' , 'E730:' ) 498call assert_notequal ( '~/fo/bar' , pathshorten ( '~/foo/bar' , 3 )) 499call assert_equal ( '~/foo/bar' , pathshorten ( '~/foo/bar' , 3 )) 500call assert_equal ( '~/f/bar' , pathshorten ( '~/foo/bar' , 0 )) 501endfunc 502 503func Test_strpart () 504call assert_equal ( 'de' , strpart ( 'abcdefg' , 3 , 2 )) 505call assert_equal ( 'ab' , strpart ( 'abcdefg' , -2 , 4 )) 506call assert_equal ( 'abcdefg' , 'abcdefg' ->strpart ( -2 )) 507call assert_equal ( 'fg' , strpart ( 'abcdefg' , 5 , 4 )) 508call assert_equal ( 'defg' , strpart ( 'abcdefg' , 3 )) 509call assert_equal ( '' , strpart ( 'abcdefg' , 10 )) 510call assert_fails ( "let s=strpart('abcdef', [])" , 'E745:' ) 511 512call assert_equal ( 'lép' , strpart ( 'éléphant' , 2 , 4 )) 513call assert_equal ( 'léphant' , strpart ( 'éléphant' , 2 )) 514 515call assert_equal ( 'é' , strpart ( 'éléphant' , 0 , 1 , 1 )) 516call assert_equal ( 'ép' , strpart ( 'éléphant' , 3 , 2 , v: true )) 517call assert_equal ( 'ó' , strpart ( 'cómposed' , 1 , 1 , 1 )) 518endfunc 519 520func Test_tolower () 521call assert_equal ( "" , tolower ( "" )) 522 523" Test with all printable ASCII characters. 524call assert_equal ( ' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~' , 525 \tolower ( ' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~' )) 526 527" Test with a few uppercase diacritics. 528call assert_equal ( "aàáâãäåāăąǎǟǡả" , tolower ( "AÀÁÂÃÄÅĀĂĄǍǞǠẢ" )) 529call assert_equal ( "bḃḇ" , tolower ( "BḂḆ" )) 530call assert_equal ( "cçćĉċč" , tolower ( "CÇĆĈĊČ" )) 531call assert_equal ( "dďđḋḏḑ" , tolower ( "DĎĐḊḎḐ" )) 532call assert_equal ( "eèéêëēĕėęěẻẽ" , tolower ( "EÈÉÊËĒĔĖĘĚẺẼ" )) 533call assert_equal ( "fḟ " , tolower ( "FḞ " )) 534call assert_equal ( "gĝğġģǥǧǵḡ" , tolower ( "GĜĞĠĢǤǦǴḠ" )) 535call assert_equal ( "hĥħḣḧḩ" , tolower ( "HĤĦḢḦḨ" )) 536call assert_equal ( "iìíîïĩīĭįiǐỉ" , tolower ( "IÌÍÎÏĨĪĬĮİǏỈ" )) 537call assert_equal ( "jĵ" , tolower ( "JĴ" )) 538call assert_equal ( "kķǩḱḵ" , tolower ( "KĶǨḰḴ" )) 539call assert_equal ( "lĺļľŀłḻ" , tolower ( "LĹĻĽĿŁḺ" )) 540call assert_equal ( "mḿṁ" , tolower ( "MḾṀ" )) 541call assert_equal ( "nñńņňṅṉ" , tolower ( "NÑŃŅŇṄṈ" )) 542call assert_equal ( "oòóôõöøōŏőơǒǫǭỏ" , tolower ( "OÒÓÔÕÖØŌŎŐƠǑǪǬỎ" )) 543call assert_equal ( "pṕṗ" , tolower ( "PṔṖ" )) 544call assert_equal ( "q" , tolower ( "Q" )) 545call assert_equal ( "rŕŗřṙṟ" , tolower ( "RŔŖŘṘṞ" )) 546call assert_equal ( "sśŝşšṡ" , tolower ( "SŚŜŞŠṠ" )) 547call assert_equal ( "tţťŧṫṯ" , tolower ( "TŢŤŦṪṮ" )) 548call assert_equal ( "uùúûüũūŭůűųưǔủ" , tolower ( "UÙÚÛÜŨŪŬŮŰŲƯǓỦ" )) 549call assert_equal ( "vṽ" , tolower ( "VṼ" )) 550call assert_equal ( "wŵẁẃẅẇ" , tolower ( "WŴẀẂẄẆ" )) 551call assert_equal ( "xẋẍ" , tolower ( "XẊẌ" )) 552call assert_equal ( "yýŷÿẏỳỷỹ" , tolower ( "YÝŶŸẎỲỶỸ" )) 553call assert_equal ( "zźżžƶẑẕ" , tolower ( "ZŹŻŽƵẐẔ" )) 554 555" Test with a few lowercase diacritics, which should remain unchanged. 556call assert_equal ( "aàáâãäåāăąǎǟǡả" , tolower ( "aàáâãäåāăąǎǟǡả" )) 557call assert_equal ( "bḃḇ" , tolower ( "bḃḇ" )) 558call assert_equal ( "cçćĉċč" , tolower ( "cçćĉċč" )) 559call assert_equal ( "dďđḋḏḑ" , tolower ( "dďđḋḏḑ" )) 560call assert_equal ( "eèéêëēĕėęěẻẽ" , tolower ( "eèéêëēĕėęěẻẽ" )) 561call assert_equal ( "fḟ" , tolower ( "fḟ" )) 562call assert_equal ( "gĝğġģǥǧǵḡ" , tolower ( "gĝğġģǥǧǵḡ" )) 563call assert_equal ( "hĥħḣḧḩẖ" , tolower ( "hĥħḣḧḩẖ" )) 564call assert_equal ( "iìíîïĩīĭįǐỉ" , tolower ( "iìíîïĩīĭįǐỉ" )) 565call assert_equal ( "jĵǰ" , tolower ( "jĵǰ" )) 566call assert_equal ( "kķǩḱḵ" , tolower ( "kķǩḱḵ" )) 567call assert_equal ( "lĺļľŀłḻ" , tolower ( "lĺļľŀłḻ" )) 568call assert_equal ( "mḿṁ " , tolower ( "mḿṁ " )) 569call assert_equal ( "nñńņňʼnṅṉ" , tolower ( "nñńņňʼnṅṉ" )) 570call assert_equal ( "oòóôõöøōŏőơǒǫǭỏ" , tolower ( "oòóôõöøōŏőơǒǫǭỏ" )) 571call assert_equal ( "pṕṗ" , tolower ( "pṕṗ" )) 572call assert_equal ( "q" , tolower ( "q" )) 573call assert_equal ( "rŕŗřṙṟ" , tolower ( "rŕŗřṙṟ" )) 574call assert_equal ( "sśŝşšṡ" , tolower ( "sśŝşšṡ" )) 575call assert_equal ( "tţťŧṫṯẗ" , tolower ( "tţťŧṫṯẗ" )) 576call assert_equal ( "uùúûüũūŭůűųưǔủ" , tolower ( "uùúûüũūŭůűųưǔủ" )) 577call assert_equal ( "vṽ" , tolower ( "vṽ" )) 578call assert_equal ( "wŵẁẃẅẇẘ" , tolower ( "wŵẁẃẅẇẘ" )) 579call assert_equal ( "ẋẍ" , tolower ( "ẋẍ" )) 580call assert_equal ( "yýÿŷẏẙỳỷỹ" , tolower ( "yýÿŷẏẙỳỷỹ" )) 581call assert_equal ( "zźżžƶẑẕ" , tolower ( "zźżžƶẑẕ" )) 582 583" According to https://twitter.com/jifa/status/625776454479970304 584" Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase 585" in length (2 to 3 bytes) when lowercased. So let's test them. 586call assert_equal ( "ⱥ ⱦ" , tolower ( "Ⱥ Ⱦ" )) 587 588" This call to tolower with invalid utf8 sequence used to cause access to 589" invalid memory. 590call tolower ( "\xC0\x80\xC0" ) 591call tolower ( "123\xC0\x80\xC0" ) 592 593" Test in latin1 encoding 594let save_enc = & encoding 595" set encoding=latin1 596call assert_equal ( "abc" , tolower ( "ABC" )) 597let & encoding = save_enc 598endfunc 599 600func Test_toupper () 601call assert_equal ( "" , toupper ( "" )) 602 603" Test with all printable ASCII characters. 604call assert_equal ( ' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~' , 605 \toupper ( ' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~' )) 606 607" Test with a few lowercase diacritics. 608call assert_equal ( "AÀÁÂÃÄÅĀĂĄǍǞǠẢ" , "aàáâãäåāăąǎǟǡả" ->toupper ()) 609call assert_equal ( "BḂḆ" , toupper ( "bḃḇ" )) 610call assert_equal ( "CÇĆĈĊČ" , toupper ( "cçćĉċč" )) 611call assert_equal ( "DĎĐḊḎḐ" , toupper ( "dďđḋḏḑ" )) 612call assert_equal ( "EÈÉÊËĒĔĖĘĚẺẼ" , toupper ( "eèéêëēĕėęěẻẽ" )) 613call assert_equal ( "FḞ" , toupper ( "fḟ" )) 614call assert_equal ( "GĜĞĠĢǤǦǴḠ" , toupper ( "gĝğġģǥǧǵḡ" )) 615call assert_equal ( "HĤĦḢḦḨẖ" , toupper ( "hĥħḣḧḩẖ" )) 616call assert_equal ( "IÌÍÎÏĨĪĬĮǏỈ" , toupper ( "iìíîïĩīĭįǐỉ" )) 617call assert_equal ( "JĴǰ" , toupper ( "jĵǰ" )) 618call assert_equal ( "KĶǨḰḴ" , toupper ( "kķǩḱḵ" )) 619call assert_equal ( "LĹĻĽĿŁḺ" , toupper ( "lĺļľŀłḻ" )) 620call assert_equal ( "MḾṀ " , toupper ( "mḿṁ " )) 621call assert_equal ( "NÑŃŅŇʼnṄṈ" , toupper ( "nñńņňʼnṅṉ" )) 622call assert_equal ( "OÒÓÔÕÖØŌŎŐƠǑǪǬỎ" , toupper ( "oòóôõöøōŏőơǒǫǭỏ" )) 623call assert_equal ( "PṔṖ" , toupper ( "pṕṗ" )) 624call assert_equal ( "Q" , toupper ( "q" )) 625call assert_equal ( "RŔŖŘṘṞ" , toupper ( "rŕŗřṙṟ" )) 626call assert_equal ( "SŚŜŞŠṠ" , toupper ( "sśŝşšṡ" )) 627call assert_equal ( "TŢŤŦṪṮẗ" , toupper ( "tţťŧṫṯẗ" )) 628call assert_equal ( "UÙÚÛÜŨŪŬŮŰŲƯǓỦ" , toupper ( "uùúûüũūŭůűųưǔủ" )) 629call assert_equal ( "VṼ" , toupper ( "vṽ" )) 630call assert_equal ( "WŴẀẂẄẆẘ" , toupper ( "wŵẁẃẅẇẘ" )) 631call assert_equal ( "ẊẌ" , toupper ( "ẋẍ" )) 632call assert_equal ( "YÝŸŶẎẙỲỶỸ" , toupper ( "yýÿŷẏẙỳỷỹ" )) 633call assert_equal ( "ZŹŻŽƵẐẔ" , toupper ( "zźżžƶẑẕ" )) 634 635" Test that uppercase diacritics, which should remain unchanged. 636call assert_equal ( "AÀÁÂÃÄÅĀĂĄǍǞǠẢ" , toupper ( "AÀÁÂÃÄÅĀĂĄǍǞǠẢ" )) 637call assert_equal ( "BḂḆ" , toupper ( "BḂḆ" )) 638call assert_equal ( "CÇĆĈĊČ" , toupper ( "CÇĆĈĊČ" )) 639call assert_equal ( "DĎĐḊḎḐ" , toupper ( "DĎĐḊḎḐ" )) 640call assert_equal ( "EÈÉÊËĒĔĖĘĚẺẼ" , toupper ( "EÈÉÊËĒĔĖĘĚẺẼ" )) 641call assert_equal ( "FḞ " , toupper ( "FḞ " )) 642call assert_equal ( "GĜĞĠĢǤǦǴḠ" , toupper ( "GĜĞĠĢǤǦǴḠ" )) 643call assert_equal ( "HĤĦḢḦḨ" , toupper ( "HĤĦḢḦḨ" )) 644call assert_equal ( "IÌÍÎÏĨĪĬĮİǏỈ" , toupper ( "IÌÍÎÏĨĪĬĮİǏỈ" )) 645call assert_equal ( "JĴ" , toupper ( "JĴ" )) 646call assert_equal ( "KĶǨḰḴ" , toupper ( "KĶǨḰḴ" )) 647call assert_equal ( "LĹĻĽĿŁḺ" , toupper ( "LĹĻĽĿŁḺ" )) 648call assert_equal ( "MḾṀ" , toupper ( "MḾṀ" )) 649call assert_equal ( "NÑŃŅŇṄṈ" , toupper ( "NÑŃŅŇṄṈ" )) 650call assert_equal ( "OÒÓÔÕÖØŌŎŐƠǑǪǬỎ" , toupper ( "OÒÓÔÕÖØŌŎŐƠǑǪǬỎ" )) 651call assert_equal ( "PṔṖ" , toupper ( "PṔṖ" )) 652call assert_equal ( "Q" , toupper ( "Q" )) 653call assert_equal ( "RŔŖŘṘṞ" , toupper ( "RŔŖŘṘṞ" )) 654call assert_equal ( "SŚŜŞŠṠ" , toupper ( "SŚŜŞŠṠ" )) 655call assert_equal ( "TŢŤŦṪṮ" , toupper ( "TŢŤŦṪṮ" )) 656call assert_equal ( "UÙÚÛÜŨŪŬŮŰŲƯǓỦ" , toupper ( "UÙÚÛÜŨŪŬŮŰŲƯǓỦ" )) 657call assert_equal ( "VṼ" , toupper ( "VṼ" )) 658call assert_equal ( "WŴẀẂẄẆ" , toupper ( "WŴẀẂẄẆ" )) 659call assert_equal ( "XẊẌ" , toupper ( "XẊẌ" )) 660call assert_equal ( "YÝŶŸẎỲỶỸ" , toupper ( "YÝŶŸẎỲỶỸ" )) 661call assert_equal ( "ZŹŻŽƵẐẔ" , toupper ( "ZŹŻŽƵẐẔ" )) 662 663call assert_equal ( "Ⱥ Ⱦ" , toupper ( "ⱥ ⱦ" )) 664 665" This call to toupper with invalid utf8 sequence used to cause access to 666" invalid memory. 667call toupper ( "\xC0\x80\xC0" ) 668call toupper ( "123\xC0\x80\xC0" ) 669 670" Test in latin1 encoding 671let save_enc = & encoding 672" set encoding=latin1 673call assert_equal ( "ABC" , toupper ( "abc" )) 674let & encoding = save_enc 675endfunc 676 677func Test_tr () 678call assert_equal ( 'foo' , tr ( 'bar' , 'bar' , 'foo' )) 679call assert_equal ( 'zxy' , 'cab' ->tr ( 'abc' , 'xyz' )) 680call assert_fails ( "let s=tr([], 'abc', 'def')" , 'E730:' ) 681call assert_fails ( "let s=tr('abc', [], 'def')" , 'E730:' ) 682call assert_fails ( "let s=tr('abc', 'abc', [])" , 'E730:' ) 683call assert_fails ( "let s=tr('abcd', 'abcd', 'def')" , 'E475:' ) 684" set encoding=latin1 685call assert_fails ( "let s=tr('abcd', 'abcd', 'def')" , 'E475:' ) 686call assert_equal ( 'hEllO' , tr ( 'hello' , 'eo' , 'EO' )) 687call assert_equal ( 'hello' , tr ( 'hello' , 'xy' , 'ab' )) 688call assert_fails ( 'call tr("abc", "123", "₁₂")' , 'E475:' ) 689set encoding = utf8 690endfunc 691 692" Tests for the mode() function 693let current_modes = '' 694func Save_mode () 695let g: current_modes = mode ( 0 ) . '-' . mode ( 1 ) 696return '' 697endfunc 698 699" Test for the mode() function 700func Test_mode () 701new 702call append ( 0 , [ "Blue Ball Black" , "Brown Band Bowl" , "" ]) 703 704" Only complete from the current buffer. 705set complete = . 706 707noremap !< F2 > < C-R > =Save_mode()< CR > 708xnoremap < F2 > <Cmd> call Save_mode () <CR> 709 710normal ! 3G 711exe "normal i\<F2>\<Esc>" 712call assert_equal ( 'i-i' , g: current_modes ) 713" i_CTRL-P: Multiple matches 714exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u" 715call assert_equal ( 'i-ic' , g: current_modes ) 716" i_CTRL-P: Single match 717exe "normal iBro\<C-P>\<F2>\<Esc>u" 718call assert_equal ( 'i-ic' , g: current_modes ) 719" i_CTRL-X 720exe "normal iBa\<C-X>\<F2>\<Esc>u" 721call assert_equal ( 'i-ix' , g: current_modes ) 722" i_CTRL-X CTRL-P: Multiple matches 723exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u" 724call assert_equal ( 'i-ic' , g: current_modes ) 725" i_CTRL-X CTRL-P: Single match 726exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u" 727call assert_equal ( 'i-ic' , g: current_modes ) 728" i_CTRL-X CTRL-P + CTRL-P: Single match 729exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" 730call assert_equal ( 'i-ic' , g: current_modes ) 731" i_CTRL-X CTRL-L: Multiple matches 732exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u" 733call assert_equal ( 'i-ic' , g: current_modes ) 734" i_CTRL-X CTRL-L: Single match 735exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u" 736call assert_equal ( 'i-ic' , g: current_modes ) 737" i_CTRL-P: No match 738exe "normal iCom\<C-P>\<F2>\<Esc>u" 739call assert_equal ( 'i-ic' , g: current_modes ) 740" i_CTRL-X CTRL-P: No match 741exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u" 742call assert_equal ( 'i-ic' , g: current_modes ) 743" i_CTRL-X CTRL-L: No match 744exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u" 745call assert_equal ( 'i-ic' , g: current_modes ) 746 747exe "normal R\<F2>\<Esc>" 748call assert_equal ( 'R-R' , g: current_modes ) 749" R_CTRL-P: Multiple matches 750exe "normal RBa\<C-P>\<F2>\<Esc>u" 751call assert_equal ( 'R-Rc' , g: current_modes ) 752" R_CTRL-P: Single match 753exe "normal RBro\<C-P>\<F2>\<Esc>u" 754call assert_equal ( 'R-Rc' , g: current_modes ) 755" R_CTRL-X 756exe "normal RBa\<C-X>\<F2>\<Esc>u" 757call assert_equal ( 'R-Rx' , g: current_modes ) 758" R_CTRL-X CTRL-P: Multiple matches 759exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u" 760call assert_equal ( 'R-Rc' , g: current_modes ) 761" R_CTRL-X CTRL-P: Single match 762exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u" 763call assert_equal ( 'R-Rc' , g: current_modes ) 764" R_CTRL-X CTRL-P + CTRL-P: Single match 765exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" 766call assert_equal ( 'R-Rc' , g: current_modes ) 767" R_CTRL-X CTRL-L: Multiple matches 768exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u" 769call assert_equal ( 'R-Rc' , g: current_modes ) 770" R_CTRL-X CTRL-L: Single match 771exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u" 772call assert_equal ( 'R-Rc' , g: current_modes ) 773" R_CTRL-P: No match 774exe "normal RCom\<C-P>\<F2>\<Esc>u" 775call assert_equal ( 'R-Rc' , g: current_modes ) 776" R_CTRL-X CTRL-P: No match 777exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u" 778call assert_equal ( 'R-Rc' , g: current_modes ) 779" R_CTRL-X CTRL-L: No match 780exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u" 781call assert_equal ( 'R-Rc' , g: current_modes ) 782 783exe "normal gR\<F2>\<Esc>" 784call assert_equal ( 'R-Rv' , g: current_modes ) 785" gR_CTRL-P: Multiple matches 786exe "normal gRBa\<C-P>\<F2>\<Esc>u" 787call assert_equal ( 'R-Rvc' , g: current_modes ) 788" gR_CTRL-P: Single match 789exe "normal gRBro\<C-P>\<F2>\<Esc>u" 790call assert_equal ( 'R-Rvc' , g: current_modes ) 791" gR_CTRL-X 792exe "normal gRBa\<C-X>\<F2>\<Esc>u" 793call assert_equal ( 'R-Rvx' , g: current_modes ) 794" gR_CTRL-X CTRL-P: Multiple matches 795exe "normal gRBa\<C-X>\<C-P>\<F2>\<Esc>u" 796call assert_equal ( 'R-Rvc' , g: current_modes ) 797" gR_CTRL-X CTRL-P: Single match 798exe "normal gRBro\<C-X>\<C-P>\<F2>\<Esc>u" 799call assert_equal ( 'R-Rvc' , g: current_modes ) 800" gR_CTRL-X CTRL-P + CTRL-P: Single match 801exe "normal gRBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" 802call assert_equal ( 'R-Rvc' , g: current_modes ) 803" gR_CTRL-X CTRL-L: Multiple matches 804exe "normal gR\<C-X>\<C-L>\<F2>\<Esc>u" 805call assert_equal ( 'R-Rvc' , g: current_modes ) 806" gR_CTRL-X CTRL-L: Single match 807exe "normal gRBlu\<C-X>\<C-L>\<F2>\<Esc>u" 808call assert_equal ( 'R-Rvc' , g: current_modes ) 809" gR_CTRL-P: No match 810exe "normal gRCom\<C-P>\<F2>\<Esc>u" 811call assert_equal ( 'R-Rvc' , g: current_modes ) 812" gR_CTRL-X CTRL-P: No match 813exe "normal gRCom\<C-X>\<C-P>\<F2>\<Esc>u" 814call assert_equal ( 'R-Rvc' , g: current_modes ) 815" gR_CTRL-X CTRL-L: No match 816exe "normal gRabc\<C-X>\<C-L>\<F2>\<Esc>u" 817call assert_equal ( 'R-Rvc' , g: current_modes ) 818 819call assert_equal ( 'n' , 0 ->mode ()) 820call assert_equal ( 'n' , 1 ->mode ()) 821 822" i_CTRL-O 823exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>" 824call assert_equal ( "n-niI" , g: current_modes ) 825 826" R_CTRL-O 827exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>" 828call assert_equal ( "n-niR" , g: current_modes ) 829 830" gR_CTRL-O 831exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>" 832call assert_equal ( "n-niV" , g: current_modes ) 833 834" How to test operator-pending mode? 835 836call feedkeys ( "v" , 'xt' ) 837call assert_equal ( 'v' , mode ()) 838call assert_equal ( 'v' , mode ( 1 )) 839call feedkeys ( "\<Esc>V" , 'xt' ) 840call assert_equal ( 'V' , mode ()) 841call assert_equal ( 'V' , mode ( 1 )) 842call feedkeys ( "\<Esc>\<C-V>" , 'xt' ) 843call assert_equal ( "\<C-V>" , mode ()) 844call assert_equal ( "\<C-V>" , mode ( 1 )) 845call feedkeys ( "\<Esc>" , 'xt' ) 846 847call feedkeys ( "gh" , 'xt' ) 848call assert_equal ( 's' , mode ()) 849call assert_equal ( 's' , mode ( 1 )) 850call feedkeys ( "\<Esc>gH" , 'xt' ) 851call assert_equal ( 'S' , mode ()) 852call assert_equal ( 'S' , mode ( 1 )) 853call feedkeys ( "\<Esc>g\<C-H>" , 'xt' ) 854call assert_equal ( "\<C-S>" , mode ()) 855call assert_equal ( "\<C-S>" , mode ( 1 )) 856call feedkeys ( "\<Esc>" , 'xt' ) 857 858" v_CTRL-O 859exe "normal gh\<C-O>\<F2>\<Esc>" 860call assert_equal ( "v-vs" , g: current_modes ) 861exe "normal gH\<C-O>\<F2>\<Esc>" 862call assert_equal ( "V-Vs" , g: current_modes ) 863exe "normal g\<C-H>\<C-O>\<F2>\<Esc>" 864call assert_equal ( "\<C-V>-\<C-V>s" , g: current_modes ) 865 866call feedkeys ( ":\<F2>\<CR>" , 'xt' ) 867call assert_equal ( 'c-c' , g: current_modes ) 868call feedkeys ( ":\<Insert>\<F2>\<CR>" , 'xt' ) 869call assert_equal ( "c-cr" , g: current_modes ) 870" Nvim: interactive Ex mode is a cmdwin wrapper: mode()=n/i ("cv" means 871" -es); cannot run inside :normal/feedkeys('x'). 872"call feedkeys("gQ\<F2>vi\<CR>", 'xt') 873"call assert_equal('c-cv', g:current_modes) 874"call feedkeys("gQ\<Insert>\<F2>vi\<CR>", 'xt') 875"call assert_equal("c-cvr", g:current_modes) 876 877" Commandline mode in Visual mode should return "c-c", never "v-v". 878call feedkeys ( "v\<Cmd>call input('')\<CR>\<F2>\<CR>\<Esc>" , 'xt' ) 879call assert_equal ( "c-c" , g: current_modes ) 880 881" Executing commands in Vim Ex mode should return "cv", never "cvr", 882" as Cmdline editing has already ended. 883"call feedkeys("gQcall Save_mode()\<CR>vi\<CR>", 'xt') 884"call assert_equal('c-cv', g:current_modes) 885"call feedkeys("gQ\<Insert>call Save_mode()\<CR>vi\<CR>", 'xt') 886"call assert_equal('c-cv', g:current_modes) 887 888" call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt') 889" call assert_equal('c-ce', g:current_modes) 890 891" Test mode in operatorfunc (it used to be Operator-pending). 892set operatorfunc = OperatorFunc 893function OperatorFunc ( _ ) 894call Save_mode () 895endfunction 896execute "normal! g@l\<Esc>" 897call assert_equal ( 'n-n' , g: current_modes ) 898execute "normal! i\<C-o>g@l\<Esc>" 899call assert_equal ( 'n-niI' , g: current_modes ) 900execute "normal! R\<C-o>g@l\<Esc>" 901call assert_equal ( 'n-niR' , g: current_modes ) 902execute "normal! gR\<C-o>g@l\<Esc>" 903call assert_equal ( 'n-niV' , g: current_modes ) 904 905 906if has ( 'terminal' ) 907term 908call feedkeys ( "\<C-W>N" , 'xt' ) 909call assert_equal ( 'n' , mode ()) 910call assert_equal ( 'nt' , mode ( 1 )) 911call feedkeys ( "aexit\<CR>" , 'xt' ) 912endif 913 914bwipe ! 915unmap ! <F2> 916xunmap <F2> 917set complete & 918set operatorfunc & 919delfunction OperatorFunc 920endfunc 921 922" Test for the mode() function using Screendump feature 923func Test_mode_screendump () 924CheckScreendump 925 926" Test statusline updates for overstrike mode 927let buf = RunVimInTerminal ( '' , { 'rows' : 12 }) 928call term_sendkeys ( buf , ":set laststatus=2 statusline=%!mode(1)\<CR>" ) 929call term_sendkeys ( buf , ":" ) 930call TermWait ( buf ) 931call VerifyScreenDump ( buf , 'Test_mode_1' , {}) 932call term_sendkeys ( buf , "\<Insert>" ) 933call TermWait ( buf ) 934call VerifyScreenDump ( buf , 'Test_mode_2' , {}) 935call StopVimInTerminal ( buf ) 936endfunc 937 938" Test for append() 939func Test_append () 940enew ! 941split 942call assert_equal ( 0 , append ( 1 , [])) 943call assert_equal ( 0 , append ( 1 , v: _null_list )) 944call assert_equal ( 0 , append ( 0 , [ "foo" ])) 945call assert_equal ( 0 , append ( 1 , [])) 946call assert_equal ( 0 , append ( 1 , v: _null_list )) 947call assert_equal ( 0 , append ( 8 , [])) 948call assert_equal ( 0 , append ( 9 , v: _null_list )) 949call assert_equal ([ 'foo' , '' ], getline ( 1 , '$' )) 950split 951only 952undo 953undo 954 955" Using $ instead of '$' must give an error 956call assert_fails ( "call append($, 'foobar')" , 'E116:' ) 957 958call assert_fails ( "call append({}, '')" , [ 'E728:' , 'E728:' ]) 959endfunc 960 961" Test for setline() 962func Test_setline () 963new 964call setline ( 0 , [ "foo" ]) 965call setline ( 0 , []) 966call setline ( 0 , v: _null_list ) 967call setline ( 1 , [ "bar" ]) 968call setline ( 1 , []) 969call setline ( 1 , v: _null_list ) 970call setline ( 2 , []) 971call setline ( 2 , v: _null_list ) 972call setline ( 3 , []) 973call setline ( 3 , v: _null_list ) 974call setline ( 2 , [ "baz" ]) 975call assert_equal ([ 'bar' , 'baz' ], getline ( 1 , '$' )) 976bw ! 977endfunc 978 979func Test_getbufvar () 980let bnr = bufnr ( '%' ) 981let b: var_num = '1234' 982let def_num = '5678' 983call assert_equal ( '1234' , getbufvar ( bnr , 'var_num' )) 984call assert_equal ( '1234' , getbufvar ( bnr , 'var_num' , def_num )) 985 986let bd = getbufvar ( bnr , '' ) 987call assert_equal ( '1234' , bd [ 'var_num' ]) 988call assert_true ( exists ( "bd['changedtick']" )) 989call assert_equal ( 2 , len ( bd )) 990 991let bd2 = getbufvar ( bnr , '' , def_num ) 992call assert_equal ( bd , bd2 ) 993 994unlet b: var_num 995call assert_equal ( def_num , getbufvar ( bnr , 'var_num' , def_num )) 996call assert_equal ( '' , getbufvar ( bnr , 'var_num' )) 997 998let bd = getbufvar ( bnr , '' ) 999call assert_equal ( 1 , len ( bd )) 1000let bd = getbufvar ( bnr , '' , def_num ) 1001call assert_equal ( 1 , len ( bd )) 1002 1003call assert_equal ( '' , getbufvar ( 9999 , '' )) 1004call assert_equal ( def_num , getbufvar ( 9999 , '' , def_num )) 1005unlet def_num 1006 1007call assert_equal ( 0 , getbufvar ( bnr , '&autoindent' )) 1008call assert_equal ( 0 , getbufvar ( bnr , '&autoindent' , 1 )) 1009 1010" Set and get a buffer-local variable 1011call setbufvar ( bnr , 'bufvar_test' , [ 'one' , 'two' ]) 1012call assert_equal ([ 'one' , 'two' ], getbufvar ( bnr , 'bufvar_test' )) 1013 1014" Open new window with forced option values 1015set fileformats = unix,dos 1016new ++ff=dos ++bin ++enc=iso-8859-2 1017call assert_equal ( 'dos' , getbufvar ( bufnr ( '%' ), '&fileformat' )) 1018call assert_equal ( 1 , getbufvar ( bufnr ( '%' ), '&bin' )) 1019call assert_equal ( 'iso-8859-2' , getbufvar ( bufnr ( '%' ), '&fenc' )) 1020close 1021 1022" Get the b: dict. 1023let b: testvar = 'one' 1024new 1025let b: testvar = 'two' 1026let thebuf = bufnr () 1027wincmd w 1028call assert_equal ( 'two' , getbufvar ( thebuf , 'testvar' )) 1029call assert_equal ( 'two' , getbufvar ( thebuf , '' ). testvar ) 1030bwipe ! 1031 1032set fileformats & 1033endfunc 1034 1035func Test_last_buffer_nr () 1036call assert_equal ( bufnr ( '$' ), last_buffer_nr ()) 1037endfunc 1038 1039func Test_stridx () 1040call assert_equal ( -1 , stridx ( '' , 'l' )) 1041call assert_equal ( 0 , stridx ( '' , '' )) 1042call assert_equal ( 0 , 'hello' ->stridx ( '' )) 1043call assert_equal ( -1 , stridx ( 'hello' , 'L' )) 1044call assert_equal ( 2 , stridx ( 'hello' , 'l' , -1 )) 1045call assert_equal ( 2 , stridx ( 'hello' , 'l' , 0 )) 1046call assert_equal ( 2 , 'hello' ->stridx ( 'l' , 1 )) 1047call assert_equal ( 3 , stridx ( 'hello' , 'l' , 3 )) 1048call assert_equal ( -1 , stridx ( 'hello' , 'l' , 4 )) 1049call assert_equal ( -1 , stridx ( 'hello' , 'l' , 10 )) 1050call assert_equal ( 2 , stridx ( 'hello' , 'll' )) 1051call assert_equal ( -1 , stridx ( 'hello' , 'hello world' )) 1052call assert_fails ( "let n=stridx('hello', [])" , 'E730:' ) 1053call assert_fails ( "let n=stridx([], 'l')" , 'E730:' ) 1054endfunc 1055 1056func Test_strridx () 1057call assert_equal ( -1 , strridx ( '' , 'l' )) 1058call assert_equal ( 0 , strridx ( '' , '' )) 1059call assert_equal ( 5 , strridx ( 'hello' , '' )) 1060call assert_equal ( -1 , strridx ( 'hello' , 'L' )) 1061call assert_equal ( 3 , 'hello' ->strridx ( 'l' )) 1062call assert_equal ( 3 , strridx ( 'hello' , 'l' , 10 )) 1063call assert_equal ( 3 , strridx ( 'hello' , 'l' , 3 )) 1064call assert_equal ( 2 , strridx ( 'hello' , 'l' , 2 )) 1065call assert_equal ( -1 , strridx ( 'hello' , 'l' , 1 )) 1066call assert_equal ( -1 , strridx ( 'hello' , 'l' , 0 )) 1067call assert_equal ( -1 , strridx ( 'hello' , 'l' , -1 )) 1068call assert_equal ( 2 , strridx ( 'hello' , 'll' )) 1069call assert_equal ( -1 , strridx ( 'hello' , 'hello world' )) 1070call assert_fails ( "let n=strridx('hello', [])" , 'E730:' ) 1071call assert_fails ( "let n=strridx([], 'l')" , 'E730:' ) 1072endfunc 1073 1074func Test_match_func () 1075call assert_equal ( 4 , match ( 'testing' , 'ing' )) 1076call assert_equal ( 4 , 'testing' ->match ( 'ing' , 2 )) 1077call assert_equal ( -1 , match ( 'testing' , 'ing' , 5 )) 1078call assert_equal ( -1 , match ( 'testing' , 'ing' , 8 )) 1079call assert_equal ( 1 , match ([ 'vim' , 'testing' , 'execute' ], 'ing' )) 1080call assert_equal ( -1 , match ([ 'vim' , 'testing' , 'execute' ], 'img' )) 1081call assert_fails ( "let x=match('vim', [])" , 'E730:' ) 1082call assert_equal ( 3 , match ([ 'a' , 'b' , 'c' , 'a' ], 'a' , 1 )) 1083call assert_equal ( -1 , match ([ 'a' , 'b' , 'c' , 'a' ], 'a' , 5 )) 1084call assert_equal ( 4 , match ( 'testing' , 'ing' , -1 )) 1085call assert_fails ( "let x=match('testing', 'ing', 0, [])" , 'E745:' ) 1086call assert_equal ( -1 , match ( v: _null_list , 2 )) 1087call assert_equal ( -1 , match ( 'abc' , '\\%(' )) 1088endfunc 1089 1090func Test_matchend () 1091call assert_equal ( 7 , matchend ( 'testing' , 'ing' )) 1092call assert_equal ( 7 , 'testing' ->matchend ( 'ing' , 2 )) 1093call assert_equal ( -1 , matchend ( 'testing' , 'ing' , 5 )) 1094call assert_equal ( -1 , matchend ( 'testing' , 'ing' , 8 )) 1095call assert_equal ( match ([ 'vim' , 'testing' , 'execute' ], 'ing' ), matchend ([ 'vim' , 'testing' , 'execute' ], 'ing' )) 1096call assert_equal ( match ([ 'vim' , 'testing' , 'execute' ], 'img' ), matchend ([ 'vim' , 'testing' , 'execute' ], 'img' )) 1097endfunc 1098 1099func Test_matchlist () 1100call assert_equal ([ 'acd' , 'a' , '' , 'c' , 'd' , '' , '' , '' , '' , '' ], matchlist ( 'acd' , '\(a\)\?\(b\)\?\(c\)\?\(.*\)' )) 1101call assert_equal ([ 'd' , '' , '' , '' , 'd' , '' , '' , '' , '' , '' ], 'acd' ->matchlist ( '\(a\)\?\(b\)\?\(c\)\?\(.*\)' , 2 )) 1102call assert_equal ([], matchlist ( 'acd' , '\(a\)\?\(b\)\?\(c\)\?\(.*\)' , 4 )) 1103endfunc 1104 1105func Test_matchstr () 1106call assert_equal ( 'ing' , matchstr ( 'testing' , 'ing' )) 1107call assert_equal ( 'ing' , 'testing' ->matchstr ( 'ing' , 2 )) 1108call assert_equal ( '' , matchstr ( 'testing' , 'ing' , 5 )) 1109call assert_equal ( '' , matchstr ( 'testing' , 'ing' , 8 )) 1110call assert_equal ( 'testing' , matchstr ([ 'vim' , 'testing' , 'execute' ], 'ing' )) 1111call assert_equal ( '' , matchstr ([ 'vim' , 'testing' , 'execute' ], 'img' )) 1112endfunc 1113 1114func Test_matchstrpos () 1115call assert_equal ([ 'ing' , 4 , 7 ], matchstrpos ( 'testing' , 'ing' )) 1116call assert_equal ([ 'ing' , 4 , 7 ], 'testing' ->matchstrpos ( 'ing' , 2 )) 1117call assert_equal ([ '' , -1 , -1 ], matchstrpos ( 'testing' , 'ing' , 5 )) 1118call assert_equal ([ '' , -1 , -1 ], matchstrpos ( 'testing' , 'ing' , 8 )) 1119call assert_equal ([ 'ing' , 1 , 4 , 7 ], matchstrpos ([ 'vim' , 'testing' , 'execute' ], 'ing' )) 1120call assert_equal ([ '' , -1 , -1 , -1 ], matchstrpos ([ 'vim' , 'testing' , 'execute' ], 'img' )) 1121call assert_equal ([ '' , -1 , -1 ], matchstrpos ( v: _null_list , '\a' )) 1122endfunc 1123 1124" Test for matchstrlist() 1125func Test_matchstrlist () 1126let lines =<< trim END 1127#" Basic match 1128call assert_equal([{'idx': 0, 'byteidx': 1, 'text': 'bout'}, 1129\ {'idx': 1, 'byteidx': 1, 'text': 'bove'}], 1130\ matchstrlist(['about', 'above'], 'bo.*')) 1131#" no match 1132call assert_equal([], matchstrlist(['about', 'above'], 'xy.*')) 1133#" empty string 1134call assert_equal([], matchstrlist([''], '.')) 1135#" empty pattern 1136call assert_equal([{'idx': 0, 'byteidx': 0, 'text': ''}], matchstrlist(['abc'], '')) 1137#" method call 1138call assert_equal([{'idx': 0, 'byteidx': 2, 'text': 'it'}], ['editor']->matchstrlist('ed\zsit\zeor')) 1139#" single character matches 1140call assert_equal([{'idx': 0, 'byteidx': 5, 'text': 'r'}], 1141\ ['editor']->matchstrlist('r')) 1142call assert_equal([{'idx': 0, 'byteidx': 0, 'text': 'a'}], ['a']->matchstrlist('a')) 1143call assert_equal([{'idx': 0, 'byteidx': 0, 'text': ''}], 1144\ matchstrlist(['foobar'], '\zs')) 1145#" string with tabs 1146call assert_equal([{'idx': 0, 'byteidx': 1, 'text': 'foo'}], 1147\ matchstrlist(["\tfoobar"], 'foo')) 1148#" string with multibyte characters 1149call assert_equal([{'idx': 0, 'byteidx': 2, 'text': '😊😊'}], 1150\ matchstrlist(["\t\t😊😊"], '\k\+')) 1151 1152#" null string 1153call assert_equal([], matchstrlist(v:_null_list, 'abc')) 1154call assert_equal([], matchstrlist([v:_null_string], 'abc')) 1155call assert_equal([{'idx': 0, 'byteidx': 0, 'text': ''}], 1156\ matchstrlist(['abc'], v:_null_string)) 1157 1158#" sub matches 1159call assert_equal([{'idx': 0, 'byteidx': 0, 'text': 'acd', 'submatches': ['a', '', 'c', 'd', '', '', '', '', '']}], matchstrlist(['acd'], '\(a\)\?\(b\)\?\(c\)\?\(.*\)', {'submatches': v:true})) 1160 1161#" null dict argument 1162call assert_equal([{'idx': 0, 'byteidx': 0, 'text': 'vim'}], 1163\ matchstrlist(['vim'], '\w\+', v:_null_dict)) 1164 1165#" Error cases 1166call assert_fails("echo matchstrlist('abc', 'a')", 'E1211: List required for argument 1') 1167call assert_fails("echo matchstrlist(['abc'], {})", 'E1174: String required for argument 2') 1168call assert_fails("echo matchstrlist(['abc'], '.', [])", 'E1206: Dictionary required for argument 3') 1169call assert_fails("echo matchstrlist(['abc'], 'a', {'submatches': []})", 'E475: Invalid value for argument submatches') 1170call assert_fails("echo matchstrlist(['abc'], '\\@=')", 'E866: (NFA regexp) Misplaced @') 1171END 1172call CheckLegacyAndVim9Success ( lines ) 1173 1174let lines =<< trim END 1175vim9script 1176# non string items 1177matchstrlist([0z10, {'a': 'x'}], 'x') 1178END 1179call CheckSourceSuccess ( lines ) 1180 1181let lines =<< trim END 1182vim9script 1183def Foo() 1184# non string items 1185assert_equal([], matchstrlist([0z10, {'a': 'x'}], 'x')) 1186enddef 1187Foo() 1188END 1189call CheckSourceFailure ( lines , 'E1013: Argument 1: type mismatch, expected list<string> but got list<any>' , 2 ) 1190endfunc 1191 1192" Test for matchbufline() 1193func Test_matchbufline () 1194let lines =<< trim END 1195#" Basic match 1196new 1197call setline(1, ['about', 'above', 'below']) 1198VAR bnr = bufnr() 1199wincmd w 1200call assert_equal([{'lnum': 1, 'byteidx': 1, 'text': 'bout'}, 1201\ {'lnum': 2, 'byteidx': 1, 'text': 'bove'}], 1202\ matchbufline(bnr, 'bo.*', 1, '$')) 1203#" multiple matches in a line 1204call setbufline(bnr, 1, ['about about', 'above above', 'below']) 1205call assert_equal([{'lnum': 1, 'byteidx': 1, 'text': 'bout'}, 1206\ {'lnum': 1, 'byteidx': 7, 'text': 'bout'}, 1207\ {'lnum': 2, 'byteidx': 1, 'text': 'bove'}, 1208\ {'lnum': 2, 'byteidx': 7, 'text': 'bove'}], 1209\ matchbufline(bnr, 'bo\k\+', 1, '$')) 1210#" no match 1211call assert_equal([], matchbufline(bnr, 'xy.*', 1, '$')) 1212#" match on a particular line 1213call assert_equal([{'lnum': 2, 'byteidx': 7, 'text': 'bove'}], 1214\ matchbufline(bnr, 'bo\k\+$', 2, 2)) 1215#" match on a particular line 1216call assert_equal([], matchbufline(bnr, 'bo.*', 3, 3)) 1217#" empty string 1218call deletebufline(bnr, 1, '$') 1219call assert_equal([], matchbufline(bnr, '.', 1, '$')) 1220#" empty pattern 1221call setbufline(bnr, 1, 'abc') 1222call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': ''}], 1223\ matchbufline(bnr, '', 1, '$')) 1224#" method call 1225call setbufline(bnr, 1, 'editor') 1226call assert_equal([{'lnum': 1, 'byteidx': 2, 'text': 'it'}], 1227\ bnr->matchbufline('ed\zsit\zeor', 1, 1)) 1228#" single character matches 1229call assert_equal([{'lnum': 1, 'byteidx': 5, 'text': 'r'}], 1230\ matchbufline(bnr, 'r', 1, '$')) 1231call setbufline(bnr, 1, 'a') 1232call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': 'a'}], 1233\ matchbufline(bnr, 'a', 1, '$')) 1234#" zero-width match 1235call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': ''}], 1236\ matchbufline(bnr, '\zs', 1, '$')) 1237#" string with tabs 1238call setbufline(bnr, 1, "\tfoobar") 1239call assert_equal([{'lnum': 1, 'byteidx': 1, 'text': 'foo'}], 1240\ matchbufline(bnr, 'foo', 1, '$')) 1241#" string with multibyte characters 1242call setbufline(bnr, 1, "\t\t😊😊") 1243call assert_equal([{'lnum': 1, 'byteidx': 2, 'text': '😊😊'}], 1244\ matchbufline(bnr, '\k\+', 1, '$')) 1245#" empty buffer 1246call deletebufline(bnr, 1, '$') 1247call assert_equal([], matchbufline(bnr, 'abc', 1, '$')) 1248 1249#" Non existing buffer 1250call setbufline(bnr, 1, 'abc') 1251call assert_fails("echo matchbufline(5000, 'abc', 1, 1)", 'E158: Invalid buffer name: 5000') 1252#" null string 1253call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': ''}], 1254\ matchbufline(bnr, v:_null_string, 1, 1)) 1255#" invalid starting line number 1256call assert_equal([], matchbufline(bnr, 'abc', 100, 100)) 1257#" ending line number greater than the last line 1258call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': 'abc'}], 1259\ matchbufline(bnr, 'abc', 1, 100)) 1260#" ending line number greater than the starting line number 1261call setbufline(bnr, 1, ['one', 'two']) 1262call assert_fails($"echo matchbufline({bnr}, 'abc', 2, 1)", 'E475: Invalid value for argument end_lnum') 1263 1264#" sub matches 1265call deletebufline(bnr, 1, '$') 1266call setbufline(bnr, 1, 'acd') 1267call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': 'acd', 'submatches': ['a', '', 'c', 'd', '', '', '', '', '']}], 1268\ matchbufline(bnr, '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 1, '$', {'submatches': v:true})) 1269 1270#" null dict argument 1271call assert_equal([{'lnum': 1, 'byteidx': 0, 'text': 'acd'}], 1272\ matchbufline(bnr, '\w\+', '$', '$', v:_null_dict)) 1273 1274#" Error cases 1275call assert_fails("echo matchbufline([1], 'abc', 1, 1)", 'E1220: String or Number required for argument 1') 1276call assert_fails("echo matchbufline(1, {}, 1, 1)", 'E1174: String required for argument 2') 1277call assert_fails("echo matchbufline(1, 'abc', {}, 1)", 'E1220: String or Number required for argument 3') 1278call assert_fails("echo matchbufline(1, 'abc', 1, {})", 'E1220: String or Number required for argument 4') 1279call assert_fails($"echo matchbufline({bnr}, 'abc', -1, '$')", 'E475: Invalid value for argument lnum') 1280call assert_fails($"echo matchbufline({bnr}, 'abc', 1, -1)", 'E475: Invalid value for argument end_lnum') 1281call assert_fails($"echo matchbufline({bnr}, '\\@=', 1, 1)", 'E866: (NFA regexp) Misplaced @') 1282call assert_fails($"echo matchbufline({bnr}, 'abc', 1, 1, {{'submatches': []}})", 'E475: Invalid value for argument submatches') 1283:%bdelete! 1284call assert_fails($"echo matchbufline({bnr}, 'abc', 1, '$'))", 'E681: Buffer is not loaded') 1285END 1286call CheckLegacyAndVim9Success ( lines ) 1287 1288call assert_fails ( $ "echo matchbufline('', 'abc', 'abc',1)", 'E475: Invalid value for argument lnum') 1289call assert_fails ( $ "echo matchbufline('', 'abc',1, 'abc')", 'E475: Invalid value for argument end_lnum') 1290 1291let lines =<< trim END 1292vim9script 1293def Foo() 1294echo matchbufline('', 'abc', 'abc', 1) 1295enddef 1296Foo() 1297END 1298call CheckSourceFailure ( lines , 'E1030: Using a String as a Number: "abc"' , 1 ) 1299 1300let lines =<< trim END 1301vim9script 1302def Foo() 1303echo matchbufline('', 'abc', 1, 'abc') 1304enddef 1305Foo() 1306END 1307call CheckSourceFailure ( lines , 'E1030: Using a String as a Number: "abc"' , 1 ) 1308endfunc 1309 1310func Test_nextnonblank_prevnonblank () 1311new 1312insert 1313This 1314 1315 1316is 1317 1318a 1319Test 1320. 1321call assert_equal ( 0 , nextnonblank ( -1 )) 1322call assert_equal ( 0 , nextnonblank ( 0 )) 1323call assert_equal ( 1 , nextnonblank ( 1 )) 1324call assert_equal ( 4 , 2 ->nextnonblank ()) 1325call assert_equal ( 4 , nextnonblank ( 3 )) 1326call assert_equal ( 4 , nextnonblank ( 4 )) 1327call assert_equal ( 6 , nextnonblank ( 5 )) 1328call assert_equal ( 6 , nextnonblank ( 6 )) 1329call assert_equal ( 7 , nextnonblank ( 7 )) 1330call assert_equal ( 0 , 8 ->nextnonblank ()) 1331 1332call assert_equal ( 0 , prevnonblank ( -1 )) 1333call assert_equal ( 0 , prevnonblank ( 0 )) 1334call assert_equal ( 1 , 1 ->prevnonblank ()) 1335call assert_equal ( 1 , prevnonblank ( 2 )) 1336call assert_equal ( 1 , prevnonblank ( 3 )) 1337call assert_equal ( 4 , prevnonblank ( 4 )) 1338call assert_equal ( 4 , 5 ->prevnonblank ()) 1339call assert_equal ( 6 , prevnonblank ( 6 )) 1340call assert_equal ( 7 , prevnonblank ( 7 )) 1341call assert_equal ( 0 , prevnonblank ( 8 )) 1342bw ! 1343endfunc 1344 1345func Test_byte2line_line2byte () 1346new 1347set endofline 1348call setline ( 1 , [ 'a' , 'bc' , 'd' ]) 1349 1350set fileformat = unix 1351call assert_equal ([ -1 , -1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , -1 ], 1352 \map ( range ( -1 , 8 ), 'byte2line(v:val)' )) 1353call assert_equal ([ -1 , -1 , 1 , 3 , 6 , 8 , -1 ], 1354 \map ( range ( -1 , 5 ), 'line2byte(v:val)' )) 1355 1356set fileformat = mac 1357call assert_equal ([ -1 , -1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , -1 ], 1358 \map ( range ( -1 , 8 ), 'v:val->byte2line()' )) 1359call assert_equal ([ -1 , -1 , 1 , 3 , 6 , 8 , -1 ], 1360 \map ( range ( -1 , 5 ), 'v:val->line2byte()' )) 1361 1362set fileformat = dos 1363call assert_equal ([ -1 , -1 , 1 , 1 , 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , -1 ], 1364 \map ( range ( -1 , 11 ), 'byte2line(v:val)' )) 1365call assert_equal ([ -1 , -1 , 1 , 4 , 8 , 11 , -1 ], 1366 \map ( range ( -1 , 5 ), 'line2byte(v:val)' )) 1367 1368bw ! 1369set noendofline nofixendofline 1370normal a- 1371for ff in [ "unix" , "mac" , "dos" ] 1372let & fileformat = ff 1373call assert_equal ( 1 , line2byte ( 1 )) 1374call assert_equal ( 2 , line2byte ( 2 )) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte). 1375endfor 1376 1377set endofline & fixendofline & fileformat & 1378bw ! 1379endfunc 1380 1381" Test for byteidx() using a character index 1382func Test_byteidx () 1383let a = '.é.' " one char of two bytes 1384call assert_equal ( 0 , byteidx ( a , 0 )) 1385call assert_equal ( 1 , byteidx ( a , 1 )) 1386call assert_equal ( 3 , byteidx ( a , 2 )) 1387call assert_equal ( 4 , byteidx ( a , 3 )) 1388call assert_equal ( -1 , byteidx ( a , 4 )) 1389 1390let b = '.é.' " normal e with composing char 1391call assert_equal ( 0 , b ->byteidx ( 0 )) 1392call assert_equal ( 1 , b ->byteidx ( 1 )) 1393call assert_equal ( 4 , b ->byteidx ( 2 )) 1394call assert_equal ( 5 , b ->byteidx ( 3 )) 1395call assert_equal ( -1 , b ->byteidx ( 4 )) 1396 1397" string with multiple composing characters 1398let str = '-ą́-ą́' 1399call assert_equal ( 0 , byteidx ( str , 0 )) 1400call assert_equal ( 1 , byteidx ( str , 1 )) 1401call assert_equal ( 6 , byteidx ( str , 2 )) 1402call assert_equal ( 7 , byteidx ( str , 3 )) 1403call assert_equal ( 12 , byteidx ( str , 4 )) 1404call assert_equal ( -1 , byteidx ( str , 5 )) 1405 1406" empty string 1407call assert_equal ( 0 , byteidx ( '' , 0 )) 1408call assert_equal ( -1 , byteidx ( '' , 1 )) 1409 1410" error cases 1411call assert_fails ( "call byteidx([], 0)" , 'E730:' ) 1412call assert_fails ( "call byteidx('abc', [])" , 'E745:' ) 1413call assert_fails ( "call byteidx('abc', 0, {})" , [ 'E728:' , 'E728:' ]) 1414call assert_fails ( "call byteidx('abc', 0, -1)" , [ 'E1023:' , 'E1023:' ]) 1415endfunc 1416 1417" Test for byteidxcomp() using a character index 1418func Test_byteidxcomp () 1419let a = '.é.' " one char of two bytes 1420call assert_equal ( 0 , byteidxcomp ( a , 0 )) 1421call assert_equal ( 1 , byteidxcomp ( a , 1 )) 1422call assert_equal ( 3 , byteidxcomp ( a , 2 )) 1423call assert_equal ( 4 , byteidxcomp ( a , 3 )) 1424call assert_equal ( -1 , byteidxcomp ( a , 4 )) 1425 1426let b = '.é.' " normal e with composing char 1427call assert_equal ( 0 , b ->byteidxcomp ( 0 )) 1428call assert_equal ( 1 , b ->byteidxcomp ( 1 )) 1429call assert_equal ( 2 , b ->byteidxcomp ( 2 )) 1430call assert_equal ( 4 , b ->byteidxcomp ( 3 )) 1431call assert_equal ( 5 , b ->byteidxcomp ( 4 )) 1432call assert_equal ( -1 , b ->byteidxcomp ( 5 )) 1433 1434" string with multiple composing characters 1435let str = '-ą́-ą́' 1436call assert_equal ( 0 , byteidxcomp ( str , 0 )) 1437call assert_equal ( 1 , byteidxcomp ( str , 1 )) 1438call assert_equal ( 2 , byteidxcomp ( str , 2 )) 1439call assert_equal ( 4 , byteidxcomp ( str , 3 )) 1440call assert_equal ( 6 , byteidxcomp ( str , 4 )) 1441call assert_equal ( 7 , byteidxcomp ( str , 5 )) 1442call assert_equal ( 8 , byteidxcomp ( str , 6 )) 1443call assert_equal ( 10 , byteidxcomp ( str , 7 )) 1444call assert_equal ( 12 , byteidxcomp ( str , 8 )) 1445call assert_equal ( -1 , byteidxcomp ( str , 9 )) 1446 1447" empty string 1448call assert_equal ( 0 , byteidxcomp ( '' , 0 )) 1449call assert_equal ( -1 , byteidxcomp ( '' , 1 )) 1450 1451" error cases 1452call assert_fails ( "call byteidxcomp([], 0)" , 'E730:' ) 1453call assert_fails ( "call byteidxcomp('abc', [])" , 'E745:' ) 1454call assert_fails ( "call byteidxcomp('abc', 0, {})" , [ 'E728:' , 'E728:' ]) 1455call assert_fails ( "call byteidxcomp('abc', 0, -1)" , [ 'E1023:' , 'E1023:' ]) 1456endfunc 1457 1458" Test for byteidx() using a UTF-16 index 1459func Test_byteidx_from_utf16_index () 1460" string with single byte characters 1461let str = "abc" 1462for i in range ( 3 ) 1463call assert_equal ( i , byteidx ( str , i , v: true )) 1464endfor 1465call assert_equal ( 3 , byteidx ( str , 3 , v: true )) 1466call assert_equal ( -1 , byteidx ( str , 4 , v: true )) 1467 1468" string with two byte characters 1469let str = "a©©b" 1470call assert_equal ( 0 , byteidx ( str , 0 , v: true )) 1471call assert_equal ( 1 , byteidx ( str , 1 , v: true )) 1472call assert_equal ( 3 , byteidx ( str , 2 , v: true )) 1473call assert_equal ( 5 , byteidx ( str , 3 , v: true )) 1474call assert_equal ( 6 , byteidx ( str , 4 , v: true )) 1475call assert_equal ( -1 , byteidx ( str , 5 , v: true )) 1476 1477" string with two byte characters 1478let str = "a😊😊b" 1479call assert_equal ( 0 , byteidx ( str , 0 , v: true )) 1480call assert_equal ( 1 , byteidx ( str , 1 , v: true )) 1481call assert_equal ( 1 , byteidx ( str , 2 , v: true )) 1482call assert_equal ( 5 , byteidx ( str , 3 , v: true )) 1483call assert_equal ( 5 , byteidx ( str , 4 , v: true )) 1484call assert_equal ( 9 , byteidx ( str , 5 , v: true )) 1485call assert_equal ( 10 , byteidx ( str , 6 , v: true )) 1486call assert_equal ( -1 , byteidx ( str , 7 , v: true )) 1487 1488" string with composing characters 1489let str = '-á-b́' 1490call assert_equal ( 0 , byteidx ( str , 0 , v: true )) 1491call assert_equal ( 1 , byteidx ( str , 1 , v: true )) 1492call assert_equal ( 4 , byteidx ( str , 2 , v: true )) 1493call assert_equal ( 5 , byteidx ( str , 3 , v: true )) 1494call assert_equal ( 8 , byteidx ( str , 4 , v: true )) 1495call assert_equal ( -1 , byteidx ( str , 5 , v: true )) 1496 1497" string with multiple composing characters 1498let str = '-ą́-ą́' 1499call assert_equal ( 0 , byteidx ( str , 0 , v: true )) 1500call assert_equal ( 1 , byteidx ( str , 1 , v: true )) 1501call assert_equal ( 6 , byteidx ( str , 2 , v: true )) 1502call assert_equal ( 7 , byteidx ( str , 3 , v: true )) 1503call assert_equal ( 12 , byteidx ( str , 4 , v: true )) 1504call assert_equal ( -1 , byteidx ( str , 5 , v: true )) 1505 1506" empty string 1507call assert_equal ( 0 , byteidx ( '' , 0 , v: true )) 1508call assert_equal ( -1 , byteidx ( '' , 1 , v: true )) 1509 1510" error cases 1511call assert_fails ( 'call byteidx(str, 0, [])' , 'E745:' ) 1512endfunc 1513 1514" Test for byteidxcomp() using a UTF-16 index 1515func Test_byteidxcomp_from_utf16_index () 1516" string with single byte characters 1517let str = "abc" 1518for i in range ( 3 ) 1519call assert_equal ( i , byteidxcomp ( str , i , v: true )) 1520endfor 1521call assert_equal ( 3 , byteidxcomp ( str , 3 , v: true )) 1522call assert_equal ( -1 , byteidxcomp ( str , 4 , v: true )) 1523 1524" string with two byte characters 1525let str = "a©©b" 1526call assert_equal ( 0 , byteidxcomp ( str , 0 , v: true )) 1527call assert_equal ( 1 , byteidxcomp ( str , 1 , v: true )) 1528call assert_equal ( 3 , byteidxcomp ( str , 2 , v: true )) 1529call assert_equal ( 5 , byteidxcomp ( str , 3 , v: true )) 1530call assert_equal ( 6 , byteidxcomp ( str , 4 , v: true )) 1531call assert_equal ( -1 , byteidxcomp ( str , 5 , v: true )) 1532 1533" string with two byte characters 1534let str = "a😊😊b" 1535call assert_equal ( 0 , byteidxcomp ( str , 0 , v: true )) 1536call assert_equal ( 1 , byteidxcomp ( str , 1 , v: true )) 1537call assert_equal ( 1 , byteidxcomp ( str , 2 , v: true )) 1538call assert_equal ( 5 , byteidxcomp ( str , 3 , v: true )) 1539call assert_equal ( 5 , byteidxcomp ( str , 4 , v: true )) 1540call assert_equal ( 9 , byteidxcomp ( str , 5 , v: true )) 1541call assert_equal ( 10 , byteidxcomp ( str , 6 , v: true )) 1542call assert_equal ( -1 , byteidxcomp ( str , 7 , v: true )) 1543 1544" string with composing characters 1545let str = '-á-b́' 1546call assert_equal ( 0 , byteidxcomp ( str , 0 , v: true )) 1547call assert_equal ( 1 , byteidxcomp ( str , 1 , v: true )) 1548call assert_equal ( 2 , byteidxcomp ( str , 2 , v: true )) 1549call assert_equal ( 4 , byteidxcomp ( str , 3 , v: true )) 1550call assert_equal ( 5 , byteidxcomp ( str , 4 , v: true )) 1551call assert_equal ( 6 , byteidxcomp ( str , 5 , v: true )) 1552call assert_equal ( 8 , byteidxcomp ( str , 6 , v: true )) 1553call assert_equal ( -1 , byteidxcomp ( str , 7 , v: true )) 1554call assert_fails ( 'call byteidxcomp(str, 0, [])' , 'E745:' ) 1555 1556" string with multiple composing characters 1557let str = '-ą́-ą́' 1558call assert_equal ( 0 , byteidxcomp ( str , 0 , v: true )) 1559call assert_equal ( 1 , byteidxcomp ( str , 1 , v: true )) 1560call assert_equal ( 2 , byteidxcomp ( str , 2 , v: true )) 1561call assert_equal ( 4 , byteidxcomp ( str , 3 , v: true )) 1562call assert_equal ( 6 , byteidxcomp ( str , 4 , v: true )) 1563call assert_equal ( 7 , byteidxcomp ( str , 5 , v: true )) 1564call assert_equal ( 8 , byteidxcomp ( str , 6 , v: true )) 1565call assert_equal ( 10 , byteidxcomp ( str , 7 , v: true )) 1566call assert_equal ( 12 , byteidxcomp ( str , 8 , v: true )) 1567call assert_equal ( -1 , byteidxcomp ( str , 9 , v: true )) 1568 1569" empty string 1570call assert_equal ( 0 , byteidxcomp ( '' , 0 , v: true )) 1571call assert_equal ( -1 , byteidxcomp ( '' , 1 , v: true )) 1572 1573" error cases 1574call assert_fails ( 'call byteidxcomp(str, 0, [])' , 'E745:' ) 1575endfunc 1576 1577" Test for charidx() using a byte index 1578func Test_charidx () 1579let a = 'xáb́y' 1580call assert_equal ( 0 , charidx ( a , 0 )) 1581call assert_equal ( 1 , charidx ( a , 3 )) 1582call assert_equal ( 2 , charidx ( a , 4 )) 1583call assert_equal ( 3 , charidx ( a , 7 )) 1584call assert_equal ( 4 , charidx ( a , 8 )) 1585call assert_equal ( -1 , charidx ( a , 9 )) 1586call assert_equal ( -1 , charidx ( a , -1 )) 1587 1588" count composing characters 1589call assert_equal ( 0 , a ->charidx ( 0 , 1 )) 1590call assert_equal ( 2 , a ->charidx ( 2 , 1 )) 1591call assert_equal ( 3 , a ->charidx ( 4 , 1 )) 1592call assert_equal ( 5 , a ->charidx ( 7 , 1 )) 1593call assert_equal ( 6 , a ->charidx ( 8 , 1 )) 1594call assert_equal ( -1 , a ->charidx ( 9 , 1 )) 1595 1596" empty string 1597call assert_equal ( 0 , charidx ( '' , 0 )) 1598call assert_equal ( -1 , charidx ( '' , 1 )) 1599call assert_equal ( 0 , charidx ( '' , 0 , 1 )) 1600call assert_equal ( -1 , charidx ( '' , 1 , 1 )) 1601 1602" error cases 1603call assert_equal ( 0 , charidx ( v: _null_string , 0 )) 1604call assert_equal ( -1 , charidx ( v: _null_string , 1 )) 1605call assert_fails ( 'let x = charidx([], 1)' , 'E1174:' ) 1606call assert_fails ( 'let x = charidx("abc", [])' , 'E1210:' ) 1607call assert_fails ( 'let x = charidx("abc", 1, [])' , 'E1212:' ) 1608call assert_fails ( 'let x = charidx("abc", 1, -1)' , 'E1212:' ) 1609call assert_fails ( 'let x = charidx("abc", 1, 2)' , 'E1212:' ) 1610endfunc 1611 1612" Test for charidx() using a UTF-16 index 1613func Test_charidx_from_utf16_index () 1614" string with single byte characters 1615let str = "abc" 1616for i in range ( 4 ) 1617call assert_equal ( i , charidx ( str , i , v: false , v: true )) 1618endfor 1619call assert_equal ( -1 , charidx ( str , 4 , v: false , v: true )) 1620 1621" string with two byte characters 1622let str = "a©©b" 1623call assert_equal ( 0 , charidx ( str , 0 , v: false , v: true )) 1624call assert_equal ( 1 , charidx ( str , 1 , v: false , v: true )) 1625call assert_equal ( 2 , charidx ( str , 2 , v: false , v: true )) 1626call assert_equal ( 3 , charidx ( str , 3 , v: false , v: true )) 1627call assert_equal ( 4 , charidx ( str , 4 , v: false , v: true )) 1628call assert_equal ( -1 , charidx ( str , 5 , v: false , v: true )) 1629 1630" string with four byte characters 1631let str = "a😊😊b" 1632call assert_equal ( 0 , charidx ( str , 0 , v: false , v: true )) 1633call assert_equal ( 1 , charidx ( str , 1 , v: false , v: true )) 1634call assert_equal ( 1 , charidx ( str , 2 , v: false , v: true )) 1635call assert_equal ( 2 , charidx ( str , 3 , v: false , v: true )) 1636call assert_equal ( 2 , charidx ( str , 4 , v: false , v: true )) 1637call assert_equal ( 3 , charidx ( str , 5 , v: false , v: true )) 1638call assert_equal ( 4 , charidx ( str , 6 , v: false , v: true )) 1639call assert_equal ( -1 , charidx ( str , 7 , v: false , v: true )) 1640 1641" string with composing characters 1642let str = '-á-b́' 1643for i in str ->strcharlen () ->range () 1644call assert_equal ( i , charidx ( str , i , v: false , v: true )) 1645endfor 1646call assert_equal ( 4 , charidx ( str , 4 , v: false , v: true )) 1647call assert_equal ( -1 , charidx ( str , 5 , v: false , v: true )) 1648for i in str ->strchars () ->range () 1649call assert_equal ( i , charidx ( str , i , v: true , v: true )) 1650endfor 1651call assert_equal ( 6 , charidx ( str , 6 , v: true , v: true )) 1652call assert_equal ( -1 , charidx ( str , 7 , v: true , v: true )) 1653 1654" string with multiple composing characters 1655let str = '-ą́-ą́' 1656for i in str ->strcharlen () ->range () 1657call assert_equal ( i , charidx ( str , i , v: false , v: true )) 1658endfor 1659call assert_equal ( 4 , charidx ( str , 4 , v: false , v: true )) 1660call assert_equal ( -1 , charidx ( str , 5 , v: false , v: true )) 1661for i in str ->strchars () ->range () 1662call assert_equal ( i , charidx ( str , i , v: true , v: true )) 1663endfor 1664call assert_equal ( 8 , charidx ( str , 8 , v: true , v: true )) 1665call assert_equal ( -1 , charidx ( str , 9 , v: true , v: true )) 1666 1667" empty string 1668call assert_equal ( 0 , charidx ( '' , 0 , v: false , v: true )) 1669call assert_equal ( -1 , charidx ( '' , 1 , v: false , v: true )) 1670call assert_equal ( 0 , charidx ( '' , 0 , v: true , v: true )) 1671call assert_equal ( -1 , charidx ( '' , 1 , v: true , v: true )) 1672 1673" error cases 1674call assert_equal ( 0 , charidx ( '' , 0 , v: false , v: true )) 1675call assert_equal ( -1 , charidx ( '' , 1 , v: false , v: true )) 1676call assert_equal ( 0 , charidx ( '' , 0 , v: true , v: true )) 1677call assert_equal ( -1 , charidx ( '' , 1 , v: true , v: true )) 1678call assert_equal ( 0 , charidx ( v: _null_string , 0 , v: false , v: true )) 1679call assert_equal ( -1 , charidx ( v: _null_string , 1 , v: false , v: true )) 1680call assert_fails ( 'let x = charidx("abc", 1, v:false, [])' , 'E1212:' ) 1681call assert_fails ( 'let x = charidx("abc", 1, v:true, [])' , 'E1212:' ) 1682endfunc 1683 1684" Test for utf16idx() using a byte index 1685func Test_utf16idx_from_byteidx () 1686" UTF-16 index of a string with single byte characters 1687let str = "abc" 1688for i in range ( 4 ) 1689call assert_equal ( i , utf16idx ( str , i )) 1690endfor 1691call assert_equal ( -1 , utf16idx ( str , 4 )) 1692 1693" UTF-16 index of a string with two byte characters 1694let str = 'a©©b' 1695call assert_equal ( 0 , str ->utf16idx ( 0 )) 1696call assert_equal ( 1 , str ->utf16idx ( 1 )) 1697call assert_equal ( 1 , str ->utf16idx ( 2 )) 1698call assert_equal ( 2 , str ->utf16idx ( 3 )) 1699call assert_equal ( 2 , str ->utf16idx ( 4 )) 1700call assert_equal ( 3 , str ->utf16idx ( 5 )) 1701call assert_equal ( 4 , str ->utf16idx ( 6 )) 1702call assert_equal ( -1 , str ->utf16idx ( 7 )) 1703 1704" UTF-16 index of a string with four byte characters 1705let str = 'a😊😊b' 1706call assert_equal ( 0 , utf16idx ( str , 0 )) 1707call assert_equal ( 1 , utf16idx ( str , 1 )) 1708call assert_equal ( 1 , utf16idx ( str , 2 )) 1709call assert_equal ( 1 , utf16idx ( str , 3 )) 1710call assert_equal ( 1 , utf16idx ( str , 4 )) 1711call assert_equal ( 3 , utf16idx ( str , 5 )) 1712call assert_equal ( 3 , utf16idx ( str , 6 )) 1713call assert_equal ( 3 , utf16idx ( str , 7 )) 1714call assert_equal ( 3 , utf16idx ( str , 8 )) 1715call assert_equal ( 5 , utf16idx ( str , 9 )) 1716call assert_equal ( 6 , utf16idx ( str , 10 )) 1717call assert_equal ( -1 , utf16idx ( str , 11 )) 1718 1719" UTF-16 index of a string with composing characters 1720let str = '-á-b́' 1721call assert_equal ( 0 , utf16idx ( str , 0 )) 1722call assert_equal ( 1 , utf16idx ( str , 1 )) 1723call assert_equal ( 1 , utf16idx ( str , 2 )) 1724call assert_equal ( 1 , utf16idx ( str , 3 )) 1725call assert_equal ( 2 , utf16idx ( str , 4 )) 1726call assert_equal ( 3 , utf16idx ( str , 5 )) 1727call assert_equal ( 3 , utf16idx ( str , 6 )) 1728call assert_equal ( 3 , utf16idx ( str , 7 )) 1729call assert_equal ( 4 , utf16idx ( str , 8 )) 1730call assert_equal ( -1 , utf16idx ( str , 9 )) 1731call assert_equal ( 0 , utf16idx ( str , 0 , v: true )) 1732call assert_equal ( 1 , utf16idx ( str , 1 , v: true )) 1733call assert_equal ( 2 , utf16idx ( str , 2 , v: true )) 1734call assert_equal ( 2 , utf16idx ( str , 3 , v: true )) 1735call assert_equal ( 3 , utf16idx ( str , 4 , v: true )) 1736call assert_equal ( 4 , utf16idx ( str , 5 , v: true )) 1737call assert_equal ( 5 , utf16idx ( str , 6 , v: true )) 1738call assert_equal ( 5 , utf16idx ( str , 7 , v: true )) 1739call assert_equal ( 6 , utf16idx ( str , 8 , v: true )) 1740call assert_equal ( -1 , utf16idx ( str , 9 , v: true )) 1741 1742" string with multiple composing characters 1743let str = '-ą́-ą́' 1744call assert_equal ( 0 , utf16idx ( str , 0 )) 1745call assert_equal ( 1 , utf16idx ( str , 1 )) 1746call assert_equal ( 1 , utf16idx ( str , 2 )) 1747call assert_equal ( 1 , utf16idx ( str , 3 )) 1748call assert_equal ( 1 , utf16idx ( str , 4 )) 1749call assert_equal ( 1 , utf16idx ( str , 5 )) 1750call assert_equal ( 2 , utf16idx ( str , 6 )) 1751call assert_equal ( 3 , utf16idx ( str , 7 )) 1752call assert_equal ( 3 , utf16idx ( str , 8 )) 1753call assert_equal ( 3 , utf16idx ( str , 9 )) 1754call assert_equal ( 3 , utf16idx ( str , 10 )) 1755call assert_equal ( 3 , utf16idx ( str , 11 )) 1756call assert_equal ( 4 , utf16idx ( str , 12 )) 1757call assert_equal ( -1 , utf16idx ( str , 13 )) 1758call assert_equal ( 0 , utf16idx ( str , 0 , v: true )) 1759call assert_equal ( 1 , utf16idx ( str , 1 , v: true )) 1760call assert_equal ( 2 , utf16idx ( str , 2 , v: true )) 1761call assert_equal ( 2 , utf16idx ( str , 3 , v: true )) 1762call assert_equal ( 3 , utf16idx ( str , 4 , v: true )) 1763call assert_equal ( 3 , utf16idx ( str , 5 , v: true )) 1764call assert_equal ( 4 , utf16idx ( str , 6 , v: true )) 1765call assert_equal ( 5 , utf16idx ( str , 7 , v: true )) 1766call assert_equal ( 6 , utf16idx ( str , 8 , v: true )) 1767call assert_equal ( 6 , utf16idx ( str , 9 , v: true )) 1768call assert_equal ( 7 , utf16idx ( str , 10 , v: true )) 1769call assert_equal ( 7 , utf16idx ( str , 11 , v: true )) 1770call assert_equal ( 8 , utf16idx ( str , 12 , v: true )) 1771call assert_equal ( -1 , utf16idx ( str , 13 , v: true )) 1772 1773" empty string 1774call assert_equal ( 0 , utf16idx ( '' , 0 )) 1775call assert_equal ( -1 , utf16idx ( '' , 1 )) 1776call assert_equal ( 0 , utf16idx ( '' , 0 , v: true )) 1777call assert_equal ( -1 , utf16idx ( '' , 1 , v: true )) 1778 1779" error cases 1780call assert_equal ( 0 , utf16idx ( "" , 0 )) 1781call assert_equal ( -1 , utf16idx ( "" , 1 )) 1782call assert_equal ( -1 , utf16idx ( "abc" , -1 )) 1783call assert_equal ( 0 , utf16idx ( v: _null_string , 0 )) 1784call assert_equal ( -1 , utf16idx ( v: _null_string , 1 )) 1785call assert_fails ( 'let l = utf16idx([], 0)' , 'E1174:' ) 1786call assert_fails ( 'let l = utf16idx("ab", [])' , 'E1210:' ) 1787call assert_fails ( 'let l = utf16idx("ab", 0, [])' , 'E1212:' ) 1788endfunc 1789 1790" Test for utf16idx() using a character index 1791func Test_utf16idx_from_charidx () 1792let str = "abc" 1793for i in str ->strcharlen () ->range () 1794call assert_equal ( i , utf16idx ( str , i , v: false , v: true )) 1795endfor 1796call assert_equal ( 3 , utf16idx ( str , 3 , v: false , v: true )) 1797call assert_equal ( -1 , utf16idx ( str , 4 , v: false , v: true )) 1798 1799" UTF-16 index of a string with two byte characters 1800let str = "a©©b" 1801for i in str ->strcharlen () ->range () 1802call assert_equal ( i , utf16idx ( str , i , v: false , v: true )) 1803endfor 1804call assert_equal ( 4 , utf16idx ( str , 4 , v: false , v: true )) 1805call assert_equal ( -1 , utf16idx ( str , 5 , v: false , v: true )) 1806 1807" UTF-16 index of a string with four byte characters 1808let str = "a😊😊b" 1809call assert_equal ( 0 , utf16idx ( str , 0 , v: false , v: true )) 1810call assert_equal ( 1 , utf16idx ( str , 1 , v: false , v: true )) 1811call assert_equal ( 3 , utf16idx ( str , 2 , v: false , v: true )) 1812call assert_equal ( 5 , utf16idx ( str , 3 , v: false , v: true )) 1813call assert_equal ( 6 , utf16idx ( str , 4 , v: false , v: true )) 1814call assert_equal ( -1 , utf16idx ( str , 5 , v: false , v: true )) 1815 1816" UTF-16 index of a string with composing characters 1817let str = '-á-b́' 1818for i in str ->strcharlen () ->range () 1819call assert_equal ( i , utf16idx ( str , i , v: false , v: true )) 1820endfor 1821call assert_equal ( 4 , utf16idx ( str , 4 , v: false , v: true )) 1822call assert_equal ( -1 , utf16idx ( str , 5 , v: false , v: true )) 1823for i in str ->strchars () ->range () 1824call assert_equal ( i , utf16idx ( str , i , v: true , v: true )) 1825endfor 1826call assert_equal ( 6 , utf16idx ( str , 6 , v: true , v: true )) 1827call assert_equal ( -1 , utf16idx ( str , 7 , v: true , v: true )) 1828 1829" string with multiple composing characters 1830let str = '-ą́-ą́' 1831for i in str ->strcharlen () ->range () 1832call assert_equal ( i , utf16idx ( str , i , v: false , v: true )) 1833endfor 1834call assert_equal ( 4 , utf16idx ( str , 4 , v: false , v: true )) 1835call assert_equal ( -1 , utf16idx ( str , 5 , v: false , v: true )) 1836for i in str ->strchars () ->range () 1837call assert_equal ( i , utf16idx ( str , i , v: true , v: true )) 1838endfor 1839call assert_equal ( 8 , utf16idx ( str , 8 , v: true , v: true )) 1840call assert_equal ( -1 , utf16idx ( str , 9 , v: true , v: true )) 1841 1842" empty string 1843call assert_equal ( 0 , utf16idx ( '' , 0 , v: false , v: true )) 1844call assert_equal ( -1 , utf16idx ( '' , 1 , v: false , v: true )) 1845call assert_equal ( 0 , utf16idx ( '' , 0 , v: true , v: true )) 1846call assert_equal ( -1 , utf16idx ( '' , 1 , v: true , v: true )) 1847 1848" error cases 1849call assert_equal ( 0 , utf16idx ( v: _null_string , 0 , v: true , v: true )) 1850call assert_equal ( -1 , utf16idx ( v: _null_string , 1 , v: true , v: true )) 1851call assert_fails ( 'let l = utf16idx("ab", 0, v:false, [])' , 'E1212:' ) 1852endfunc 1853 1854" Test for strutf16len() 1855func Test_strutf16len () 1856call assert_equal ( 3 , strutf16len ( 'abc' )) 1857call assert_equal ( 3 , 'abc' ->strutf16len ( v: true )) 1858call assert_equal ( 4 , strutf16len ( 'a©©b' )) 1859call assert_equal ( 4 , strutf16len ( 'a©©b' , v: true )) 1860call assert_equal ( 6 , strutf16len ( 'a😊😊b' )) 1861call assert_equal ( 6 , strutf16len ( 'a😊😊b' , v: true )) 1862call assert_equal ( 4 , strutf16len ( '-á-b́' )) 1863call assert_equal ( 6 , strutf16len ( '-á-b́' , v: true )) 1864call assert_equal ( 4 , strutf16len ( '-ą́-ą́' )) 1865call assert_equal ( 8 , strutf16len ( '-ą́-ą́' , v: true )) 1866call assert_equal ( 0 , strutf16len ( '' )) 1867 1868" error cases 1869call assert_fails ( 'let l = strutf16len([])' , 'E1174:' ) 1870call assert_fails ( 'let l = strutf16len("a", [])' , 'E1212:' ) 1871call assert_equal ( 0 , strutf16len ( v: _null_string )) 1872endfunc 1873 1874func Test_count () 1875let l = [ 'a' , 'a' , 'A' , 'b' ] 1876call assert_equal ( 2 , count ( l , 'a' )) 1877call assert_equal ( 1 , count ( l , 'A' )) 1878call assert_equal ( 1 , count ( l , 'b' )) 1879call assert_equal ( 0 , count ( l , 'B' )) 1880 1881call assert_equal ( 2 , count ( l , 'a' , 0 )) 1882call assert_equal ( 1 , count ( l , 'A' , 0 )) 1883call assert_equal ( 1 , count ( l , 'b' , 0 )) 1884call assert_equal ( 0 , count ( l , 'B' , 0 )) 1885 1886call assert_equal ( 3 , count ( l , 'a' , 1 )) 1887call assert_equal ( 3 , count ( l , 'A' , 1 )) 1888call assert_equal ( 1 , count ( l , 'b' , 1 )) 1889call assert_equal ( 1 , count ( l , 'B' , 1 )) 1890call assert_equal ( 0 , count ( l , 'c' , 1 )) 1891 1892call assert_equal ( 1 , count ( l , 'a' , 0 , 1 )) 1893call assert_equal ( 2 , count ( l , 'a' , 1 , 1 )) 1894call assert_fails ( 'call count(l, "a", 0, 10)' , 'E684:' ) 1895call assert_fails ( 'call count(l, "a", [])' , 'E745:' ) 1896 1897let d = { 1 : 'a' , 2 : 'a' , 3 : 'A' , 4 : 'b' } 1898call assert_equal ( 2 , count ( d , 'a' )) 1899call assert_equal ( 1 , count ( d , 'A' )) 1900call assert_equal ( 1 , count ( d , 'b' )) 1901call assert_equal ( 0 , count ( d , 'B' )) 1902 1903call assert_equal ( 2 , count ( d , 'a' , 0 )) 1904call assert_equal ( 1 , count ( d , 'A' , 0 )) 1905call assert_equal ( 1 , count ( d , 'b' , 0 )) 1906call assert_equal ( 0 , count ( d , 'B' , 0 )) 1907 1908call assert_equal ( 3 , count ( d , 'a' , 1 )) 1909call assert_equal ( 3 , count ( d , 'A' , 1 )) 1910call assert_equal ( 1 , count ( d , 'b' , 1 )) 1911call assert_equal ( 1 , count ( d , 'B' , 1 )) 1912call assert_equal ( 0 , count ( d , 'c' , 1 )) 1913 1914call assert_fails ( 'call count(d, "a", 0, 1)' , 'E474:' ) 1915 1916call assert_equal ( 0 , count ( "foo" , "bar" )) 1917call assert_equal ( 1 , count ( "foo" , "oo" )) 1918call assert_equal ( 2 , count ( "foo" , "o" )) 1919call assert_equal ( 0 , count ( "foo" , "O" )) 1920call assert_equal ( 2 , count ( "foo" , "O" , 1 )) 1921call assert_equal ( 2 , count ( "fooooo" , "oo" )) 1922call assert_equal ( 0 , count ( "foo" , "" )) 1923 1924call assert_fails ( 'call count(0, 0)' , 'E706:' ) 1925call assert_fails ( 'call count("", "", {})' , [ 'E728:' , 'E728:' ]) 1926endfunc 1927 1928func Test_changenr () 1929new Xchangenr 1930call assert_equal ( 0 , changenr ()) 1931norm ifoo 1932call assert_equal ( 1 , changenr ()) 1933set undolevels = 10 1934norm Sbar 1935call assert_equal ( 2 , changenr ()) 1936undo 1937call assert_equal ( 1 , changenr ()) 1938redo 1939call assert_equal ( 2 , changenr ()) 1940bw ! 1941set undolevels & 1942endfunc 1943 1944func Test_filewritable () 1945new Xfilewritable 1946write ! 1947call assert_equal ( 1 , filewritable ( 'Xfilewritable' )) 1948 1949call assert_notequal ( 0 , setfperm ( 'Xfilewritable' , 'r--r-----' )) 1950call assert_equal ( 0 , filewritable ( 'Xfilewritable' )) 1951 1952call assert_notequal ( 0 , setfperm ( 'Xfilewritable' , 'rw-r-----' )) 1953call assert_equal ( 1 , 'Xfilewritable' ->filewritable ()) 1954 1955call assert_equal ( 0 , filewritable ( 'doesnotexist' )) 1956 1957call mkdir ( 'Xdir' ) 1958call assert_equal ( 2 , filewritable ( 'Xdir' )) 1959call delete ( 'Xdir' , 'd' ) 1960 1961call delete ( 'Xfilewritable' ) 1962bw ! 1963endfunc 1964 1965func Test_Executable () 1966if has ( 'win32' ) 1967call assert_equal ( 1 , executable ( 'notepad' )) 1968call assert_equal ( 1 , 'notepad.exe' ->executable ()) 1969call assert_equal ( 0 , executable ( 'notepad.exe.exe' )) 1970call assert_equal ( 0 , executable ( 'shell32.dll' )) 1971call assert_equal ( 0 , executable ( 'win.ini' )) 1972elseif has ( 'unix' ) 1973call assert_equal ( 1 , 'cat' ->executable ()) 1974call assert_equal ( 0 , executable ( 'nodogshere' )) 1975 1976" get "cat" path and remove the leading / 1977let catcmd = exepath ( 'cat' )[ 1 :] 1978new 1979" check that the relative path works in / 1980lcd / 1981call assert_equal ( 1 , executable ( catcmd )) 1982let result = catcmd ->exepath () 1983" when using chroot looking for sbin/cat can return bin/cat, that is OK 1984if catcmd =~ '\<sbin\>' && result =~ '\<bin\>' 1985call assert_equal ( '/' .. substitute ( catcmd , '\<sbin\>' , 'bin' , '' ), result ) 1986else 1987" /bin/cat and /usr/bin/cat may be hard linked, we could get either 1988let result = substitute ( result , '/usr/bin/cat' , '/bin/cat' , '' ) 1989let catcmd = substitute ( catcmd , 'usr/bin/cat' , 'bin/cat' , '' ) 1990call assert_equal ( '/' .. catcmd , result ) 1991endif 1992bwipe 1993else 1994throw 'Skipped: does not work on this platform' 1995endif 1996endfunc 1997 1998func Test_executable_longname () 1999CheckMSWindows 2000 2001" Create a temporary .bat file with 205 characters in the name. 2002" Maximum length of a filename (including the path) on MS-Windows is 259 2003" characters. 2004" See https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation 2005let len = 259 - getcwd () ->len () - 6 2006if len > 200 2007let len = 200 2008endif 2009 2010let fname = 'X' . repeat ( 'あ' , len ) . '.bat' 2011call writefile ([], fname ) 2012call assert_equal ( 1 , executable ( fname )) 2013call delete ( fname ) 2014endfunc 2015 2016func Test_executable_single_character_dir () 2017call mkdir ( 'Xpath' , 'R' ) 2018call mkdir ( 'Xpath/a' ) 2019call mkdir ( 'Xpath/b' ) 2020call mkdir ( 'Xpath/c' ) 2021if has ( 'win32' ) 2022call writefile ([], 'Xpath/a/Xcmd1.bat' ) 2023call writefile ([], 'Xpath/b/Xcmd2.bat' ) 2024call writefile ([], 'Xpath/c/Xcmd3.bat' ) 2025let sep = ';' 2026else 2027call writefile ([], 'Xpath/a/Xcmd1' ) 2028call writefile ([], 'Xpath/b/Xcmd2' ) 2029call writefile ([], 'Xpath/c/Xcmd3' ) 2030call setfperm ( 'Xpath/a/Xcmd1' , 'rwxr-xr-x' ) 2031call setfperm ( 'Xpath/b/Xcmd2' , 'rwxr-xr-x' ) 2032call setfperm ( 'Xpath/c/Xcmd3' , 'rwxr-xr-x' ) 2033let sep = ':' 2034endif 2035 2036let save_path = $ PATH 2037" a: single character name without path seperator 2038" b: single character name with path seperator 2039" c: single character name without path seperator at last of PATH 2040let $ PATH = [ 2041 \fnamemodify ( './Xpath/a' , ':p:h' ), 2042 \fnamemodify ( './Xpath/b' , ':p' ), 2043 \fnamemodify ( './Xpath/c' , ':p:h' ) 2044 \] ->join ( sep ) 2045call assert_true ( executable ( 'Xcmd1' )) 2046call assert_true ( executable ( 'Xcmd2' )) 2047call assert_true ( executable ( 'Xcmd3' )) 2048let $ PATH = save_path 2049endfunc 2050 2051func Test_hostname () 2052let hostname_vim = hostname () 2053if has ( 'unix' ) 2054let hostname_system = systemlist ( 'uname -n' )[ 0 ] 2055call assert_equal ( hostname_vim , hostname_system ) 2056endif 2057endfunc 2058 2059func Test_getpid () 2060" getpid() always returns the same value within a vim instance. 2061call assert_equal ( getpid (), getpid ()) 2062if has ( 'unix' ) 2063call assert_equal ( systemlist ( 'echo $PPID' )[ 0 ], string ( getpid ())) 2064endif 2065endfunc 2066 2067func Test_hlexists () 2068call assert_equal ( 0 , hlexists ( 'does_not_exist' )) 2069" call assert_equal(0, 'Number'->hlexists()) 2070call assert_equal ( 0 , highlight_exists ( 'does_not_exist' )) 2071" call assert_equal(0, highlight_exists('Number')) 2072syntax on 2073call assert_equal ( 0 , hlexists ( 'does_not_exist' )) 2074" call assert_equal(1, hlexists('Number')) 2075call assert_equal ( 0 , highlight_exists ( 'does_not_exist' )) 2076" call assert_equal(1, highlight_exists('Number')) 2077syntax off 2078endfunc 2079 2080" Test for the col() function 2081func Test_col () 2082new 2083call setline ( 1 , 'abcdef' ) 2084norm gg4|mx6|mY2| 2085call assert_equal ( 2 , col ( '.' )) 2086call assert_equal ( 7 , col ( '$' )) 2087call assert_equal ( 2 , col ( 'v' )) 2088call assert_equal ( 4 , col ( "'x" )) 2089call assert_equal ( 6 , col ( "'Y" )) 2090call assert_equal ( 2 , [ 1 , 2 ] ->col ()) 2091call assert_equal ( 7 , col ([ 1 , '$' ])) 2092 2093call assert_equal ( 0 , col ( '' )) 2094call assert_equal ( 0 , col ( 'x' )) 2095call assert_equal ( 0 , col ([ 2 , '$' ])) 2096call assert_equal ( 0 , col ([ 1 , 100 ])) 2097call assert_equal ( 0 , col ([ 1 ])) 2098call assert_equal ( 0 , col ( v: _null_list )) 2099call assert_fails ( 'let c = col({})' , 'E1222:' ) 2100call assert_fails ( 'let c = col(".", [])' , 'E1210:' ) 2101 2102" test for getting the visual start column 2103func T () 2104let g: Vcol = col ( 'v' ) 2105return '' 2106endfunc 2107let g: Vcol = 0 2108xmap <expr> < F2 > T () 2109exe "normal gg3|ve\<F2>" 2110call assert_equal ( 3 , g: Vcol ) 2111xunmap <F2> 2112delfunc T 2113 2114" Test for the visual line start and end marks '< and '> 2115call setline ( 1 , [ 'one' , 'one two' , 'one two three' ]) 2116"normal! ggVG 2117call feedkeys ( "ggVG\<Esc>" , 'xt' ) 2118call assert_equal ( 1 , col ( "'<" )) 2119call assert_equal ( 14 , col ( "'>" )) 2120" Delete the last line of the visually selected region 2121$ d 2122call assert_notequal ( 14 , col ( "'>" )) 2123 2124" Test with 'virtualedit' 2125set virtualedit = all 2126call cursor ( 1 , 10 ) 2127call assert_equal ( 4 , col ( '.' )) 2128set virtualedit & 2129 2130" Test for getting the column number in another window 2131let winid = win_getid () 2132new 2133call win_execute ( winid , 'normal 1G$' ) 2134call assert_equal ( 3 , col ( '.' , winid )) 2135call win_execute ( winid , 'normal 2G' ) 2136call assert_equal ( 8 , col ( '$' , winid )) 2137call assert_equal ( 0 , col ( '.' , 5001 )) 2138 2139bw ! 2140endfunc 2141 2142" Test for input() 2143func Test_input_func () 2144" Test for prompt with multiple lines 2145redir => v 2146call feedkeys ( ":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>" , 'xt' ) 2147redir END 2148call assert_equal ( "B" , c ) 2149call assert_equal ([ 'A' , 'B' , 'C' ], split ( v , "\n" )) 2150 2151" Test for default value 2152call feedkeys ( ":let c = input('color? ', 'red')\<CR>\<CR>" , 'xt' ) 2153call assert_equal ( 'red' , c ) 2154 2155" Test for completion at the input prompt 2156func ! Tcomplete ( arglead , cmdline , pos ) 2157return "item1\nitem2\nitem3" 2158endfunc 2159call feedkeys ( ":let c = input('Q? ', '', 'custom,Tcomplete')\<CR>" 2160 \.. "\<C-A>\<CR>" , 'xt' ) 2161delfunc Tcomplete 2162call assert_equal ( 'item1 item2 item3' , c ) 2163 2164" Test for using special characters as default input 2165call feedkeys ( ":let c = input('name? ', \"x\\<BS>y\")\<CR>\<CR>" , 'xt' ) 2166call assert_equal ( 'y' , c ) 2167 2168" Test for using text with composing characters as default input 2169call feedkeys ( ":let c = input('name? ', \"ã̳\")\<CR>\<CR>" , 'xt' ) 2170call assert_equal ( 'ã̳' , c ) 2171 2172" Test for using <CR> as default input 2173call feedkeys ( ":let c = input('name? ', \"\\<CR>\")\<CR>x\<CR>" , 'xt' ) 2174call assert_equal ( ' x' , c ) 2175 2176call assert_fails ( "call input('F:', '', 'invalid')" , 'E180:' ) 2177call assert_fails ( "call input('F:', '', [])" , 'E730:' ) 2178 2179" Test for using "command" as the completion function 2180call feedkeys ( ":let c = input('Command? ', '', 'command')\<CR>" 2181 \.. "echo bufnam\<C-A>\<CR>" , 'xt' ) 2182call assert_equal ( 'echo bufname(' , c ) 2183 2184" Test for using "shellcmdline" as the completion function 2185call feedkeys ( ":let c = input('Shell? ', '', 'shellcmdline')\<CR>" 2186 \.. "vim test_functions.\<C-A>\<CR>" , 'xt' ) 2187call assert_equal ( 'vim test_functions.vim' , c ) 2188if executable ( 'whoami' ) 2189call feedkeys ( ":let c = input('Shell? ', '', 'shellcmdline')\<CR>" 2190 \.. "whoam\<C-A>\<CR>" , 'xt' ) 2191call assert_match ( '\<whoami\>' , c ) 2192endif 2193endfunc 2194 2195" Test for the inputdialog() function 2196func Test_inputdialog () 2197set timeout timeoutlen = 10 2198if has ( 'gui_running' ) 2199call assert_fails ( 'let v=inputdialog([], "xx")' , 'E730:' ) 2200call assert_fails ( 'let v=inputdialog("Q", [])' , 'E730:' ) 2201else 2202call feedkeys ( ":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>" , 'xt' ) 2203call assert_equal ( 'xx' , v ) 2204call feedkeys ( ":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>" , 'xt' ) 2205call assert_equal ( 'yy' , v ) 2206endif 2207set timeout & timeoutlen & 2208endfunc 2209 2210" Test for inputlist() 2211func Test_inputlist () 2212call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>" , 'tx' ) 2213call assert_equal ( 1 , c ) 2214call feedkeys ( ":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>" , 'tx' ) 2215call assert_equal ( 2 , c ) 2216call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>" , 'tx' ) 2217call assert_equal ( 3 , c ) 2218 2219" CR to cancel 2220call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>" , 'tx' ) 2221call assert_equal ( 0 , c ) 2222 2223" Esc to cancel 2224call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>" , 'tx' ) 2225call assert_equal ( 0 , c ) 2226 2227" q to cancel 2228call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q" , 'tx' ) 2229call assert_equal ( 0 , c ) 2230 2231" Cancel after inputting a number 2232call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q" , 'tx' ) 2233call assert_equal ( 0 , c ) 2234 2235" Use backspace to delete characters in the prompt 2236call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>" , 'tx' ) 2237call assert_equal ( 2 , c ) 2238 2239" Use mouse to make a selection 2240call Ntest_setmouse ( & lines - 3 , 2 ) 2241call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>" , 'tx' ) 2242call assert_equal ( 1 , c ) 2243" Mouse click outside of the list 2244call Ntest_setmouse ( & lines - 6 , 2 ) 2245call feedkeys ( ":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>" , 'tx' ) 2246call assert_equal ( -2 , c ) 2247 2248call assert_fails ( 'call inputlist("")' , 'E686:' ) 2249" Nvim accepts null list as empty list 2250" call assert_fails('call inputlist(v:_null_list)', 'E686:') 2251endfunc 2252 2253func Test_range_inputlist () 2254" flush out any garbage left in the buffer 2255while getchar ( 0 ) 2256endwhile 2257 2258call feedkeys ( ":let result = inputlist(range(10))\<CR>1\<CR>" , 'x' ) 2259call assert_equal ( 1 , result ) 2260call feedkeys ( ":let result = inputlist(range(3, 10))\<CR>1\<CR>" , 'x' ) 2261call assert_equal ( 1 , result ) 2262 2263unlet result 2264endfunc 2265 2266func Test_balloon_show () 2267CheckFeature balloon_eval 2268 2269" This won't do anything but must not crash either. 2270call balloon_show ( 'hi!' ) 2271if ! has ( 'gui_running' ) 2272call balloon_show ( range ( 3 )) 2273call balloon_show ([]) 2274endif 2275endfunc 2276 2277func Test_setbufvar_options () 2278" This tests that ctx_switch() and ctx_restore() properly restore the 2279" window layout and cursor position. 2280call assert_equal ( 1 , winnr ( '$' )) 2281split dummy_preview 2282resize 2 2283set winfixheight winfixwidth 2284let prev_id = win_getid () 2285 2286wincmd j 2287let wh = winheight ( 0 ) 2288let dummy_buf = bufnr ( 'dummy_buf1' , v: true ) 2289call setbufvar ( dummy_buf , '&buftype' , 'nofile' ) 2290execute 'belowright vertical split #' . dummy_buf 2291call assert_equal ( wh , winheight ( 0 )) 2292let dum1_id = win_getid () 2293call setline ( 1 , 'foo' ) 2294normal ! V$ 2295call assert_equal ( 4 , col ( '.' )) 2296call setbufvar ( 'dummy_preview' , '&buftype' , 'nofile' ) 2297call assert_equal ( 4 , col ( '.' )) 2298 2299wincmd h 2300let wh = winheight ( 0 ) 2301call setline ( 1 , 'foo' ) 2302normal ! V$ 2303call assert_equal ( 4 , col ( '.' )) 2304let dummy_buf = bufnr ( 'dummy_buf2' , v: true ) 2305eval 'nofile' ->setbufvar ( dummy_buf , '&buftype' ) 2306call assert_equal ( 4 , col ( '.' )) 2307execute 'belowright vertical split #' . dummy_buf 2308call assert_equal ( wh , winheight ( 0 )) 2309 2310bwipe ! 2311call win_gotoid ( prev_id ) 2312bwipe ! 2313call win_gotoid ( dum1_id ) 2314bwipe ! 2315endfunc 2316 2317func Test_setbufvar_keep_window_title () 2318CheckRunVimInTerminal 2319if ! has ( 'title' ) || empty ( & t_ts ) 2320throw "Skipped: can't get/set title" 2321endif 2322 2323let lines =<< trim END 2324set title 2325edit Xa.txt 2326let g:buf = bufadd('Xb.txt') 2327inoremap <F2> <C-R>=setbufvar(g:buf, '&autoindent', 1) ?? ''<CR> 2328END 2329call writefile ( lines , 'Xsetbufvar' ) 2330let buf = RunVimInTerminal ( '-S Xsetbufvar' , {}) 2331call WaitForAssert ({ ->assert_match ( 'Xa.txt' , term_gettitle ( buf ))}, 1000 ) 2332 2333call term_sendkeys ( buf , "i\<F2>" ) 2334call TermWait ( buf ) 2335call term_sendkeys ( buf , "\<Esc>" ) 2336call TermWait ( buf ) 2337call assert_match ( 'Xa.txt' , term_gettitle ( buf )) 2338 2339call StopVimInTerminal ( buf ) 2340call delete ( 'Xsetbufvar' ) 2341endfunc 2342 2343func Test_redo_in_nested_functions () 2344nnoremap g. :set opfunc=Operator< CR > g@ 2345function Operator ( type , ... ) 2346let @x= 'XXX' 2347execute 'normal! g`[' . ( a: type ==# 'line' ? 'V' : 'v' ) . 'g`]' . '"xp' 2348endfunction 2349 2350function ! Apply () 23515 , 6 normal ! . 2352endfunction 2353 2354new 2355call setline ( 1 , repeat ([ 'some "quoted" text' , 'more "quoted" text' ], 3 )) 23561 normal g.i" 2357call assert_equal ( 'some "XXX" text' , getline ( 1 )) 23583 , 4 normal . 2359call assert_equal ( 'some "XXX" text' , getline ( 3 )) 2360call assert_equal ( 'more "XXX" text' , getline ( 4 )) 2361call Apply () 2362call assert_equal ( 'some "XXX" text' , getline ( 5 )) 2363call assert_equal ( 'more "XXX" text' , getline ( 6 )) 2364bwipe ! 2365 2366nunmap g. 2367delfunc Operator 2368delfunc Apply 2369endfunc 2370 2371func Test_trim () 2372call assert_equal ( "Testing" , trim ( " \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B" )) 2373call assert_equal ( "Testing" , " \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B" ->trim ()) 2374call assert_equal ( "RESERVE" , trim ( "xyz \twwRESERVEzyww \t\t" , " wxyz\t" )) 2375call assert_equal ( "wRE \tSERVEzyww" , trim ( "wRE \tSERVEzyww" )) 2376call assert_equal ( "abcd\t xxxx tail" , trim ( " \tabcd\t xxxx tail" )) 2377call assert_equal ( "\tabcd\t xxxx tail" , trim ( " \tabcd\t xxxx tail" , " " )) 2378call assert_equal ( " \tabcd\t xxxx tail" , trim ( " \tabcd\t xxxx tail" , "abx" )) 2379call assert_equal ( "RESERVE" , trim ( "你RESERVE好" , "你好" )) 2380call assert_equal ( "您R E SER V E早" , trim ( "你好您R E SER V E早好你你" , "你好" )) 2381call assert_equal ( "你好您R E SER V E早好你你" , trim ( " \n\r\r 你好您R E SER V E早好你你 \t \x0B" , )) 2382call assert_equal ( "您R E SER V E早好你你 \t \x0B" , trim ( " 你好您R E SER V E早好你你 \t \x0B" , " 你好" )) 2383call assert_equal ( "您R E SER V E早好你你 \t \x0B" , trim ( " tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt" , " 你好tes" )) 2384call assert_equal ( "您R E SER V E早好你你 \t \x0B" , trim ( " tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt" , " 你你你好好好tttsses" )) 2385call assert_equal ( "留下" , trim ( "这些些不要这些留下这些" , "这些不要" )) 2386call assert_equal ( "" , trim ( "" , "" )) 2387call assert_equal ( "a" , trim ( "a" , "" )) 2388call assert_equal ( "" , trim ( "" , "a" )) 2389call assert_equal ( "vim" , trim ( " vim " , " " , 0 )) 2390call assert_equal ( "vim " , trim ( " vim " , " " , 1 )) 2391call assert_equal ( " vim" , trim ( " vim " , " " , 2 )) 2392call assert_fails ( 'eval trim(" vim ", " ", [])' , 'E745:' ) 2393call assert_fails ( 'eval trim(" vim ", " ", -1)' , 'E475:' ) 2394call assert_fails ( 'eval trim(" vim ", " ", 3)' , 'E475:' ) 2395call assert_fails ( 'eval trim(" vim ", 0)' , 'E1174:' ) 2396 2397let chars = join ( map ( range ( 1 , 0x20 ) + [ 0xa0 ], { n ->n ->nr2char ()}), '' ) 2398call assert_equal ( "x" , trim ( chars . "x" . chars )) 2399 2400call assert_equal ( "x" , trim ( chars . "x" . chars , '' , 0 )) 2401call assert_equal ( "x" . chars , trim ( chars . "x" . chars , '' , 1 )) 2402call assert_equal ( chars . "x" , trim ( chars . "x" . chars , '' , 2 )) 2403 2404call assert_fails ( 'let c=trim([])' , 'E730:' ) 2405endfunc 2406 2407" Test for reg_recording() and reg_executing() 2408func Test_reg_executing_and_recording () 2409let s: reg_stat = '' 2410func s: save_reg_stat () 2411let s: reg_stat = reg_recording () . ':' . reg_executing () 2412return '' 2413endfunc 2414 2415new 2416call s: save_reg_stat () 2417call assert_equal ( ':' , s: reg_stat ) 2418call feedkeys ( "qa\"=s:save_reg_stat()\<CR>pq" , 'xt' ) 2419call assert_equal ( 'a:' , s: reg_stat ) 2420call feedkeys ( "@a" , 'xt' ) 2421call assert_equal ( ':a' , s: reg_stat ) 2422call feedkeys ( "qb@aq" , 'xt' ) 2423call assert_equal ( 'b:a' , s: reg_stat ) 2424call feedkeys ( "q\"\"=s:save_reg_stat()\<CR>pq" , 'xt' ) 2425call assert_equal ( '":' , s: reg_stat ) 2426 2427" :normal command saves and restores reg_executing 2428let s: reg_stat = '' 2429let @q= ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>" 2430func TestFunc () abort 2431normal ! ia 2432endfunc 2433call feedkeys ( "@q" , 'xt' ) 2434call assert_equal ( ':q' , s: reg_stat ) 2435delfunc TestFunc 2436 2437" getchar() command saves and restores reg_executing 2438map W :call TestFunc()< CR > 2439let @q= "W" 2440let g: typed = '' 2441let g: regs = [] 2442func TestFunc () abort 2443let g: regs += [ reg_executing ()] 2444let g: typed = getchar ( 0 ) 2445let g: regs += [ reg_executing ()] 2446endfunc 2447call feedkeys ( "@qy" , 'xt' ) 2448call assert_equal ( char2nr ( "y" ), g: typed ) 2449call assert_equal ([ 'q' , 'q' ], g: regs ) 2450delfunc TestFunc 2451unmap W 2452unlet g: typed 2453unlet g: regs 2454 2455" input() command saves and restores reg_executing 2456map W :call TestFunc()< CR > 2457let @q= "W" 2458let g: typed = '' 2459let g: regs = [] 2460func TestFunc () abort 2461let g: regs += [ reg_executing ()] 2462let g: typed = '?' ->input () 2463let g: regs += [ reg_executing ()] 2464endfunc 2465call feedkeys ( "@qy\<CR>" , 'xt' ) 2466call assert_equal ( "y" , g: typed ) 2467call assert_equal ([ 'q' , 'q' ], g: regs ) 2468delfunc TestFunc 2469unmap W 2470unlet g: typed 2471unlet g: regs 2472 2473bwipe ! 2474delfunc s:save_reg_stat 2475unlet s: reg_stat 2476endfunc 2477 2478func Test_inputsecret () 2479map W :call TestFunc()< CR > 2480let @q= "W" 2481let g: typed1 = '' 2482let g: typed2 = '' 2483let g: regs = [] 2484func TestFunc () abort 2485let g: typed1 = '?' ->inputsecret () 2486let g: typed2 = inputsecret ( 'password: ' ) 2487endfunc 2488call feedkeys ( "@qsomething\<CR>else\<CR>" , 'xt' ) 2489call assert_equal ( "something" , g: typed1 ) 2490call assert_equal ( "else" , g: typed2 ) 2491delfunc TestFunc 2492unmap W 2493unlet g: typed1 2494unlet g: typed2 2495endfunc 2496 2497func Test_getchar () 2498call feedkeys ( 'a' , '' ) 2499call assert_equal ( char2nr ( 'a' ), getchar ()) 2500call assert_equal ( 0 , getchar ( 0 )) 2501call assert_equal ( 0 , getchar ( 1 )) 2502 2503call feedkeys ( 'a' , '' ) 2504call assert_equal ( 'a' , getcharstr ()) 2505call assert_equal ( '' , getcharstr ( 0 )) 2506call assert_equal ( '' , getcharstr ( 1 )) 2507 2508call feedkeys ( "\<M-F2>" , '' ) 2509call assert_equal ( "\<M-F2>" , getchar ( 0 )) 2510call assert_equal ( 0 , getchar ( 0 )) 2511 2512call feedkeys ( "\<Tab>" , '' ) 2513call assert_equal ( char2nr ( "\<Tab>" ), getchar ()) 2514call feedkeys ( "\<Tab>" , '' ) 2515call assert_equal ( char2nr ( "\<Tab>" ), getchar ( -1 )) 2516call feedkeys ( "\<Tab>" , '' ) 2517call assert_equal ( char2nr ( "\<Tab>" ), getchar ( -1 , {})) 2518call feedkeys ( "\<Tab>" , '' ) 2519call assert_equal ( char2nr ( "\<Tab>" ), getchar ( -1 , #{ number : v: true })) 2520call assert_equal ( 0 , getchar ( 0 )) 2521call assert_equal ( 0 , getchar ( 1 )) 2522call assert_equal ( 0 , getchar ( 0 , #{ number : v: true })) 2523call assert_equal ( 0 , getchar ( 1 , #{ number : v: true })) 2524 2525call feedkeys ( "\<Tab>" , '' ) 2526call assert_equal ( "\<Tab>" , getcharstr ()) 2527call feedkeys ( "\<Tab>" , '' ) 2528call assert_equal ( "\<Tab>" , getcharstr ( -1 )) 2529call feedkeys ( "\<Tab>" , '' ) 2530call assert_equal ( "\<Tab>" , getcharstr ( -1 , {})) 2531call feedkeys ( "\<Tab>" , '' ) 2532call assert_equal ( "\<Tab>" , getchar ( -1 , #{ number : v: false })) 2533call assert_equal ( '' , getcharstr ( 0 )) 2534call assert_equal ( '' , getcharstr ( 1 )) 2535call assert_equal ( '' , getchar ( 0 , #{ number : v: false })) 2536call assert_equal ( '' , getchar ( 1 , #{ number : v: false })) 2537 2538" Nvim: <M-x> is never simplified 2539" for key in ["C-I", "C-X", "M-x"] 2540for key in [ "C-I" , "C-X" ] 2541let lines =<< eval trim END 2542call feedkeys("\<*{key}>", '') 2543call assert_equal(char2nr("\<{key}>"), getchar()) 2544call feedkeys("\<*{key}>", '') 2545call assert_equal(char2nr("\<{key}>"), getchar(-1)) 2546call feedkeys("\<*{key}>", '') 2547call assert_equal(char2nr("\<{key}>"), getchar(-1, {{}})) 2548call feedkeys("\<*{key}>", '') 2549call assert_equal(char2nr("\<{key}>"), getchar(-1, {{'number': 1}})) 2550call feedkeys("\<*{key}>", '') 2551call assert_equal(char2nr("\<{key}>"), getchar(-1, {{'simplify': 1}})) 2552call feedkeys("\<*{key}>", '') 2553call assert_equal("\<*{key}>", getchar(-1, {{'simplify': v:false}})) 2554call assert_equal(0, getchar(0)) 2555call assert_equal(0, getchar(1)) 2556END 2557call CheckLegacyAndVim9Success ( lines ) 2558 2559let lines =<< eval trim END 2560call feedkeys("\<*{key}>", '') 2561call assert_equal("\<{key}>", getcharstr()) 2562call feedkeys("\<*{key}>", '') 2563call assert_equal("\<{key}>", getcharstr(-1)) 2564call feedkeys("\<*{key}>", '') 2565call assert_equal("\<{key}>", getcharstr(-1, {{}})) 2566call feedkeys("\<*{key}>", '') 2567call assert_equal("\<{key}>", getchar(-1, {{'number': 0}})) 2568call feedkeys("\<*{key}>", '') 2569call assert_equal("\<{key}>", getcharstr(-1, {{'simplify': 1}})) 2570call feedkeys("\<*{key}>", '') 2571call assert_equal("\<*{key}>", getcharstr(-1, {{'simplify': v:false}})) 2572call assert_equal('', getcharstr(0)) 2573call assert_equal('', getcharstr(1)) 2574END 2575call CheckLegacyAndVim9Success ( lines ) 2576endfor 2577 2578call assert_fails ( 'call getchar(1, 1)' , 'E1206:' ) 2579call assert_fails ( 'call getcharstr(1, 1)' , 'E1206:' ) 2580call assert_fails ( 'call getchar(1, #{cursor: "foo"})' , 'E475:' ) 2581call assert_fails ( 'call getcharstr(1, #{cursor: "foo"})' , 'E475:' ) 2582call assert_fails ( 'call getchar(1, #{cursor: 0z})' , 'E976:' ) 2583call assert_fails ( 'call getcharstr(1, #{cursor: 0z})' , 'E976:' ) 2584call assert_fails ( 'call getchar(1, #{simplify: 0z})' , 'E974:' ) 2585call assert_fails ( 'call getcharstr(1, #{simplify: 0z})' , 'E974:' ) 2586call assert_fails ( 'call getchar(1, #{number: []})' , 'E745:' ) 2587call assert_fails ( 'call getchar(1, #{number: {}})' , 'E728:' ) 2588call assert_fails ( 'call getcharstr(1, #{number: v:true})' , 'E475:' ) 2589call assert_fails ( 'call getcharstr(1, #{number: v:false})' , 'E475:' ) 2590 2591call setline ( 1 , 'xxxx' ) 2592call Ntest_setmouse ( 1 , 3 ) 2593let v: mouse_win = 9 2594let v: mouse_winid = 9 2595let v: mouse_lnum = 9 2596let v: mouse_col = 9 2597call feedkeys ( "\<S-LeftMouse>" , '' ) 2598call assert_equal ( "\<S-LeftMouse>" , getchar ()) 2599call assert_equal ( 1 , v: mouse_win ) 2600call assert_equal ( win_getid ( 1 ), v: mouse_winid ) 2601call assert_equal ( 1 , v: mouse_lnum ) 2602call assert_equal ( 3 , v: mouse_col ) 2603enew ! 2604endfunc 2605 2606func Test_getchar_cursor_position () 2607CheckRunVimInTerminal 2608 2609let lines =<< trim END 2610call setline(1, ['foobar', 'foobar', 'foobar']) 2611call cursor(3, 6) 2612nnoremap <F1> <Cmd>echo 1234<Bar>call getchar()<CR> 2613nnoremap <F2> <Cmd>call getchar()<CR> 2614nnoremap <F3> <Cmd>call getchar(-1, {})<CR> 2615nnoremap <F4> <Cmd>call getchar(-1, #{cursor: 'msg'})<CR> 2616nnoremap <F5> <Cmd>call getchar(-1, #{cursor: 'keep'})<CR> 2617nnoremap <F6> <Cmd>call getchar(-1, #{cursor: 'hide'})<CR> 2618END 2619call writefile ( lines , 'XgetcharCursorPos' , 'D' ) 2620let buf = RunVimInTerminal ( '-S XgetcharCursorPos' , { 'rows' : 6 }) 2621call WaitForAssert ({ ->assert_equal ([ 3 , 6 ], term_getcursor ( buf )[ 0 : 1 ])}) 2622 2623call term_sendkeys ( buf , "\<F1>" ) 2624call WaitForAssert ({ ->assert_equal ([ 6 , 5 ], term_getcursor ( buf )[ 0 : 1 ])}) 2625call assert_true ( term_getcursor ( buf )[ 2 ]. visible ) 2626call term_sendkeys ( buf , 'a' ) 2627call WaitForAssert ({ ->assert_equal ([ 3 , 6 ], term_getcursor ( buf )[ 0 : 1 ])}) 2628call assert_true ( term_getcursor ( buf )[ 2 ]. visible ) 2629 2630for key in [ "\<F2>" , "\<F3>" , "\<F4>" ] 2631call term_sendkeys ( buf , key ) 2632call WaitForAssert ({ ->assert_equal ([ 6 , 1 ], term_getcursor ( buf )[ 0 : 1 ])}) 2633call assert_true ( term_getcursor ( buf )[ 2 ]. visible ) 2634call term_sendkeys ( buf , 'a' ) 2635call WaitForAssert ({ ->assert_equal ([ 3 , 6 ], term_getcursor ( buf )[ 0 : 1 ])}) 2636call assert_true ( term_getcursor ( buf )[ 2 ]. visible ) 2637endfor 2638 2639call term_sendkeys ( buf , "\<F5>" ) 2640call TermWait ( buf , 50 ) 2641call assert_equal ([ 3 , 6 ], term_getcursor ( buf )[ 0 : 1 ]) 2642call assert_true ( term_getcursor ( buf )[ 2 ]. visible ) 2643call term_sendkeys ( buf , 'a' ) 2644call TermWait ( buf , 50 ) 2645call assert_equal ([ 3 , 6 ], term_getcursor ( buf )[ 0 : 1 ]) 2646call assert_true ( term_getcursor ( buf )[ 2 ]. visible ) 2647 2648call term_sendkeys ( buf , "\<F6>" ) 2649call WaitForAssert ({ ->assert_false ( term_getcursor ( buf )[ 2 ]. visible )}) 2650call term_sendkeys ( buf , 'a' ) 2651call WaitForAssert ({ ->assert_true ( term_getcursor ( buf )[ 2 ]. visible )}) 2652call assert_equal ([ 3 , 6 ], term_getcursor ( buf )[ 0 : 1 ]) 2653 2654call StopVimInTerminal ( buf ) 2655endfunc 2656 2657func Test_libcall_libcallnr () 2658CheckFeature libcall 2659 2660if has ( 'win32' ) 2661let libc = 'msvcrt.dll' 2662elseif has ( 'mac' ) 2663let libc = 'libSystem.B.dylib' 2664elseif executable ( 'ldd' ) 2665let libc = matchstr ( split ( system ( 'ldd ' . GetVimProg ())), '/libc\.so\>' ) 2666endif 2667if get ( l:, 'libc' , '' ) ==# '' 2668" On Unix, libc.so can be in various places. 2669if has ( 'linux' ) 2670" There is not documented but regarding the 1st argument of glibc's 2671" dlopen an empty string and nullptr are equivalent, so using an empty 2672" string for the 1st argument of libcall allows to call functions. 2673let libc = '' 2674elseif has ( 'sun' ) 2675" Set the path to libc.so according to the architecture. 2676let test_bits = system ( 'file ' . GetVimProg ()) 2677let test_arch = system ( 'uname -p' ) 2678if test_bits =~ '64-bit' && test_arch =~ 'sparc' 2679let libc = '/usr/lib/sparcv9/libc.so' 2680elseif test_bits =~ '64-bit' && test_arch =~ 'i386' 2681let libc = '/usr/lib/amd64/libc.so' 2682else 2683let libc = '/usr/lib/libc.so' 2684endif 2685else 2686" Unfortunately skip this test until a good way is found. 2687return 2688endif 2689endif 2690 2691if has ( 'win32' ) 2692call assert_equal ( $ USERPROFILE , 'USERPROFILE' ->libcall ( libc , 'getenv' )) 2693else 2694call assert_equal ( $ HOME , 'HOME' ->libcall ( libc , 'getenv' )) 2695endif 2696 2697" If function returns NULL, libcall() should return an empty string. 2698call assert_equal ( '' , libcall ( libc , 'getenv' , 'X_ENV_DOES_NOT_EXIT' )) 2699 2700" Test libcallnr() with string and integer argument. 2701call assert_equal ( 4 , 'abcd' ->libcallnr ( libc , 'strlen' )) 2702call assert_equal ( char2nr ( 'A' ), char2nr ( 'a' ) ->libcallnr ( libc , 'toupper' )) 2703 2704call assert_fails ( "call libcall(libc, 'Xdoesnotexist_', '')" , [ '' , 'E364:' ]) 2705call assert_fails ( "call libcallnr(libc, 'Xdoesnotexist_', '')" , [ '' , 'E364:' ]) 2706 2707call assert_fails ( "call libcall('Xdoesnotexist_', 'getenv', 'HOME')" , [ '' , 'E364:' ]) 2708call assert_fails ( "call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')" , [ '' , 'E364:' ]) 2709endfunc 2710 2711sandbox function Fsandbox() 2712normal ix 2713endfunc 2714 2715func Test_func_sandbox () 2716sandbox let F = {->'hello' } 2717call assert_equal ( 'hello' , F ()) 2718 2719sandbox let F = {->"normal ix\<Esc>" ->execute()} 2720call assert_fails ( 'call F()' , 'E48:' ) 2721unlet F 2722 2723call assert_fails ( 'call Fsandbox()' , 'E48:' ) 2724delfunc Fsandbox 2725 2726" From a sandbox try to set a predefined variable (which cannot be modified 2727" from a sandbox) 2728call assert_fails ( 'sandbox let v:lnum = 10' , 'E794:' ) 2729endfunc 2730 2731func EditAnotherFile () 2732let word = expand ( '<cword>' ) 2733edit Xfuncrange2 2734endfunc 2735 2736func Test_func_range_with_edit () 2737" Define a function that edits another buffer, then call it with a range that 2738" is invalid in that buffer. 2739call writefile ([ 'just one line' ], 'Xfuncrange2' ) 2740new 2741eval 10 ->range () ->setline ( 1 ) 2742write Xfuncrange1 2743call assert_fails ( '5,8call EditAnotherFile()' , 'E16:' ) 2744 2745call delete ( 'Xfuncrange1' ) 2746call delete ( 'Xfuncrange2' ) 2747bwipe ! 2748endfunc 2749 2750func Test_func_exists_on_reload () 2751call writefile ([ 'func ExistingFunction()' , 'echo "yes"' , 'endfunc' ], 'Xfuncexists' ) 2752call assert_equal ( 0 , exists ( '*ExistingFunction' )) 2753source Xfuncexists 2754call assert_equal ( 1 , '*ExistingFunction' ->exists ()) 2755" Redefining a function when reloading a script is OK. 2756source Xfuncexists 2757call assert_equal ( 1 , exists ( '*ExistingFunction' )) 2758 2759" But redefining in another script is not OK. 2760call writefile ([ 'func ExistingFunction()' , 'echo "yes"' , 'endfunc' ], 'Xfuncexists2' ) 2761call assert_fails ( 'source Xfuncexists2' , 'E122:' ) 2762 2763delfunc ExistingFunction 2764call assert_equal ( 0 , exists ( '*ExistingFunction' )) 2765call writefile ([ 2766 \'func ExistingFunction()' , 'echo "yes"' , 'endfunc' , 2767 \'func ExistingFunction()' , 'echo "no"' , 'endfunc' , 2768 \], 'Xfuncexists' ) 2769call assert_fails ( 'source Xfuncexists' , 'E122:' ) 2770call assert_equal ( 1 , exists ( '*ExistingFunction' )) 2771 2772call delete ( 'Xfuncexists2' ) 2773call delete ( 'Xfuncexists' ) 2774delfunc ExistingFunction 2775endfunc 2776 2777" Test confirm({msg} [, {choices} [, {default} [, {type}]]]) 2778func Test_confirm () 2779CheckUnix 2780CheckNotGui 2781 2782call feedkeys ( 'o' , 'L' ) 2783let a = confirm ( 'Press O to proceed' ) 2784call assert_equal ( 1 , a ) 2785 2786call feedkeys ( 'y' , 'L' ) 2787let a = 'Are you sure?' ->confirm ( "&Yes\n&No" ) 2788call assert_equal ( 1 , a ) 2789 2790call feedkeys ( 'n' , 'L' ) 2791let a = confirm ( 'Are you sure?' , "&Yes\n&No" ) 2792call assert_equal ( 2 , a ) 2793 2794" confirm() should return 0 when pressing CTRL-C. 2795call feedkeys ( "\<C-C>" , 'L' ) 2796let a = confirm ( 'Are you sure?' , "&Yes\n&No" ) 2797call assert_equal ( 0 , a ) 2798 2799" <Esc> requires another character to avoid it being seen as the start of an 2800" escape sequence. Zero should be harmless. 2801eval "\<Esc>0" ->feedkeys ( 'L' ) 2802let a = confirm ( 'Are you sure?' , "&Yes\n&No" ) 2803call assert_equal ( 0 , a ) 2804 2805" Default choice is returned when pressing <CR>. 2806call feedkeys ( "\<CR>" , 'L' ) 2807let a = confirm ( 'Are you sure?' , "&Yes\n&No" ) 2808call assert_equal ( 1 , a ) 2809 2810call feedkeys ( "\<CR>" , 'L' ) 2811let a = confirm ( 'Are you sure?' , "&Yes\n&No" , 2 ) 2812call assert_equal ( 2 , a ) 2813 2814call feedkeys ( "\<CR>" , 'L' ) 2815let a = confirm ( 'Are you sure?' , "&Yes\n&No" , 0 ) 2816call assert_equal ( 0 , a ) 2817 2818" Test with the {type} 4th argument 2819for type in [ 'Error' , 'Question' , 'Info' , 'Warning' , 'Generic' ] 2820call feedkeys ( 'y' , 'L' ) 2821let a = confirm ( 'Are you sure?' , "&Yes\n&No\n" , 1 , type ) 2822call assert_equal ( 1 , a ) 2823endfor 2824 2825call assert_fails ( 'call confirm([])' , 'E730:' ) 2826call assert_fails ( 'call confirm("Are you sure?", [])' , 'E730:' ) 2827call assert_fails ( 'call confirm("Are you sure?", "&Yes\n&No\n", [])' , 'E745:' ) 2828call assert_fails ( 'call confirm("Are you sure?", "&Yes\n&No\n", 0, [])' , 'E730:' ) 2829endfunc 2830 2831func Test_platform_name () 2832" The system matches at most only one name. 2833let names = [ 'amiga' , 'bsd' , 'hpux' , 'linux' , 'mac' , 'qnx' , 'sun' , 'vms' , 'win32' , 'win32unix' ] 2834call assert_inrange ( 0 , 1 , len ( filter ( copy ( names ), 'has(v:val)' ))) 2835 2836" Is Unix? 2837call assert_equal ( has ( 'bsd' ), has ( 'bsd' ) && has ( 'unix' )) 2838call assert_equal ( has ( 'hpux' ), has ( 'hpux' ) && has ( 'unix' )) 2839call assert_equal ( has ( 'hurd' ), has ( 'hurd' ) && has ( 'unix' )) 2840call assert_equal ( has ( 'linux' ), has ( 'linux' ) && has ( 'unix' )) 2841call assert_equal ( has ( 'mac' ), has ( 'mac' ) && has ( 'unix' )) 2842call assert_equal ( has ( 'qnx' ), has ( 'qnx' ) && has ( 'unix' )) 2843call assert_equal ( has ( 'sun' ), has ( 'sun' ) && has ( 'unix' )) 2844call assert_equal ( has ( 'win32' ), has ( 'win32' ) && ! has ( 'unix' )) 2845call assert_equal ( has ( 'win32unix' ), has ( 'win32unix' ) && has ( 'unix' )) 2846 2847if has ( 'unix' ) && executable ( 'uname' ) 2848let uname = system ( 'uname' ) 2849" GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined 2850call assert_equal ( uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly' , has ( 'bsd' )) 2851call assert_equal ( uname =~? 'HP-UX' , has ( 'hpux' )) 2852call assert_equal ( uname =~? 'Linux' , has ( 'linux' )) 2853call assert_equal ( uname =~? 'Darwin' , has ( 'mac' )) 2854call assert_equal ( uname =~? 'QNX' , has ( 'qnx' )) 2855call assert_equal ( uname =~? 'SunOS' , has ( 'sun' )) 2856call assert_equal ( uname =~? 'CYGWIN\|MSYS' , has ( 'win32unix' )) 2857call assert_equal ( uname =~? 'GNU' , has ( 'hurd' )) 2858endif 2859endfunc 2860 2861func Test_readdir () 2862call mkdir ( 'Xdir' ) 2863call writefile ([], 'Xdir/foo.txt' ) 2864call writefile ([], 'Xdir/bar.txt' ) 2865call mkdir ( 'Xdir/dir' ) 2866 2867" All results 2868let files = readdir ( 'Xdir' ) 2869call assert_equal ([ 'bar.txt' , 'dir' , 'foo.txt' ], sort ( files )) 2870 2871" Only results containing "f" 2872let files = 'Xdir' ->readdir ({ x ->stridx ( x , 'f' ) !=- 1 }) 2873call assert_equal ([ 'foo.txt' ], sort ( files )) 2874 2875" Only .txt files 2876let files = readdir ( 'Xdir' , { x ->x =~ '.txt$' }) 2877call assert_equal ([ 'bar.txt' , 'foo.txt' ], sort ( files )) 2878 2879" Only .txt files with string 2880let files = readdir ( 'Xdir' , 'v:val =~ ".txt$"' ) 2881call assert_equal ([ 'bar.txt' , 'foo.txt' ], sort ( files )) 2882 2883" Limit to 1 result. 2884let l = [] 2885let files = readdir ( 'Xdir' , { x ->len ( add ( l , x )) == 2 ? -1 : 1 }) 2886call assert_equal ( 1 , len ( files )) 2887 2888" Nested readdir() must not crash 2889let files = readdir ( 'Xdir' , 'readdir("Xdir", "1") != []' ) 2890call sort ( files ) ->assert_equal ([ 'bar.txt' , 'dir' , 'foo.txt' ]) 2891 2892eval 'Xdir' ->delete ( 'rf' ) 2893endfunc 2894 2895func Test_delete_rf () 2896call mkdir ( 'Xdir' ) 2897call writefile ([], 'Xdir/foo.txt' ) 2898call writefile ([], 'Xdir/bar.txt' ) 2899call mkdir ( 'Xdir/[a-1]' ) " issue #696 2900call writefile ([], 'Xdir/[a-1]/foo.txt' ) 2901call writefile ([], 'Xdir/[a-1]/bar.txt' ) 2902call assert_true ( filereadable ( 'Xdir/foo.txt' )) 2903call assert_true ( 'Xdir/[a-1]/foo.txt' ->filereadable ()) 2904 2905call assert_equal ( 0 , delete ( 'Xdir' , 'rf' )) 2906call assert_false ( filereadable ( 'Xdir/foo.txt' )) 2907call assert_false ( filereadable ( 'Xdir/[a-1]/foo.txt' )) 2908 2909if has ( 'unix' ) 2910call mkdir ( 'Xdir/Xdir2' , 'p' ) 2911silent !chmod555 Xdir 2912call assert_equal ( -1 , delete ( 'Xdir/Xdir2' , 'rf' )) 2913call assert_equal ( -1 , delete ( 'Xdir' , 'rf' )) 2914silent !chmod755 Xdir 2915call assert_equal ( 0 , delete ( 'Xdir' , 'rf' )) 2916endif 2917endfunc 2918 2919func Test_call () 2920call assert_equal ( 3 , call ( 'len' , [ 123 ])) 2921call assert_equal ( 3 , 'len' ->call ([ 123 ])) 2922call assert_equal ( 4 , call ({ x ->len ( x ) }, [ 'xxxx' ])) 2923call assert_equal ( 2 , call ( function ( 'len' ), [ 'xx' ])) 2924call assert_fails ( "call call('len', 123)" , 'E1211:' ) 2925call assert_equal ( 0 , call ( '' , [])) 2926call assert_equal ( 0 , call ( 'len' , v: _null_list )) 2927 2928function Mylen () dict 2929return len ( self . data ) 2930endfunction 2931let mydict = { 'data' : [ 0 , 1 , 2 , 3 ], 'len' : function ( "Mylen" )} 2932eval mydict . len ->call ([], mydict ) ->assert_equal ( 4 ) 2933call assert_fails ( "call call('Mylen', [], 0)" , 'E1206:' ) 2934call assert_fails ( 'call foo' , 'E107:' ) 2935 2936" These once caused a crash. 2937" Nvim doesn't have null functions 2938" call call(test_null_function(), []) 2939" Nvim doesn't have null partials 2940" call call(test_null_partial(), []) 2941" Nvim doesn't have null functions 2942" call assert_fails('call test_null_function()()', 'E1192:') 2943" Nvim doesn't have null partials 2944" call assert_fails('call test_null_partial()()', 'E117:') 2945 2946let lines =<< trim END 2947let Time = 'localtime' 2948call Time() 2949END 2950call CheckScriptFailure ( lines , 'E1085:' ) 2951endfunc 2952 2953func Test_char2nr () 2954call assert_equal ( 12354 , char2nr ( 'あ' , 1 )) 2955call assert_equal ( 120 , 'x' ->char2nr ()) 2956" set encoding=latin1 2957call assert_equal ( 120 , 'x' ->char2nr ()) 2958set encoding = utf-8 2959endfunc 2960 2961func Test_charclass () 2962call assert_equal ( 0 , charclass ( ' ' )) 2963call assert_equal ( 1 , charclass ( '.' )) 2964call assert_equal ( 2 , charclass ( 'x' )) 2965call assert_equal ( 3 , charclass ( "\u203c" )) 2966" this used to crash vim 2967call assert_equal ( 0 , "xxx" [ -1 ] ->charclass ()) 2968endfunc 2969 2970func Test_eventhandler () 2971call assert_equal ( 0 , eventhandler ()) 2972endfunc 2973 2974func Test_bufadd_bufload () 2975call assert_equal ( 0 , bufexists ( 'someName' )) 2976let buf = bufadd ( 'someName' ) 2977call assert_notequal ( 0 , buf ) 2978call assert_equal ( 1 , bufexists ( 'someName' )) 2979call assert_equal ( 0 , getbufvar ( buf , '&buflisted' )) 2980call assert_equal ( 0 , bufloaded ( buf )) 2981call bufload ( buf ) 2982call assert_equal ( 1 , bufloaded ( buf )) 2983call assert_equal ([ '' ], getbufline ( buf , 1 , '$' )) 2984 2985let curbuf = bufnr ( '' ) 2986eval [ 'some' , 'text' ] ->writefile ( 'XotherName' ) 2987let buf = 'XotherName' ->bufadd () 2988call assert_notequal ( 0 , buf ) 2989eval 'XotherName' ->bufexists () ->assert_equal ( 1 ) 2990call assert_equal ( 0 , getbufvar ( buf , '&buflisted' )) 2991call assert_equal ( 0 , bufloaded ( buf )) 2992eval buf ->bufload () 2993call assert_equal ( 1 , bufloaded ( buf )) 2994call assert_equal ([ 'some' , 'text' ], getbufline ( buf , 1 , '$' )) 2995call assert_equal ( curbuf , bufnr ( '' )) 2996 2997let buf1 = bufadd ( '' ) 2998let buf2 = bufadd ( '' ) 2999call assert_notequal ( 0 , buf1 ) 3000call assert_notequal ( 0 , buf2 ) 3001call assert_notequal ( buf1 , buf2 ) 3002call assert_equal ( 1 , bufexists ( buf1 )) 3003call assert_equal ( 1 , bufexists ( buf2 )) 3004call assert_equal ( 0 , bufloaded ( buf1 )) 3005exe 'bwipe ' .. buf1 3006call assert_equal ( 0 , bufexists ( buf1 )) 3007call assert_equal ( 1 , bufexists ( buf2 )) 3008exe 'bwipe ' .. buf2 3009call assert_equal ( 0 , bufexists ( buf2 )) 3010 3011" When 'buftype' is "nofile" then bufload() does not read the file. 3012" Other values too. 3013for val in [[ 'nofile' , 0 ], 3014 \[ 'nowrite' , 1 ], 3015 \[ 'acwrite' , 1 ], 3016 \[ 'quickfix' , 0 ], 3017 \[ 'help' , 1 ], 3018"\ ['terminal', 0], 3019 \[ 'prompt' , 0 ], 3020"\ ['popup', 0], 3021 \] 3022bwipe ! XotherName 3023let buf = bufadd ( 'XotherName' ) 3024call setbufvar ( buf , '&bt' , val [ 0 ]) 3025call bufload ( buf ) 3026call assert_equal ( val [ 1 ] ? [ 'some' , 'text' ] : [ '' ], getbufline ( buf , 1 , '$' ), val [ 0 ]) 3027endfor 3028 3029bwipe someName 3030bwipe XotherName 3031call assert_equal ( 0 , bufexists ( 'someName' )) 3032call delete ( 'XotherName' ) 3033endfunc 3034 3035func Test_state () 3036CheckRunVimInTerminal 3037 3038let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>" 3039 3040let lines =<< trim END 3041call setline(1, ['one', 'two', 'three']) 3042map ;; gg 3043set complete=. 3044func RunTimer() 3045call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')}) 3046endfunc 3047au Filetype foobar let g:state = state()|let g:mode = mode() 3048END 3049call writefile ( lines , 'XState' ) 3050let buf = RunVimInTerminal ( '-S XState' , #{ rows : 6 }) 3051 3052" Using a ":" command Vim is busy, thus "S" is returned 3053call term_sendkeys ( buf , ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>" ) 3054call WaitForAssert ({ ->assert_match ( 'state: S; mode: n' , term_getline ( buf , 6 ))}, 1000 ) 3055call term_sendkeys ( buf , ":\<CR>" ) 3056 3057" Using a timer callback 3058call term_sendkeys ( buf , ":call RunTimer()\<CR>" ) 3059call TermWait ( buf , 25 ) 3060call term_sendkeys ( buf , getstate ) 3061call WaitForAssert ({ ->assert_match ( 'state: c; mode: n' , term_getline ( buf , 6 ))}, 1000 ) 3062 3063" Halfway a mapping 3064call term_sendkeys ( buf , ":call RunTimer()\<CR>;" ) 3065call TermWait ( buf , 25 ) 3066call term_sendkeys ( buf , ";" ) 3067call term_sendkeys ( buf , getstate ) 3068call WaitForAssert ({ ->assert_match ( 'state: mSc; mode: n' , term_getline ( buf , 6 ))}, 1000 ) 3069 3070" An operator is pending 3071call term_sendkeys ( buf , ":call RunTimer()\<CR>y" ) 3072call TermWait ( buf , 25 ) 3073call term_sendkeys ( buf , "y" ) 3074call term_sendkeys ( buf , getstate ) 3075call WaitForAssert ({ ->assert_match ( 'state: oSc; mode: n' , term_getline ( buf , 6 ))}, 1000 ) 3076 3077" A register was specified 3078call term_sendkeys ( buf , ":call RunTimer()\<CR>\"r" ) 3079call TermWait ( buf , 25 ) 3080call term_sendkeys ( buf , "yy" ) 3081call term_sendkeys ( buf , getstate ) 3082call WaitForAssert ({ ->assert_match ( 'state: oSc; mode: n' , term_getline ( buf , 6 ))}, 1000 ) 3083 3084" Insert mode completion (bit slower on Mac) 3085call term_sendkeys ( buf , ":call RunTimer()\<CR>Got\<C-N>" ) 3086call TermWait ( buf , 25 ) 3087call term_sendkeys ( buf , "\<Esc>" ) 3088call term_sendkeys ( buf , getstate ) 3089call WaitForAssert ({ ->assert_match ( 'state: aSc; mode: i' , term_getline ( buf , 6 ))}, 1000 ) 3090 3091" Autocommand executing 3092call term_sendkeys ( buf , ":set filetype=foobar\<CR>" ) 3093call TermWait ( buf , 25 ) 3094call term_sendkeys ( buf , getstate ) 3095call WaitForAssert ({ ->assert_match ( 'state: xS; mode: n' , term_getline ( buf , 6 ))}, 1000 ) 3096 3097" Todo: "w" - waiting for ch_evalexpr() 3098 3099" messages scrolled 3100call term_sendkeys ( buf , ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>" ) 3101call TermWait ( buf , 25 ) 3102call term_sendkeys ( buf , "\<CR>" ) 3103call term_sendkeys ( buf , getstate ) 3104call WaitForAssert ({ ->assert_match ( 'state: Scs; mode: r' , term_getline ( buf , 6 ))}, 1000 ) 3105 3106call StopVimInTerminal ( buf ) 3107call delete ( 'XState' ) 3108endfunc 3109 3110func Test_range () 3111" destructuring 3112let [ x , y ] = range ( 2 ) 3113call assert_equal ([ 0 , 1 ], [ x , y ]) 3114 3115" index 3116call assert_equal ( 4 , range ( 1 , 10 )[ 3 ]) 3117 3118" add() 3119call assert_equal ([ 0 , 1 , 2 , 3 ], add ( range ( 3 ), 3 )) 3120call assert_equal ([ 0 , 1 , 2 , [ 0 , 1 , 2 ]], add ([ 0 , 1 , 2 ], range ( 3 ))) 3121call assert_equal ([ 0 , 1 , 2 , [ 0 , 1 , 2 ]], add ( range ( 3 ), range ( 3 ))) 3122 3123" append() 3124new 3125call append ( '.' , range ( 5 )) 3126call assert_equal ([ '' , '0' , '1' , '2' , '3' , '4' ], getline ( 1 , '$' )) 3127bwipe ! 3128 3129" appendbufline() 3130new 3131call appendbufline ( bufnr ( '' ), '.' , range ( 5 )) 3132call assert_equal ([ '0' , '1' , '2' , '3' , '4' , '' ], getline ( 1 , '$' )) 3133bwipe ! 3134 3135" call() 3136func TwoArgs ( a , b ) 3137return [ a: a , a: b ] 3138endfunc 3139call assert_equal ([ 0 , 1 ], call ( 'TwoArgs' , range ( 2 ))) 3140 3141" col() 3142new 3143call setline ( 1 , [ 'foo' , 'bar' ]) 3144call assert_equal ( 2 , col ( range ( 1 , 2 ))) 3145bwipe ! 3146 3147" complete() 3148execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>" 3149" complete_info() 3150execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>" 3151 3152" copy() 3153call assert_equal ([ 1 , 2 , 3 ], copy ( range ( 1 , 3 ))) 3154 3155" count() 3156call assert_equal ( 0 , count ( range ( 0 ), 3 )) 3157call assert_equal ( 0 , count ( range ( 2 ), 3 )) 3158call assert_equal ( 1 , count ( range ( 5 ), 3 )) 3159 3160" cursor() 3161new 3162call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' ]) 3163call cursor ( range ( 1 , 2 )) 3164call assert_equal ([ 2 , 1 ], [ col ( '.' ), line ( '.' )]) 3165bwipe ! 3166 3167" deepcopy() 3168call assert_equal ([ 1 , 2 , 3 ], deepcopy ( range ( 1 , 3 ))) 3169 3170" empty() 3171call assert_true ( empty ( range ( 0 ))) 3172call assert_false ( empty ( range ( 2 ))) 3173 3174" execute() 3175new 3176call setline ( 1 , [ 'aaa' , 'bbb' , 'ccc' ]) 3177call execute ( range ( 3 )) 3178call assert_equal ( 2 , line ( '.' )) 3179bwipe ! 3180 3181" extend() 3182call assert_equal ([ 1 , 2 , 3 , 4 ], extend ([ 1 ], range ( 2 , 4 ))) 3183call assert_equal ([ 1 , 2 , 3 , 4 ], extend ( range ( 1 , 1 ), range ( 2 , 4 ))) 3184call assert_equal ([ 1 , 2 , 3 , 4 ], extend ( range ( 1 , 1 ), [ 2 , 3 , 4 ])) 3185 3186" filter() 3187call assert_equal ([ 1 , 3 ], filter ( range ( 5 ), 'v:val % 2' )) 3188 3189" funcref() 3190call assert_equal ([ 0 , 1 ], funcref ( 'TwoArgs' , range ( 2 ))()) 3191 3192" function() 3193call assert_equal ([ 0 , 1 ], function ( 'TwoArgs' , range ( 2 ))()) 3194 3195" garbagecollect() 3196let thelist = [ 1 , range ( 2 ), 3 ] 3197let otherlist = range ( 3 ) 3198call test_garbagecollect_now () 3199 3200" get() 3201call assert_equal ( 4 , get ( range ( 1 , 10 ), 3 )) 3202call assert_equal ( -1 , get ( range ( 1 , 10 ), 42 , -1 )) 3203call assert_equal ( 0 , get ( range ( 1 , 0 , 2 ), 0 )) 3204call assert_equal ( 0 , get ( range ( 0 , -1 , 2 ), 0 )) 3205call assert_equal ( 0 , get ( range ( -2 , -1 , -2 ), 0 )) 3206 3207" index() 3208call assert_equal ( 1 , index ( range ( 1 , 5 ), 2 )) 3209call assert_fails ( "echo index([1, 2], 1, [])" , 'E745:' ) 3210 3211" insert() 3212call assert_equal ([ 42 , 1 , 2 , 3 , 4 , 5 ], insert ( range ( 1 , 5 ), 42 )) 3213call assert_equal ([ 42 , 1 , 2 , 3 , 4 , 5 ], insert ( range ( 1 , 5 ), 42 , 0 )) 3214call assert_equal ([ 1 , 42 , 2 , 3 , 4 , 5 ], insert ( range ( 1 , 5 ), 42 , 1 )) 3215call assert_equal ([ 1 , 2 , 3 , 4 , 42 , 5 ], insert ( range ( 1 , 5 ), 42 , 4 )) 3216call assert_equal ([ 1 , 2 , 3 , 4 , 42 , 5 ], insert ( range ( 1 , 5 ), 42 , -1 )) 3217call assert_equal ([ 1 , 2 , 3 , 4 , 5 , 42 ], insert ( range ( 1 , 5 ), 42 , 5 )) 3218 3219" join() 3220call assert_equal ( '0 1 2 3 4' , join ( range ( 5 ))) 3221 3222" json_encode() 3223" call assert_equal('[0,1,2,3]', json_encode(range(4))) 3224call assert_equal ( '[0, 1, 2, 3]' , json_encode ( range ( 4 ))) 3225 3226" len() 3227call assert_equal ( 0 , len ( range ( 0 ))) 3228call assert_equal ( 2 , len ( range ( 2 ))) 3229call assert_equal ( 5 , len ( range ( 0 , 12 , 3 ))) 3230call assert_equal ( 4 , len ( range ( 3 , 0 , -1 ))) 3231 3232" list2str() 3233call assert_equal ( 'ABC' , list2str ( range ( 65 , 67 ))) 3234call assert_fails ( 'let s = list2str(5)' , 'E474:' ) 3235 3236" lock() 3237let thelist = range ( 5 ) 3238lockvar thelist 3239 3240" map() 3241call assert_equal ([ 0 , 2 , 4 , 6 , 8 ], map ( range ( 5 ), 'v:val * 2' )) 3242 3243" match() 3244call assert_equal ( 3 , match ( range ( 5 ), 3 )) 3245 3246" matchaddpos() 3247highlight MyGreenGroup ctermbg = green guibg = green 3248call matchaddpos ( 'MyGreenGroup' , range ( line ( '.' ), line ( '.' ))) 3249 3250" matchend() 3251call assert_equal ( 4 , matchend ( range ( 5 ), '4' )) 3252call assert_equal ( 3 , matchend ( range ( 1 , 5 ), '4' )) 3253call assert_equal ( -1 , matchend ( range ( 1 , 5 ), '42' )) 3254 3255" matchstrpos() 3256call assert_equal ([ '4' , 4 , 0 , 1 ], matchstrpos ( range ( 5 ), '4' )) 3257call assert_equal ([ '4' , 3 , 0 , 1 ], matchstrpos ( range ( 1 , 5 ), '4' )) 3258call assert_equal ([ '' , -1 , -1 , -1 ], matchstrpos ( range ( 1 , 5 ), '42' )) 3259 3260" max() reverse() 3261call assert_equal ( 0 , max ( range ( 0 ))) 3262call assert_equal ( 0 , max ( range ( 10 , 9 ))) 3263call assert_equal ( 9 , max ( range ( 10 ))) 3264call assert_equal ( 18 , max ( range ( 0 , 20 , 3 ))) 3265call assert_equal ( 20 , max ( range ( 20 , 0 , -3 ))) 3266call assert_equal ( 99999 , max ( range ( 100000 ))) 3267call assert_equal ( 99999 , max ( range ( 99999 , 0 , -1 ))) 3268call assert_equal ( 99999 , max ( reverse ( range ( 100000 )))) 3269call assert_equal ( 99999 , max ( reverse ( range ( 99999 , 0 , -1 )))) 3270 3271" min() reverse() 3272call assert_equal ( 0 , min ( range ( 0 ))) 3273call assert_equal ( 0 , min ( range ( 10 , 9 ))) 3274call assert_equal ( 5 , min ( range ( 5 , 10 ))) 3275call assert_equal ( 5 , min ( range ( 5 , 10 , 3 ))) 3276call assert_equal ( 2 , min ( range ( 20 , 0 , -3 ))) 3277call assert_equal ( 0 , min ( range ( 100000 ))) 3278call assert_equal ( 0 , min ( range ( 99999 , 0 , -1 ))) 3279call assert_equal ( 0 , min ( reverse ( range ( 100000 )))) 3280call assert_equal ( 0 , min ( reverse ( range ( 99999 , 0 , -1 )))) 3281 3282" remove() 3283call assert_equal ( 1 , remove ( range ( 1 , 10 ), 0 )) 3284call assert_equal ( 2 , remove ( range ( 1 , 10 ), 1 )) 3285call assert_equal ( 9 , remove ( range ( 1 , 10 ), 8 )) 3286call assert_equal ( 10 , remove ( range ( 1 , 10 ), 9 )) 3287call assert_equal ( 10 , remove ( range ( 1 , 10 ), -1 )) 3288call assert_equal ([ 3 , 4 , 5 ], remove ( range ( 1 , 10 ), 2 , 4 )) 3289 3290" repeat() 3291call assert_equal ([ 0 , 1 , 2 , 0 , 1 , 2 ], repeat ( range ( 3 ), 2 )) 3292call assert_equal ([ 0 , 1 , 2 ], repeat ( range ( 3 ), 1 )) 3293call assert_equal ([], repeat ( range ( 3 ), 0 )) 3294call assert_equal ([], repeat ( range ( 5 , 4 ), 2 )) 3295call assert_equal ([], repeat ( range ( 5 , 4 ), 0 )) 3296 3297" reverse() 3298call assert_equal ([ 2 , 1 , 0 ], reverse ( range ( 3 ))) 3299call assert_equal ([ 0 , 1 , 2 , 3 ], reverse ( range ( 3 , 0 , -1 ))) 3300call assert_equal ([ 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 ], reverse ( range ( 10 ))) 3301call assert_equal ([ 20 , 19 , 18 , 17 , 16 , 15 , 14 , 13 , 12 , 11 , 10 ], reverse ( range ( 10 , 20 ))) 3302call assert_equal ([ 16 , 13 , 10 ], reverse ( range ( 10 , 18 , 3 ))) 3303call assert_equal ([ 19 , 16 , 13 , 10 ], reverse ( range ( 10 , 19 , 3 ))) 3304call assert_equal ([ 19 , 16 , 13 , 10 ], reverse ( range ( 10 , 20 , 3 ))) 3305call assert_equal ([ 11 , 14 , 17 , 20 ], reverse ( range ( 20 , 10 , -3 ))) 3306call assert_equal ([], reverse ( range ( 0 ))) 3307 3308" TODO: setpos() 3309" new 3310" call setline(1, repeat([''], bufnr(''))) 3311" call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6)) 3312" call setpos('x', range(bufnr(''), bufnr('') + 3)) 3313" bwipe! 3314 3315" setreg() 3316call setreg ( 'a' , range ( 3 )) 3317call assert_equal ( "0\n1\n2\n" , getreg ( 'a' )) 3318 3319" settagstack() 3320call settagstack ( 1 , #{ items : range ( 4 )}) 3321 3322" sign_define() 3323call assert_fails ( "call sign_define(range(5))" , "E715:" ) 3324call assert_fails ( "call sign_placelist(range(5))" , "E715:" ) 3325 3326" sign_undefine() 3327" call assert_fails("call sign_undefine(range(5))", "E908:") 3328call assert_fails ( "call sign_undefine(range(5))" , "E155:" ) 3329 3330" sign_unplacelist() 3331call assert_fails ( "call sign_unplacelist(range(5))" , "E715:" ) 3332 3333" sort() 3334call assert_equal ([ 0 , 1 , 2 , 3 , 4 , 5 ], sort ( range ( 5 , 0 , -1 ))) 3335 3336" string() 3337call assert_equal ( '[0, 1, 2, 3, 4]' , string ( range ( 5 ))) 3338 3339" taglist() with 'tagfunc' 3340func TagFunc ( pattern , flags , info ) 3341return range ( 10 ) 3342endfunc 3343set tagfunc = TagFunc 3344call assert_fails ( "call taglist('asdf')" , 'E987:' ) 3345set tagfunc = 3346 3347" term_start() 3348if has ( 'terminal' ) && has ( 'termguicolors' ) 3349call assert_fails ( 'call term_start(range(3, 4))' , 'E474:' ) 3350let g: terminal_ansi_colors = range ( 16 ) 3351if has ( 'win32' ) 3352let cmd = "cmd /D /c dir" 3353else 3354let cmd = "ls" 3355endif 3356call assert_fails ( 'call term_start("' .. cmd .. '", #{term_finish: "close"})' , 'E475:' ) 3357unlet g: terminal_ansi_colors 3358endif 3359 3360" type() 3361call assert_equal ( v: t_list , type ( range ( 5 ))) 3362 3363" uniq() 3364call assert_equal ([ 0 , 1 , 2 , 3 , 4 ], uniq ( range ( 5 ))) 3365 3366" errors 3367call assert_fails ( 'let x=range(2, 8, 0)' , 'E726:' ) 3368call assert_fails ( 'let x=range(3, 1)' , 'E727:' ) 3369call assert_fails ( 'let x=range(1, 3, -2)' , 'E727:' ) 3370call assert_fails ( 'let x=range([])' , 'E745:' ) 3371call assert_fails ( 'let x=range(1, [])' , 'E745:' ) 3372call assert_fails ( 'let x=range(1, 4, [])' , 'E745:' ) 3373endfunc 3374 3375func Test_garbagecollect_now_fails () 3376let v: testing = 0 3377call assert_fails ( 'call test_garbagecollect_now()' , 'E1142:' ) 3378let v: testing = 1 3379endfunc 3380 3381" Test for echo highlighting 3382func Test_echohl () 3383echohl Search 3384echo 'Vim' 3385call assert_equal ( 'Vim' , Screenline ( & lines )) 3386" TODO: How to check the highlight group used by echohl? 3387" ScreenAttrs() returns all zeros. 3388echohl None 3389endfunc 3390 3391" Test for the eval() function 3392func Test_eval () 3393call assert_fails ( "call eval('5 a')" , 'E488:' ) 3394endfunc 3395 3396" Test for the keytrans() function 3397func Test_keytrans () 3398call assert_equal ( '<Space>' , keytrans ( ' ' )) 3399call assert_equal ( '<lt>' , keytrans ( '<' )) 3400call assert_equal ( '<lt>Tab>' , keytrans ( '<Tab>' )) 3401call assert_equal ( '<Tab>' , keytrans ( "\<Tab>" )) 3402call assert_equal ( '<C-V>' , keytrans ( "\<C-V>" )) 3403call assert_equal ( '<BS>' , keytrans ( "\<BS>" )) 3404call assert_equal ( '<Home>' , keytrans ( "\<Home>" )) 3405call assert_equal ( '<C-Home>' , keytrans ( "\<C-Home>" )) 3406call assert_equal ( '<M-Home>' , keytrans ( "\<M-Home>" )) 3407call assert_equal ( '<C-Space>' , keytrans ( "\<C-Space>" )) 3408call assert_equal ( '<M-Space>' , keytrans ( "\<*M-Space>" )) 3409call assert_equal ( '<M-x>' , "\<*M-x>" ->keytrans ()) 3410call assert_equal ( '<C-I>' , "\<*C-I>" ->keytrans ()) 3411call assert_equal ( '<S-3>' , "\<*S-3>" ->keytrans ()) 3412call assert_equal ( '<Bar>' , '|' ->keytrans ()) 3413call assert_equal ( '<M-Bar>' , "\<*M-|>" ->keytrans ()) 3414call assert_equal ( '<Bslash>' , '\' ->keytrans ()) 3415call assert_equal ( '<M-Bslash>' , "\<*M-\>" ->keytrans ()) 3416call assert_equal ( 'π' , 'π' ->keytrans ()) 3417call assert_equal ( '<M-π>' , "\<M-π>" ->keytrans ()) 3418call assert_equal ( 'ě' , 'ě' ->keytrans ()) 3419call assert_equal ( '<M-ě>' , "\<M-ě>" ->keytrans ()) 3420call assert_equal ( '' , '' ->keytrans ()) 3421call assert_equal ( '' , v: _null_string ->keytrans ()) 3422call assert_fails ( 'call keytrans(1)' , 'E1174:' ) 3423call assert_fails ( 'call keytrans()' , 'E119:' ) 3424endfunc 3425 3426" Test for the nr2char() function 3427func Test_nr2char () 3428" set encoding=latin1 3429call assert_equal ( '@' , nr2char ( 64 )) 3430set encoding = utf8 3431call assert_equal ( 'a' , nr2char ( 97 , 1 )) 3432call assert_equal ( 'a' , nr2char ( 97 , 0 )) 3433 3434call assert_equal ( "\x80\xfc\b" .. nr2char ( 0x100000 ), eval ( '"\<M-' .. nr2char ( 0x100000 ) .. '>"' )) 3435call assert_equal ( "\x80\xfc\b" .. nr2char ( 0x40000000 ), eval ( '"\<M-' .. nr2char ( 0x40000000 ) .. '>"' )) 3436endfunc 3437 3438" Test for screenattr(), screenchar() and screenchars() functions 3439func Test_screen_functions () 3440call assert_equal ( -1 , screenattr ( -1 , -1 )) 3441call assert_equal ( -1 , screenchar ( -1 , -1 )) 3442call assert_equal ([], screenchars ( -1 , -1 )) 3443 3444" Run this in a separate Vim instance to avoid messing up. 3445let after =<< trim [CODE] 3446scriptencoding utf-8 3447call setline(1, '口') 3448redraw 3449call assert_equal(0, screenattr(1, 1)) 3450call assert_equal(char2nr('口'), screenchar(1, 1)) 3451call assert_equal([char2nr('口')], screenchars(1, 1)) 3452call assert_equal('口', screenstring(1, 1)) 3453call writefile(v:errors, 'Xresult') 3454qall! 3455[CODE] 3456 3457let encodings = [ 'utf-8' , 'cp932' , 'cp936' , 'cp949' , 'cp950' ] 3458if ! has ( 'win32' ) 3459let encodings += [ 'euc-jp' ] 3460endif 3461if has ( 'nvim' ) 3462let encodings = [ 'utf-8' ] 3463endif 3464for enc in encodings 3465let msg = 'enc=' .. enc 3466if RunVim ([], after , $ '--clean --cmd "set encoding={enc}"') 3467call assert_equal ([], readfile ( 'Xresult' ), msg ) 3468endif 3469call delete ( 'Xresult' ) 3470endfor 3471endfunc 3472 3473" Test for getcurpos() and setpos() 3474func Test_getcurpos_setpos () 3475new 3476call setline ( 1 , [ '012345678' , '012345678' ]) 3477normal gg6l 3478let sp = getcurpos () 3479normal 0 3480call setpos ( '.' , sp ) 3481normal jyl 3482call assert_equal ( '6' , @") 3483call assert_equal ( -1 , setpos ( '.' , v: _null_list )) 3484call assert_equal ( -1 , setpos ( '.' , {})) 3485 3486let winid = win_getid () 3487normal G$ 3488let pos = getcurpos () 3489wincmd w 3490call assert_equal ( pos , getcurpos ( winid )) 3491 3492wincmd w 3493close ! 3494 3495call assert_equal ( getcurpos (), getcurpos ( 0 )) 3496call assert_equal ([ 0 , 0 , 0 , 0 , 0 ], getcurpos ( -1 )) 3497call assert_equal ([ 0 , 0 , 0 , 0 , 0 ], getcurpos ( 1999 )) 3498endfunc 3499 3500func Test_getmousepos () 3501enew ! 3502call setline ( 1 , "\t\t\t1234" ) 3503call Ntest_setmouse ( 1 , 1 ) 3504call assert_equal (#{ 3505 \screenrow : 1 , 3506 \screencol : 1 , 3507 \winid : win_getid (), 3508 \winrow : 1 , 3509 \wincol : 1 , 3510 \line : 1 , 3511 \column : 1 , 3512 \coladd : 0 , 3513 \}, getmousepos ()) 3514call Ntest_setmouse ( 1 , 2 ) 3515call assert_equal (#{ 3516 \screenrow : 1 , 3517 \screencol : 2 , 3518 \winid : win_getid (), 3519 \winrow : 1 , 3520 \wincol : 2 , 3521 \line : 1 , 3522 \column : 1 , 3523 \coladd : 1 , 3524 \}, getmousepos ()) 3525call Ntest_setmouse ( 1 , 8 ) 3526call assert_equal (#{ 3527 \screenrow : 1 , 3528 \screencol : 8 , 3529 \winid : win_getid (), 3530 \winrow : 1 , 3531 \wincol : 8 , 3532 \line : 1 , 3533 \column : 1 , 3534 \coladd : 7 , 3535 \}, getmousepos ()) 3536call Ntest_setmouse ( 1 , 9 ) 3537call assert_equal (#{ 3538 \screenrow : 1 , 3539 \screencol : 9 , 3540 \winid : win_getid (), 3541 \winrow : 1 , 3542 \wincol : 9 , 3543 \line : 1 , 3544 \column : 2 , 3545 \coladd : 0 , 3546 \}, getmousepos ()) 3547call Ntest_setmouse ( 1 , 12 ) 3548call assert_equal (#{ 3549 \screenrow : 1 , 3550 \screencol : 12 , 3551 \winid : win_getid (), 3552 \winrow : 1 , 3553 \wincol : 12 , 3554 \line : 1 , 3555 \column : 2 , 3556 \coladd : 3 , 3557 \}, getmousepos ()) 3558call Ntest_setmouse ( 1 , 25 ) 3559call assert_equal (#{ 3560 \screenrow : 1 , 3561 \screencol : 25 , 3562 \winid : win_getid (), 3563 \winrow : 1 , 3564 \wincol : 25 , 3565 \line : 1 , 3566 \column : 4 , 3567 \coladd : 0 , 3568 \}, getmousepos ()) 3569call Ntest_setmouse ( 1 , 28 ) 3570call assert_equal (#{ 3571 \screenrow : 1 , 3572 \screencol : 28 , 3573 \winid : win_getid (), 3574 \winrow : 1 , 3575 \wincol : 28 , 3576 \line : 1 , 3577 \column : 7 , 3578 \coladd : 0 , 3579 \}, getmousepos ()) 3580call Ntest_setmouse ( 1 , 29 ) 3581call assert_equal (#{ 3582 \screenrow : 1 , 3583 \screencol : 29 , 3584 \winid : win_getid (), 3585 \winrow : 1 , 3586 \wincol : 29 , 3587 \line : 1 , 3588 \column : 8 , 3589 \coladd : 0 , 3590 \}, getmousepos ()) 3591call Ntest_setmouse ( 1 , 50 ) 3592call assert_equal (#{ 3593 \screenrow : 1 , 3594 \screencol : 50 , 3595 \winid : win_getid (), 3596 \winrow : 1 , 3597 \wincol : 50 , 3598 \line : 1 , 3599 \column : 8 , 3600 \coladd : 21 , 3601 \}, getmousepos ()) 3602 3603" If the mouse is positioned past the last buffer line, "line" and "column" 3604" should act like it's positioned on the last buffer line. 3605call Ntest_setmouse ( 2 , 25 ) 3606call assert_equal (#{ 3607 \screenrow : 2 , 3608 \screencol : 25 , 3609 \winid : win_getid (), 3610 \winrow : 2 , 3611 \wincol : 25 , 3612 \line : 1 , 3613 \column : 4 , 3614 \coladd : 0 , 3615 \}, getmousepos ()) 3616call Ntest_setmouse ( 2 , 50 ) 3617call assert_equal (#{ 3618 \screenrow : 2 , 3619 \screencol : 50 , 3620 \winid : win_getid (), 3621 \winrow : 2 , 3622 \wincol : 50 , 3623 \line : 1 , 3624 \column : 8 , 3625 \coladd : 21 , 3626 \}, getmousepos ()) 3627 362830 vnew 3629setlocal smoothscroll number 3630call setline ( 1 , join ( range ( 100 ))) 3631exe "normal! \<C-E>" 3632call Ntest_setmouse ( 1 , 5 ) 3633call assert_equal (#{ 3634 \screenrow : 1 , 3635 \screencol : 5 , 3636 \winid : win_getid (), 3637 \winrow : 1 , 3638 \wincol : 5 , 3639 \line : 1 , 3640 \column : 27 , 3641 \coladd : 0 , 3642 \}, getmousepos ()) 3643call Ntest_setmouse ( 2 , 5 ) 3644call assert_equal (#{ 3645 \screenrow : 2 , 3646 \screencol : 5 , 3647 \winid : win_getid (), 3648 \winrow : 2 , 3649 \wincol : 5 , 3650 \line : 1 , 3651 \column : 53 , 3652 \coladd : 0 , 3653 \}, getmousepos ()) 3654 3655exe "normal! \<C-E>" 3656call Ntest_setmouse ( 1 , 5 ) 3657call assert_equal (#{ 3658 \screenrow : 1 , 3659 \screencol : 5 , 3660 \winid : win_getid (), 3661 \winrow : 1 , 3662 \wincol : 5 , 3663 \line : 1 , 3664 \column : 53 , 3665 \coladd : 0 , 3666 \}, getmousepos ()) 3667call Ntest_setmouse ( 2 , 5 ) 3668call assert_equal (#{ 3669 \screenrow : 2 , 3670 \screencol : 5 , 3671 \winid : win_getid (), 3672 \winrow : 2 , 3673 \wincol : 5 , 3674 \line : 1 , 3675 \column : 79 , 3676 \coladd : 0 , 3677 \}, getmousepos ()) 3678 3679vert resize 4 3680call Ntest_setmouse ( 2 , 2 ) 3681" This used to crash Vim 3682call assert_equal (#{ 3683 \screenrow : 2 , 3684 \screencol : 2 , 3685 \winid : win_getid (), 3686 \winrow : 2 , 3687 \wincol : 2 , 3688 \line : 1 , 3689 \column : 53 , 3690 \coladd : 0 , 3691 \}, getmousepos ()) 3692 3693bwipe ! 3694bwipe ! 3695endfunc 3696 3697" Test for glob() 3698func Test_glob () 3699call assert_equal ( '' , glob ( v: _null_string )) 3700call assert_equal ( '' , globpath ( v: _null_string , v: _null_string )) 3701call assert_fails ( "let x = globpath(&rtp, 'syntax/c.vim', [])" , 'E745:' ) 3702 3703call writefile ([], 'Xglob1' ) 3704call writefile ([], 'XGLOB2' ) 3705set wildignorecase 3706" Sort output of glob() otherwise we end up with different 3707" ordering depending on whether file system is case-sensitive. 3708call assert_equal ([ 'XGLOB2' , 'Xglob1' ], sort ( glob ( 'Xglob[12]' , 0 , 1 ))) 3709" wildignorecase shall be applied even when the pattern contains no wildcards. 3710call assert_equal ( 'XGLOB2' , glob ( 'xglob2' )) 3711set wildignorecase & 3712 3713call delete ( 'Xglob1' ) 3714call delete ( 'XGLOB2' ) 3715 3716call assert_fails ( "call glob('*', 0, {})" , 'E728:' ) 3717endfunc 3718 3719func Test_glob2 () 3720call mkdir ( '[XglobDir]' , 'R' ) 3721call mkdir ( 'abc[glob]def' , 'R' ) 3722 3723call writefile ([ 'glob' ], '[XglobDir]/Xglob' ) 3724call writefile ([ 'glob' ], 'abc[glob]def/Xglob' ) 3725if has ( "unix" ) 3726call assert_equal ([], ( glob ( '[XglobDir]/*' , 0 , 1 ))) 3727call assert_equal ([], ( glob ( 'abc[glob]def/*' , 0 , 1 ))) 3728call assert_equal ([ '[XglobDir]/Xglob' ], ( glob ( '\[XglobDir]/*' , 0 , 1 ))) 3729call assert_equal ([ 'abc[glob]def/Xglob' ], ( glob ( 'abc\[glob]def/*' , 0 , 1 ))) 3730elseif has ( "win32" ) 3731let _sl =& shellslash 3732call assert_equal ([], ( glob ( '[XglobDir]\*' , 0 , 1 ))) 3733call assert_equal ([], ( glob ( 'abc[glob]def\*' , 0 , 1 ))) 3734call assert_equal ([], ( glob ( '\[XglobDir]\*' , 0 , 1 ))) 3735call assert_equal ([], ( glob ( 'abc\[glob]def\*' , 0 , 1 ))) 3736" Test that changing 'shellslash' doesn't affect the result of glob() 3737set noshellslash 3738call assert_equal ([ '[XglobDir]/Xglob' ], ( glob ( '[[]XglobDir]/*' , 0 , 1 ))) 3739call assert_equal ([ 'abc[glob]def/Xglob' ], ( glob ( 'abc[[]glob]def/*' , 0 , 1 ))) 3740set shellslash 3741call assert_equal ([ '[XglobDir]/Xglob' ], ( glob ( '[[]XglobDir]/*' , 0 , 1 ))) 3742call assert_equal ([ 'abc[glob]def/Xglob' ], ( glob ( 'abc[[]glob]def/*' , 0 , 1 ))) 3743let & shellslash = _sl 3744endif 3745endfunc 3746 3747func Test_glob_symlinks () 3748call writefile ([], 'Xglob1' ) 3749 3750if has ( "win32" ) 3751silent !mklinkXglobBad DoesNotExist 3752if v: shell_error 3753throw 'Skipped: cannot create symlinks' 3754endif 3755silent !mklinkXglobOk Xglob1 3756else 3757silent !ln -sDoesNotExist XglobBad 3758silent !ln -sXglob1 XglobOk 3759endif 3760 3761" The broken symlink is excluded when alllinks is false. 3762call assert_equal ([ 'Xglob1' , 'XglobBad' , 'XglobOk' ], sort ( glob ( 'Xglob*' , 0 , 1 , 1 ))) 3763call assert_equal ([ 'Xglob1' , 'XglobOk' ], sort ( glob ( 'Xglob*' , 0 , 1 , 0 ))) 3764 3765call delete ( 'Xglob1' ) 3766call delete ( 'XglobBad' ) 3767call delete ( 'XglobOk' ) 3768endfunc 3769 3770" Test for browse() 3771func Test_browse () 3772CheckFeature browse 3773call assert_fails ( 'call browse([], "open", "x", "a.c")' , 'E745:' ) 3774endfunc 3775 3776" Test for browsedir() 3777func Test_browsedir () 3778CheckFeature browse 3779call assert_fails ( 'call browsedir("open", [])' , 'E730:' ) 3780endfunc 3781 3782func HasDefault ( msg = 'msg' ) 3783return a: msg 3784endfunc 3785 3786func Test_default_arg_value () 3787call assert_equal ( 'msg' , HasDefault ()) 3788endfunc 3789 3790" Test for gettext() 3791func Test_gettext () 3792call assert_fails ( 'call gettext(1)' , 'E1174:' ) 3793endfunc 3794 3795func Test_builtin_check () 3796call assert_fails ( 'let g:["trim"] = {x -> " " .. x}' , 'E704:' ) 3797call assert_fails ( 'let g:.trim = {x -> " " .. x}' , 'E704:' ) 3798call assert_fails ( 'let l:["trim"] = {x -> " " .. x}' , 'E704:' ) 3799call assert_fails ( 'let l:.trim = {x -> " " .. x}' , 'E704:' ) 3800let lines =<< trim END 3801vim9script 3802var trim = (x) => " " .. x 3803END 3804call CheckScriptFailure ( lines , 'E704:' ) 3805 3806call assert_fails ( 'call extend(g:, #{foo: { -> "foo" }})' , 'E704:' ) 3807let g: bar = 123 3808call extend ( g:, #{ bar : { ->"foo" }}, "keep" ) 3809call assert_fails ( 'call extend(g:, #{bar: { -> "foo" }}, "force")' , 'E704:' ) 3810unlet g: bar 3811 3812call assert_fails ( 'call extend(l:, #{foo: { -> "foo" }})' , 'E704:' ) 3813let bar = 123 3814call extend ( l:, #{ bar : { ->"foo" }}, "keep" ) 3815call assert_fails ( 'call extend(l:, #{bar: { -> "foo" }}, "force")' , 'E704:' ) 3816unlet bar 3817 3818call assert_fails ( 'call extend(g:, #{foo: function("extend")})' , 'E704:' ) 3819let g: bar = 123 3820call extend ( g:, #{ bar : function ( "extend" )}, "keep" ) 3821call assert_fails ( 'call extend(g:, #{bar: function("extend")}, "force")' , 'E704:' ) 3822unlet g: bar 3823 3824call assert_fails ( 'call extend(l:, #{foo: function("extend")})' , 'E704:' ) 3825let bar = 123 3826call extend ( l:, #{ bar : function ( "extend" )}, "keep" ) 3827call assert_fails ( 'call extend(l:, #{bar: function("extend")}, "force")' , 'E704:' ) 3828unlet bar 3829endfunc 3830 3831func Test_funcref_to_string () 3832let Fn = funcref ( 'g:Test_funcref_to_string' ) 3833call assert_equal ( "function('g:Test_funcref_to_string')" , string ( Fn )) 3834endfunc 3835 3836" Test for isabsolutepath() 3837func Test_isabsolutepath () 3838call assert_false ( isabsolutepath ( '' )) 3839call assert_false ( isabsolutepath ( '.' )) 3840call assert_false ( isabsolutepath ( '../Foo' )) 3841call assert_false ( isabsolutepath ( 'Foo/' )) 3842if has ( 'win32' ) 3843call assert_true ( isabsolutepath ( 'A:\' )) 3844call assert_true ( isabsolutepath ( 'A:\Foo' )) 3845call assert_true ( isabsolutepath ( 'A:/Foo' )) 3846call assert_false ( isabsolutepath ( 'A:Foo' )) 3847call assert_true ( isabsolutepath ( '\Windows' )) 3848call assert_true ( isabsolutepath ( '/Windows' )) 3849call assert_true ( isabsolutepath ( '\\Server2\Share\Test\Foo.txt' )) 3850else 3851call assert_true ( isabsolutepath ( '/' )) 3852call assert_true ( isabsolutepath ( '/usr/share/' )) 3853endif 3854endfunc 3855 3856" Test for exepath() 3857func Test_exepath () 3858if has ( 'win32' ) 3859call assert_notequal ( exepath ( 'cmd' ), '' ) 3860 3861let oldNoDefaultCurrentDirectoryInExePath = $ NoDefaultCurrentDirectoryInExePath 3862call writefile ([ '@echo off' , 'echo Evil' ], 'vim-test-evil.bat' ) 3863let $ NoDefaultCurrentDirectoryInExePath = '' 3864call assert_notequal ( exepath ( "vim-test-evil.bat" ), '' ) 3865let $ NoDefaultCurrentDirectoryInExePath = '1' 3866call assert_equal ( exepath ( "vim-test-evil.bat" ), '' ) 3867let $ NoDefaultCurrentDirectoryInExePath = oldNoDefaultCurrentDirectoryInExePath 3868call delete ( 'vim-test-evil.bat' ) 3869else 3870call assert_notequal ( exepath ( 'sh' ), '' ) 3871endif 3872endfunc 3873 3874" Test for virtcol() 3875func Test_virtcol () 3876new 3877call setline ( 1 , "the\tquick\tbrown\tfox" ) 3878norm ! 4| 3879call assert_equal ( 8 , virtcol ( '.' )) 3880call assert_equal ( 8 , virtcol ( '.' , v: false )) 3881call assert_equal ([ 4 , 8 ], virtcol ( '.' , v: true )) 3882 3883let w = winwidth ( 0 ) 3884call setline ( 2 , repeat ( 'a' , w + 2 )) 3885let win_nosbr = win_getid () 3886split 3887setlocal showbreak = !! 3888let win_sbr = win_getid () 3889call assert_equal ([ w , w ], virtcol ([ 2 , w ], v: true , win_nosbr )) 3890call assert_equal ([ w + 1 , w + 1 ], virtcol ([ 2 , w + 1 ], v: true , win_nosbr )) 3891call assert_equal ([ w + 2 , w + 2 ], virtcol ([ 2 , w + 2 ], v: true , win_nosbr )) 3892call assert_equal ([ w , w ], virtcol ([ 2 , w ], v: true , win_sbr )) 3893call assert_equal ([ w + 3 , w + 3 ], virtcol ([ 2 , w + 1 ], v: true , win_sbr )) 3894call assert_equal ([ w + 4 , w + 4 ], virtcol ([ 2 , w + 2 ], v: true , win_sbr )) 3895close 3896 3897call assert_equal ( 0 , virtcol ( '' )) 3898call assert_equal ([ 0 , 0 ], virtcol ( '' , v: true )) 3899call assert_equal ( 0 , virtcol ( '.' , v: false , 5001 )) 3900call assert_equal ([ 0 , 0 ], virtcol ( '.' , v: true , 5001 )) 3901 3902bwipe ! 3903endfunc 3904 3905func Test_delfunc_while_listing () 3906CheckRunVimInTerminal 3907 3908let lines =<< trim END 3909set nocompatible 3910for i in range(1, 999) 3911exe 'func ' .. 'MyFunc' .. i .. '()' 3912endfunc 3913endfor 3914au CmdlineLeave : call timer_start(0, {-> execute('delfunc MyFunc622')}) 3915END 3916call writefile ( lines , 'Xfunctionclear' , 'D' ) 3917let buf = RunVimInTerminal ( '-S Xfunctionclear' , { 'rows' : 12 }) 3918 3919" This was using freed memory. The height of the terminal must be so that 3920" the next function to be listed with "j" is the one that is deleted in the 3921" timer callback, tricky! 3922call term_sendkeys ( buf , ":func /MyFunc\<CR>" ) 3923call TermWait ( buf , 50 ) 3924call term_sendkeys ( buf , "j" ) 3925call TermWait ( buf , 50 ) 3926call term_sendkeys ( buf , "\<CR>" ) 3927 3928call StopVimInTerminal ( buf ) 3929endfunc 3930 3931" Test for the reverse() function with a string 3932func Test_string_reverse () 3933let lines =<< trim END 3934call assert_equal('', reverse(v:_null_string)) 3935for [s1, s2] in [['', ''], ['a', 'a'], ['ab', 'ba'], ['abc', 'cba'], 3936\ ['abcd', 'dcba'], ['«-«-»-»', '»-»-«-«'], 3937\ ['🇦', '🇦'], ['🇦🇧', '🇦🇧'], ['🇦🇧🇨', '🇨🇦🇧'], 3938\ ['🇦«🇧-🇨»🇩', '🇩»🇨-🇧«🇦']] 3939call assert_equal(s2, reverse(s1)) 3940endfor 3941END 3942call CheckLegacyAndVim9Success ( lines ) 3943 3944" test in latin1 encoding 3945let save_enc = & encoding 3946" set encoding=latin1 3947call assert_equal ( 'dcba' , reverse ( 'abcd' )) 3948let & encoding = save_enc 3949endfunc 3950 3951func Test_fullcommand () 3952" this used to crash vim 3953call assert_equal ( '' , fullcommand ( 10 )) 3954endfunc 3955 3956" Test for glob() with shell special patterns 3957func Test_glob_extended_bash () 3958CheckExecutable bash 3959CheckNotMSWindows 3960CheckNotMac " The default version of bash is old on macOS. 3961 3962let _shell = & shell 3963set shell = bash 3964 3965call mkdir ( 'Xtestglob/foo/bar/src' , 'p' ) 3966call writefile ([], 'Xtestglob/foo/bar/src/foo.sh' ) 3967call writefile ([], 'Xtestglob/foo/bar/src/foo.h' ) 3968call writefile ([], 'Xtestglob/foo/bar/src/foo.cpp' ) 3969 3970" Sort output of glob() otherwise we end up with different 3971" ordering depending on whether file system is case-sensitive. 3972let expected = [ 'Xtestglob/foo/bar/src/foo.cpp' , 'Xtestglob/foo/bar/src/foo.h' ] 3973call assert_equal ( expected , sort ( glob ( 'Xtestglob/**/foo.{h,cpp}' , 0 , 1 ))) 3974call delete ( 'Xtestglob' , 'rf' ) 3975let & shell = _shell 3976endfunc 3977 3978" Test for glob() with extended patterns (MS-Windows) 3979" Vim doesn't use 'shell' to expand wildcards on MS-Windows. 3980" Unlike bash, it doesn't support {,} expansion. 3981func Test_glob_extended_mswin () 3982CheckMSWindows 3983 3984call mkdir ( 'Xtestglob/foo/bar/src' , 'p' ) 3985call writefile ([], 'Xtestglob/foo/bar/src/foo.sh' ) 3986call writefile ([], 'Xtestglob/foo/bar/src/foo.h' ) 3987call writefile ([], 'Xtestglob/foo/bar/src/foo.cpp' ) 3988 3989" Sort output of glob() otherwise we end up with different 3990" ordering depending on whether file system is case-sensitive. 3991let expected = [ 'Xtestglob/foo/bar/src/foo.cpp' , 'Xtestglob/foo/bar/src/foo.h' , 'Xtestglob/foo/bar/src/foo.sh' ] 3992call assert_equal ( expected , sort ( glob ( 'Xtestglob/**/foo.*' , 0 , 1 ))) 3993call delete ( 'Xtestglob' , 'rf' ) 3994endfunc 3995 3996" Tests for the slice() function. 3997func Test_slice () 3998let lines =<< trim END 3999call assert_equal([1, 2, 3, 4, 5], slice(range(6), 1)) 4000call assert_equal([2, 3, 4, 5], slice(range(6), 2)) 4001call assert_equal([2, 3], slice(range(6), 2, 4)) 4002call assert_equal([0, 1, 2, 3], slice(range(6), 0, 4)) 4003call assert_equal([1, 2, 3], slice(range(6), 1, 4)) 4004call assert_equal([1, 2, 3, 4], slice(range(6), 1, -1)) 4005call assert_equal([1, 2], slice(range(6), 1, -3)) 4006call assert_equal([1], slice(range(6), 1, -4)) 4007call assert_equal([], slice(range(6), 1, -5)) 4008call assert_equal([], slice(range(6), 1, -6)) 4009 4010call assert_equal(0z1122334455, slice(0z001122334455, 1)) 4011call assert_equal(0z22334455, slice(0z001122334455, 2)) 4012call assert_equal(0z2233, slice(0z001122334455, 2, 4)) 4013call assert_equal(0z00112233, slice(0z001122334455, 0, 4)) 4014call assert_equal(0z112233, slice(0z001122334455, 1, 4)) 4015call assert_equal(0z11223344, slice(0z001122334455, 1, -1)) 4016call assert_equal(0z1122, slice(0z001122334455, 1, -3)) 4017call assert_equal(0z11, slice(0z001122334455, 1, -4)) 4018call assert_equal(0z, slice(0z001122334455, 1, -5)) 4019call assert_equal(0z, slice(0z001122334455, 1, -6)) 4020 4021call assert_equal('12345', slice('012345', 1)) 4022call assert_equal('2345', slice('012345', 2)) 4023call assert_equal('23', slice('012345', 2, 4)) 4024call assert_equal('0123', slice('012345', 0, 4)) 4025call assert_equal('123', slice('012345', 1, 4)) 4026call assert_equal('1234', slice('012345', 1, -1)) 4027call assert_equal('12', slice('012345', 1, -3)) 4028call assert_equal('1', slice('012345', 1, -4)) 4029call assert_equal('', slice('012345', 1, -5)) 4030call assert_equal('', slice('012345', 1, -6)) 4031 4032#" Composing chars are treated as a part of the preceding base char. 4033call assert_equal('β̳́γ̳̂δ̳̃ε̳̄ζ̳̅', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1)) 4034call assert_equal('γ̳̂δ̳̃ε̳̄ζ̳̅', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(2)) 4035call assert_equal('γ̳̂δ̳̃', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(2, 4)) 4036call assert_equal('ὰ̳β̳́γ̳̂δ̳̃', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(0, 4)) 4037call assert_equal('β̳́γ̳̂δ̳̃', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, 4)) 4038call assert_equal('β̳́γ̳̂δ̳̃ε̳̄', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -1)) 4039call assert_equal('β̳́γ̳̂', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -3)) 4040call assert_equal('β̳́', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -4)) 4041call assert_equal('', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -5)) 4042call assert_equal('', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -6)) 4043END 4044call CheckLegacyAndVim9Success ( lines ) 4045 4046call assert_equal ( 0 , slice ( v: true , 1 )) 4047endfunc 4048 4049" vim: shiftwidth=2 sts=2 expandtab