test_let.vim 21 KB

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