blob: dffc5c69599847033eda393e124cb703829f07e1 [file] [log] [blame]
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001" Tests for the :let command.
2
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01003import './vim9.vim' as v9
4
Bram Moolenaar4a137b42017-08-04 22:37:11 +02005func 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
thinca6c667bd2022-09-02 11:25:37 +010011 let foo#tr = function('tr')
12 call assert_equal("function('tr')", string(foo#tr))
13 unlet foo#tr
14
Bram Moolenaar4a137b42017-08-04 22:37:11 +020015 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)
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +010033
Bram Moolenaar8dfcce32020-03-18 19:32:26 +010034 " 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
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +010058 let x = 0
59 if 0 | let x = 1 | endif
60 call assert_equal(0, x)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +010061
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:'))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +020090
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 errors in conditional expression
97 call assert_fails('let val = [] ? 1 : 2', 'E745:')
98 call assert_fails('let val = 1 ? 5+ : 6', 'E121:')
99 call assert_fails('let val = 1 ? 0 : 5+', 'E15:')
100 call assert_false(exists('val'))
101
102 " Test for errors in logical operators
103 let @a = 'if [] || 0 | let val = 2 | endif'
104 call assert_fails('exe @a', 'E745:')
105 call assert_fails('call feedkeys(":let val = 0 || []\<cr>", "xt")', 'E745:')
106 call assert_fails('exe "let val = [] && 5"', 'E745:')
107 call assert_fails('exe "let val = 6 && []"', 'E745:')
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200108endfunc
Bram Moolenaar31b81602019-02-10 22:14:27 +0100109
110func s:set_arg1(a) abort
111 let a:a = 1
112endfunction
113
114func s:set_arg2(a) abort
115 let a:b = 1
116endfunction
117
118func s:set_arg3(a) abort
119 let b = a:
120 let b['a'] = 1
121endfunction
122
123func s:set_arg4(a) abort
124 let b = a:
125 let b['a'] = 1
126endfunction
127
128func s:set_arg5(a) abort
129 let b = a:
130 let b['a'][0] = 1
131endfunction
132
133func s:set_arg6(a) abort
134 let a:a[0] = 1
135endfunction
136
137func s:set_arg7(a) abort
138 call extend(a:, {'a': 1})
139endfunction
140
141func s:set_arg8(a) abort
142 call extend(a:, {'b': 1})
143endfunction
144
145func s:set_arg9(a) abort
146 let a:['b'] = 1
147endfunction
148
149func s:set_arg10(a) abort
150 let b = a:
151 call extend(b, {'a': 1})
152endfunction
153
154func s:set_arg11(a) abort
155 let b = a:
156 call extend(b, {'b': 1})
157endfunction
158
159func s:set_arg12(a) abort
160 let b = a:
161 let b['b'] = 1
162endfunction
163
164func Test_let_arg_fail()
165 call assert_fails('call s:set_arg1(1)', 'E46:')
166 call assert_fails('call s:set_arg2(1)', 'E461:')
167 call assert_fails('call s:set_arg3(1)', 'E46:')
168 call assert_fails('call s:set_arg4(1)', 'E46:')
169 call assert_fails('call s:set_arg5(1)', 'E46:')
170 call s:set_arg6([0])
171 call assert_fails('call s:set_arg7(1)', 'E742:')
172 call assert_fails('call s:set_arg8(1)', 'E742:')
173 call assert_fails('call s:set_arg9(1)', 'E461:')
174 call assert_fails('call s:set_arg10(1)', 'E742:')
175 call assert_fails('call s:set_arg11(1)', 'E742:')
176 call assert_fails('call s:set_arg12(1)', 'E461:')
177endfunction
178
179func s:set_varg1(...) abort
180 let a:000 = []
181endfunction
182
183func s:set_varg2(...) abort
184 let a:000[0] = 1
185endfunction
186
187func s:set_varg3(...) abort
188 let a:000 += [1]
189endfunction
190
191func s:set_varg4(...) abort
192 call add(a:000, 1)
193endfunction
194
195func s:set_varg5(...) abort
196 let a:000[0][0] = 1
197endfunction
198
199func s:set_varg6(...) abort
200 let b = a:000
201 let b[0] = 1
202endfunction
203
204func s:set_varg7(...) abort
205 let b = a:000
206 let b += [1]
207endfunction
208
209func s:set_varg8(...) abort
210 let b = a:000
211 call add(b, 1)
212endfunction
213
214func s:set_varg9(...) abort
215 let b = a:000
216 let b[0][0] = 1
217endfunction
218
219func Test_let_varg_fail()
220 call assert_fails('call s:set_varg1(1)', 'E46:')
221 call assert_fails('call s:set_varg2(1)', 'E742:')
222 call assert_fails('call s:set_varg3(1)', 'E46:')
223 call assert_fails('call s:set_varg4(1)', 'E742:')
224 call s:set_varg5([0])
225 call assert_fails('call s:set_varg6(1)', 'E742:')
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100226 call assert_fails('call s:set_varg7(1)', 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +0100227 call assert_fails('call s:set_varg8(1)', 'E742:')
228 call s:set_varg9([0])
229endfunction
Bram Moolenaarf0908e62019-03-30 20:11:50 +0100230
231func Test_let_utf8_environment()
232 let $a = 'ĀĒĪŌŪあいうえお'
233 call assert_equal('ĀĒĪŌŪあいうえお', $a)
234endfunc
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200235
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100236func Test_let_no_type_checking()
237 let v = 1
238 let v = [1,2,3]
239 let v = {'a': 1, 'b': 2}
240 let v = 3.4
241 let v = 'hello'
242endfunc
243
244func Test_let_termcap()
245 " Terminal code
246 let old_t_te = &t_te
247 let &t_te = "\<Esc>[yes;"
248 call assert_match('t_te.*^[[yes;', execute("set termcap"))
249 let &t_te = old_t_te
250
251 if exists("+t_k1")
252 " Key code
253 let old_t_k1 = &t_k1
254 let &t_k1 = "that"
255 call assert_match('t_k1.*that', execute("set termcap"))
256 let &t_k1 = old_t_k1
257 endif
258
Bram Moolenaare2e40752020-09-04 21:18:46 +0200259 call assert_fails('let x = &t_xx', 'E113:')
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100260 let &t_xx = "yes"
261 call assert_equal("yes", &t_xx)
262 let &t_xx = ""
Bram Moolenaare2e40752020-09-04 21:18:46 +0200263 call assert_fails('let x = &t_xx', 'E113:')
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100264endfunc
265
266func Test_let_option_error()
267 let _w = &tw
268 let &tw = 80
zeertzjq4c7cb372023-06-14 16:39:54 +0100269 call assert_fails('let &tw .= 1', ['E734:', 'E734:'])
270 call assert_fails('let &tw .= []', ['E734:', 'E734:'])
271 call assert_fails('let &tw = []', ['E745:', 'E745:'])
272 call assert_fails('let &tw += []', ['E745:', 'E745:'])
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100273 call assert_equal(80, &tw)
274 let &tw = _w
275
zeertzjq4c7cb372023-06-14 16:39:54 +0100276 let _w = &autoread
277 let &autoread = 1
278 call assert_fails('let &autoread .= 1', ['E734:', 'E734:'])
279 call assert_fails('let &autoread .= []', ['E734:', 'E734:'])
280 call assert_fails('let &autoread = []', ['E745:', 'E745:'])
281 call assert_fails('let &autoread += []', ['E745:', 'E745:'])
282 call assert_equal(1, &autoread)
283 let &autoread = _w
284
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100285 let _w = &fillchars
286 let &fillchars = "vert:|"
zeertzjq4c7cb372023-06-14 16:39:54 +0100287 call assert_fails('let &fillchars += "diff:-"', ['E734:', 'E734:'])
288 call assert_fails('let &fillchars += []', ['E734:', 'E734:'])
289 call assert_fails('let &fillchars = []', ['E730:', 'E730:'])
290 call assert_fails('let &fillchars .= []', ['E730:', 'E730:'])
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100291 call assert_equal("vert:|", &fillchars)
292 let &fillchars = _w
zeertzjq4c7cb372023-06-14 16:39:54 +0100293
294 call assert_fails('let &nosuchoption = 1', ['E355:', 'E355:'])
295 call assert_fails('let &nosuchoption = ""', ['E355:', 'E355:'])
296 call assert_fails('let &nosuchoption = []', ['E355:', 'E355:'])
297 call assert_fails('let &t_xx = []', ['E730:', 'E730:'])
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100298endfunc
299
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100300" Errors with the :let statement
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100301func Test_let_errors()
302 let s = 'abcd'
303 call assert_fails('let s[1] = 5', 'E689:')
304
305 let l = [1, 2, 3]
306 call assert_fails('let l[:] = 5', 'E709:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100307
Bram Moolenaarb5217662021-08-14 14:27:30 +0200308 call assert_fails('let x:lnum=5', ['E121:', 'E121:'])
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100309 call assert_fails('let v:=5', 'E461:')
310 call assert_fails('let [a]', 'E474:')
311 call assert_fails('let [a, b] = [', 'E697:')
312 call assert_fails('let [a, b] = [10, 20', 'E696:')
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100313 call assert_fails('let [a, b] = 10', 'E1535:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100314 call assert_fails('let [a, , b] = [10, 20]', 'E475:')
315 call assert_fails('let [a, b&] = [10, 20]', 'E475:')
316 call assert_fails('let $ = 10', 'E475:')
317 call assert_fails('let $FOO[1] = "abc"', 'E18:')
318 call assert_fails('let &buftype[1] = "nofile"', 'E18:')
319 let s = "var"
320 let var = 1
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200321 call assert_fails('let var += [1,2]', 'E734:')
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200322 call assert_fails('let {s}.1 = 2', 'E1203:')
Bram Moolenaar8b633132020-03-20 18:20:51 +0100323 call assert_fails('let a[1] = 5', 'E121:')
324 let l = [[1,2]]
325 call assert_fails('let l[:][0] = [5]', 'E708:')
326 let d = {'k' : 4}
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200327 call assert_fails('let d.# = 5', 'E488:')
Bram Moolenaar58fb7c32021-04-05 20:59:41 +0200328 call assert_fails('let d.m += 5', 'E716:')
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200329 call assert_fails('let m = d[{]', 'E15:')
Bram Moolenaar8b633132020-03-20 18:20:51 +0100330 let l = [1, 2]
331 call assert_fails('let l[2] = 0', 'E684:')
332 call assert_fails('let l[0:1] = [1, 2, 3]', 'E710:')
333 call assert_fails('let l[-2:-3] = [3, 4]', 'E684:')
334 call assert_fails('let l[0:4] = [5, 6]', 'E711:')
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200335 call assert_fails('let l -= 2', 'E734:')
336 call assert_fails('let l += 2', 'E734:')
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200337 call assert_fails('let g:["a;b"] = 10', 'E461:')
338 call assert_fails('let g:.min = function("max")', 'E704:')
zeertzjq91c75d12022-11-05 20:21:58 +0000339 call assert_fails('let g:cos = "" | let g:.cos = {-> 42}', 'E704:')
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200340 if has('channel')
341 let ch = test_null_channel()
342 call assert_fails('let ch += 1', 'E734:')
343 endif
Bram Moolenaarfae55a92021-06-17 22:08:30 +0200344 call assert_fails('let name = "a" .. "b",', 'E488: Trailing characters: ,')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100345
346 " This test works only when the language is English
347 if v:lang == "C" || v:lang =~ '^[Ee]n'
348 call assert_fails('let [a ; b;] = [10, 20]',
349 \ 'Double ; in list of variables')
350 endif
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100351endfunc
352
Bram Moolenaar8471e572019-05-19 21:37:18 +0200353func Test_let_heredoc_fails()
354 call assert_fails('let v =<< marker', 'E991:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100355 try
356 exe "let v =<< TEXT | abc | TEXT"
357 call assert_report('No exception thrown')
358 catch /E488:/
359 catch
zeertzjqa93d9cd2023-05-02 16:25:47 +0100360 call assert_report('Caught exception: ' .. v:exception)
361 endtry
362
363 try
364 let &commentstring =<< trim TEXT
365 change
366 insert
367 append
368 TEXT
369 call assert_report('No exception thrown')
370 catch /E730:/
371 catch
372 call assert_report('Caught exception: ' .. v:exception)
373 endtry
374
375 try
376 let $SOME_ENV_VAR =<< trim TEXT
377 change
378 insert
379 append
380 TEXT
381 call assert_report('No exception thrown')
382 catch /E730:/
383 catch
384 call assert_report('Caught exception: ' .. v:exception)
385 endtry
386
387 try
388 let @r =<< trim TEXT
389 change
390 insert
391 append
392 TEXT
393 call assert_report('No exception thrown')
394 catch /E730:/
395 catch
396 call assert_report('Caught exception: ' .. v:exception)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100397 endtry
Bram Moolenaar8471e572019-05-19 21:37:18 +0200398
zeertzjq9a91d2b2024-04-09 21:47:10 +0200399 try
400 let @- =<< trim TEXT
401 change
402 insert
403 append
404 TEXT
405 call assert_report('No exception thrown')
406 catch /E730:/
407 catch
408 call assert_report('Caught exception: ' .. v:exception)
409 endtry
410
411 try
zeertzjq1817ccd2024-04-10 17:37:47 +0200412 let [] =<< trim TEXT
413 TEXT
414 call assert_report('No exception thrown')
415 catch /E475:/
416 catch
417 call assert_report('Caught exception: ' .. v:exception)
418 endtry
419
420 try
zeertzjq9a91d2b2024-04-09 21:47:10 +0200421 let [a b c] =<< trim TEXT
zeertzjq9a91d2b2024-04-09 21:47:10 +0200422 TEXT
423 call assert_report('No exception thrown')
424 catch /E475:/
425 catch
426 call assert_report('Caught exception: ' .. v:exception)
427 endtry
428
429 try
430 let [a; b; c] =<< trim TEXT
zeertzjq9a91d2b2024-04-09 21:47:10 +0200431 TEXT
432 call assert_report('No exception thrown')
433 catch /E452:/
434 catch
435 call assert_report('Caught exception: ' .. v:exception)
436 endtry
437
zeertzjq449c2e52025-02-03 18:56:16 +0100438 try
439 let v =<< trim trimm
440 trimm
441 call assert_report('No exception thrown')
442 catch /E221:/
443 catch
444 call assert_report('Caught exception: ' .. v:exception)
445 endtry
446
447 try
448 let v =<< trim trim evall
449 evall
450 call assert_report('No exception thrown')
451 catch /E221:/
452 catch
453 call assert_report('Caught exception: ' .. v:exception)
454 endtry
455
Bram Moolenaar8471e572019-05-19 21:37:18 +0200456 let text =<< trim END
457 func WrongSyntax()
458 let v =<< that there
459 endfunc
460 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100461 call writefile(text, 'XheredocFail', 'D')
Bram Moolenaarb5b94802020-12-13 17:50:20 +0100462 call assert_fails('source XheredocFail', 'E1145:')
Bram Moolenaar8471e572019-05-19 21:37:18 +0200463
Bram Moolenaar24582002019-07-21 14:14:26 +0200464 let text =<< trim CodeEnd
Bram Moolenaar8471e572019-05-19 21:37:18 +0200465 func MissingEnd()
466 let v =<< END
467 endfunc
Bram Moolenaar24582002019-07-21 14:14:26 +0200468 CodeEnd
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100469 call writefile(text, 'XheredocWrong', 'D')
Bram Moolenaarb5b94802020-12-13 17:50:20 +0100470 call assert_fails('source XheredocWrong', 'E1145:')
Bram Moolenaar24582002019-07-21 14:14:26 +0200471
472 let text =<< trim TEXTend
473 let v =<< " comment
474 TEXTend
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100475 call writefile(text, 'XheredocNoMarker', 'D')
Bram Moolenaar24582002019-07-21 14:14:26 +0200476 call assert_fails('source XheredocNoMarker', 'E172:')
Bram Moolenaar24582002019-07-21 14:14:26 +0200477
478 let text =<< trim TEXTend
479 let v =<< text
480 TEXTend
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100481 call writefile(text, 'XheredocBadMarker', 'D')
Bram Moolenaar24582002019-07-21 14:14:26 +0200482 call assert_fails('source XheredocBadMarker', 'E221:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100483
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100484 call writefile(['let v =<< TEXT', 'abc'], 'XheredocMissingMarker', 'D')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100485 call assert_fails('source XheredocMissingMarker', 'E990:')
Bram Moolenaar8471e572019-05-19 21:37:18 +0200486endfunc
487
Bram Moolenaarecaa75b2019-07-21 23:04:21 +0200488func Test_let_heredoc_trim_no_indent_marker()
489 let text =<< trim END
490 Text
491 with
492 indent
493END
494 call assert_equal(['Text', 'with', 'indent'], text)
495endfunc
496
LemonBoy2eaef102022-05-06 13:14:50 +0100497func Test_let_interpolated()
498 call assert_equal('{text}', $'{{text}}')
499 call assert_equal('{{text}}', $'{{{{text}}}}')
500 let text = 'text'
501 call assert_equal('text{{', $'{text .. "{{"}')
502 call assert_equal('text{{', $"{text .. '{{'}")
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100503 call assert_equal('text{{', $'{text .. '{{'}')
504 call assert_equal('text{{', $"{text .. "{{"}")
LemonBoy2eaef102022-05-06 13:14:50 +0100505endfunc
506
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100507" Test for the setting a variable using the heredoc syntax.
508" Keep near the end, this messes up highlighting.
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200509func Test_let_heredoc()
510 let var1 =<< END
511Some sample text
512 Text with indent
513 !@#$%^&*()-+_={}|[]\~`:";'<>?,./
514END
515
516 call assert_equal(["Some sample text", "\tText with indent", " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1)
517
Bram Moolenaar24582002019-07-21 14:14:26 +0200518 let var2 =<< XXX
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200519Editor
Bram Moolenaar24582002019-07-21 14:14:26 +0200520XXX
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200521 call assert_equal(['Editor'], var2)
522
523 let var3 =<<END
524END
525 call assert_equal([], var3)
526
527 let var3 =<<END
528vim
529
530end
531 END
532END
533END
534 call assert_equal(['vim', '', 'end', ' END', 'END '], var3)
535
536 let var1 =<< trim END
537 Line1
538 Line2
539 Line3
540 END
541 END
542 call assert_equal(['Line1', ' Line2', "\tLine3", ' END'], var1)
543
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200544 let var1 =<< trim !!!
545 Line1
546 line2
547 Line3
548 !!!
549 !!!
550 call assert_equal(['Line1', ' line2', "\tLine3", '!!!',], var1)
551
Bram Moolenaar24582002019-07-21 14:14:26 +0200552 let var1 =<< trim XX
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200553 Line1
Bram Moolenaar24582002019-07-21 14:14:26 +0200554 XX
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200555 call assert_equal(['Line1'], var1)
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200556
zeertzjq1f5175d2024-04-13 17:52:26 +0200557 let var1 =<< trim XX " comment
558 Line1
559 Line2
560 Line3
561 XX
562 call assert_equal(['Line1', ' Line2', 'Line3'], var1)
563
Bram Moolenaar8471e572019-05-19 21:37:18 +0200564 " ignore "endfunc"
565 let var1 =<< END
566something
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200567endfunc
Bram Moolenaar8471e572019-05-19 21:37:18 +0200568END
569 call assert_equal(['something', 'endfunc'], var1)
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200570
Bram Moolenaar8471e572019-05-19 21:37:18 +0200571 " ignore "endfunc" with trim
572 let var1 =<< trim END
573 something
574 endfunc
575 END
576 call assert_equal(['something', 'endfunc'], var1)
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200577
Bram Moolenaare96a2492019-06-25 04:12:16 +0200578 " not concatenate lines
579 let var1 =<< END
580some
581 \thing
582 \ else
583END
584 call assert_equal(['some', ' \thing', ' \ else'], var1)
585
Bram Moolenaar8471e572019-05-19 21:37:18 +0200586 " ignore "python << xx"
587 let var1 =<<END
588something
589python << xx
590END
591 call assert_equal(['something', 'python << xx'], var1)
592
593 " ignore "python << xx" with trim
594 let var1 =<< trim END
595 something
596 python << xx
597 END
598 call assert_equal(['something', 'python << xx'], var1)
599
600 " ignore "append"
Bram Moolenaar24582002019-07-21 14:14:26 +0200601 let var1 =<< E
Bram Moolenaar8471e572019-05-19 21:37:18 +0200602something
603app
Bram Moolenaar24582002019-07-21 14:14:26 +0200604E
Bram Moolenaar8471e572019-05-19 21:37:18 +0200605 call assert_equal(['something', 'app'], var1)
606
607 " ignore "append" with trim
Bram Moolenaar24582002019-07-21 14:14:26 +0200608 let var1 =<< trim END
Bram Moolenaar8471e572019-05-19 21:37:18 +0200609 something
610 app
Bram Moolenaar24582002019-07-21 14:14:26 +0200611 END
Bram Moolenaar8471e572019-05-19 21:37:18 +0200612 call assert_equal(['something', 'app'], var1)
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200613
614 let check = []
615 if 0
616 let check =<< trim END
617 from heredoc
618 END
619 endif
620 call assert_equal([], check)
Bram Moolenaar1e673b92019-11-06 15:02:50 +0100621
622 " unpack assignment
623 let [a, b, c] =<< END
624 x
625 \y
626 z
627END
628 call assert_equal([' x', ' \y', ' z'], [a, b, c])
zeertzjqa93d9cd2023-05-02 16:25:47 +0100629
630 " unpack assignment without whitespace
631 let[a,b,c]=<<END
632change
633insert
634append
635END
636 call assert_equal(['change', 'insert', 'append'], [a, b, c])
637
zeertzjq9a91d2b2024-04-09 21:47:10 +0200638 " unpack assignment with semicolon
639 let [a; b] =<< END
640change
641insert
642append
643END
644 call assert_equal(['change', ['insert', 'append']], [a, b])
645
646 " unpack assignment with registers
647 let [@/, @", @-] =<< END
648change
649insert
650append
651END
652 call assert_equal(['change', 'insert', 'append'], [@/, @", @-])
653
zeertzjqa93d9cd2023-05-02 16:25:47 +0100654 " curly braces name and list slice assignment
655 let foo_3_bar = ['', '', '']
656 let foo_{1 + 2}_bar[ : ] =<< END
657change
658insert
659append
660END
661 call assert_equal(['change', 'insert', 'append'], foo_3_bar)
662
663 " dictionary key containing brackets and spaces
664 let d = {'abc] 123': 'baz'}
665 let d[d['abc] 123'] .. '{'] =<< END
666change
667insert
668append
669END
670 call assert_equal(['change', 'insert', 'append'], d['baz{'])
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200671endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100672
LemonBoy2eaef102022-05-06 13:14:50 +0100673" Test for evaluating Vim expressions in a heredoc using {expr}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100674" Keep near the end, this messes up highlighting.
675func Test_let_heredoc_eval()
676 let str = ''
677 let code =<< trim eval END
LemonBoy2eaef102022-05-06 13:14:50 +0100678 let a = {5 + 10}
679 let b = {min([10, 6])} + {max([4, 6])}
680 {str}
681 let c = "abc{str}d"
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100682 END
683 call assert_equal(['let a = 15', 'let b = 6 + 6', '', 'let c = "abcd"'], code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100684
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100685 let $TESTVAR = "Hello"
686 let code =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100687 let s = "{$TESTVAR}"
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100688 END
689 call assert_equal(['let s = "Hello"'], code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100690
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100691 let code =<< eval END
LemonBoy2eaef102022-05-06 13:14:50 +0100692 let s = "{$TESTVAR}"
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100693END
694 call assert_equal([' let s = "Hello"'], code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100695
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100696 let a = 10
697 let data =<< eval END
LemonBoy2eaef102022-05-06 13:14:50 +0100698{a}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100699END
700 call assert_equal(['10'], data)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100701
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100702 let x = 'X'
703 let code =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100704 let a = {{abc}}
705 let b = {x}
706 let c = {{
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100707 END
LemonBoy2eaef102022-05-06 13:14:50 +0100708 call assert_equal(['let a = {abc}', 'let b = X', 'let c = {'], code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100709
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +0200710 " Evaluate a dictionary
711 let d1 = #{a: 10, b: 'ss', c: {}}
712 let code =<< eval trim END
713 let d2 = {d1}
714 END
715 call assert_equal(["let d2 = {'a': 10, 'b': 'ss', 'c': {}}"], code)
716
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200717 " Empty dictionary
718 let d1 = {}
719 let code =<< eval trim END
720 let d2 = {d1}
721 END
722 call assert_equal(["let d2 = {}"], code)
723
724 " null dictionary
725 let d1 = test_null_dict()
726 let code =<< eval trim END
727 let d2 = {d1}
728 END
729 call assert_equal(["let d2 = {}"], code)
730
731 " Evaluate a List
732 let l1 = ['a', 'b', 'c']
733 let code =<< eval trim END
734 let l2 = {l1}
735 END
736 call assert_equal(["let l2 = ['a', 'b', 'c']"], code)
737
738 " Empty List
739 let l1 = []
740 let code =<< eval trim END
741 let l2 = {l1}
742 END
743 call assert_equal(["let l2 = []"], code)
744
745 " Null List
746 let l1 = test_null_list()
747 let code =<< eval trim END
748 let l2 = {l1}
749 END
750 call assert_equal(["let l2 = []"], code)
751
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100752 let code = 'xxx'
753 let code =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100754 let n = {5 +
755 6}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100756 END
757 call assert_equal('xxx', code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100758
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100759 let code =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100760 let n = {min([1, 2]} + {max([3, 4])}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100761 END
762 call assert_equal('xxx', code)
763
764 let lines =<< trim LINES
765 let text =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100766 let b = {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100767 END
768 LINES
LemonBoy2eaef102022-05-06 13:14:50 +0100769 call v9.CheckScriptFailure(lines, 'E1279:')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100770
771 let lines =<< trim LINES
772 let text =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100773 let b = {abc
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100774 END
775 LINES
LemonBoy2eaef102022-05-06 13:14:50 +0100776 call v9.CheckScriptFailure(lines, 'E1279:')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100777
778 let lines =<< trim LINES
779 let text =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100780 let b = {}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100781 END
782 LINES
783 call v9.CheckScriptFailure(lines, 'E15:')
784
zeertzjq3d936302024-04-14 18:49:56 +0200785 " Test for using heredoc in a single string using :execute or execute()
786 for [cmd, res] in items({
787 \ "let x =<< trim END\n one\n two\nEND": ['one', 'two'],
788 \ "let x =<< trim END\n one\n two\nEND": ['one', ' two'],
789 \ " let x =<< trim END\n one\n two\n END": ['one', 'two'],
790 \ " let x =<< trim END\n one\n two\n END": ['one', ' two'],
791 \ "let x =<< END\n one\n two\nEND": [' one', ' two'],
792 \ "let x =<< END\none\ntwo\nEND": ['one', 'two'],
793 \ "let x =<< END \" comment\none\ntwo\nEND": ['one', 'two'],
794 \ })
795 execute cmd
796 call assert_equal(res, x)
797 unlet x
798 call assert_equal($"\n{string(res)}", execute($"{cmd}\necho x"))
799 unlet x
800 endfor
801 for [cmd, err] in items({
802 \ "let x =<<\none\ntwo": "E172:",
803 \ "let x =<< trim\n one\n two": "E172:",
804 \ "let x =<< end\none\ntwo\nend": "E221:",
805 \ "let x =<< END\none\ntwo": "E990: Missing end marker 'END'",
806 \ "let x =<< END !\none\ntwo\nEND": "E488: Trailing characters: !",
807 \ "let x =<< eval END\none\ntwo{y}\nEND": "E121: Undefined variable: y",
808 \ })
809 call assert_fails('execute cmd', err)
810 call assert_fails('call execute(cmd)', err)
811 endfor
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200812
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100813 " skipped heredoc
814 if 0
815 let msg =<< trim eval END
LemonBoy2eaef102022-05-06 13:14:50 +0100816 n is: {n}
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100817 END
818 endif
819
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100820 " Test for sourcing a script containing a heredoc with invalid expression.
821 " Variable assignment should fail, if expression evaluation fails
822 new
823 let g:Xvar = 'test'
824 let g:b = 10
825 let lines =<< trim END
826 let Xvar =<< eval CODE
827 let a = 1
LemonBoy2eaef102022-05-06 13:14:50 +0100828 let b = {5+}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100829 let c = 2
830 CODE
831 let g:Count += 1
832 END
833 call setline(1, lines)
834 let g:Count = 0
835 call assert_fails('source', 'E15:')
836 call assert_equal(1, g:Count)
LemonBoy2eaef102022-05-06 13:14:50 +0100837 call setline(3, 'let b = {abc}')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100838 call assert_fails('source', 'E121:')
839 call assert_equal(2, g:Count)
LemonBoy2eaef102022-05-06 13:14:50 +0100840 call setline(3, 'let b = {abc} + {min([9, 4])} + 2')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100841 call assert_fails('source', 'E121:')
842 call assert_equal(3, g:Count)
843 call assert_equal('test', g:Xvar)
844 call assert_equal(10, g:b)
845 bw!
846endfunc
847
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100848" vim: shiftwidth=2 sts=2 expandtab