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