robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #33098 from zeertzjq/vim-9.1.1245e1f1386d5e

master
21.0 KiB874 linesraw
1" Tests for the :let command.
2
3source vim9.vim
4
5func Test_let()
6  " Test to not autoload when assigning.  It causes internal error.
7  set runtimepath+=./sautest
8  let Test104#numvar = function('tr')
9  call assert_equal("function('tr')", string(Test104#numvar))
10
11  let foo#tr = function('tr')
12  call assert_equal("function('tr')", string(foo#tr))
13  unlet foo#tr
14
15  let a = 1
16  let b = 2
17
18  let out = execute('let a b')
19  let s = "\na                     #1\nb                     #2"
20  call assert_equal(s, out)
21
22  let out = execute('let {0 == 1 ? "a" : "b"}')
23  let s = "\nb                     #2"
24  call assert_equal(s, out)
25
26  let out = execute('let {0 == 1 ? "a" : "b"} a')
27  let s = "\nb                     #2\na                     #1"
28  call assert_equal(s, out)
29
30  let out = execute('let a {0 == 1 ? "a" : "b"}')
31  let s = "\na                     #1\nb                     #2"
32  call assert_equal(s, out)
33
34  " Test for displaying a string variable
35  let s = 'vim'
36  let out = execute('let s')
37  let s = "\ns                      vim"
38  call assert_equal(s, out)
39
40  " Test for displaying a list variable
41  let l = [1, 2]
42  let out = execute('let l')
43  let s = "\nl                     [1, 2]"
44  call assert_equal(s, out)
45
46  " Test for displaying a dict variable
47  let d = {'k' : 'v'}
48  let out = execute('let d')
49  let s = "\nd                     {'k': 'v'}"
50  call assert_equal(s, out)
51
52  " Test for displaying a function reference variable
53  let F = function('min')
54  let out = execute('let F')
55  let s = "\nF                     *min()"
56  call assert_equal(s, out)
57
58  let x = 0
59  if 0 | let x = 1 | endif
60  call assert_equal(0, x)
61
62  " Display a list item using an out of range index
63  let l = [10]
64  call assert_fails('let l[1]', 'E684:')
65
66  " List special variable dictionaries
67  let g:Test_Global_Var = 5
68  call assert_match("\nTest_Global_Var       #5", execute('let g:'))
69  unlet g:Test_Global_Var
70
71  let b:Test_Buf_Var = 8
72  call assert_match("\nb:Test_Buf_Var        #8", execute('let b:'))
73  unlet b:Test_Buf_Var
74
75  let w:Test_Win_Var = 'foo'
76  call assert_equal("\nw:Test_Win_Var         foo", execute('let w:'))
77  unlet w:Test_Win_Var
78
79  let t:Test_Tab_Var = 'bar'
80  call assert_equal("\nt:Test_Tab_Var         bar", execute('let t:'))
81  unlet t:Test_Tab_Var
82
83  let s:Test_Script_Var = [7]
84  call assert_match("\ns:Test_Script_Var     \\[7]", execute('let s:'))
85  unlet s:Test_Script_Var
86
87  let l:Test_Local_Var = {'k' : 5}
88  call assert_match("\nl:Test_Local_Var      {'k': 5}", execute('let l:'))
89  call assert_match("v:errors              []", execute('let v:'))
90
91  " Test for assigning multiple list items
92  let l = [1, 2, 3]
93  let [l[0], l[1]] = [10, 20]
94  call assert_equal([10, 20, 3], l)
95
96  " Test for using curly brace name in the LHS of an assignment
97  let listvar = [1, 2]
98  let s = 'listvar'
99  let {s} = [3, 4]
100  call assert_equal([3, 4], listvar)
101
102  " Test for using curly brace name as a list and as list index in the LHS of
103  " an assignment
104  let listvar = [1, 2]
105  let idx = 1
106  let s = 'listvar'
107  let {s}[0] = 10
108  let s = 'idx'
109  let listvar[{s}] = 20
110  call assert_equal([10, 20], listvar)
111  let s1 = 'listvar'
112  let s2 = 'idx'
113  let {s1}[{s2}] = 30
114  call assert_equal([10, 30], listvar)
115
116  " Test for errors in conditional expression
117  call assert_fails('let val = [] ? 1 : 2', 'E745:')
118  call assert_fails('let val = 1 ? 5+ : 6', 'E121:')
119  call assert_fails('let val = 1 ? 0 : 5+', 'E15:')
120  call assert_false(exists('val'))
121
122  " Test for errors in logical operators
123  let @a = 'if [] || 0 | let val = 2 | endif'
124  call assert_fails('exe @a', 'E745:')
125  call assert_fails('call feedkeys(":let val = 0 || []\<cr>", "xt")', 'E745:')
126  call assert_fails('exe "let val = [] && 5"', 'E745:')
127  call assert_fails('exe "let val = 6 && []"', 'E745:')
128endfunc
129
130func s:set_arg1(a) abort
131  let a:a = 1
132endfunction
133
134func s:set_arg2(a) abort
135  let a:b = 1
136endfunction
137
138func s:set_arg3(a) abort
139  let b = a:
140  let b['a'] = 1
141endfunction
142
143func s:set_arg4(a) abort
144  let b = a:
145  let b['a'] = 1
146endfunction
147
148func s:set_arg5(a) abort
149  let b = a:
150  let b['a'][0] = 1
151endfunction
152
153func s:set_arg6(a) abort
154  let a:a[0] = 1
155endfunction
156
157func s:set_arg7(a) abort
158  call extend(a:, {'a': 1})
159endfunction
160
161func s:set_arg8(a) abort
162  call extend(a:, {'b': 1})
163endfunction
164
165func s:set_arg9(a) abort
166  let a:['b'] = 1
167endfunction
168
169func s:set_arg10(a) abort
170  let b = a:
171  call extend(b, {'a': 1})
172endfunction
173
174func s:set_arg11(a) abort
175  let b = a:
176  call extend(b, {'b': 1})
177endfunction
178
179func s:set_arg12(a) abort
180  let b = a:
181  let b['b'] = 1
182endfunction
183
184func Test_let_arg_fail()
185  call assert_fails('call s:set_arg1(1)', 'E46:')
186  call assert_fails('call s:set_arg2(1)', 'E461:')
187  call assert_fails('call s:set_arg3(1)', 'E46:')
188  call assert_fails('call s:set_arg4(1)', 'E46:')
189  call assert_fails('call s:set_arg5(1)', 'E46:')
190  call s:set_arg6([0])
191  call assert_fails('call s:set_arg7(1)', 'E742:')
192  call assert_fails('call s:set_arg8(1)', 'E742:')
193  call assert_fails('call s:set_arg9(1)', 'E461:')
194  call assert_fails('call s:set_arg10(1)', 'E742:')
195  call assert_fails('call s:set_arg11(1)', 'E742:')
196  call assert_fails('call s:set_arg12(1)', 'E461:')
197endfunction
198
199func s:set_varg1(...) abort
200  let a:000 = []
201endfunction
202
203func s:set_varg2(...) abort
204  let a:000[0] = 1
205endfunction
206
207func s:set_varg3(...) abort
208  let a:000 += [1]
209endfunction
210
211func s:set_varg4(...) abort
212  call add(a:000, 1)
213endfunction
214
215func s:set_varg5(...) abort
216  let a:000[0][0] = 1
217endfunction
218
219func s:set_varg6(...) abort
220  let b = a:000
221  let b[0] = 1
222endfunction
223
224func s:set_varg7(...) abort
225  let b = a:000
226  let b += [1]
227endfunction
228
229func s:set_varg8(...) abort
230  let b = a:000
231  call add(b, 1)
232endfunction
233
234func s:set_varg9(...) abort
235  let b = a:000
236  let b[0][0] = 1
237endfunction
238
239func Test_let_varg_fail()
240  call assert_fails('call s:set_varg1(1)', 'E46:')
241  call assert_fails('call s:set_varg2(1)', 'E742:')
242  call assert_fails('call s:set_varg3(1)', 'E46:')
243  call assert_fails('call s:set_varg4(1)', 'E742:')
244  call s:set_varg5([0])
245  call assert_fails('call s:set_varg6(1)', 'E742:')
246  call assert_fails('call s:set_varg7(1)', 'E742:')
247  call assert_fails('call s:set_varg8(1)', 'E742:')
248  call s:set_varg9([0])
249endfunction
250
251func Test_let_utf8_environment()
252  let $a = 'ĀĒĪŌŪあいうえお'
253  call assert_equal('ĀĒĪŌŪあいうえお', $a)
254endfunc
255
256func Test_let_no_type_checking()
257  let v = 1
258  let v = [1,2,3]
259  let v = {'a': 1, 'b': 2}
260  let v = 3.4
261  let v = 'hello'
262endfunc
263
264func Test_let_termcap()
265  throw 'Skipped: Nvim does not support termcap options'
266  " Terminal code
267  let old_t_te = &t_te
268  let &t_te = "\<Esc>[yes;"
269  call assert_match('t_te.*^[[yes;', execute("set termcap"))
270  let &t_te = old_t_te
271
272  if exists("+t_k1")
273    " Key code
274    let old_t_k1 = &t_k1
275    let &t_k1 = "that"
276    call assert_match('t_k1.*that', execute("set termcap"))
277    let &t_k1 = old_t_k1
278  endif
279
280  call assert_fails('let x = &t_xx', 'E113:')
281  let &t_xx = "yes"
282  call assert_equal("yes", &t_xx)
283  let &t_xx = ""
284  call assert_fails('let x = &t_xx', 'E113:')
285endfunc
286
287func Test_let_option_error()
288  let _w = &tw
289  let &tw = 80
290  call assert_fails('let &tw .= 1', ['E734:', 'E734:'])
291  call assert_fails('let &tw .= []', ['E734:', 'E734:'])
292  call assert_fails('let &tw = []', ['E745:', 'E745:'])
293  call assert_fails('let &tw += []', ['E745:', 'E745:'])
294  call assert_equal(80, &tw)
295  let &tw = _w
296
297  let _w = &autoread
298  let &autoread = 1
299  call assert_fails('let &autoread .= 1', ['E734:', 'E734:'])
300  call assert_fails('let &autoread .= []', ['E734:', 'E734:'])
301  call assert_fails('let &autoread = []', ['E745:', 'E745:'])
302  call assert_fails('let &autoread += []', ['E745:', 'E745:'])
303  call assert_equal(1, &autoread)
304  let &autoread = _w
305
306  let _w = &fillchars
307  let &fillchars = "vert:|"
308  call assert_fails('let &fillchars += "diff:-"', ['E734:', 'E734:'])
309  call assert_fails('let &fillchars += []', ['E734:', 'E734:'])
310  call assert_fails('let &fillchars = []', ['E730:', 'E730:'])
311  call assert_fails('let &fillchars .= []', ['E730:', 'E730:'])
312  call assert_equal("vert:|", &fillchars)
313  let &fillchars = _w
314
315  call assert_fails('let &nosuchoption = 1', ['E355:', 'E355:'])
316  call assert_fails('let &nosuchoption = ""', ['E355:', 'E355:'])
317  call assert_fails('let &nosuchoption = []', ['E355:', 'E355:'])
318  call assert_fails('let &t_xx = []', ['E730:', 'E730:'])
319endfunc
320
321" Errors with the :let statement
322func Test_let_errors()
323  let s = 'abcd'
324  call assert_fails('let s[1] = 5', 'E689:')
325
326  let l = [1, 2, 3]
327  call assert_fails('let l[:] = 5', 'E709:')
328
329  call assert_fails('let x:lnum=5', ['E121:', 'E488:'])
330  call assert_fails('let v:=5', 'E461:')
331  call assert_fails('let [a]', 'E474:')
332  call assert_fails('let [a, b] = [', 'E697:')
333  call assert_fails('let [a, b] = [10, 20', 'E696:')
334  call assert_fails('let [a, b] = 10', 'E714:')
335  call assert_fails('let [a, , b] = [10, 20]', 'E475:')
336  call assert_fails('let [a, b&] = [10, 20]', 'E475:')
337  call assert_fails('let $ = 10', 'E475:')
338  call assert_fails('let $FOO[1] = "abc"', 'E18:')
339  call assert_fails('let &buftype[1] = "nofile"', 'E18:')
340  let s = "var"
341  let var = 1
342  call assert_fails('let var += [1,2]', 'E734:')
343  call assert_fails('let {s}.1 = 2', 'E1203:')
344  call assert_fails('let a[1] = 5', 'E121:')
345  let l = [[1,2]]
346  call assert_fails('let l[:][0] = [5]', 'E708:')
347  let d = {'k' : 4}
348  call assert_fails('let d.# = 5', 'E488:')
349  call assert_fails('let d.m += 5', 'E716:')
350  call assert_fails('let m = d[{]', 'E15:')
351  let l = [1, 2]
352  call assert_fails('let l[2] = 0', 'E684:')
353  call assert_fails('let l[0:1] = [1, 2, 3]', 'E710:')
354  call assert_fails('let l[-2:-3] = [3, 4]', 'E684:')
355  call assert_fails('let l[0:4] = [5, 6]', 'E711:')
356  call assert_fails('let l -= 2', 'E734:')
357  call assert_fails('let l += 2', 'E734:')
358  call assert_fails('let g:["a;b"] = 10', 'E461:')
359  call assert_fails('let g:.min = function("max")', 'E704:')
360  call assert_fails('let g:cos = "" | let g:.cos = {-> 42}', 'E704:')
361  if has('channel')
362    let ch = test_null_channel()
363    call assert_fails('let ch += 1', 'E734:')
364  endif
365  call assert_fails('let name = "a" .. "b",', 'E488: Trailing characters: ,')
366
367  " This test works only when the language is English
368  if v:lang == "C" || v:lang =~ '^[Ee]n'
369    call assert_fails('let [a ; b;] = [10, 20]',
370          \ 'Double ; in list of variables')
371  endif
372endfunc
373
374func Test_let_heredoc_fails()
375  call assert_fails('let v =<< marker', 'E991:')
376  try
377    exe "let v =<< TEXT | abc | TEXT"
378    call assert_report('No exception thrown')
379  catch /E488:/
380  catch
381    call assert_report('Caught exception: ' .. v:exception)
382  endtry
383
384  try
385    let &commentstring =<< trim TEXT
386      change
387      insert
388      append
389    TEXT
390    call assert_report('No exception thrown')
391  catch /E730:/
392  catch
393    call assert_report('Caught exception: ' .. v:exception)
394  endtry
395
396  try
397    let $SOME_ENV_VAR =<< trim TEXT
398      change
399      insert
400      append
401    TEXT
402    call assert_report('No exception thrown')
403  catch /E730:/
404  catch
405    call assert_report('Caught exception: ' .. v:exception)
406  endtry
407
408  try
409    let @r =<< trim TEXT
410      change
411      insert
412      append
413    TEXT
414    call assert_report('No exception thrown')
415  catch /E730:/
416  catch
417    call assert_report('Caught exception: ' .. v:exception)
418  endtry
419
420  try
421    let @- =<< trim TEXT
422      change
423      insert
424      append
425    TEXT
426    call assert_report('No exception thrown')
427  catch /E730:/
428  catch
429    call assert_report('Caught exception: ' .. v:exception)
430  endtry
431
432  try
433    let [] =<< trim TEXT
434    TEXT
435    call assert_report('No exception thrown')
436  catch /E475:/
437  catch
438    call assert_report('Caught exception: ' .. v:exception)
439  endtry
440
441  try
442    let [a b c] =<< trim TEXT
443    TEXT
444    call assert_report('No exception thrown')
445  catch /E475:/
446  catch
447    call assert_report('Caught exception: ' .. v:exception)
448  endtry
449
450  try
451    let [a; b; c] =<< trim TEXT
452    TEXT
453    call assert_report('No exception thrown')
454  catch /E452:/
455  catch
456    call assert_report('Caught exception: ' .. v:exception)
457  endtry
458
459  try
460    let v =<< trim trimm
461    trimm
462    call assert_report('No exception thrown')
463  catch /E221:/
464  catch
465    call assert_report('Caught exception: ' .. v:exception)
466  endtry
467
468  try
469    let v =<< trim trim evall
470    evall
471    call assert_report('No exception thrown')
472  catch /E221:/
473  catch
474    call assert_report('Caught exception: ' .. v:exception)
475  endtry
476
477  let text =<< trim END
478  func WrongSyntax()
479    let v =<< that there
480  endfunc
481  END
482  call writefile(text, 'XheredocFail')
483  call assert_fails('source XheredocFail', 'E1145:')
484  call delete('XheredocFail')
485
486  let text =<< trim CodeEnd
487  func MissingEnd()
488    let v =<< END
489  endfunc
490  CodeEnd
491  call writefile(text, 'XheredocWrong')
492  call assert_fails('source XheredocWrong', 'E1145:')
493  call delete('XheredocWrong')
494
495  let text =<< trim TEXTend
496    let v =<< " comment
497  TEXTend
498  call writefile(text, 'XheredocNoMarker')
499  call assert_fails('source XheredocNoMarker', 'E172:')
500  call delete('XheredocNoMarker')
501
502  let text =<< trim TEXTend
503    let v =<< text
504  TEXTend
505  call writefile(text, 'XheredocBadMarker')
506  call assert_fails('source XheredocBadMarker', 'E221:')
507  call delete('XheredocBadMarker')
508
509  call writefile(['let v =<< TEXT', 'abc'], 'XheredocMissingMarker')
510  call assert_fails('source XheredocMissingMarker', 'E990:')
511  call delete('XheredocMissingMarker')
512endfunc
513
514func Test_let_heredoc_trim_no_indent_marker()
515  let text =<< trim END
516  Text
517  with
518  indent
519END
520  call assert_equal(['Text', 'with', 'indent'], text)
521endfunc
522
523func Test_let_interpolated()
524  call assert_equal('{text}', $'{{text}}')
525  call assert_equal('{{text}}', $'{{{{text}}}}')
526  let text = 'text'
527  call assert_equal('text{{', $'{text .. "{{"}')
528  call assert_equal('text{{', $"{text .. '{{'}")
529  call assert_equal('text{{', $'{text .. '{{'}')
530  call assert_equal('text{{', $"{text .. "{{"}")
531endfunc
532
533" Test for the setting a variable using the heredoc syntax.
534" Keep near the end, this messes up highlighting.
535func Test_let_heredoc()
536  let var1 =<< END
537Some sample text
538	Text with indent
539  !@#$%^&*()-+_={}|[]\~`:";'<>?,./
540END
541
542  call assert_equal(["Some sample text", "\tText with indent", "  !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1)
543
544  let var2 =<< XXX
545Editor
546XXX
547  call assert_equal(['Editor'], var2)
548
549  let var3 =<<END
550END
551  call assert_equal([], var3)
552
553  let var3 =<<END
554vim
555
556end
557  END
558END 
559END
560  call assert_equal(['vim', '', 'end', '  END', 'END '], var3)
561
562	let var1 =<< trim END
563	Line1
564	  Line2
565		Line3
566	 END
567	END
568  call assert_equal(['Line1', '  Line2', "\tLine3", ' END'], var1)
569
570  let var1 =<< trim !!!
571	Line1
572	 line2
573		Line3
574	!!!
575  !!!
576  call assert_equal(['Line1', ' line2', "\tLine3", '!!!',], var1)
577
578  let var1 =<< trim XX
579    Line1
580  XX
581  call assert_equal(['Line1'], var1)
582
583  let var1 =<< trim XX " comment
584    Line1
585      Line2
586    Line3
587  XX
588  call assert_equal(['Line1', '  Line2', 'Line3'], var1)
589
590  " ignore "endfunc"
591  let var1 =<< END
592something
593endfunc
594END
595  call assert_equal(['something', 'endfunc'], var1)
596
597  " ignore "endfunc" with trim
598  let var1 =<< trim END
599  something
600  endfunc
601  END
602  call assert_equal(['something', 'endfunc'], var1)
603
604  " not concatenate lines
605  let var1 =<< END
606some
607  \thing
608  \ else
609END
610  call assert_equal(['some', '  \thing', '  \ else'], var1)
611
612  " ignore "python << xx"
613  let var1 =<<END
614something
615python << xx
616END
617  call assert_equal(['something', 'python << xx'], var1)
618
619  " ignore "python << xx" with trim
620  let var1 =<< trim END
621  something
622  python << xx
623  END
624  call assert_equal(['something', 'python << xx'], var1)
625
626  " ignore "append"
627  let var1 =<< E
628something
629app
630E
631  call assert_equal(['something', 'app'], var1)
632
633  " ignore "append" with trim
634  let var1 =<< trim END
635  something
636  app
637  END
638  call assert_equal(['something', 'app'], var1)
639
640  let check = []
641  if 0
642     let check =<< trim END
643       from heredoc
644     END
645  endif
646  call assert_equal([], check)
647
648  " unpack assignment
649  let [a, b, c] =<< END
650     x
651     \y
652     z
653END
654  call assert_equal(['     x', '     \y', '     z'], [a, b, c])
655
656  " unpack assignment without whitespace
657  let[a,b,c]=<<END
658change
659insert
660append
661END
662  call assert_equal(['change', 'insert', 'append'], [a, b, c])
663
664  " unpack assignment with semicolon
665  let [a; b] =<< END
666change
667insert
668append
669END
670  call assert_equal(['change', ['insert', 'append']], [a, b])
671
672  " unpack assignment with registers
673  let [@/, @", @-] =<< END
674change
675insert
676append
677END
678  call assert_equal(['change', 'insert', 'append'], [@/, @", @-])
679
680  " curly braces name and list slice assignment
681  let foo_3_bar = ['', '', '']
682  let foo_{1 + 2}_bar[ : ] =<< END
683change
684insert
685append
686END
687  call assert_equal(['change', 'insert', 'append'], foo_3_bar)
688
689  " dictionary key containing brackets and spaces
690  let d = {'abc] 123': 'baz'}
691  let d[d['abc] 123'] .. '{'] =<< END
692change
693insert
694append
695END
696  call assert_equal(['change', 'insert', 'append'], d['baz{'])
697endfunc
698
699" Test for evaluating Vim expressions in a heredoc using {expr}
700" Keep near the end, this messes up highlighting.
701func Test_let_heredoc_eval()
702  let str = ''
703  let code =<< trim eval END
704    let a = {5 + 10}
705    let b = {min([10, 6])} + {max([4, 6])}
706    {str}
707    let c = "abc{str}d"
708  END
709  call assert_equal(['let a = 15', 'let b = 6 + 6', '', 'let c = "abcd"'], code)
710
711  let $TESTVAR = "Hello"
712  let code =<< eval trim END
713    let s = "{$TESTVAR}"
714  END
715  call assert_equal(['let s = "Hello"'], code)
716
717  let code =<< eval END
718    let s = "{$TESTVAR}"
719END
720  call assert_equal(['    let s = "Hello"'], code)
721
722  let a = 10
723  let data =<< eval END
724{a}
725END
726  call assert_equal(['10'], data)
727
728  let x = 'X'
729  let code =<< eval trim END
730    let a = {{abc}}
731    let b = {x}
732    let c = {{
733  END
734  call assert_equal(['let a = {abc}', 'let b = X', 'let c = {'], code)
735
736  " Evaluate a dictionary
737  let d1 = #{a: 10, b: 'ss', c: {}}
738  let code =<< eval trim END
739    let d2 = {d1}
740  END
741  call assert_equal(["let d2 = {'a': 10, 'b': 'ss', 'c': {}}"], code)
742
743  " Empty dictionary
744  let d1 = {}
745  let code =<< eval trim END
746    let d2 = {d1}
747  END
748  call assert_equal(["let d2 = {}"], code)
749
750  " null dictionary
751  let d1 = v:_null_dict
752  let code =<< eval trim END
753    let d2 = {d1}
754  END
755  call assert_equal(["let d2 = {}"], code)
756
757  " Evaluate a List
758  let l1 = ['a', 'b', 'c']
759  let code =<< eval trim END
760    let l2 = {l1}
761  END
762  call assert_equal(["let l2 = ['a', 'b', 'c']"], code)
763
764  " Empty List
765  let l1 = []
766  let code =<< eval trim END
767    let l2 = {l1}
768  END
769  call assert_equal(["let l2 = []"], code)
770
771  " Null List
772  let l1 = v:_null_list
773  let code =<< eval trim END
774    let l2 = {l1}
775  END
776  call assert_equal(["let l2 = []"], code)
777
778  let code = 'xxx'
779  let code =<< eval trim END
780    let n = {5 +
781    6}
782  END
783  call assert_equal('xxx', code)
784
785  let code =<< eval trim END
786     let n = {min([1, 2]} + {max([3, 4])}
787  END
788  call assert_equal('xxx', code)
789
790  let lines =<< trim LINES
791      let text =<< eval trim END
792        let b = {
793      END
794  LINES
795  call CheckScriptFailure(lines, 'E1279:')
796
797  let lines =<< trim LINES
798      let text =<< eval trim END
799        let b = {abc
800      END
801  LINES
802  call CheckScriptFailure(lines, 'E1279:')
803
804  let lines =<< trim LINES
805      let text =<< eval trim END
806        let b = {}
807      END
808  LINES
809  call CheckScriptFailure(lines, 'E15:')
810
811  " Test for using heredoc in a single string using :execute or execute()
812  for [cmd, res] in items({
813      \ "let x =<< trim END\n  one\n  two\nEND": ['one', 'two'],
814      \ "let x =<< trim END\n  one\n    two\nEND": ['one', '  two'],
815      \ "  let x =<< trim END\n    one\n    two\n  END": ['one', 'two'],
816      \ "  let x =<< trim END\n    one\n      two\n  END": ['one', '  two'],
817      \ "let x =<< END\n  one\n  two\nEND": ['  one', '  two'],
818      \ "let x =<< END\none\ntwo\nEND": ['one', 'two'],
819      \ "let x =<< END \" comment\none\ntwo\nEND": ['one', 'two'],
820      \ })
821    execute cmd
822    call assert_equal(res, x)
823    unlet x
824    call assert_equal($"\n{string(res)}", execute($"{cmd}\necho x"))
825    unlet x
826  endfor
827  for [cmd, err] in items({
828      \ "let x =<<\none\ntwo": "E172:",
829      \ "let x =<< trim\n  one\n  two": "E172:",
830      \ "let x =<< end\none\ntwo\nend": "E221:",
831      \ "let x =<< END\none\ntwo": "E990: Missing end marker 'END'",
832      \ "let x =<< END !\none\ntwo\nEND": "E488: Trailing characters:  !",
833      \ "let x =<< eval END\none\ntwo{y}\nEND": "E121: Undefined variable: y",
834      \ })
835    call assert_fails('execute cmd', err)
836    call assert_fails('call execute(cmd)', err)
837  endfor
838
839  " skipped heredoc
840  if 0
841    let msg =<< trim eval END
842        n is: {n}
843    END
844  endif
845
846  " Test for sourcing a script containing a heredoc with invalid expression.
847  " Variable assignment should fail, if expression evaluation fails
848  new
849  let g:Xvar = 'test'
850  let g:b = 10
851  let lines =<< trim END
852    let Xvar =<< eval CODE
853    let a = 1
854    let b = {5+}
855    let c = 2
856    CODE
857    let g:Count += 1
858  END
859  call setline(1, lines)
860  let g:Count = 0
861  call assert_fails('source', 'E15:')
862  call assert_equal(1, g:Count)
863  call setline(3, 'let b = {abc}')
864  call assert_fails('source', 'E121:')
865  call assert_equal(2, g:Count)
866  call setline(3, 'let b = {abc} + {min([9, 4])} + 2')
867  call assert_fails('source', 'E121:')
868  call assert_equal(3, g:Count)
869  call assert_equal('test', g:Xvar)
870  call assert_equal(10, g:b)
871  bw!
872endfunc
873
874" vim: shiftwidth=2 sts=2 expandtab