test_expr.vim 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. " Tests for expressions.
  2. source check.vim
  3. source vim9.vim
  4. func Test_equal()
  5. let base = {}
  6. func base.method()
  7. return 1
  8. endfunc
  9. func base.other() dict
  10. return 1
  11. endfunc
  12. let instance = copy(base)
  13. call assert_true(base.method == instance.method)
  14. call assert_true([base.method] == [instance.method])
  15. call assert_true(base.other == instance.other)
  16. call assert_true([base.other] == [instance.other])
  17. call assert_false(base.method == base.other)
  18. call assert_false([base.method] == [base.other])
  19. call assert_false(base.method == instance.other)
  20. call assert_false([base.method] == [instance.other])
  21. call assert_fails('echo base.method > instance.method')
  22. " Nvim doesn't have null functions
  23. " call assert_equal(0, test_null_function() == function('min'))
  24. " call assert_equal(1, test_null_function() == test_null_function())
  25. " Nvim doesn't have test_unknown()
  26. " call assert_fails('eval 10 == test_unknown()', 'E685:')
  27. endfunc
  28. func Test_version()
  29. call assert_true(has('patch-7.4.001'))
  30. call assert_true(has('patch-7.4.01'))
  31. call assert_true(has('patch-7.4.1'))
  32. call assert_true(has('patch-6.9.999'))
  33. call assert_true(has('patch-7.1.999'))
  34. call assert_true(has('patch-7.4.123'))
  35. call assert_false(has('patch-7'))
  36. call assert_false(has('patch-7.4'))
  37. call assert_false(has('patch-7.4.'))
  38. call assert_false(has('patch-9.1.0'))
  39. call assert_false(has('patch-9.9.1'))
  40. endfunc
  41. func Test_op_ternary()
  42. let lines =<< trim END
  43. call assert_equal('yes', 1 ? 'yes' : 'no')
  44. call assert_equal('no', 0 ? 'yes' : 'no')
  45. call assert_fails('echo [1] ? "yes" : "no"', 'E745:')
  46. call assert_fails('echo {} ? "yes" : "no"', 'E728:')
  47. END
  48. call CheckLegacyAndVim9Success(lines)
  49. call assert_equal('no', 'x' ? 'yes' : 'no')
  50. call CheckDefAndScriptFailure(["'x' ? 'yes' : 'no'"], 'E1135:')
  51. call assert_equal('yes', '1x' ? 'yes' : 'no')
  52. call CheckDefAndScriptFailure(["'1x' ? 'yes' : 'no'"], 'E1135:')
  53. endfunc
  54. func Test_op_falsy()
  55. let lines =<< trim END
  56. call assert_equal(v:true, v:true ?? 456)
  57. call assert_equal(123, 123 ?? 456)
  58. call assert_equal('yes', 'yes' ?? 456)
  59. call assert_equal(0z00, 0z00 ?? 456)
  60. call assert_equal([1], [1] ?? 456)
  61. call assert_equal({'one': 1}, {'one': 1} ?? 456)
  62. call assert_equal(0.1, 0.1 ?? 456)
  63. call assert_equal(456, v:false ?? 456)
  64. call assert_equal(456, 0 ?? 456)
  65. call assert_equal(456, '' ?? 456)
  66. call assert_equal(456, 0z ?? 456)
  67. call assert_equal(456, [] ?? 456)
  68. call assert_equal(456, {} ?? 456)
  69. call assert_equal(456, 0.0 ?? 456)
  70. call assert_equal(456, v:null ?? 456)
  71. #" call assert_equal(456, v:none ?? 456)
  72. call assert_equal(456, v:_null_string ?? 456)
  73. call assert_equal(456, v:_null_blob ?? 456)
  74. call assert_equal(456, v:_null_list ?? 456)
  75. call assert_equal(456, v:_null_dict ?? 456)
  76. #" Nvim doesn't have null functions
  77. #" call assert_equal(456, test_null_function() ?? 456)
  78. #" Nvim doesn't have null partials
  79. #" call assert_equal(456, test_null_partial() ?? 456)
  80. if has('job')
  81. call assert_equal(456, test_null_job() ?? 456)
  82. endif
  83. if has('channel')
  84. call assert_equal(456, test_null_channel() ?? 456)
  85. endif
  86. END
  87. call CheckLegacyAndVim9Success(lines)
  88. endfunc
  89. func Test_dict()
  90. let lines =<< trim END
  91. VAR d = {'': 'empty', 'a': 'a', 0: 'zero'}
  92. call assert_equal('empty', d[''])
  93. call assert_equal('a', d['a'])
  94. call assert_equal('zero', d[0])
  95. call assert_true(has_key(d, ''))
  96. call assert_true(has_key(d, 'a'))
  97. LET d[''] = 'none'
  98. LET d['a'] = 'aaa'
  99. call assert_equal('none', d[''])
  100. call assert_equal('aaa', d['a'])
  101. LET d[ 'b' ] = 'bbb'
  102. call assert_equal('bbb', d[ 'b' ])
  103. END
  104. call CheckLegacyAndVim9Success(lines)
  105. call CheckLegacyAndVim9Failure(["VAR i = has_key([], 'a')"], ['E1206:', 'E1013:', 'E1206:'])
  106. endfunc
  107. func Test_strgetchar()
  108. let lines =<< trim END
  109. call assert_equal(char2nr('a'), strgetchar('axb', 0))
  110. call assert_equal(char2nr('x'), 'axb'->strgetchar(1))
  111. call assert_equal(char2nr('b'), strgetchar('axb', 2))
  112. call assert_equal(-1, strgetchar('axb', -1))
  113. call assert_equal(-1, strgetchar('axb', 3))
  114. call assert_equal(-1, strgetchar('', 0))
  115. END
  116. call CheckLegacyAndVim9Success(lines)
  117. call CheckLegacyAndVim9Failure(["VAR c = strgetchar([], 1)"], ['E730:', 'E1013:', 'E1174:'])
  118. call CheckLegacyAndVim9Failure(["VAR c = strgetchar('axb', [])"], ['E745:', 'E1013:', 'E1210:'])
  119. endfunc
  120. func Test_strcharpart()
  121. let lines =<< trim END
  122. call assert_equal('a', strcharpart('axb', 0, 1))
  123. call assert_equal('x', 'axb'->strcharpart(1, 1))
  124. call assert_equal('b', strcharpart('axb', 2, 1))
  125. call assert_equal('xb', strcharpart('axb', 1))
  126. call assert_equal('', strcharpart('axb', 1, 0))
  127. call assert_equal('', strcharpart('axb', 1, -1))
  128. call assert_equal('', strcharpart('axb', -1, 1))
  129. call assert_equal('', strcharpart('axb', -2, 2))
  130. call assert_equal('a', strcharpart('axb', -1, 2))
  131. call assert_equal('edit', "editor"[-10 : 3])
  132. END
  133. call CheckLegacyAndVim9Success(lines)
  134. call assert_fails('call strcharpart("", 0, 0, {})', ['E728:', 'E728:'])
  135. call assert_fails('call strcharpart("", 0, 0, -1)', ['E1023:', 'E1023:'])
  136. endfunc
  137. func Test_getreg_empty_list()
  138. let lines =<< trim END
  139. call assert_equal('', getreg('x'))
  140. call assert_equal([], getreg('x', 1, 1))
  141. VAR x = getreg('x', 1, 1)
  142. VAR y = x
  143. call add(x, 'foo')
  144. call assert_equal(['foo'], y)
  145. END
  146. call CheckLegacyAndVim9Success(lines)
  147. call CheckLegacyAndVim9Failure(['call getreg([])'], ['E730:', 'E1013:', 'E1174:'])
  148. endfunc
  149. func Test_loop_over_null_list()
  150. let lines =<< trim END
  151. VAR null_list = v:_null_list
  152. for i in null_list
  153. call assert_report('should not get here')
  154. endfor
  155. END
  156. call CheckLegacyAndVim9Success(lines)
  157. endfunc
  158. func Test_setreg_null_list()
  159. let lines =<< trim END
  160. call setreg('x', v:_null_list)
  161. END
  162. call CheckLegacyAndVim9Success(lines)
  163. endfunc
  164. func Test_special_char()
  165. " The failure is only visible using valgrind.
  166. call CheckLegacyAndVim9Failure(['echo "\<C-">'], ['E15:', 'E1004:', 'E1004:'])
  167. endfunc
  168. func Test_method_with_prefix()
  169. let lines =<< trim END
  170. call assert_equal(TRUE, !range(5)->empty())
  171. call assert_equal(FALSE, !-3)
  172. END
  173. call CheckLegacyAndVim9Success(lines)
  174. call assert_equal([0, 1, 2], --3->range())
  175. call CheckDefAndScriptFailure(['eval --3->range()'], 'E15')
  176. call assert_equal(1, !+-+0)
  177. call CheckDefAndScriptFailure(['eval !+-+0'], 'E15')
  178. endfunc
  179. func Test_option_value()
  180. let lines =<< trim END
  181. #" boolean
  182. set bri
  183. call assert_equal(TRUE, &bri)
  184. set nobri
  185. call assert_equal(FALSE, &bri)
  186. #" number
  187. set ts=1
  188. call assert_equal(1, &ts)
  189. set ts=8
  190. call assert_equal(8, &ts)
  191. #" string
  192. exe "set cedit=\<Esc>"
  193. call assert_equal("\<Esc>", &cedit)
  194. set cpo=
  195. call assert_equal("", &cpo)
  196. set cpo=abcdefi
  197. call assert_equal("abcdefi", &cpo)
  198. set cpo&vim
  199. END
  200. call CheckLegacyAndVim9Success(lines)
  201. endfunc
  202. func Test_printf_misc()
  203. let lines =<< trim END
  204. call assert_equal('123', printf('123'))
  205. call assert_equal('', printf('%'))
  206. call assert_equal('', printf('%.0d', 0))
  207. call assert_equal('123', printf('%d', 123))
  208. call assert_equal('123', printf('%i', 123))
  209. call assert_equal('123', printf('%D', 123))
  210. call assert_equal('123', printf('%U', 123))
  211. call assert_equal('173', printf('%o', 123))
  212. call assert_equal('173', printf('%O', 123))
  213. call assert_equal('7b', printf('%x', 123))
  214. call assert_equal('7B', printf('%X', 123))
  215. call assert_equal('123', printf('%hd', 123))
  216. call assert_equal('-123', printf('%hd', -123))
  217. call assert_equal('-1', printf('%hd', 0xFFFF))
  218. call assert_equal('-1', printf('%hd', 0x1FFFFF))
  219. call assert_equal('123', printf('%hu', 123))
  220. call assert_equal('65413', printf('%hu', -123))
  221. call assert_equal('65535', printf('%hu', 0xFFFF))
  222. call assert_equal('65535', printf('%hu', 0x1FFFFF))
  223. call assert_equal('123', printf('%ld', 123))
  224. call assert_equal('-123', printf('%ld', -123))
  225. call assert_equal('65535', printf('%ld', 0xFFFF))
  226. call assert_equal('131071', printf('%ld', 0x1FFFF))
  227. call assert_equal('{', printf('%c', 123))
  228. call assert_equal('abc', printf('%s', 'abc'))
  229. call assert_equal('abc', printf('%S', 'abc'))
  230. call assert_equal('+123', printf('%+d', 123))
  231. call assert_equal('-123', printf('%+d', -123))
  232. call assert_equal('+123', printf('%+ d', 123))
  233. call assert_equal(' 123', printf('% d', 123))
  234. call assert_equal(' 123', printf('% d', 123))
  235. call assert_equal('-123', printf('% d', -123))
  236. call assert_equal('123', printf('%2d', 123))
  237. call assert_equal(' 123', printf('%6d', 123))
  238. call assert_equal('000123', printf('%06d', 123))
  239. call assert_equal('+00123', printf('%+06d', 123))
  240. call assert_equal(' 00123', printf('% 06d', 123))
  241. call assert_equal(' +123', printf('%+6d', 123))
  242. call assert_equal(' 123', printf('% 6d', 123))
  243. call assert_equal(' -123', printf('% 6d', -123))
  244. #" Test left adjusted.
  245. call assert_equal('123 ', printf('%-6d', 123))
  246. call assert_equal('+123 ', printf('%-+6d', 123))
  247. call assert_equal(' 123 ', printf('%- 6d', 123))
  248. call assert_equal('-123 ', printf('%- 6d', -123))
  249. call assert_equal(' 00123', printf('%7.5d', 123))
  250. call assert_equal(' -00123', printf('%7.5d', -123))
  251. call assert_equal(' +00123', printf('%+7.5d', 123))
  252. #" Precision field should not be used when combined with %0
  253. call assert_equal(' 00123', printf('%07.5d', 123))
  254. call assert_equal(' -00123', printf('%07.5d', -123))
  255. call assert_equal(' 123', printf('%*d', 5, 123))
  256. call assert_equal('123 ', printf('%*d', -5, 123))
  257. call assert_equal('00123', printf('%.*d', 5, 123))
  258. call assert_equal(' 123', printf('% *d', 5, 123))
  259. call assert_equal(' +123', printf('%+ *d', 5, 123))
  260. call assert_equal('foobar', printf('%.*s', 9, 'foobar'))
  261. call assert_equal('foo', printf('%.*s', 3, 'foobar'))
  262. call assert_equal('', printf('%.*s', 0, 'foobar'))
  263. call assert_equal('foobar', printf('%.*s', -1, 'foobar'))
  264. #" Simple quote (thousand grouping char) is ignored.
  265. call assert_equal('+00123456', printf("%+'09d", 123456))
  266. #" Unrecognized format specifier kept as-is.
  267. call assert_equal('_123', printf("%_%d", 123))
  268. #" Test alternate forms.
  269. call assert_equal('0x7b', printf('%#x', 123))
  270. call assert_equal('0X7B', printf('%#X', 123))
  271. call assert_equal('0173', printf('%#o', 123))
  272. call assert_equal('0173', printf('%#O', 123))
  273. call assert_equal('abc', printf('%#s', 'abc'))
  274. call assert_equal('abc', printf('%#S', 'abc'))
  275. call assert_equal(' 0173', printf('%#6o', 123))
  276. call assert_equal(' 00173', printf('%#6.5o', 123))
  277. call assert_equal(' 0173', printf('%#6.2o', 123))
  278. call assert_equal(' 0173', printf('%#6.2o', 123))
  279. call assert_equal('0173', printf('%#2.2o', 123))
  280. call assert_equal(' 00123', printf('%6.5d', 123))
  281. call assert_equal(' 0007b', printf('%6.5x', 123))
  282. call assert_equal('123', printf('%.2d', 123))
  283. call assert_equal('0123', printf('%.4d', 123))
  284. call assert_equal('0000000123', printf('%.10d', 123))
  285. call assert_equal('123', printf('%.0d', 123))
  286. call assert_equal('abc', printf('%2s', 'abc'))
  287. call assert_equal('abc', printf('%2S', 'abc'))
  288. call assert_equal('abc', printf('%.4s', 'abc'))
  289. call assert_equal('abc', printf('%.4S', 'abc'))
  290. call assert_equal('ab', printf('%.2s', 'abc'))
  291. call assert_equal('ab', printf('%.2S', 'abc'))
  292. call assert_equal('', printf('%.0s', 'abc'))
  293. call assert_equal('', printf('%.s', 'abc'))
  294. call assert_equal(' abc', printf('%4s', 'abc'))
  295. call assert_equal(' abc', printf('%4S', 'abc'))
  296. call assert_equal('0abc', printf('%04s', 'abc'))
  297. call assert_equal('0abc', printf('%04S', 'abc'))
  298. call assert_equal('abc ', printf('%-4s', 'abc'))
  299. call assert_equal('abc ', printf('%-4S', 'abc'))
  300. call assert_equal('๐Ÿ', printf('%.2S', '๐Ÿ๐Ÿ'))
  301. call assert_equal('', printf('%.1S', '๐Ÿ๐Ÿ'))
  302. call assert_equal('[ ใ‚ใ„ใ†]', printf('[%10.6S]', 'ใ‚ใ„ใ†ใˆใŠ'))
  303. call assert_equal('[ ใ‚ใ„ใ†ใˆ]', printf('[%10.8S]', 'ใ‚ใ„ใ†ใˆใŠ'))
  304. call assert_equal('[ใ‚ใ„ใ†ใˆใŠ]', printf('[%10.10S]', 'ใ‚ใ„ใ†ใˆใŠ'))
  305. call assert_equal('[ใ‚ใ„ใ†ใˆใŠ]', printf('[%10.12S]', 'ใ‚ใ„ใ†ใˆใŠ'))
  306. call assert_equal('ใ‚ใ„ใ†', printf('%S', 'ใ‚ใ„ใ†'))
  307. call assert_equal('ใ‚ใ„ใ†', printf('%#S', 'ใ‚ใ„ใ†'))
  308. call assert_equal('ใ‚b', printf('%2S', 'ใ‚b'))
  309. call assert_equal('ใ‚b', printf('%.4S', 'ใ‚b'))
  310. call assert_equal('ใ‚', printf('%.2S', 'ใ‚b'))
  311. call assert_equal(' ใ‚b', printf('%4S', 'ใ‚b'))
  312. call assert_equal('0ใ‚b', printf('%04S', 'ใ‚b'))
  313. call assert_equal('ใ‚b ', printf('%-4S', 'ใ‚b'))
  314. call assert_equal('ใ‚ ', printf('%-4.2S', 'ใ‚b'))
  315. call assert_equal('aใ„', printf('%2S', 'aใ„'))
  316. call assert_equal('aใ„', printf('%.4S', 'aใ„'))
  317. call assert_equal('a', printf('%.2S', 'aใ„'))
  318. call assert_equal(' aใ„', printf('%4S', 'aใ„'))
  319. call assert_equal('0aใ„', printf('%04S', 'aใ„'))
  320. call assert_equal('aใ„ ', printf('%-4S', 'aใ„'))
  321. call assert_equal('a ', printf('%-4.2S', 'aใ„'))
  322. call assert_equal('[ใ‚ใ„ใ†]', printf('[%05S]', 'ใ‚ใ„ใ†'))
  323. call assert_equal('[ใ‚ใ„ใ†]', printf('[%06S]', 'ใ‚ใ„ใ†'))
  324. call assert_equal('[0ใ‚ใ„ใ†]', printf('[%07S]', 'ใ‚ใ„ใ†'))
  325. call assert_equal('[ใ‚iใ†]', printf('[%05S]', 'ใ‚iใ†'))
  326. call assert_equal('[0ใ‚iใ†]', printf('[%06S]', 'ใ‚iใ†'))
  327. call assert_equal('[00ใ‚iใ†]', printf('[%07S]', 'ใ‚iใ†'))
  328. call assert_equal('[0ใ‚ใ„]', printf('[%05.4S]', 'ใ‚ใ„ใ†'))
  329. call assert_equal('[00ใ‚ใ„]', printf('[%06.4S]', 'ใ‚ใ„ใ†'))
  330. call assert_equal('[000ใ‚ใ„]', printf('[%07.4S]', 'ใ‚ใ„ใ†'))
  331. call assert_equal('[00ใ‚i]', printf('[%05.4S]', 'ใ‚iใ†'))
  332. call assert_equal('[000ใ‚i]', printf('[%06.4S]', 'ใ‚iใ†'))
  333. call assert_equal('[0000ใ‚i]', printf('[%07.4S]', 'ใ‚iใ†'))
  334. call assert_equal('[0ใ‚ใ„]', printf('[%05.5S]', 'ใ‚ใ„ใ†'))
  335. call assert_equal('[00ใ‚ใ„]', printf('[%06.5S]', 'ใ‚ใ„ใ†'))
  336. call assert_equal('[000ใ‚ใ„]', printf('[%07.5S]', 'ใ‚ใ„ใ†'))
  337. call assert_equal('[ใ‚iใ†]', printf('[%05.5S]', 'ใ‚iใ†'))
  338. call assert_equal('[0ใ‚iใ†]', printf('[%06.5S]', 'ใ‚iใ†'))
  339. call assert_equal('[00ใ‚iใ†]', printf('[%07.5S]', 'ใ‚iใ†'))
  340. call assert_equal('[0000000000]', printf('[%010.0S]', 'ใ‚ใ„ใ†'))
  341. call assert_equal('[0000000000]', printf('[%010.1S]', 'ใ‚ใ„ใ†'))
  342. call assert_equal('[00000000ใ‚]', printf('[%010.2S]', 'ใ‚ใ„ใ†'))
  343. call assert_equal('[00000000ใ‚]', printf('[%010.3S]', 'ใ‚ใ„ใ†'))
  344. call assert_equal('[000000ใ‚ใ„]', printf('[%010.4S]', 'ใ‚ใ„ใ†'))
  345. call assert_equal('[000000ใ‚ใ„]', printf('[%010.5S]', 'ใ‚ใ„ใ†'))
  346. call assert_equal('[0000ใ‚ใ„ใ†]', printf('[%010.6S]', 'ใ‚ใ„ใ†'))
  347. call assert_equal('[0000ใ‚ใ„ใ†]', printf('[%010.7S]', 'ใ‚ใ„ใ†'))
  348. call assert_equal('[0000000000]', printf('[%010.1S]', 'ใ‚iใ†'))
  349. call assert_equal('[00000000ใ‚]', printf('[%010.2S]', 'ใ‚iใ†'))
  350. call assert_equal('[0000000ใ‚i]', printf('[%010.3S]', 'ใ‚iใ†'))
  351. call assert_equal('[0000000ใ‚i]', printf('[%010.4S]', 'ใ‚iใ†'))
  352. call assert_equal('[00000ใ‚iใ†]', printf('[%010.5S]', 'ใ‚iใ†'))
  353. call assert_equal('[00000ใ‚iใ†]', printf('[%010.6S]', 'ใ‚iใ†'))
  354. call assert_equal('[00000ใ‚iใ†]', printf('[%010.7S]', 'ใ‚iใ†'))
  355. call assert_equal('1%', printf('%d%%', 1))
  356. call assert_notequal('', printf('%p', "abc"))
  357. END
  358. call CheckLegacyAndVim9Success(lines)
  359. call CheckLegacyAndVim9Failure(["call printf('123', 3)"], "E767:")
  360. " this was using uninitialized memory
  361. call CheckLegacyAndVim9Failure(["eval ''->printf()"], "E119:")
  362. endfunc
  363. func Test_printf_float()
  364. if has('float')
  365. let lines =<< trim END
  366. call assert_equal('1.000000', printf('%f', 1))
  367. call assert_equal('1.230000', printf('%f', 1.23))
  368. call assert_equal('1.230000', printf('%F', 1.23))
  369. call assert_equal('9999999.9', printf('%g', 9999999.9))
  370. call assert_equal('9999999.9', printf('%G', 9999999.9))
  371. call assert_equal('1.00000001e7', printf('%.8g', 10000000.1))
  372. call assert_equal('1.00000001E7', printf('%.8G', 10000000.1))
  373. call assert_equal('1.230000e+00', printf('%e', 1.23))
  374. call assert_equal('1.230000E+00', printf('%E', 1.23))
  375. call assert_equal('1.200000e-02', printf('%e', 0.012))
  376. call assert_equal('-1.200000e-02', printf('%e', -0.012))
  377. call assert_equal('0.33', printf('%.2f', 1.0 / 3.0))
  378. call assert_equal(' 0.33', printf('%6.2f', 1.0 / 3.0))
  379. call assert_equal(' -0.33', printf('%6.2f', -1.0 / 3.0))
  380. call assert_equal('000.33', printf('%06.2f', 1.0 / 3.0))
  381. call assert_equal('-00.33', printf('%06.2f', -1.0 / 3.0))
  382. call assert_equal('-00.33', printf('%+06.2f', -1.0 / 3.0))
  383. call assert_equal('+00.33', printf('%+06.2f', 1.0 / 3.0))
  384. call assert_equal(' 00.33', printf('% 06.2f', 1.0 / 3.0))
  385. call assert_equal('000.33', printf('%06.2g', 1.0 / 3.0))
  386. call assert_equal('-00.33', printf('%06.2g', -1.0 / 3.0))
  387. call assert_equal('0.33', printf('%3.2f', 1.0 / 3.0))
  388. call assert_equal('003.33e-01', printf('%010.2e', 1.0 / 3.0))
  389. call assert_equal(' 03.33e-01', printf('% 010.2e', 1.0 / 3.0))
  390. call assert_equal('+03.33e-01', printf('%+010.2e', 1.0 / 3.0))
  391. call assert_equal('-03.33e-01', printf('%010.2e', -1.0 / 3.0))
  392. #" When precision is 0, the dot should be omitted.
  393. call assert_equal(' 2', printf('%3.f', 7.0 / 3.0))
  394. call assert_equal(' 2', printf('%3.g', 7.0 / 3.0))
  395. call assert_equal(' 2e+00', printf('%7.e', 7.0 / 3.0))
  396. #" Float zero can be signed.
  397. call assert_equal('+0.000000', printf('%+f', 0.0))
  398. call assert_equal('0.000000', printf('%f', 1.0 / (1.0 / 0.0)))
  399. call assert_equal('-0.000000', printf('%f', 1.0 / (-1.0 / 0.0)))
  400. call assert_equal('0.0', printf('%s', 1.0 / (1.0 / 0.0)))
  401. call assert_equal('-0.0', printf('%s', 1.0 / (-1.0 / 0.0)))
  402. call assert_equal('0.0', printf('%S', 1.0 / (1.0 / 0.0)))
  403. call assert_equal('-0.0', printf('%S', 1.0 / (-1.0 / 0.0)))
  404. #" Float infinity can be signed.
  405. call assert_equal('inf', printf('%f', 1.0 / 0.0))
  406. call assert_equal('-inf', printf('%f', -1.0 / 0.0))
  407. call assert_equal('inf', printf('%g', 1.0 / 0.0))
  408. call assert_equal('-inf', printf('%g', -1.0 / 0.0))
  409. call assert_equal('inf', printf('%e', 1.0 / 0.0))
  410. call assert_equal('-inf', printf('%e', -1.0 / 0.0))
  411. call assert_equal('INF', printf('%F', 1.0 / 0.0))
  412. call assert_equal('-INF', printf('%F', -1.0 / 0.0))
  413. call assert_equal('INF', printf('%E', 1.0 / 0.0))
  414. call assert_equal('-INF', printf('%E', -1.0 / 0.0))
  415. call assert_equal('INF', printf('%E', 1.0 / 0.0))
  416. call assert_equal('-INF', printf('%G', -1.0 / 0.0))
  417. call assert_equal('+inf', printf('%+f', 1.0 / 0.0))
  418. call assert_equal('-inf', printf('%+f', -1.0 / 0.0))
  419. call assert_equal(' inf', printf('% f', 1.0 / 0.0))
  420. call assert_equal(' inf', printf('%6f', 1.0 / 0.0))
  421. call assert_equal(' -inf', printf('%6f', -1.0 / 0.0))
  422. call assert_equal(' inf', printf('%6g', 1.0 / 0.0))
  423. call assert_equal(' -inf', printf('%6g', -1.0 / 0.0))
  424. call assert_equal(' +inf', printf('%+6f', 1.0 / 0.0))
  425. call assert_equal(' inf', printf('% 6f', 1.0 / 0.0))
  426. call assert_equal(' +inf', printf('%+06f', 1.0 / 0.0))
  427. call assert_equal('inf ', printf('%-6f', 1.0 / 0.0))
  428. call assert_equal('-inf ', printf('%-6f', -1.0 / 0.0))
  429. call assert_equal('+inf ', printf('%-+6f', 1.0 / 0.0))
  430. call assert_equal(' inf ', printf('%- 6f', 1.0 / 0.0))
  431. call assert_equal('-INF ', printf('%-6F', -1.0 / 0.0))
  432. call assert_equal('+INF ', printf('%-+6F', 1.0 / 0.0))
  433. call assert_equal(' INF ', printf('%- 6F', 1.0 / 0.0))
  434. call assert_equal('INF ', printf('%-6G', 1.0 / 0.0))
  435. call assert_equal('-INF ', printf('%-6G', -1.0 / 0.0))
  436. call assert_equal('INF ', printf('%-6E', 1.0 / 0.0))
  437. call assert_equal('-INF ', printf('%-6E', -1.0 / 0.0))
  438. call assert_equal("str2float('inf')", printf('%s', 1.0 / 0.0))
  439. call assert_equal("-str2float('inf')", printf('%s', -1.0 / 0.0))
  440. #" Test special case where max precision is truncated at 340.
  441. call assert_equal('1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.330f', 1.0))
  442. call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.340f', 1.0))
  443. call assert_equal('1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', printf('%.350f', 1.0))
  444. #" Float nan (not a number) has no sign.
  445. call assert_equal('nan', printf('%f', sqrt(-1.0)))
  446. call assert_equal('nan', printf('%f', 0.0 / 0.0))
  447. call assert_equal('nan', printf('%f', -0.0 / 0.0))
  448. call assert_equal('nan', printf('%g', 0.0 / 0.0))
  449. call assert_equal('nan', printf('%e', 0.0 / 0.0))
  450. call assert_equal('NAN', printf('%F', 0.0 / 0.0))
  451. call assert_equal('NAN', printf('%G', 0.0 / 0.0))
  452. call assert_equal('NAN', printf('%E', 0.0 / 0.0))
  453. call assert_equal('NAN', printf('%F', -0.0 / 0.0))
  454. call assert_equal('NAN', printf('%G', -0.0 / 0.0))
  455. call assert_equal('NAN', printf('%E', -0.0 / 0.0))
  456. call assert_equal(' nan', printf('%6f', 0.0 / 0.0))
  457. call assert_equal(' nan', printf('%06f', 0.0 / 0.0))
  458. call assert_equal('nan ', printf('%-6f', 0.0 / 0.0))
  459. call assert_equal('nan ', printf('%- 6f', 0.0 / 0.0))
  460. call assert_equal("str2float('nan')", printf('%s', 0.0 / 0.0))
  461. call assert_equal("str2float('nan')", printf('%s', -0.0 / 0.0))
  462. call assert_equal("str2float('nan')", printf('%S', 0.0 / 0.0))
  463. call assert_equal("str2float('nan')", printf('%S', -0.0 / 0.0))
  464. END
  465. call CheckLegacyAndVim9Success(lines)
  466. call CheckLegacyAndVim9Failure(['echo printf("%f", "a")'], 'E807:')
  467. endif
  468. endfunc
  469. func Test_printf_errors()
  470. call CheckLegacyAndVim9Failure(['echo printf("%d", {})'], 'E728:')
  471. call CheckLegacyAndVim9Failure(['echo printf("%d", [])'], 'E745:')
  472. call CheckLegacyAndVim9Failure(['echo printf("%d", 1, 2)'], 'E767:')
  473. call CheckLegacyAndVim9Failure(['echo printf("%*d", 1)'], 'E766:')
  474. call CheckLegacyAndVim9Failure(['echo printf("%s")'], 'E766:')
  475. if has('float')
  476. call CheckLegacyAndVim9Failure(['echo printf("%d", 1.2)'], 'E805:')
  477. call CheckLegacyAndVim9Failure(['echo printf("%f")'], 'E766:')
  478. endif
  479. endfunc
  480. func Test_printf_64bit()
  481. let lines =<< trim END
  482. call assert_equal("123456789012345", printf('%d', 123456789012345))
  483. END
  484. call CheckLegacyAndVim9Success(lines)
  485. endfunc
  486. func Test_printf_spec_s()
  487. let lines =<< trim END
  488. #" number
  489. call assert_equal("1234567890", printf('%s', 1234567890))
  490. #" string
  491. call assert_equal("abcdefgi", printf('%s', "abcdefgi"))
  492. #" float
  493. if has('float')
  494. call assert_equal("1.23", printf('%s', 1.23))
  495. endif
  496. #" list
  497. VAR lvalue = [1, 'two', ['three', 4]]
  498. call assert_equal(string(lvalue), printf('%s', lvalue))
  499. #" dict
  500. VAR dvalue = {'key1': 'value1', 'key2': ['list', 'lvalue'], 'key3': {'dict': 'lvalue'}}
  501. call assert_equal(string(dvalue), printf('%s', dvalue))
  502. #" funcref
  503. call assert_equal('printf', printf('%s', 'printf'->function()))
  504. #" partial
  505. call assert_equal(string(function('printf', ['%s'])), printf('%s', function('printf', ['%s'])))
  506. END
  507. call CheckLegacyAndVim9Success(lines)
  508. endfunc
  509. func Test_printf_spec_b()
  510. let lines =<< trim END
  511. call assert_equal("0", printf('%b', 0))
  512. call assert_equal("00001100", printf('%08b', 12))
  513. call assert_equal("11111111", printf('%08b', 0xff))
  514. call assert_equal(" 1111011", printf('%10b', 123))
  515. call assert_equal("0001111011", printf('%010b', 123))
  516. call assert_equal(" 0b1111011", printf('%#10b', 123))
  517. call assert_equal("0B01111011", printf('%#010B', 123))
  518. call assert_equal("1001001100101100000001011010010", printf('%b', 1234567890))
  519. call assert_equal("11100000100100010000110000011011101111101111001", printf('%b', 123456789012345))
  520. call assert_equal("1111111111111111111111111111111111111111111111111111111111111111", printf('%b', -1))
  521. END
  522. call CheckLegacyAndVim9Success(lines)
  523. endfunc
  524. func Test_max_min_errors()
  525. call CheckLegacyAndVim9Failure(['call max(v:true)'], ['E712:', 'E1013:', 'E1227:'])
  526. call CheckLegacyAndVim9Failure(['call max(v:true)'], ['max()', 'E1013:', 'E1227:'])
  527. call CheckLegacyAndVim9Failure(['call min(v:true)'], ['E712:', 'E1013:', 'E1227:'])
  528. call CheckLegacyAndVim9Failure(['call min(v:true)'], ['min()', 'E1013:', 'E1227:'])
  529. endfunc
  530. func Test_function_with_funcref()
  531. let lines =<< trim END
  532. let s:F = function('type')
  533. let s:Fref = function(s:F)
  534. call assert_equal(v:t_string, s:Fref('x'))
  535. call assert_fails("call function('s:F')", 'E700:')
  536. call assert_fails("call function('foo()')", 'E475:')
  537. call assert_fails("call function('foo()')", 'foo()')
  538. call assert_fails("function('')", 'E129:')
  539. let s:Len = {s -> strlen(s)}
  540. call assert_equal(6, s:Len('foobar'))
  541. let name = string(s:Len)
  542. " can evaluate "function('<lambda>99')"
  543. call execute('let Ref = ' .. name)
  544. call assert_equal(4, Ref('text'))
  545. END
  546. call CheckScriptSuccess(lines)
  547. let lines =<< trim END
  548. vim9script
  549. var F = function('type')
  550. var Fref = function(F)
  551. call assert_equal(v:t_string, Fref('x'))
  552. call assert_fails("call function('F')", 'E700:')
  553. call assert_fails("call function('foo()')", 'E475:')
  554. call assert_fails("call function('foo()')", 'foo()')
  555. call assert_fails("function('')", 'E129:')
  556. var Len = (s) => strlen(s)
  557. call assert_equal(6, Len('foobar'))
  558. var name = string(Len)
  559. # can evaluate "function('<lambda>99')"
  560. call execute('var Ref = ' .. name)
  561. call assert_equal(4, Ref('text'))
  562. END
  563. call CheckScriptSuccess(lines)
  564. endfunc
  565. func Test_funcref()
  566. func! One()
  567. return 1
  568. endfunc
  569. let OneByName = function('One')
  570. let OneByRef = funcref('One')
  571. func! One()
  572. return 2
  573. endfunc
  574. call assert_equal(2, OneByName())
  575. call assert_equal(1, OneByRef())
  576. let OneByRef = 'One'->funcref()
  577. call assert_equal(2, OneByRef())
  578. call assert_fails('echo funcref("{")', 'E475:')
  579. let OneByRef = funcref("One", repeat(["foo"], 20))
  580. call assert_fails('let OneByRef = funcref("One", repeat(["foo"], 21))', 'E118:')
  581. call assert_fails('echo function("min") =~ function("min")', 'E694:')
  582. endfunc
  583. " Test for calling function() and funcref() outside of a Vim script context.
  584. func Test_function_outside_script()
  585. let cleanup =<< trim END
  586. call writefile([execute('messages')], 'Xtest.out')
  587. qall
  588. END
  589. call writefile(cleanup, 'Xverify.vim')
  590. call RunVim([], [], "-c \"echo function('s:abc')\" -S Xverify.vim")
  591. call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0])
  592. call RunVim([], [], "-c \"echo funcref('s:abc')\" -S Xverify.vim")
  593. call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0])
  594. call delete('Xtest.out')
  595. call delete('Xverify.vim')
  596. endfunc
  597. func Test_setmatches()
  598. let lines =<< trim END
  599. hi def link 1 Comment
  600. hi def link 2 PreProc
  601. VAR set = [{"group": 1, "pattern": 2, "id": 3, "priority": 4}]
  602. VAR exp = [{"group": '1', "pattern": '2', "id": 3, "priority": 4}]
  603. if has('conceal')
  604. LET set[0]['conceal'] = 5
  605. LET exp[0]['conceal'] = '5'
  606. endif
  607. eval set->setmatches()
  608. call assert_equal(exp, getmatches())
  609. END
  610. call CheckLegacyAndVim9Success(lines)
  611. call CheckLegacyAndVim9Failure(['VAR m = setmatches([], [])'], ['E745:', 'E1013:', 'E1210:'])
  612. endfunc
  613. func Test_empty_concatenate()
  614. let lines =<< trim END
  615. call assert_equal('b', 'a'[4 : 0] .. 'b')
  616. call assert_equal('b', 'b' .. 'a'[4 : 0])
  617. END
  618. call CheckLegacyAndVim9Success(lines)
  619. endfunc
  620. func Test_broken_number()
  621. call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 1X'], 'E15:')
  622. call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 0b1X'], 'E15:')
  623. call CheckLegacyAndVim9Failure(['echo 0b12'], 'E15:')
  624. call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 0x1X'], 'E15:')
  625. call CheckLegacyAndVim9Failure(['VAR X = "bad"', 'echo 011X'], 'E15:')
  626. call CheckLegacyAndVim9Success(['call assert_equal(2, str2nr("2a"))'])
  627. call CheckLegacyAndVim9Failure(['inoremap <Char-0b1z> b'], 'E474:')
  628. endfunc
  629. func Test_eval_after_if()
  630. let s:val = ''
  631. func SetVal(x)
  632. let s:val ..= a:x
  633. endfunc
  634. if 0 | eval SetVal('a') | endif | call SetVal('b')
  635. call assert_equal('b', s:val)
  636. endfunc
  637. func Test_divide_by_zero()
  638. " only tests that this doesn't crash, the result is not important
  639. echo 0 / 0
  640. echo 0 / 0 / -1
  641. endfunc
  642. " Test for command-line completion of expressions
  643. func Test_expr_completion()
  644. CheckFeature cmdline_compl
  645. for cmd in [
  646. \ 'let a = ',
  647. \ 'const a = ',
  648. \ 'if',
  649. \ 'elseif',
  650. \ 'while',
  651. \ 'for',
  652. \ 'echo',
  653. \ 'echon',
  654. \ 'execute',
  655. \ 'echomsg',
  656. \ 'echoerr',
  657. \ 'call',
  658. \ 'return',
  659. \ 'cexpr',
  660. \ 'caddexpr',
  661. \ 'cgetexpr',
  662. \ 'lexpr',
  663. \ 'laddexpr',
  664. \ 'lgetexpr']
  665. call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
  666. call assert_equal('"' . cmd . ' getline(', getreg(':'))
  667. endfor
  668. " completion for the expression register
  669. call feedkeys(":\"\<C-R>=float2\t\"\<C-B>\"\<CR>", 'xt')
  670. call assert_equal('"float2nr("', @=)
  671. " completion for window local variables
  672. let w:wvar1 = 10
  673. let w:wvar2 = 10
  674. call feedkeys(":echo w:wvar\<C-A>\<C-B>\"\<CR>", 'xt')
  675. call assert_equal('"echo w:wvar1 w:wvar2', @:)
  676. unlet w:wvar1 w:wvar2
  677. " completion for tab local variables
  678. let t:tvar1 = 10
  679. let t:tvar2 = 10
  680. call feedkeys(":echo t:tvar\<C-A>\<C-B>\"\<CR>", 'xt')
  681. call assert_equal('"echo t:tvar1 t:tvar2', @:)
  682. unlet t:tvar1 t:tvar2
  683. " completion for variables
  684. let g:tvar1 = 1
  685. let g:tvar2 = 2
  686. call feedkeys(":let g:tv\<C-A>\<C-B>\"\<CR>", 'xt')
  687. call assert_equal('"let g:tvar1 g:tvar2', @:)
  688. " completion for variables after a ||
  689. call feedkeys(":echo 1 || g:tv\<C-A>\<C-B>\"\<CR>", 'xt')
  690. call assert_equal('"echo 1 || g:tvar1 g:tvar2', @:)
  691. " completion for options
  692. "call feedkeys(":echo &compat\<C-A>\<C-B>\"\<CR>", 'xt')
  693. "call assert_equal('"echo &compatible', @:)
  694. "call feedkeys(":echo 1 && &compat\<C-A>\<C-B>\"\<CR>", 'xt')
  695. "call assert_equal('"echo 1 && &compatible', @:)
  696. call feedkeys(":echo &g:equala\<C-A>\<C-B>\"\<CR>", 'xt')
  697. call assert_equal('"echo &g:equalalways', @:)
  698. " completion for string
  699. call feedkeys(":echo \"Hello\\ World\"\<C-A>\<C-B>\"\<CR>", 'xt')
  700. call assert_equal("\"echo \"Hello\\ World\"\<C-A>", @:)
  701. call feedkeys(":echo 'Hello World'\<C-A>\<C-B>\"\<CR>", 'xt')
  702. call assert_equal("\"echo 'Hello World'\<C-A>", @:)
  703. " completion for command after a |
  704. call feedkeys(":echo 'Hello' | cwin\<C-A>\<C-B>\"\<CR>", 'xt')
  705. call assert_equal("\"echo 'Hello' | cwindow", @:)
  706. " completion for environment variable
  707. let $X_VIM_TEST_COMPLETE_ENV = 'foo'
  708. call feedkeys(":let $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
  709. call assert_match('"let $X_VIM_TEST_COMPLETE_ENV', @:)
  710. unlet $X_VIM_TEST_COMPLETE_ENV
  711. endfunc
  712. " Test for errors in expression evaluation
  713. func Test_expr_eval_error()
  714. call CheckLegacyAndVim9Failure(["VAR i = 'abc' .. []"], ['E730:', 'E1105:', 'E730:'])
  715. call CheckLegacyAndVim9Failure(["VAR l = [] + 10"], ['E745:', 'E1051:', 'E745'])
  716. call CheckLegacyAndVim9Failure(["VAR v = 10 + []"], ['E745:', 'E1051:', 'E745:'])
  717. call CheckLegacyAndVim9Failure(["VAR v = 10 / []"], ['E745:', 'E1036:', 'E745:'])
  718. call CheckLegacyAndVim9Failure(["VAR v = -{}"], ['E728:', 'E1012:', 'E728:'])
  719. endfunc
  720. func Test_white_in_function_call()
  721. let lines =<< trim END
  722. VAR text = substitute ( 'some text' , 't' , 'T' , 'g' )
  723. call assert_equal('some TexT', text)
  724. END
  725. call CheckTransLegacySuccess(lines)
  726. let lines =<< trim END
  727. var text = substitute ( 'some text' , 't' , 'T' , 'g' )
  728. call assert_equal('some TexT', text)
  729. END
  730. call CheckDefAndScriptFailure(lines, ['E1001:', 'E121:'])
  731. endfunc
  732. " Test for float value comparison
  733. func Test_float_compare()
  734. CheckFeature float
  735. let lines =<< trim END
  736. call assert_true(1.2 == 1.2)
  737. call assert_true(1.0 != 1.2)
  738. call assert_true(1.2 > 1.0)
  739. call assert_true(1.2 >= 1.2)
  740. call assert_true(1.0 < 1.2)
  741. call assert_true(1.2 <= 1.2)
  742. call assert_true(+0.0 == -0.0)
  743. #" two NaNs (not a number) are not equal
  744. call assert_true(sqrt(-4.01) != (0.0 / 0.0))
  745. #" two inf (infinity) are equal
  746. call assert_true((1.0 / 0) == (2.0 / 0))
  747. #" two -inf (infinity) are equal
  748. call assert_true(-(1.0 / 0) == -(2.0 / 0))
  749. #" +infinity != -infinity
  750. call assert_true((1.0 / 0) != -(2.0 / 0))
  751. END
  752. call CheckLegacyAndVim9Success(lines)
  753. endfunc
  754. func Test_string_interp()
  755. let lines =<< trim END
  756. call assert_equal('', $"")
  757. call assert_equal('foobar', $"foobar")
  758. #" Escaping rules.
  759. call assert_equal('"foo"{bar}', $"\"foo\"{{bar}}")
  760. call assert_equal('"foo"{bar}', $'"foo"{{bar}}')
  761. call assert_equal('foobar', $"{"foo"}" .. $'{'bar'}')
  762. #" Whitespace before/after the expression.
  763. call assert_equal('3', $"{ 1 + 2 }")
  764. #" String conversion.
  765. call assert_equal('hello from ' .. v:version, $"hello from {v:version}")
  766. call assert_equal('hello from ' .. v:version, $'hello from {v:version}')
  767. #" Paper over a small difference between Vim script behaviour.
  768. call assert_equal(string(v:true), $"{v:true}")
  769. call assert_equal('(1+1=2)', $"(1+1={1 + 1})")
  770. #" Hex-escaped opening brace: char2nr('{') == 0x7b
  771. call assert_equal('esc123ape', $"esc{123}ape")
  772. call assert_equal('me{}me', $"me{"\x7b"}\x7dme")
  773. VAR var1 = "sun"
  774. VAR var2 = "shine"
  775. call assert_equal('sunshine', $"{var1}{var2}")
  776. call assert_equal('sunsunsun', $"{var1->repeat(3)}")
  777. #" Multibyte strings.
  778. call assert_equal('say ใƒใƒญใƒผใƒปใƒฏใƒผใƒซใƒ‰', $"say {'ใƒใƒญใƒผใƒปใƒฏใƒผใƒซใƒ‰'}")
  779. #" Nested.
  780. call assert_equal('foobarbaz', $"foo{$"{'bar'}"}baz")
  781. #" Do not evaluate blocks when the expr is skipped.
  782. VAR tmp = 0
  783. if v:false
  784. echo "${ LET tmp += 1 }"
  785. endif
  786. call assert_equal(0, tmp)
  787. #" Dict interpolation
  788. VAR d = {'a': 10, 'b': [1, 2]}
  789. call assert_equal("{'a': 10, 'b': [1, 2]}", $'{d}')
  790. VAR emptydict = {}
  791. call assert_equal("a{}b", $'a{emptydict}b')
  792. VAR nulldict = v:_null_dict
  793. call assert_equal("a{}b", $'a{nulldict}b')
  794. #" List interpolation
  795. VAR l = ['a', 'b', 'c']
  796. call assert_equal("['a', 'b', 'c']", $'{l}')
  797. VAR emptylist = []
  798. call assert_equal("a[]b", $'a{emptylist}b')
  799. VAR nulllist = v:_null_list
  800. call assert_equal("a[]b", $'a{nulllist}b')
  801. #" Stray closing brace.
  802. call assert_fails('echo $"moo}"', 'E1278:')
  803. #" Undefined variable in expansion.
  804. call assert_fails('echo $"{moo}"', 'E121:')
  805. #" Empty blocks are rejected.
  806. call assert_fails('echo $"{}"', 'E15:')
  807. call assert_fails('echo $"{ }"', 'E15:')
  808. END
  809. call CheckLegacyAndVim9Success(lines)
  810. let lines =<< trim END
  811. call assert_equal('5', $"{({x -> x + 1})(4)}")
  812. END
  813. call CheckLegacySuccess(lines)
  814. let lines =<< trim END
  815. call assert_equal('5', $"{((x) => x + 1)(4)}")
  816. call assert_fails('echo $"{ # foo }"', 'E1279:')
  817. END
  818. call CheckDefAndScriptSuccess(lines)
  819. endfunc
  820. " vim: shiftwidth=2 sts=2 expandtab