1" Tests for various functions. 2source shared.vim 3 4" Must be done first, since the alternate buffer must be unset. 5func Test_00_bufexists() 6 call assert_equal(0, bufexists('does_not_exist')) 7 call assert_equal(1, bufexists(bufnr('%'))) 8 call assert_equal(0, bufexists(0)) 9 new Xfoo 10 let bn = bufnr('%') 11 call assert_equal(1, bufexists(bn)) 12 call assert_equal(1, bufexists('Xfoo')) 13 call assert_equal(1, bufexists(getcwd() . '/Xfoo')) 14 call assert_equal(1, bufexists(0)) 15 bw 16 call assert_equal(0, bufexists(bn)) 17 call assert_equal(0, bufexists('Xfoo')) 18endfunc 19 20func Test_empty() 21 call assert_equal(1, empty('')) 22 call assert_equal(0, empty('a')) 23 24 call assert_equal(1, empty(0)) 25 call assert_equal(1, empty(-0)) 26 call assert_equal(0, empty(1)) 27 call assert_equal(0, empty(-1)) 28 29 call assert_equal(1, empty(0.0)) 30 call assert_equal(1, empty(-0.0)) 31 call assert_equal(0, empty(1.0)) 32 call assert_equal(0, empty(-1.0)) 33 call assert_equal(0, empty(1.0/0.0)) 34 call assert_equal(0, empty(0.0/0.0)) 35 36 call assert_equal(1, empty([])) 37 call assert_equal(0, empty(['a'])) 38 39 call assert_equal(1, empty({})) 40 call assert_equal(0, empty({'a':1})) 41 42 call assert_equal(1, empty(v:null)) 43 call assert_equal(1, empty(v:none)) 44 call assert_equal(1, empty(v:false)) 45 call assert_equal(0, empty(v:true)) 46 47 if has('channel') 48 call assert_equal(1, empty(test_null_channel())) 49 endif 50 if has('job') 51 call assert_equal(1, empty(test_null_job())) 52 endif 53 54 call assert_equal(0, empty(function('Test_empty'))) 55endfunc 56 57func Test_len() 58 call assert_equal(1, len(0)) 59 call assert_equal(2, len(12)) 60 61 call assert_equal(0, len('')) 62 call assert_equal(2, len('ab')) 63 64 call assert_equal(0, len([])) 65 call assert_equal(2, len([2, 1])) 66 67 call assert_equal(0, len({})) 68 call assert_equal(2, len({'a': 1, 'b': 2})) 69 70 call assert_fails('call len(v:none)', 'E701:') 71 call assert_fails('call len({-> 0})', 'E701:') 72endfunc 73 74func Test_max() 75 call assert_equal(0, max([])) 76 call assert_equal(2, max([2])) 77 call assert_equal(2, max([1, 2])) 78 call assert_equal(2, max([1, 2, v:null])) 79 80 call assert_equal(0, max({})) 81 call assert_equal(2, max({'a':1, 'b':2})) 82 83 call assert_fails('call max(1)', 'E712:') 84 call assert_fails('call max(v:none)', 'E712:') 85endfunc 86 87func Test_min() 88 call assert_equal(0, min([])) 89 call assert_equal(2, min([2])) 90 call assert_equal(1, min([1, 2])) 91 call assert_equal(0, min([1, 2, v:null])) 92 93 call assert_equal(0, min({})) 94 call assert_equal(1, min({'a':1, 'b':2})) 95 96 call assert_fails('call min(1)', 'E712:') 97 call assert_fails('call min(v:none)', 'E712:') 98endfunc 99 100func Test_strwidth() 101 for aw in ['single', 'double'] 102 exe 'set ambiwidth=' . aw 103 call assert_equal(0, strwidth('')) 104 call assert_equal(1, strwidth("\t")) 105 call assert_equal(3, strwidth('Vim')) 106 call assert_equal(4, strwidth(1234)) 107 call assert_equal(5, strwidth(-1234)) 108 109 call assert_equal(2, strwidth('')) 110 call assert_equal(17, strwidth('Eĥoŝanĝo ĉiuĵaŭde')) 111 call assert_equal((aw == 'single') ? 6 : 7, strwidth('Straße')) 112 113 call assert_fails('call strwidth({->0})', 'E729:') 114 call assert_fails('call strwidth([])', 'E730:') 115 call assert_fails('call strwidth({})', 'E731:') 116 call assert_fails('call strwidth(1.2)', 'E806:') 117 endfor 118 119 set ambiwidth& 120endfunc 121 122func Test_str2nr() 123 call assert_equal(0, str2nr('')) 124 call assert_equal(1, str2nr('1')) 125 call assert_equal(1, str2nr(' 1 ')) 126 127 call assert_equal(1, str2nr('+1')) 128 call assert_equal(1, str2nr('+ 1')) 129 call assert_equal(1, str2nr(' + 1 ')) 130 131 call assert_equal(-1, str2nr('-1')) 132 call assert_equal(-1, str2nr('- 1')) 133 call assert_equal(-1, str2nr(' - 1 ')) 134 135 call assert_equal(123456789, str2nr('123456789')) 136 call assert_equal(-123456789, str2nr('-123456789')) 137 138 call assert_equal(5, str2nr('101', 2)) 139 call assert_equal(5, str2nr('0b101', 2)) 140 call assert_equal(5, str2nr('0B101', 2)) 141 call assert_equal(-5, str2nr('-101', 2)) 142 call assert_equal(-5, str2nr('-0b101', 2)) 143 call assert_equal(-5, str2nr('-0B101', 2)) 144 145 call assert_equal(65, str2nr('101', 8)) 146 call assert_equal(65, str2nr('0101', 8)) 147 call assert_equal(-65, str2nr('-101', 8)) 148 call assert_equal(-65, str2nr('-0101', 8)) 149 150 call assert_equal(11259375, str2nr('abcdef', 16)) 151 call assert_equal(11259375, str2nr('ABCDEF', 16)) 152 call assert_equal(-11259375, str2nr('-ABCDEF', 16)) 153 call assert_equal(11259375, str2nr('0xabcdef', 16)) 154 call assert_equal(11259375, str2nr('0Xabcdef', 16)) 155 call assert_equal(11259375, str2nr('0XABCDEF', 16)) 156 call assert_equal(-11259375, str2nr('-0xABCDEF', 16)) 157 158 call assert_equal(0, str2nr('0x10')) 159 call assert_equal(0, str2nr('0b10')) 160 call assert_equal(1, str2nr('12', 2)) 161 call assert_equal(1, str2nr('18', 8)) 162 call assert_equal(1, str2nr('1g', 16)) 163 164 call assert_equal(0, str2nr(v:null)) 165 call assert_equal(0, str2nr(v:none)) 166 167 call assert_fails('call str2nr([])', 'E730:') 168 call assert_fails('call str2nr({->2})', 'E729:') 169 call assert_fails('call str2nr(1.2)', 'E806:') 170 call assert_fails('call str2nr(10, [])', 'E474:') 171endfunc 172 173func Test_strftime() 174 if !exists('*strftime') 175 return 176 endif 177 " Format of strftime() depends on system. We assume 178 " that basic formats tested here are available and 179 " identical on all systems which support strftime(). 180 " 181 " The 2nd parameter of strftime() is a local time, so the output day 182 " of strftime() can be 17 or 18, depending on timezone. 183 call assert_match('^2017-01-1[78]$', strftime('%Y-%m-%d', 1484695512)) 184 " 185 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\)$', strftime('%Y-%m-%d %H:%M:%S')) 186 187 call assert_fails('call strftime([])', 'E730:') 188 call assert_fails('call strftime("%Y", [])', 'E745:') 189endfunc 190 191func Test_resolve_unix() 192 if !has('unix') 193 return 194 endif 195 196 " Xlink1 -> Xlink2 197 " Xlink2 -> Xlink3 198 silent !ln -s -f Xlink2 Xlink1 199 silent !ln -s -f Xlink3 Xlink2 200 call assert_equal('Xlink3', resolve('Xlink1')) 201 call assert_equal('./Xlink3', resolve('./Xlink1')) 202 call assert_equal('Xlink3/', resolve('Xlink2/')) 203 " FIXME: these tests result in things like "Xlink2/" instead of "Xlink3/"?! 204 "call assert_equal('Xlink3/', resolve('Xlink1/')) 205 "call assert_equal('./Xlink3/', resolve('./Xlink1/')) 206 "call assert_equal(getcwd() . '/Xlink3/', resolve(getcwd() . '/Xlink1/')) 207 call assert_equal(getcwd() . '/Xlink3', resolve(getcwd() . '/Xlink1')) 208 209 " Test resolve() with a symlink cycle. 210 " Xlink1 -> Xlink2 211 " Xlink2 -> Xlink3 212 " Xlink3 -> Xlink1 213 silent !ln -s -f Xlink1 Xlink3 214 call assert_fails('call resolve("Xlink1")', 'E655:') 215 call assert_fails('call resolve("./Xlink1")', 'E655:') 216 call assert_fails('call resolve("Xlink2")', 'E655:') 217 call assert_fails('call resolve("Xlink3")', 'E655:') 218 call delete('Xlink1') 219 call delete('Xlink2') 220 call delete('Xlink3') 221 222 silent !ln -s -f Xdir//Xfile Xlink 223 call assert_equal('Xdir/Xfile', resolve('Xlink')) 224 call delete('Xlink') 225 226 silent !ln -s -f Xlink2/ Xlink1 227 call assert_equal('Xlink2', resolve('Xlink1')) 228 call assert_equal('Xlink2/', resolve('Xlink1/')) 229 call delete('Xlink1') 230 231 silent !ln -s -f ./Xlink2 Xlink1 232 call assert_equal('Xlink2', resolve('Xlink1')) 233 call assert_equal('./Xlink2', resolve('./Xlink1')) 234 call delete('Xlink1') 235endfunc 236 237func s:normalize_fname(fname) 238 let ret = substitute(a:fname, '\', '/', 'g') 239 let ret = substitute(ret, '//', '/', 'g') 240 let ret = tolower(ret) 241endfunc 242 243func Test_resolve_win32() 244 if !has('win32') 245 return 246 endif 247 248 " test for shortcut file 249 if executable('cscript') 250 new Xfile 251 wq 252 call writefile([ 253 \ 'Set fs = CreateObject("Scripting.FileSystemObject")', 254 \ 'Set ws = WScript.CreateObject("WScript.Shell")', 255 \ 'Set shortcut = ws.CreateShortcut("Xlink.lnk")', 256 \ 'shortcut.TargetPath = fs.BuildPath(ws.CurrentDirectory, "Xfile")', 257 \ 'shortcut.Save' 258 \], 'link.vbs') 259 silent !cscript link.vbs 260 call delete('link.vbs') 261 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk'))) 262 call delete('Xfile') 263 264 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink.lnk'))) 265 call delete('Xlink.lnk') 266 else 267 echomsg 'skipped test for shortcut file' 268 endif 269 270 " remove files 271 call delete('Xlink') 272 call delete('Xdir', 'd') 273 call delete('Xfile') 274 275 " test for symbolic link to a file 276 new Xfile 277 wq 278 silent !mklink Xlink Xfile 279 if !v:shell_error 280 call assert_equal(s:normalize_fname(getcwd() . '\Xfile'), s:normalize_fname(resolve('./Xlink'))) 281 call delete('Xlink') 282 else 283 echomsg 'skipped test for symbolic link to a file' 284 endif 285 call delete('Xfile') 286 287 " test for junction to a directory 288 call mkdir('Xdir') 289 silent !mklink /J Xlink Xdir 290 if !v:shell_error 291 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) 292 293 call delete('Xdir', 'd') 294 295 " test for junction already removed 296 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) 297 call delete('Xlink') 298 else 299 echomsg 'skipped test for junction to a directory' 300 call delete('Xdir', 'd') 301 endif 302 303 " test for symbolic link to a directory 304 call mkdir('Xdir') 305 silent !mklink /D Xlink Xdir 306 if !v:shell_error 307 call assert_equal(s:normalize_fname(getcwd() . '\Xdir'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) 308 309 call delete('Xdir', 'd') 310 311 " test for symbolic link already removed 312 call assert_equal(s:normalize_fname(getcwd() . '\Xlink'), s:normalize_fname(resolve(getcwd() . '/Xlink'))) 313 call delete('Xlink') 314 else 315 echomsg 'skipped test for symbolic link to a directory' 316 call delete('Xdir', 'd') 317 endif 318 319 " test for buffer name 320 new Xfile 321 wq 322 silent !mklink Xlink Xfile 323 if !v:shell_error 324 edit Xlink 325 call assert_equal('Xlink', bufname('%')) 326 call delete('Xlink') 327 bw! 328 else 329 echomsg 'skipped test for buffer name' 330 endif 331 call delete('Xfile') 332endfunc 333 334func Test_simplify() 335 call assert_equal('', simplify('')) 336 call assert_equal('/', simplify('/')) 337 call assert_equal('/', simplify('/.')) 338 call assert_equal('/', simplify('/..')) 339 call assert_equal('/...', simplify('/...')) 340 call assert_equal('./dir/file', simplify('./dir/file')) 341 call assert_equal('./dir/file', simplify('.///dir//file')) 342 call assert_equal('./dir/file', simplify('./dir/./file')) 343 call assert_equal('./file', simplify('./dir/../file')) 344 call assert_equal('../dir/file', simplify('dir/../../dir/file')) 345 call assert_equal('./file', simplify('dir/.././file')) 346 347 call assert_fails('call simplify({->0})', 'E729:') 348 call assert_fails('call simplify([])', 'E730:') 349 call assert_fails('call simplify({})', 'E731:') 350 call assert_fails('call simplify(1.2)', 'E806:') 351endfunc 352 353func Test_pathshorten() 354 call assert_equal('', pathshorten('')) 355 call assert_equal('foo', pathshorten('foo')) 356 call assert_equal('/foo', pathshorten('/foo')) 357 call assert_equal('f/', pathshorten('foo/')) 358 call assert_equal('f/bar', pathshorten('foo/bar')) 359 call assert_equal('f/b/foobar', pathshorten('foo/bar/foobar')) 360 call assert_equal('/f/b/foobar', pathshorten('/foo/bar/foobar')) 361 call assert_equal('.f/bar', pathshorten('.foo/bar')) 362 call assert_equal('~f/bar', pathshorten('~foo/bar')) 363 call assert_equal('~.f/bar', pathshorten('~.foo/bar')) 364 call assert_equal('.~f/bar', pathshorten('.~foo/bar')) 365 call assert_equal('~/f/bar', pathshorten('~/foo/bar')) 366endfunc 367 368func Test_strpart() 369 call assert_equal('de', strpart('abcdefg', 3, 2)) 370 call assert_equal('ab', strpart('abcdefg', -2, 4)) 371 call assert_equal('abcdefg', strpart('abcdefg', -2)) 372 call assert_equal('fg', strpart('abcdefg', 5, 4)) 373 call assert_equal('defg', strpart('abcdefg', 3)) 374 375 call assert_equal('lép', strpart('éléphant', 2, 4)) 376 call assert_equal('léphant', strpart('éléphant', 2)) 377endfunc 378 379func Test_tolower() 380 call assert_equal("", tolower("")) 381 382 " Test with all printable ASCII characters. 383 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 384 \ tolower(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) 385 386 " Test with a few uppercase diacritics. 387 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) 388 call assert_equal("bḃḇ", tolower("BḂḆ")) 389 call assert_equal("cçćĉċč", tolower("CÇĆĈĊČ")) 390 call assert_equal("dďđḋḏḑ", tolower("DĎĐḊḎḐ")) 391 call assert_equal("eèéêëēĕėęěẻẽ", tolower("EÈÉÊËĒĔĖĘĚẺẼ")) 392 call assert_equal("fḟ ", tolower("FḞ ")) 393 call assert_equal("gĝğġģǥǧǵḡ", tolower("GĜĞĠĢǤǦǴḠ")) 394 call assert_equal("hĥħḣḧḩ", tolower("HĤĦḢḦḨ")) 395 call assert_equal("iìíîïĩīĭįiǐỉ", tolower("IÌÍÎÏĨĪĬĮİǏỈ")) 396 call assert_equal("jĵ", tolower("JĴ")) 397 call assert_equal("kķǩḱḵ", tolower("KĶǨḰḴ")) 398 call assert_equal("lĺļľŀłḻ", tolower("LĹĻĽĿŁḺ")) 399 call assert_equal("mḿṁ", tolower("MḾṀ")) 400 call assert_equal("nñńņňṅṉ", tolower("NÑŃŅŇṄṈ")) 401 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) 402 call assert_equal("pṕṗ", tolower("PṔṖ")) 403 call assert_equal("q", tolower("Q")) 404 call assert_equal("rŕŗřṙṟ", tolower("RŔŖŘṘṞ")) 405 call assert_equal("sśŝşšṡ", tolower("SŚŜŞŠṠ")) 406 call assert_equal("tţťŧṫṯ", tolower("TŢŤŦṪṮ")) 407 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) 408 call assert_equal("vṽ", tolower("VṼ")) 409 call assert_equal("wŵẁẃẅẇ", tolower("WŴẀẂẄẆ")) 410 call assert_equal("xẋẍ", tolower("XẊẌ")) 411 call assert_equal("yýŷÿẏỳỷỹ", tolower("YÝŶŸẎỲỶỸ")) 412 call assert_equal("zźżžƶẑẕ", tolower("ZŹŻŽƵẐẔ")) 413 414 " Test with a few lowercase diacritics, which should remain unchanged. 415 call assert_equal("aàáâãäåāăąǎǟǡả", tolower("aàáâãäåāăąǎǟǡả")) 416 call assert_equal("bḃḇ", tolower("bḃḇ")) 417 call assert_equal("cçćĉċč", tolower("cçćĉċč")) 418 call assert_equal("dďđḋḏḑ", tolower("dďđḋḏḑ")) 419 call assert_equal("eèéêëēĕėęěẻẽ", tolower("eèéêëēĕėęěẻẽ")) 420 call assert_equal("fḟ", tolower("fḟ")) 421 call assert_equal("gĝğġģǥǧǵḡ", tolower("gĝğġģǥǧǵḡ")) 422 call assert_equal("hĥħḣḧḩẖ", tolower("hĥħḣḧḩẖ")) 423 call assert_equal("iìíîïĩīĭįǐỉ", tolower("iìíîïĩīĭįǐỉ")) 424 call assert_equal("jĵǰ", tolower("jĵǰ")) 425 call assert_equal("kķǩḱḵ", tolower("kķǩḱḵ")) 426 call assert_equal("lĺļľŀłḻ", tolower("lĺļľŀłḻ")) 427 call assert_equal("mḿṁ ", tolower("mḿṁ ")) 428 call assert_equal("nñńņňʼnṅṉ", tolower("nñńņňʼnṅṉ")) 429 call assert_equal("oòóôõöøōŏőơǒǫǭỏ", tolower("oòóôõöøōŏőơǒǫǭỏ")) 430 call assert_equal("pṕṗ", tolower("pṕṗ")) 431 call assert_equal("q", tolower("q")) 432 call assert_equal("rŕŗřṙṟ", tolower("rŕŗřṙṟ")) 433 call assert_equal("sśŝşšṡ", tolower("sśŝşšṡ")) 434 call assert_equal("tţťŧṫṯẗ", tolower("tţťŧṫṯẗ")) 435 call assert_equal("uùúûüũūŭůűųưǔủ", tolower("uùúûüũūŭůűųưǔủ")) 436 call assert_equal("vṽ", tolower("vṽ")) 437 call assert_equal("wŵẁẃẅẇẘ", tolower("wŵẁẃẅẇẘ")) 438 call assert_equal("ẋẍ", tolower("ẋẍ")) 439 call assert_equal("yýÿŷẏẙỳỷỹ", tolower("yýÿŷẏẙỳỷỹ")) 440 call assert_equal("zźżžƶẑẕ", tolower("zźżžƶẑẕ")) 441 442 " According to https://twitter.com/jifa/status/625776454479970304 443 " Ⱥ (U+023A) and Ⱦ (U+023E) are the *only* code points to increase 444 " in length (2 to 3 bytes) when lowercased. So let's test them. 445 call assert_equal("ⱥ ⱦ", tolower("Ⱥ Ⱦ")) 446 447 " This call to tolower with invalid utf8 sequence used to cause access to 448 " invalid memory. 449 call tolower("\xC0\x80\xC0") 450 call tolower("123\xC0\x80\xC0") 451endfunc 452 453func Test_toupper() 454 call assert_equal("", toupper("")) 455 456 " Test with all printable ASCII characters. 457 call assert_equal(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~', 458 \ toupper(' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~')) 459 460 " Test with a few lowercase diacritics. 461 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("aàáâãäåāăąǎǟǡả")) 462 call assert_equal("BḂḆ", toupper("bḃḇ")) 463 call assert_equal("CÇĆĈĊČ", toupper("cçćĉċč")) 464 call assert_equal("DĎĐḊḎḐ", toupper("dďđḋḏḑ")) 465 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("eèéêëēĕėęěẻẽ")) 466 call assert_equal("FḞ", toupper("fḟ")) 467 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("gĝğġģǥǧǵḡ")) 468 call assert_equal("HĤĦḢḦḨẖ", toupper("hĥħḣḧḩẖ")) 469 call assert_equal("IÌÍÎÏĨĪĬĮǏỈ", toupper("iìíîïĩīĭįǐỉ")) 470 call assert_equal("JĴǰ", toupper("jĵǰ")) 471 call assert_equal("KĶǨḰḴ", toupper("kķǩḱḵ")) 472 call assert_equal("LĹĻĽĿŁḺ", toupper("lĺļľŀłḻ")) 473 call assert_equal("MḾṀ ", toupper("mḿṁ ")) 474 call assert_equal("NÑŃŅŇʼnṄṈ", toupper("nñńņňʼnṅṉ")) 475 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("oòóôõöøōŏőơǒǫǭỏ")) 476 call assert_equal("PṔṖ", toupper("pṕṗ")) 477 call assert_equal("Q", toupper("q")) 478 call assert_equal("RŔŖŘṘṞ", toupper("rŕŗřṙṟ")) 479 call assert_equal("SŚŜŞŠṠ", toupper("sśŝşšṡ")) 480 call assert_equal("TŢŤŦṪṮẗ", toupper("tţťŧṫṯẗ")) 481 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("uùúûüũūŭůűųưǔủ")) 482 call assert_equal("VṼ", toupper("vṽ")) 483 call assert_equal("WŴẀẂẄẆẘ", toupper("wŵẁẃẅẇẘ")) 484 call assert_equal("ẊẌ", toupper("ẋẍ")) 485 call assert_equal("YÝŸŶẎẙỲỶỸ", toupper("yýÿŷẏẙỳỷỹ")) 486 call assert_equal("ZŹŻŽƵẐẔ", toupper("zźżžƶẑẕ")) 487 488 " Test that uppercase diacritics, which should remain unchanged. 489 call assert_equal("AÀÁÂÃÄÅĀĂĄǍǞǠẢ", toupper("AÀÁÂÃÄÅĀĂĄǍǞǠẢ")) 490 call assert_equal("BḂḆ", toupper("BḂḆ")) 491 call assert_equal("CÇĆĈĊČ", toupper("CÇĆĈĊČ")) 492 call assert_equal("DĎĐḊḎḐ", toupper("DĎĐḊḎḐ")) 493 call assert_equal("EÈÉÊËĒĔĖĘĚẺẼ", toupper("EÈÉÊËĒĔĖĘĚẺẼ")) 494 call assert_equal("FḞ ", toupper("FḞ ")) 495 call assert_equal("GĜĞĠĢǤǦǴḠ", toupper("GĜĞĠĢǤǦǴḠ")) 496 call assert_equal("HĤĦḢḦḨ", toupper("HĤĦḢḦḨ")) 497 call assert_equal("IÌÍÎÏĨĪĬĮİǏỈ", toupper("IÌÍÎÏĨĪĬĮİǏỈ")) 498 call assert_equal("JĴ", toupper("JĴ")) 499 call assert_equal("KĶǨḰḴ", toupper("KĶǨḰḴ")) 500 call assert_equal("LĹĻĽĿŁḺ", toupper("LĹĻĽĿŁḺ")) 501 call assert_equal("MḾṀ", toupper("MḾṀ")) 502 call assert_equal("NÑŃŅŇṄṈ", toupper("NÑŃŅŇṄṈ")) 503 call assert_equal("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ", toupper("OÒÓÔÕÖØŌŎŐƠǑǪǬỎ")) 504 call assert_equal("PṔṖ", toupper("PṔṖ")) 505 call assert_equal("Q", toupper("Q")) 506 call assert_equal("RŔŖŘṘṞ", toupper("RŔŖŘṘṞ")) 507 call assert_equal("SŚŜŞŠṠ", toupper("SŚŜŞŠṠ")) 508 call assert_equal("TŢŤŦṪṮ", toupper("TŢŤŦṪṮ")) 509 call assert_equal("UÙÚÛÜŨŪŬŮŰŲƯǓỦ", toupper("UÙÚÛÜŨŪŬŮŰŲƯǓỦ")) 510 call assert_equal("VṼ", toupper("VṼ")) 511 call assert_equal("WŴẀẂẄẆ", toupper("WŴẀẂẄẆ")) 512 call assert_equal("XẊẌ", toupper("XẊẌ")) 513 call assert_equal("YÝŶŸẎỲỶỸ", toupper("YÝŶŸẎỲỶỸ")) 514 call assert_equal("ZŹŻŽƵẐẔ", toupper("ZŹŻŽƵẐẔ")) 515 516 call assert_equal("Ⱥ Ⱦ", toupper("ⱥ ⱦ")) 517 518 " This call to toupper with invalid utf8 sequence used to cause access to 519 " invalid memory. 520 call toupper("\xC0\x80\xC0") 521 call toupper("123\xC0\x80\xC0") 522endfunc 523 524" Tests for the mode() function 525let current_modes = '' 526func Save_mode() 527 let g:current_modes = mode(0) . '-' . mode(1) 528 return '' 529endfunc 530 531func Test_mode() 532 new 533 call append(0, ["Blue Ball Black", "Brown Band Bowl", ""]) 534 535 " Only complete from the current buffer. 536 set complete=. 537 538 inoremap <F2> <C-R>=Save_mode()<CR> 539 540 normal! 3G 541 exe "normal i\<F2>\<Esc>" 542 call assert_equal('i-i', g:current_modes) 543 " i_CTRL-P: Multiple matches 544 exe "normal i\<C-G>uBa\<C-P>\<F2>\<Esc>u" 545 call assert_equal('i-ic', g:current_modes) 546 " i_CTRL-P: Single match 547 exe "normal iBro\<C-P>\<F2>\<Esc>u" 548 call assert_equal('i-ic', g:current_modes) 549 " i_CTRL-X 550 exe "normal iBa\<C-X>\<F2>\<Esc>u" 551 call assert_equal('i-ix', g:current_modes) 552 " i_CTRL-X CTRL-P: Multiple matches 553 exe "normal iBa\<C-X>\<C-P>\<F2>\<Esc>u" 554 call assert_equal('i-ic', g:current_modes) 555 " i_CTRL-X CTRL-P: Single match 556 exe "normal iBro\<C-X>\<C-P>\<F2>\<Esc>u" 557 call assert_equal('i-ic', g:current_modes) 558 " i_CTRL-X CTRL-P + CTRL-P: Single match 559 exe "normal iBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" 560 call assert_equal('i-ic', g:current_modes) 561 " i_CTRL-X CTRL-L: Multiple matches 562 exe "normal i\<C-X>\<C-L>\<F2>\<Esc>u" 563 call assert_equal('i-ic', g:current_modes) 564 " i_CTRL-X CTRL-L: Single match 565 exe "normal iBlu\<C-X>\<C-L>\<F2>\<Esc>u" 566 call assert_equal('i-ic', g:current_modes) 567 " i_CTRL-P: No match 568 exe "normal iCom\<C-P>\<F2>\<Esc>u" 569 call assert_equal('i-ic', g:current_modes) 570 " i_CTRL-X CTRL-P: No match 571 exe "normal iCom\<C-X>\<C-P>\<F2>\<Esc>u" 572 call assert_equal('i-ic', g:current_modes) 573 " i_CTRL-X CTRL-L: No match 574 exe "normal iabc\<C-X>\<C-L>\<F2>\<Esc>u" 575 call assert_equal('i-ic', g:current_modes) 576 577 " R_CTRL-P: Multiple matches 578 exe "normal RBa\<C-P>\<F2>\<Esc>u" 579 call assert_equal('R-Rc', g:current_modes) 580 " R_CTRL-P: Single match 581 exe "normal RBro\<C-P>\<F2>\<Esc>u" 582 call assert_equal('R-Rc', g:current_modes) 583 " R_CTRL-X 584 exe "normal RBa\<C-X>\<F2>\<Esc>u" 585 call assert_equal('R-Rx', g:current_modes) 586 " R_CTRL-X CTRL-P: Multiple matches 587 exe "normal RBa\<C-X>\<C-P>\<F2>\<Esc>u" 588 call assert_equal('R-Rc', g:current_modes) 589 " R_CTRL-X CTRL-P: Single match 590 exe "normal RBro\<C-X>\<C-P>\<F2>\<Esc>u" 591 call assert_equal('R-Rc', g:current_modes) 592 " R_CTRL-X CTRL-P + CTRL-P: Single match 593 exe "normal RBro\<C-X>\<C-P>\<C-P>\<F2>\<Esc>u" 594 call assert_equal('R-Rc', g:current_modes) 595 " R_CTRL-X CTRL-L: Multiple matches 596 exe "normal R\<C-X>\<C-L>\<F2>\<Esc>u" 597 call assert_equal('R-Rc', g:current_modes) 598 " R_CTRL-X CTRL-L: Single match 599 exe "normal RBlu\<C-X>\<C-L>\<F2>\<Esc>u" 600 call assert_equal('R-Rc', g:current_modes) 601 " R_CTRL-P: No match 602 exe "normal RCom\<C-P>\<F2>\<Esc>u" 603 call assert_equal('R-Rc', g:current_modes) 604 " R_CTRL-X CTRL-P: No match 605 exe "normal RCom\<C-X>\<C-P>\<F2>\<Esc>u" 606 call assert_equal('R-Rc', g:current_modes) 607 " R_CTRL-X CTRL-L: No match 608 exe "normal Rabc\<C-X>\<C-L>\<F2>\<Esc>u" 609 call assert_equal('R-Rc', g:current_modes) 610 611 call assert_equal('n', mode(0)) 612 call assert_equal('n', mode(1)) 613 614 " i_CTRL-O 615 exe "normal i\<C-O>:call Save_mode()\<Cr>\<Esc>" 616 call assert_equal("n-niI", g:current_modes) 617 618 " R_CTRL-O 619 exe "normal R\<C-O>:call Save_mode()\<Cr>\<Esc>" 620 call assert_equal("n-niR", g:current_modes) 621 622 " gR_CTRL-O 623 exe "normal gR\<C-O>:call Save_mode()\<Cr>\<Esc>" 624 call assert_equal("n-niV", g:current_modes) 625 626 " How to test operator-pending mode? 627 628 call feedkeys("v", 'xt') 629 call assert_equal('v', mode()) 630 call assert_equal('v', mode(1)) 631 call feedkeys("\<Esc>V", 'xt') 632 call assert_equal('V', mode()) 633 call assert_equal('V', mode(1)) 634 call feedkeys("\<Esc>\<C-V>", 'xt') 635 call assert_equal("\<C-V>", mode()) 636 call assert_equal("\<C-V>", mode(1)) 637 call feedkeys("\<Esc>", 'xt') 638 639 call feedkeys("gh", 'xt') 640 call assert_equal('s', mode()) 641 call assert_equal('s', mode(1)) 642 call feedkeys("\<Esc>gH", 'xt') 643 call assert_equal('S', mode()) 644 call assert_equal('S', mode(1)) 645 call feedkeys("\<Esc>g\<C-H>", 'xt') 646 call assert_equal("\<C-S>", mode()) 647 call assert_equal("\<C-S>", mode(1)) 648 call feedkeys("\<Esc>", 'xt') 649 650 call feedkeys(":echo \<C-R>=Save_mode()\<C-U>\<CR>", 'xt') 651 call assert_equal('c-c', g:current_modes) 652 call feedkeys("gQecho \<C-R>=Save_mode()\<CR>\<CR>vi\<CR>", 'xt') 653 call assert_equal('c-cv', g:current_modes) 654 " How to test Ex mode? 655 656 bwipe! 657 iunmap <F2> 658 set complete& 659endfunc 660 661func Test_getbufvar() 662 let bnr = bufnr('%') 663 let b:var_num = '1234' 664 let def_num = '5678' 665 call assert_equal('1234', getbufvar(bnr, 'var_num')) 666 call assert_equal('1234', getbufvar(bnr, 'var_num', def_num)) 667 668 let bd = getbufvar(bnr, '') 669 call assert_equal('1234', bd['var_num']) 670 call assert_true(exists("bd['changedtick']")) 671 call assert_equal(2, len(bd)) 672 673 let bd2 = getbufvar(bnr, '', def_num) 674 call assert_equal(bd, bd2) 675 676 unlet b:var_num 677 call assert_equal(def_num, getbufvar(bnr, 'var_num', def_num)) 678 call assert_equal('', getbufvar(bnr, 'var_num')) 679 680 let bd = getbufvar(bnr, '') 681 call assert_equal(1, len(bd)) 682 let bd = getbufvar(bnr, '',def_num) 683 call assert_equal(1, len(bd)) 684 685 call assert_equal('', getbufvar(9999, '')) 686 call assert_equal(def_num, getbufvar(9999, '', def_num)) 687 unlet def_num 688 689 call assert_equal(0, getbufvar(bnr, '&autoindent')) 690 call assert_equal(0, getbufvar(bnr, '&autoindent', 1)) 691 692 " Open new window with forced option values 693 set fileformats=unix,dos 694 new ++ff=dos ++bin ++enc=iso-8859-2 695 call assert_equal('dos', getbufvar(bufnr('%'), '&fileformat')) 696 call assert_equal(1, getbufvar(bufnr('%'), '&bin')) 697 call assert_equal('iso-8859-2', getbufvar(bufnr('%'), '&fenc')) 698 close 699 700 set fileformats& 701endfunc 702 703func Test_last_buffer_nr() 704 call assert_equal(bufnr('$'), last_buffer_nr()) 705endfunc 706 707func Test_stridx() 708 call assert_equal(-1, stridx('', 'l')) 709 call assert_equal(0, stridx('', '')) 710 call assert_equal(0, stridx('hello', '')) 711 call assert_equal(-1, stridx('hello', 'L')) 712 call assert_equal(2, stridx('hello', 'l', -1)) 713 call assert_equal(2, stridx('hello', 'l', 0)) 714 call assert_equal(2, stridx('hello', 'l', 1)) 715 call assert_equal(3, stridx('hello', 'l', 3)) 716 call assert_equal(-1, stridx('hello', 'l', 4)) 717 call assert_equal(-1, stridx('hello', 'l', 10)) 718 call assert_equal(2, stridx('hello', 'll')) 719 call assert_equal(-1, stridx('hello', 'hello world')) 720endfunc 721 722func Test_strridx() 723 call assert_equal(-1, strridx('', 'l')) 724 call assert_equal(0, strridx('', '')) 725 call assert_equal(5, strridx('hello', '')) 726 call assert_equal(-1, strridx('hello', 'L')) 727 call assert_equal(3, strridx('hello', 'l')) 728 call assert_equal(3, strridx('hello', 'l', 10)) 729 call assert_equal(3, strridx('hello', 'l', 3)) 730 call assert_equal(2, strridx('hello', 'l', 2)) 731 call assert_equal(-1, strridx('hello', 'l', 1)) 732 call assert_equal(-1, strridx('hello', 'l', 0)) 733 call assert_equal(-1, strridx('hello', 'l', -1)) 734 call assert_equal(2, strridx('hello', 'll')) 735 call assert_equal(-1, strridx('hello', 'hello world')) 736endfunc 737 738func Test_match_func() 739 call assert_equal(4, match('testing', 'ing')) 740 call assert_equal(4, match('testing', 'ing', 2)) 741 call assert_equal(-1, match('testing', 'ing', 5)) 742 call assert_equal(-1, match('testing', 'ing', 8)) 743 call assert_equal(1, match(['vim', 'testing', 'execute'], 'ing')) 744 call assert_equal(-1, match(['vim', 'testing', 'execute'], 'img')) 745endfunc 746 747func Test_matchend() 748 call assert_equal(7, matchend('testing', 'ing')) 749 call assert_equal(7, matchend('testing', 'ing', 2)) 750 call assert_equal(-1, matchend('testing', 'ing', 5)) 751 call assert_equal(-1, matchend('testing', 'ing', 8)) 752 call assert_equal(match(['vim', 'testing', 'execute'], 'ing'), matchend(['vim', 'testing', 'execute'], 'ing')) 753 call assert_equal(match(['vim', 'testing', 'execute'], 'img'), matchend(['vim', 'testing', 'execute'], 'img')) 754endfunc 755 756func Test_matchlist() 757 call assert_equal(['acd', 'a', '', 'c', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)')) 758 call assert_equal(['d', '', '', '', 'd', '', '', '', '', ''], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 2)) 759 call assert_equal([], matchlist('acd', '\(a\)\?\(b\)\?\(c\)\?\(.*\)', 4)) 760endfunc 761 762func Test_matchstr() 763 call assert_equal('ing', matchstr('testing', 'ing')) 764 call assert_equal('ing', matchstr('testing', 'ing', 2)) 765 call assert_equal('', matchstr('testing', 'ing', 5)) 766 call assert_equal('', matchstr('testing', 'ing', 8)) 767 call assert_equal('testing', matchstr(['vim', 'testing', 'execute'], 'ing')) 768 call assert_equal('', matchstr(['vim', 'testing', 'execute'], 'img')) 769endfunc 770 771func Test_matchstrpos() 772 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing')) 773 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2)) 774 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5)) 775 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 8)) 776 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing')) 777 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img')) 778endfunc 779 780func Test_nextnonblank_prevnonblank() 781 new 782insert 783This 784 785 786is 787 788a 789Test 790. 791 call assert_equal(0, nextnonblank(-1)) 792 call assert_equal(0, nextnonblank(0)) 793 call assert_equal(1, nextnonblank(1)) 794 call assert_equal(4, nextnonblank(2)) 795 call assert_equal(4, nextnonblank(3)) 796 call assert_equal(4, nextnonblank(4)) 797 call assert_equal(6, nextnonblank(5)) 798 call assert_equal(6, nextnonblank(6)) 799 call assert_equal(7, nextnonblank(7)) 800 call assert_equal(0, nextnonblank(8)) 801 802 call assert_equal(0, prevnonblank(-1)) 803 call assert_equal(0, prevnonblank(0)) 804 call assert_equal(1, prevnonblank(1)) 805 call assert_equal(1, prevnonblank(2)) 806 call assert_equal(1, prevnonblank(3)) 807 call assert_equal(4, prevnonblank(4)) 808 call assert_equal(4, prevnonblank(5)) 809 call assert_equal(6, prevnonblank(6)) 810 call assert_equal(7, prevnonblank(7)) 811 call assert_equal(0, prevnonblank(8)) 812 bw! 813endfunc 814 815func Test_byte2line_line2byte() 816 new 817 set endofline 818 call setline(1, ['a', 'bc', 'd']) 819 820 set fileformat=unix 821 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], 822 \ map(range(-1, 8), 'byte2line(v:val)')) 823 call assert_equal([-1, -1, 1, 3, 6, 8, -1], 824 \ map(range(-1, 5), 'line2byte(v:val)')) 825 826 set fileformat=mac 827 call assert_equal([-1, -1, 1, 1, 2, 2, 2, 3, 3, -1], 828 \ map(range(-1, 8), 'byte2line(v:val)')) 829 call assert_equal([-1, -1, 1, 3, 6, 8, -1], 830 \ map(range(-1, 5), 'line2byte(v:val)')) 831 832 set fileformat=dos 833 call assert_equal([-1, -1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, -1], 834 \ map(range(-1, 11), 'byte2line(v:val)')) 835 call assert_equal([-1, -1, 1, 4, 8, 11, -1], 836 \ map(range(-1, 5), 'line2byte(v:val)')) 837 838 bw! 839 set noendofline nofixendofline 840 normal a- 841 for ff in ["unix", "mac", "dos"] 842 let &fileformat = ff 843 call assert_equal(1, line2byte(1)) 844 call assert_equal(2, line2byte(2)) " line2byte(line("$") + 1) is the buffer size plus one (as per :help line2byte). 845 endfor 846 847 set endofline& fixendofline& fileformat& 848 bw! 849endfunc 850 851func Test_count() 852 let l = ['a', 'a', 'A', 'b'] 853 call assert_equal(2, count(l, 'a')) 854 call assert_equal(1, count(l, 'A')) 855 call assert_equal(1, count(l, 'b')) 856 call assert_equal(0, count(l, 'B')) 857 858 call assert_equal(2, count(l, 'a', 0)) 859 call assert_equal(1, count(l, 'A', 0)) 860 call assert_equal(1, count(l, 'b', 0)) 861 call assert_equal(0, count(l, 'B', 0)) 862 863 call assert_equal(3, count(l, 'a', 1)) 864 call assert_equal(3, count(l, 'A', 1)) 865 call assert_equal(1, count(l, 'b', 1)) 866 call assert_equal(1, count(l, 'B', 1)) 867 call assert_equal(0, count(l, 'c', 1)) 868 869 call assert_equal(1, count(l, 'a', 0, 1)) 870 call assert_equal(2, count(l, 'a', 1, 1)) 871 call assert_fails('call count(l, "a", 0, 10)', 'E684:') 872 873 let d = {1: 'a', 2: 'a', 3: 'A', 4: 'b'} 874 call assert_equal(2, count(d, 'a')) 875 call assert_equal(1, count(d, 'A')) 876 call assert_equal(1, count(d, 'b')) 877 call assert_equal(0, count(d, 'B')) 878 879 call assert_equal(2, count(d, 'a', 0)) 880 call assert_equal(1, count(d, 'A', 0)) 881 call assert_equal(1, count(d, 'b', 0)) 882 call assert_equal(0, count(d, 'B', 0)) 883 884 call assert_equal(3, count(d, 'a', 1)) 885 call assert_equal(3, count(d, 'A', 1)) 886 call assert_equal(1, count(d, 'b', 1)) 887 call assert_equal(1, count(d, 'B', 1)) 888 call assert_equal(0, count(d, 'c', 1)) 889 890 call assert_fails('call count(d, "a", 0, 1)', 'E474:') 891 892 call assert_equal(0, count("foo", "bar")) 893 call assert_equal(1, count("foo", "oo")) 894 call assert_equal(2, count("foo", "o")) 895 call assert_equal(0, count("foo", "O")) 896 call assert_equal(2, count("foo", "O", 1)) 897 call assert_equal(2, count("fooooo", "oo")) 898 call assert_equal(0, count("foo", "")) 899endfunc 900 901func Test_changenr() 902 new Xchangenr 903 call assert_equal(0, changenr()) 904 norm ifoo 905 call assert_equal(1, changenr()) 906 set undolevels=10 907 norm Sbar 908 call assert_equal(2, changenr()) 909 undo 910 call assert_equal(1, changenr()) 911 redo 912 call assert_equal(2, changenr()) 913 bw! 914 set undolevels& 915endfunc 916 917func Test_filewritable() 918 new Xfilewritable 919 write! 920 call assert_equal(1, filewritable('Xfilewritable')) 921 922 call assert_notequal(0, setfperm('Xfilewritable', 'r--r-----')) 923 call assert_equal(0, filewritable('Xfilewritable')) 924 925 call assert_notequal(0, setfperm('Xfilewritable', 'rw-r-----')) 926 call assert_equal(1, filewritable('Xfilewritable')) 927 928 call assert_equal(0, filewritable('doesnotexist')) 929 930 call delete('Xfilewritable') 931 bw! 932endfunc 933 934func Test_Executable() 935 if has('win32') 936 call assert_equal(1, executable('notepad')) 937 call assert_equal(1, executable('notepad.exe')) 938 call assert_equal(0, executable('notepad.exe.exe')) 939 call assert_equal(0, executable('shell32.dll')) 940 call assert_equal(0, executable('win.ini')) 941 elseif has('unix') 942 call assert_equal(1, executable('cat')) 943 call assert_equal(0, executable('nodogshere')) 944 endif 945endfunc 946 947func Test_hostname() 948 let hostname_vim = hostname() 949 if has('unix') 950 let hostname_system = systemlist('uname -n')[0] 951 call assert_equal(hostname_vim, hostname_system) 952 endif 953endfunc 954 955func Test_getpid() 956 " getpid() always returns the same value within a vim instance. 957 call assert_equal(getpid(), getpid()) 958 if has('unix') 959 call assert_equal(systemlist('echo $PPID')[0], string(getpid())) 960 endif 961endfunc 962 963func Test_hlexists() 964 call assert_equal(0, hlexists('does_not_exist')) 965 call assert_equal(0, hlexists('Number')) 966 call assert_equal(0, highlight_exists('does_not_exist')) 967 call assert_equal(0, highlight_exists('Number')) 968 syntax on 969 call assert_equal(0, hlexists('does_not_exist')) 970 call assert_equal(1, hlexists('Number')) 971 call assert_equal(0, highlight_exists('does_not_exist')) 972 call assert_equal(1, highlight_exists('Number')) 973 syntax off 974endfunc 975 976func Test_col() 977 new 978 call setline(1, 'abcdef') 979 norm gg4|mx6|mY2| 980 call assert_equal(2, col('.')) 981 call assert_equal(7, col('$')) 982 call assert_equal(4, col("'x")) 983 call assert_equal(6, col("'Y")) 984 call assert_equal(2, col([1, 2])) 985 call assert_equal(7, col([1, '$'])) 986 987 call assert_equal(0, col('')) 988 call assert_equal(0, col('x')) 989 call assert_equal(0, col([2, '$'])) 990 call assert_equal(0, col([1, 100])) 991 call assert_equal(0, col([1])) 992 bw! 993endfunc 994 995func Test_inputlist() 996 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>1\<cr>", 'tx') 997 call assert_equal(1, c) 998 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>2\<cr>", 'tx') 999 call assert_equal(2, c) 1000 call feedkeys(":let c = inputlist(['Select color:', '1. red', '2. green', '3. blue'])\<cr>3\<cr>", 'tx') 1001 call assert_equal(3, c) 1002 1003 call assert_fails('call inputlist("")', 'E686:') 1004endfunc 1005 1006func Test_balloon_show() 1007 if has('balloon_eval') 1008 " This won't do anything but must not crash either. 1009 call balloon_show('hi!') 1010 endif 1011endfunc 1012 1013func Test_setbufvar_options() 1014 " This tests that aucmd_prepbuf() and aucmd_restbuf() properly restore the 1015 " window layout. 1016 call assert_equal(1, winnr('$')) 1017 split dummy_preview 1018 resize 2 1019 set winfixheight winfixwidth 1020 let prev_id = win_getid() 1021 1022 wincmd j 1023 let wh = winheight('.') 1024 let dummy_buf = bufnr('dummy_buf1', v:true) 1025 call setbufvar(dummy_buf, '&buftype', 'nofile') 1026 execute 'belowright vertical split #' . dummy_buf 1027 call assert_equal(wh, winheight('.')) 1028 let dum1_id = win_getid() 1029 1030 wincmd h 1031 let wh = winheight('.') 1032 let dummy_buf = bufnr('dummy_buf2', v:true) 1033 call setbufvar(dummy_buf, '&buftype', 'nofile') 1034 execute 'belowright vertical split #' . dummy_buf 1035 call assert_equal(wh, winheight('.')) 1036 1037 bwipe! 1038 call win_gotoid(prev_id) 1039 bwipe! 1040 call win_gotoid(dum1_id) 1041 bwipe! 1042endfunc 1043 1044func Test_redo_in_nested_functions() 1045 nnoremap g. :set opfunc=Operator<CR>g@ 1046 function Operator( type, ... ) 1047 let @x = 'XXX' 1048 execute 'normal! g`[' . (a:type ==# 'line' ? 'V' : 'v') . 'g`]' . '"xp' 1049 endfunction 1050 1051 function! Apply() 1052 5,6normal! . 1053 endfunction 1054 1055 new 1056 call setline(1, repeat(['some "quoted" text', 'more "quoted" text'], 3)) 1057 1normal g.i" 1058 call assert_equal('some "XXX" text', getline(1)) 1059 3,4normal . 1060 call assert_equal('some "XXX" text', getline(3)) 1061 call assert_equal('more "XXX" text', getline(4)) 1062 call Apply() 1063 call assert_equal('some "XXX" text', getline(5)) 1064 call assert_equal('more "XXX" text', getline(6)) 1065 bwipe! 1066 1067 nunmap g. 1068 delfunc Operator 1069 delfunc Apply 1070endfunc 1071 1072func Test_shellescape() 1073 let save_shell = &shell 1074 set shell=bash 1075 call assert_equal("'text'", shellescape('text')) 1076 call assert_equal("'te\"xt'", shellescape('te"xt')) 1077 call assert_equal("'te'\\''xt'", shellescape("te'xt")) 1078 1079 call assert_equal("'te%xt'", shellescape("te%xt")) 1080 call assert_equal("'te\\%xt'", shellescape("te%xt", 1)) 1081 call assert_equal("'te#xt'", shellescape("te#xt")) 1082 call assert_equal("'te\\#xt'", shellescape("te#xt", 1)) 1083 call assert_equal("'te!xt'", shellescape("te!xt")) 1084 call assert_equal("'te\\!xt'", shellescape("te!xt", 1)) 1085 1086 call assert_equal("'te\nxt'", shellescape("te\nxt")) 1087 call assert_equal("'te\\\nxt'", shellescape("te\nxt", 1)) 1088 set shell=tcsh 1089 call assert_equal("'te\\!xt'", shellescape("te!xt")) 1090 call assert_equal("'te\\\\!xt'", shellescape("te!xt", 1)) 1091 call assert_equal("'te\\\nxt'", shellescape("te\nxt")) 1092 call assert_equal("'te\\\\\nxt'", shellescape("te\nxt", 1)) 1093 1094 let &shell = save_shell 1095endfunc 1096 1097func Test_trim() 1098 call assert_equal("Testing", trim(" \t\r\r\x0BTesting \t\n\r\n\t\x0B\x0B")) 1099 call assert_equal("Testing", trim(" \t \r\r\n\n\x0BTesting \t\n\r\n\t\x0B\x0B")) 1100 call assert_equal("RESERVE", trim("xyz \twwRESERVEzyww \t\t", " wxyz\t")) 1101 call assert_equal("wRE \tSERVEzyww", trim("wRE \tSERVEzyww")) 1102 call assert_equal("abcd\t xxxx tail", trim(" \tabcd\t xxxx tail")) 1103 call assert_equal("\tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", " ")) 1104 call assert_equal(" \tabcd\t xxxx tail", trim(" \tabcd\t xxxx tail", "abx")) 1105 call assert_equal("RESERVE", trim("你RESERVE好", "你好")) 1106 call assert_equal("您R E SER V E早", trim("你好您R E SER V E早好你你", "你好")) 1107 call assert_equal("你好您R E SER V E早好你你", trim(" \n\r\r 你好您R E SER V E早好你你 \t \x0B", )) 1108 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" 你好您R E SER V E早好你你 \t \x0B", " 你好")) 1109 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你好tes")) 1110 call assert_equal("您R E SER V E早好你你 \t \x0B", trim(" tteesstttt你好您R E SER V E早好你你 \t \x0B ttestt", " 你你你好好好tttsses")) 1111 call assert_equal("留下", trim("这些些不要这些留下这些", "这些不要")) 1112 call assert_equal("", trim("", "")) 1113 call assert_equal("a", trim("a", "")) 1114 call assert_equal("", trim("", "a")) 1115 1116 let chars = join(map(range(1, 0x20) + [0xa0], {n -> nr2char(n)}), '') 1117 call assert_equal("x", trim(chars . "x" . chars)) 1118endfunc 1119 1120" Test for reg_recording() and reg_executing() 1121func Test_reg_executing_and_recording() 1122 let s:reg_stat = '' 1123 func s:save_reg_stat() 1124 let s:reg_stat = reg_recording() . ':' . reg_executing() 1125 return '' 1126 endfunc 1127 1128 new 1129 call s:save_reg_stat() 1130 call assert_equal(':', s:reg_stat) 1131 call feedkeys("qa\"=s:save_reg_stat()\<CR>pq", 'xt') 1132 call assert_equal('a:', s:reg_stat) 1133 call feedkeys("@a", 'xt') 1134 call assert_equal(':a', s:reg_stat) 1135 call feedkeys("qb@aq", 'xt') 1136 call assert_equal('b:a', s:reg_stat) 1137 call feedkeys("q\"\"=s:save_reg_stat()\<CR>pq", 'xt') 1138 call assert_equal('":', s:reg_stat) 1139 1140 bwipe! 1141 delfunc s:save_reg_stat 1142 unlet s:reg_stat 1143endfunc 1144 1145func Test_libcall_libcallnr() 1146 if !has('libcall') 1147 return 1148 endif 1149 1150 if has('win32') 1151 let libc = 'msvcrt.dll' 1152 elseif has('mac') 1153 let libc = 'libSystem.B.dylib' 1154 elseif executable('ldd') 1155 let libc = matchstr(split(system('ldd ' . GetVimProg())), '/libc\.so\>') 1156 endif 1157 if get(l:, 'libc', '') ==# '' 1158 " On Unix, libc.so can be in various places. 1159 if has('linux') 1160 " There is not documented but regarding the 1st argument of glibc's 1161 " dlopen an empty string and nullptr are equivalent, so using an empty 1162 " string for the 1st argument of libcall allows to call functions. 1163 let libc = '' 1164 elseif has('sun') 1165 " Set the path to libc.so according to the architecture. 1166 let test_bits = system('file ' . GetVimProg()) 1167 let test_arch = system('uname -p') 1168 if test_bits =~ '64-bit' && test_arch =~ 'sparc' 1169 let libc = '/usr/lib/sparcv9/libc.so' 1170 elseif test_bits =~ '64-bit' && test_arch =~ 'i386' 1171 let libc = '/usr/lib/amd64/libc.so' 1172 else 1173 let libc = '/usr/lib/libc.so' 1174 endif 1175 else 1176 " Unfortunately skip this test until a good way is found. 1177 return 1178 endif 1179 endif 1180 1181 if has('win32') 1182 call assert_equal($USERPROFILE, libcall(libc, 'getenv', 'USERPROFILE')) 1183 else 1184 call assert_equal($HOME, libcall(libc, 'getenv', 'HOME')) 1185 endif 1186 1187 " If function returns NULL, libcall() should return an empty string. 1188 call assert_equal('', libcall(libc, 'getenv', 'X_ENV_DOES_NOT_EXIT')) 1189 1190 " Test libcallnr() with string and integer argument. 1191 call assert_equal(4, libcallnr(libc, 'strlen', 'abcd')) 1192 call assert_equal(char2nr('A'), libcallnr(libc, 'toupper', char2nr('a'))) 1193 1194 call assert_fails("call libcall(libc, 'Xdoesnotexist_', '')", 'E364:') 1195 call assert_fails("call libcallnr(libc, 'Xdoesnotexist_', '')", 'E364:') 1196 1197 call assert_fails("call libcall('Xdoesnotexist_', 'getenv', 'HOME')", 'E364:') 1198 call assert_fails("call libcallnr('Xdoesnotexist_', 'strlen', 'abcd')", 'E364:') 1199endfunc 1200 1201sandbox function Fsandbox() 1202 normal ix 1203endfunc 1204 1205func Test_func_sandbox() 1206 sandbox let F = {-> 'hello'} 1207 call assert_equal('hello', F()) 1208 1209 sandbox let F = {-> execute("normal ix\<Esc>")} 1210 call assert_fails('call F()', 'E48:') 1211 unlet F 1212 1213 call assert_fails('call Fsandbox()', 'E48:') 1214 delfunc Fsandbox 1215endfunc 1216 1217func EditAnotherFile() 1218 let word = expand('<cword>') 1219 edit Xfuncrange2 1220endfunc 1221 1222func Test_func_range_with_edit() 1223 " Define a function that edits another buffer, then call it with a range that 1224 " is invalid in that buffer. 1225 call writefile(['just one line'], 'Xfuncrange2') 1226 new 1227 call setline(1, range(10)) 1228 write Xfuncrange1 1229 call assert_fails('5,8call EditAnotherFile()', 'E16:') 1230 1231 call delete('Xfuncrange1') 1232 call delete('Xfuncrange2') 1233 bwipe! 1234endfunc 1235 1236func Test_func_exists_on_reload() 1237 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists') 1238 call assert_equal(0, exists('*ExistingFunction')) 1239 source Xfuncexists 1240 call assert_equal(1, exists('*ExistingFunction')) 1241 " Redefining a function when reloading a script is OK. 1242 source Xfuncexists 1243 call assert_equal(1, exists('*ExistingFunction')) 1244 1245 " But redefining in another script is not OK. 1246 call writefile(['func ExistingFunction()', 'echo "yes"', 'endfunc'], 'Xfuncexists2') 1247 call assert_fails('source Xfuncexists2', 'E122:') 1248 1249 delfunc ExistingFunction 1250 call assert_equal(0, exists('*ExistingFunction')) 1251 call writefile([ 1252 \ 'func ExistingFunction()', 'echo "yes"', 'endfunc', 1253 \ 'func ExistingFunction()', 'echo "no"', 'endfunc', 1254 \ ], 'Xfuncexists') 1255 call assert_fails('source Xfuncexists', 'E122:') 1256 call assert_equal(1, exists('*ExistingFunction')) 1257 1258 call delete('Xfuncexists2') 1259 call delete('Xfuncexists') 1260 delfunc ExistingFunction 1261endfunc 1262 1263" Test confirm({msg} [, {choices} [, {default} [, {type}]]]) 1264func Test_confirm() 1265 if !has('unix') || has('gui_running') 1266 return 1267 endif 1268 1269 call feedkeys('o', 'L') 1270 let a = confirm('Press O to proceed') 1271 call assert_equal(1, a) 1272 1273 call feedkeys('y', 'L') 1274 let a = confirm('Are you sure?', "&Yes\n&No") 1275 call assert_equal(1, a) 1276 1277 call feedkeys('n', 'L') 1278 let a = confirm('Are you sure?', "&Yes\n&No") 1279 call assert_equal(2, a) 1280 1281 " confirm() should return 0 when pressing CTRL-C. 1282 call feedkeys("\<C-c>", 'L') 1283 let a = confirm('Are you sure?', "&Yes\n&No") 1284 call assert_equal(0, a) 1285 1286 " <Esc> requires another character to avoid it being seen as the start of an 1287 " escape sequence. Zero should be harmless. 1288 call feedkeys("\<Esc>0", 'L') 1289 let a = confirm('Are you sure?', "&Yes\n&No") 1290 call assert_equal(0, a) 1291 1292 " Default choice is returned when pressing <CR>. 1293 call feedkeys("\<CR>", 'L') 1294 let a = confirm('Are you sure?', "&Yes\n&No") 1295 call assert_equal(1, a) 1296 1297 call feedkeys("\<CR>", 'L') 1298 let a = confirm('Are you sure?', "&Yes\n&No", 2) 1299 call assert_equal(2, a) 1300 1301 call feedkeys("\<CR>", 'L') 1302 let a = confirm('Are you sure?', "&Yes\n&No", 0) 1303 call assert_equal(0, a) 1304 1305 " Test with the {type} 4th argument 1306 for type in ['Error', 'Question', 'Info', 'Warning', 'Generic'] 1307 call feedkeys('y', 'L') 1308 let a = confirm('Are you sure?', "&Yes\n&No\n", 1, type) 1309 call assert_equal(1, a) 1310 endfor 1311 1312 call assert_fails('call confirm([])', 'E730:') 1313 call assert_fails('call confirm("Are you sure?", [])', 'E730:') 1314 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", [])', 'E745:') 1315 call assert_fails('call confirm("Are you sure?", "&Yes\n&No\n", 0, [])', 'E730:') 1316endfunc 1317 1318func Test_platform_name() 1319 " The system matches at most only one name. 1320 let names = ['amiga', 'beos', 'bsd', 'hpux', 'linux', 'mac', 'qnx', 'sun', 'vms', 'win32', 'win32unix'] 1321 call assert_inrange(0, 1, len(filter(copy(names), 'has(v:val)'))) 1322 1323 " Is Unix? 1324 call assert_equal(has('beos'), has('beos') && has('unix')) 1325 call assert_equal(has('bsd'), has('bsd') && has('unix')) 1326 call assert_equal(has('hpux'), has('hpux') && has('unix')) 1327 call assert_equal(has('linux'), has('linux') && has('unix')) 1328 call assert_equal(has('mac'), has('mac') && has('unix')) 1329 call assert_equal(has('qnx'), has('qnx') && has('unix')) 1330 call assert_equal(has('sun'), has('sun') && has('unix')) 1331 call assert_equal(has('win32'), has('win32') && !has('unix')) 1332 call assert_equal(has('win32unix'), has('win32unix') && has('unix')) 1333 1334 if has('unix') && executable('uname') 1335 let uname = system('uname') 1336 call assert_equal(uname =~? 'BeOS', has('beos')) 1337 " GNU userland on BSD kernels (e.g., GNU/kFreeBSD) don't have BSD defined 1338 call assert_equal(uname =~? '\%(GNU/k\w\+\)\@<!BSD\|DragonFly', has('bsd')) 1339 call assert_equal(uname =~? 'HP-UX', has('hpux')) 1340 call assert_equal(uname =~? 'Linux', has('linux')) 1341 call assert_equal(uname =~? 'Darwin', has('mac')) 1342 call assert_equal(uname =~? 'QNX', has('qnx')) 1343 call assert_equal(uname =~? 'SunOS', has('sun')) 1344 call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix')) 1345 endif 1346endfunc 1347