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() 11 call assert_equal(0, bufexists('does_not_exist')) 12 call assert_equal(1, bufexists(bufnr('%'))) 13 call assert_equal(0, bufexists(0)) 14 new Xfoo 15 let bn = bufnr('%') 16 call assert_equal(1, bufexists(bn)) 17 call assert_equal(1, bufexists('Xfoo')) 18 call assert_equal(1, bufexists(getcwd() . '/Xfoo')) 19 call assert_equal(1, bufexists(0)) 20 bw 21 call assert_equal(0, bufexists(bn)) 22 call assert_equal(0, bufexists('Xfoo')) 23endfunc 24 25func Test_has() 26 call assert_equal(1, has('eval')) 27 call assert_equal(1, has('eval', 1)) 28 29 if has('unix') 30 call assert_equal(1, or(has('ttyin'), 1)) 31 call assert_equal(0, and(has('ttyout'), 0)) 32 call assert_equal(1, has('multi_byte_encoding')) 33 endif 34 call assert_equal(1, has('vcon', 1)) 35 call assert_equal(1, has('mouse_gpm_enabled', 1)) 36 37 call assert_equal(0, has('nonexistent')) 38 call assert_equal(0, has('nonexistent', 1)) 39 40 " Will we ever have patch 9999? 41 let ver = 'patch-' .. v:version / 100 .. '.' .. v:version % 100 .. '.9999' 42 call assert_equal(0, has(ver)) 43endfunc 44 45func Test_empty() 46 call assert_equal(1, empty('')) 47 call assert_equal(0, empty('a')) 48 49 call assert_equal(1, empty(0)) 50 call assert_equal(1, empty(-0)) 51 call assert_equal(0, empty(1)) 52 call assert_equal(0, empty(-1)) 53 54 if has('float') 55 call assert_equal(1, empty(0.0)) 56 call assert_equal(1, empty(-0.0)) 57 call assert_equal(0, empty(1.0)) 58 call assert_equal(0, empty(-1.0)) 59 call assert_equal(0, empty(1.0/0.0)) 60 call assert_equal(0, empty(0.0/0.0)) 61 endif 62 63 call assert_equal(1, empty([])) 64 call assert_equal(0, empty(['a'])) 65 66 call assert_equal(1, empty({})) 67 call assert_equal(0, empty({'a':1})) 68 69 call assert_equal(1, empty(v:null)) 70 call assert_equal(1, empty(v:none)) 71 call assert_equal(1, empty(v:false)) 72 call assert_equal(0, empty(v:true)) 73 74 if has('channel') 75 call assert_equal(1, empty(test_null_channel())) 76 endif 77 if has('job') 78 call assert_equal(1, empty(test_null_job())) 79 endif 80 81 call assert_equal(0, empty(function('Test_empty'))) 82 call assert_equal(0, empty(function('Test_empty', [0]))) 83 84 call assert_fails("call empty(test_void())", 'E685:') 85 call assert_fails("call empty(test_unknown())", 'E685:') 86endfunc 87 88func Test_test_void() 89 call assert_fails('echo 1 == test_void()', 'E1031:') 90 if has('float') 91 call assert_fails('echo 1.0 == test_void()', 'E1031:') 92 endif 93 call assert_fails('let x = json_encode(test_void())', 'E685:') 94 call assert_fails('let x = copy(test_void())', 'E685:') 95 call assert_fails('let x = copy([test_void()])', 'E1031:') 96endfunc 97 98func Test_islocked() 99 call assert_fails('call islocked(99)', 'E475:') 100 call assert_fails('call islocked("s: x")', 'E488:') 101endfunc 102 103func Test_len() 104 call assert_equal(1, len(0)) 105 call assert_equal(2, len(12)) 106 107 call assert_equal(0, len('')) 108 call assert_equal(2, len('ab')) 109 110 call assert_equal(0, len([])) 111 call assert_equal(0, len(test_null_list())) 112 call assert_equal(2, len([2, 1])) 113 114 call assert_equal(0, len({})) 115 call assert_equal(0, len(test_null_dict())) 116 call assert_equal(2, len({'a': 1, 'b': 2})) 117 118 call assert_fails('call len(v:none)', 'E701:') 119 call assert_fails('call len({-> 0})', 'E701:') 120endfunc 121 122func Test_max() 123 call assert_equal(0, max([])) 124 call assert_equal(2, max([2])) 125 call assert_equal(2, max([1, 2])) 126 call assert_equal(2, max([1, 2, v:null])) 127 128 call assert_equal(0, max({})) 129 call assert_equal(2, max({'a':1, 'b':2})) 130 131 call assert_fails('call max(1)', 'E712:') 132 call assert_fails('call max(v:none)', 'E712:') 133 134 " check we only get one error 135 call assert_fails('call max([#{}, [1]])', ['E728:', 'E728:']) 136 call assert_fails('call max(#{a: {}, b: [1]})', ['E728:', 'E728:']) 137endfunc 138 139func Test_min() 140 call assert_equal(0, min([])) 141 call assert_equal(2, min([2])) 142 call assert_equal(1, min([1, 2])) 143 call assert_equal(0, min([1, 2, v:null])) 144 145 call assert_equal(0, min({})) 146 call assert_equal(1, min({'a':1, 'b':2})) 147 148 call assert_fails('call min(1)', 'E712:') 149 call assert_fails('call min(v:none)', 'E712:') 150 call assert_fails('call min([1, {}])', 'E728:') 151 152 " check we only get one error 153 call assert_fails('call min([[1], #{}])', ['E745:', 'E745:']) 154 call assert_fails('call min(#{a: [1], b: #{}})', ['E745:', 'E745:']) 155endfunc 156 157func Test_strwidth() 158 for aw in ['single', 'double'] 159 exe 'set ambiwidth=' . aw 160 call assert_equal(0, strwidth('')) 161 call assert_equal(1, strwidth("\t")) 162 call assert_equal(3, strwidth('Vim')) 163 call assert_equal(4, strwidth(1234)) 164 call assert_equal(5, strwidth(-1234)) 165 166 call assert_equal(2, strwidth('')) 167 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde')) 168 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße')) 169 170 call assert_fails('call strwidth({->0})', 'E729:') 171 call assert_fails('call strwidth([])', 'E730:') 172 call assert_fails('call strwidth({})', 'E731:') 173 endfor 174 175 if has('float') 176 call assert_equal(3, strwidth(1.2)) 177 call CheckDefAndScriptFailure2(['echo strwidth(1.2)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1') 178 endif 179 180 set ambiwidth& 181endfunc 182 183func Test_str2nr() 184 call assert_equal(0, str2nr('')) 185 call assert_equal(1, str2nr('1')) 186 call assert_equal(1, str2nr(' 1 ')) 187 188 call assert_equal(1, str2nr('+1')) 189 call assert_equal(1, str2nr('+ 1')) 190 call assert_equal(1, str2nr(' + 1 ')) 191 192 call assert_equal(-1, str2nr('-1')) 193 call assert_equal(-1, str2nr('- 1')) 194 call assert_equal(-1, str2nr(' - 1 ')) 195 196 call assert_equal(123456789, str2nr('123456789')) 197 call assert_equal(-123456789, str2nr('-123456789')) 198 199 call assert_equal(5, str2nr('101', 2)) 200 call assert_equal(5, '0b101'->str2nr(2)) 201 call assert_equal(5, str2nr('0B101', 2)) 202 call assert_equal(-5, str2nr('-101', 2)) 203 call assert_equal(-5, str2nr('-0b101', 2)) 204 call assert_equal(-5, str2nr('-0B101', 2)) 205 206 call assert_equal(65, str2nr('101', 8)) 207 call assert_equal(65, str2nr('0101', 8)) 208 call assert_equal(-65, str2nr('-101', 8)) 209 call assert_equal(-65, str2nr('-0101', 8)) 210 call assert_equal(65, str2nr('0o101', 8)) 211 call assert_equal(65, str2nr('0O0101', 8)) 212 call assert_equal(-65, str2nr('-0O101', 8)) 213 call assert_equal(-65, str2nr('-0o0101', 8)) 214 215 call assert_equal(11259375, str2nr('abcdef', 16)) 216 call assert_equal(11259375, str2nr('ABCDEF', 16)) 217 call assert_equal(-11259375, str2nr('-ABCDEF', 16)) 218 call assert_equal(11259375, str2nr('0xabcdef', 16)) 219 call assert_equal(11259375, str2nr('0Xabcdef', 16)) 220 call assert_equal(11259375, str2nr('0XABCDEF', 16)) 221 call assert_equal(-11259375, str2nr('-0xABCDEF', 16)) 222 223 call assert_equal(1, str2nr("1'000'000", 10, 0)) 224 call assert_equal(256, str2nr("1'0000'0000", 2, 1)) 225 call assert_equal(262144, str2nr("1'000'000", 8, 1)) 226 call assert_equal(1000000, str2nr("1'000'000", 10, 1)) 227 call assert_equal(1000, str2nr("1'000''000", 10, 1)) 228 call assert_equal(65536, str2nr("1'00'00", 16, 1)) 229 230 call assert_equal(0, str2nr('0x10')) 231 call assert_equal(0, str2nr('0b10')) 232 call assert_equal(0, str2nr('0o10')) 233 call assert_equal(1, str2nr('12', 2)) 234 call assert_equal(1, str2nr('18', 8)) 235 call assert_equal(1, str2nr('1g', 16)) 236 237 call assert_equal(0, str2nr(v:null)) 238 call assert_equal(0, str2nr(v:none)) 239 240 call assert_fails('call str2nr([])', 'E730:') 241 call assert_fails('call str2nr({->2})', 'E729:') 242 if has('float') 243 call assert_equal(1, str2nr(1.2)) 244 call CheckDefAndScriptFailure2(['echo str2nr(1.2)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1') 245 endif 246 call assert_fails('call str2nr(10, [])', 'E745:') 247endfunc 248 249func Test_strftime() 250 CheckFunction strftime 251 252 " Format of strftime() depends on system. We assume 253 " that basic formats tested here are available and 254 " identical on all systems which support strftime(). 255 " 256 " The 2nd parameter of strftime() is a local time, so the output day 257 " of strftime() can be 17 or 18, depending on timezone. 258 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512)) 259 " 260 call 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()) 261 262 call assert_fails('call strftime([])', 'E730:') 263 call assert_fails('call strftime("%Y", [])', 'E745:') 264 265 " Check that the time changes after we change the timezone 266 " Save previous timezone value, if any 267 if exists('$TZ') 268 let tz = $TZ 269 endif 270 271 " Force EST and then UTC, save the current hour (24-hour clock) for each 272 let $TZ = 'EST' | let est = strftime('%H') 273 let $TZ = 'UTC' | let utc = strftime('%H') 274 275 " Those hours should be two bytes long, and should not be the same; if they 276 " are, a tzset(3) call may have failed somewhere 277 call assert_equal(strlen(est), 2) 278 call assert_equal(strlen(utc), 2) 279 " TODO: this fails on MS-Windows 280 if has('unix') 281 call assert_notequal(est, utc) 282 endif 283 284 " If we cached a timezone value, put it back, otherwise clear it 285 if exists('tz') 286 let $TZ = tz 287 else 288 unlet $TZ 289 endif 290endfunc 291 292func Test_strptime() 293 CheckFunction strptime 294 295 if exists('$TZ') 296 let tz = $TZ 297 endif 298 let $TZ = 'UTC' 299 300 call assert_equal(1484653763, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23')) 301 302 " Force DST and check that it's considered 303 let $TZ = 'WINTER0SUMMER,J1,J365' 304 call assert_equal(1484653763 - 3600, strptime('%Y-%m-%d %T', '2017-01-17 11:49:23')) 305 306 call assert_fails('call strptime()', 'E119:') 307 call assert_fails('call strptime("xxx")', 'E119:') 308 call assert_equal(0, strptime("%Y", '')) 309 call assert_equal(0, strptime("%Y", "xxx")) 310 311 if exists('tz') 312 let $TZ = tz 313 else 314 unlet $TZ 315 endif 316endfunc 317 318func Test_resolve_unix() 319 CheckUnix 320 321 " Xlink1 -> Xlink2 322 " Xlink2 -> Xlink3 323 silent !ln -s -f Xlink2 Xlink1 324 silent !ln -s -f Xlink3 Xlink2 325 call assert_equal('Xlink3', resolve('Xlink1')) 326 call assert_equal('./Xlink3', resolve('./Xlink1')) 327 call assert_equal('Xlink3/', resolve('Xlink2/')) 328 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?! 329 "call assert_equal('Xlink3/', resolve('Xlink1/')) 330 "call assert_equal('./Xlink3/', resolve('./Xlink1/')) 331 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/')) 332 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1')) 333 334 " Test resolve() with a symlink cycle. 335 " Xlink1 -> Xlink2 336 " Xlink2 -> Xlink3 337 " Xlink3 -> Xlink1 338 silent !ln -s -f Xlink1 Xlink3 339 call assert_fails('call resolve("Xlink1")', 'E655:') 340 call assert_fails('call resolve("./Xlink1")', 'E655:') 341 call assert_fails('call resolve("Xlink2")', 'E655:') 342 call assert_fails('call resolve("Xlink3")', 'E655:') 343 call delete('Xlink1') 344 call delete('Xlink2') 345 call delete('Xlink3') 346 347 silent !ln -s -f Xdir//Xfile Xlink 348 call assert_equal('Xdir/Xfile', resolve('Xlink')) 349 call delete('Xlink') 350 351 silent !ln -s -f Xlink2/ Xlink1 352 call assert_equal('Xlink2', 'Xlink1'->resolve()) 353 call assert_equal('Xlink2/', resolve('Xlink1/')) 354 call delete('Xlink1') 355 356 silent !ln -s -f ./Xlink2 Xlink1 357 call assert_equal('Xlink2', resolve('Xlink1')) 358 call assert_equal('./Xlink2', resolve('./Xlink1')) 359 call delete('Xlink1') 360 361 call assert_equal('/', resolve('/')) 362endfunc 363 364func s:normalize_fname(fname) 365 let ret = substitute(a:fname, '\', '/', 'g') 366 let ret = substitute(ret, '//', '/', 'g') 367 return ret->tolower() 368endfunc 369 370func Test_resolve_win32() 371 CheckMSWindows 372 373 " test for shortcut file 374 if executable('cscript') 375 new Xfile 376 wq 377 let lines =<< trim END 378 Set fs = CreateObject("Scripting.FileSystemObject") 379 Set ws = WScript.CreateObject("WScript.Shell") 380 Set shortcut = ws.CreateShortcut("Xlink.lnk") 381 shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile") 382 shortcut.Save 383 END 384 call writefile(lines, 'link.vbs') 385 silent !cscript link.vbs 386 call delete('link.vbs') 387 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk'))) 388 call delete('Xfile') 389 390 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk'))) 391 call delete('Xlink.lnk') 392 else 393 echomsg 'skipped test for shortcut file' 394 endif 395 396 " remove files 397 call delete('Xlink') 398 call delete('Xdir', 'd') 399 call delete('Xfile') 400 401 " test for symbolic link to a file 402 new Xfile 403 wq 404 call assert_equal('Xfile', resolve('Xfile')) 405 silent !mklink Xlink Xfile 406 if !v:shell_error 407 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink'))) 408 call delete('Xlink') 409 else 410 echomsg 'skipped test for symbolic link to a file' 411 endif 412 call delete('Xfile') 413 414 " test for junction to a directory 415 call mkdir('Xdir') 416 silent !mklink /J Xlink Xdir 417 if !v:shell_error 418 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) 419 420 call delete('Xdir', 'd') 421 422 " test for junction already removed 423 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) 424 call delete('Xlink') 425 else 426 echomsg 'skipped test for junction to a directory' 427 call delete('Xdir', 'd') 428 endif 429 430 " test for symbolic link to a directory 431 call mkdir('Xdir') 432 silent !mklink /D Xlink Xdir 433 if !v:shell_error 434 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) 435 436 call delete('Xdir', 'd') 437 438 " test for symbolic link already removed 439 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) 440 call delete('Xlink') 441 else 442 echomsg 'skipped test for symbolic link to a directory' 443 call delete('Xdir', 'd') 444 endif 445 446 " test for buffer name 447 new Xfile 448 wq 449 silent !mklink Xlink Xfile 450 if !v:shell_error 451 edit Xlink 452 call assert_equal('Xlink', bufname('%')) 453 call delete('Xlink') 454 bw! 455 else 456 echomsg 'skipped test for buffer name' 457 endif 458 call delete('Xfile') 459 460 " test for reparse point 461 call mkdir('Xdir') 462 call assert_equal('Xdir', resolve('Xdir')) 463 silent !mklink /D Xdirlink Xdir 464 if !v:shell_error 465 w Xdir/text.txt 466 call assert_equal('Xdir/text.txt', resolve('Xdir/text.txt')) 467 call assert_equal(s:normalize_fname(getcwd() . '\Xdir\text.txt'), s:normalize_fname(resolve('Xdirlink\text.txt'))) 468 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve('Xdirlink'))) 469 call delete('Xdirlink') 470 else 471 echomsg 'skipped test for reparse point' 472 endif 473 474 call delete('Xdir', 'rf') 475endfunc 476 477func Test_simplify() 478 call assert_equal('', simplify('')) 479 call assert_equal('/', simplify('/')) 480 call assert_equal('/', simplify('/.')) 481 call assert_equal('/', simplify('/..')) 482 call assert_equal('/...', simplify('/...')) 483 call assert_equal('//path', simplify('//path')) 484 if has('unix') 485 call assert_equal('/path', simplify('///path')) 486 call assert_equal('/path', simplify('////path')) 487 endif 488 489 call assert_equal('./dir/file', './dir/file'->simplify()) 490 call assert_equal('./dir/file', simplify('.///dir//file')) 491 call assert_equal('./dir/file', simplify('./dir/./file')) 492 call assert_equal('./file', simplify('./dir/../file')) 493 call assert_equal('../dir/file', simplify('dir/../../dir/file')) 494 call assert_equal('./file', simplify('dir/.././file')) 495 call assert_equal('../dir', simplify('./../dir')) 496 call assert_equal('..', simplify('../testdir/..')) 497 call mkdir('Xdir') 498 call assert_equal('.', simplify('Xdir/../.')) 499 call delete('Xdir', 'd') 500 501 call assert_fails('call simplify({->0})', 'E729:') 502 call assert_fails('call simplify([])', 'E730:') 503 call assert_fails('call simplify({})', 'E731:') 504 if has('float') 505 call assert_equal('1.2', simplify(1.2)) 506 call CheckDefAndScriptFailure2(['echo simplify(1.2)'], 'E1013: Argument 1: type mismatch, expected string but got float', 'E1174: String required for argument 1') 507 endif 508endfunc 509 510func Test_pathshorten() 511 call assert_equal('', pathshorten('')) 512 call assert_equal('foo', pathshorten('foo')) 513 call assert_equal('/foo', '/foo'->pathshorten()) 514 call assert_equal('f/', pathshorten('foo/')) 515 call assert_equal('f/bar', pathshorten('foo/bar')) 516 call assert_equal('f/b/foobar', 'foo/bar/foobar'->pathshorten()) 517 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar')) 518 call assert_equal('.f/bar', pathshorten('.foo/bar')) 519 call assert_equal('~f/bar', pathshorten('~foo/bar')) 520 call assert_equal('~.f/bar', pathshorten('~.foo/bar')) 521 call assert_equal('.~f/bar', pathshorten('.~foo/bar')) 522 call assert_equal('~/f/bar', pathshorten('~/foo/bar')) 523 call assert_fails('call pathshorten([])', 'E730:') 524 525 " test pathshorten with optional variable to set preferred size of shortening 526 call assert_equal('', pathshorten('', 2)) 527 call assert_equal('foo', pathshorten('foo', 2)) 528 call assert_equal('/foo', pathshorten('/foo', 2)) 529 call assert_equal('fo/', pathshorten('foo/', 2)) 530 call assert_equal('fo/bar', pathshorten('foo/bar', 2)) 531 call assert_equal('fo/ba/foobar', pathshorten('foo/bar/foobar', 2)) 532 call assert_equal('/fo/ba/foobar', pathshorten('/foo/bar/foobar', 2)) 533 call assert_equal('.fo/bar', pathshorten('.foo/bar', 2)) 534 call assert_equal('~fo/bar', pathshorten('~foo/bar', 2)) 535 call assert_equal('~.fo/bar', pathshorten('~.foo/bar', 2)) 536 call assert_equal('.~fo/bar', pathshorten('.~foo/bar', 2)) 537 call assert_equal('~/fo/bar', pathshorten('~/foo/bar', 2)) 538 call assert_fails('call pathshorten([],2)', 'E730:') 539 call assert_notequal('~/fo/bar', pathshorten('~/foo/bar', 3)) 540 call assert_equal('~/foo/bar', pathshorten('~/foo/bar', 3)) 541 call assert_equal('~/f/bar', pathshorten('~/foo/bar', 0)) 542endfunc 543 544func Test_strpart() 545 call assert_equal('de', strpart('abcdefg', 3, 2)) 546 call assert_equal('ab', strpart('abcdefg', -2, 4)) 547 call assert_equal('abcdefg', 'abcdefg'->strpart(-2)) 548 call assert_equal('fg', strpart('abcdefg', 5, 4)) 549 call assert_equal('defg', strpart('abcdefg', 3)) 550 call assert_equal('', strpart('abcdefg', 10)) 551 call assert_fails("let s=strpart('abcdef', [])", 'E745:') 552 553 call assert_equal('lép', strpart('éléphant', 2, 4)) 554 call assert_equal('léphant', strpart('éléphant', 2)) 555 556 call assert_equal('é', strpart('éléphant', 0, 1, 1)) 557 call assert_equal('ép', strpart('éléphant', 3, 2, v:true)) 558 call assert_equal('ó', strpart('cómposed', 1, 1, 1)) 559endfunc 560 561func Test_tolower() 562 call assert_equal("", tolower("")) 563 564 " Test with all printable ASCII characters. 565 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 566 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) 567 568 " Test with a few uppercase diacritics. 569 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) 570 call assert_equal("bḃḇ", tolower("BḂḆ")) 571 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ")) 572 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ")) 573 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ")) 574 call assert_equal("fḟ ", tolower("FḞ ")) 575 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ")) 576 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ")) 577 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ")) 578 call assert_equal("jĵ", tolower("JĴ")) 579 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ")) 580 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ")) 581 call assert_equal("mḿṁ", tolower("MḾṀ")) 582 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ")) 583 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) 584 call assert_equal("pṕṗ", tolower("PṔṖ")) 585 call assert_equal("q", tolower("Q")) 586 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ")) 587 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ")) 588 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ")) 589 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) 590 call assert_equal("vṽ", tolower("VṼ")) 591 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ")) 592 call assert_equal("xẋẍ", tolower("XẊẌ")) 593 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ")) 594 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ")) 595 596 " Test with a few lowercase diacritics, which should remain unchanged. 597 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả")) 598 call assert_equal("bḃḇ", tolower("bḃḇ")) 599 call assert_equal("cçćĉċč", tolower("cçćĉċč")) 600 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ")) 601 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ")) 602 call assert_equal("fḟ", tolower("fḟ")) 603 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ")) 604 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ")) 605 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ")) 606 call assert_equal("jĵǰ", tolower("jĵǰ")) 607 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ")) 608 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ")) 609 call assert_equal("mḿṁ ", tolower("mḿṁ ")) 610 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ")) 611 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ")) 612 call assert_equal("pṕṗ", tolower("pṕṗ")) 613 call assert_equal("q", tolower("q")) 614 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ")) 615 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ")) 616 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ")) 617 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ")) 618 call assert_equal("vṽ", tolower("vṽ")) 619 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ")) 620 call assert_equal("ẋẍ", tolower("ẋẍ")) 621 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ")) 622 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ")) 623 624 " According to https://twitter.com/jifa/status/625776454479970304 625 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase 626 " in length (2 to 3 bytes) when lowercased. So let's test them. 627 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ")) 628 629 " This call to tolower with invalid utf8 sequence used to cause access to 630 " invalid memory. 631 call tolower("\xC0\x80\xC0") 632 call tolower("123\xC0\x80\xC0") 633 634 " Test in latin1 encoding 635 let save_enc = &encoding 636 set encoding=latin1 637 call assert_equal("abc", tolower("ABC")) 638 let &encoding = save_enc 639endfunc 640 641func Test_toupper() 642 call assert_equal("", toupper("")) 643 644 " Test with all printable ASCII characters. 645 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~', 646 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) 647 648 " Test with a few lowercase diacritics. 649 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", "aàáâãäåāăąǎǟǡả"->toupper()) 650 call assert_equal("BḂḆ", toupper("bḃḇ")) 651 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč")) 652 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ")) 653 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ")) 654 call assert_equal("FḞ", toupper("fḟ")) 655 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ")) 656 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ")) 657 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ")) 658 call assert_equal("JĴǰ", toupper("jĵǰ")) 659 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ")) 660 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ")) 661 call assert_equal("MḾṀ ", toupper("mḿṁ ")) 662 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ")) 663 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ")) 664 call assert_equal("PṔṖ", toupper("pṕṗ")) 665 call assert_equal("Q", toupper("q")) 666 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ")) 667 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ")) 668 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ")) 669 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ")) 670 call assert_equal("VṼ", toupper("vṽ")) 671 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ")) 672 call assert_equal("ẊẌ", toupper("ẋẍ")) 673 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ")) 674 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ")) 675 676 " Test that uppercase diacritics, which should remain unchanged. 677 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) 678 call assert_equal("BḂḆ", toupper("BḂḆ")) 679 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ")) 680 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ")) 681 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ")) 682 call assert_equal("FḞ ", toupper("FḞ ")) 683 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ")) 684 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ")) 685 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ")) 686 call assert_equal("JĴ", toupper("JĴ")) 687 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ")) 688 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ")) 689 call assert_equal("MḾṀ", toupper("MḾṀ")) 690 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ")) 691 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) 692 call assert_equal("PṔṖ", toupper("PṔṖ")) 693 call assert_equal("Q", toupper("Q")) 694 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ")) 695 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ")) 696 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ")) 697 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) 698 call assert_equal("VṼ", toupper("VṼ")) 699 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ")) 700 call assert_equal("XẊẌ", toupper("XẊẌ")) 701 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ")) 702 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ")) 703 704 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ")) 705 706 " This call to toupper with invalid utf8 sequence used to cause access to 707 " invalid memory. 708 call toupper("\xC0\x80\xC0") 709 call toupper("123\xC0\x80\xC0") 710 711 " Test in latin1 encoding 712 let save_enc = &encoding 713 set encoding=latin1 714 call assert_equal("ABC", toupper("abc")) 715 let &encoding = save_enc 716endfunc 717 718func Test_tr() 719 call assert_equal('foo', tr('bar', 'bar', 'foo')) 720 call assert_equal('zxy', 'cab'->tr('abc', 'xyz')) 721 call assert_fails("let s=tr([], 'abc', 'def')", 'E730:') 722 call assert_fails("let s=tr('abc', [], 'def')", 'E730:') 723 call assert_fails("let s=tr('abc', 'abc', [])", 'E730:') 724 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:') 725 set encoding=latin1 726 call assert_fails("let s=tr('abcd', 'abcd', 'def')", 'E475:') 727 call assert_equal('hEllO', tr('hello', 'eo', 'EO')) 728 call assert_equal('hello', tr('hello', 'xy', 'ab')) 729 call assert_fails('call tr("abc", "123", "₁₂")', 'E475:') 730 set encoding=utf8 731endfunc 732 733" Tests for the mode() function 734let current_modes = '' 735func Save_mode() 736 let g:current_modes = mode(0) . '-' . mode(1) 737 return '' 738endfunc 739 740" Test for the mode() function 741func Test_mode() 742 new 743 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""]) 744 745 " Only complete from the current buffer. 746 set complete=. 747 748 inoremap <F2> <C-R>=Save_mode()<CR> 749 xnoremap <F2> <Cmd>call Save_mode()<CR> 750 751 normal! 3G 752 exe "normal i\<F2>\<Esc>" 753 call assert_equal('i-i', g:current_modes) 754 " i_CTRL-P: Multiple matches 755 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u" 756 call assert_equal('i-ic', g:current_modes) 757 " i_CTRL-P: Single match 758 exe "normal iBro\<C-P>\<F2>\<Esc>u" 759 call assert_equal('i-ic', g:current_modes) 760 " i_CTRL-X 761 exe "normal iBa\<C-X>\<F2>\<Esc>u" 762 call assert_equal('i-ix', g:current_modes) 763 " i_CTRL-X CTRL-P: Multiple matches 764 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u" 765 call assert_equal('i-ic', g:current_modes) 766 " i_CTRL-X CTRL-P: Single match 767 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u" 768 call assert_equal('i-ic', g:current_modes) 769 " i_CTRL-X CTRL-P + CTRL-P: Single match 770 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" 771 call assert_equal('i-ic', g:current_modes) 772 " i_CTRL-X CTRL-L: Multiple matches 773 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u" 774 call assert_equal('i-ic', g:current_modes) 775 " i_CTRL-X CTRL-L: Single match 776 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u" 777 call assert_equal('i-ic', g:current_modes) 778 " i_CTRL-P: No match 779 exe "normal iCom\<C-P>\<F2>\<Esc>u" 780 call assert_equal('i-ic', g:current_modes) 781 " i_CTRL-X CTRL-P: No match 782 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u" 783 call assert_equal('i-ic', g:current_modes) 784 " i_CTRL-X CTRL-L: No match 785 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u" 786 call assert_equal('i-ic', g:current_modes) 787 788 " R_CTRL-P: Multiple matches 789 exe "normal RBa\<C-P>\<F2>\<Esc>u" 790 call assert_equal('R-Rc', g:current_modes) 791 " R_CTRL-P: Single match 792 exe "normal RBro\<C-P>\<F2>\<Esc>u" 793 call assert_equal('R-Rc', g:current_modes) 794 " R_CTRL-X 795 exe "normal RBa\<C-X>\<F2>\<Esc>u" 796 call assert_equal('R-Rx', g:current_modes) 797 " R_CTRL-X CTRL-P: Multiple matches 798 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u" 799 call assert_equal('R-Rc', g:current_modes) 800 " R_CTRL-X CTRL-P: Single match 801 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u" 802 call assert_equal('R-Rc', g:current_modes) 803 " R_CTRL-X CTRL-P + CTRL-P: Single match 804 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" 805 call assert_equal('R-Rc', g:current_modes) 806 " R_CTRL-X CTRL-L: Multiple matches 807 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u" 808 call assert_equal('R-Rc', g:current_modes) 809 " R_CTRL-X CTRL-L: Single match 810 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u" 811 call assert_equal('R-Rc', g:current_modes) 812 " R_CTRL-P: No match 813 exe "normal RCom\<C-P>\<F2>\<Esc>u" 814 call assert_equal('R-Rc', g:current_modes) 815 " R_CTRL-X CTRL-P: No match 816 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u" 817 call assert_equal('R-Rc', g:current_modes) 818 " R_CTRL-X CTRL-L: No match 819 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u" 820 call assert_equal('R-Rc', g:current_modes) 821 822 call assert_equal('n', 0->mode()) 823 call assert_equal('n', 1->mode()) 824 825 " i_CTRL-O 826 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>" 827 call assert_equal("n-niI", g:current_modes) 828 829 " R_CTRL-O 830 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>" 831 call assert_equal("n-niR", g:current_modes) 832 833 " gR_CTRL-O 834 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>" 835 call assert_equal("n-niV", g:current_modes) 836 837 " How to test operator-pending mode? 838 839 call feedkeys("v", 'xt') 840 call assert_equal('v', mode()) 841 call assert_equal('v', mode(1)) 842 call feedkeys("\<Esc>V", 'xt') 843 call assert_equal('V', mode()) 844 call assert_equal('V', mode(1)) 845 call feedkeys("\<Esc>\<C-V>", 'xt') 846 call assert_equal("\<C-V>", mode()) 847 call assert_equal("\<C-V>", mode(1)) 848 call feedkeys("\<Esc>", 'xt') 849 850 call feedkeys("gh", 'xt') 851 call assert_equal('s', mode()) 852 call assert_equal('s', mode(1)) 853 call feedkeys("\<Esc>gH", 'xt') 854 call assert_equal('S', mode()) 855 call assert_equal('S', mode(1)) 856 call feedkeys("\<Esc>g\<C-H>", 'xt') 857 call assert_equal("\<C-S>", mode()) 858 call assert_equal("\<C-S>", mode(1)) 859 call feedkeys("\<Esc>", 'xt') 860 861 " v_CTRL-O 862 exe "normal gh\<C-O>\<F2>\<Esc>" 863 call assert_equal("v-vs", g:current_modes) 864 exe "normal gH\<C-O>\<F2>\<Esc>" 865 call assert_equal("V-Vs", g:current_modes) 866 exe "normal g\<C-H>\<C-O>\<F2>\<Esc>" 867 call assert_equal("\<C-V>-\<C-V>s", g:current_modes) 868 869 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt') 870 call assert_equal('c-c', g:current_modes) 871 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt') 872 call assert_equal('c-cv', g:current_modes) 873 call feedkeys("Qcall Save_mode()\<CR>vi\<CR>", 'xt') 874 call assert_equal('c-ce', g:current_modes) 875 " How to test Ex mode? 876 877 bwipe! 878 iunmap <F2> 879 xunmap <F2> 880 set complete& 881endfunc 882 883" Test for append() 884func Test_append() 885 enew! 886 split 887 call append(0, ["foo"]) 888 call append(1, []) 889 call append(1, test_null_list()) 890 call assert_equal(['foo', ''], getline(1, '$')) 891 split 892 only 893 undo 894 undo 895 896 " Using $ instead of '$' must give an error 897 call assert_fails("call append($, 'foobar')", 'E116:') 898endfunc 899 900" Test for setline() 901func Test_setline() 902 new 903 call setline(0, ["foo"]) 904 call setline(0, []) 905 call setline(0, test_null_list()) 906 call setline(1, ["bar"]) 907 call setline(1, []) 908 call setline(1, test_null_list()) 909 call setline(2, []) 910 call setline(2, test_null_list()) 911 call setline(3, []) 912 call setline(3, test_null_list()) 913 call setline(2, ["baz"]) 914 call assert_equal(['bar', 'baz'], getline(1, '$')) 915 close! 916endfunc 917 918func Test_getbufvar() 919 let bnr = bufnr('%') 920 let b:var_num = '1234' 921 let def_num = '5678' 922 call assert_equal('1234', getbufvar(bnr, 'var_num')) 923 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num)) 924 925 let bd = getbufvar(bnr, '') 926 call assert_equal('1234', bd['var_num']) 927 call assert_true(exists("bd['changedtick']")) 928 call assert_equal(2, len(bd)) 929 930 let bd2 = getbufvar(bnr, '', def_num) 931 call assert_equal(bd, bd2) 932 933 unlet b:var_num 934 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num)) 935 call assert_equal('', getbufvar(bnr, 'var_num')) 936 937 let bd = getbufvar(bnr, '') 938 call assert_equal(1, len(bd)) 939 let bd = getbufvar(bnr, '',def_num) 940 call assert_equal(1, len(bd)) 941 942 call assert_equal('', getbufvar(9999, '')) 943 call assert_equal(def_num, getbufvar(9999, '', def_num)) 944 unlet def_num 945 946 call assert_equal(0, getbufvar(bnr, '&autoindent')) 947 call assert_equal(0, getbufvar(bnr, '&autoindent', 1)) 948 949 " Set and get a buffer-local variable 950 call setbufvar(bnr, 'bufvar_test', ['one', 'two']) 951 call assert_equal(['one', 'two'], getbufvar(bnr, 'bufvar_test')) 952 953 " Open new window with forced option values 954 set fileformats=unix,dos 955 new ++ff=dos ++bin ++enc=iso-8859-2 956 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat')) 957 call assert_equal(1, getbufvar(bufnr('%'), '&bin')) 958 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc')) 959 close 960 961 " Get the b: dict. 962 let b:testvar = 'one' 963 new 964 let b:testvar = 'two' 965 let thebuf = bufnr() 966 wincmd w 967 call assert_equal('two', getbufvar(thebuf, 'testvar')) 968 call assert_equal('two', getbufvar(thebuf, '').testvar) 969 bwipe! 970 971 set fileformats& 972endfunc 973 974func Test_last_buffer_nr() 975 call assert_equal(bufnr('$'), last_buffer_nr()) 976endfunc 977 978func Test_stridx() 979 call assert_equal(-1, stridx('', 'l')) 980 call assert_equal(0, stridx('', '')) 981 call assert_equal(0, 'hello'->stridx('')) 982 call assert_equal(-1, stridx('hello', 'L')) 983 call assert_equal(2, stridx('hello', 'l', -1)) 984 call assert_equal(2, stridx('hello', 'l', 0)) 985 call assert_equal(2, 'hello'->stridx('l', 1)) 986 call assert_equal(3, stridx('hello', 'l', 3)) 987 call assert_equal(-1, stridx('hello', 'l', 4)) 988 call assert_equal(-1, stridx('hello', 'l', 10)) 989 call assert_equal(2, stridx('hello', 'll')) 990 call assert_equal(-1, stridx('hello', 'hello world')) 991 call assert_fails("let n=stridx('hello', [])", 'E730:') 992 call assert_fails("let n=stridx([], 'l')", 'E730:') 993endfunc 994 995func Test_strridx() 996 call assert_equal(-1, strridx('', 'l')) 997 call assert_equal(0, strridx('', '')) 998 call assert_equal(5, strridx('hello', '')) 999 call assert_equal(-1, strridx('hello', 'L')) 1000 call assert_equal(3, 'hello'->strridx('l')) 1001 call assert_equal(3, strridx('hello', 'l', 10)) 1002 call assert_equal(3, strridx('hello', 'l', 3)) 1003 call assert_equal(2, strridx('hello', 'l', 2)) 1004 call assert_equal(-1, strridx('hello', 'l', 1)) 1005 call assert_equal(-1, strridx('hello', 'l', 0)) 1006 call assert_equal(-1, strridx('hello', 'l', -1)) 1007 call assert_equal(2, strridx('hello', 'll')) 1008 call assert_equal(-1, strridx('hello', 'hello world')) 1009 call assert_fails("let n=strridx('hello', [])", 'E730:') 1010 call assert_fails("let n=strridx([], 'l')", 'E730:') 1011endfunc 1012 1013func Test_match_func() 1014 call assert_equal(4, match('testing', 'ing')) 1015 call assert_equal(4, 'testing'->match('ing', 2)) 1016 call assert_equal(-1, match('testing', 'ing', 5)) 1017 call assert_equal(-1, match('testing', 'ing', 8)) 1018 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing')) 1019 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img')) 1020 call assert_fails("let x=match('vim', [])", 'E730:') 1021 call assert_equal(3, match(['a', 'b', 'c', 'a'], 'a', 1)) 1022 call assert_equal(-1, match(['a', 'b', 'c', 'a'], 'a', 5)) 1023 call assert_equal(4, match('testing', 'ing', -1)) 1024 call assert_fails("let x=match('testing', 'ing', 0, [])", 'E745:') 1025 call assert_equal(-1, match(test_null_list(), 2)) 1026 call assert_equal(-1, match('abc', '\\%(')) 1027endfunc 1028 1029func Test_matchend() 1030 call assert_equal(7, matchend('testing', 'ing')) 1031 call assert_equal(7, 'testing'->matchend('ing', 2)) 1032 call assert_equal(-1, matchend('testing', 'ing', 5)) 1033 call assert_equal(-1, matchend('testing', 'ing', 8)) 1034 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing')) 1035 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img')) 1036endfunc 1037 1038func Test_matchlist() 1039 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)')) 1040 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], 'acd'->matchlist('\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2)) 1041 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4)) 1042endfunc 1043 1044func Test_matchstr() 1045 call assert_equal('ing', matchstr('testing', 'ing')) 1046 call assert_equal('ing', 'testing'->matchstr('ing', 2)) 1047 call assert_equal('', matchstr('testing', 'ing', 5)) 1048 call assert_equal('', matchstr('testing', 'ing', 8)) 1049 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing')) 1050 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img')) 1051endfunc 1052 1053func Test_matchstrpos() 1054 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing')) 1055 call assert_equal(['ing', 4, 7], 'testing'->matchstrpos('ing', 2)) 1056 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5)) 1057 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8)) 1058 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing')) 1059 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img')) 1060 call assert_equal(['', -1, -1], matchstrpos(test_null_list(), '\a')) 1061endfunc 1062 1063func Test_nextnonblank_prevnonblank() 1064 new 1065insert 1066This 1067 1068 1069is 1070 1071a 1072Test 1073. 1074 call assert_equal(0, nextnonblank(-1)) 1075 call assert_equal(0, nextnonblank(0)) 1076 call assert_equal(1, nextnonblank(1)) 1077 call assert_equal(4, 2->nextnonblank()) 1078 call assert_equal(4, nextnonblank(3)) 1079 call assert_equal(4, nextnonblank(4)) 1080 call assert_equal(6, nextnonblank(5)) 1081 call assert_equal(6, nextnonblank(6)) 1082 call assert_equal(7, nextnonblank(7)) 1083 call assert_equal(0, 8->nextnonblank()) 1084 1085 call assert_equal(0, prevnonblank(-1)) 1086 call assert_equal(0, prevnonblank(0)) 1087 call assert_equal(1, 1->prevnonblank()) 1088 call assert_equal(1, prevnonblank(2)) 1089 call assert_equal(1, prevnonblank(3)) 1090 call assert_equal(4, prevnonblank(4)) 1091 call assert_equal(4, 5->prevnonblank()) 1092 call assert_equal(6, prevnonblank(6)) 1093 call assert_equal(7, prevnonblank(7)) 1094 call assert_equal(0, prevnonblank(8)) 1095 bw! 1096endfunc 1097 1098func Test_byte2line_line2byte() 1099 new 1100 set endofline 1101 call setline(1, ['a', 'bc', 'd']) 1102 1103 set fileformat=unix 1104 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], 1105 \ map(range(-1, 8), 'byte2line(v:val)')) 1106 call assert_equal([-1, -1, 1, 3, 6, 8, -1], 1107 \ map(range(-1, 5), 'line2byte(v:val)')) 1108 1109 set fileformat=mac 1110 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], 1111 \ map(range(-1, 8), 'v:val->byte2line()')) 1112 call assert_equal([-1, -1, 1, 3, 6, 8, -1], 1113 \ map(range(-1, 5), 'v:val->line2byte()')) 1114 1115 set fileformat=dos 1116 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1], 1117 \ map(range(-1, 11), 'byte2line(v:val)')) 1118 call assert_equal([-1, -1, 1, 4, 8, 11, -1], 1119 \ map(range(-1, 5), 'line2byte(v:val)')) 1120 1121 bw! 1122 set noendofline nofixendofline 1123 normal a- 1124 for ff in ["unix", "mac", "dos"] 1125 let &fileformat = ff 1126 call assert_equal(1, line2byte(1)) 1127 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte). 1128 endfor 1129 1130 set endofline& fixendofline& fileformat& 1131 bw! 1132endfunc 1133 1134" Test for byteidx() and byteidxcomp() functions 1135func Test_byteidx() 1136 let a = '.é.' " one char of two bytes 1137 call assert_equal(0, byteidx(a, 0)) 1138 call assert_equal(0, byteidxcomp(a, 0)) 1139 call assert_equal(1, byteidx(a, 1)) 1140 call assert_equal(1, byteidxcomp(a, 1)) 1141 call assert_equal(3, byteidx(a, 2)) 1142 call assert_equal(3, byteidxcomp(a, 2)) 1143 call assert_equal(4, byteidx(a, 3)) 1144 call assert_equal(4, byteidxcomp(a, 3)) 1145 call assert_equal(-1, byteidx(a, 4)) 1146 call assert_equal(-1, byteidxcomp(a, 4)) 1147 1148 let b = '.é.' " normal e with composing char 1149 call assert_equal(0, b->byteidx(0)) 1150 call assert_equal(1, b->byteidx(1)) 1151 call assert_equal(4, b->byteidx(2)) 1152 call assert_equal(5, b->byteidx(3)) 1153 call assert_equal(-1, b->byteidx(4)) 1154 call assert_fails("call byteidx([], 0)", 'E730:') 1155 1156 call assert_equal(0, b->byteidxcomp(0)) 1157 call assert_equal(1, b->byteidxcomp(1)) 1158 call assert_equal(2, b->byteidxcomp(2)) 1159 call assert_equal(4, b->byteidxcomp(3)) 1160 call assert_equal(5, b->byteidxcomp(4)) 1161 call assert_equal(-1, b->byteidxcomp(5)) 1162 call assert_fails("call byteidxcomp([], 0)", 'E730:') 1163endfunc 1164 1165" Test for charidx() 1166func Test_charidx() 1167 let a = 'xáb́y' 1168 call assert_equal(0, charidx(a, 0)) 1169 call assert_equal(1, charidx(a, 3)) 1170 call assert_equal(2, charidx(a, 4)) 1171 call assert_equal(3, charidx(a, 7)) 1172 call assert_equal(-1, charidx(a, 8)) 1173 call assert_equal(-1, charidx(a, -1)) 1174 call assert_equal(-1, charidx('', 0)) 1175 call assert_equal(-1, charidx(test_null_string(), 0)) 1176 1177 " count composing characters 1178 call assert_equal(0, charidx(a, 0, 1)) 1179 call assert_equal(2, charidx(a, 2, 1)) 1180 call assert_equal(3, charidx(a, 4, 1)) 1181 call assert_equal(5, charidx(a, 7, 1)) 1182 call assert_equal(-1, charidx(a, 8, 1)) 1183 call assert_equal(-1, charidx('', 0, 1)) 1184 1185 call assert_fails('let x = charidx([], 1)', 'E474:') 1186 call assert_fails('let x = charidx("abc", [])', 'E474:') 1187 call assert_fails('let x = charidx("abc", 1, [])', 'E474:') 1188 call assert_fails('let x = charidx("abc", 1, -1)', 'E1023:') 1189 call assert_fails('let x = charidx("abc", 1, 2)', 'E1023:') 1190endfunc 1191 1192func Test_count() 1193 let l = ['a', 'a', 'A', 'b'] 1194 call assert_equal(2, count(l, 'a')) 1195 call assert_equal(1, count(l, 'A')) 1196 call assert_equal(1, count(l, 'b')) 1197 call assert_equal(0, count(l, 'B')) 1198 1199 call assert_equal(2, count(l, 'a', 0)) 1200 call assert_equal(1, count(l, 'A', 0)) 1201 call assert_equal(1, count(l, 'b', 0)) 1202 call assert_equal(0, count(l, 'B', 0)) 1203 1204 call assert_equal(3, count(l, 'a', 1)) 1205 call assert_equal(3, count(l, 'A', 1)) 1206 call assert_equal(1, count(l, 'b', 1)) 1207 call assert_equal(1, count(l, 'B', 1)) 1208 call assert_equal(0, count(l, 'c', 1)) 1209 1210 call assert_equal(1, count(l, 'a', 0, 1)) 1211 call assert_equal(2, count(l, 'a', 1, 1)) 1212 call assert_fails('call count(l, "a", 0, 10)', 'E684:') 1213 call assert_fails('call count(l, "a", [])', 'E745:') 1214 1215 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'} 1216 call assert_equal(2, count(d, 'a')) 1217 call assert_equal(1, count(d, 'A')) 1218 call assert_equal(1, count(d, 'b')) 1219 call assert_equal(0, count(d, 'B')) 1220 1221 call assert_equal(2, count(d, 'a', 0)) 1222 call assert_equal(1, count(d, 'A', 0)) 1223 call assert_equal(1, count(d, 'b', 0)) 1224 call assert_equal(0, count(d, 'B', 0)) 1225 1226 call assert_equal(3, count(d, 'a', 1)) 1227 call assert_equal(3, count(d, 'A', 1)) 1228 call assert_equal(1, count(d, 'b', 1)) 1229 call assert_equal(1, count(d, 'B', 1)) 1230 call assert_equal(0, count(d, 'c', 1)) 1231 1232 call assert_fails('call count(d, "a", 0, 1)', 'E474:') 1233 1234 call assert_equal(0, count("foo", "bar")) 1235 call assert_equal(1, count("foo", "oo")) 1236 call assert_equal(2, count("foo", "o")) 1237 call assert_equal(0, count("foo", "O")) 1238 call assert_equal(2, count("foo", "O", 1)) 1239 call assert_equal(2, count("fooooo", "oo")) 1240 call assert_equal(0, count("foo", "")) 1241 1242 call assert_fails('call count(0, 0)', 'E712:') 1243endfunc 1244 1245func Test_changenr() 1246 new Xchangenr 1247 call assert_equal(0, changenr()) 1248 norm ifoo 1249 call assert_equal(1, changenr()) 1250 set undolevels=10 1251 norm Sbar 1252 call assert_equal(2, changenr()) 1253 undo 1254 call assert_equal(1, changenr()) 1255 redo 1256 call assert_equal(2, changenr()) 1257 bw! 1258 set undolevels& 1259endfunc 1260 1261func Test_filewritable() 1262 new Xfilewritable 1263 write! 1264 call assert_equal(1, filewritable('Xfilewritable')) 1265 1266 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----')) 1267 call assert_equal(0, filewritable('Xfilewritable')) 1268 1269 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----')) 1270 call assert_equal(1, 'Xfilewritable'->filewritable()) 1271 1272 call assert_equal(0, filewritable('doesnotexist')) 1273 1274 call mkdir('Xdir') 1275 call assert_equal(2, filewritable('Xdir')) 1276 call delete('Xdir', 'd') 1277 1278 call delete('Xfilewritable') 1279 bw! 1280endfunc 1281 1282func Test_Executable() 1283 if has('win32') 1284 call assert_equal(1, executable('notepad')) 1285 call assert_equal(1, 'notepad.exe'->executable()) 1286 call assert_equal(0, executable('notepad.exe.exe')) 1287 call assert_equal(0, executable('shell32.dll')) 1288 call assert_equal(0, executable('win.ini')) 1289 1290 " get "notepad" path and remove the leading drive and sep. (ex. 'C:\') 1291 let notepadcmd = exepath('notepad.exe') 1292 let driveroot = notepadcmd[:2] 1293 let notepadcmd = notepadcmd[3:] 1294 new 1295 " check that the relative path works in / 1296 execute 'lcd' driveroot 1297 call assert_equal(1, executable(notepadcmd)) 1298 call assert_equal(driveroot .. notepadcmd, notepadcmd->exepath()) 1299 bwipe 1300 1301 " create "notepad.bat" 1302 call mkdir('Xdir') 1303 let notepadbat = fnamemodify('Xdir/notepad.bat', ':p') 1304 call writefile([], notepadbat) 1305 new 1306 " check that the path and the pathext order is valid 1307 lcd Xdir 1308 let [pathext, $PATHEXT] = [$PATHEXT, '.com;.exe;.bat;.cmd'] 1309 call assert_equal(notepadbat, exepath('notepad')) 1310 let $PATHEXT = pathext 1311 bwipe 1312 eval 'Xdir'->delete('rf') 1313 elseif has('unix') 1314 call assert_equal(1, 'cat'->executable()) 1315 call assert_equal(0, executable('nodogshere')) 1316 1317 " get "cat" path and remove the leading / 1318 let catcmd = exepath('cat')[1:] 1319 new 1320 " check that the relative path works in / 1321 lcd / 1322 call assert_equal(1, executable(catcmd)) 1323 let result = catcmd->exepath() 1324 " when using chroot looking for sbin/cat can return bin/cat, that is OK 1325 if catcmd =~ '\<sbin\>' && result =~ '\<bin\>' 1326 call assert_equal('/' .. substitute(catcmd, '\<sbin\>', 'bin', ''), result) 1327 else 1328 " /bin/cat and /usr/bin/cat may be hard linked, we could get either 1329 let result = substitute(result, '/usr/bin/cat', '/bin/cat', '') 1330 let catcmd = substitute(catcmd, 'usr/bin/cat', 'bin/cat', '') 1331 call assert_equal('/' .. catcmd, result) 1332 endif 1333 bwipe 1334 else 1335 throw 'Skipped: does not work on this platform' 1336 endif 1337endfunc 1338 1339func Test_executable_longname() 1340 CheckMSWindows 1341 1342 " Create a temporary .bat file with 205 characters in the name. 1343 " Maximum length of a filename (including the path) on MS-Windows is 259 1344 " characters. 1345 " See https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation 1346 let len = 259 - getcwd()->len() - 6 1347 if len > 200 1348 let len = 200 1349 endif 1350 1351 let fname = 'X' . repeat('あ', len) . '.bat' 1352 call writefile([], fname) 1353 call assert_equal(1, executable(fname)) 1354 call delete(fname) 1355endfunc 1356 1357func Test_hostname() 1358 let hostname_vim = hostname() 1359 if has('unix') 1360 let hostname_system = systemlist('uname -n')[0] 1361 call assert_equal(hostname_vim, hostname_system) 1362 endif 1363endfunc 1364 1365func Test_getpid() 1366 " getpid() always returns the same value within a vim instance. 1367 call assert_equal(getpid(), getpid()) 1368 if has('unix') 1369 call assert_equal(systemlist('echo $PPID')[0], string(getpid())) 1370 endif 1371endfunc 1372 1373func Test_hlexists() 1374 call assert_equal(0, hlexists('does_not_exist')) 1375 call assert_equal(0, 'Number'->hlexists()) 1376 call assert_equal(0, highlight_exists('does_not_exist')) 1377 call assert_equal(0, highlight_exists('Number')) 1378 syntax on 1379 call assert_equal(0, hlexists('does_not_exist')) 1380 call assert_equal(1, hlexists('Number')) 1381 call assert_equal(0, highlight_exists('does_not_exist')) 1382 call assert_equal(1, highlight_exists('Number')) 1383 syntax off 1384endfunc 1385 1386" Test for the col() function 1387func Test_col() 1388 new 1389 call setline(1, 'abcdef') 1390 norm gg4|mx6|mY2| 1391 call assert_equal(2, col('.')) 1392 call assert_equal(7, col('$')) 1393 call assert_equal(2, col('v')) 1394 call assert_equal(4, col("'x")) 1395 call assert_equal(6, col("'Y")) 1396 call assert_equal(2, [1, 2]->col()) 1397 call assert_equal(7, col([1, '$'])) 1398 1399 call assert_equal(0, col('')) 1400 call assert_equal(0, col('x')) 1401 call assert_equal(0, col([2, '$'])) 1402 call assert_equal(0, col([1, 100])) 1403 call assert_equal(0, col([1])) 1404 call assert_equal(0, col(test_null_list())) 1405 call assert_fails('let c = col({})', 'E731:') 1406 1407 " test for getting the visual start column 1408 func T() 1409 let g:Vcol = col('v') 1410 return '' 1411 endfunc 1412 let g:Vcol = 0 1413 xmap <expr> <F2> T() 1414 exe "normal gg3|ve\<F2>" 1415 call assert_equal(3, g:Vcol) 1416 xunmap <F2> 1417 delfunc T 1418 1419 " Test for the visual line start and end marks '< and '> 1420 call setline(1, ['one', 'one two', 'one two three']) 1421 "normal! ggVG 1422 call feedkeys("ggVG\<Esc>", 'xt') 1423 call assert_equal(1, col("'<")) 1424 call assert_equal(14, col("'>")) 1425 " Delete the last line of the visually selected region 1426 $d 1427 call assert_notequal(14, col("'>")) 1428 1429 " Test with 'virtualedit' 1430 set virtualedit=all 1431 call cursor(1, 10) 1432 call assert_equal(4, col('.')) 1433 set virtualedit& 1434 1435 bw! 1436endfunc 1437 1438" Test for input() 1439func Test_input_func() 1440 " Test for prompt with multiple lines 1441 redir => v 1442 call feedkeys(":let c = input(\"A\\nB\\nC\\n? \")\<CR>B\<CR>", 'xt') 1443 redir END 1444 call assert_equal("B", c) 1445 call assert_equal(['A', 'B', 'C'], split(v, "\n")) 1446 1447 " Test for default value 1448 call feedkeys(":let c = input('color? ', 'red')\<CR>\<CR>", 'xt') 1449 call assert_equal('red', c) 1450 1451 " Test for completion at the input prompt 1452 func! Tcomplete(arglead, cmdline, pos) 1453 return "item1\nitem2\nitem3" 1454 endfunc 1455 call feedkeys(":let c = input('Q? ', '', 'custom,Tcomplete')\<CR>" 1456 \ .. "\<C-A>\<CR>", 'xt') 1457 delfunc Tcomplete 1458 call assert_equal('item1 item2 item3', c) 1459 1460 " Test for using special characters as default input 1461 call feedkeys(":let c = input('name? ', \"x\\<BS>y\")\<CR>\<CR>", 'xt') 1462 call assert_equal('y', c) 1463 1464 " Test for using <CR> as default input 1465 call feedkeys(":let c = input('name? ', \"\\<CR>\")\<CR>x\<CR>", 'xt') 1466 call assert_equal(' x', c) 1467 1468 call assert_fails("call input('F:', '', 'invalid')", 'E180:') 1469 call assert_fails("call input('F:', '', [])", 'E730:') 1470endfunc 1471 1472" Test for the inputdialog() function 1473func Test_inputdialog() 1474 set timeout timeoutlen=10 1475 if has('gui_running') 1476 call assert_fails('let v=inputdialog([], "xx")', 'E730:') 1477 call assert_fails('let v=inputdialog("Q", [])', 'E730:') 1478 else 1479 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<CR>", 'xt') 1480 call assert_equal('xx', v) 1481 call feedkeys(":let v=inputdialog('Q:', 'xx', 'yy')\<CR>\<Esc>", 'xt') 1482 call assert_equal('yy', v) 1483 endif 1484 set timeout& timeoutlen& 1485endfunc 1486 1487" Test for inputlist() 1488func Test_inputlist() 1489 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx') 1490 call assert_equal(1, c) 1491 call feedkeys(":let c = ['Select color:', '1. red', '2. green', '3. blue']->inputlist()\<cr>2\<cr>", 'tx') 1492 call assert_equal(2, c) 1493 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx') 1494 call assert_equal(3, c) 1495 1496 " CR to cancel 1497 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<cr>", 'tx') 1498 call assert_equal(0, c) 1499 1500 " Esc to cancel 1501 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<Esc>", 'tx') 1502 call assert_equal(0, c) 1503 1504 " q to cancel 1505 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>q", 'tx') 1506 call assert_equal(0, c) 1507 1508 " Cancel after inputting a number 1509 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>5q", 'tx') 1510 call assert_equal(0, c) 1511 1512 " Use backspace to delete characters in the prompt 1513 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<BS>3\<BS>2\<cr>", 'tx') 1514 call assert_equal(2, c) 1515 1516 " Use mouse to make a selection 1517 call test_setmouse(&lines - 3, 2) 1518 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx') 1519 call assert_equal(1, c) 1520 " Mouse click outside of the list 1521 call test_setmouse(&lines - 6, 2) 1522 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>\<LeftMouse>", 'tx') 1523 call assert_equal(-2, c) 1524 1525 call assert_fails('call inputlist("")', 'E686:') 1526 call assert_fails('call inputlist(test_null_list())', 'E686:') 1527endfunc 1528 1529func Test_balloon_show() 1530 CheckFeature balloon_eval 1531 1532 " This won't do anything but must not crash either. 1533 call balloon_show('hi!') 1534 if !has('gui_running') 1535 call balloon_show(range(3)) 1536 call balloon_show([]) 1537 endif 1538endfunc 1539 1540func Test_setbufvar_options() 1541 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the 1542 " window layout. 1543 call assert_equal(1, winnr('$')) 1544 split dummy_preview 1545 resize 2 1546 set winfixheight winfixwidth 1547 let prev_id = win_getid() 1548 1549 wincmd j 1550 let wh = winheight(0) 1551 let dummy_buf = bufnr('dummy_buf1', v:true) 1552 call setbufvar(dummy_buf, '&buftype', 'nofile') 1553 execute 'belowright vertical split #' . dummy_buf 1554 call assert_equal(wh, winheight(0)) 1555 let dum1_id = win_getid() 1556 1557 wincmd h 1558 let wh = winheight(0) 1559 let dummy_buf = bufnr('dummy_buf2', v:true) 1560 eval 'nofile'->setbufvar(dummy_buf, '&buftype') 1561 execute 'belowright vertical split #' . dummy_buf 1562 call assert_equal(wh, winheight(0)) 1563 1564 bwipe! 1565 call win_gotoid(prev_id) 1566 bwipe! 1567 call win_gotoid(dum1_id) 1568 bwipe! 1569endfunc 1570 1571func Test_redo_in_nested_functions() 1572 nnoremap g. :set opfunc=Operator<CR>g@ 1573 function Operator( type, ... ) 1574 let @x = 'XXX' 1575 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp' 1576 endfunction 1577 1578 function! Apply() 1579 5,6normal! . 1580 endfunction 1581 1582 new 1583 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3)) 1584 1normal g.i" 1585 call assert_equal('some "XXX" text', getline(1)) 1586 3,4normal . 1587 call assert_equal('some "XXX" text', getline(3)) 1588 call assert_equal('more "XXX" text', getline(4)) 1589 call Apply() 1590 call assert_equal('some "XXX" text', getline(5)) 1591 call assert_equal('more "XXX" text', getline(6)) 1592 bwipe! 1593 1594 nunmap g. 1595 delfunc Operator 1596 delfunc Apply 1597endfunc 1598 1599func Test_trim() 1600 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B")) 1601 call assert_equal("Testing", " \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B"->trim()) 1602 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t")) 1603 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww")) 1604 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail")) 1605 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " ")) 1606 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) 1607 call assert_equal("RESERVE", trim("你RESERVE好", "你好")) 1608 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好")) 1609 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", )) 1610 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好")) 1611 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes")) 1612 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses")) 1613 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要")) 1614 call assert_equal("", trim("", "")) 1615 call assert_equal("a", trim("a", "")) 1616 call assert_equal("", trim("", "a")) 1617 1618 call assert_equal("vim", trim(" vim ", " ", 0)) 1619 call assert_equal("vim ", trim(" vim ", " ", 1)) 1620 call assert_equal(" vim", trim(" vim ", " ", 2)) 1621 call assert_fails('eval trim(" vim ", " ", [])', 'E745:') 1622 call assert_fails('eval trim(" vim ", " ", -1)', 'E475:') 1623 call assert_fails('eval trim(" vim ", " ", 3)', 'E475:') 1624 1625 let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '') 1626 call assert_equal("x", trim(chars . "x" . chars)) 1627 1628 call assert_fails('let c=trim([])', 'E730:') 1629endfunc 1630 1631" Test for reg_recording() and reg_executing() 1632func Test_reg_executing_and_recording() 1633 let s:reg_stat = '' 1634 func s:save_reg_stat() 1635 let s:reg_stat = reg_recording() . ':' . reg_executing() 1636 return '' 1637 endfunc 1638 1639 new 1640 call s:save_reg_stat() 1641 call assert_equal(':', s:reg_stat) 1642 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt') 1643 call assert_equal('a:', s:reg_stat) 1644 call feedkeys("@a", 'xt') 1645 call assert_equal(':a', s:reg_stat) 1646 call feedkeys("qb@aq", 'xt') 1647 call assert_equal('b:a', s:reg_stat) 1648 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt') 1649 call assert_equal('":', s:reg_stat) 1650 1651 " :normal command saves and restores reg_executing 1652 let s:reg_stat = '' 1653 let @q = ":call TestFunc()\<CR>:call s:save_reg_stat()\<CR>" 1654 func TestFunc() abort 1655 normal! ia 1656 endfunc 1657 call feedkeys("@q", 'xt') 1658 call assert_equal(':q', s:reg_stat) 1659 delfunc TestFunc 1660 1661 " getchar() command saves and restores reg_executing 1662 map W :call TestFunc()<CR> 1663 let @q = "W" 1664 let g:typed = '' 1665 let g:regs = [] 1666 func TestFunc() abort 1667 let g:regs += [reg_executing()] 1668 let g:typed = getchar(0) 1669 let g:regs += [reg_executing()] 1670 endfunc 1671 call feedkeys("@qy", 'xt') 1672 call assert_equal(char2nr("y"), g:typed) 1673 call assert_equal(['q', 'q'], g:regs) 1674 delfunc TestFunc 1675 unmap W 1676 unlet g:typed 1677 unlet g:regs 1678 1679 " input() command saves and restores reg_executing 1680 map W :call TestFunc()<CR> 1681 let @q = "W" 1682 let g:typed = '' 1683 let g:regs = [] 1684 func TestFunc() abort 1685 let g:regs += [reg_executing()] 1686 let g:typed = '?'->input() 1687 let g:regs += [reg_executing()] 1688 endfunc 1689 call feedkeys("@qy\<CR>", 'xt') 1690 call assert_equal("y", g:typed) 1691 call assert_equal(['q', 'q'], g:regs) 1692 delfunc TestFunc 1693 unmap W 1694 unlet g:typed 1695 unlet g:regs 1696 1697 bwipe! 1698 delfunc s:save_reg_stat 1699 unlet s:reg_stat 1700endfunc 1701 1702func Test_inputsecret() 1703 map W :call TestFunc()<CR> 1704 let @q = "W" 1705 let g:typed1 = '' 1706 let g:typed2 = '' 1707 let g:regs = [] 1708 func TestFunc() abort 1709 let g:typed1 = '?'->inputsecret() 1710 let g:typed2 = inputsecret('password: ') 1711 endfunc 1712 call feedkeys("@qsomething\<CR>else\<CR>", 'xt') 1713 call assert_equal("something", g:typed1) 1714 call assert_equal("else", g:typed2) 1715 delfunc TestFunc 1716 unmap W 1717 unlet g:typed1 1718 unlet g:typed2 1719endfunc 1720 1721func Test_getchar() 1722 call feedkeys('a', '') 1723 call assert_equal(char2nr('a'), getchar()) 1724 call assert_equal(0, getchar(0)) 1725 call assert_equal(0, getchar(1)) 1726 1727 call feedkeys('a', '') 1728 call assert_equal('a', getcharstr()) 1729 call assert_equal('', getcharstr(0)) 1730 call assert_equal('', getcharstr(1)) 1731 1732 call setline(1, 'xxxx') 1733 call test_setmouse(1, 3) 1734 let v:mouse_win = 9 1735 let v:mouse_winid = 9 1736 let v:mouse_lnum = 9 1737 let v:mouse_col = 9 1738 call feedkeys("\<S-LeftMouse>", '') 1739 call assert_equal("\<S-LeftMouse>", getchar()) 1740 call assert_equal(1, v:mouse_win) 1741 call assert_equal(win_getid(1), v:mouse_winid) 1742 call assert_equal(1, v:mouse_lnum) 1743 call assert_equal(3, v:mouse_col) 1744 enew! 1745endfunc 1746 1747func Test_libcall_libcallnr() 1748 CheckFeature libcall 1749 1750 if has('win32') 1751 let libc = 'msvcrt.dll' 1752 elseif has('mac') 1753 let libc = 'libSystem.B.dylib' 1754 elseif executable('ldd') 1755 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>') 1756 endif 1757 if get(l:, 'libc', '') ==# '' 1758 " On Unix, libc.so can be in various places. 1759 if has('linux') 1760 " There is not documented but regarding the 1st argument of glibc's 1761 " dlopen an empty string and nullptr are equivalent, so using an empty 1762 " string for the 1st argument of libcall allows to call functions. 1763 let libc = '' 1764 elseif has('sun') 1765 " Set the path to libc.so according to the architecture. 1766 let test_bits = system('file ' . GetVimProg()) 1767 let test_arch = system('uname -p') 1768 if test_bits =~ '64-bit' && test_arch =~ 'sparc' 1769 let libc = '/usr/lib/sparcv9/libc.so' 1770 elseif test_bits =~ '64-bit' && test_arch =~ 'i386' 1771 let libc = '/usr/lib/amd64/libc.so' 1772 else 1773 let libc = '/usr/lib/libc.so' 1774 endif 1775 else 1776 " Unfortunately skip this test until a good way is found. 1777 return 1778 endif 1779 endif 1780 1781 if has('win32') 1782 call assert_equal($USERPROFILE, 'USERPROFILE'->libcall(libc, 'getenv')) 1783 else 1784 call assert_equal($HOME, 'HOME'->libcall(libc, 'getenv')) 1785 endif 1786 1787 " If function returns NULL, libcall() should return an empty string. 1788 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT')) 1789 1790 " Test libcallnr() with string and integer argument. 1791 call assert_equal(4, 'abcd'->libcallnr(libc, 'strlen')) 1792 call assert_equal(char2nr('A'), char2nr('a')->libcallnr(libc, 'toupper')) 1793 1794 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", ['', 'E364:']) 1795 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", ['', 'E364:']) 1796 1797 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", ['', 'E364:']) 1798 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", ['', 'E364:']) 1799endfunc 1800 1801sandbox function Fsandbox() 1802 normal ix 1803endfunc 1804 1805func Test_func_sandbox() 1806 sandbox let F = {-> 'hello'} 1807 call assert_equal('hello', F()) 1808 1809 sandbox let F = {-> "normal ix\<Esc>"->execute()} 1810 call assert_fails('call F()', 'E48:') 1811 unlet F 1812 1813 call assert_fails('call Fsandbox()', 'E48:') 1814 delfunc Fsandbox 1815 1816 " From a sandbox try to set a predefined variable (which cannot be modified 1817 " from a sandbox) 1818 call assert_fails('sandbox let v:lnum = 10', 'E794:') 1819endfunc 1820 1821func EditAnotherFile() 1822 let word = expand('<cword>') 1823 edit Xfuncrange2 1824endfunc 1825 1826func Test_func_range_with_edit() 1827 " Define a function that edits another buffer, then call it with a range that 1828 " is invalid in that buffer. 1829 call writefile(['just one line'], 'Xfuncrange2') 1830 new 1831 eval 10->range()->setline(1) 1832 write Xfuncrange1 1833 call assert_fails('5,8call EditAnotherFile()', 'E16:') 1834 1835 call delete('Xfuncrange1') 1836 call delete('Xfuncrange2') 1837 bwipe! 1838endfunc 1839 1840func Test_func_exists_on_reload() 1841 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists') 1842 call assert_equal(0, exists('*ExistingFunction')) 1843 source Xfuncexists 1844 call assert_equal(1, '*ExistingFunction'->exists()) 1845 " Redefining a function when reloading a script is OK. 1846 source Xfuncexists 1847 call assert_equal(1, exists('*ExistingFunction')) 1848 1849 " But redefining in another script is not OK. 1850 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2') 1851 call assert_fails('source Xfuncexists2', 'E122:') 1852 1853 " Defining a new function from the cmdline should fail if the function is 1854 " already defined 1855 call assert_fails('call feedkeys(":func ExistingFunction()\<CR>", "xt")', 'E122:') 1856 1857 delfunc ExistingFunction 1858 call assert_equal(0, exists('*ExistingFunction')) 1859 call writefile([ 1860 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc', 1861 \ 'func ExistingFunction()', 'echo "no"', 'endfunc', 1862 \ ], 'Xfuncexists') 1863 call assert_fails('source Xfuncexists', 'E122:') 1864 call assert_equal(1, exists('*ExistingFunction')) 1865 1866 call delete('Xfuncexists2') 1867 call delete('Xfuncexists') 1868 delfunc ExistingFunction 1869endfunc 1870 1871" Test confirm({msg} [, {choices} [, {default} [, {type}]]]) 1872func Test_confirm() 1873 CheckUnix 1874 CheckNotGui 1875 1876 call feedkeys('o', 'L') 1877 let a = confirm('Press O to proceed') 1878 call assert_equal(1, a) 1879 1880 call feedkeys('y', 'L') 1881 let a = 'Are you sure?'->confirm("&Yes\n&No") 1882 call assert_equal(1, a) 1883 1884 call feedkeys('n', 'L') 1885 let a = confirm('Are you sure?', "&Yes\n&No") 1886 call assert_equal(2, a) 1887 1888 " confirm() should return 0 when pressing CTRL-C. 1889 call feedkeys("\<C-C>", 'L') 1890 let a = confirm('Are you sure?', "&Yes\n&No") 1891 call assert_equal(0, a) 1892 1893 " <Esc> requires another character to avoid it being seen as the start of an 1894 " escape sequence. Zero should be harmless. 1895 eval "\<Esc>0"->feedkeys('L') 1896 let a = confirm('Are you sure?', "&Yes\n&No") 1897 call assert_equal(0, a) 1898 1899 " Default choice is returned when pressing <CR>. 1900 call feedkeys("\<CR>", 'L') 1901 let a = confirm('Are you sure?', "&Yes\n&No") 1902 call assert_equal(1, a) 1903 1904 call feedkeys("\<CR>", 'L') 1905 let a = confirm('Are you sure?', "&Yes\n&No", 2) 1906 call assert_equal(2, a) 1907 1908 call feedkeys("\<CR>", 'L') 1909 let a = confirm('Are you sure?', "&Yes\n&No", 0) 1910 call assert_equal(0, a) 1911 1912 " Test with the {type} 4th argument 1913 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic'] 1914 call feedkeys('y', 'L') 1915 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type) 1916 call assert_equal(1, a) 1917 endfor 1918 1919 call assert_fails('call confirm([])', 'E730:') 1920 call assert_fails('call confirm("Are you sure?", [])', 'E730:') 1921 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:') 1922 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:') 1923endfunc 1924 1925func Test_platform_name() 1926 " The system matches at most only one name. 1927 let names = ['amiga', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix'] 1928 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)'))) 1929 1930 " Is Unix? 1931 call assert_equal(has('bsd'), has('bsd') && has('unix')) 1932 call assert_equal(has('hpux'), has('hpux') && has('unix')) 1933 call assert_equal(has('linux'), has('linux') && has('unix')) 1934 call assert_equal(has('mac'), has('mac') && has('unix')) 1935 call assert_equal(has('qnx'), has('qnx') && has('unix')) 1936 call assert_equal(has('sun'), has('sun') && has('unix')) 1937 call assert_equal(has('win32'), has('win32') && !has('unix')) 1938 call assert_equal(has('win32unix'), has('win32unix') && has('unix')) 1939 1940 if has('unix') && executable('uname') 1941 let uname = system('uname') 1942 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined 1943 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd')) 1944 call assert_equal(uname =~? 'HP-UX', has('hpux')) 1945 call assert_equal(uname =~? 'Linux', has('linux')) 1946 call assert_equal(uname =~? 'Darwin', has('mac')) 1947 call assert_equal(uname =~? 'QNX', has('qnx')) 1948 call assert_equal(uname =~? 'SunOS', has('sun')) 1949 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix')) 1950 endif 1951endfunc 1952 1953func Test_readdir() 1954 call mkdir('Xdir') 1955 call writefile([], 'Xdir/foo.txt') 1956 call writefile([], 'Xdir/bar.txt') 1957 call mkdir('Xdir/dir') 1958 1959 " All results 1960 let files = readdir('Xdir') 1961 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files)) 1962 1963 " Only results containing "f" 1964 let files = 'Xdir'->readdir({ x -> stridx(x, 'f') != -1 }) 1965 call assert_equal(['foo.txt'], sort(files)) 1966 1967 " Only .txt files 1968 let files = readdir('Xdir', { x -> x =~ '.txt$' }) 1969 call assert_equal(['bar.txt', 'foo.txt'], sort(files)) 1970 1971 " Only .txt files with string 1972 let files = readdir('Xdir', 'v:val =~ ".txt$"') 1973 call assert_equal(['bar.txt', 'foo.txt'], sort(files)) 1974 1975 " Limit to 1 result. 1976 let l = [] 1977 let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1}) 1978 call assert_equal(1, len(files)) 1979 1980 " Nested readdir() must not crash 1981 let files = readdir('Xdir', 'readdir("Xdir", "1") != []') 1982 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt']) 1983 1984 eval 'Xdir'->delete('rf') 1985endfunc 1986 1987func Test_readdirex() 1988 call mkdir('Xdir') 1989 call writefile(['foo'], 'Xdir/foo.txt') 1990 call writefile(['barbar'], 'Xdir/bar.txt') 1991 call mkdir('Xdir/dir') 1992 1993 " All results 1994 let files = readdirex('Xdir')->map({-> v:val.name}) 1995 call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files)) 1996 let sizes = readdirex('Xdir')->map({-> v:val.size}) 1997 call assert_equal([0, 4, 7], sort(sizes)) 1998 1999 " Only results containing "f" 2000 let files = 'Xdir'->readdirex({ e -> stridx(e.name, 'f') != -1 }) 2001 \ ->map({-> v:val.name}) 2002 call assert_equal(['foo.txt'], sort(files)) 2003 2004 " Only .txt files 2005 let files = readdirex('Xdir', { e -> e.name =~ '.txt$' }) 2006 \ ->map({-> v:val.name}) 2007 call assert_equal(['bar.txt', 'foo.txt'], sort(files)) 2008 2009 " Only .txt files with string 2010 let files = readdirex('Xdir', 'v:val.name =~ ".txt$"') 2011 \ ->map({-> v:val.name}) 2012 call assert_equal(['bar.txt', 'foo.txt'], sort(files)) 2013 2014 " Limit to 1 result. 2015 let l = [] 2016 let files = readdirex('Xdir', {e -> len(add(l, e.name)) == 2 ? -1 : 1}) 2017 \ ->map({-> v:val.name}) 2018 call assert_equal(1, len(files)) 2019 2020 " Nested readdirex() must not crash 2021 let files = readdirex('Xdir', 'readdirex("Xdir", "1") != []') 2022 \ ->map({-> v:val.name}) 2023 call sort(files)->assert_equal(['bar.txt', 'dir', 'foo.txt']) 2024 2025 " report broken link correctly 2026 if has("unix") 2027 call writefile([], 'Xdir/abc.txt') 2028 call system("ln -s Xdir/abc.txt Xdir/link") 2029 call delete('Xdir/abc.txt') 2030 let files = readdirex('Xdir', 'readdirex("Xdir", "1") != []') 2031 \ ->map({-> v:val.name .. '_' .. v:val.type}) 2032 call sort(files)->assert_equal( 2033 \ ['bar.txt_file', 'dir_dir', 'foo.txt_file', 'link_link']) 2034 endif 2035 eval 'Xdir'->delete('rf') 2036 2037 call assert_fails('call readdirex("doesnotexist")', 'E484:') 2038endfunc 2039 2040func Test_readdirex_sort() 2041 CheckUnix 2042 " Skip tests on Mac OS X and Cygwin (does not allow several files with different casing) 2043 if has("osxdarwin") || has("osx") || has("macunix") || has("win32unix") 2044 throw 'Skipped: Test_readdirex_sort on systems that do not allow this using the default filesystem' 2045 endif 2046 let _collate = v:collate 2047 call mkdir('Xdir2') 2048 call writefile(['1'], 'Xdir2/README.txt') 2049 call writefile(['2'], 'Xdir2/Readme.txt') 2050 call writefile(['3'], 'Xdir2/readme.txt') 2051 2052 " 1) default 2053 let files = readdirex('Xdir2')->map({-> v:val.name}) 2054 let default = copy(files) 2055 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort using default') 2056 2057 " 2) no sorting 2058 let files = readdirex('Xdir2', 1, #{sort: 'none'})->map({-> v:val.name}) 2059 let unsorted = copy(files) 2060 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], sort(files), 'unsorted') 2061 call assert_fails("call readdirex('Xdir2', 1, #{slort: 'none'})", 'E857: Dictionary key "sort" required') 2062 2063 " 3) sort by case (same as default) 2064 let files = readdirex('Xdir2', 1, #{sort: 'case'})->map({-> v:val.name}) 2065 call assert_equal(default, files, 'sort by case') 2066 2067 " 4) sort by ignoring case 2068 let files = readdirex('Xdir2', 1, #{sort: 'icase'})->map({-> v:val.name}) 2069 call assert_equal(unsorted->sort('i'), files, 'sort by icase') 2070 2071 " 5) Default Collation 2072 let collate = v:collate 2073 lang collate C 2074 let files = readdirex('Xdir2', 1, #{sort: 'collate'})->map({-> v:val.name}) 2075 call assert_equal(['README.txt', 'Readme.txt', 'readme.txt'], files, 'sort by C collation') 2076 2077 " 6) Collation de_DE 2078 " Switch locale, this may not work on the CI system, if the locale isn't 2079 " available 2080 try 2081 lang collate de_DE 2082 let files = readdirex('Xdir2', 1, #{sort: 'collate'})->map({-> v:val.name}) 2083 call assert_equal(['readme.txt', 'Readme.txt', 'README.txt'], files, 'sort by de_DE collation') 2084 catch 2085 throw 'Skipped: de_DE collation is not available' 2086 2087 finally 2088 exe 'lang collate' collate 2089 eval 'Xdir2'->delete('rf') 2090 endtry 2091endfunc 2092 2093func Test_readdir_sort() 2094 " some more cases for testing sorting for readdirex 2095 let dir = 'Xdir3' 2096 call mkdir(dir) 2097 call writefile(['1'], dir .. '/README.txt') 2098 call writefile(['2'], dir .. '/Readm.txt') 2099 call writefile(['3'], dir .. '/read.txt') 2100 call writefile(['4'], dir .. '/Z.txt') 2101 call writefile(['5'], dir .. '/a.txt') 2102 call writefile(['6'], dir .. '/b.txt') 2103 2104 " 1) default 2105 let files = readdir(dir) 2106 let default = copy(files) 2107 call assert_equal(default->sort(), files, 'sort using default') 2108 2109 " 2) sort by case (same as default) 2110 let files = readdir(dir, '1', #{sort: 'case'}) 2111 call assert_equal(default, files, 'sort using default') 2112 2113 " 3) sort by ignoring case 2114 let files = readdir(dir, '1', #{sort: 'icase'}) 2115 call assert_equal(default->sort('i'), files, 'sort by ignoring case') 2116 2117 " 4) collation 2118 let collate = v:collate 2119 lang collate C 2120 let files = readdir(dir, 1, #{sort: 'collate'}) 2121 call assert_equal(default->sort(), files, 'sort by C collation') 2122 exe "lang collate" collate 2123 2124 " 5) Errors 2125 call assert_fails('call readdir(dir, 1, 1)', 'E715:') 2126 call assert_fails('call readdir(dir, 1, #{sorta: 1})') 2127 call assert_fails('call readdirex(dir, 1, #{sorta: 1})') 2128 2129 " 6) ignore other values in dict 2130 let files = readdir(dir, '1', #{sort: 'c'}) 2131 call assert_equal(default, files, 'sort using default2') 2132 2133 " Cleanup 2134 exe "lang collate" collate 2135 2136 eval dir->delete('rf') 2137endfunc 2138 2139func Test_delete_rf() 2140 call mkdir('Xdir') 2141 call writefile([], 'Xdir/foo.txt') 2142 call writefile([], 'Xdir/bar.txt') 2143 call mkdir('Xdir/[a-1]') " issue #696 2144 call writefile([], 'Xdir/[a-1]/foo.txt') 2145 call writefile([], 'Xdir/[a-1]/bar.txt') 2146 call assert_true(filereadable('Xdir/foo.txt')) 2147 call assert_true('Xdir/[a-1]/foo.txt'->filereadable()) 2148 2149 call assert_equal(0, delete('Xdir', 'rf')) 2150 call assert_false(filereadable('Xdir/foo.txt')) 2151 call assert_false(filereadable('Xdir/[a-1]/foo.txt')) 2152endfunc 2153 2154func Test_call() 2155 call assert_equal(3, call('len', [123])) 2156 call assert_equal(3, 'len'->call([123])) 2157 call assert_fails("call call('len', 123)", 'E714:') 2158 call assert_equal(0, call('', [])) 2159 call assert_equal(0, call('len', test_null_list())) 2160 2161 function Mylen() dict 2162 return len(self.data) 2163 endfunction 2164 let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")} 2165 eval mydict.len->call([], mydict)->assert_equal(4) 2166 call assert_fails("call call('Mylen', [], 0)", 'E715:') 2167 call assert_fails('call foo', 'E107:') 2168 2169 " These once caused a crash. 2170 call call(test_null_function(), []) 2171 call call(test_null_partial(), []) 2172 call assert_fails('call test_null_function()()', 'E1192:') 2173 call assert_fails('call test_null_partial()()', 'E117:') 2174endfunc 2175 2176func Test_char2nr() 2177 call assert_equal(12354, char2nr('あ', 1)) 2178 call assert_equal(120, 'x'->char2nr()) 2179 set encoding=latin1 2180 call assert_equal(120, 'x'->char2nr()) 2181 set encoding=utf-8 2182endfunc 2183 2184func Test_charclass() 2185 call assert_equal(0, charclass(' ')) 2186 call assert_equal(1, charclass('.')) 2187 call assert_equal(2, charclass('x')) 2188 call assert_equal(3, charclass("\u203c")) 2189 " this used to crash vim 2190 call assert_equal(0, "xxx"[-1]->charclass()) 2191endfunc 2192 2193func Test_eventhandler() 2194 call assert_equal(0, eventhandler()) 2195endfunc 2196 2197func Test_bufadd_bufload() 2198 call assert_equal(0, bufexists('someName')) 2199 let buf = bufadd('someName') 2200 call assert_notequal(0, buf) 2201 call assert_equal(1, bufexists('someName')) 2202 call assert_equal(0, getbufvar(buf, '&buflisted')) 2203 call assert_equal(0, bufloaded(buf)) 2204 call bufload(buf) 2205 call assert_equal(1, bufloaded(buf)) 2206 call assert_equal([''], getbufline(buf, 1, '$')) 2207 2208 let curbuf = bufnr('') 2209 eval ['some', 'text']->writefile('XotherName') 2210 let buf = 'XotherName'->bufadd() 2211 call assert_notequal(0, buf) 2212 eval 'XotherName'->bufexists()->assert_equal(1) 2213 call assert_equal(0, getbufvar(buf, '&buflisted')) 2214 call assert_equal(0, bufloaded(buf)) 2215 eval buf->bufload() 2216 call assert_equal(1, bufloaded(buf)) 2217 call assert_equal(['some', 'text'], getbufline(buf, 1, '$')) 2218 call assert_equal(curbuf, bufnr('')) 2219 2220 let buf1 = bufadd('') 2221 let buf2 = bufadd('') 2222 call assert_notequal(0, buf1) 2223 call assert_notequal(0, buf2) 2224 call assert_notequal(buf1, buf2) 2225 call assert_equal(1, bufexists(buf1)) 2226 call assert_equal(1, bufexists(buf2)) 2227 call assert_equal(0, bufloaded(buf1)) 2228 exe 'bwipe ' .. buf1 2229 call assert_equal(0, bufexists(buf1)) 2230 call assert_equal(1, bufexists(buf2)) 2231 exe 'bwipe ' .. buf2 2232 call assert_equal(0, bufexists(buf2)) 2233 2234 bwipe someName 2235 bwipe XotherName 2236 call assert_equal(0, bufexists('someName')) 2237 call delete('XotherName') 2238endfunc 2239 2240func Test_state() 2241 CheckRunVimInTerminal 2242 2243 let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>" 2244 2245 let lines =<< trim END 2246 call setline(1, ['one', 'two', 'three']) 2247 map ;; gg 2248 set complete=. 2249 func RunTimer() 2250 call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')}) 2251 endfunc 2252 au Filetype foobar let g:state = state()|let g:mode = mode() 2253 END 2254 call writefile(lines, 'XState') 2255 let buf = RunVimInTerminal('-S XState', #{rows: 6}) 2256 2257 " Using a ":" command Vim is busy, thus "S" is returned 2258 call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>") 2259 call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000) 2260 call term_sendkeys(buf, ":\<CR>") 2261 2262 " Using a timer callback 2263 call term_sendkeys(buf, ":call RunTimer()\<CR>") 2264 call TermWait(buf, 25) 2265 call term_sendkeys(buf, getstate) 2266 call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000) 2267 2268 " Halfway a mapping 2269 call term_sendkeys(buf, ":call RunTimer()\<CR>;") 2270 call TermWait(buf, 25) 2271 call term_sendkeys(buf, ";") 2272 call term_sendkeys(buf, getstate) 2273 call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000) 2274 2275 " Insert mode completion (bit slower on Mac) 2276 call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>") 2277 call TermWait(buf, 25) 2278 call term_sendkeys(buf, "\<Esc>") 2279 call term_sendkeys(buf, getstate) 2280 call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000) 2281 2282 " Autocommand executing 2283 call term_sendkeys(buf, ":set filetype=foobar\<CR>") 2284 call TermWait(buf, 25) 2285 call term_sendkeys(buf, getstate) 2286 call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000) 2287 2288 " Todo: "w" - waiting for ch_evalexpr() 2289 2290 " messages scrolled 2291 call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>") 2292 call TermWait(buf, 25) 2293 call term_sendkeys(buf, "\<CR>") 2294 call term_sendkeys(buf, getstate) 2295 call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000) 2296 2297 call StopVimInTerminal(buf) 2298 call delete('XState') 2299endfunc 2300 2301func Test_range() 2302 " destructuring 2303 let [x, y] = range(2) 2304 call assert_equal([0, 1], [x, y]) 2305 2306 " index 2307 call assert_equal(4, range(1, 10)[3]) 2308 2309 " add() 2310 call assert_equal([0, 1, 2, 3], add(range(3), 3)) 2311 call assert_equal([0, 1, 2, [0, 1, 2]], add([0, 1, 2], range(3))) 2312 call assert_equal([0, 1, 2, [0, 1, 2]], add(range(3), range(3))) 2313 2314 " append() 2315 new 2316 call append('.', range(5)) 2317 call assert_equal(['', '0', '1', '2', '3', '4'], getline(1, '$')) 2318 bwipe! 2319 2320 " appendbufline() 2321 new 2322 call appendbufline(bufnr(''), '.', range(5)) 2323 call assert_equal(['0', '1', '2', '3', '4', ''], getline(1, '$')) 2324 bwipe! 2325 2326 " call() 2327 func TwoArgs(a, b) 2328 return [a:a, a:b] 2329 endfunc 2330 call assert_equal([0, 1], call('TwoArgs', range(2))) 2331 2332 " col() 2333 new 2334 call setline(1, ['foo', 'bar']) 2335 call assert_equal(2, col(range(1, 2))) 2336 bwipe! 2337 2338 " complete() 2339 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>" 2340 " complete_info() 2341 execute "normal! a\<C-r>=[complete(col('.'), range(10)), ''][1]\<CR>\<C-r>=[complete_info(range(5)), ''][1]\<CR>" 2342 2343 " copy() 2344 call assert_equal([1, 2, 3], copy(range(1, 3))) 2345 2346 " count() 2347 call assert_equal(0, count(range(0), 3)) 2348 call assert_equal(0, count(range(2), 3)) 2349 call assert_equal(1, count(range(5), 3)) 2350 2351 " cursor() 2352 new 2353 call setline(1, ['aaa', 'bbb', 'ccc']) 2354 call cursor(range(1, 2)) 2355 call assert_equal([2, 1], [col('.'), line('.')]) 2356 bwipe! 2357 2358 " deepcopy() 2359 call assert_equal([1, 2, 3], deepcopy(range(1, 3))) 2360 2361 " empty() 2362 call assert_true(empty(range(0))) 2363 call assert_false(empty(range(2))) 2364 2365 " execute() 2366 new 2367 call setline(1, ['aaa', 'bbb', 'ccc']) 2368 call execute(range(3)) 2369 call assert_equal(2, line('.')) 2370 bwipe! 2371 2372 " extend() 2373 call assert_equal([1, 2, 3, 4], extend([1], range(2, 4))) 2374 call assert_equal([1, 2, 3, 4], extend(range(1, 1), range(2, 4))) 2375 call assert_equal([1, 2, 3, 4], extend(range(1, 1), [2, 3, 4])) 2376 2377 " filter() 2378 call assert_equal([1, 3], filter(range(5), 'v:val % 2')) 2379 call assert_equal([1, 5, 7, 11, 13], filter(filter(range(15), 'v:val % 2'), 'v:val % 3')) 2380 2381 " funcref() 2382 call assert_equal([0, 1], funcref('TwoArgs', range(2))()) 2383 2384 " function() 2385 call assert_equal([0, 1], function('TwoArgs', range(2))()) 2386 2387 " garbagecollect() 2388 let thelist = [1, range(2), 3] 2389 let otherlist = range(3) 2390 call test_garbagecollect_now() 2391 2392 " get() 2393 call assert_equal(4, get(range(1, 10), 3)) 2394 call assert_equal(-1, get(range(1, 10), 42, -1)) 2395 2396 " index() 2397 call assert_equal(1, index(range(1, 5), 2)) 2398 call assert_fails("echo index([1, 2], 1, [])", 'E745:') 2399 2400 " inputlist() 2401 call feedkeys(":let result = inputlist(range(10))\<CR>1\<CR>", 'x') 2402 call assert_equal(1, result) 2403 call feedkeys(":let result = inputlist(range(3, 10))\<CR>1\<CR>", 'x') 2404 call assert_equal(1, result) 2405 2406 " insert() 2407 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42)) 2408 call assert_equal([42, 1, 2, 3, 4, 5], insert(range(1, 5), 42, 0)) 2409 call assert_equal([1, 42, 2, 3, 4, 5], insert(range(1, 5), 42, 1)) 2410 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, 4)) 2411 call assert_equal([1, 2, 3, 4, 42, 5], insert(range(1, 5), 42, -1)) 2412 call assert_equal([1, 2, 3, 4, 5, 42], insert(range(1, 5), 42, 5)) 2413 2414 " join() 2415 call assert_equal('0 1 2 3 4', join(range(5))) 2416 2417 " json_encode() 2418 call assert_equal('[0,1,2,3]', json_encode(range(4))) 2419 2420 " len() 2421 call assert_equal(0, len(range(0))) 2422 call assert_equal(2, len(range(2))) 2423 call assert_equal(5, len(range(0, 12, 3))) 2424 call assert_equal(4, len(range(3, 0, -1))) 2425 2426 " list2str() 2427 call assert_equal('ABC', list2str(range(65, 67))) 2428 call assert_fails('let s = list2str(5)', 'E474:') 2429 2430 " lock() 2431 let thelist = range(5) 2432 lockvar thelist 2433 2434 " map() 2435 call assert_equal([0, 2, 4, 6, 8], map(range(5), 'v:val * 2')) 2436 call assert_equal([3, 5, 7, 9, 11], map(map(range(5), 'v:val * 2'), 'v:val + 3')) 2437 call assert_equal([2, 6], map(filter(range(5), 'v:val % 2'), 'v:val * 2')) 2438 call assert_equal([2, 4, 8], filter(map(range(5), 'v:val * 2'), 'v:val % 3')) 2439 2440 " match() 2441 call assert_equal(3, match(range(5), 3)) 2442 2443 " matchaddpos() 2444 highlight MyGreenGroup ctermbg=green guibg=green 2445 call matchaddpos('MyGreenGroup', range(line('.'), line('.'))) 2446 2447 " matchend() 2448 call assert_equal(4, matchend(range(5), '4')) 2449 call assert_equal(3, matchend(range(1, 5), '4')) 2450 call assert_equal(-1, matchend(range(1, 5), '42')) 2451 2452 " matchstrpos() 2453 call assert_equal(['4', 4, 0, 1], matchstrpos(range(5), '4')) 2454 call assert_equal(['4', 3, 0, 1], matchstrpos(range(1, 5), '4')) 2455 call assert_equal(['', -1, -1, -1], matchstrpos(range(1, 5), '42')) 2456 2457 " max() reverse() 2458 call assert_equal(0, max(range(0))) 2459 call assert_equal(0, max(range(10, 9))) 2460 call assert_equal(9, max(range(10))) 2461 call assert_equal(18, max(range(0, 20, 3))) 2462 call assert_equal(20, max(range(20, 0, -3))) 2463 call assert_equal(99999, max(range(100000))) 2464 call assert_equal(99999, max(range(99999, 0, -1))) 2465 call assert_equal(99999, max(reverse(range(100000)))) 2466 call assert_equal(99999, max(reverse(range(99999, 0, -1)))) 2467 2468 " min() reverse() 2469 call assert_equal(0, min(range(0))) 2470 call assert_equal(0, min(range(10, 9))) 2471 call assert_equal(5, min(range(5, 10))) 2472 call assert_equal(5, min(range(5, 10, 3))) 2473 call assert_equal(2, min(range(20, 0, -3))) 2474 call assert_equal(0, min(range(100000))) 2475 call assert_equal(0, min(range(99999, 0, -1))) 2476 call assert_equal(0, min(reverse(range(100000)))) 2477 call assert_equal(0, min(reverse(range(99999, 0, -1)))) 2478 2479 " remove() 2480 call assert_equal(1, remove(range(1, 10), 0)) 2481 call assert_equal(2, remove(range(1, 10), 1)) 2482 call assert_equal(9, remove(range(1, 10), 8)) 2483 call assert_equal(10, remove(range(1, 10), 9)) 2484 call assert_equal(10, remove(range(1, 10), -1)) 2485 call assert_equal([3, 4, 5], remove(range(1, 10), 2, 4)) 2486 2487 " repeat() 2488 call assert_equal([0, 1, 2, 0, 1, 2], repeat(range(3), 2)) 2489 call assert_equal([0, 1, 2], repeat(range(3), 1)) 2490 call assert_equal([], repeat(range(3), 0)) 2491 call assert_equal([], repeat(range(5, 4), 2)) 2492 call assert_equal([], repeat(range(5, 4), 0)) 2493 2494 " reverse() 2495 call assert_equal([2, 1, 0], reverse(range(3))) 2496 call assert_equal([0, 1, 2, 3], reverse(range(3, 0, -1))) 2497 call assert_equal([9, 8, 7, 6, 5, 4, 3, 2, 1, 0], reverse(range(10))) 2498 call assert_equal([20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], reverse(range(10, 20))) 2499 call assert_equal([16, 13, 10], reverse(range(10, 18, 3))) 2500 call assert_equal([19, 16, 13, 10], reverse(range(10, 19, 3))) 2501 call assert_equal([19, 16, 13, 10], reverse(range(10, 20, 3))) 2502 call assert_equal([11, 14, 17, 20], reverse(range(20, 10, -3))) 2503 call assert_equal([], reverse(range(0))) 2504 2505 " TODO: setpos() 2506 " new 2507 " call setline(1, repeat([''], bufnr(''))) 2508 " call setline(bufnr('') + 1, repeat('x', bufnr('') * 2 + 6)) 2509 " call setpos('x', range(bufnr(''), bufnr('') + 3)) 2510 " bwipe! 2511 2512 " setreg() 2513 call setreg('a', range(3)) 2514 call assert_equal("0\n1\n2\n", getreg('a')) 2515 2516 " settagstack() 2517 call settagstack(1, #{items : range(4)}) 2518 2519 " sign_define() 2520 call assert_fails("call sign_define(range(5))", "E715:") 2521 call assert_fails("call sign_placelist(range(5))", "E715:") 2522 2523 " sign_undefine() 2524 call assert_fails("call sign_undefine(range(5))", "E908:") 2525 2526 " sign_unplacelist() 2527 call assert_fails("call sign_unplacelist(range(5))", "E715:") 2528 2529 " sort() 2530 call assert_equal([0, 1, 2, 3, 4, 5], sort(range(5, 0, -1))) 2531 2532 " string() 2533 call assert_equal('[0, 1, 2, 3, 4]', string(range(5))) 2534 2535 " taglist() with 'tagfunc' 2536 func TagFunc(pattern, flags, info) 2537 return range(10) 2538 endfunc 2539 set tagfunc=TagFunc 2540 call assert_fails("call taglist('asdf')", 'E987:') 2541 set tagfunc= 2542 2543 " term_start() 2544 if has('terminal') && has('termguicolors') 2545 call assert_fails('call term_start(range(3, 4))', 'E474:') 2546 let g:terminal_ansi_colors = range(16) 2547 if has('win32') 2548 let cmd = "cmd /c dir" 2549 else 2550 let cmd = "ls" 2551 endif 2552 call assert_fails('call term_start("' .. cmd .. '", #{term_finish: "close"})', 'E475:') 2553 unlet g:terminal_ansi_colors 2554 endif 2555 2556 " type() 2557 call assert_equal(v:t_list, type(range(5))) 2558 2559 " uniq() 2560 call assert_equal([0, 1, 2, 3, 4], uniq(range(5))) 2561 2562 " errors 2563 call assert_fails('let x=range(2, 8, 0)', 'E726:') 2564 call assert_fails('let x=range(3, 1)', 'E727:') 2565 call assert_fails('let x=range(1, 3, -2)', 'E727:') 2566 call assert_fails('let x=range([])', 'E745:') 2567 call assert_fails('let x=range(1, [])', 'E745:') 2568 call assert_fails('let x=range(1, 4, [])', 'E745:') 2569endfunc 2570 2571func Test_echoraw() 2572 CheckScreendump 2573 2574 " Normally used for escape codes, but let's test with a CR. 2575 let lines =<< trim END 2576 call echoraw("hello\<CR>x") 2577 END 2578 call writefile(lines, 'XTest_echoraw') 2579 let buf = RunVimInTerminal('-S XTest_echoraw', {'rows': 5, 'cols': 40}) 2580 call VerifyScreenDump(buf, 'Test_functions_echoraw', {}) 2581 2582 " clean up 2583 call StopVimInTerminal(buf) 2584 call delete('XTest_echoraw') 2585endfunc 2586 2587" Test for echo highlighting 2588func Test_echohl() 2589 echohl Search 2590 echo 'Vim' 2591 call assert_equal('Vim', Screenline(&lines)) 2592 " TODO: How to check the highlight group used by echohl? 2593 " ScreenAttrs() returns all zeros. 2594 echohl None 2595endfunc 2596 2597" Test for the eval() function 2598func Test_eval() 2599 call assert_fails("call eval('5 a')", 'E488:') 2600endfunc 2601 2602" Test for the nr2char() function 2603func Test_nr2char() 2604 set encoding=latin1 2605 call assert_equal('@', nr2char(64)) 2606 set encoding=utf8 2607 call assert_equal('a', nr2char(97, 1)) 2608 call assert_equal('a', nr2char(97, 0)) 2609 2610 call assert_equal("\x80\xfc\b\xf4\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x100000) .. '>"')) 2611 call assert_equal("\x80\xfc\b\xfd\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX\x80\xfeX", eval('"\<M-' .. nr2char(0x40000000) .. '>"')) 2612endfunc 2613 2614" Test for screenattr(), screenchar() and screenchars() functions 2615func Test_screen_functions() 2616 call assert_equal(-1, screenattr(-1, -1)) 2617 call assert_equal(-1, screenchar(-1, -1)) 2618 call assert_equal([], screenchars(-1, -1)) 2619endfunc 2620 2621" Test for getcurpos() and setpos() 2622func Test_getcurpos_setpos() 2623 new 2624 call setline(1, ['012345678', '012345678']) 2625 normal gg6l 2626 let sp = getcurpos() 2627 normal 0 2628 call setpos('.', sp) 2629 normal jyl 2630 call assert_equal('6', @") 2631 call assert_equal(-1, setpos('.', test_null_list())) 2632 call assert_equal(-1, setpos('.', {})) 2633 2634 let winid = win_getid() 2635 normal G$ 2636 let pos = getcurpos() 2637 wincmd w 2638 call assert_equal(pos, getcurpos(winid)) 2639 2640 wincmd w 2641 close! 2642 2643 call assert_equal(getcurpos(), getcurpos(0)) 2644 call assert_equal([0, 0, 0, 0, 0], getcurpos(-1)) 2645 call assert_equal([0, 0, 0, 0, 0], getcurpos(1999)) 2646endfunc 2647 2648" Test for glob() 2649func Test_glob() 2650 call assert_equal('', glob(test_null_string())) 2651 call assert_equal('', globpath(test_null_string(), test_null_string())) 2652 call assert_fails("let x = globpath(&rtp, 'syntax/c.vim', [])", 'E745:') 2653 2654 call writefile([], 'Xglob1') 2655 call writefile([], 'XGLOB2') 2656 set wildignorecase 2657 " Sort output of glob() otherwise we end up with different 2658 " ordering depending on whether file system is case-sensitive. 2659 call assert_equal(['XGLOB2', 'Xglob1'], sort(glob('Xglob[12]', 0, 1))) 2660 set wildignorecase& 2661 2662 call delete('Xglob1') 2663 call delete('XGLOB2') 2664 2665 call assert_fails("call glob('*', 0, {})", 'E728:') 2666endfunc 2667 2668" Test for browse() 2669func Test_browse() 2670 CheckFeature browse 2671 call assert_fails('call browse([], "open", "x", "a.c")', 'E745:') 2672endfunc 2673 2674" Test for browsedir() 2675func Test_browsedir() 2676 CheckFeature browse 2677 call assert_fails('call browsedir("open", [])', 'E730:') 2678endfunc 2679 2680func HasDefault(msg = 'msg') 2681 return a:msg 2682endfunc 2683 2684func Test_default_arg_value() 2685 call assert_equal('msg', HasDefault()) 2686endfunc 2687 2688" Test for gettext() 2689func Test_gettext() 2690 call assert_fails('call gettext(1)', 'E475:') 2691endfunc 2692 2693func Test_builtin_check() 2694 call assert_fails('let g:["trim"] = {x -> " " .. x}', 'E704:') 2695 call assert_fails('let g:.trim = {x -> " " .. x}', 'E704:') 2696 call assert_fails('let l:["trim"] = {x -> " " .. x}', 'E704:') 2697 call assert_fails('let l:.trim = {x -> " " .. x}', 'E704:') 2698 let lines =<< trim END 2699 vim9script 2700 var s:trim = (x) => " " .. x 2701 END 2702 call CheckScriptFailure(lines, 'E704:') 2703 2704 call assert_fails('call extend(g:, #{foo: { -> "foo" }})', 'E704:') 2705 let g:bar = 123 2706 call extend(g:, #{bar: { -> "foo" }}, "keep") 2707 call assert_fails('call extend(g:, #{bar: { -> "foo" }}, "force")', 'E704:') 2708endfunc 2709 2710 2711" vim: shiftwidth=2 sts=2 expandtab 2712