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