test_let.vim 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. let text =<< trim END
  372. func WrongSyntax()
  373. let v =<< that there
  374. endfunc
  375. END
  376. call writefile(text, 'XheredocFail')
  377. call assert_fails('source XheredocFail', 'E1145:')
  378. call delete('XheredocFail')
  379. let text =<< trim CodeEnd
  380. func MissingEnd()
  381. let v =<< END
  382. endfunc
  383. CodeEnd
  384. call writefile(text, 'XheredocWrong')
  385. call assert_fails('source XheredocWrong', 'E1145:')
  386. call delete('XheredocWrong')
  387. let text =<< trim TEXTend
  388. let v =<< " comment
  389. TEXTend
  390. call writefile(text, 'XheredocNoMarker')
  391. call assert_fails('source XheredocNoMarker', 'E172:')
  392. call delete('XheredocNoMarker')
  393. let text =<< trim TEXTend
  394. let v =<< text
  395. TEXTend
  396. call writefile(text, 'XheredocBadMarker')
  397. call assert_fails('source XheredocBadMarker', 'E221:')
  398. call delete('XheredocBadMarker')
  399. call writefile(['let v =<< TEXT', 'abc'], 'XheredocMissingMarker')
  400. call assert_fails('source XheredocMissingMarker', 'E990:')
  401. call delete('XheredocMissingMarker')
  402. endfunc
  403. func Test_let_heredoc_trim_no_indent_marker()
  404. let text =<< trim END
  405. Text
  406. with
  407. indent
  408. END
  409. call assert_equal(['Text', 'with', 'indent'], text)
  410. endfunc
  411. func Test_let_interpolated()
  412. call assert_equal('{text}', $'{{text}}')
  413. call assert_equal('{{text}}', $'{{{{text}}}}')
  414. let text = 'text'
  415. call assert_equal('text{{', $'{text .. "{{"}')
  416. call assert_equal('text{{', $"{text .. '{{'}")
  417. call assert_equal('text{{', $'{text .. '{{'}')
  418. call assert_equal('text{{', $"{text .. "{{"}")
  419. endfunc
  420. " Test for the setting a variable using the heredoc syntax.
  421. " Keep near the end, this messes up highlighting.
  422. func Test_let_heredoc()
  423. let var1 =<< END
  424. Some sample text
  425. Text with indent
  426. !@#$%^&*()-+_={}|[]\~`:";'<>?,./
  427. END
  428. call assert_equal(["Some sample text", "\tText with indent", " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./"], var1)
  429. let var2 =<< XXX
  430. Editor
  431. XXX
  432. call assert_equal(['Editor'], var2)
  433. let var3 =<<END
  434. END
  435. call assert_equal([], var3)
  436. let var3 =<<END
  437. vim
  438. end
  439. END
  440. END
  441. END
  442. call assert_equal(['vim', '', 'end', ' END', 'END '], var3)
  443. let var1 =<< trim END
  444. Line1
  445. Line2
  446. Line3
  447. END
  448. END
  449. call assert_equal(['Line1', ' Line2', "\tLine3", ' END'], var1)
  450. let var1 =<< trim !!!
  451. Line1
  452. line2
  453. Line3
  454. !!!
  455. !!!
  456. call assert_equal(['Line1', ' line2', "\tLine3", '!!!',], var1)
  457. let var1 =<< trim XX
  458. Line1
  459. XX
  460. call assert_equal(['Line1'], var1)
  461. let var1 =<< trim XX " comment
  462. Line1
  463. Line2
  464. Line3
  465. XX
  466. call assert_equal(['Line1', ' Line2', 'Line3'], var1)
  467. " ignore "endfunc"
  468. let var1 =<< END
  469. something
  470. endfunc
  471. END
  472. call assert_equal(['something', 'endfunc'], var1)
  473. " ignore "endfunc" with trim
  474. let var1 =<< trim END
  475. something
  476. endfunc
  477. END
  478. call assert_equal(['something', 'endfunc'], var1)
  479. " not concatenate lines
  480. let var1 =<< END
  481. some
  482. \thing
  483. \ else
  484. END
  485. call assert_equal(['some', ' \thing', ' \ else'], var1)
  486. " ignore "python << xx"
  487. let var1 =<<END
  488. something
  489. python << xx
  490. END
  491. call assert_equal(['something', 'python << xx'], var1)
  492. " ignore "python << xx" with trim
  493. let var1 =<< trim END
  494. something
  495. python << xx
  496. END
  497. call assert_equal(['something', 'python << xx'], var1)
  498. " ignore "append"
  499. let var1 =<< E
  500. something
  501. app
  502. E
  503. call assert_equal(['something', 'app'], var1)
  504. " ignore "append" with trim
  505. let var1 =<< trim END
  506. something
  507. app
  508. END
  509. call assert_equal(['something', 'app'], var1)
  510. let check = []
  511. if 0
  512. let check =<< trim END
  513. from heredoc
  514. END
  515. endif
  516. call assert_equal([], check)
  517. " unpack assignment
  518. let [a, b, c] =<< END
  519. x
  520. \y
  521. z
  522. END
  523. call assert_equal([' x', ' \y', ' z'], [a, b, c])
  524. " unpack assignment without whitespace
  525. let[a,b,c]=<<END
  526. change
  527. insert
  528. append
  529. END
  530. call assert_equal(['change', 'insert', 'append'], [a, b, c])
  531. " unpack assignment with semicolon
  532. let [a; b] =<< END
  533. change
  534. insert
  535. append
  536. END
  537. call assert_equal(['change', ['insert', 'append']], [a, b])
  538. " unpack assignment with registers
  539. let [@/, @", @-] =<< END
  540. change
  541. insert
  542. append
  543. END
  544. call assert_equal(['change', 'insert', 'append'], [@/, @", @-])
  545. " curly braces name and list slice assignment
  546. let foo_3_bar = ['', '', '']
  547. let foo_{1 + 2}_bar[ : ] =<< END
  548. change
  549. insert
  550. append
  551. END
  552. call assert_equal(['change', 'insert', 'append'], foo_3_bar)
  553. " dictionary key containing brackets and spaces
  554. let d = {'abc] 123': 'baz'}
  555. let d[d['abc] 123'] .. '{'] =<< END
  556. change
  557. insert
  558. append
  559. END
  560. call assert_equal(['change', 'insert', 'append'], d['baz{'])
  561. endfunc
  562. " Test for evaluating Vim expressions in a heredoc using {expr}
  563. " Keep near the end, this messes up highlighting.
  564. func Test_let_heredoc_eval()
  565. let str = ''
  566. let code =<< trim eval END
  567. let a = {5 + 10}
  568. let b = {min([10, 6])} + {max([4, 6])}
  569. {str}
  570. let c = "abc{str}d"
  571. END
  572. call assert_equal(['let a = 15', 'let b = 6 + 6', '', 'let c = "abcd"'], code)
  573. let $TESTVAR = "Hello"
  574. let code =<< eval trim END
  575. let s = "{$TESTVAR}"
  576. END
  577. call assert_equal(['let s = "Hello"'], code)
  578. let code =<< eval END
  579. let s = "{$TESTVAR}"
  580. END
  581. call assert_equal([' let s = "Hello"'], code)
  582. let a = 10
  583. let data =<< eval END
  584. {a}
  585. END
  586. call assert_equal(['10'], data)
  587. let x = 'X'
  588. let code =<< eval trim END
  589. let a = {{abc}}
  590. let b = {x}
  591. let c = {{
  592. END
  593. call assert_equal(['let a = {abc}', 'let b = X', 'let c = {'], code)
  594. " Evaluate a dictionary
  595. let d1 = #{a: 10, b: 'ss', c: {}}
  596. let code =<< eval trim END
  597. let d2 = {d1}
  598. END
  599. call assert_equal(["let d2 = {'a': 10, 'b': 'ss', 'c': {}}"], code)
  600. " Empty dictionary
  601. let d1 = {}
  602. let code =<< eval trim END
  603. let d2 = {d1}
  604. END
  605. call assert_equal(["let d2 = {}"], code)
  606. " null dictionary
  607. let d1 = v:_null_dict
  608. let code =<< eval trim END
  609. let d2 = {d1}
  610. END
  611. call assert_equal(["let d2 = {}"], code)
  612. " Evaluate a List
  613. let l1 = ['a', 'b', 'c']
  614. let code =<< eval trim END
  615. let l2 = {l1}
  616. END
  617. call assert_equal(["let l2 = ['a', 'b', 'c']"], code)
  618. " Empty List
  619. let l1 = []
  620. let code =<< eval trim END
  621. let l2 = {l1}
  622. END
  623. call assert_equal(["let l2 = []"], code)
  624. " Null List
  625. let l1 = v:_null_list
  626. let code =<< eval trim END
  627. let l2 = {l1}
  628. END
  629. call assert_equal(["let l2 = []"], code)
  630. let code = 'xxx'
  631. let code =<< eval trim END
  632. let n = {5 +
  633. 6}
  634. END
  635. call assert_equal('xxx', code)
  636. let code =<< eval trim END
  637. let n = {min([1, 2]} + {max([3, 4])}
  638. END
  639. call assert_equal('xxx', code)
  640. let lines =<< trim LINES
  641. let text =<< eval trim END
  642. let b = {
  643. END
  644. LINES
  645. call CheckScriptFailure(lines, 'E1279:')
  646. let lines =<< trim LINES
  647. let text =<< eval trim END
  648. let b = {abc
  649. END
  650. LINES
  651. call CheckScriptFailure(lines, 'E1279:')
  652. let lines =<< trim LINES
  653. let text =<< eval trim END
  654. let b = {}
  655. END
  656. LINES
  657. call CheckScriptFailure(lines, 'E15:')
  658. " Test for using heredoc in a single string using :execute or execute()
  659. for [cmd, res] in items({
  660. \ "let x =<< trim END\n one\n two\nEND": ['one', 'two'],
  661. \ "let x =<< trim END\n one\n two\nEND": ['one', ' two'],
  662. \ " let x =<< trim END\n one\n two\n END": ['one', 'two'],
  663. \ " let x =<< trim END\n one\n two\n END": ['one', ' two'],
  664. \ "let x =<< END\n one\n two\nEND": [' one', ' two'],
  665. \ "let x =<< END\none\ntwo\nEND": ['one', 'two'],
  666. \ "let x =<< END \" comment\none\ntwo\nEND": ['one', 'two'],
  667. \ })
  668. execute cmd
  669. call assert_equal(res, x)
  670. unlet x
  671. call assert_equal($"\n{string(res)}", execute($"{cmd}\necho x"))
  672. unlet x
  673. endfor
  674. for [cmd, err] in items({
  675. \ "let x =<<\none\ntwo": "E172:",
  676. \ "let x =<< trim\n one\n two": "E172:",
  677. \ "let x =<< end\none\ntwo\nend": "E221:",
  678. \ "let x =<< END\none\ntwo": "E990: Missing end marker 'END'",
  679. \ "let x =<< END !\none\ntwo\nEND": "E488: Trailing characters: !",
  680. \ "let x =<< eval END\none\ntwo{y}\nEND": "E121: Undefined variable: y",
  681. \ })
  682. call assert_fails('execute cmd', err)
  683. call assert_fails('call execute(cmd)', err)
  684. endfor
  685. " skipped heredoc
  686. if 0
  687. let msg =<< trim eval END
  688. n is: {n}
  689. END
  690. endif
  691. " Test for sourcing a script containing a heredoc with invalid expression.
  692. " Variable assignment should fail, if expression evaluation fails
  693. new
  694. let g:Xvar = 'test'
  695. let g:b = 10
  696. let lines =<< trim END
  697. let Xvar =<< eval CODE
  698. let a = 1
  699. let b = {5+}
  700. let c = 2
  701. CODE
  702. let g:Count += 1
  703. END
  704. call setline(1, lines)
  705. let g:Count = 0
  706. call assert_fails('source', 'E15:')
  707. call assert_equal(1, g:Count)
  708. call setline(3, 'let b = {abc}')
  709. call assert_fails('source', 'E121:')
  710. call assert_equal(2, g:Count)
  711. call setline(3, 'let b = {abc} + {min([9, 4])} + 2')
  712. call assert_fails('source', 'E121:')
  713. call assert_equal(3, g:Count)
  714. call assert_equal('test', g:Xvar)
  715. call assert_equal(10, g:b)
  716. bw!
  717. endfunc
  718. " vim: shiftwidth=2 sts=2 expandtab