blob: a90b55cb4e430026a4c633e0fbcdec11ebd0c484 [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
Yegappan Lakshmanand9b82cf2025-03-27 17:31:31 +010096 " 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
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200116 " 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:')
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200128endfunc
Bram Moolenaar31b81602019-02-10 22:14:27 +0100129
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:')
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100246 call assert_fails('call s:set_varg7(1)', 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +0100247 call assert_fails('call s:set_varg8(1)', 'E742:')
248 call s:set_varg9([0])
249endfunction
Bram Moolenaarf0908e62019-03-30 20:11:50 +0100250
251func Test_let_utf8_environment()
252 let $a = 'ĀĒĪŌŪあいうえお'
253 call assert_equal('ĀĒĪŌŪあいうえお', $a)
254endfunc
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200255
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100256func 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 " Terminal code
266 let old_t_te = &t_te
267 let &t_te = "\<Esc>[yes;"
268 call assert_match('t_te.*^[[yes;', execute("set termcap"))
269 let &t_te = old_t_te
270
271 if exists("+t_k1")
272 " Key code
273 let old_t_k1 = &t_k1
274 let &t_k1 = "that"
275 call assert_match('t_k1.*that', execute("set termcap"))
276 let &t_k1 = old_t_k1
277 endif
278
Bram Moolenaare2e40752020-09-04 21:18:46 +0200279 call assert_fails('let x = &t_xx', 'E113:')
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100280 let &t_xx = "yes"
281 call assert_equal("yes", &t_xx)
282 let &t_xx = ""
Bram Moolenaare2e40752020-09-04 21:18:46 +0200283 call assert_fails('let x = &t_xx', 'E113:')
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100284endfunc
285
286func Test_let_option_error()
287 let _w = &tw
288 let &tw = 80
zeertzjq4c7cb372023-06-14 16:39:54 +0100289 call assert_fails('let &tw .= 1', ['E734:', 'E734:'])
290 call assert_fails('let &tw .= []', ['E734:', 'E734:'])
291 call assert_fails('let &tw = []', ['E745:', 'E745:'])
292 call assert_fails('let &tw += []', ['E745:', 'E745:'])
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100293 call assert_equal(80, &tw)
294 let &tw = _w
295
zeertzjq4c7cb372023-06-14 16:39:54 +0100296 let _w = &autoread
297 let &autoread = 1
298 call assert_fails('let &autoread .= 1', ['E734:', 'E734:'])
299 call assert_fails('let &autoread .= []', ['E734:', 'E734:'])
300 call assert_fails('let &autoread = []', ['E745:', 'E745:'])
301 call assert_fails('let &autoread += []', ['E745:', 'E745:'])
302 call assert_equal(1, &autoread)
303 let &autoread = _w
304
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100305 let _w = &fillchars
306 let &fillchars = "vert:|"
zeertzjq4c7cb372023-06-14 16:39:54 +0100307 call assert_fails('let &fillchars += "diff:-"', ['E734:', 'E734:'])
308 call assert_fails('let &fillchars += []', ['E734:', 'E734:'])
309 call assert_fails('let &fillchars = []', ['E730:', 'E730:'])
310 call assert_fails('let &fillchars .= []', ['E730:', 'E730:'])
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100311 call assert_equal("vert:|", &fillchars)
312 let &fillchars = _w
zeertzjq4c7cb372023-06-14 16:39:54 +0100313
314 call assert_fails('let &nosuchoption = 1', ['E355:', 'E355:'])
315 call assert_fails('let &nosuchoption = ""', ['E355:', 'E355:'])
316 call assert_fails('let &nosuchoption = []', ['E355:', 'E355:'])
317 call assert_fails('let &t_xx = []', ['E730:', 'E730:'])
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100318endfunc
319
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100320" Errors with the :let statement
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100321func Test_let_errors()
322 let s = 'abcd'
323 call assert_fails('let s[1] = 5', 'E689:')
324
325 let l = [1, 2, 3]
326 call assert_fails('let l[:] = 5', 'E709:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100327
Bram Moolenaarb5217662021-08-14 14:27:30 +0200328 call assert_fails('let x:lnum=5', ['E121:', 'E121:'])
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100329 call assert_fails('let v:=5', 'E461:')
330 call assert_fails('let [a]', 'E474:')
331 call assert_fails('let [a, b] = [', 'E697:')
332 call assert_fails('let [a, b] = [10, 20', 'E696:')
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100333 call assert_fails('let [a, b] = 10', 'E1535:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100334 call assert_fails('let [a, , b] = [10, 20]', 'E475:')
335 call assert_fails('let [a, b&] = [10, 20]', 'E475:')
336 call assert_fails('let $ = 10', 'E475:')
337 call assert_fails('let $FOO[1] = "abc"', 'E18:')
338 call assert_fails('let &buftype[1] = "nofile"', 'E18:')
339 let s = "var"
340 let var = 1
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200341 call assert_fails('let var += [1,2]', 'E734:')
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200342 call assert_fails('let {s}.1 = 2', 'E1203:')
Bram Moolenaar8b633132020-03-20 18:20:51 +0100343 call assert_fails('let a[1] = 5', 'E121:')
344 let l = [[1,2]]
345 call assert_fails('let l[:][0] = [5]', 'E708:')
346 let d = {'k' : 4}
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200347 call assert_fails('let d.# = 5', 'E488:')
Bram Moolenaar58fb7c32021-04-05 20:59:41 +0200348 call assert_fails('let d.m += 5', 'E716:')
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200349 call assert_fails('let m = d[{]', 'E15:')
Bram Moolenaar8b633132020-03-20 18:20:51 +0100350 let l = [1, 2]
351 call assert_fails('let l[2] = 0', 'E684:')
352 call assert_fails('let l[0:1] = [1, 2, 3]', 'E710:')
353 call assert_fails('let l[-2:-3] = [3, 4]', 'E684:')
354 call assert_fails('let l[0:4] = [5, 6]', 'E711:')
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200355 call assert_fails('let l -= 2', 'E734:')
356 call assert_fails('let l += 2', 'E734:')
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200357 call assert_fails('let g:["a;b"] = 10', 'E461:')
358 call assert_fails('let g:.min = function("max")', 'E704:')
zeertzjq91c75d12022-11-05 20:21:58 +0000359 call assert_fails('let g:cos = "" | let g:.cos = {-> 42}', 'E704:')
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200360 if has('channel')
361 let ch = test_null_channel()
362 call assert_fails('let ch += 1', 'E734:')
363 endif
Bram Moolenaarfae55a92021-06-17 22:08:30 +0200364 call assert_fails('let name = "a" .. "b",', 'E488: Trailing characters: ,')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100365
366 " This test works only when the language is English
367 if v:lang == "C" || v:lang =~ '^[Ee]n'
368 call assert_fails('let [a ; b;] = [10, 20]',
369 \ 'Double ; in list of variables')
370 endif
Bram Moolenaarfcf8a872019-11-06 15:22:00 +0100371endfunc
372
Bram Moolenaar8471e572019-05-19 21:37:18 +0200373func Test_let_heredoc_fails()
374 call assert_fails('let v =<< marker', 'E991:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100375 try
376 exe "let v =<< TEXT | abc | TEXT"
377 call assert_report('No exception thrown')
378 catch /E488:/
379 catch
zeertzjqa93d9cd2023-05-02 16:25:47 +0100380 call assert_report('Caught exception: ' .. v:exception)
381 endtry
382
383 try
384 let &commentstring =<< trim TEXT
385 change
386 insert
387 append
388 TEXT
389 call assert_report('No exception thrown')
390 catch /E730:/
391 catch
392 call assert_report('Caught exception: ' .. v:exception)
393 endtry
394
395 try
396 let $SOME_ENV_VAR =<< trim TEXT
397 change
398 insert
399 append
400 TEXT
401 call assert_report('No exception thrown')
402 catch /E730:/
403 catch
404 call assert_report('Caught exception: ' .. v:exception)
405 endtry
406
407 try
408 let @r =<< trim TEXT
409 change
410 insert
411 append
412 TEXT
413 call assert_report('No exception thrown')
414 catch /E730:/
415 catch
416 call assert_report('Caught exception: ' .. v:exception)
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100417 endtry
Bram Moolenaar8471e572019-05-19 21:37:18 +0200418
zeertzjq9a91d2b2024-04-09 21:47:10 +0200419 try
420 let @- =<< trim TEXT
421 change
422 insert
423 append
424 TEXT
425 call assert_report('No exception thrown')
426 catch /E730:/
427 catch
428 call assert_report('Caught exception: ' .. v:exception)
429 endtry
430
431 try
zeertzjq1817ccd2024-04-10 17:37:47 +0200432 let [] =<< trim TEXT
433 TEXT
434 call assert_report('No exception thrown')
435 catch /E475:/
436 catch
437 call assert_report('Caught exception: ' .. v:exception)
438 endtry
439
440 try
zeertzjq9a91d2b2024-04-09 21:47:10 +0200441 let [a b c] =<< trim TEXT
zeertzjq9a91d2b2024-04-09 21:47:10 +0200442 TEXT
443 call assert_report('No exception thrown')
444 catch /E475:/
445 catch
446 call assert_report('Caught exception: ' .. v:exception)
447 endtry
448
449 try
450 let [a; b; c] =<< trim TEXT
zeertzjq9a91d2b2024-04-09 21:47:10 +0200451 TEXT
452 call assert_report('No exception thrown')
453 catch /E452:/
454 catch
455 call assert_report('Caught exception: ' .. v:exception)
456 endtry
457
zeertzjq449c2e52025-02-03 18:56:16 +0100458 try
459 let v =<< trim trimm
460 trimm
461 call assert_report('No exception thrown')
462 catch /E221:/
463 catch
464 call assert_report('Caught exception: ' .. v:exception)
465 endtry
466
467 try
468 let v =<< trim trim evall
469 evall
470 call assert_report('No exception thrown')
471 catch /E221:/
472 catch
473 call assert_report('Caught exception: ' .. v:exception)
474 endtry
475
Bram Moolenaar8471e572019-05-19 21:37:18 +0200476 let text =<< trim END
477 func WrongSyntax()
478 let v =<< that there
479 endfunc
480 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100481 call writefile(text, 'XheredocFail', 'D')
Bram Moolenaarb5b94802020-12-13 17:50:20 +0100482 call assert_fails('source XheredocFail', 'E1145:')
Bram Moolenaar8471e572019-05-19 21:37:18 +0200483
Bram Moolenaar24582002019-07-21 14:14:26 +0200484 let text =<< trim CodeEnd
Bram Moolenaar8471e572019-05-19 21:37:18 +0200485 func MissingEnd()
486 let v =<< END
487 endfunc
Bram Moolenaar24582002019-07-21 14:14:26 +0200488 CodeEnd
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100489 call writefile(text, 'XheredocWrong', 'D')
Bram Moolenaarb5b94802020-12-13 17:50:20 +0100490 call assert_fails('source XheredocWrong', 'E1145:')
Bram Moolenaar24582002019-07-21 14:14:26 +0200491
492 let text =<< trim TEXTend
493 let v =<< " comment
494 TEXTend
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100495 call writefile(text, 'XheredocNoMarker', 'D')
Bram Moolenaar24582002019-07-21 14:14:26 +0200496 call assert_fails('source XheredocNoMarker', 'E172:')
Bram Moolenaar24582002019-07-21 14:14:26 +0200497
498 let text =<< trim TEXTend
499 let v =<< text
500 TEXTend
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100501 call writefile(text, 'XheredocBadMarker', 'D')
Bram Moolenaar24582002019-07-21 14:14:26 +0200502 call assert_fails('source XheredocBadMarker', 'E221:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100503
Bram Moolenaar7dd5a782022-09-29 21:01:57 +0100504 call writefile(['let v =<< TEXT', 'abc'], 'XheredocMissingMarker', 'D')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100505 call assert_fails('source XheredocMissingMarker', 'E990:')
Bram Moolenaar8471e572019-05-19 21:37:18 +0200506endfunc
507
Bram Moolenaarecaa75b2019-07-21 23:04:21 +0200508func Test_let_heredoc_trim_no_indent_marker()
509 let text =<< trim END
510 Text
511 with
512 indent
513END
514 call assert_equal(['Text', 'with', 'indent'], text)
515endfunc
516
LemonBoy2eaef102022-05-06 13:14:50 +0100517func Test_let_interpolated()
518 call assert_equal('{text}', $'{{text}}')
519 call assert_equal('{{text}}', $'{{{{text}}}}')
520 let text = 'text'
521 call assert_equal('text{{', $'{text .. "{{"}')
522 call assert_equal('text{{', $"{text .. '{{'}")
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100523 call assert_equal('text{{', $'{text .. '{{'}')
524 call assert_equal('text{{', $"{text .. "{{"}")
LemonBoy2eaef102022-05-06 13:14:50 +0100525endfunc
526
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100527" Test for the setting a variable using the heredoc syntax.
528" Keep near the end, this messes up highlighting.
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200529func Test_let_heredoc()
530 let var1 =<< END
531Some sample text
532 Text with indent
533 !@#$%^&*()-+_={}|[]\~`:";'<>?,./
534END
535
536 call assert_equal(["Some sample text", "\tText with indent", " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1)
537
Bram Moolenaar24582002019-07-21 14:14:26 +0200538 let var2 =<< XXX
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200539Editor
Bram Moolenaar24582002019-07-21 14:14:26 +0200540XXX
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200541 call assert_equal(['Editor'], var2)
542
543 let var3 =<<END
544END
545 call assert_equal([], var3)
546
547 let var3 =<<END
548vim
549
550end
551 END
552END
553END
554 call assert_equal(['vim', '', 'end', ' END', 'END '], var3)
555
556 let var1 =<< trim END
557 Line1
558 Line2
559 Line3
560 END
561 END
562 call assert_equal(['Line1', ' Line2', "\tLine3", ' END'], var1)
563
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200564 let var1 =<< trim !!!
565 Line1
566 line2
567 Line3
568 !!!
569 !!!
570 call assert_equal(['Line1', ' line2', "\tLine3", '!!!',], var1)
571
Bram Moolenaar24582002019-07-21 14:14:26 +0200572 let var1 =<< trim XX
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200573 Line1
Bram Moolenaar24582002019-07-21 14:14:26 +0200574 XX
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200575 call assert_equal(['Line1'], var1)
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200576
zeertzjq1f5175d2024-04-13 17:52:26 +0200577 let var1 =<< trim XX " comment
578 Line1
579 Line2
580 Line3
581 XX
582 call assert_equal(['Line1', ' Line2', 'Line3'], var1)
583
Bram Moolenaar8471e572019-05-19 21:37:18 +0200584 " ignore "endfunc"
585 let var1 =<< END
586something
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200587endfunc
Bram Moolenaar8471e572019-05-19 21:37:18 +0200588END
589 call assert_equal(['something', 'endfunc'], var1)
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200590
Bram Moolenaar8471e572019-05-19 21:37:18 +0200591 " ignore "endfunc" with trim
592 let var1 =<< trim END
593 something
594 endfunc
595 END
596 call assert_equal(['something', 'endfunc'], var1)
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200597
Bram Moolenaare96a2492019-06-25 04:12:16 +0200598 " not concatenate lines
599 let var1 =<< END
600some
601 \thing
602 \ else
603END
604 call assert_equal(['some', ' \thing', ' \ else'], var1)
605
Bram Moolenaar8471e572019-05-19 21:37:18 +0200606 " ignore "python << xx"
607 let var1 =<<END
608something
609python << xx
610END
611 call assert_equal(['something', 'python << xx'], var1)
612
613 " ignore "python << xx" with trim
614 let var1 =<< trim END
615 something
616 python << xx
617 END
618 call assert_equal(['something', 'python << xx'], var1)
619
620 " ignore "append"
Bram Moolenaar24582002019-07-21 14:14:26 +0200621 let var1 =<< E
Bram Moolenaar8471e572019-05-19 21:37:18 +0200622something
623app
Bram Moolenaar24582002019-07-21 14:14:26 +0200624E
Bram Moolenaar8471e572019-05-19 21:37:18 +0200625 call assert_equal(['something', 'app'], var1)
626
627 " ignore "append" with trim
Bram Moolenaar24582002019-07-21 14:14:26 +0200628 let var1 =<< trim END
Bram Moolenaar8471e572019-05-19 21:37:18 +0200629 something
630 app
Bram Moolenaar24582002019-07-21 14:14:26 +0200631 END
Bram Moolenaar8471e572019-05-19 21:37:18 +0200632 call assert_equal(['something', 'app'], var1)
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200633
634 let check = []
635 if 0
636 let check =<< trim END
637 from heredoc
638 END
639 endif
640 call assert_equal([], check)
Bram Moolenaar1e673b92019-11-06 15:02:50 +0100641
642 " unpack assignment
643 let [a, b, c] =<< END
644 x
645 \y
646 z
647END
648 call assert_equal([' x', ' \y', ' z'], [a, b, c])
zeertzjqa93d9cd2023-05-02 16:25:47 +0100649
650 " unpack assignment without whitespace
651 let[a,b,c]=<<END
652change
653insert
654append
655END
656 call assert_equal(['change', 'insert', 'append'], [a, b, c])
657
zeertzjq9a91d2b2024-04-09 21:47:10 +0200658 " unpack assignment with semicolon
659 let [a; b] =<< END
660change
661insert
662append
663END
664 call assert_equal(['change', ['insert', 'append']], [a, b])
665
666 " unpack assignment with registers
667 let [@/, @", @-] =<< END
668change
669insert
670append
671END
672 call assert_equal(['change', 'insert', 'append'], [@/, @", @-])
673
zeertzjqa93d9cd2023-05-02 16:25:47 +0100674 " curly braces name and list slice assignment
675 let foo_3_bar = ['', '', '']
676 let foo_{1 + 2}_bar[ : ] =<< END
677change
678insert
679append
680END
681 call assert_equal(['change', 'insert', 'append'], foo_3_bar)
682
683 " dictionary key containing brackets and spaces
684 let d = {'abc] 123': 'baz'}
685 let d[d['abc] 123'] .. '{'] =<< END
686change
687insert
688append
689END
690 call assert_equal(['change', 'insert', 'append'], d['baz{'])
Bram Moolenaarf5842c52019-05-19 18:41:26 +0200691endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100692
LemonBoy2eaef102022-05-06 13:14:50 +0100693" Test for evaluating Vim expressions in a heredoc using {expr}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100694" Keep near the end, this messes up highlighting.
695func Test_let_heredoc_eval()
696 let str = ''
697 let code =<< trim eval END
LemonBoy2eaef102022-05-06 13:14:50 +0100698 let a = {5 + 10}
699 let b = {min([10, 6])} + {max([4, 6])}
700 {str}
701 let c = "abc{str}d"
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100702 END
703 call assert_equal(['let a = 15', 'let b = 6 + 6', '', 'let c = "abcd"'], code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100704
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100705 let $TESTVAR = "Hello"
706 let code =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100707 let s = "{$TESTVAR}"
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100708 END
709 call assert_equal(['let s = "Hello"'], code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100710
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100711 let code =<< eval END
LemonBoy2eaef102022-05-06 13:14:50 +0100712 let s = "{$TESTVAR}"
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100713END
714 call assert_equal([' let s = "Hello"'], code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100715
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100716 let a = 10
717 let data =<< eval END
LemonBoy2eaef102022-05-06 13:14:50 +0100718{a}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100719END
720 call assert_equal(['10'], data)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100721
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100722 let x = 'X'
723 let code =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100724 let a = {{abc}}
725 let b = {x}
726 let c = {{
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100727 END
LemonBoy2eaef102022-05-06 13:14:50 +0100728 call assert_equal(['let a = {abc}', 'let b = X', 'let c = {'], code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100729
Yegappan Lakshmananf01493c2024-04-14 23:21:02 +0200730 " Evaluate a dictionary
731 let d1 = #{a: 10, b: 'ss', c: {}}
732 let code =<< eval trim END
733 let d2 = {d1}
734 END
735 call assert_equal(["let d2 = {'a': 10, 'b': 'ss', 'c': {}}"], code)
736
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200737 " Empty dictionary
738 let d1 = {}
739 let code =<< eval trim END
740 let d2 = {d1}
741 END
742 call assert_equal(["let d2 = {}"], code)
743
744 " null dictionary
745 let d1 = test_null_dict()
746 let code =<< eval trim END
747 let d2 = {d1}
748 END
749 call assert_equal(["let d2 = {}"], code)
750
751 " Evaluate a List
752 let l1 = ['a', 'b', 'c']
753 let code =<< eval trim END
754 let l2 = {l1}
755 END
756 call assert_equal(["let l2 = ['a', 'b', 'c']"], code)
757
758 " Empty List
759 let l1 = []
760 let code =<< eval trim END
761 let l2 = {l1}
762 END
763 call assert_equal(["let l2 = []"], code)
764
765 " Null List
766 let l1 = test_null_list()
767 let code =<< eval trim END
768 let l2 = {l1}
769 END
770 call assert_equal(["let l2 = []"], code)
771
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100772 let code = 'xxx'
773 let code =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100774 let n = {5 +
775 6}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100776 END
777 call assert_equal('xxx', code)
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100778
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100779 let code =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100780 let n = {min([1, 2]} + {max([3, 4])}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100781 END
782 call assert_equal('xxx', code)
783
784 let lines =<< trim LINES
785 let text =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100786 let b = {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100787 END
788 LINES
LemonBoy2eaef102022-05-06 13:14:50 +0100789 call v9.CheckScriptFailure(lines, 'E1279:')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100790
791 let lines =<< trim LINES
792 let text =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100793 let b = {abc
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100794 END
795 LINES
LemonBoy2eaef102022-05-06 13:14:50 +0100796 call v9.CheckScriptFailure(lines, 'E1279:')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100797
798 let lines =<< trim LINES
799 let text =<< eval trim END
LemonBoy2eaef102022-05-06 13:14:50 +0100800 let b = {}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100801 END
802 LINES
803 call v9.CheckScriptFailure(lines, 'E15:')
804
zeertzjq3d936302024-04-14 18:49:56 +0200805 " Test for using heredoc in a single string using :execute or execute()
806 for [cmd, res] in items({
807 \ "let x =<< trim END\n one\n two\nEND": ['one', 'two'],
808 \ "let x =<< trim END\n one\n two\nEND": ['one', ' two'],
809 \ " let x =<< trim END\n one\n two\n END": ['one', 'two'],
810 \ " let x =<< trim END\n one\n two\n END": ['one', ' two'],
811 \ "let x =<< END\n one\n two\nEND": [' one', ' two'],
812 \ "let x =<< END\none\ntwo\nEND": ['one', 'two'],
813 \ "let x =<< END \" comment\none\ntwo\nEND": ['one', 'two'],
814 \ })
815 execute cmd
816 call assert_equal(res, x)
817 unlet x
818 call assert_equal($"\n{string(res)}", execute($"{cmd}\necho x"))
819 unlet x
820 endfor
821 for [cmd, err] in items({
822 \ "let x =<<\none\ntwo": "E172:",
823 \ "let x =<< trim\n one\n two": "E172:",
824 \ "let x =<< end\none\ntwo\nend": "E221:",
825 \ "let x =<< END\none\ntwo": "E990: Missing end marker 'END'",
826 \ "let x =<< END !\none\ntwo\nEND": "E488: Trailing characters: !",
827 \ "let x =<< eval END\none\ntwo{y}\nEND": "E121: Undefined variable: y",
828 \ })
829 call assert_fails('execute cmd', err)
830 call assert_fails('call execute(cmd)', err)
831 endfor
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200832
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100833 " skipped heredoc
834 if 0
835 let msg =<< trim eval END
LemonBoy2eaef102022-05-06 13:14:50 +0100836 n is: {n}
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100837 END
838 endif
839
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100840 " Test for sourcing a script containing a heredoc with invalid expression.
841 " Variable assignment should fail, if expression evaluation fails
842 new
843 let g:Xvar = 'test'
844 let g:b = 10
845 let lines =<< trim END
846 let Xvar =<< eval CODE
847 let a = 1
LemonBoy2eaef102022-05-06 13:14:50 +0100848 let b = {5+}
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100849 let c = 2
850 CODE
851 let g:Count += 1
852 END
853 call setline(1, lines)
854 let g:Count = 0
855 call assert_fails('source', 'E15:')
856 call assert_equal(1, g:Count)
LemonBoy2eaef102022-05-06 13:14:50 +0100857 call setline(3, 'let b = {abc}')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100858 call assert_fails('source', 'E121:')
859 call assert_equal(2, g:Count)
LemonBoy2eaef102022-05-06 13:14:50 +0100860 call setline(3, 'let b = {abc} + {min([9, 4])} + 2')
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100861 call assert_fails('source', 'E121:')
862 call assert_equal(3, g:Count)
863 call assert_equal('test', g:Xvar)
864 call assert_equal(10, g:b)
865 bw!
866endfunc
867
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100868" vim: shiftwidth=2 sts=2 expandtab