blob: 439114d19b3b3182d1aeed659d27fff3fc970ab6 [file] [log] [blame]
Bram Moolenaar5d7ead32018-02-27 17:17:42 +01001" Test various aspects of the Vim script language.
Bram Moolenaar4119cf82016-01-17 14:59:01 +01002" Most of this was formerly in test49.
Bram Moolenaarf49e2402015-12-30 15:59:25 +01003
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaar93344c22019-08-14 21:12:05 +02005source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02006
Bram Moolenaarf49e2402015-12-30 15:59:25 +01007"-------------------------------------------------------------------------------
8" Test environment {{{1
9"-------------------------------------------------------------------------------
10
Bram Moolenaar1e115362019-01-09 23:01:02 +010011com! XpathINIT let g:Xpath = ''
Bram Moolenaarf49e2402015-12-30 15:59:25 +010012com! -nargs=1 -bar Xpath let g:Xpath = g:Xpath . <args>
13
14" Append a message to the "messages" file
Bram Moolenaar1e115362019-01-09 23:01:02 +010015func Xout(text)
Bram Moolenaarf49e2402015-12-30 15:59:25 +010016 split messages
17 $put =a:text
18 wq
19endfunc
20
21com! -nargs=1 Xout call Xout(<args>)
22
23" MakeScript() - Make a script file from a function. {{{2
24"
25" Create a script that consists of the body of the function a:funcname.
26" Replace any ":return" by a ":finish", any argument variable by a global
Bram Moolenaare21c1582019-03-02 11:57:09 +010027" variable, and every ":call" by a ":source" for the next following argument
Bram Moolenaarf49e2402015-12-30 15:59:25 +010028" in the variable argument list. This function is useful if similar tests are
29" to be made for a ":return" from a function call or a ":finish" in a script
30" file.
Bram Moolenaar1e115362019-01-09 23:01:02 +010031func MakeScript(funcname, ...)
Bram Moolenaarf49e2402015-12-30 15:59:25 +010032 let script = tempname()
33 execute "redir! >" . script
34 execute "function" a:funcname
35 redir END
36 execute "edit" script
37 " Delete the "function" and the "endfunction" lines. Do not include the
38 " word "function" in the pattern since it might be translated if LANG is
39 " set. When MakeScript() is being debugged, this deletes also the debugging
40 " output of its line 3 and 4.
41 exec '1,/.*' . a:funcname . '(.*)/d'
42 /^\d*\s*endfunction\>/,$d
43 %s/^\d*//e
44 %s/return/finish/e
45 %s/\<a:\(\h\w*\)/g:\1/ge
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
Bram Moolenaar1e115362019-01-09 23:01:02 +010056endfunc
Bram Moolenaarf49e2402015-12-30 15:59:25 +010057
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
Bram Moolenaarf4f79b82016-01-25 20:38:30 +010061" delete it afterwards. However, if an exception is thrown the file may remain,
62" the caller should call DeleteTheScript() afterwards.
63let s:script_name = ''
Bram Moolenaarf49e2402015-12-30 15:59:25 +010064function! ExecAsScript(funcname)
65 " Make a script from the function passed as argument.
Bram Moolenaarf4f79b82016-01-25 20:38:30 +010066 let s:script_name = MakeScript(a:funcname)
Bram Moolenaarf49e2402015-12-30 15:59:25 +010067
68 " Source and delete the script.
Bram Moolenaarf4f79b82016-01-25 20:38:30 +010069 exec "source" s:script_name
70 call delete(s:script_name)
71 let s:script_name = ''
Bram Moolenaarf49e2402015-12-30 15:59:25 +010072endfunction
73
Bram Moolenaarf4f79b82016-01-25 20:38:30 +010074function! DeleteTheScript()
75 if s:script_name
76 call delete(s:script_name)
77 let s:script_name = ''
78 endif
79endfunc
80
Bram Moolenaarf49e2402015-12-30 15:59:25 +010081com! -nargs=1 -bar ExecAsScript call ExecAsScript(<f-args>)
82
83
84"-------------------------------------------------------------------------------
85" Test 1: :endwhile in function {{{1
86"
87" Detect if a broken loop is (incorrectly) reactivated by the
88" :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
90" tests will hang.
91"-------------------------------------------------------------------------------
92
93function! T1_F()
94 Xpath 'a'
95 let first = 1
96 while 1
97 Xpath 'b'
98 if first
99 Xpath 'c'
100 let first = 0
101 break
102 else
103 Xpath 'd'
104 return
105 endif
106 endwhile
107endfunction
108
109function! T1_G()
110 Xpath 'h'
111 let first = 1
112 while 1
113 Xpath 'i'
114 if first
115 Xpath 'j'
116 let first = 0
117 break
118 else
119 Xpath 'k'
120 return
121 endif
122 if 1 " unmatched :if
123 endwhile
124endfunction
125
126func Test_endwhile_function()
127 XpathINIT
128 call T1_F()
129 Xpath 'F'
130
131 try
132 call T1_G()
133 catch
134 " Catch missing :endif
135 call assert_true(v:exception =~ 'E171')
136 Xpath 'x'
137 endtry
138 Xpath 'G'
139
140 call assert_equal('abcFhijxG', g:Xpath)
141endfunc
142
143"-------------------------------------------------------------------------------
144" Test 2: :endwhile in script {{{1
145"
146" Detect if a broken loop is (incorrectly) reactivated by the
147" :endwhile. Use a :finish to prevent an endless loop, and place
148" this test before others that might hang to get a meaningful result
149" on an error.
150"
151" This test executes the bodies of the functions T1_F and T1_G from
152" the previous test as script files (:return replaced by :finish).
153"-------------------------------------------------------------------------------
154
155func Test_endwhile_script()
156 XpathINIT
157 ExecAsScript T1_F
158 Xpath 'F'
Bram Moolenaarf4f79b82016-01-25 20:38:30 +0100159 call DeleteTheScript()
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100160
161 try
162 ExecAsScript T1_G
163 catch
164 " Catch missing :endif
165 call assert_true(v:exception =~ 'E171')
166 Xpath 'x'
167 endtry
168 Xpath 'G'
Bram Moolenaarf4f79b82016-01-25 20:38:30 +0100169 call DeleteTheScript()
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100170
171 call assert_equal('abcFhijxG', g:Xpath)
172endfunc
173
174"-------------------------------------------------------------------------------
175" Test 3: :if, :elseif, :while, :continue, :break {{{1
176"-------------------------------------------------------------------------------
177
178function Test_if_while()
179 XpathINIT
180 if 1
181 Xpath 'a'
182 let loops = 3
183 while loops > -1 " main loop: loops == 3, 2, 1 (which breaks)
184 if loops <= 0
185 let break_err = 1
186 let loops = -1
187 else
188 Xpath 'b' . loops
189 endif
190 if (loops == 2)
191 while loops == 2 " dummy loop
192 Xpath 'c' . loops
193 let loops = loops - 1
194 continue " stop dummy loop
195 Xpath 'd' . loops
196 endwhile
197 continue " continue main loop
198 Xpath 'e' . loops
199 elseif (loops == 1)
200 let p = 1
201 while p " dummy loop
202 Xpath 'f' . loops
203 let p = 0
204 break " break dummy loop
205 Xpath 'g' . loops
206 endwhile
207 Xpath 'h' . loops
208 unlet p
209 break " break main loop
210 Xpath 'i' . loops
211 endif
212 if (loops > 0)
213 Xpath 'j' . loops
214 endif
215 while loops == 3 " dummy loop
216 let loops = loops - 1
217 endwhile " end dummy loop
218 endwhile " end main loop
219 Xpath 'k'
220 else
221 Xpath 'l'
222 endif
223 Xpath 'm'
224 if exists("break_err")
225 Xpath 'm'
226 unlet break_err
227 endif
228
229 unlet loops
230
231 call assert_equal('ab3j3b2c2b1f1h1km', g:Xpath)
232endfunc
233
234"-------------------------------------------------------------------------------
235" Test 4: :return {{{1
236"-------------------------------------------------------------------------------
237
238function! T4_F()
239 if 1
240 Xpath 'a'
241 let loops = 3
242 while loops > 0 " 3: 2: 1:
243 Xpath 'b' . loops
244 if (loops == 2)
245 Xpath 'c' . loops
246 return
247 Xpath 'd' . loops
248 endif
249 Xpath 'e' . loops
250 let loops = loops - 1
251 endwhile
252 Xpath 'f'
253 else
254 Xpath 'g'
255 endif
256endfunction
257
258function Test_return()
259 XpathINIT
260 call T4_F()
261 Xpath '4'
262
263 call assert_equal('ab3e3b2c24', g:Xpath)
264endfunction
265
266
267"-------------------------------------------------------------------------------
268" Test 5: :finish {{{1
269"
270" This test executes the body of the function T4_F from the previous
271" test as a script file (:return replaced by :finish).
272"-------------------------------------------------------------------------------
273
274function Test_finish()
275 XpathINIT
276 ExecAsScript T4_F
277 Xpath '5'
Bram Moolenaarf4f79b82016-01-25 20:38:30 +0100278 call DeleteTheScript()
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100279
280 call assert_equal('ab3e3b2c25', g:Xpath)
281endfunction
282
283
284
285"-------------------------------------------------------------------------------
286" Test 6: Defining functions in :while loops {{{1
287"
288" Functions can be defined inside other functions. An inner function
289" gets defined when the outer function is executed. Functions may
290" also be defined inside while loops. Expressions in braces for
291" defining the function name are allowed.
292"
293" The functions are defined when sourcing the script, only the
294" resulting path is checked in the test function.
295"-------------------------------------------------------------------------------
296
297XpathINIT
298
299" The command CALL collects the argument of all its invocations in "calls"
300" when used from a function (that is, when the global variable "calls" needs
301" the "g:" prefix). This is to check that the function code is skipped when
302" the function is defined. For inner functions, do so only if the outer
303" function is not being executed.
304"
305let calls = ""
306com! -nargs=1 CALL
Bram Moolenaar1e115362019-01-09 23:01:02 +0100307 \ if !exists("calls") && !exists("outer") |
308 \ let g:calls = g:calls . <args> |
309 \ endif
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100310
311let i = 0
312while i < 3
313 let i = i + 1
314 if i == 1
315 Xpath 'a'
316 function! F1(arg)
317 CALL a:arg
318 let outer = 1
319
320 let j = 0
321 while j < 1
322 Xpath 'b'
323 let j = j + 1
324 function! G1(arg)
325 CALL a:arg
326 endfunction
327 Xpath 'c'
328 endwhile
329 endfunction
330 Xpath 'd'
331
332 continue
333 endif
334
335 Xpath 'e' . i
336 function! F{i}(i, arg)
337 CALL a:arg
338 let outer = 1
339
340 if a:i == 3
341 Xpath 'f'
342 endif
343 let k = 0
344 while k < 3
345 Xpath 'g' . k
346 let k = k + 1
347 function! G{a:i}{k}(arg)
348 CALL a:arg
349 endfunction
350 Xpath 'h' . k
351 endwhile
352 endfunction
353 Xpath 'i'
354
355endwhile
356
357if exists("*G1")
358 Xpath 'j'
359endif
360if exists("*F1")
361 call F1("F1")
362 if exists("*G1")
Bram Moolenaar1e115362019-01-09 23:01:02 +0100363 call G1("G1")
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100364 endif
365endif
366
367if exists("G21") || exists("G22") || exists("G23")
368 Xpath 'k'
369endif
370if exists("*F2")
371 call F2(2, "F2")
372 if exists("*G21")
Bram Moolenaar1e115362019-01-09 23:01:02 +0100373 call G21("G21")
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100374 endif
375 if exists("*G22")
Bram Moolenaar1e115362019-01-09 23:01:02 +0100376 call G22("G22")
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100377 endif
378 if exists("*G23")
Bram Moolenaar1e115362019-01-09 23:01:02 +0100379 call G23("G23")
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100380 endif
381endif
382
383if exists("G31") || exists("G32") || exists("G33")
384 Xpath 'l'
385endif
386if exists("*F3")
387 call F3(3, "F3")
388 if exists("*G31")
Bram Moolenaar1e115362019-01-09 23:01:02 +0100389 call G31("G31")
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100390 endif
391 if exists("*G32")
Bram Moolenaar1e115362019-01-09 23:01:02 +0100392 call G32("G32")
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100393 endif
394 if exists("*G33")
Bram Moolenaar1e115362019-01-09 23:01:02 +0100395 call G33("G33")
Bram Moolenaarf49e2402015-12-30 15:59:25 +0100396 endif
397endif
398
399Xpath 'm'
400
401let g:test6_result = g:Xpath
402let g:test6_calls = calls
403
404unlet calls
405delfunction F1
406delfunction G1
407delfunction F2
408delfunction G21
409delfunction G22
410delfunction G23
411delfunction G31
412delfunction G32
413delfunction G33
414
415function Test_defining_functions()
416 call assert_equal('ade2ie3ibcg0h1g1h2g2h3fg0h1g1h2g2h3m', g:test6_result)
417 call assert_equal('F1G1F2G21G22G23F3G31G32G33', g:test6_calls)
418endfunc
419
420"-------------------------------------------------------------------------------
Bram Moolenaara2cce862016-01-02 19:50:04 +0100421" Test 7: Continuing on errors outside functions {{{1
422"
423" On an error outside a function, the script processing continues
424" at the line following the outermost :endif or :endwhile. When not
425" inside an :if or :while, the script processing continues at the next
426" line.
427"-------------------------------------------------------------------------------
428
429XpathINIT
430
431if 1
432 Xpath 'a'
433 while 1
434 Xpath 'b'
435 asdf
436 Xpath 'c'
437 break
438 endwhile | Xpath 'd'
439 Xpath 'e'
440endif | Xpath 'f'
441Xpath 'g'
442
443while 1
444 Xpath 'h'
445 if 1
446 Xpath 'i'
447 asdf
448 Xpath 'j'
449 endif | Xpath 'k'
450 Xpath 'l'
451 break
452endwhile | Xpath 'm'
453Xpath 'n'
454
455asdf
456Xpath 'o'
457
458asdf | Xpath 'p'
459Xpath 'q'
460
461let g:test7_result = g:Xpath
462
463func Test_error_in_script()
464 call assert_equal('abghinoq', g:test7_result)
465endfunc
466
467"-------------------------------------------------------------------------------
468" Test 8: Aborting and continuing on errors inside functions {{{1
469"
470" On an error inside a function without the "abort" attribute, the
471" script processing continues at the next line (unless the error was
472" in a :return command). On an error inside a function with the
473" "abort" attribute, the function is aborted and the script processing
474" continues after the function call; the value -1 is returned then.
475"-------------------------------------------------------------------------------
476
477XpathINIT
478
479function! T8_F()
480 if 1
481 Xpath 'a'
482 while 1
483 Xpath 'b'
484 asdf
485 Xpath 'c'
486 asdf | Xpath 'd'
487 Xpath 'e'
488 break
489 endwhile
490 Xpath 'f'
491 endif | Xpath 'g'
492 Xpath 'h'
493
494 while 1
495 Xpath 'i'
496 if 1
497 Xpath 'j'
498 asdf
499 Xpath 'k'
500 asdf | Xpath 'l'
501 Xpath 'm'
502 endif
503 Xpath 'n'
504 break
505 endwhile | Xpath 'o'
506 Xpath 'p'
507
508 return novar " returns (default return value 0)
509 Xpath 'q'
510 return 1 " not reached
511endfunction
512
513function! T8_G() abort
514 if 1
515 Xpath 'r'
516 while 1
517 Xpath 's'
518 asdf " returns -1
519 Xpath 't'
520 break
521 endwhile
522 Xpath 'v'
523 endif | Xpath 'w'
524 Xpath 'x'
525
526 return -4 " not reached
527endfunction
528
529function! T8_H() abort
530 while 1
531 Xpath 'A'
532 if 1
533 Xpath 'B'
534 asdf " returns -1
535 Xpath 'C'
536 endif
537 Xpath 'D'
538 break
539 endwhile | Xpath 'E'
540 Xpath 'F'
541
542 return -4 " not reached
543endfunction
544
545" Aborted functions (T8_G and T8_H) return -1.
546let g:test8_sum = (T8_F() + 1) - 4 * T8_G() - 8 * T8_H()
547Xpath 'X'
548let g:test8_result = g:Xpath
549
550func Test_error_in_function()
551 call assert_equal(13, g:test8_sum)
552 call assert_equal('abcefghijkmnoprsABX', g:test8_result)
553
554 delfunction T8_F
555 delfunction T8_G
556 delfunction T8_H
557endfunc
558
559
560"-------------------------------------------------------------------------------
561" Test 9: Continuing after aborted functions {{{1
562"
563" When a function with the "abort" attribute is aborted due to an
564" error, the next function back in the call hierarchy without an
565" "abort" attribute continues; the value -1 is returned then.
566"-------------------------------------------------------------------------------
567
568XpathINIT
569
570function! F() abort
571 Xpath 'a'
572 let result = G() " not aborted
573 Xpath 'b'
574 if result != 2
575 Xpath 'c'
576 endif
577 return 1
578endfunction
579
580function! G() " no abort attribute
581 Xpath 'd'
582 if H() != -1 " aborted
583 Xpath 'e'
584 endif
585 Xpath 'f'
586 return 2
587endfunction
588
589function! H() abort
590 Xpath 'g'
591 call I() " aborted
592 Xpath 'h'
593 return 4
594endfunction
595
596function! I() abort
597 Xpath 'i'
598 asdf " error
599 Xpath 'j'
600 return 8
601endfunction
602
603if F() != 1
604 Xpath 'k'
605endif
606
607let g:test9_result = g:Xpath
608
609delfunction F
610delfunction G
611delfunction H
612delfunction I
613
614func Test_func_abort()
615 call assert_equal('adgifb', g:test9_result)
616endfunc
617
618
619"-------------------------------------------------------------------------------
620" Test 10: :if, :elseif, :while argument parsing {{{1
621"
622" A '"' or '|' in an argument expression must not be mixed up with
623" a comment or a next command after a bar. Parsing errors should
624" be recognized.
625"-------------------------------------------------------------------------------
626
627XpathINIT
628
629function! MSG(enr, emsg)
630 let english = v:lang == "C" || v:lang =~ '^[Ee]n'
631 if a:enr == ""
632 Xout "TODO: Add message number for:" a:emsg
633 let v:errmsg = ":" . v:errmsg
634 endif
635 let match = 1
636 if v:errmsg !~ '^'.a:enr.':' || (english && v:errmsg !~ a:emsg)
637 let match = 0
638 if v:errmsg == ""
639 Xout "Message missing."
640 else
Bram Moolenaara4208962019-08-24 20:50:19 +0200641 let v:errmsg = v:errmsg->escape('"')
Bram Moolenaara2cce862016-01-02 19:50:04 +0100642 Xout "Unexpected message:" v:errmsg
643 endif
644 endif
645 return match
Bram Moolenaar1e115362019-01-09 23:01:02 +0100646endfunc
Bram Moolenaara2cce862016-01-02 19:50:04 +0100647
648if 1 || strlen("\"") | Xpath 'a'
649 Xpath 'b'
650endif
651Xpath 'c'
652
653if 0
654elseif 1 || strlen("\"") | Xpath 'd'
655 Xpath 'e'
656endif
657Xpath 'f'
658
659while 1 || strlen("\"") | Xpath 'g'
660 Xpath 'h'
661 break
662endwhile
663Xpath 'i'
664
665let v:errmsg = ""
666if 1 ||| strlen("\"") | Xpath 'j'
667 Xpath 'k'
668endif
669Xpath 'l'
670if !MSG('E15', "Invalid expression")
671 Xpath 'm'
672endif
673
674let v:errmsg = ""
675if 0
676elseif 1 ||| strlen("\"") | Xpath 'n'
677 Xpath 'o'
678endif
679Xpath 'p'
680if !MSG('E15', "Invalid expression")
681 Xpath 'q'
682endif
683
684let v:errmsg = ""
685while 1 ||| strlen("\"") | Xpath 'r'
686 Xpath 's'
687 break
688endwhile
689Xpath 't'
690if !MSG('E15', "Invalid expression")
691 Xpath 'u'
692endif
693
694let g:test10_result = g:Xpath
695delfunction MSG
696
697func Test_expr_parsing()
698 call assert_equal('abcdefghilpt', g:test10_result)
699endfunc
700
701
702"-------------------------------------------------------------------------------
703" Test 11: :if, :elseif, :while argument evaluation after abort {{{1
704"
705" When code is skipped over due to an error, the boolean argument to
706" an :if, :elseif, or :while must not be evaluated.
707"-------------------------------------------------------------------------------
708
709XpathINIT
710
711let calls = 0
712
713function! P(num)
714 let g:calls = g:calls + a:num " side effect on call
715 return 0
716endfunction
717
718if 1
719 Xpath 'a'
720 asdf " error
721 Xpath 'b'
722 if P(1) " should not be called
723 Xpath 'c'
724 elseif !P(2) " should not be called
725 Xpath 'd'
726 else
727 Xpath 'e'
728 endif
729 Xpath 'f'
730 while P(4) " should not be called
731 Xpath 'g'
732 endwhile
733 Xpath 'h'
734endif
735Xpath 'x'
736
737let g:test11_calls = calls
738let g:test11_result = g:Xpath
739
740unlet calls
741delfunction P
742
743func Test_arg_abort()
744 call assert_equal(0, g:test11_calls)
745 call assert_equal('ax', g:test11_result)
746endfunc
747
748
749"-------------------------------------------------------------------------------
750" Test 12: Expressions in braces in skipped code {{{1
751"
752" In code skipped over due to an error or inactive conditional,
753" an expression in braces as part of a variable or function name
754" should not be evaluated.
755"-------------------------------------------------------------------------------
756
757XpathINIT
758
759function! NULL()
760 Xpath 'a'
761 return 0
762endfunction
763
764function! ZERO()
765 Xpath 'b'
766 return 0
767endfunction
768
769function! F0()
770 Xpath 'c'
771endfunction
772
773function! F1(arg)
774 Xpath 'e'
775endfunction
776
777let V0 = 1
778
779Xpath 'f'
780echo 0 ? F{NULL() + V{ZERO()}}() : 1
781
782Xpath 'g'
783if 0
784 Xpath 'h'
785 call F{NULL() + V{ZERO()}}()
786endif
787
788Xpath 'i'
789if 1
790 asdf " error
791 Xpath 'j'
792 call F1(F{NULL() + V{ZERO()}}())
793endif
794
795Xpath 'k'
796if 1
797 asdf " error
798 Xpath 'l'
799 call F{NULL() + V{ZERO()}}()
800endif
801
802let g:test12_result = g:Xpath
803
804func Test_braces_skipped()
805 call assert_equal('fgik', g:test12_result)
806endfunc
807
808
809"-------------------------------------------------------------------------------
810" Test 13: Failure in argument evaluation for :while {{{1
811"
812" A failure in the expression evaluation for the condition of a :while
813" causes the whole :while loop until the matching :endwhile being
814" ignored. Continuation is at the next following line.
815"-------------------------------------------------------------------------------
816
817XpathINIT
818
819Xpath 'a'
820while asdf
821 Xpath 'b'
822 while 1
823 Xpath 'c'
824 break
825 endwhile
826 Xpath 'd'
827 break
828endwhile
829Xpath 'e'
830
831while asdf | Xpath 'f' | endwhile | Xpath 'g'
832Xpath 'h'
833let g:test13_result = g:Xpath
834
835func Test_while_fail()
836 call assert_equal('aeh', g:test13_result)
837endfunc
838
839
840"-------------------------------------------------------------------------------
841" Test 14: Failure in argument evaluation for :if {{{1
842"
843" A failure in the expression evaluation for the condition of an :if
844" does not cause the corresponding :else or :endif being matched to
845" a previous :if/:elseif. Neither of both branches of the failed :if
846" are executed.
847"-------------------------------------------------------------------------------
848
849XpathINIT
850
851function! F()
852 Xpath 'a'
853 let x = 0
854 if x " false
855 Xpath 'b'
856 elseif !x " always true
857 Xpath 'c'
858 let x = 1
859 if g:boolvar " possibly undefined
860 Xpath 'd'
861 else
862 Xpath 'e'
863 endif
864 Xpath 'f'
865 elseif x " never executed
866 Xpath 'g'
867 endif
868 Xpath 'h'
869endfunction
870
871let boolvar = 1
872call F()
873Xpath '-'
874
875unlet boolvar
876call F()
877let g:test14_result = g:Xpath
878
879delfunction F
880
881func Test_if_fail()
882 call assert_equal('acdfh-acfh', g:test14_result)
883endfunc
884
885
886"-------------------------------------------------------------------------------
887" Test 15: Failure in argument evaluation for :if (bar) {{{1
888"
889" Like previous test, except that the failing :if ... | ... | :endif
890" is in a single line.
891"-------------------------------------------------------------------------------
892
893XpathINIT
894
895function! F()
896 Xpath 'a'
897 let x = 0
898 if x " false
899 Xpath 'b'
900 elseif !x " always true
901 Xpath 'c'
902 let x = 1
903 if g:boolvar | Xpath 'd' | else | Xpath 'e' | endif
904 Xpath 'f'
905 elseif x " never executed
906 Xpath 'g'
907 endif
908 Xpath 'h'
909endfunction
910
911let boolvar = 1
912call F()
913Xpath '-'
914
915unlet boolvar
916call F()
917let g:test15_result = g:Xpath
918
919delfunction F
920
921func Test_if_bar_fail()
922 call assert_equal('acdfh-acfh', g:test15_result)
923endfunc
924
Bram Moolenaar4119cf82016-01-17 14:59:01 +0100925"-------------------------------------------------------------------------------
Bram Moolenaar1f068232019-11-03 16:17:26 +0100926" Test 16: Double :else or :elseif after :else {{{1
927"
928" Multiple :elses or an :elseif after an :else are forbidden.
929"-------------------------------------------------------------------------------
930
931func T16_F() abort
932 if 0
933 Xpath 'a'
934 else
935 Xpath 'b'
936 else " aborts function
937 Xpath 'c'
938 endif
939 Xpath 'd'
940endfunc
941
942func T16_G() abort
943 if 0
944 Xpath 'a'
945 else
946 Xpath 'b'
947 elseif 1 " aborts function
948 Xpath 'c'
949 else
950 Xpath 'd'
951 endif
952 Xpath 'e'
953endfunc
954
955func T16_H() abort
956 if 0
957 Xpath 'a'
958 elseif 0
959 Xpath 'b'
960 else
961 Xpath 'c'
962 else " aborts function
963 Xpath 'd'
964 endif
965 Xpath 'e'
966endfunc
967
968func T16_I() abort
969 if 0
970 Xpath 'a'
971 elseif 0
972 Xpath 'b'
973 else
974 Xpath 'c'
975 elseif 1 " aborts function
976 Xpath 'd'
977 else
978 Xpath 'e'
979 endif
980 Xpath 'f'
981endfunc
982
983func Test_Multi_Else()
984 XpathINIT
985 try
986 call T16_F()
987 catch /E583:/
988 Xpath 'e'
989 endtry
990 call assert_equal('be', g:Xpath)
991
992 XpathINIT
993 try
994 call T16_G()
995 catch /E584:/
996 Xpath 'f'
997 endtry
998 call assert_equal('bf', g:Xpath)
999
1000 XpathINIT
1001 try
1002 call T16_H()
1003 catch /E583:/
1004 Xpath 'f'
1005 endtry
1006 call assert_equal('cf', g:Xpath)
1007
1008 XpathINIT
1009 try
1010 call T16_I()
1011 catch /E584:/
1012 Xpath 'g'
1013 endtry
1014 call assert_equal('cg', g:Xpath)
1015endfunc
1016
1017"-------------------------------------------------------------------------------
1018" Test 17: Nesting of unmatched :if or :endif inside a :while {{{1
1019"
1020" The :while/:endwhile takes precedence in nesting over an unclosed
1021" :if or an unopened :endif.
1022"-------------------------------------------------------------------------------
1023
1024" While loops inside a function are continued on error.
1025func T17_F()
1026 let loops = 3
1027 while loops > 0
1028 let loops -= 1
1029 Xpath 'a' . loops
1030 if (loops == 1)
1031 Xpath 'b' . loops
1032 continue
1033 elseif (loops == 0)
1034 Xpath 'c' . loops
1035 break
1036 elseif 1
1037 Xpath 'd' . loops
1038 " endif missing!
1039 endwhile " :endwhile after :if 1
1040 Xpath 'e'
1041endfunc
1042
1043func T17_G()
1044 let loops = 2
1045 while loops > 0
1046 let loops -= 1
1047 Xpath 'a' . loops
1048 if 0
1049 Xpath 'b' . loops
1050 " endif missing
1051 endwhile " :endwhile after :if 0
1052endfunc
1053
1054func T17_H()
1055 let loops = 2
1056 while loops > 0
1057 let loops -= 1
1058 Xpath 'a' . loops
1059 " if missing!
1060 endif " :endif without :if in while
1061 Xpath 'b' . loops
1062 endwhile
1063endfunc
1064
1065" Error continuation outside a function is at the outermost :endwhile or :endif.
1066XpathINIT
1067let v:errmsg = ''
1068let loops = 2
1069while loops > 0
1070 let loops -= 1
1071 Xpath 'a' . loops
1072 if 0
1073 Xpath 'b' . loops
1074 " endif missing! Following :endwhile fails.
1075endwhile | Xpath 'c'
1076Xpath 'd'
1077call assert_match('E171:', v:errmsg)
1078call assert_equal('a1d', g:Xpath)
1079
1080func Test_unmatched_if_in_while()
1081 XpathINIT
1082 call assert_fails('call T17_F()', 'E171:')
1083 call assert_equal('a2d2a1b1a0c0e', g:Xpath)
1084
1085 XpathINIT
1086 call assert_fails('call T17_G()', 'E171:')
1087 call assert_equal('a1a0', g:Xpath)
1088
1089 XpathINIT
1090 call assert_fails('call T17_H()', 'E580:')
1091 call assert_equal('a1b1a0b0', g:Xpath)
1092endfunc
1093
1094"-------------------------------------------------------------------------------
1095"-------------------------------------------------------------------------------
1096"-------------------------------------------------------------------------------
1097" Test 87 using (expr) ? funcref : funcref {{{1
1098"
1099" Vim needs to correctly parse the funcref and even when it does
1100" not execute the funcref, it needs to consume the trailing ()
1101"-------------------------------------------------------------------------------
1102
1103func Add2(x1, x2)
1104 return a:x1 + a:x2
1105endfu
1106
1107func GetStr()
1108 return "abcdefghijklmnopqrstuvwxyp"
1109endfu
1110
1111func Test_funcref_with_condexpr()
1112 call assert_equal(5, function('Add2')(2,3))
1113
1114 call assert_equal(3, 1 ? function('Add2')(1,2) : function('Add2')(2,3))
1115 call assert_equal(5, 0 ? function('Add2')(1,2) : function('Add2')(2,3))
1116 " Make sure, GetStr() still works.
1117 call assert_equal('abcdefghijk', GetStr()[0:10])
1118endfunc
1119
Bram Moolenaar4119cf82016-01-17 14:59:01 +01001120" Test 90: Recognizing {} in variable name. {{{1
1121"-------------------------------------------------------------------------------
1122
1123func Test_curlies()
1124 let s:var = 66
1125 let ns = 's'
1126 call assert_equal(66, {ns}:var)
1127
1128 let g:a = {}
1129 let g:b = 't'
1130 let g:a[g:b] = 77
1131 call assert_equal(77, g:a['t'])
1132endfunc
Bram Moolenaara2cce862016-01-02 19:50:04 +01001133
1134"-------------------------------------------------------------------------------
Bram Moolenaarf95534c2016-01-23 21:59:52 +01001135" Test 91: using type(). {{{1
1136"-------------------------------------------------------------------------------
1137
1138func Test_type()
1139 call assert_equal(0, type(0))
1140 call assert_equal(1, type(""))
1141 call assert_equal(2, type(function("tr")))
Bram Moolenaar953cc7f2016-03-19 18:52:29 +01001142 call assert_equal(2, type(function("tr", [8])))
Bram Moolenaarf95534c2016-01-23 21:59:52 +01001143 call assert_equal(3, type([]))
1144 call assert_equal(4, type({}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001145 if has('float')
1146 call assert_equal(5, type(0.0))
1147 endif
Bram Moolenaarf95534c2016-01-23 21:59:52 +01001148 call assert_equal(6, type(v:false))
1149 call assert_equal(6, type(v:true))
1150 call assert_equal(7, type(v:none))
1151 call assert_equal(7, type(v:null))
Bram Moolenaarf562e722016-07-19 17:25:25 +02001152 call assert_equal(8, v:t_job)
1153 call assert_equal(9, v:t_channel)
1154 call assert_equal(v:t_number, type(0))
1155 call assert_equal(v:t_string, type(""))
1156 call assert_equal(v:t_func, type(function("tr")))
1157 call assert_equal(v:t_func, type(function("tr", [8])))
1158 call assert_equal(v:t_list, type([]))
1159 call assert_equal(v:t_dict, type({}))
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001160 if has('float')
1161 call assert_equal(v:t_float, type(0.0))
1162 endif
Bram Moolenaarf562e722016-07-19 17:25:25 +02001163 call assert_equal(v:t_bool, type(v:false))
1164 call assert_equal(v:t_bool, type(v:true))
1165 call assert_equal(v:t_none, type(v:none))
1166 call assert_equal(v:t_none, type(v:null))
1167
Bram Moolenaar7c215c52020-02-29 13:43:27 +01001168 call assert_fails("call type(test_void())", 'E685:')
1169 call assert_fails("call type(test_unknown())", 'E685:')
Bram Moolenaar17a13432016-01-24 14:22:10 +01001170
1171 call assert_equal(0, 0 + v:false)
1172 call assert_equal(1, 0 + v:true)
1173 call assert_equal(0, 0 + v:none)
1174 call assert_equal(0, 0 + v:null)
1175
Bram Moolenaarf48aa162016-01-24 17:54:24 +01001176 call assert_equal('v:false', '' . v:false)
1177 call assert_equal('v:true', '' . v:true)
1178 call assert_equal('v:none', '' . v:none)
1179 call assert_equal('v:null', '' . v:null)
Bram Moolenaar6039c7f2016-01-24 15:05:32 +01001180
1181 call assert_true(v:false == 0)
1182 call assert_false(v:false != 0)
1183 call assert_true(v:true == 1)
1184 call assert_false(v:true != 1)
1185 call assert_false(v:true == v:false)
1186 call assert_true(v:true != v:false)
1187
1188 call assert_true(v:null == 0)
1189 call assert_false(v:null != 0)
1190 call assert_true(v:none == 0)
1191 call assert_false(v:none != 0)
Bram Moolenaar04369222016-01-24 17:21:29 +01001192
1193 call assert_true(v:false is v:false)
1194 call assert_true(v:true is v:true)
1195 call assert_true(v:none is v:none)
1196 call assert_true(v:null is v:null)
1197
1198 call assert_false(v:false isnot v:false)
1199 call assert_false(v:true isnot v:true)
1200 call assert_false(v:none isnot v:none)
1201 call assert_false(v:null isnot v:null)
1202
1203 call assert_false(v:false is 0)
1204 call assert_false(v:true is 1)
1205 call assert_false(v:true is v:false)
1206 call assert_false(v:none is 0)
1207 call assert_false(v:null is 0)
1208 call assert_false(v:null is v:none)
1209
1210 call assert_true(v:false isnot 0)
1211 call assert_true(v:true isnot 1)
1212 call assert_true(v:true isnot v:false)
1213 call assert_true(v:none isnot 0)
1214 call assert_true(v:null isnot 0)
1215 call assert_true(v:null isnot v:none)
Bram Moolenaar65591002016-01-24 21:51:57 +01001216
1217 call assert_equal(v:false, eval(string(v:false)))
1218 call assert_equal(v:true, eval(string(v:true)))
1219 call assert_equal(v:none, eval(string(v:none)))
1220 call assert_equal(v:null, eval(string(v:null)))
Bram Moolenaar767d8c12016-01-25 20:22:54 +01001221
Bram Moolenaar15550002016-01-31 18:45:24 +01001222 call assert_equal(v:false, copy(v:false))
1223 call assert_equal(v:true, copy(v:true))
1224 call assert_equal(v:none, copy(v:none))
1225 call assert_equal(v:null, copy(v:null))
1226
1227 call assert_equal([v:false], deepcopy([v:false]))
1228 call assert_equal([v:true], deepcopy([v:true]))
1229 call assert_equal([v:none], deepcopy([v:none]))
1230 call assert_equal([v:null], deepcopy([v:null]))
1231
Bram Moolenaar767d8c12016-01-25 20:22:54 +01001232 call assert_true(empty(v:false))
1233 call assert_false(empty(v:true))
1234 call assert_true(empty(v:null))
1235 call assert_true(empty(v:none))
Bram Moolenaar6650a692016-01-26 19:59:10 +01001236
1237 func ChangeYourMind()
Bram Moolenaar1e115362019-01-09 23:01:02 +01001238 try
1239 return v:true
1240 finally
1241 return 'something else'
1242 endtry
Bram Moolenaar6650a692016-01-26 19:59:10 +01001243 endfunc
1244
1245 call ChangeYourMind()
Bram Moolenaarf95534c2016-01-23 21:59:52 +01001246endfunc
1247
1248"-------------------------------------------------------------------------------
Bram Moolenaarea8c2192016-02-07 19:27:53 +01001249" Test 92: skipping code {{{1
1250"-------------------------------------------------------------------------------
1251
1252func Test_skip()
1253 let Fn = function('Test_type')
1254 call assert_false(0 && Fn[1])
1255 call assert_false(0 && string(Fn))
1256 call assert_false(0 && len(Fn))
1257 let l = []
1258 call assert_false(0 && l[1])
1259 call assert_false(0 && string(l))
1260 call assert_false(0 && len(l))
1261 let f = 1.0
1262 call assert_false(0 && f[1])
1263 call assert_false(0 && string(f))
1264 call assert_false(0 && len(f))
1265 let sp = v:null
1266 call assert_false(0 && sp[1])
1267 call assert_false(0 && string(sp))
1268 call assert_false(0 && len(sp))
1269
1270endfunc
1271
1272"-------------------------------------------------------------------------------
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001273" Test 93: :echo and string() {{{1
1274"-------------------------------------------------------------------------------
1275
1276func Test_echo_and_string()
1277 " String
1278 let a = 'foo bar'
1279 redir => result
1280 echo a
1281 echo string(a)
1282 redir END
1283 let l = split(result, "\n")
1284 call assert_equal(["foo bar",
1285 \ "'foo bar'"], l)
1286
1287 " Float
1288 if has('float')
1289 let a = -1.2e0
1290 redir => result
1291 echo a
1292 echo string(a)
1293 redir END
1294 let l = split(result, "\n")
1295 call assert_equal(["-1.2",
1296 \ "-1.2"], l)
1297 endif
1298
1299 " Funcref
1300 redir => result
1301 echo function('string')
1302 echo string(function('string'))
1303 redir END
1304 let l = split(result, "\n")
1305 call assert_equal(["string",
1306 \ "function('string')"], l)
1307
1308 " Recursive dictionary
1309 let a = {}
1310 let a["a"] = a
1311 redir => result
1312 echo a
1313 echo string(a)
1314 redir END
1315 let l = split(result, "\n")
1316 call assert_equal(["{'a': {...}}",
1317 \ "{'a': {...}}"], l)
1318
1319 " Recursive list
1320 let a = [0]
1321 let a[0] = a
1322 redir => result
1323 echo a
1324 echo string(a)
1325 redir END
1326 let l = split(result, "\n")
1327 call assert_equal(["[[...]]",
1328 \ "[[...]]"], l)
1329
1330 " Empty dictionaries in a list
1331 let a = {}
1332 redir => result
1333 echo [a, a, a]
1334 echo string([a, a, a])
1335 redir END
1336 let l = split(result, "\n")
1337 call assert_equal(["[{}, {}, {}]",
1338 \ "[{}, {}, {}]"], l)
1339
1340 " Empty dictionaries in a dictionary
1341 let a = {}
1342 let b = {"a": a, "b": a}
1343 redir => result
1344 echo b
1345 echo string(b)
1346 redir END
1347 let l = split(result, "\n")
1348 call assert_equal(["{'a': {}, 'b': {}}",
1349 \ "{'a': {}, 'b': {}}"], l)
1350
1351 " Empty lists in a list
1352 let a = []
1353 redir => result
1354 echo [a, a, a]
1355 echo string([a, a, a])
1356 redir END
1357 let l = split(result, "\n")
1358 call assert_equal(["[[], [], []]",
1359 \ "[[], [], []]"], l)
1360
1361 " Empty lists in a dictionary
1362 let a = []
1363 let b = {"a": a, "b": a}
1364 redir => result
1365 echo b
1366 echo string(b)
1367 redir END
1368 let l = split(result, "\n")
1369 call assert_equal(["{'a': [], 'b': []}",
1370 \ "{'a': [], 'b': []}"], l)
1371
1372 " Dictionaries in a list
1373 let a = {"one": "yes", "two": "yes", "three": "yes"}
1374 redir => result
1375 echo [a, a, a]
1376 echo string([a, a, a])
1377 redir END
1378 let l = split(result, "\n")
1379 call assert_equal(["[{'one': 'yes', 'two': 'yes', 'three': 'yes'}, {...}, {...}]",
1380 \ "[{'one': 'yes', 'two': 'yes', 'three': 'yes'}, {'one': 'yes', 'two': 'yes', 'three': 'yes'}, {'one': 'yes', 'two': 'yes', 'three': 'yes'}]"], l)
1381
1382 " Dictionaries in a dictionary
1383 let a = {"one": "yes", "two": "yes", "three": "yes"}
1384 let b = {"a": a, "b": a}
1385 redir => result
1386 echo b
1387 echo string(b)
1388 redir END
1389 let l = split(result, "\n")
1390 call assert_equal(["{'a': {'one': 'yes', 'two': 'yes', 'three': 'yes'}, 'b': {...}}",
1391 \ "{'a': {'one': 'yes', 'two': 'yes', 'three': 'yes'}, 'b': {'one': 'yes', 'two': 'yes', 'three': 'yes'}}"], l)
1392
1393 " Lists in a list
1394 let a = [1, 2, 3]
1395 redir => result
1396 echo [a, a, a]
1397 echo string([a, a, a])
1398 redir END
1399 let l = split(result, "\n")
1400 call assert_equal(["[[1, 2, 3], [...], [...]]",
1401 \ "[[1, 2, 3], [1, 2, 3], [1, 2, 3]]"], l)
1402
1403 " Lists in a dictionary
1404 let a = [1, 2, 3]
1405 let b = {"a": a, "b": a}
1406 redir => result
1407 echo b
1408 echo string(b)
1409 redir END
1410 let l = split(result, "\n")
1411 call assert_equal(["{'a': [1, 2, 3], 'b': [...]}",
1412 \ "{'a': [1, 2, 3], 'b': [1, 2, 3]}"], l)
1413
Bram Moolenaar1363a302020-04-12 13:50:26 +02001414 call assert_fails('echo &:', 'E112:')
1415 call assert_fails('echo &g:', 'E112:')
1416 call assert_fails('echo &l:', 'E112:')
1417
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001418endfunc
1419
1420"-------------------------------------------------------------------------------
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001421" Test 94: 64-bit Numbers {{{1
1422"-------------------------------------------------------------------------------
1423
1424func Test_num64()
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001425 call assert_notequal( 4294967296, 0)
1426 call assert_notequal(-4294967296, 0)
1427 call assert_equal( 4294967296, 0xFFFFffff + 1)
1428 call assert_equal(-4294967296, -0xFFFFffff - 1)
1429
1430 call assert_equal( 9223372036854775807, 1 / 0)
1431 call assert_equal(-9223372036854775807, -1 / 0)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01001432 call assert_equal(-9223372036854775807 - 1, 0 / 0)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001433
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001434 if has('float')
1435 call assert_equal( 0x7FFFffffFFFFffff, float2nr( 1.0e150))
1436 call assert_equal(-0x7FFFffffFFFFffff, float2nr(-1.0e150))
1437 endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001438
1439 let rng = range(0xFFFFffff, 0x100000001)
1440 call assert_equal([0xFFFFffff, 0x100000000, 0x100000001], rng)
1441 call assert_equal(0x100000001, max(rng))
1442 call assert_equal(0xFFFFffff, min(rng))
1443 call assert_equal(rng, sort(range(0x100000001, 0xFFFFffff, -1), 'N'))
1444endfunc
1445
1446"-------------------------------------------------------------------------------
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001447" Test 95: lines of :append, :change, :insert {{{1
1448"-------------------------------------------------------------------------------
1449
1450function! DefineFunction(name, body)
1451 let func = join(['function! ' . a:name . '()'] + a:body + ['endfunction'], "\n")
1452 exec func
1453endfunction
1454
1455func Test_script_lines()
1456 " :append
1457 try
Bram Moolenaar1e115362019-01-09 23:01:02 +01001458 call DefineFunction('T_Append', [
1459 \ 'append',
1460 \ 'py <<EOS',
1461 \ '.',
1462 \ ])
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001463 catch
Bram Moolenaar1e115362019-01-09 23:01:02 +01001464 call assert_report("Can't define function")
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001465 endtry
1466 try
Bram Moolenaar1e115362019-01-09 23:01:02 +01001467 call DefineFunction('T_Append', [
1468 \ 'append',
1469 \ 'abc',
1470 \ ])
1471 call assert_report("Shouldn't be able to define function")
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001472 catch
Bram Moolenaar1e115362019-01-09 23:01:02 +01001473 call assert_exception('Vim(function):E126: Missing :endfunction')
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001474 endtry
1475
1476 " :change
1477 try
Bram Moolenaar1e115362019-01-09 23:01:02 +01001478 call DefineFunction('T_Change', [
1479 \ 'change',
1480 \ 'py <<EOS',
1481 \ '.',
1482 \ ])
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001483 catch
Bram Moolenaar1e115362019-01-09 23:01:02 +01001484 call assert_report("Can't define function")
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001485 endtry
1486 try
Bram Moolenaar1e115362019-01-09 23:01:02 +01001487 call DefineFunction('T_Change', [
1488 \ 'change',
1489 \ 'abc',
1490 \ ])
1491 call assert_report("Shouldn't be able to define function")
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001492 catch
Bram Moolenaar1e115362019-01-09 23:01:02 +01001493 call assert_exception('Vim(function):E126: Missing :endfunction')
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001494 endtry
1495
1496 " :insert
1497 try
Bram Moolenaar1e115362019-01-09 23:01:02 +01001498 call DefineFunction('T_Insert', [
1499 \ 'insert',
1500 \ 'py <<EOS',
1501 \ '.',
1502 \ ])
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001503 catch
Bram Moolenaar1e115362019-01-09 23:01:02 +01001504 call assert_report("Can't define function")
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001505 endtry
1506 try
Bram Moolenaar1e115362019-01-09 23:01:02 +01001507 call DefineFunction('T_Insert', [
1508 \ 'insert',
1509 \ 'abc',
1510 \ ])
1511 call assert_report("Shouldn't be able to define function")
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001512 catch
Bram Moolenaar1e115362019-01-09 23:01:02 +01001513 call assert_exception('Vim(function):E126: Missing :endfunction')
Bram Moolenaar70bcd732017-01-12 22:20:54 +01001514 endtry
1515endfunc
1516
1517"-------------------------------------------------------------------------------
Bram Moolenaar478af672017-04-10 22:22:42 +02001518" Test 96: line continuation {{{1
1519"
Bram Moolenaar1e115362019-01-09 23:01:02 +01001520" Undefined behavior was detected by ubsan with line continuation
1521" after an empty line.
Bram Moolenaar478af672017-04-10 22:22:42 +02001522"-------------------------------------------------------------------------------
1523func Test_script_emty_line_continuation()
1524
1525 \
1526endfunc
1527
1528"-------------------------------------------------------------------------------
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001529" Test 97: bitwise functions {{{1
1530"-------------------------------------------------------------------------------
1531func Test_bitwise_functions()
1532 " and
1533 call assert_equal(127, and(127, 127))
1534 call assert_equal(16, and(127, 16))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001535 eval 127->and(16)->assert_equal(16)
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001536 call assert_equal(0, and(127, 128))
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001537 call assert_fails("call and([], 1)", 'E745:')
1538 call assert_fails("call and({}, 1)", 'E728:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001539 if has('float')
1540 call assert_fails("call and(1.0, 1)", 'E805:')
1541 call assert_fails("call and(1, 1.0)", 'E805:')
1542 endif
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001543 call assert_fails("call and(1, [])", 'E745:')
1544 call assert_fails("call and(1, {})", 'E728:')
1545 " or
1546 call assert_equal(23, or(16, 7))
1547 call assert_equal(15, or(8, 7))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001548 eval 8->or(7)->assert_equal(15)
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001549 call assert_equal(123, or(0, 123))
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001550 call assert_fails("call or([], 1)", 'E745:')
1551 call assert_fails("call or({}, 1)", 'E728:')
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001552 if has('float')
1553 call assert_fails("call or(1.0, 1)", 'E805:')
1554 call assert_fails("call or(1, 1.0)", 'E805:')
1555 endif
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001556 call assert_fails("call or(1, [])", 'E745:')
1557 call assert_fails("call or(1, {})", 'E728:')
1558 " xor
1559 call assert_equal(0, xor(127, 127))
1560 call assert_equal(111, xor(127, 16))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001561 eval 127->xor(16)->assert_equal(111)
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001562 call assert_equal(255, xor(127, 128))
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001563 if has('float')
1564 call assert_fails("call xor(1.0, 1)", 'E805:')
1565 call assert_fails("call xor(1, 1.0)", 'E805:')
1566 endif
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001567 call assert_fails("call xor([], 1)", 'E745:')
1568 call assert_fails("call xor({}, 1)", 'E728:')
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001569 call assert_fails("call xor(1, [])", 'E745:')
1570 call assert_fails("call xor(1, {})", 'E728:')
1571 " invert
1572 call assert_equal(65408, and(invert(127), 65535))
Bram Moolenaar073e4b92019-08-18 23:01:56 +02001573 eval 127->invert()->and(65535)->assert_equal(65408)
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001574 call assert_equal(65519, and(invert(16), 65535))
1575 call assert_equal(65407, and(invert(128), 65535))
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001576 if has('float')
1577 call assert_fails("call invert(1.0)", 'E805:')
1578 endif
Bram Moolenaar863e80b2017-06-04 20:30:00 +02001579 call assert_fails("call invert([])", 'E745:')
1580 call assert_fails("call invert({})", 'E728:')
1581endfunc
1582
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001583" Test using bang after user command {{{1
1584func Test_user_command_with_bang()
1585 command -bang Nieuw let nieuw = 1
1586 Ni!
1587 call assert_equal(1, nieuw)
1588 unlet nieuw
1589 delcommand Nieuw
1590endfunc
1591
Bram Moolenaarb9adef72020-01-02 14:31:22 +01001592func Test_script_expand_sfile()
1593 let lines =<< trim END
1594 func s:snr()
1595 return expand('<sfile>')
1596 endfunc
1597 let g:result = s:snr()
1598 END
1599 call writefile(lines, 'Xexpand')
1600 source Xexpand
1601 call assert_match('<SNR>\d\+_snr', g:result)
1602 source Xexpand
1603 call assert_match('<SNR>\d\+_snr', g:result)
1604
1605 call delete('Xexpand')
1606 unlet g:result
1607endfunc
1608
Bram Moolenaarff697e62019-02-12 22:28:33 +01001609func Test_compound_assignment_operators()
1610 " Test for number
1611 let x = 1
1612 let x += 10
1613 call assert_equal(11, x)
1614 let x -= 5
1615 call assert_equal(6, x)
1616 let x *= 4
1617 call assert_equal(24, x)
1618 let x /= 3
1619 call assert_equal(8, x)
1620 let x %= 3
1621 call assert_equal(2, x)
1622 let x .= 'n'
1623 call assert_equal('2n', x)
1624
Bram Moolenaare21c1582019-03-02 11:57:09 +01001625 " Test special cases: division or modulus with 0.
1626 let x = 1
1627 let x /= 0
Bram Moolenaar82f654e2020-02-17 22:12:50 +01001628 call assert_equal(0x7FFFFFFFFFFFFFFF, x)
Bram Moolenaare21c1582019-03-02 11:57:09 +01001629
1630 let x = -1
1631 let x /= 0
Bram Moolenaar82f654e2020-02-17 22:12:50 +01001632 call assert_equal(-0x7FFFFFFFFFFFFFFF, x)
Bram Moolenaare21c1582019-03-02 11:57:09 +01001633
1634 let x = 0
1635 let x /= 0
Bram Moolenaar82f654e2020-02-17 22:12:50 +01001636 call assert_equal(-0x7FFFFFFFFFFFFFFF - 1, x)
Bram Moolenaare21c1582019-03-02 11:57:09 +01001637
1638 let x = 1
1639 let x %= 0
1640 call assert_equal(0, x)
1641
1642 let x = -1
1643 let x %= 0
1644 call assert_equal(0, x)
1645
1646 let x = 0
1647 let x %= 0
1648 call assert_equal(0, x)
1649
Bram Moolenaarff697e62019-02-12 22:28:33 +01001650 " Test for string
1651 let x = 'str'
1652 let x .= 'ing'
1653 call assert_equal('string', x)
1654 let x += 1
1655 call assert_equal(1, x)
Bram Moolenaarff697e62019-02-12 22:28:33 +01001656
1657 if has('float')
Bram Moolenaar5feabe02020-01-30 18:24:53 +01001658 " Test for float
1659 let x -= 1.5
1660 call assert_equal(-0.5, x)
1661 let x = 0.5
1662 let x += 4.5
1663 call assert_equal(5.0, x)
1664 let x -= 1.5
1665 call assert_equal(3.5, x)
1666 let x *= 3.0
1667 call assert_equal(10.5, x)
1668 let x /= 2.5
1669 call assert_equal(4.2, x)
1670 call assert_fails('let x %= 0.5', 'E734')
1671 call assert_fails('let x .= "f"', 'E734')
Bram Moolenaar8b633132020-03-20 18:20:51 +01001672 let x = !3.14
1673 call assert_equal(0.0, x)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001674
1675 " integer and float operations
1676 let x = 1
1677 let x *= 2.1
1678 call assert_equal(2.1, x)
1679 let x = 1
1680 let x /= 0.25
1681 call assert_equal(4.0, x)
1682 let x = 1
1683 call assert_fails('let x %= 0.25', 'E734:')
1684 let x = 1
1685 call assert_fails('let x .= 0.25', 'E734:')
1686 let x = 1.0
1687 call assert_fails('let x += [1.1]', 'E734:')
Bram Moolenaarff697e62019-02-12 22:28:33 +01001688 endif
1689
1690 " Test for environment variable
1691 let $FOO = 1
1692 call assert_fails('let $FOO += 1', 'E734')
1693 call assert_fails('let $FOO -= 1', 'E734')
1694 call assert_fails('let $FOO *= 1', 'E734')
1695 call assert_fails('let $FOO /= 1', 'E734')
1696 call assert_fails('let $FOO %= 1', 'E734')
1697 let $FOO .= 's'
1698 call assert_equal('1s', $FOO)
1699 unlet $FOO
1700
1701 " Test for option variable (type: number)
1702 let &scrolljump = 1
1703 let &scrolljump += 5
1704 call assert_equal(6, &scrolljump)
1705 let &scrolljump -= 2
1706 call assert_equal(4, &scrolljump)
1707 let &scrolljump *= 3
1708 call assert_equal(12, &scrolljump)
1709 let &scrolljump /= 2
1710 call assert_equal(6, &scrolljump)
1711 let &scrolljump %= 5
1712 call assert_equal(1, &scrolljump)
1713 call assert_fails('let &scrolljump .= "j"', 'E734')
1714 set scrolljump&vim
1715
1716 " Test for register
1717 let @/ = 1
1718 call assert_fails('let @/ += 1', 'E734')
1719 call assert_fails('let @/ -= 1', 'E734')
1720 call assert_fails('let @/ *= 1', 'E734')
1721 call assert_fails('let @/ /= 1', 'E734')
1722 call assert_fails('let @/ %= 1', 'E734')
1723 let @/ .= 's'
1724 call assert_equal('1s', @/)
1725 let @/ = ''
1726endfunc
1727
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001728func Test_unlet_env()
1729 let $TESTVAR = 'yes'
1730 call assert_equal('yes', $TESTVAR)
1731 call assert_fails('lockvar $TESTVAR', 'E940')
1732 call assert_fails('unlockvar $TESTVAR', 'E940')
1733 call assert_equal('yes', $TESTVAR)
1734 if 0
1735 unlet $TESTVAR
1736 endif
1737 call assert_equal('yes', $TESTVAR)
1738 unlet $TESTVAR
1739 call assert_equal('', $TESTVAR)
1740endfunc
1741
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001742func Test_refcount()
1743 " Immediate values
1744 call assert_equal(-1, test_refcount(1))
1745 call assert_equal(-1, test_refcount('s'))
1746 call assert_equal(-1, test_refcount(v:true))
1747 call assert_equal(0, test_refcount([]))
1748 call assert_equal(0, test_refcount({}))
1749 call assert_equal(0, test_refcount(0zff))
1750 call assert_equal(0, test_refcount({-> line('.')}))
1751 if has('float')
1752 call assert_equal(-1, test_refcount(0.1))
1753 endif
1754 if has('job')
1755 call assert_equal(0, test_refcount(job_start([&shell, &shellcmdflag, 'echo .'])))
1756 endif
1757
1758 " No refcount types
1759 let x = 1
1760 call assert_equal(-1, test_refcount(x))
1761 let x = 's'
1762 call assert_equal(-1, test_refcount(x))
1763 let x = v:true
1764 call assert_equal(-1, test_refcount(x))
1765 if has('float')
1766 let x = 0.1
1767 call assert_equal(-1, test_refcount(x))
1768 endif
1769
1770 " Check refcount
1771 let x = []
1772 call assert_equal(1, test_refcount(x))
1773
1774 let x = {}
Bram Moolenaarce90e362019-09-08 18:58:44 +02001775 call assert_equal(1, x->test_refcount())
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001776
1777 let x = 0zff
1778 call assert_equal(1, test_refcount(x))
1779
1780 let X = {-> line('.')}
1781 call assert_equal(1, test_refcount(X))
1782 let Y = X
1783 call assert_equal(2, test_refcount(X))
1784
1785 if has('job')
1786 let job = job_start([&shell, &shellcmdflag, 'echo .'])
1787 call assert_equal(1, test_refcount(job))
1788 call assert_equal(1, test_refcount(job_getchannel(job)))
1789 call assert_equal(1, test_refcount(job))
1790 endif
1791
1792 " Function arguments, copying and unassigning
1793 func ExprCheck(x, i)
1794 let i = a:i + 1
1795 call assert_equal(i, test_refcount(a:x))
1796 let Y = a:x
1797 call assert_equal(i + 1, test_refcount(a:x))
1798 call assert_equal(test_refcount(a:x), test_refcount(Y))
1799 let Y = 0
1800 call assert_equal(i, test_refcount(a:x))
1801 endfunc
1802 call ExprCheck([], 0)
1803 call ExprCheck({}, 0)
1804 call ExprCheck(0zff, 0)
1805 call ExprCheck({-> line('.')}, 0)
1806 if has('job')
1807 call ExprCheck(job, 1)
1808 call ExprCheck(job_getchannel(job), 1)
1809 call job_stop(job)
1810 endif
1811 delfunc ExprCheck
1812
1813 " Regarding function
1814 func Func(x) abort
1815 call assert_equal(2, test_refcount(function('Func')))
1816 call assert_equal(0, test_refcount(funcref('Func')))
1817 endfunc
1818 call assert_equal(1, test_refcount(function('Func')))
1819 call assert_equal(0, test_refcount(function('Func', [1])))
1820 call assert_equal(0, test_refcount(funcref('Func')))
1821 call assert_equal(0, test_refcount(funcref('Func', [1])))
1822 let X = function('Func')
1823 let Y = X
1824 call assert_equal(1, test_refcount(X))
1825 let X = function('Func', [1])
1826 let Y = X
1827 call assert_equal(2, test_refcount(X))
1828 let X = funcref('Func')
1829 let Y = X
1830 call assert_equal(2, test_refcount(X))
1831 let X = funcref('Func', [1])
1832 let Y = X
1833 call assert_equal(2, test_refcount(X))
1834 unlet X
1835 unlet Y
1836 call Func(1)
1837 delfunc Func
1838
1839 " Function with dict
1840 func DictFunc() dict
1841 call assert_equal(3, test_refcount(self))
1842 endfunc
1843 let d = {'Func': function('DictFunc')}
1844 call assert_equal(1, test_refcount(d))
1845 call assert_equal(0, test_refcount(d.Func))
1846 call d.Func()
1847 unlet d
1848 delfunc DictFunc
1849endfunc
1850
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001851" Test for missing :endif, :endfor, :endwhile and :endtry {{{1
1852func Test_missing_end()
1853 call writefile(['if 2 > 1', 'echo ">"'], 'Xscript')
1854 call assert_fails('source Xscript', 'E171:')
1855 call writefile(['for i in range(5)', 'echo i'], 'Xscript')
1856 call assert_fails('source Xscript', 'E170:')
1857 call writefile(['while v:true', 'echo "."'], 'Xscript')
1858 call assert_fails('source Xscript', 'E170:')
1859 call writefile(['try', 'echo "."'], 'Xscript')
1860 call assert_fails('source Xscript', 'E600:')
1861 call delete('Xscript')
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001862
1863 " Using endfor with :while
1864 let caught_e732 = 0
1865 try
1866 while v:true
1867 endfor
1868 catch /E732:/
1869 let caught_e732 = 1
1870 endtry
1871 call assert_equal(1, caught_e732)
1872
1873 " Using endwhile with :for
1874 let caught_e733 = 0
1875 try
1876 for i in range(1)
1877 endwhile
1878 catch /E733:/
1879 let caught_e733 = 1
1880 endtry
1881 call assert_equal(1, caught_e733)
1882
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001883 " Using endfunc with :if
1884 call assert_fails('exe "if 1 | endfunc | endif"', 'E193:')
1885
Bram Moolenaar818fc9a2020-02-21 17:54:45 +01001886 " Missing 'in' in a :for statement
1887 call assert_fails('for i range(1) | endfor', 'E690:')
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001888
1889 " Incorrect number of variables in for
1890 call assert_fails('for [i,] in range(3) | endfor', 'E475:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001891endfunc
1892
1893" Test for deep nesting of if/for/while/try statements {{{1
1894func Test_deep_nest()
1895 if !CanRunVimInTerminal()
1896 throw 'Skipped: cannot run vim in terminal'
1897 endif
1898
1899 let lines =<< trim [SCRIPT]
1900 " Deep nesting of if ... endif
1901 func Test1()
1902 let @a = join(repeat(['if v:true'], 51), "\n")
1903 let @a ..= "\n"
1904 let @a ..= join(repeat(['endif'], 51), "\n")
1905 @a
1906 let @a = ''
1907 endfunc
1908
1909 " Deep nesting of for ... endfor
1910 func Test2()
1911 let @a = join(repeat(['for i in [1]'], 51), "\n")
1912 let @a ..= "\n"
1913 let @a ..= join(repeat(['endfor'], 51), "\n")
1914 @a
1915 let @a = ''
1916 endfunc
1917
1918 " Deep nesting of while ... endwhile
1919 func Test3()
1920 let @a = join(repeat(['while v:true'], 51), "\n")
1921 let @a ..= "\n"
1922 let @a ..= join(repeat(['endwhile'], 51), "\n")
1923 @a
1924 let @a = ''
1925 endfunc
1926
1927 " Deep nesting of try ... endtry
1928 func Test4()
1929 let @a = join(repeat(['try'], 51), "\n")
1930 let @a ..= "\necho v:true\n"
1931 let @a ..= join(repeat(['endtry'], 51), "\n")
1932 @a
1933 let @a = ''
1934 endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001935
1936 " Deep nesting of function ... endfunction
1937 func Test5()
1938 let @a = join(repeat(['function X()'], 51), "\n")
1939 let @a ..= "\necho v:true\n"
1940 let @a ..= join(repeat(['endfunction'], 51), "\n")
1941 @a
1942 let @a = ''
1943 endfunc
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001944 [SCRIPT]
1945 call writefile(lines, 'Xscript')
1946
1947 let buf = RunVimInTerminal('-S Xscript', {'rows': 6})
1948
1949 " Deep nesting of if ... endif
1950 call term_sendkeys(buf, ":call Test1()\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001951 call TermWait(buf)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001952 call WaitForAssert({-> assert_match('^E579:', term_getline(buf, 5))})
1953
1954 " Deep nesting of for ... endfor
1955 call term_sendkeys(buf, ":call Test2()\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001956 call TermWait(buf)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001957 call WaitForAssert({-> assert_match('^E585:', term_getline(buf, 5))})
1958
1959 " Deep nesting of while ... endwhile
1960 call term_sendkeys(buf, ":call Test3()\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001961 call TermWait(buf)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001962 call WaitForAssert({-> assert_match('^E585:', term_getline(buf, 5))})
1963
1964 " Deep nesting of try ... endtry
1965 call term_sendkeys(buf, ":call Test4()\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001966 call TermWait(buf)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001967 call WaitForAssert({-> assert_match('^E601:', term_getline(buf, 5))})
1968
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001969 " Deep nesting of function ... endfunction
1970 call term_sendkeys(buf, ":call Test5()\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001971 call TermWait(buf)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001972 call WaitForAssert({-> assert_match('^E1058:', term_getline(buf, 4))})
1973 call term_sendkeys(buf, "\<C-C>\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001974 call TermWait(buf)
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001975
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01001976 "let l = ''
1977 "for i in range(1, 6)
1978 " let l ..= term_getline(buf, i) . "\n"
1979 "endfor
1980 "call assert_report(l)
1981
1982 call StopVimInTerminal(buf)
1983 call delete('Xscript')
1984endfunc
1985
Bram Moolenaar8b633132020-03-20 18:20:51 +01001986" Test for errors in converting to float from various types {{{1
1987func Test_float_conversion_errors()
1988 if has('float')
1989 call assert_fails('let x = 4.0 % 2.0', 'E804')
1990 call assert_fails('echo 1.1[0]', 'E806')
1991 call assert_fails('echo sort([function("min"), 1], "f")', 'E891:')
1992 call assert_fails('echo 3.2 == "vim"', 'E892:')
1993 call assert_fails('echo sort([[], 1], "f")', 'E893:')
1994 call assert_fails('echo sort([{}, 1], "f")', 'E894:')
1995 call assert_fails('echo 3.2 == v:true', 'E362:')
1996 call assert_fails('echo 3.2 == v:none', 'E907:')
1997 endif
1998endfunc
1999
Bram Moolenaar08f41572020-04-20 16:50:00 +02002000func Test_invalid_function_names()
2001 " function name not starting with capital
2002 let caught_e128 = 0
2003 try
2004 func! g:test()
2005 echo "test"
2006 endfunc
2007 catch /E128:/
2008 let caught_e128 = 1
2009 endtry
2010 call assert_equal(1, caught_e128)
2011
2012 " function name includes a colon
2013 let caught_e884 = 0
2014 try
2015 func! b:test()
2016 echo "test"
2017 endfunc
2018 catch /E884:/
2019 let caught_e884 = 1
2020 endtry
2021 call assert_equal(1, caught_e884)
2022
2023 " function name folowed by #
2024 let caught_e128 = 0
2025 try
2026 func! test2() "#
2027 echo "test2"
2028 endfunc
2029 catch /E128:/
2030 let caught_e128 = 1
2031 endtry
2032 call assert_equal(1, caught_e128)
2033
2034 " function name starting with/without "g:", buffer-local funcref.
2035 function! g:Foo(n)
2036 return 'called Foo(' . a:n . ')'
2037 endfunction
2038 let b:my_func = function('Foo')
2039 call assert_equal('called Foo(1)', b:my_func(1))
2040 call assert_equal('called Foo(2)', g:Foo(2))
2041 call assert_equal('called Foo(3)', Foo(3))
2042 delfunc g:Foo
2043
2044 " script-local function used in Funcref must exist.
2045 let lines =<< trim END
2046 func s:Testje()
2047 return "foo"
2048 endfunc
2049 let Bar = function('s:Testje')
2050 call assert_equal(0, exists('s:Testje'))
2051 call assert_equal(1, exists('*s:Testje'))
2052 call assert_equal(1, exists('Bar'))
2053 call assert_equal(1, exists('*Bar'))
2054 END
2055 call writefile(lines, 'Xscript')
2056 source Xscript
2057 call delete('Xscript')
2058endfunc
2059
2060" substring and variable name
2061func Test_substring_var()
2062 let str = 'abcdef'
2063 let n = 3
2064 call assert_equal('def', str[n:])
2065 call assert_equal('abcd', str[:n])
2066 call assert_equal('d', str[n:n])
2067 unlet n
2068 let nn = 3
2069 call assert_equal('def', str[nn:])
2070 call assert_equal('abcd', str[:nn])
2071 call assert_equal('d', str[nn:nn])
2072 unlet nn
2073 let b:nn = 4
2074 call assert_equal('ef', str[b:nn:])
2075 call assert_equal('abcde', str[:b:nn])
2076 call assert_equal('e', str[b:nn:b:nn])
2077 unlet b:nn
2078endfunc
2079
Bram Moolenaar863e80b2017-06-04 20:30:00 +02002080"-------------------------------------------------------------------------------
Bram Moolenaarf49e2402015-12-30 15:59:25 +01002081" Modelines {{{1
Bram Moolenaar1f068232019-11-03 16:17:26 +01002082" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
Bram Moolenaarf49e2402015-12-30 15:59:25 +01002083"-------------------------------------------------------------------------------