Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 1 | " Test various aspects of the Vim language. |
Bram Moolenaar | 4119cf8 | 2016-01-17 14:59:01 +0100 | [diff] [blame] | 2 | " Most of this was formerly in test49. |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 3 | |
| 4 | "------------------------------------------------------------------------------- |
| 5 | " Test environment {{{1 |
| 6 | "------------------------------------------------------------------------------- |
| 7 | |
| 8 | com! XpathINIT let g:Xpath = '' |
| 9 | com! -nargs=1 -bar Xpath let g:Xpath = g:Xpath . <args> |
| 10 | |
| 11 | " Append a message to the "messages" file |
| 12 | func! Xout(text) |
| 13 | split messages |
| 14 | $put =a:text |
| 15 | wq |
| 16 | endfunc |
| 17 | |
| 18 | com! -nargs=1 Xout call Xout(<args>) |
| 19 | |
| 20 | " MakeScript() - Make a script file from a function. {{{2 |
| 21 | " |
| 22 | " Create a script that consists of the body of the function a:funcname. |
| 23 | " Replace any ":return" by a ":finish", any argument variable by a global |
| 24 | " variable, and and every ":call" by a ":source" for the next following argument |
| 25 | " in the variable argument list. This function is useful if similar tests are |
| 26 | " to be made for a ":return" from a function call or a ":finish" in a script |
| 27 | " file. |
| 28 | function! MakeScript(funcname, ...) |
| 29 | let script = tempname() |
| 30 | execute "redir! >" . script |
| 31 | execute "function" a:funcname |
| 32 | redir END |
| 33 | execute "edit" script |
| 34 | " Delete the "function" and the "endfunction" lines. Do not include the |
| 35 | " word "function" in the pattern since it might be translated if LANG is |
| 36 | " set. When MakeScript() is being debugged, this deletes also the debugging |
| 37 | " output of its line 3 and 4. |
| 38 | exec '1,/.*' . a:funcname . '(.*)/d' |
| 39 | /^\d*\s*endfunction\>/,$d |
| 40 | %s/^\d*//e |
| 41 | %s/return/finish/e |
| 42 | %s/\<a:\(\h\w*\)/g:\1/ge |
| 43 | normal gg0 |
| 44 | let cnt = 0 |
| 45 | while search('\<call\s*\%(\u\|s:\)\w*\s*(.*)', 'W') > 0 |
| 46 | let cnt = cnt + 1 |
| 47 | s/\<call\s*\%(\u\|s:\)\w*\s*(.*)/\='source ' . a:{cnt}/ |
| 48 | endwhile |
| 49 | g/^\s*$/d |
| 50 | write |
| 51 | bwipeout |
| 52 | return script |
| 53 | endfunction |
| 54 | |
| 55 | " ExecAsScript - Source a temporary script made from a function. {{{2 |
| 56 | " |
| 57 | " Make a temporary script file from the function a:funcname, ":source" it, and |
Bram Moolenaar | f4f79b8 | 2016-01-25 20:38:30 +0100 | [diff] [blame] | 58 | " delete it afterwards. However, if an exception is thrown the file may remain, |
| 59 | " the caller should call DeleteTheScript() afterwards. |
| 60 | let s:script_name = '' |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 61 | function! ExecAsScript(funcname) |
| 62 | " Make a script from the function passed as argument. |
Bram Moolenaar | f4f79b8 | 2016-01-25 20:38:30 +0100 | [diff] [blame] | 63 | let s:script_name = MakeScript(a:funcname) |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 64 | |
| 65 | " Source and delete the script. |
Bram Moolenaar | f4f79b8 | 2016-01-25 20:38:30 +0100 | [diff] [blame] | 66 | exec "source" s:script_name |
| 67 | call delete(s:script_name) |
| 68 | let s:script_name = '' |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 69 | endfunction |
| 70 | |
Bram Moolenaar | f4f79b8 | 2016-01-25 20:38:30 +0100 | [diff] [blame] | 71 | function! DeleteTheScript() |
| 72 | if s:script_name |
| 73 | call delete(s:script_name) |
| 74 | let s:script_name = '' |
| 75 | endif |
| 76 | endfunc |
| 77 | |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 78 | com! -nargs=1 -bar ExecAsScript call ExecAsScript(<f-args>) |
| 79 | |
| 80 | |
| 81 | "------------------------------------------------------------------------------- |
| 82 | " Test 1: :endwhile in function {{{1 |
| 83 | " |
| 84 | " Detect if a broken loop is (incorrectly) reactivated by the |
| 85 | " :endwhile. Use a :return to prevent an endless loop, and make |
| 86 | " this test first to get a meaningful result on an error before other |
| 87 | " tests will hang. |
| 88 | "------------------------------------------------------------------------------- |
| 89 | |
| 90 | function! T1_F() |
| 91 | Xpath 'a' |
| 92 | let first = 1 |
| 93 | while 1 |
| 94 | Xpath 'b' |
| 95 | if first |
| 96 | Xpath 'c' |
| 97 | let first = 0 |
| 98 | break |
| 99 | else |
| 100 | Xpath 'd' |
| 101 | return |
| 102 | endif |
| 103 | endwhile |
| 104 | endfunction |
| 105 | |
| 106 | function! T1_G() |
| 107 | Xpath 'h' |
| 108 | let first = 1 |
| 109 | while 1 |
| 110 | Xpath 'i' |
| 111 | if first |
| 112 | Xpath 'j' |
| 113 | let first = 0 |
| 114 | break |
| 115 | else |
| 116 | Xpath 'k' |
| 117 | return |
| 118 | endif |
| 119 | if 1 " unmatched :if |
| 120 | endwhile |
| 121 | endfunction |
| 122 | |
| 123 | func Test_endwhile_function() |
| 124 | XpathINIT |
| 125 | call T1_F() |
| 126 | Xpath 'F' |
| 127 | |
| 128 | try |
| 129 | call T1_G() |
| 130 | catch |
| 131 | " Catch missing :endif |
| 132 | call assert_true(v:exception =~ 'E171') |
| 133 | Xpath 'x' |
| 134 | endtry |
| 135 | Xpath 'G' |
| 136 | |
| 137 | call assert_equal('abcFhijxG', g:Xpath) |
| 138 | endfunc |
| 139 | |
| 140 | "------------------------------------------------------------------------------- |
| 141 | " Test 2: :endwhile in script {{{1 |
| 142 | " |
| 143 | " Detect if a broken loop is (incorrectly) reactivated by the |
| 144 | " :endwhile. Use a :finish to prevent an endless loop, and place |
| 145 | " this test before others that might hang to get a meaningful result |
| 146 | " on an error. |
| 147 | " |
| 148 | " This test executes the bodies of the functions T1_F and T1_G from |
| 149 | " the previous test as script files (:return replaced by :finish). |
| 150 | "------------------------------------------------------------------------------- |
| 151 | |
| 152 | func Test_endwhile_script() |
| 153 | XpathINIT |
| 154 | ExecAsScript T1_F |
| 155 | Xpath 'F' |
Bram Moolenaar | f4f79b8 | 2016-01-25 20:38:30 +0100 | [diff] [blame] | 156 | call DeleteTheScript() |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 157 | |
| 158 | try |
| 159 | ExecAsScript T1_G |
| 160 | catch |
| 161 | " Catch missing :endif |
| 162 | call assert_true(v:exception =~ 'E171') |
| 163 | Xpath 'x' |
| 164 | endtry |
| 165 | Xpath 'G' |
Bram Moolenaar | f4f79b8 | 2016-01-25 20:38:30 +0100 | [diff] [blame] | 166 | call DeleteTheScript() |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 167 | |
| 168 | call assert_equal('abcFhijxG', g:Xpath) |
| 169 | endfunc |
| 170 | |
| 171 | "------------------------------------------------------------------------------- |
| 172 | " Test 3: :if, :elseif, :while, :continue, :break {{{1 |
| 173 | "------------------------------------------------------------------------------- |
| 174 | |
| 175 | function Test_if_while() |
| 176 | XpathINIT |
| 177 | if 1 |
| 178 | Xpath 'a' |
| 179 | let loops = 3 |
| 180 | while loops > -1 " main loop: loops == 3, 2, 1 (which breaks) |
| 181 | if loops <= 0 |
| 182 | let break_err = 1 |
| 183 | let loops = -1 |
| 184 | else |
| 185 | Xpath 'b' . loops |
| 186 | endif |
| 187 | if (loops == 2) |
| 188 | while loops == 2 " dummy loop |
| 189 | Xpath 'c' . loops |
| 190 | let loops = loops - 1 |
| 191 | continue " stop dummy loop |
| 192 | Xpath 'd' . loops |
| 193 | endwhile |
| 194 | continue " continue main loop |
| 195 | Xpath 'e' . loops |
| 196 | elseif (loops == 1) |
| 197 | let p = 1 |
| 198 | while p " dummy loop |
| 199 | Xpath 'f' . loops |
| 200 | let p = 0 |
| 201 | break " break dummy loop |
| 202 | Xpath 'g' . loops |
| 203 | endwhile |
| 204 | Xpath 'h' . loops |
| 205 | unlet p |
| 206 | break " break main loop |
| 207 | Xpath 'i' . loops |
| 208 | endif |
| 209 | if (loops > 0) |
| 210 | Xpath 'j' . loops |
| 211 | endif |
| 212 | while loops == 3 " dummy loop |
| 213 | let loops = loops - 1 |
| 214 | endwhile " end dummy loop |
| 215 | endwhile " end main loop |
| 216 | Xpath 'k' |
| 217 | else |
| 218 | Xpath 'l' |
| 219 | endif |
| 220 | Xpath 'm' |
| 221 | if exists("break_err") |
| 222 | Xpath 'm' |
| 223 | unlet break_err |
| 224 | endif |
| 225 | |
| 226 | unlet loops |
| 227 | |
| 228 | call assert_equal('ab3j3b2c2b1f1h1km', g:Xpath) |
| 229 | endfunc |
| 230 | |
| 231 | "------------------------------------------------------------------------------- |
| 232 | " Test 4: :return {{{1 |
| 233 | "------------------------------------------------------------------------------- |
| 234 | |
| 235 | function! T4_F() |
| 236 | if 1 |
| 237 | Xpath 'a' |
| 238 | let loops = 3 |
| 239 | while loops > 0 " 3: 2: 1: |
| 240 | Xpath 'b' . loops |
| 241 | if (loops == 2) |
| 242 | Xpath 'c' . loops |
| 243 | return |
| 244 | Xpath 'd' . loops |
| 245 | endif |
| 246 | Xpath 'e' . loops |
| 247 | let loops = loops - 1 |
| 248 | endwhile |
| 249 | Xpath 'f' |
| 250 | else |
| 251 | Xpath 'g' |
| 252 | endif |
| 253 | endfunction |
| 254 | |
| 255 | function Test_return() |
| 256 | XpathINIT |
| 257 | call T4_F() |
| 258 | Xpath '4' |
| 259 | |
| 260 | call assert_equal('ab3e3b2c24', g:Xpath) |
| 261 | endfunction |
| 262 | |
| 263 | |
| 264 | "------------------------------------------------------------------------------- |
| 265 | " Test 5: :finish {{{1 |
| 266 | " |
| 267 | " This test executes the body of the function T4_F from the previous |
| 268 | " test as a script file (:return replaced by :finish). |
| 269 | "------------------------------------------------------------------------------- |
| 270 | |
| 271 | function Test_finish() |
| 272 | XpathINIT |
| 273 | ExecAsScript T4_F |
| 274 | Xpath '5' |
Bram Moolenaar | f4f79b8 | 2016-01-25 20:38:30 +0100 | [diff] [blame] | 275 | call DeleteTheScript() |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 276 | |
| 277 | call assert_equal('ab3e3b2c25', g:Xpath) |
| 278 | endfunction |
| 279 | |
| 280 | |
| 281 | |
| 282 | "------------------------------------------------------------------------------- |
| 283 | " Test 6: Defining functions in :while loops {{{1 |
| 284 | " |
| 285 | " Functions can be defined inside other functions. An inner function |
| 286 | " gets defined when the outer function is executed. Functions may |
| 287 | " also be defined inside while loops. Expressions in braces for |
| 288 | " defining the function name are allowed. |
| 289 | " |
| 290 | " The functions are defined when sourcing the script, only the |
| 291 | " resulting path is checked in the test function. |
| 292 | "------------------------------------------------------------------------------- |
| 293 | |
| 294 | XpathINIT |
| 295 | |
| 296 | " The command CALL collects the argument of all its invocations in "calls" |
| 297 | " when used from a function (that is, when the global variable "calls" needs |
| 298 | " the "g:" prefix). This is to check that the function code is skipped when |
| 299 | " the function is defined. For inner functions, do so only if the outer |
| 300 | " function is not being executed. |
| 301 | " |
| 302 | let calls = "" |
| 303 | com! -nargs=1 CALL |
| 304 | \ if !exists("calls") && !exists("outer") | |
| 305 | \ let g:calls = g:calls . <args> | |
| 306 | \ endif |
| 307 | |
| 308 | let i = 0 |
| 309 | while i < 3 |
| 310 | let i = i + 1 |
| 311 | if i == 1 |
| 312 | Xpath 'a' |
| 313 | function! F1(arg) |
| 314 | CALL a:arg |
| 315 | let outer = 1 |
| 316 | |
| 317 | let j = 0 |
| 318 | while j < 1 |
| 319 | Xpath 'b' |
| 320 | let j = j + 1 |
| 321 | function! G1(arg) |
| 322 | CALL a:arg |
| 323 | endfunction |
| 324 | Xpath 'c' |
| 325 | endwhile |
| 326 | endfunction |
| 327 | Xpath 'd' |
| 328 | |
| 329 | continue |
| 330 | endif |
| 331 | |
| 332 | Xpath 'e' . i |
| 333 | function! F{i}(i, arg) |
| 334 | CALL a:arg |
| 335 | let outer = 1 |
| 336 | |
| 337 | if a:i == 3 |
| 338 | Xpath 'f' |
| 339 | endif |
| 340 | let k = 0 |
| 341 | while k < 3 |
| 342 | Xpath 'g' . k |
| 343 | let k = k + 1 |
| 344 | function! G{a:i}{k}(arg) |
| 345 | CALL a:arg |
| 346 | endfunction |
| 347 | Xpath 'h' . k |
| 348 | endwhile |
| 349 | endfunction |
| 350 | Xpath 'i' |
| 351 | |
| 352 | endwhile |
| 353 | |
| 354 | if exists("*G1") |
| 355 | Xpath 'j' |
| 356 | endif |
| 357 | if exists("*F1") |
| 358 | call F1("F1") |
| 359 | if exists("*G1") |
| 360 | call G1("G1") |
| 361 | endif |
| 362 | endif |
| 363 | |
| 364 | if exists("G21") || exists("G22") || exists("G23") |
| 365 | Xpath 'k' |
| 366 | endif |
| 367 | if exists("*F2") |
| 368 | call F2(2, "F2") |
| 369 | if exists("*G21") |
| 370 | call G21("G21") |
| 371 | endif |
| 372 | if exists("*G22") |
| 373 | call G22("G22") |
| 374 | endif |
| 375 | if exists("*G23") |
| 376 | call G23("G23") |
| 377 | endif |
| 378 | endif |
| 379 | |
| 380 | if exists("G31") || exists("G32") || exists("G33") |
| 381 | Xpath 'l' |
| 382 | endif |
| 383 | if exists("*F3") |
| 384 | call F3(3, "F3") |
| 385 | if exists("*G31") |
| 386 | call G31("G31") |
| 387 | endif |
| 388 | if exists("*G32") |
| 389 | call G32("G32") |
| 390 | endif |
| 391 | if exists("*G33") |
| 392 | call G33("G33") |
| 393 | endif |
| 394 | endif |
| 395 | |
| 396 | Xpath 'm' |
| 397 | |
| 398 | let g:test6_result = g:Xpath |
| 399 | let g:test6_calls = calls |
| 400 | |
| 401 | unlet calls |
| 402 | delfunction F1 |
| 403 | delfunction G1 |
| 404 | delfunction F2 |
| 405 | delfunction G21 |
| 406 | delfunction G22 |
| 407 | delfunction G23 |
| 408 | delfunction G31 |
| 409 | delfunction G32 |
| 410 | delfunction G33 |
| 411 | |
| 412 | function Test_defining_functions() |
| 413 | call assert_equal('ade2ie3ibcg0h1g1h2g2h3fg0h1g1h2g2h3m', g:test6_result) |
| 414 | call assert_equal('F1G1F2G21G22G23F3G31G32G33', g:test6_calls) |
| 415 | endfunc |
| 416 | |
| 417 | "------------------------------------------------------------------------------- |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 418 | " Test 7: Continuing on errors outside functions {{{1 |
| 419 | " |
| 420 | " On an error outside a function, the script processing continues |
| 421 | " at the line following the outermost :endif or :endwhile. When not |
| 422 | " inside an :if or :while, the script processing continues at the next |
| 423 | " line. |
| 424 | "------------------------------------------------------------------------------- |
| 425 | |
| 426 | XpathINIT |
| 427 | |
| 428 | if 1 |
| 429 | Xpath 'a' |
| 430 | while 1 |
| 431 | Xpath 'b' |
| 432 | asdf |
| 433 | Xpath 'c' |
| 434 | break |
| 435 | endwhile | Xpath 'd' |
| 436 | Xpath 'e' |
| 437 | endif | Xpath 'f' |
| 438 | Xpath 'g' |
| 439 | |
| 440 | while 1 |
| 441 | Xpath 'h' |
| 442 | if 1 |
| 443 | Xpath 'i' |
| 444 | asdf |
| 445 | Xpath 'j' |
| 446 | endif | Xpath 'k' |
| 447 | Xpath 'l' |
| 448 | break |
| 449 | endwhile | Xpath 'm' |
| 450 | Xpath 'n' |
| 451 | |
| 452 | asdf |
| 453 | Xpath 'o' |
| 454 | |
| 455 | asdf | Xpath 'p' |
| 456 | Xpath 'q' |
| 457 | |
| 458 | let g:test7_result = g:Xpath |
| 459 | |
| 460 | func Test_error_in_script() |
| 461 | call assert_equal('abghinoq', g:test7_result) |
| 462 | endfunc |
| 463 | |
| 464 | "------------------------------------------------------------------------------- |
| 465 | " Test 8: Aborting and continuing on errors inside functions {{{1 |
| 466 | " |
| 467 | " On an error inside a function without the "abort" attribute, the |
| 468 | " script processing continues at the next line (unless the error was |
| 469 | " in a :return command). On an error inside a function with the |
| 470 | " "abort" attribute, the function is aborted and the script processing |
| 471 | " continues after the function call; the value -1 is returned then. |
| 472 | "------------------------------------------------------------------------------- |
| 473 | |
| 474 | XpathINIT |
| 475 | |
| 476 | function! T8_F() |
| 477 | if 1 |
| 478 | Xpath 'a' |
| 479 | while 1 |
| 480 | Xpath 'b' |
| 481 | asdf |
| 482 | Xpath 'c' |
| 483 | asdf | Xpath 'd' |
| 484 | Xpath 'e' |
| 485 | break |
| 486 | endwhile |
| 487 | Xpath 'f' |
| 488 | endif | Xpath 'g' |
| 489 | Xpath 'h' |
| 490 | |
| 491 | while 1 |
| 492 | Xpath 'i' |
| 493 | if 1 |
| 494 | Xpath 'j' |
| 495 | asdf |
| 496 | Xpath 'k' |
| 497 | asdf | Xpath 'l' |
| 498 | Xpath 'm' |
| 499 | endif |
| 500 | Xpath 'n' |
| 501 | break |
| 502 | endwhile | Xpath 'o' |
| 503 | Xpath 'p' |
| 504 | |
| 505 | return novar " returns (default return value 0) |
| 506 | Xpath 'q' |
| 507 | return 1 " not reached |
| 508 | endfunction |
| 509 | |
| 510 | function! T8_G() abort |
| 511 | if 1 |
| 512 | Xpath 'r' |
| 513 | while 1 |
| 514 | Xpath 's' |
| 515 | asdf " returns -1 |
| 516 | Xpath 't' |
| 517 | break |
| 518 | endwhile |
| 519 | Xpath 'v' |
| 520 | endif | Xpath 'w' |
| 521 | Xpath 'x' |
| 522 | |
| 523 | return -4 " not reached |
| 524 | endfunction |
| 525 | |
| 526 | function! T8_H() abort |
| 527 | while 1 |
| 528 | Xpath 'A' |
| 529 | if 1 |
| 530 | Xpath 'B' |
| 531 | asdf " returns -1 |
| 532 | Xpath 'C' |
| 533 | endif |
| 534 | Xpath 'D' |
| 535 | break |
| 536 | endwhile | Xpath 'E' |
| 537 | Xpath 'F' |
| 538 | |
| 539 | return -4 " not reached |
| 540 | endfunction |
| 541 | |
| 542 | " Aborted functions (T8_G and T8_H) return -1. |
| 543 | let g:test8_sum = (T8_F() + 1) - 4 * T8_G() - 8 * T8_H() |
| 544 | Xpath 'X' |
| 545 | let g:test8_result = g:Xpath |
| 546 | |
| 547 | func Test_error_in_function() |
| 548 | call assert_equal(13, g:test8_sum) |
| 549 | call assert_equal('abcefghijkmnoprsABX', g:test8_result) |
| 550 | |
| 551 | delfunction T8_F |
| 552 | delfunction T8_G |
| 553 | delfunction T8_H |
| 554 | endfunc |
| 555 | |
| 556 | |
| 557 | "------------------------------------------------------------------------------- |
| 558 | " Test 9: Continuing after aborted functions {{{1 |
| 559 | " |
| 560 | " When a function with the "abort" attribute is aborted due to an |
| 561 | " error, the next function back in the call hierarchy without an |
| 562 | " "abort" attribute continues; the value -1 is returned then. |
| 563 | "------------------------------------------------------------------------------- |
| 564 | |
| 565 | XpathINIT |
| 566 | |
| 567 | function! F() abort |
| 568 | Xpath 'a' |
| 569 | let result = G() " not aborted |
| 570 | Xpath 'b' |
| 571 | if result != 2 |
| 572 | Xpath 'c' |
| 573 | endif |
| 574 | return 1 |
| 575 | endfunction |
| 576 | |
| 577 | function! G() " no abort attribute |
| 578 | Xpath 'd' |
| 579 | if H() != -1 " aborted |
| 580 | Xpath 'e' |
| 581 | endif |
| 582 | Xpath 'f' |
| 583 | return 2 |
| 584 | endfunction |
| 585 | |
| 586 | function! H() abort |
| 587 | Xpath 'g' |
| 588 | call I() " aborted |
| 589 | Xpath 'h' |
| 590 | return 4 |
| 591 | endfunction |
| 592 | |
| 593 | function! I() abort |
| 594 | Xpath 'i' |
| 595 | asdf " error |
| 596 | Xpath 'j' |
| 597 | return 8 |
| 598 | endfunction |
| 599 | |
| 600 | if F() != 1 |
| 601 | Xpath 'k' |
| 602 | endif |
| 603 | |
| 604 | let g:test9_result = g:Xpath |
| 605 | |
| 606 | delfunction F |
| 607 | delfunction G |
| 608 | delfunction H |
| 609 | delfunction I |
| 610 | |
| 611 | func Test_func_abort() |
| 612 | call assert_equal('adgifb', g:test9_result) |
| 613 | endfunc |
| 614 | |
| 615 | |
| 616 | "------------------------------------------------------------------------------- |
| 617 | " Test 10: :if, :elseif, :while argument parsing {{{1 |
| 618 | " |
| 619 | " A '"' or '|' in an argument expression must not be mixed up with |
| 620 | " a comment or a next command after a bar. Parsing errors should |
| 621 | " be recognized. |
| 622 | "------------------------------------------------------------------------------- |
| 623 | |
| 624 | XpathINIT |
| 625 | |
| 626 | function! MSG(enr, emsg) |
| 627 | let english = v:lang == "C" || v:lang =~ '^[Ee]n' |
| 628 | if a:enr == "" |
| 629 | Xout "TODO: Add message number for:" a:emsg |
| 630 | let v:errmsg = ":" . v:errmsg |
| 631 | endif |
| 632 | let match = 1 |
| 633 | if v:errmsg !~ '^'.a:enr.':' || (english && v:errmsg !~ a:emsg) |
| 634 | let match = 0 |
| 635 | if v:errmsg == "" |
| 636 | Xout "Message missing." |
| 637 | else |
| 638 | let v:errmsg = escape(v:errmsg, '"') |
| 639 | Xout "Unexpected message:" v:errmsg |
| 640 | endif |
| 641 | endif |
| 642 | return match |
| 643 | endfunction |
| 644 | |
| 645 | if 1 || strlen("\"") | Xpath 'a' |
| 646 | Xpath 'b' |
| 647 | endif |
| 648 | Xpath 'c' |
| 649 | |
| 650 | if 0 |
| 651 | elseif 1 || strlen("\"") | Xpath 'd' |
| 652 | Xpath 'e' |
| 653 | endif |
| 654 | Xpath 'f' |
| 655 | |
| 656 | while 1 || strlen("\"") | Xpath 'g' |
| 657 | Xpath 'h' |
| 658 | break |
| 659 | endwhile |
| 660 | Xpath 'i' |
| 661 | |
| 662 | let v:errmsg = "" |
| 663 | if 1 ||| strlen("\"") | Xpath 'j' |
| 664 | Xpath 'k' |
| 665 | endif |
| 666 | Xpath 'l' |
| 667 | if !MSG('E15', "Invalid expression") |
| 668 | Xpath 'm' |
| 669 | endif |
| 670 | |
| 671 | let v:errmsg = "" |
| 672 | if 0 |
| 673 | elseif 1 ||| strlen("\"") | Xpath 'n' |
| 674 | Xpath 'o' |
| 675 | endif |
| 676 | Xpath 'p' |
| 677 | if !MSG('E15', "Invalid expression") |
| 678 | Xpath 'q' |
| 679 | endif |
| 680 | |
| 681 | let v:errmsg = "" |
| 682 | while 1 ||| strlen("\"") | Xpath 'r' |
| 683 | Xpath 's' |
| 684 | break |
| 685 | endwhile |
| 686 | Xpath 't' |
| 687 | if !MSG('E15', "Invalid expression") |
| 688 | Xpath 'u' |
| 689 | endif |
| 690 | |
| 691 | let g:test10_result = g:Xpath |
| 692 | delfunction MSG |
| 693 | |
| 694 | func Test_expr_parsing() |
| 695 | call assert_equal('abcdefghilpt', g:test10_result) |
| 696 | endfunc |
| 697 | |
| 698 | |
| 699 | "------------------------------------------------------------------------------- |
| 700 | " Test 11: :if, :elseif, :while argument evaluation after abort {{{1 |
| 701 | " |
| 702 | " When code is skipped over due to an error, the boolean argument to |
| 703 | " an :if, :elseif, or :while must not be evaluated. |
| 704 | "------------------------------------------------------------------------------- |
| 705 | |
| 706 | XpathINIT |
| 707 | |
| 708 | let calls = 0 |
| 709 | |
| 710 | function! P(num) |
| 711 | let g:calls = g:calls + a:num " side effect on call |
| 712 | return 0 |
| 713 | endfunction |
| 714 | |
| 715 | if 1 |
| 716 | Xpath 'a' |
| 717 | asdf " error |
| 718 | Xpath 'b' |
| 719 | if P(1) " should not be called |
| 720 | Xpath 'c' |
| 721 | elseif !P(2) " should not be called |
| 722 | Xpath 'd' |
| 723 | else |
| 724 | Xpath 'e' |
| 725 | endif |
| 726 | Xpath 'f' |
| 727 | while P(4) " should not be called |
| 728 | Xpath 'g' |
| 729 | endwhile |
| 730 | Xpath 'h' |
| 731 | endif |
| 732 | Xpath 'x' |
| 733 | |
| 734 | let g:test11_calls = calls |
| 735 | let g:test11_result = g:Xpath |
| 736 | |
| 737 | unlet calls |
| 738 | delfunction P |
| 739 | |
| 740 | func Test_arg_abort() |
| 741 | call assert_equal(0, g:test11_calls) |
| 742 | call assert_equal('ax', g:test11_result) |
| 743 | endfunc |
| 744 | |
| 745 | |
| 746 | "------------------------------------------------------------------------------- |
| 747 | " Test 12: Expressions in braces in skipped code {{{1 |
| 748 | " |
| 749 | " In code skipped over due to an error or inactive conditional, |
| 750 | " an expression in braces as part of a variable or function name |
| 751 | " should not be evaluated. |
| 752 | "------------------------------------------------------------------------------- |
| 753 | |
| 754 | XpathINIT |
| 755 | |
| 756 | function! NULL() |
| 757 | Xpath 'a' |
| 758 | return 0 |
| 759 | endfunction |
| 760 | |
| 761 | function! ZERO() |
| 762 | Xpath 'b' |
| 763 | return 0 |
| 764 | endfunction |
| 765 | |
| 766 | function! F0() |
| 767 | Xpath 'c' |
| 768 | endfunction |
| 769 | |
| 770 | function! F1(arg) |
| 771 | Xpath 'e' |
| 772 | endfunction |
| 773 | |
| 774 | let V0 = 1 |
| 775 | |
| 776 | Xpath 'f' |
| 777 | echo 0 ? F{NULL() + V{ZERO()}}() : 1 |
| 778 | |
| 779 | Xpath 'g' |
| 780 | if 0 |
| 781 | Xpath 'h' |
| 782 | call F{NULL() + V{ZERO()}}() |
| 783 | endif |
| 784 | |
| 785 | Xpath 'i' |
| 786 | if 1 |
| 787 | asdf " error |
| 788 | Xpath 'j' |
| 789 | call F1(F{NULL() + V{ZERO()}}()) |
| 790 | endif |
| 791 | |
| 792 | Xpath 'k' |
| 793 | if 1 |
| 794 | asdf " error |
| 795 | Xpath 'l' |
| 796 | call F{NULL() + V{ZERO()}}() |
| 797 | endif |
| 798 | |
| 799 | let g:test12_result = g:Xpath |
| 800 | |
| 801 | func Test_braces_skipped() |
| 802 | call assert_equal('fgik', g:test12_result) |
| 803 | endfunc |
| 804 | |
| 805 | |
| 806 | "------------------------------------------------------------------------------- |
| 807 | " Test 13: Failure in argument evaluation for :while {{{1 |
| 808 | " |
| 809 | " A failure in the expression evaluation for the condition of a :while |
| 810 | " causes the whole :while loop until the matching :endwhile being |
| 811 | " ignored. Continuation is at the next following line. |
| 812 | "------------------------------------------------------------------------------- |
| 813 | |
| 814 | XpathINIT |
| 815 | |
| 816 | Xpath 'a' |
| 817 | while asdf |
| 818 | Xpath 'b' |
| 819 | while 1 |
| 820 | Xpath 'c' |
| 821 | break |
| 822 | endwhile |
| 823 | Xpath 'd' |
| 824 | break |
| 825 | endwhile |
| 826 | Xpath 'e' |
| 827 | |
| 828 | while asdf | Xpath 'f' | endwhile | Xpath 'g' |
| 829 | Xpath 'h' |
| 830 | let g:test13_result = g:Xpath |
| 831 | |
| 832 | func Test_while_fail() |
| 833 | call assert_equal('aeh', g:test13_result) |
| 834 | endfunc |
| 835 | |
| 836 | |
| 837 | "------------------------------------------------------------------------------- |
| 838 | " Test 14: Failure in argument evaluation for :if {{{1 |
| 839 | " |
| 840 | " A failure in the expression evaluation for the condition of an :if |
| 841 | " does not cause the corresponding :else or :endif being matched to |
| 842 | " a previous :if/:elseif. Neither of both branches of the failed :if |
| 843 | " are executed. |
| 844 | "------------------------------------------------------------------------------- |
| 845 | |
| 846 | XpathINIT |
| 847 | |
| 848 | function! F() |
| 849 | Xpath 'a' |
| 850 | let x = 0 |
| 851 | if x " false |
| 852 | Xpath 'b' |
| 853 | elseif !x " always true |
| 854 | Xpath 'c' |
| 855 | let x = 1 |
| 856 | if g:boolvar " possibly undefined |
| 857 | Xpath 'd' |
| 858 | else |
| 859 | Xpath 'e' |
| 860 | endif |
| 861 | Xpath 'f' |
| 862 | elseif x " never executed |
| 863 | Xpath 'g' |
| 864 | endif |
| 865 | Xpath 'h' |
| 866 | endfunction |
| 867 | |
| 868 | let boolvar = 1 |
| 869 | call F() |
| 870 | Xpath '-' |
| 871 | |
| 872 | unlet boolvar |
| 873 | call F() |
| 874 | let g:test14_result = g:Xpath |
| 875 | |
| 876 | delfunction F |
| 877 | |
| 878 | func Test_if_fail() |
| 879 | call assert_equal('acdfh-acfh', g:test14_result) |
| 880 | endfunc |
| 881 | |
| 882 | |
| 883 | "------------------------------------------------------------------------------- |
| 884 | " Test 15: Failure in argument evaluation for :if (bar) {{{1 |
| 885 | " |
| 886 | " Like previous test, except that the failing :if ... | ... | :endif |
| 887 | " is in a single line. |
| 888 | "------------------------------------------------------------------------------- |
| 889 | |
| 890 | XpathINIT |
| 891 | |
| 892 | function! F() |
| 893 | Xpath 'a' |
| 894 | let x = 0 |
| 895 | if x " false |
| 896 | Xpath 'b' |
| 897 | elseif !x " always true |
| 898 | Xpath 'c' |
| 899 | let x = 1 |
| 900 | if g:boolvar | Xpath 'd' | else | Xpath 'e' | endif |
| 901 | Xpath 'f' |
| 902 | elseif x " never executed |
| 903 | Xpath 'g' |
| 904 | endif |
| 905 | Xpath 'h' |
| 906 | endfunction |
| 907 | |
| 908 | let boolvar = 1 |
| 909 | call F() |
| 910 | Xpath '-' |
| 911 | |
| 912 | unlet boolvar |
| 913 | call F() |
| 914 | let g:test15_result = g:Xpath |
| 915 | |
| 916 | delfunction F |
| 917 | |
| 918 | func Test_if_bar_fail() |
| 919 | call assert_equal('acdfh-acfh', g:test15_result) |
| 920 | endfunc |
| 921 | |
Bram Moolenaar | 4119cf8 | 2016-01-17 14:59:01 +0100 | [diff] [blame] | 922 | "------------------------------------------------------------------------------- |
| 923 | " Test 90: Recognizing {} in variable name. {{{1 |
| 924 | "------------------------------------------------------------------------------- |
| 925 | |
| 926 | func Test_curlies() |
| 927 | let s:var = 66 |
| 928 | let ns = 's' |
| 929 | call assert_equal(66, {ns}:var) |
| 930 | |
| 931 | let g:a = {} |
| 932 | let g:b = 't' |
| 933 | let g:a[g:b] = 77 |
| 934 | call assert_equal(77, g:a['t']) |
| 935 | endfunc |
Bram Moolenaar | a2cce86 | 2016-01-02 19:50:04 +0100 | [diff] [blame] | 936 | |
| 937 | "------------------------------------------------------------------------------- |
Bram Moolenaar | f95534c | 2016-01-23 21:59:52 +0100 | [diff] [blame] | 938 | " Test 91: using type(). {{{1 |
| 939 | "------------------------------------------------------------------------------- |
| 940 | |
| 941 | func Test_type() |
| 942 | call assert_equal(0, type(0)) |
| 943 | call assert_equal(1, type("")) |
| 944 | call assert_equal(2, type(function("tr"))) |
| 945 | call assert_equal(3, type([])) |
| 946 | call assert_equal(4, type({})) |
| 947 | call assert_equal(5, type(0.0)) |
| 948 | call assert_equal(6, type(v:false)) |
| 949 | call assert_equal(6, type(v:true)) |
| 950 | call assert_equal(7, type(v:none)) |
| 951 | call assert_equal(7, type(v:null)) |
Bram Moolenaar | 17a1343 | 2016-01-24 14:22:10 +0100 | [diff] [blame] | 952 | |
| 953 | call assert_equal(0, 0 + v:false) |
| 954 | call assert_equal(1, 0 + v:true) |
| 955 | call assert_equal(0, 0 + v:none) |
| 956 | call assert_equal(0, 0 + v:null) |
| 957 | |
Bram Moolenaar | f48aa16 | 2016-01-24 17:54:24 +0100 | [diff] [blame] | 958 | call assert_equal('v:false', '' . v:false) |
| 959 | call assert_equal('v:true', '' . v:true) |
| 960 | call assert_equal('v:none', '' . v:none) |
| 961 | call assert_equal('v:null', '' . v:null) |
Bram Moolenaar | 6039c7f | 2016-01-24 15:05:32 +0100 | [diff] [blame] | 962 | |
| 963 | call assert_true(v:false == 0) |
| 964 | call assert_false(v:false != 0) |
| 965 | call assert_true(v:true == 1) |
| 966 | call assert_false(v:true != 1) |
| 967 | call assert_false(v:true == v:false) |
| 968 | call assert_true(v:true != v:false) |
| 969 | |
| 970 | call assert_true(v:null == 0) |
| 971 | call assert_false(v:null != 0) |
| 972 | call assert_true(v:none == 0) |
| 973 | call assert_false(v:none != 0) |
Bram Moolenaar | 0436922 | 2016-01-24 17:21:29 +0100 | [diff] [blame] | 974 | |
| 975 | call assert_true(v:false is v:false) |
| 976 | call assert_true(v:true is v:true) |
| 977 | call assert_true(v:none is v:none) |
| 978 | call assert_true(v:null is v:null) |
| 979 | |
| 980 | call assert_false(v:false isnot v:false) |
| 981 | call assert_false(v:true isnot v:true) |
| 982 | call assert_false(v:none isnot v:none) |
| 983 | call assert_false(v:null isnot v:null) |
| 984 | |
| 985 | call assert_false(v:false is 0) |
| 986 | call assert_false(v:true is 1) |
| 987 | call assert_false(v:true is v:false) |
| 988 | call assert_false(v:none is 0) |
| 989 | call assert_false(v:null is 0) |
| 990 | call assert_false(v:null is v:none) |
| 991 | |
| 992 | call assert_true(v:false isnot 0) |
| 993 | call assert_true(v:true isnot 1) |
| 994 | call assert_true(v:true isnot v:false) |
| 995 | call assert_true(v:none isnot 0) |
| 996 | call assert_true(v:null isnot 0) |
| 997 | call assert_true(v:null isnot v:none) |
Bram Moolenaar | 6559100 | 2016-01-24 21:51:57 +0100 | [diff] [blame] | 998 | |
| 999 | call assert_equal(v:false, eval(string(v:false))) |
| 1000 | call assert_equal(v:true, eval(string(v:true))) |
| 1001 | call assert_equal(v:none, eval(string(v:none))) |
| 1002 | call assert_equal(v:null, eval(string(v:null))) |
Bram Moolenaar | 767d8c1 | 2016-01-25 20:22:54 +0100 | [diff] [blame] | 1003 | |
| 1004 | call assert_true(empty(v:false)) |
| 1005 | call assert_false(empty(v:true)) |
| 1006 | call assert_true(empty(v:null)) |
| 1007 | call assert_true(empty(v:none)) |
Bram Moolenaar | 6650a69 | 2016-01-26 19:59:10 +0100 | [diff] [blame] | 1008 | |
| 1009 | func ChangeYourMind() |
| 1010 | try |
| 1011 | return v:true |
| 1012 | finally |
| 1013 | return 'something else' |
| 1014 | endtry |
| 1015 | endfunc |
| 1016 | |
| 1017 | call ChangeYourMind() |
Bram Moolenaar | f95534c | 2016-01-23 21:59:52 +0100 | [diff] [blame] | 1018 | endfunc |
| 1019 | |
| 1020 | "------------------------------------------------------------------------------- |
Bram Moolenaar | f49e240 | 2015-12-30 15:59:25 +0100 | [diff] [blame] | 1021 | " Modelines {{{1 |
| 1022 | " vim: ts=8 sw=4 tw=80 fdm=marker |
| 1023 | " vim: fdt=substitute(substitute(foldtext(),\ '\\%(^+--\\)\\@<=\\(\\s*\\)\\(.\\{-}\\)\:\ \\%(\"\ \\)\\=\\(Test\ \\d*\\)\:\\s*',\ '\\3\ (\\2)\:\ \\1',\ \"\"),\ '\\(Test\\s*\\)\\(\\d\\)\\D\\@=',\ '\\1\ \\2',\ "") |
| 1024 | "------------------------------------------------------------------------------- |