comparison src/testdir/test_vimscript.vim @ 21632:792398a9fe39 v8.2.1366

patch 8.2.1366: test 49 is old style Commit: https://github.com/vim/vim/commit/a6296200bd5191bab7efcdcc16c9e79eb498e8e0 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Aug 5 11:23:13 2020 +0200 patch 8.2.1366: test 49 is old style Problem: Test 49 is old style. Solution: Convert several tests to new style. (Yegappan Lakshmanan, closes #6629)
author Bram Moolenaar <Bram@vim.org>
date Wed, 05 Aug 2020 11:30:03 +0200
parents 8fb0e507491d
children 97b887aecd4a
comparison
equal deleted inserted replaced
21631:e55aba8c2146 21632:792398a9fe39
1 " Test various aspects of the Vim script language. 1 " Test various aspects of the Vim script language.
2 " Most of this was formerly in test49. 2 " Most of this was formerly in test49.
3 3
4 source check.vim 4 source check.vim
5 source shared.vim 5 source shared.vim
6 source script_util.vim
6 7
7 "------------------------------------------------------------------------------- 8 "-------------------------------------------------------------------------------
8 " Test environment {{{1 9 " Test environment {{{1
9 "------------------------------------------------------------------------------- 10 "-------------------------------------------------------------------------------
10
11 com! XpathINIT let g:Xpath = ''
12 com! -nargs=1 -bar Xpath let g:Xpath = g:Xpath . <args>
13 11
14 " Append a message to the "messages" file 12 " Append a message to the "messages" file
15 func Xout(text) 13 func Xout(text)
16 split messages 14 split messages
17 $put =a:text 15 $put =a:text
18 wq 16 wq
19 endfunc 17 endfunc
20 18
21 com! -nargs=1 Xout call Xout(<args>) 19 com! -nargs=1 Xout call Xout(<args>)
22 20
23 " MakeScript() - Make a script file from a function. {{{2 21 " Create a new instance of Vim and run the commands in 'test' and then 'verify'
24 " 22 " The commands in 'test' are expected to store the test results in the Xtest.out
25 " Create a script that consists of the body of the function a:funcname. 23 " file. If the test passes successfully, then Xtest.out should be empty.
26 " Replace any ":return" by a ":finish", any argument variable by a global 24 func RunInNewVim(test, verify)
27 " variable, and every ":call" by a ":source" for the next following argument 25 let init =<< trim END
28 " in the variable argument list. This function is useful if similar tests are 26 source script_util.vim
29 " to be made for a ":return" from a function call or a ":finish" in a script 27 XpathINIT
30 " file. 28 XloopINIT
31 func MakeScript(funcname, ...) 29 END
32 let script = tempname() 30 let cleanup =<< trim END
33 execute "redir! >" . script 31 call writefile(v:errors, 'Xtest.out')
34 execute "function" a:funcname 32 qall
35 redir END 33 END
36 execute "edit" script 34 call writefile(init, 'Xtest.vim')
37 " Delete the "function" and the "endfunction" lines. Do not include the 35 call writefile(a:test, 'Xtest.vim', 'a')
38 " word "function" in the pattern since it might be translated if LANG is 36 call writefile(a:verify, 'Xverify.vim')
39 " set. When MakeScript() is being debugged, this deletes also the debugging 37 call writefile(cleanup, 'Xverify.vim', 'a')
40 " output of its line 3 and 4. 38 call RunVim([], [], "-S Xtest.vim -S Xverify.vim")
41 exec '1,/.*' . a:funcname . '(.*)/d' 39 call assert_equal([], readfile('Xtest.out'))
42 /^\d*\s*endfunction\>/,$d 40 call delete('Xtest.out')
43 %s/^\d*//e 41 call delete('Xtest.vim')
44 %s/return/finish/e 42 call delete('Xverify.vim')
45 %s/\<a:\(\h\w*\)/g:\1/ge 43 endfunc
46 normal gg0
47 let cnt = 0
48 while search('\<call\s*\%(\u\|s:\)\w*\s*(.*)', 'W') > 0
49 let cnt = cnt + 1
50 s/\<call\s*\%(\u\|s:\)\w*\s*(.*)/\='source ' . a:{cnt}/
51 endwhile
52 g/^\s*$/d
53 write
54 bwipeout
55 return script
56 endfunc
57
58 " ExecAsScript - Source a temporary script made from a function. {{{2
59 "
60 " Make a temporary script file from the function a:funcname, ":source" it, and
61 " delete it afterwards. However, if an exception is thrown the file may remain,
62 " the caller should call DeleteTheScript() afterwards.
63 let s:script_name = ''
64 function! ExecAsScript(funcname)
65 " Make a script from the function passed as argument.
66 let s:script_name = MakeScript(a:funcname)
67
68 " Source and delete the script.
69 exec "source" s:script_name
70 call delete(s:script_name)
71 let s:script_name = ''
72 endfunction
73
74 function! DeleteTheScript()
75 if s:script_name
76 call delete(s:script_name)
77 let s:script_name = ''
78 endif
79 endfunc
80
81 com! -nargs=1 -bar ExecAsScript call ExecAsScript(<f-args>)
82
83 44
84 "------------------------------------------------------------------------------- 45 "-------------------------------------------------------------------------------
85 " Test 1: :endwhile in function {{{1 46 " Test 1: :endwhile in function {{{1
86 " 47 "
87 " Detect if a broken loop is (incorrectly) reactivated by the 48 " Detect if a broken loop is (incorrectly) reactivated by the
88 " :endwhile. Use a :return to prevent an endless loop, and make 49 " :endwhile. Use a :return to prevent an endless loop, and make
89 " this test first to get a meaningful result on an error before other 50 " this test first to get a meaningful result on an error before other
90 " tests will hang. 51 " tests will hang.
91 "------------------------------------------------------------------------------- 52 "-------------------------------------------------------------------------------
92 53
93 function! T1_F() 54 func T1_F()
94 Xpath 'a' 55 Xpath 'a'
95 let first = 1 56 let first = 1
96 while 1 57 while 1
97 Xpath 'b' 58 Xpath 'b'
98 if first 59 if first
102 else 63 else
103 Xpath 'd' 64 Xpath 'd'
104 return 65 return
105 endif 66 endif
106 endwhile 67 endwhile
107 endfunction 68 endfunc
108 69
109 function! T1_G() 70 func T1_G()
110 Xpath 'h' 71 Xpath 'h'
111 let first = 1 72 let first = 1
112 while 1 73 while 1
113 Xpath 'i' 74 Xpath 'i'
114 if first 75 if first
119 Xpath 'k' 80 Xpath 'k'
120 return 81 return
121 endif 82 endif
122 if 1 " unmatched :if 83 if 1 " unmatched :if
123 endwhile 84 endwhile
124 endfunction 85 endfunc
125 86
126 func Test_endwhile_function() 87 func Test_endwhile_function()
127 XpathINIT 88 XpathINIT
128 call T1_F() 89 call T1_F()
129 Xpath 'F' 90 Xpath 'F'
173 134
174 "------------------------------------------------------------------------------- 135 "-------------------------------------------------------------------------------
175 " Test 3: :if, :elseif, :while, :continue, :break {{{1 136 " Test 3: :if, :elseif, :while, :continue, :break {{{1
176 "------------------------------------------------------------------------------- 137 "-------------------------------------------------------------------------------
177 138
178 function Test_if_while() 139 func Test_if_while()
179 XpathINIT 140 XpathINIT
180 if 1 141 if 1
181 Xpath 'a' 142 Xpath 'a'
182 let loops = 3 143 let loops = 3
183 while loops > -1 " main loop: loops == 3, 2, 1 (which breaks) 144 while loops > -1 " main loop: loops == 3, 2, 1 (which breaks)
233 194
234 "------------------------------------------------------------------------------- 195 "-------------------------------------------------------------------------------
235 " Test 4: :return {{{1 196 " Test 4: :return {{{1
236 "------------------------------------------------------------------------------- 197 "-------------------------------------------------------------------------------
237 198
238 function! T4_F() 199 func T4_F()
239 if 1 200 if 1
240 Xpath 'a' 201 Xpath 'a'
241 let loops = 3 202 let loops = 3
242 while loops > 0 " 3: 2: 1: 203 while loops > 0 " 3: 2: 1:
243 Xpath 'b' . loops 204 Xpath 'b' . loops
251 endwhile 212 endwhile
252 Xpath 'f' 213 Xpath 'f'
253 else 214 else
254 Xpath 'g' 215 Xpath 'g'
255 endif 216 endif
256 endfunction 217 endfunc
257 218
258 function Test_return() 219 func Test_return()
259 XpathINIT 220 XpathINIT
260 call T4_F() 221 call T4_F()
261 Xpath '4' 222 Xpath '4'
262 223
263 call assert_equal('ab3e3b2c24', g:Xpath) 224 call assert_equal('ab3e3b2c24', g:Xpath)
264 endfunction 225 endfunc
265 226
266 227
267 "------------------------------------------------------------------------------- 228 "-------------------------------------------------------------------------------
268 " Test 5: :finish {{{1 229 " Test 5: :finish {{{1
269 " 230 "
270 " This test executes the body of the function T4_F from the previous 231 " This test executes the body of the function T4_F from the previous
271 " test as a script file (:return replaced by :finish). 232 " test as a script file (:return replaced by :finish).
272 "------------------------------------------------------------------------------- 233 "-------------------------------------------------------------------------------
273 234
274 function Test_finish() 235 func Test_finish()
275 XpathINIT 236 XpathINIT
276 ExecAsScript T4_F 237 ExecAsScript T4_F
277 Xpath '5' 238 Xpath '5'
278 call DeleteTheScript() 239 call DeleteTheScript()
279 240
280 call assert_equal('ab3e3b2c25', g:Xpath) 241 call assert_equal('ab3e3b2c25', g:Xpath)
281 endfunction 242 endfunc
282 243
283 244
284 245
285 "------------------------------------------------------------------------------- 246 "-------------------------------------------------------------------------------
286 " Test 6: Defining functions in :while loops {{{1 247 " Test 6: Defining functions in :while loops {{{1
410 delfunction G23 371 delfunction G23
411 delfunction G31 372 delfunction G31
412 delfunction G32 373 delfunction G32
413 delfunction G33 374 delfunction G33
414 375
415 function Test_defining_functions() 376 func Test_defining_functions()
416 call assert_equal('ade2ie3ibcg0h1g1h2g2h3fg0h1g1h2g2h3m', g:test6_result) 377 call assert_equal('ade2ie3ibcg0h1g1h2g2h3fg0h1g1h2g2h3m', g:test6_result)
417 call assert_equal('F1G1F2G21G22G23F3G31G32G33', g:test6_calls) 378 call assert_equal('F1G1F2G21G22G23F3G31G32G33', g:test6_calls)
418 endfunc 379 endfunc
419 380
420 "------------------------------------------------------------------------------- 381 "-------------------------------------------------------------------------------
474 " continues after the function call; the value -1 is returned then. 435 " continues after the function call; the value -1 is returned then.
475 "------------------------------------------------------------------------------- 436 "-------------------------------------------------------------------------------
476 437
477 XpathINIT 438 XpathINIT
478 439
479 function! T8_F() 440 func T8_F()
480 if 1 441 if 1
481 Xpath 'a' 442 Xpath 'a'
482 while 1 443 while 1
483 Xpath 'b' 444 Xpath 'b'
484 asdf 445 asdf
506 Xpath 'p' 467 Xpath 'p'
507 468
508 return novar " returns (default return value 0) 469 return novar " returns (default return value 0)
509 Xpath 'q' 470 Xpath 'q'
510 return 1 " not reached 471 return 1 " not reached
511 endfunction 472 endfunc
512 473
513 function! T8_G() abort 474 func T8_G() abort
514 if 1 475 if 1
515 Xpath 'r' 476 Xpath 'r'
516 while 1 477 while 1
517 Xpath 's' 478 Xpath 's'
518 asdf " returns -1 479 asdf " returns -1
522 Xpath 'v' 483 Xpath 'v'
523 endif | Xpath 'w' 484 endif | Xpath 'w'
524 Xpath 'x' 485 Xpath 'x'
525 486
526 return -4 " not reached 487 return -4 " not reached
527 endfunction 488 endfunc
528 489
529 function! T8_H() abort 490 func T8_H() abort
530 while 1 491 while 1
531 Xpath 'A' 492 Xpath 'A'
532 if 1 493 if 1
533 Xpath 'B' 494 Xpath 'B'
534 asdf " returns -1 495 asdf " returns -1
538 break 499 break
539 endwhile | Xpath 'E' 500 endwhile | Xpath 'E'
540 Xpath 'F' 501 Xpath 'F'
541 502
542 return -4 " not reached 503 return -4 " not reached
543 endfunction 504 endfunc
544 505
545 " Aborted functions (T8_G and T8_H) return -1. 506 " Aborted functions (T8_G and T8_H) return -1.
546 let g:test8_sum = (T8_F() + 1) - 4 * T8_G() - 8 * T8_H() 507 let g:test8_sum = (T8_F() + 1) - 4 * T8_G() - 8 * T8_H()
547 Xpath 'X' 508 Xpath 'X'
548 let g:test8_result = g:Xpath 509 let g:test8_result = g:Xpath
565 " "abort" attribute continues; the value -1 is returned then. 526 " "abort" attribute continues; the value -1 is returned then.
566 "------------------------------------------------------------------------------- 527 "-------------------------------------------------------------------------------
567 528
568 XpathINIT 529 XpathINIT
569 530
570 function! F() abort 531 func F() abort
571 Xpath 'a' 532 Xpath 'a'
572 let result = G() " not aborted 533 let result = G() " not aborted
573 Xpath 'b' 534 Xpath 'b'
574 if result != 2 535 if result != 2
575 Xpath 'c' 536 Xpath 'c'
576 endif 537 endif
577 return 1 538 return 1
578 endfunction 539 endfunc
579 540
580 function! G() " no abort attribute 541 func G() " no abort attribute
581 Xpath 'd' 542 Xpath 'd'
582 if H() != -1 " aborted 543 if H() != -1 " aborted
583 Xpath 'e' 544 Xpath 'e'
584 endif 545 endif
585 Xpath 'f' 546 Xpath 'f'
586 return 2 547 return 2
587 endfunction 548 endfunc
588 549
589 function! H() abort 550 func H() abort
590 Xpath 'g' 551 Xpath 'g'
591 call I() " aborted 552 call I() " aborted
592 Xpath 'h' 553 Xpath 'h'
593 return 4 554 return 4
594 endfunction 555 endfunc
595 556
596 function! I() abort 557 func I() abort
597 Xpath 'i' 558 Xpath 'i'
598 asdf " error 559 asdf " error
599 Xpath 'j' 560 Xpath 'j'
600 return 8 561 return 8
601 endfunction 562 endfunc
602 563
603 if F() != 1 564 if F() != 1
604 Xpath 'k' 565 Xpath 'k'
605 endif 566 endif
606 567
624 " be recognized. 585 " be recognized.
625 "------------------------------------------------------------------------------- 586 "-------------------------------------------------------------------------------
626 587
627 XpathINIT 588 XpathINIT
628 589
629 function! MSG(enr, emsg) 590 func MSG(enr, emsg)
630 let english = v:lang == "C" || v:lang =~ '^[Ee]n' 591 let english = v:lang == "C" || v:lang =~ '^[Ee]n'
631 if a:enr == "" 592 if a:enr == ""
632 Xout "TODO: Add message number for:" a:emsg 593 Xout "TODO: Add message number for:" a:emsg
633 let v:errmsg = ":" . v:errmsg 594 let v:errmsg = ":" . v:errmsg
634 endif 595 endif
708 669
709 XpathINIT 670 XpathINIT
710 671
711 let calls = 0 672 let calls = 0
712 673
713 function! P(num) 674 func P(num)
714 let g:calls = g:calls + a:num " side effect on call 675 let g:calls = g:calls + a:num " side effect on call
715 return 0 676 return 0
716 endfunction 677 endfunc
717 678
718 if 1 679 if 1
719 Xpath 'a' 680 Xpath 'a'
720 asdf " error 681 asdf " error
721 Xpath 'b' 682 Xpath 'b'
1090 call assert_fails('call T17_H()', 'E580:') 1051 call assert_fails('call T17_H()', 'E580:')
1091 call assert_equal('a1b1a0b0', g:Xpath) 1052 call assert_equal('a1b1a0b0', g:Xpath)
1092 endfunc 1053 endfunc
1093 1054
1094 "------------------------------------------------------------------------------- 1055 "-------------------------------------------------------------------------------
1095 "------------------------------------------------------------------------------- 1056 " Test 18: Interrupt (Ctrl-C pressed) {{{1
1057 "
1058 " On an interrupt, the script processing is terminated immediately.
1059 "-------------------------------------------------------------------------------
1060
1061 func Test_interrupt_while_if()
1062 let test =<< trim [CODE]
1063 try
1064 if 1
1065 Xpath 'a'
1066 while 1
1067 Xpath 'b'
1068 if 1
1069 Xpath 'c'
1070 call interrupt()
1071 call assert_report('should not get here')
1072 break
1073 finish
1074 endif | call assert_report('should not get here')
1075 call assert_report('should not get here')
1076 endwhile | call assert_report('should not get here')
1077 call assert_report('should not get here')
1078 endif | call assert_report('should not get here')
1079 call assert_report('should not get here')
1080 catch /^Vim:Interrupt$/
1081 Xpath 'd'
1082 endtry | Xpath 'e'
1083 Xpath 'f'
1084 [CODE]
1085 let verify =<< trim [CODE]
1086 call assert_equal('abcdef', g:Xpath)
1087 [CODE]
1088 call RunInNewVim(test, verify)
1089 endfunc
1090
1091 func Test_interrupt_try()
1092 let test =<< trim [CODE]
1093 try
1094 try
1095 Xpath 'a'
1096 call interrupt()
1097 call assert_report('should not get here')
1098 endtry | call assert_report('should not get here')
1099 call assert_report('should not get here')
1100 catch /^Vim:Interrupt$/
1101 Xpath 'b'
1102 endtry | Xpath 'c'
1103 Xpath 'd'
1104 [CODE]
1105 let verify =<< trim [CODE]
1106 call assert_equal('abcd', g:Xpath)
1107 [CODE]
1108 call RunInNewVim(test, verify)
1109 endfunc
1110
1111 func Test_interrupt_func_while_if()
1112 let test =<< trim [CODE]
1113 func F()
1114 if 1
1115 Xpath 'a'
1116 while 1
1117 Xpath 'b'
1118 if 1
1119 Xpath 'c'
1120 call interrupt()
1121 call assert_report('should not get here')
1122 break
1123 return
1124 endif | call assert_report('should not get here')
1125 call assert_report('should not get here')
1126 endwhile | call assert_report('should not get here')
1127 call assert_report('should not get here')
1128 endif | call assert_report('should not get here')
1129 call assert_report('should not get here')
1130 endfunc
1131
1132 Xpath 'd'
1133 try
1134 call F() | call assert_report('should not get here')
1135 catch /^Vim:Interrupt$/
1136 Xpath 'e'
1137 endtry | Xpath 'f'
1138 Xpath 'g'
1139 [CODE]
1140 let verify =<< trim [CODE]
1141 call assert_equal('dabcefg', g:Xpath)
1142 [CODE]
1143 call RunInNewVim(test, verify)
1144 endfunc
1145
1146 func Test_interrupt_func_try()
1147 let test =<< trim [CODE]
1148 func G()
1149 try
1150 Xpath 'a'
1151 call interrupt()
1152 call assert_report('should not get here')
1153 endtry | call assert_report('should not get here')
1154 call assert_report('should not get here')
1155 endfunc
1156
1157 Xpath 'b'
1158 try
1159 call G() | call assert_report('should not get here')
1160 catch /^Vim:Interrupt$/
1161 Xpath 'c'
1162 endtry | Xpath 'd'
1163 Xpath 'e'
1164 [CODE]
1165 let verify =<< trim [CODE]
1166 call assert_equal('bacde', g:Xpath)
1167 [CODE]
1168 call RunInNewVim(test, verify)
1169 endfunc
1170
1171 "-------------------------------------------------------------------------------
1172 " Test 19: Aborting on errors inside :try/:endtry {{{1
1173 "
1174 " An error in a command dynamically enclosed in a :try/:endtry region
1175 " aborts script processing immediately. It does not matter whether
1176 " the failing command is outside or inside a function and whether a
1177 " function has an "abort" attribute.
1178 "-------------------------------------------------------------------------------
1179
1180 func Test_try_error_abort_1()
1181 let test =<< trim [CODE]
1182 func F() abort
1183 Xpath 'a'
1184 asdf
1185 call assert_report('should not get here')
1186 endfunc
1187
1188 try
1189 Xpath 'b'
1190 call F()
1191 call assert_report('should not get here')
1192 endtry | call assert_report('should not get here')
1193 call assert_report('should not get here')
1194 [CODE]
1195 let verify =<< trim [CODE]
1196 call assert_equal('ba', g:Xpath)
1197 [CODE]
1198 call RunInNewVim(test, verify)
1199 endfunc
1200
1201 func Test_try_error_abort_2()
1202 let test =<< trim [CODE]
1203 func G()
1204 Xpath 'a'
1205 asdf
1206 call assert_report('should not get here')
1207 endfunc
1208
1209 try
1210 Xpath 'b'
1211 call G()
1212 call assert_report('should not get here')
1213 endtry | call assert_report('should not get here')
1214 call assert_report('should not get here')
1215 [CODE]
1216 let verify =<< trim [CODE]
1217 call assert_equal('ba', g:Xpath)
1218 [CODE]
1219 call RunInNewVim(test, verify)
1220 endfunc
1221
1222 func Test_try_error_abort_3()
1223 let test =<< trim [CODE]
1224 try
1225 Xpath 'a'
1226 asdf
1227 call assert_report('should not get here')
1228 endtry | call assert_report('should not get here')
1229 call assert_report('should not get here')
1230 [CODE]
1231 let verify =<< trim [CODE]
1232 call assert_equal('a', g:Xpath)
1233 [CODE]
1234 call RunInNewVim(test, verify)
1235 endfunc
1236
1237 func Test_try_error_abort_4()
1238 let test =<< trim [CODE]
1239 if 1
1240 try
1241 Xpath 'a'
1242 asdf
1243 call assert_report('should not get here')
1244 endtry | call assert_report('should not get here')
1245 endif | call assert_report('should not get here')
1246 call assert_report('should not get here')
1247 [CODE]
1248 let verify =<< trim [CODE]
1249 call assert_equal('a', g:Xpath)
1250 [CODE]
1251 call RunInNewVim(test, verify)
1252 endfunc
1253
1254 func Test_try_error_abort_5()
1255 let test =<< trim [CODE]
1256 let p = 1
1257 while p
1258 let p = 0
1259 try
1260 Xpath 'a'
1261 asdf
1262 call assert_report('should not get here')
1263 endtry | call assert_report('should not get here')
1264 endwhile | call assert_report('should not get here')
1265 call assert_report('should not get here')
1266 [CODE]
1267 let verify =<< trim [CODE]
1268 call assert_equal('a', g:Xpath)
1269 [CODE]
1270 call RunInNewVim(test, verify)
1271 endfunc
1272
1273 func Test_try_error_abort_6()
1274 let test =<< trim [CODE]
1275 let p = 1
1276 Xpath 'a'
1277 while p
1278 Xpath 'b'
1279 let p = 0
1280 try
1281 Xpath 'c'
1282 endwhile | call assert_report('should not get here')
1283 call assert_report('should not get here')
1284 [CODE]
1285 let verify =<< trim [CODE]
1286 call assert_equal('abc', g:Xpath)
1287 [CODE]
1288 call RunInNewVim(test, verify)
1289 endfunc
1290
1291 "-------------------------------------------------------------------------------
1292 " Test 20: Aborting on errors after :try/:endtry {{{1
1293 "
1294 " When an error occurs after the last active :try/:endtry region has
1295 " been left, termination behavior is as if no :try/:endtry has been
1296 " seen.
1297 "-------------------------------------------------------------------------------
1298
1299 func Test_error_after_try_1()
1300 let test =<< trim [CODE]
1301 let p = 1
1302 while p
1303 let p = 0
1304 Xpath 'a'
1305 try
1306 Xpath 'b'
1307 endtry
1308 asdf
1309 call assert_report('should not get here')
1310 endwhile | call assert_report('should not get here')
1311 Xpath 'c'
1312 [CODE]
1313 let verify =<< trim [CODE]
1314 call assert_equal('abc', g:Xpath)
1315 [CODE]
1316 call RunInNewVim(test, verify)
1317 endfunc
1318
1319 func Test_error_after_try_2()
1320 let test =<< trim [CODE]
1321 while 1
1322 try
1323 Xpath 'a'
1324 break
1325 call assert_report('should not get here')
1326 endtry
1327 endwhile
1328 Xpath 'b'
1329 asdf
1330 Xpath 'c'
1331 [CODE]
1332 let verify =<< trim [CODE]
1333 call assert_equal('abc', g:Xpath)
1334 [CODE]
1335 call RunInNewVim(test, verify)
1336 endfunc
1337
1338 func Test_error_after_try_3()
1339 let test =<< trim [CODE]
1340 while 1
1341 try
1342 Xpath 'a'
1343 break
1344 call assert_report('should not get here')
1345 finally
1346 Xpath 'b'
1347 endtry
1348 endwhile
1349 Xpath 'c'
1350 asdf
1351 Xpath 'd'
1352 [CODE]
1353 let verify =<< trim [CODE]
1354 call assert_equal('abcd', g:Xpath)
1355 [CODE]
1356 call RunInNewVim(test, verify)
1357 endfunc
1358
1359 func Test_error_after_try_4()
1360 let test =<< trim [CODE]
1361 while 1
1362 try
1363 Xpath 'a'
1364 finally
1365 Xpath 'b'
1366 break
1367 call assert_report('should not get here')
1368 endtry
1369 endwhile
1370 Xpath 'c'
1371 asdf
1372 Xpath 'd'
1373 [CODE]
1374 let verify =<< trim [CODE]
1375 call assert_equal('abcd', g:Xpath)
1376 [CODE]
1377 call RunInNewVim(test, verify)
1378 endfunc
1379
1380 func Test_error_after_try_5()
1381 let test =<< trim [CODE]
1382 let p = 1
1383 while p
1384 let p = 0
1385 try
1386 Xpath 'a'
1387 continue
1388 call assert_report('should not get here')
1389 endtry
1390 endwhile
1391 Xpath 'b'
1392 asdf
1393 Xpath 'c'
1394 [CODE]
1395 let verify =<< trim [CODE]
1396 call assert_equal('abc', g:Xpath)
1397 [CODE]
1398 call RunInNewVim(test, verify)
1399 endfunc
1400
1401 func Test_error_after_try_6()
1402 let test =<< trim [CODE]
1403 let p = 1
1404 while p
1405 let p = 0
1406 try
1407 Xpath 'a'
1408 continue
1409 call assert_report('should not get here')
1410 finally
1411 Xpath 'b'
1412 endtry
1413 endwhile
1414 Xpath 'c'
1415 asdf
1416 Xpath 'd'
1417 [CODE]
1418 let verify =<< trim [CODE]
1419 call assert_equal('abcd', g:Xpath)
1420 [CODE]
1421 call RunInNewVim(test, verify)
1422 endfunc
1423
1424 func Test_error_after_try_7()
1425 let test =<< trim [CODE]
1426 let p = 1
1427 while p
1428 let p = 0
1429 try
1430 Xpath 'a'
1431 finally
1432 Xpath 'b'
1433 continue
1434 call assert_report('should not get here')
1435 endtry
1436 endwhile
1437 Xpath 'c'
1438 asdf
1439 Xpath 'd'
1440 [CODE]
1441 let verify =<< trim [CODE]
1442 call assert_equal('abcd', g:Xpath)
1443 [CODE]
1444 call RunInNewVim(test, verify)
1445 endfunc
1446
1447 "-------------------------------------------------------------------------------
1448 " Test 21: :finally for :try after :continue/:break/:return/:finish {{{1
1449 "
1450 " If a :try conditional stays inactive due to a preceding :continue,
1451 " :break, :return, or :finish, its :finally clause should not be
1452 " executed.
1453 "-------------------------------------------------------------------------------
1454
1455 func Test_finally_after_loop_ctrl_statement()
1456 let test =<< trim [CODE]
1457 func F()
1458 let loops = 2
1459 while loops > 0
1460 XloopNEXT
1461 let loops = loops - 1
1462 try
1463 if loops == 1
1464 Xloop 'a'
1465 continue
1466 call assert_report('should not get here')
1467 elseif loops == 0
1468 Xloop 'b'
1469 break
1470 call assert_report('should not get here')
1471 endif
1472
1473 try " inactive
1474 call assert_report('should not get here')
1475 finally
1476 call assert_report('should not get here')
1477 endtry
1478 finally
1479 Xloop 'c'
1480 endtry
1481 call assert_report('should not get here')
1482 endwhile
1483
1484 try
1485 Xpath 'd'
1486 return
1487 call assert_report('should not get here')
1488 try " inactive
1489 call assert_report('should not get here')
1490 finally
1491 call assert_report('should not get here')
1492 endtry
1493 finally
1494 Xpath 'e'
1495 endtry
1496 call assert_report('should not get here')
1497 endfunc
1498
1499 try
1500 Xpath 'f'
1501 call F()
1502 Xpath 'g'
1503 finish
1504 call assert_report('should not get here')
1505 try " inactive
1506 call assert_report('should not get here')
1507 finally
1508 call assert_report('should not get here')
1509 endtry
1510 finally
1511 Xpath 'h'
1512 endtry
1513 call assert_report('should not get here')
1514 [CODE]
1515 let verify =<< trim [CODE]
1516 call assert_equal('fa2c2b3c3degh', g:Xpath)
1517 [CODE]
1518 call RunInNewVim(test, verify)
1519 endfunc
1520
1521 "-------------------------------------------------------------------------------
1522 " Test 22: :finally for a :try after an error/interrupt/:throw {{{1
1523 "
1524 " If a :try conditional stays inactive due to a preceding error or
1525 " interrupt or :throw, its :finally clause should not be executed.
1526 "-------------------------------------------------------------------------------
1527
1528 func Test_finally_after_error_in_func()
1529 let test =<< trim [CODE]
1530 func Error()
1531 try
1532 Xpath 'b'
1533 asdf " aborting error, triggering error exception
1534 call assert_report('should not get here')
1535 endtry
1536 call assert_report('should not get here')
1537 endfunc
1538
1539 Xpath 'a'
1540 call Error()
1541 call assert_report('should not get here')
1542
1543 if 1 " not active due to error
1544 try " not active since :if inactive
1545 call assert_report('should not get here')
1546 finally
1547 call assert_report('should not get here')
1548 endtry
1549 endif
1550
1551 try " not active due to error
1552 call assert_report('should not get here')
1553 finally
1554 call assert_report('should not get here')
1555 endtry
1556 [CODE]
1557 let verify =<< trim [CODE]
1558 call assert_equal('ab', g:Xpath)
1559 [CODE]
1560 call RunInNewVim(test, verify)
1561 endfunc
1562
1563 func Test_finally_after_interrupt()
1564 let test =<< trim [CODE]
1565 func Interrupt()
1566 try
1567 Xpath 'a'
1568 call interrupt() " triggering interrupt exception
1569 call assert_report('should not get here')
1570 endtry
1571 endfunc
1572
1573 Xpath 'b'
1574 try
1575 call Interrupt()
1576 catch /^Vim:Interrupt$/
1577 Xpath 'c'
1578 finish
1579 endtry
1580 call assert_report('should not get here')
1581
1582 if 1 " not active due to interrupt
1583 try " not active since :if inactive
1584 call assert_report('should not get here')
1585 finally
1586 call assert_report('should not get here')
1587 endtry
1588 endif
1589
1590 try " not active due to interrupt
1591 call assert_report('should not get here')
1592 finally
1593 call assert_report('should not get here')
1594 endtry
1595 [CODE]
1596 let verify =<< trim [CODE]
1597 call assert_equal('bac', g:Xpath)
1598 [CODE]
1599 call RunInNewVim(test, verify)
1600 endfunc
1601
1602 func Test_finally_after_throw()
1603 let test =<< trim [CODE]
1604 func Throw()
1605 Xpath 'a'
1606 throw 'xyz'
1607 endfunc
1608
1609 Xpath 'b'
1610 call Throw()
1611 call assert_report('should not get here')
1612
1613 if 1 " not active due to :throw
1614 try " not active since :if inactive
1615 call assert_report('should not get here')
1616 finally
1617 call assert_report('should not get here')
1618 endtry
1619 endif
1620
1621 try " not active due to :throw
1622 call assert_report('should not get here')
1623 finally
1624 call assert_report('should not get here')
1625 endtry
1626 [CODE]
1627 let verify =<< trim [CODE]
1628 call assert_equal('ba', g:Xpath)
1629 [CODE]
1630 call RunInNewVim(test, verify)
1631 endfunc
1632
1633 "-------------------------------------------------------------------------------
1634 " Test 23: :catch clauses for a :try after a :throw {{{1
1635 "
1636 " If a :try conditional stays inactive due to a preceding :throw,
1637 " none of its :catch clauses should be executed.
1638 "-------------------------------------------------------------------------------
1639
1640 func Test_catch_after_throw()
1641 let test =<< trim [CODE]
1642 try
1643 Xpath 'a'
1644 throw "xyz"
1645 call assert_report('should not get here')
1646
1647 if 1 " not active due to :throw
1648 try " not active since :if inactive
1649 call assert_report('should not get here')
1650 catch /xyz/
1651 call assert_report('should not get here')
1652 endtry
1653 endif
1654 catch /xyz/
1655 Xpath 'b'
1656 endtry
1657
1658 Xpath 'c'
1659 throw "abc"
1660 call assert_report('should not get here')
1661
1662 try " not active due to :throw
1663 call assert_report('should not get here')
1664 catch /abc/
1665 call assert_report('should not get here')
1666 endtry
1667 [CODE]
1668 let verify =<< trim [CODE]
1669 call assert_equal('abc', g:Xpath)
1670 [CODE]
1671 call RunInNewVim(test, verify)
1672 endfunc
1673
1674 "-------------------------------------------------------------------------------
1675 " Test 24: :endtry for a :try after a :throw {{{1
1676 "
1677 " If a :try conditional stays inactive due to a preceding :throw,
1678 " its :endtry should not rethrow the exception to the next surrounding
1679 " active :try conditional.
1680 "-------------------------------------------------------------------------------
1681
1682 func Test_endtry_after_throw()
1683 let test =<< trim [CODE]
1684 try " try 1
1685 try " try 2
1686 Xpath 'a'
1687 throw "xyz" " makes try 2 inactive
1688 call assert_report('should not get here')
1689
1690 try " try 3
1691 call assert_report('should not get here')
1692 endtry " no rethrow to try 1
1693 catch /xyz/ " should catch although try 2 inactive
1694 Xpath 'b'
1695 endtry
1696 catch /xyz/ " try 1 active, but exception already caught
1697 call assert_report('should not get here')
1698 endtry
1699 Xpath 'c'
1700 [CODE]
1701 let verify =<< trim [CODE]
1702 call assert_equal('abc', g:Xpath)
1703 [CODE]
1704 call RunInNewVim(test, verify)
1705 endfunc
1706
1707 "-------------------------------------------------------------------------------
1708 " Test 27: Executing :finally clauses after :return {{{1
1709 "
1710 " For a :return command dynamically enclosed in a :try/:endtry region,
1711 " :finally clauses are executed and the called function is ended.
1712 "-------------------------------------------------------------------------------
1713
1714 func T27_F()
1715 try
1716 Xpath 'a'
1717 try
1718 Xpath 'b'
1719 return
1720 call assert_report('should not get here')
1721 finally
1722 Xpath 'c'
1723 endtry
1724 Xpath 'd'
1725 finally
1726 Xpath 'e'
1727 endtry
1728 call assert_report('should not get here')
1729 endfunc
1730
1731 func T27_G()
1732 try
1733 Xpath 'f'
1734 return
1735 call assert_report('should not get here')
1736 finally
1737 Xpath 'g'
1738 call T27_F()
1739 Xpath 'h'
1740 endtry
1741 call assert_report('should not get here')
1742 endfunc
1743
1744 func T27_H()
1745 try
1746 Xpath 'i'
1747 call T27_G()
1748 Xpath 'j'
1749 finally
1750 Xpath 'k'
1751 return
1752 call assert_report('should not get here')
1753 endtry
1754 call assert_report('should not get here')
1755 endfunction
1756
1757 func Test_finally_after_return()
1758 XpathINIT
1759 try
1760 Xpath 'l'
1761 call T27_H()
1762 Xpath 'm'
1763 finally
1764 Xpath 'n'
1765 endtry
1766 call assert_equal('lifgabcehjkmn', g:Xpath)
1767 endfunc
1768
1769 "-------------------------------------------------------------------------------
1770 " Test 28: Executing :finally clauses after :finish {{{1
1771 "
1772 " For a :finish command dynamically enclosed in a :try/:endtry region,
1773 " :finally clauses are executed and the sourced file is finished.
1774 "
1775 " This test executes the bodies of the functions F, G, and H from the
1776 " previous test as script files (:return replaced by :finish).
1777 "-------------------------------------------------------------------------------
1778
1779 func Test_finally_after_finish()
1780 XpathINIT
1781
1782 let scriptF = MakeScript("T27_F")
1783 let scriptG = MakeScript("T27_G", scriptF)
1784 let scriptH = MakeScript("T27_H", scriptG)
1785
1786 try
1787 Xpath 'A'
1788 exec "source" scriptH
1789 Xpath 'B'
1790 finally
1791 Xpath 'C'
1792 endtry
1793 Xpath 'D'
1794 call assert_equal('AifgabcehjkBCD', g:Xpath)
1795 call delete(scriptF)
1796 call delete(scriptG)
1797 call delete(scriptH)
1798 endfunc
1799
1800 "-------------------------------------------------------------------------------
1801 " Test 29: Executing :finally clauses on errors {{{1
1802 "
1803 " After an error in a command dynamically enclosed in a :try/:endtry
1804 " region, :finally clauses are executed and the script processing is
1805 " terminated.
1806 "-------------------------------------------------------------------------------
1807
1808 func Test_finally_after_error_1()
1809 let test =<< trim [CODE]
1810 func F()
1811 while 1
1812 try
1813 Xpath 'a'
1814 while 1
1815 try
1816 Xpath 'b'
1817 asdf " error
1818 call assert_report('should not get here')
1819 finally
1820 Xpath 'c'
1821 endtry | call assert_report('should not get here')
1822 call assert_report('should not get here')
1823 break
1824 endwhile
1825 call assert_report('should not get here')
1826 finally
1827 Xpath 'd'
1828 endtry | call assert_report('should not get here')
1829 call assert_report('should not get here')
1830 break
1831 endwhile
1832 call assert_report('should not get here')
1833 endfunc
1834
1835 while 1
1836 try
1837 Xpath 'e'
1838 while 1
1839 call F()
1840 call assert_report('should not get here')
1841 break
1842 endwhile | call assert_report('should not get here')
1843 call assert_report('should not get here')
1844 finally
1845 Xpath 'f'
1846 endtry | call assert_report('should not get here')
1847 endwhile | call assert_report('should not get here')
1848 call assert_report('should not get here')
1849 [CODE]
1850 let verify =<< trim [CODE]
1851 call assert_equal('eabcdf', g:Xpath)
1852 [CODE]
1853 call RunInNewVim(test, verify)
1854 endfunc
1855
1856 func Test_finally_after_error_2()
1857 let test =<< trim [CODE]
1858 func G() abort
1859 if 1
1860 try
1861 Xpath 'a'
1862 asdf " error
1863 call assert_report('should not get here')
1864 finally
1865 Xpath 'b'
1866 endtry | Xpath 'c'
1867 endif | Xpath 'd'
1868 call assert_report('should not get here')
1869 endfunc
1870
1871 if 1
1872 try
1873 Xpath 'e'
1874 call G()
1875 call assert_report('should not get here')
1876 finally
1877 Xpath 'f'
1878 endtry | call assert_report('should not get here')
1879 endif | call assert_report('should not get here')
1880 call assert_report('should not get here')
1881 [CODE]
1882 let verify =<< trim [CODE]
1883 call assert_equal('eabf', g:Xpath)
1884 [CODE]
1885 call RunInNewVim(test, verify)
1886 endfunc
1887
1888 "-------------------------------------------------------------------------------
1889 " Test 30: Executing :finally clauses on interrupt {{{1
1890 "
1891 " After an interrupt in a command dynamically enclosed in
1892 " a :try/:endtry region, :finally clauses are executed and the
1893 " script processing is terminated.
1894 "-------------------------------------------------------------------------------
1895
1896 func Test_finally_on_interrupt()
1897 let test =<< trim [CODE]
1898 func F()
1899 try
1900 Xloop 'a'
1901 call interrupt()
1902 call assert_report('should not get here')
1903 finally
1904 Xloop 'b'
1905 endtry
1906 call assert_report('should not get here')
1907 endfunc
1908
1909 try
1910 try
1911 Xpath 'c'
1912 try
1913 Xpath 'd'
1914 call interrupt()
1915 call assert_report('should not get here')
1916 finally
1917 Xpath 'e'
1918 try
1919 Xpath 'f'
1920 try
1921 Xpath 'g'
1922 finally
1923 Xpath 'h'
1924 try
1925 Xpath 'i'
1926 call interrupt()
1927 call assert_report('should not get here')
1928 endtry
1929 call assert_report('should not get here')
1930 endtry
1931 call assert_report('should not get here')
1932 endtry
1933 call assert_report('should not get here')
1934 endtry
1935 call assert_report('should not get here')
1936 finally
1937 Xpath 'j'
1938 try
1939 Xpath 'k'
1940 call F()
1941 call assert_report('should not get here')
1942 finally
1943 Xpath 'l'
1944 try
1945 Xpath 'm'
1946 XloopNEXT
1947 ExecAsScript F
1948 call assert_report('should not get here')
1949 finally
1950 Xpath 'n'
1951 endtry
1952 call assert_report('should not get here')
1953 endtry
1954 call assert_report('should not get here')
1955 endtry
1956 call assert_report('should not get here')
1957 catch /^Vim:Interrupt$/
1958 Xpath 'o'
1959 endtry
1960 [CODE]
1961 let verify =<< trim [CODE]
1962 call assert_equal('cdefghijka1b1lma2b2no', g:Xpath)
1963 [CODE]
1964 call RunInNewVim(test, verify)
1965 endfunc
1966
1967 "-------------------------------------------------------------------------------
1968 " Test 31: Executing :finally clauses after :throw {{{1
1969 "
1970 " After a :throw dynamically enclosed in a :try/:endtry region,
1971 " :finally clauses are executed and the script processing is
1972 " terminated.
1973 "-------------------------------------------------------------------------------
1974
1975 func Test_finally_after_throw_2()
1976 let test =<< trim [CODE]
1977 func F()
1978 try
1979 Xloop 'a'
1980 throw "exception"
1981 call assert_report('should not get here')
1982 finally
1983 Xloop 'b'
1984 endtry
1985 call assert_report('should not get here')
1986 endfunc
1987
1988 try
1989 Xpath 'c'
1990 try
1991 Xpath 'd'
1992 throw "exception"
1993 call assert_report('should not get here')
1994 finally
1995 Xpath 'e'
1996 try
1997 Xpath 'f'
1998 try
1999 Xpath 'g'
2000 finally
2001 Xpath 'h'
2002 try
2003 Xpath 'i'
2004 throw "exception"
2005 call assert_report('should not get here')
2006 endtry
2007 call assert_report('should not get here')
2008 endtry
2009 call assert_report('should not get here')
2010 endtry
2011 call assert_report('should not get here')
2012 endtry
2013 call assert_report('should not get here')
2014 finally
2015 Xpath 'j'
2016 try
2017 Xpath 'k'
2018 call F()
2019 call assert_report('should not get here')
2020 finally
2021 Xpath 'l'
2022 try
2023 Xpath 'm'
2024 XloopNEXT
2025 ExecAsScript F
2026 call assert_report('should not get here')
2027 finally
2028 Xpath 'n'
2029 endtry
2030 call assert_report('should not get here')
2031 endtry
2032 call assert_report('should not get here')
2033 endtry
2034 call assert_report('should not get here')
2035 [CODE]
2036 let verify =<< trim [CODE]
2037 call assert_equal('cdefghijka1b1lma2b2n', g:Xpath)
2038 [CODE]
2039 call RunInNewVim(test, verify)
2040 endfunc
2041
2042 "-------------------------------------------------------------------------------
2043 " Test 34: :finally reason discarded by :continue {{{1
2044 "
2045 " When a :finally clause is executed due to a :continue, :break,
2046 " :return, :finish, error, interrupt or :throw, the jump reason is
2047 " discarded by a :continue in the finally clause.
2048 "-------------------------------------------------------------------------------
2049
2050 func Test_finally_after_continue()
2051 let test =<< trim [CODE]
2052 func C(jump)
2053 XloopNEXT
2054 let loop = 0
2055 while loop < 2
2056 let loop = loop + 1
2057 if loop == 1
2058 try
2059 if a:jump == "continue"
2060 continue
2061 elseif a:jump == "break"
2062 break
2063 elseif a:jump == "return" || a:jump == "finish"
2064 return
2065 elseif a:jump == "error"
2066 asdf
2067 elseif a:jump == "interrupt"
2068 call interrupt()
2069 let dummy = 0
2070 elseif a:jump == "throw"
2071 throw "abc"
2072 endif
2073 finally
2074 continue " discards jump that caused the :finally
2075 call assert_report('should not get here')
2076 endtry
2077 call assert_report('should not get here')
2078 elseif loop == 2
2079 Xloop 'a'
2080 endif
2081 endwhile
2082 endfunc
2083
2084 call C("continue")
2085 Xpath 'b'
2086 call C("break")
2087 Xpath 'c'
2088 call C("return")
2089 Xpath 'd'
2090 let g:jump = "finish"
2091 ExecAsScript C
2092 unlet g:jump
2093 Xpath 'e'
2094 try
2095 call C("error")
2096 Xpath 'f'
2097 finally
2098 Xpath 'g'
2099 try
2100 call C("interrupt")
2101 Xpath 'h'
2102 finally
2103 Xpath 'i'
2104 call C("throw")
2105 Xpath 'j'
2106 endtry
2107 endtry
2108 Xpath 'k'
2109 [CODE]
2110 let verify =<< trim [CODE]
2111 call assert_equal('a2ba3ca4da5ea6fga7hia8jk', g:Xpath)
2112 [CODE]
2113 call RunInNewVim(test, verify)
2114 endfunc
2115
2116 "-------------------------------------------------------------------------------
2117 " Test 35: :finally reason discarded by :break {{{1
2118 "
2119 " When a :finally clause is executed due to a :continue, :break,
2120 " :return, :finish, error, interrupt or :throw, the jump reason is
2121 " discarded by a :break in the finally clause.
2122 "-------------------------------------------------------------------------------
2123
2124 func Test_finally_discard_by_break()
2125 let test =<< trim [CODE]
2126 func B(jump)
2127 XloopNEXT
2128 let loop = 0
2129 while loop < 2
2130 let loop = loop + 1
2131 if loop == 1
2132 try
2133 if a:jump == "continue"
2134 continue
2135 elseif a:jump == "break"
2136 break
2137 elseif a:jump == "return" || a:jump == "finish"
2138 return
2139 elseif a:jump == "error"
2140 asdf
2141 elseif a:jump == "interrupt"
2142 call interrupt()
2143 let dummy = 0
2144 elseif a:jump == "throw"
2145 throw "abc"
2146 endif
2147 finally
2148 break " discards jump that caused the :finally
2149 call assert_report('should not get here')
2150 endtry
2151 elseif loop == 2
2152 call assert_report('should not get here')
2153 endif
2154 endwhile
2155 Xloop 'a'
2156 endfunc
2157
2158 call B("continue")
2159 Xpath 'b'
2160 call B("break")
2161 Xpath 'c'
2162 call B("return")
2163 Xpath 'd'
2164 let g:jump = "finish"
2165 ExecAsScript B
2166 unlet g:jump
2167 Xpath 'e'
2168 try
2169 call B("error")
2170 Xpath 'f'
2171 finally
2172 Xpath 'g'
2173 try
2174 call B("interrupt")
2175 Xpath 'h'
2176 finally
2177 Xpath 'i'
2178 call B("throw")
2179 Xpath 'j'
2180 endtry
2181 endtry
2182 Xpath 'k'
2183 [CODE]
2184 let verify =<< trim [CODE]
2185 call assert_equal('a2ba3ca4da5ea6fga7hia8jk', g:Xpath)
2186 [CODE]
2187 call RunInNewVim(test, verify)
2188 endfunc
2189
2190 "-------------------------------------------------------------------------------
2191 " Test 36: :finally reason discarded by :return {{{1
2192 "
2193 " When a :finally clause is executed due to a :continue, :break,
2194 " :return, :finish, error, interrupt or :throw, the jump reason is
2195 " discarded by a :return in the finally clause.
2196 "-------------------------------------------------------------------------------
2197
2198 func Test_finally_discard_by_return()
2199 let test =<< trim [CODE]
2200 func R(jump, retval) abort
2201 let loop = 0
2202 while loop < 2
2203 let loop = loop + 1
2204 if loop == 1
2205 try
2206 if a:jump == "continue"
2207 continue
2208 elseif a:jump == "break"
2209 break
2210 elseif a:jump == "return"
2211 return
2212 elseif a:jump == "error"
2213 asdf
2214 elseif a:jump == "interrupt"
2215 call interrupt()
2216 let dummy = 0
2217 elseif a:jump == "throw"
2218 throw "abc"
2219 endif
2220 finally
2221 return a:retval " discards jump that caused the :finally
2222 call assert_report('should not get here')
2223 endtry
2224 elseif loop == 2
2225 call assert_report('should not get here')
2226 endif
2227 endwhile
2228 call assert_report('should not get here')
2229 endfunc
2230
2231 let sum = -R("continue", -8)
2232 Xpath 'a'
2233 let sum = sum - R("break", -16)
2234 Xpath 'b'
2235 let sum = sum - R("return", -32)
2236 Xpath 'c'
2237 try
2238 let sum = sum - R("error", -64)
2239 Xpath 'd'
2240 finally
2241 Xpath 'e'
2242 try
2243 let sum = sum - R("interrupt", -128)
2244 Xpath 'f'
2245 finally
2246 Xpath 'g'
2247 let sum = sum - R("throw", -256)
2248 Xpath 'h'
2249 endtry
2250 endtry
2251 Xpath 'i'
2252
2253 let expected = 8 + 16 + 32 + 64 + 128 + 256
2254 call assert_equal(sum, expected)
2255 [CODE]
2256 let verify =<< trim [CODE]
2257 call assert_equal('abcdefghi', g:Xpath)
2258 [CODE]
2259 call RunInNewVim(test, verify)
2260 endfunc
2261
2262 "-------------------------------------------------------------------------------
2263 " Test 37: :finally reason discarded by :finish {{{1
2264 "
2265 " When a :finally clause is executed due to a :continue, :break,
2266 " :return, :finish, error, interrupt or :throw, the jump reason is
2267 " discarded by a :finish in the finally clause.
2268 "-------------------------------------------------------------------------------
2269
2270 func Test_finally_discard_by_finish()
2271 let test =<< trim [CODE]
2272 func F(jump) " not executed as function, transformed to a script
2273 let loop = 0
2274 while loop < 2
2275 let loop = loop + 1
2276 if loop == 1
2277 try
2278 if a:jump == "continue"
2279 continue
2280 elseif a:jump == "break"
2281 break
2282 elseif a:jump == "finish"
2283 finish
2284 elseif a:jump == "error"
2285 asdf
2286 elseif a:jump == "interrupt"
2287 call interrupt()
2288 let dummy = 0
2289 elseif a:jump == "throw"
2290 throw "abc"
2291 endif
2292 finally
2293 finish " discards jump that caused the :finally
2294 call assert_report('should not get here')
2295 endtry
2296 elseif loop == 2
2297 call assert_report('should not get here')
2298 endif
2299 endwhile
2300 call assert_report('should not get here')
2301 endfunc
2302
2303 let scriptF = MakeScript("F")
2304 delfunction F
2305
2306 let g:jump = "continue"
2307 exec "source" scriptF
2308 Xpath 'a'
2309 let g:jump = "break"
2310 exec "source" scriptF
2311 Xpath 'b'
2312 let g:jump = "finish"
2313 exec "source" scriptF
2314 Xpath 'c'
2315 try
2316 let g:jump = "error"
2317 exec "source" scriptF
2318 Xpath 'd'
2319 finally
2320 Xpath 'e'
2321 try
2322 let g:jump = "interrupt"
2323 exec "source" scriptF
2324 Xpath 'f'
2325 finally
2326 Xpath 'g'
2327 try
2328 let g:jump = "throw"
2329 exec "source" scriptF
2330 Xpath 'h'
2331 finally
2332 Xpath 'i'
2333 endtry
2334 endtry
2335 endtry
2336 unlet g:jump
2337 call delete(scriptF)
2338 [CODE]
2339 let verify =<< trim [CODE]
2340 call assert_equal('abcdefghi', g:Xpath)
2341 [CODE]
2342 call RunInNewVim(test, verify)
2343 endfunc
2344
2345 "-------------------------------------------------------------------------------
2346 " Test 38: :finally reason discarded by an error {{{1
2347 "
2348 " When a :finally clause is executed due to a :continue, :break,
2349 " :return, :finish, error, interrupt or :throw, the jump reason is
2350 " discarded by an error in the finally clause.
2351 "-------------------------------------------------------------------------------
2352
2353 func Test_finally_discard_by_error()
2354 let test =<< trim [CODE]
2355 func E(jump)
2356 let loop = 0
2357 while loop < 2
2358 let loop = loop + 1
2359 if loop == 1
2360 try
2361 if a:jump == "continue"
2362 continue
2363 elseif a:jump == "break"
2364 break
2365 elseif a:jump == "return" || a:jump == "finish"
2366 return
2367 elseif a:jump == "error"
2368 asdf
2369 elseif a:jump == "interrupt"
2370 call interrupt()
2371 let dummy = 0
2372 elseif a:jump == "throw"
2373 throw "abc"
2374 endif
2375 finally
2376 asdf " error; discards jump that caused the :finally
2377 endtry
2378 elseif loop == 2
2379 call assert_report('should not get here')
2380 endif
2381 endwhile
2382 call assert_report('should not get here')
2383 endfunc
2384
2385 try
2386 Xpath 'a'
2387 call E("continue")
2388 call assert_report('should not get here')
2389 finally
2390 try
2391 Xpath 'b'
2392 call E("break")
2393 call assert_report('should not get here')
2394 finally
2395 try
2396 Xpath 'c'
2397 call E("return")
2398 call assert_report('should not get here')
2399 finally
2400 try
2401 Xpath 'd'
2402 let g:jump = "finish"
2403 ExecAsScript E
2404 call assert_report('should not get here')
2405 finally
2406 unlet g:jump
2407 try
2408 Xpath 'e'
2409 call E("error")
2410 call assert_report('should not get here')
2411 finally
2412 try
2413 Xpath 'f'
2414 call E("interrupt")
2415 call assert_report('should not get here')
2416 finally
2417 try
2418 Xpath 'g'
2419 call E("throw")
2420 call assert_report('should not get here')
2421 finally
2422 Xpath 'h'
2423 delfunction E
2424 endtry
2425 endtry
2426 endtry
2427 endtry
2428 endtry
2429 endtry
2430 endtry
2431 call assert_report('should not get here')
2432 [CODE]
2433 let verify =<< trim [CODE]
2434 call assert_equal('abcdefgh', g:Xpath)
2435 [CODE]
2436 call RunInNewVim(test, verify)
2437 endfunc
2438
2439 "-------------------------------------------------------------------------------
2440 " Test 39: :finally reason discarded by an interrupt {{{1
2441 "
2442 " When a :finally clause is executed due to a :continue, :break,
2443 " :return, :finish, error, interrupt or :throw, the jump reason is
2444 " discarded by an interrupt in the finally clause.
2445 "-------------------------------------------------------------------------------
2446
2447 func Test_finally_discarded_by_interrupt()
2448 let test =<< trim [CODE]
2449 func I(jump)
2450 let loop = 0
2451 while loop < 2
2452 let loop = loop + 1
2453 if loop == 1
2454 try
2455 if a:jump == "continue"
2456 continue
2457 elseif a:jump == "break"
2458 break
2459 elseif a:jump == "return" || a:jump == "finish"
2460 return
2461 elseif a:jump == "error"
2462 asdf
2463 elseif a:jump == "interrupt"
2464 call interrupt()
2465 let dummy = 0
2466 elseif a:jump == "throw"
2467 throw "abc"
2468 endif
2469 finally
2470 call interrupt()
2471 let dummy = 0
2472 endtry
2473 elseif loop == 2
2474 call assert_report('should not get here')
2475 endif
2476 endwhile
2477 call assert_report('should not get here')
2478 endfunc
2479
2480 try
2481 try
2482 Xpath 'a'
2483 call I("continue")
2484 call assert_report('should not get here')
2485 finally
2486 try
2487 Xpath 'b'
2488 call I("break")
2489 call assert_report('should not get here')
2490 finally
2491 try
2492 Xpath 'c'
2493 call I("return")
2494 call assert_report('should not get here')
2495 finally
2496 try
2497 Xpath 'd'
2498 let g:jump = "finish"
2499 ExecAsScript I
2500 call assert_report('should not get here')
2501 finally
2502 unlet g:jump
2503 try
2504 Xpath 'e'
2505 call I("error")
2506 call assert_report('should not get here')
2507 finally
2508 try
2509 Xpath 'f'
2510 call I("interrupt")
2511 call assert_report('should not get here')
2512 finally
2513 try
2514 Xpath 'g'
2515 call I("throw")
2516 call assert_report('should not get here')
2517 finally
2518 Xpath 'h'
2519 delfunction I
2520 endtry
2521 endtry
2522 endtry
2523 endtry
2524 endtry
2525 endtry
2526 endtry
2527 call assert_report('should not get here')
2528 catch /^Vim:Interrupt$/
2529 Xpath 'A'
2530 endtry
2531 [CODE]
2532 let verify =<< trim [CODE]
2533 call assert_equal('abcdefghA', g:Xpath)
2534 [CODE]
2535 call RunInNewVim(test, verify)
2536 endfunc
2537
2538 "-------------------------------------------------------------------------------
2539 " Test 40: :finally reason discarded by :throw {{{1
2540 "
2541 " When a :finally clause is executed due to a :continue, :break,
2542 " :return, :finish, error, interrupt or :throw, the jump reason is
2543 " discarded by a :throw in the finally clause.
2544 "-------------------------------------------------------------------------------
2545
2546 func Test_finally_discard_by_throw()
2547 let test =<< trim [CODE]
2548 func T(jump)
2549 let loop = 0
2550 while loop < 2
2551 let loop = loop + 1
2552 if loop == 1
2553 try
2554 if a:jump == "continue"
2555 continue
2556 elseif a:jump == "break"
2557 break
2558 elseif a:jump == "return" || a:jump == "finish"
2559 return
2560 elseif a:jump == "error"
2561 asdf
2562 elseif a:jump == "interrupt"
2563 call interrupt()
2564 let dummy = 0
2565 elseif a:jump == "throw"
2566 throw "abc"
2567 endif
2568 finally
2569 throw "xyz" " discards jump that caused the :finally
2570 endtry
2571 elseif loop == 2
2572 call assert_report('should not get here')
2573 endif
2574 endwhile
2575 call assert_report('should not get here')
2576 endfunc
2577
2578 try
2579 Xpath 'a'
2580 call T("continue")
2581 call assert_report('should not get here')
2582 finally
2583 try
2584 Xpath 'b'
2585 call T("break")
2586 call assert_report('should not get here')
2587 finally
2588 try
2589 Xpath 'c'
2590 call T("return")
2591 call assert_report('should not get here')
2592 finally
2593 try
2594 Xpath 'd'
2595 let g:jump = "finish"
2596 ExecAsScript T
2597 call assert_report('should not get here')
2598 finally
2599 unlet g:jump
2600 try
2601 Xpath 'e'
2602 call T("error")
2603 call assert_report('should not get here')
2604 finally
2605 try
2606 Xpath 'f'
2607 call T("interrupt")
2608 call assert_report('should not get here')
2609 finally
2610 try
2611 Xpath 'g'
2612 call T("throw")
2613 call assert_report('should not get here')
2614 finally
2615 Xpath 'h'
2616 delfunction T
2617 endtry
2618 endtry
2619 endtry
2620 endtry
2621 endtry
2622 endtry
2623 endtry
2624 call assert_report('should not get here')
2625 [CODE]
2626 let verify =<< trim [CODE]
2627 call assert_equal('abcdefgh', g:Xpath)
2628 [CODE]
2629 call RunInNewVim(test, verify)
2630 endfunc
2631
2632 "-------------------------------------------------------------------------------
2633 " Test 49: Throwing exceptions across functions {{{1
2634 "
2635 " When an exception is thrown but not caught inside a function, the
2636 " caller is checked for a matching :catch clause.
2637 "-------------------------------------------------------------------------------
2638
2639 func T49_C()
2640 try
2641 Xpath 'a'
2642 throw "arrgh"
2643 call assert_report('should not get here')
2644 catch /arrgh/
2645 Xpath 'b'
2646 endtry
2647 Xpath 'c'
2648 endfunc
2649
2650 func T49_T1()
2651 XloopNEXT
2652 try
2653 Xloop 'd'
2654 throw "arrgh"
2655 call assert_report('should not get here')
2656 finally
2657 Xloop 'e'
2658 endtry
2659 Xloop 'f'
2660 endfunc
2661
2662 func T49_T2()
2663 try
2664 Xpath 'g'
2665 call T49_T1()
2666 call assert_report('should not get here')
2667 finally
2668 Xpath 'h'
2669 endtry
2670 call assert_report('should not get here')
2671 endfunc
2672
2673 func Test_throw_exception_across_funcs()
2674 XpathINIT
2675 XloopINIT
2676 try
2677 Xpath 'i'
2678 call T49_C() " throw and catch
2679 Xpath 'j'
2680 catch /.*/
2681 call assert_report('should not get here')
2682 endtry
2683
2684 try
2685 Xpath 'k'
2686 call T49_T1() " throw, one level
2687 call assert_report('should not get here')
2688 catch /arrgh/
2689 Xpath 'l'
2690 catch /.*/
2691 call assert_report('should not get here')
2692 endtry
2693
2694 try
2695 Xpath 'm'
2696 call T49_T2() " throw, two levels
2697 call assert_report('should not get here')
2698 catch /arrgh/
2699 Xpath 'n'
2700 catch /.*/
2701 call assert_report('should not get here')
2702 endtry
2703 Xpath 'o'
2704
2705 call assert_equal('iabcjkd2e2lmgd3e3hno', g:Xpath)
2706 endfunc
2707
2708 "-------------------------------------------------------------------------------
2709 " Test 50: Throwing exceptions across script files {{{1
2710 "
2711 " When an exception is thrown but not caught inside a script file,
2712 " the sourcing script or function is checked for a matching :catch
2713 " clause.
2714 "
2715 " This test executes the bodies of the functions C, T1, and T2 from
2716 " the previous test as script files (:return replaced by :finish).
2717 "-------------------------------------------------------------------------------
2718
2719 func T50_F()
2720 try
2721 Xpath 'A'
2722 exec "source" g:scriptC
2723 Xpath 'B'
2724 catch /.*/
2725 call assert_report('should not get here')
2726 endtry
2727
2728 try
2729 Xpath 'C'
2730 exec "source" g:scriptT1
2731 call assert_report('should not get here')
2732 catch /arrgh/
2733 Xpath 'D'
2734 catch /.*/
2735 call assert_report('should not get here')
2736 endtry
2737 endfunc
2738
2739 func Test_throw_across_script()
2740 XpathINIT
2741 XloopINIT
2742 let g:scriptC = MakeScript("T49_C")
2743 let g:scriptT1 = MakeScript("T49_T1")
2744 let scriptT2 = MakeScript("T49_T2", g:scriptT1)
2745
2746 try
2747 Xpath 'E'
2748 call T50_F()
2749 Xpath 'F'
2750 exec "source" scriptT2
2751 call assert_report('should not get here')
2752 catch /arrgh/
2753 Xpath 'G'
2754 catch /.*/
2755 call assert_report('should not get here')
2756 endtry
2757 Xpath 'H'
2758 call assert_equal('EAabcBCd2e2DFgd3e3hGH', g:Xpath)
2759
2760 call delete(g:scriptC)
2761 call delete(g:scriptT1)
2762 call delete(scriptT2)
2763 unlet g:scriptC g:scriptT1 scriptT2
2764 endfunc
2765
1096 "------------------------------------------------------------------------------- 2766 "-------------------------------------------------------------------------------
1097 " Test 87 using (expr) ? funcref : funcref {{{1 2767 " Test 87 using (expr) ? funcref : funcref {{{1
1098 " 2768 "
1099 " Vim needs to correctly parse the funcref and even when it does 2769 " Vim needs to correctly parse the funcref and even when it does
1100 " not execute the funcref, it needs to consume the trailing () 2770 " not execute the funcref, it needs to consume the trailing ()