test_listdict.vim 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. " Tests for the List and Dict types
  2. scriptencoding utf-8
  3. source vim9.vim
  4. func TearDown()
  5. " Run garbage collection after every test
  6. call test_garbagecollect_now()
  7. endfunc
  8. " Tests for List type
  9. " List creation
  10. func Test_list_create()
  11. " Creating List directly with different types
  12. let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
  13. call assert_equal("[1, 'as''d', [1, 2, function('strlen')], {'a': 1}]", string(l))
  14. call assert_equal({'a' : 1}, l[-1])
  15. call assert_equal(1, l[-4])
  16. let x = 10
  17. try
  18. let x = l[-5]
  19. catch
  20. call assert_match('E684:', v:exception)
  21. endtry
  22. call assert_equal(10, x)
  23. endfunc
  24. " List slices
  25. func Test_list_slice()
  26. let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
  27. call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[:])
  28. call assert_equal(['as''d', [1, 2, function('strlen')], {'a': 1}], l[1:])
  29. call assert_equal([1, 'as''d', [1, 2, function('strlen')]], l[:-2])
  30. call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[0:8])
  31. call assert_equal([], l[8:-1])
  32. call assert_equal([], l[0:-10])
  33. " perform an operation on a list slice
  34. let l = [1, 2, 3]
  35. let l[:1] += [1, 2]
  36. let l[2:] -= [1]
  37. call assert_equal([2, 4, 2], l)
  38. let lines =<< trim END
  39. VAR l = [1, 2]
  40. call assert_equal([1, 2], l[:])
  41. call assert_equal([2], l[-1 : -1])
  42. call assert_equal([1, 2], l[-2 : -1])
  43. END
  44. call CheckLegacyAndVim9Success(lines)
  45. let l = [1, 2]
  46. call assert_equal([], l[-3 : -1])
  47. let lines =<< trim END
  48. var l = [1, 2]
  49. assert_equal([1, 2], l[-3 : -1])
  50. END
  51. call CheckDefAndScriptSuccess(lines)
  52. call assert_fails('let l[[]] = 1', 'E730: Using a List as a String')
  53. call assert_fails('let l[1 : []] = [1]', 'E730: Using a List as a String')
  54. endfunc
  55. " List identity
  56. func Test_list_identity()
  57. let lines =<< trim END
  58. VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
  59. VAR ll = l
  60. VAR lx = copy(l)
  61. call assert_true(l == ll)
  62. call assert_false(l isnot ll)
  63. call assert_true(l is ll)
  64. call assert_true(l == lx)
  65. call assert_false(l is lx)
  66. call assert_true(l isnot lx)
  67. END
  68. call CheckLegacyAndVim9Success(lines)
  69. endfunc
  70. " removing items with :unlet
  71. func Test_list_unlet()
  72. let lines =<< trim END
  73. VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
  74. unlet l[2]
  75. call assert_equal([1, 'as''d', {'a': 1}], l)
  76. LET l = range(8)
  77. unlet l[: 3]
  78. unlet l[1 :]
  79. call assert_equal([4], l)
  80. #" removing items out of range: silently skip items that don't exist
  81. LET l = [0, 1, 2, 3]
  82. unlet l[2 : 2]
  83. call assert_equal([0, 1, 3], l)
  84. LET l = [0, 1, 2, 3]
  85. unlet l[2 : 3]
  86. call assert_equal([0, 1], l)
  87. LET l = [0, 1, 2, 3]
  88. unlet l[2 : 4]
  89. call assert_equal([0, 1], l)
  90. LET l = [0, 1, 2, 3]
  91. unlet l[2 : 5]
  92. call assert_equal([0, 1], l)
  93. LET l = [0, 1, 2, 3]
  94. unlet l[-2 : 2]
  95. call assert_equal([0, 1, 3], l)
  96. LET l = [0, 1, 2, 3]
  97. unlet l[-3 : 2]
  98. call assert_equal([0, 3], l)
  99. LET l = [0, 1, 2, 3]
  100. unlet l[-4 : 2]
  101. call assert_equal([3], l)
  102. LET l = [0, 1, 2, 3]
  103. unlet l[-5 : 2]
  104. call assert_equal([3], l)
  105. LET l = [0, 1, 2, 3]
  106. unlet l[-6 : 2]
  107. call assert_equal([3], l)
  108. END
  109. call CheckLegacyAndVim9Success(lines)
  110. let l = [0, 1, 2, 3]
  111. unlet l[2:2]
  112. call assert_equal([0, 1, 3], l)
  113. let l = [0, 1, 2, 3]
  114. unlet l[2:3]
  115. call assert_equal([0, 1], l)
  116. let lines =<< trim END
  117. VAR l = [0, 1, 2, 3]
  118. unlet l[2 : 1]
  119. END
  120. call CheckLegacyAndVim9Failure(lines, 'E684:')
  121. let lines =<< trim END
  122. VAR l = [0, 1, 2, 3]
  123. unlet l[-1 : 2]
  124. END
  125. call CheckLegacyAndVim9Failure(lines, 'E684:')
  126. endfunc
  127. " assignment to a list
  128. func Test_list_assign()
  129. let lines =<< trim END
  130. VAR l = [0, 1, 2, 3]
  131. VAR va = 0
  132. VAR vb = 0
  133. LET [va, vb] = l[2 : 3]
  134. call assert_equal([2, 3], [va, vb])
  135. END
  136. call CheckLegacyAndVim9Success(lines)
  137. let lines =<< trim END
  138. let l = [0, 1, 2, 3]
  139. let [va, vb] = l
  140. END
  141. call CheckScriptFailure(lines, 'E687:')
  142. let lines =<< trim END
  143. var l = [0, 1, 2, 3]
  144. var va = 0
  145. var vb = 0
  146. [va, vb] = l
  147. END
  148. call CheckScriptFailure(['vim9script'] + lines, 'E687:')
  149. call CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 4')
  150. let lines =<< trim END
  151. let l = [0, 1, 2, 3]
  152. let [va, vb] = l[1:1]
  153. END
  154. call CheckScriptFailure(lines, 'E688:')
  155. let lines =<< trim END
  156. var l = [0, 1, 2, 3]
  157. var va = 0
  158. var vb = 0
  159. [va, vb] = l[1 : 1]
  160. END
  161. call CheckScriptFailure(['vim9script'] + lines, 'E688:')
  162. call CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 1')
  163. let lines =<< trim END
  164. VAR l = [2]
  165. LET l += v:_null_list
  166. call assert_equal([2], l)
  167. LET l = v:_null_list
  168. LET l += [1]
  169. call assert_equal([1], l)
  170. END
  171. call CheckLegacyAndVim9Success(lines)
  172. let d = {'abc': [1, 2, 3]}
  173. call assert_fails('let d.abc[0:0z10] = [10, 20]', 'E976: Using a Blob as a String')
  174. endfunc
  175. " test for range assign
  176. func Test_list_range_assign()
  177. let lines =<< trim END
  178. VAR l = [0]
  179. LET l[:] = [1, 2]
  180. call assert_equal([1, 2], l)
  181. LET l[-4 : -1] = [5, 6]
  182. call assert_equal([5, 6], l)
  183. END
  184. call CheckLegacyAndVim9Success(lines)
  185. let lines =<< trim END
  186. var l = [7]
  187. l[:] = ['text']
  188. END
  189. call CheckDefAndScriptFailure(lines, 'E1012:', 2)
  190. endfunc
  191. func Test_list_items()
  192. let r = []
  193. let l = ['a', 'b', 'c']
  194. for [idx, val] in items(l)
  195. call extend(r, [[idx, val]])
  196. endfor
  197. call assert_equal([[0, 'a'], [1, 'b'], [2, 'c']], r)
  198. call assert_fails('call items(3)', 'E1225:')
  199. endfunc
  200. func Test_string_items()
  201. let r = []
  202. let s = 'ábツ'
  203. for [idx, val] in items(s)
  204. call extend(r, [[idx, val]])
  205. endfor
  206. call assert_equal([[0, 'á'], [1, 'b'], [2, 'ツ']], r)
  207. endfunc
  208. " Test removing items in list
  209. func Test_list_func_remove()
  210. let lines =<< trim END
  211. #" Test removing 1 element
  212. VAR l = [1, 2, 3, 4]
  213. call assert_equal(1, remove(l, 0))
  214. call assert_equal([2, 3, 4], l)
  215. LET l = [1, 2, 3, 4]
  216. call assert_equal(2, remove(l, 1))
  217. call assert_equal([1, 3, 4], l)
  218. LET l = [1, 2, 3, 4]
  219. call assert_equal(4, remove(l, -1))
  220. call assert_equal([1, 2, 3], l)
  221. #" Test removing range of element(s)
  222. LET l = [1, 2, 3, 4]
  223. call assert_equal([3], remove(l, 2, 2))
  224. call assert_equal([1, 2, 4], l)
  225. LET l = [1, 2, 3, 4]
  226. call assert_equal([2, 3], remove(l, 1, 2))
  227. call assert_equal([1, 4], l)
  228. LET l = [1, 2, 3, 4]
  229. call assert_equal([2, 3], remove(l, -3, -2))
  230. call assert_equal([1, 4], l)
  231. END
  232. call CheckLegacyAndVim9Success(lines)
  233. " Test invalid cases
  234. let l = [1, 2, 3, 4]
  235. call assert_fails("call remove(l, 5)", 'E684:')
  236. call assert_fails("call remove(l, 1, 5)", 'E684:')
  237. call assert_fails("call remove(l, 3, 2)", 'E16:')
  238. call assert_fails("call remove(1, 0)", 'E896:')
  239. call assert_fails("call remove(l, l)", 'E745:')
  240. endfunc
  241. " List add() function
  242. func Test_list_add()
  243. let lines =<< trim END
  244. VAR l = []
  245. call add(l, 1)
  246. call add(l, [2, 3])
  247. call add(l, [])
  248. call add(l, v:_null_list)
  249. call add(l, {'k': 3})
  250. call add(l, {})
  251. call add(l, v:_null_dict)
  252. call assert_equal([1, [2, 3], [], [], {'k': 3}, {}, {}], l)
  253. END
  254. call CheckLegacyAndVim9Success(lines)
  255. " weird legacy behavior
  256. " call assert_equal(1, add(v:_null_list, 4))
  257. endfunc
  258. " Tests for Dictionary type
  259. func Test_dict()
  260. " Creating Dictionary directly with different types
  261. let lines =<< trim END
  262. VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}, }
  263. call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
  264. call assert_equal('asd', d.1)
  265. call assert_equal(['-1', '1', 'b'], sort(keys(d)))
  266. call assert_equal(['asd', [1, 2, function('strlen')], {'a': 1}], values(d))
  267. call extend(d, {3: 33, 1: 99})
  268. call extend(d, {'b': 'bbb', 'c': 'ccc'}, "keep")
  269. call assert_equal({'c': 'ccc', '1': 99, 'b': [1, 2, function('strlen')], '3': 33, '-1': {'a': 1}}, d)
  270. END
  271. call CheckLegacyAndVim9Success(lines)
  272. let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
  273. call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
  274. let v = []
  275. for [key, val] in items(d)
  276. call extend(v, [key, val])
  277. unlet key val
  278. endfor
  279. call assert_equal(['1','asd','b',[1, 2, function('strlen')],'-1',{'a': 1}], v)
  280. call extend(d, {3: 33, 1: 99})
  281. call assert_fails("call extend(d, {3:333,4:444}, 'error')", 'E737:')
  282. " duplicate key
  283. call assert_fails("let d = {'k' : 10, 'k' : 20}", 'E721:')
  284. " missing comma
  285. call assert_fails("let d = {'k' : 10 'k' : 20}", 'E722:')
  286. " missing curly brace
  287. call assert_fails("let d = {'k' : 10,", 'E723:')
  288. " invalid key
  289. call assert_fails('let d = #{++ : 10}', 'E15:')
  290. " wrong type for key
  291. call assert_fails('let d={[] : 10}', 'E730:')
  292. " undefined variable as value
  293. call assert_fails("let d={'k' : i}", 'E121:')
  294. " allow key starting with number at the start, not a curly expression
  295. call assert_equal({'1foo': 77}, #{1foo: 77})
  296. " #{expr} is not a curly expression
  297. let x = 'x'
  298. call assert_equal(#{g: x}, #{g:x})
  299. endfunc
  300. " Dictionary identity
  301. func Test_dict_identity()
  302. let lines =<< trim END
  303. VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1}, }
  304. VAR dd = d
  305. VAR dx = copy(d)
  306. call assert_true(d == dd)
  307. call assert_false(d isnot dd)
  308. call assert_true(d is dd)
  309. call assert_true(d == dx)
  310. call assert_false(d is dx)
  311. call assert_true(d isnot dx)
  312. END
  313. call CheckLegacyAndVim9Success(lines)
  314. endfunc
  315. " removing items with :unlet
  316. func Test_dict_unlet()
  317. let lines =<< trim END
  318. VAR d = {'b': 'bbb', '1': 99, '3': 33, '-1': {'a': 1}}
  319. unlet d.b
  320. unlet d[-1]
  321. call assert_equal({'1': 99, '3': 33}, d)
  322. END
  323. call CheckLegacyAndVim9Success(lines)
  324. endfunc
  325. " manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
  326. func Test_dict_big()
  327. let d = {}
  328. for i in range(1500)
  329. let d[i] = 3000 - i
  330. endfor
  331. call assert_equal([3000, 2900, 2001, 1600, 1501], [d[0], d[100], d[999], d[1400], d[1499]])
  332. let str = ''
  333. try
  334. let n = d[1500]
  335. catch
  336. let str = substitute(v:exception, '\v(.{14}).*( "\d{4}").*', '\1\2', '')
  337. endtry
  338. call assert_equal('Vim(let):E716: "1500"', str)
  339. " lookup each item
  340. for i in range(1500)
  341. call assert_equal(3000 - i, d[i])
  342. endfor
  343. let i += 1
  344. " delete even items
  345. while i >= 2
  346. let i -= 2
  347. unlet d[i]
  348. endwhile
  349. call assert_equal('NONE', get(d, 1500 - 100, 'NONE'))
  350. call assert_equal(2999, d[1])
  351. " delete odd items, checking value, one intentionally wrong
  352. let d[33] = 999
  353. let i = 1
  354. while i < 1500
  355. if i != 33
  356. call assert_equal(3000 - i, d[i])
  357. else
  358. call assert_equal(999, d[i])
  359. endif
  360. unlet d[i]
  361. let i += 2
  362. endwhile
  363. call assert_equal({}, d)
  364. unlet d
  365. endfunc
  366. " Dictionary function
  367. func Test_dict_func()
  368. let d = {}
  369. func d.func(a) dict
  370. return a:a . len(self.data)
  371. endfunc
  372. let d.data = [1,2,3]
  373. call assert_equal('len: 3', d.func('len: '))
  374. let x = d.func('again: ')
  375. call assert_equal('again: 3', x)
  376. let Fn = d.func
  377. call assert_equal('xxx3', Fn('xxx'))
  378. endfunc
  379. func Test_dict_assign()
  380. let d = {}
  381. let d.1 = 1
  382. let d._ = 2
  383. call assert_equal({'1': 1, '_': 2}, d)
  384. let lines =<< trim END
  385. VAR d = {}
  386. LET d.a = 1
  387. LET d._ = 2
  388. call assert_equal({'a': 1, '_': 2}, d)
  389. END
  390. call CheckLegacyAndVim9Success(lines)
  391. let lines =<< trim END
  392. let n = 0
  393. let n.key = 3
  394. END
  395. call CheckScriptFailure(lines, 'E1203: Dot can only be used on a dictionary: n.key = 3')
  396. let lines =<< trim END
  397. vim9script
  398. var n = 0
  399. n.key = 3
  400. END
  401. call CheckScriptFailure(lines, 'E1203: Dot can only be used on a dictionary: n.key = 3')
  402. let lines =<< trim END
  403. var n = 0
  404. n.key = 3
  405. END
  406. call CheckDefFailure(lines, 'E1141:')
  407. let d = {'abc': {}}
  408. call assert_fails("let d.abc[0z10] = 10", 'E976: Using a Blob as a String')
  409. endfunc
  410. " Function in script-local List or Dict
  411. func Test_script_local_dict_func()
  412. let g:dict = {}
  413. function g:dict.func() dict
  414. return 'g:dict.func' . self.foo[1] . self.foo[0]('asdf')
  415. endfunc
  416. let g:dict.foo = ['-', 2, 3]
  417. call insert(g:dict.foo, function('strlen'))
  418. call assert_equal('g:dict.func-4', g:dict.func())
  419. unlet g:dict
  420. endfunc
  421. " Test removing items in a dictionary
  422. func Test_dict_func_remove()
  423. let lines =<< trim END
  424. VAR d = {1: 'a', 2: 'b', 3: 'c'}
  425. call assert_equal('b', remove(d, 2))
  426. call assert_equal({1: 'a', 3: 'c'}, d)
  427. END
  428. call CheckLegacyAndVim9Success(lines)
  429. let lines =<< trim END
  430. VAR d = {1: 'a', 3: 'c'}
  431. call remove(d, 1, 2)
  432. END
  433. call CheckLegacyAndVim9Failure(lines, 'E118:')
  434. let lines =<< trim END
  435. VAR d = {1: 'a', 3: 'c'}
  436. call remove(d, 'a')
  437. END
  438. call CheckLegacyAndVim9Failure(lines, 'E716:')
  439. let lines =<< trim END
  440. let d = {'a-b': 55}
  441. echo d.a-b
  442. END
  443. call CheckScriptFailure(lines, 'E716: Key not present in Dictionary: "a"')
  444. let lines =<< trim END
  445. vim9script
  446. var d = {'a-b': 55}
  447. echo d.a-b
  448. END
  449. call CheckScriptFailure(lines, 'E716: Key not present in Dictionary: "a"')
  450. let lines =<< trim END
  451. var d = {'a-b': 55}
  452. echo d.a-b
  453. END
  454. call CheckDefFailure(lines, 'E1004: White space required before and after ''-''')
  455. let lines =<< trim END
  456. let d = {1: 'a', 3: 'c'}
  457. call remove(d, [])
  458. END
  459. call CheckScriptFailure(lines, 'E730:')
  460. let lines =<< trim END
  461. vim9script
  462. var d = {1: 'a', 3: 'c'}
  463. call remove(d, [])
  464. END
  465. call CheckScriptFailure(lines, 'E1174: String required for argument 2')
  466. let lines =<< trim END
  467. var d = {1: 'a', 3: 'c'}
  468. call remove(d, [])
  469. END
  470. call CheckDefExecFailure(lines, 'E1013: Argument 2: type mismatch, expected string but got list<unknown>')
  471. endfunc
  472. " Nasty: remove func from Dict that's being called (works)
  473. func Test_dict_func_remove_in_use()
  474. let d = {1:1}
  475. func d.func(a)
  476. return "a:" . a:a
  477. endfunc
  478. let expected = 'a:' . string(get(d, 'func'))
  479. call assert_equal(expected, d.func(string(remove(d, 'func'))))
  480. " similar, in a way it also works in Vim9
  481. let lines =<< trim END
  482. VAR d = {1: 1, 2: 'x'}
  483. func GetArg(a)
  484. return "a:" .. a:a
  485. endfunc
  486. LET d.func = function('GetArg')
  487. VAR expected = 'a:' .. string(get(d, 'func'))
  488. call assert_equal(expected, d.func(string(remove(d, 'func'))))
  489. END
  490. call CheckTransLegacySuccess(lines)
  491. call CheckTransVim9Success(lines)
  492. endfunc
  493. func Test_dict_literal_keys()
  494. call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, #{one: 1, two2: 2, 3three: 3, 44: 4},)
  495. " why *{} cannot be used for a literal dictionary
  496. let blue = 'blue'
  497. call assert_equal('6', trim(execute('echo 2 *{blue: 3}.blue')))
  498. endfunc
  499. " Nasty: deepcopy() dict that refers to itself (fails when noref used)
  500. func Test_dict_deepcopy()
  501. let lines =<< trim END
  502. VAR d = {1: 1, 2: '2'}
  503. VAR l = [4, d, 6]
  504. LET d[3] = l
  505. VAR dc = deepcopy(d)
  506. call deepcopy(d, 1)
  507. END
  508. call CheckLegacyAndVim9Failure(lines, 'E698:')
  509. let lines =<< trim END
  510. VAR d = {1: 1, 2: '2'}
  511. VAR l = [4, d, 6]
  512. LET d[3] = l
  513. VAR l2 = [0, l, l, 3]
  514. LET l[1] = l2
  515. VAR l3 = deepcopy(l2)
  516. call assert_true(l3[1] is l3[2])
  517. END
  518. call CheckLegacyAndVim9Success(lines)
  519. call assert_fails("call deepcopy([1, 2], 2)", 'E1212:')
  520. endfunc
  521. " Locked variables
  522. func Test_list_locked_var()
  523. " Not tested with :def function, local vars cannot be locked.
  524. let lines =<< trim END
  525. VAR expected = [
  526. \ [['1000-000', 'ppppppF'],
  527. \ ['0000-000', 'ppppppp'],
  528. \ ['0000-000', 'ppppppp']],
  529. \ [['1000-000', 'ppppppF'],
  530. \ ['0000-000', 'ppppppp'],
  531. \ ['0000-000', 'ppppppp']],
  532. \ [['1100-100', 'ppFppFF'],
  533. \ ['0000-000', 'ppppppp'],
  534. \ ['0000-000', 'ppppppp']],
  535. \ [['1110-110', 'pFFpFFF'],
  536. \ ['0010-010', 'pFppFpp'],
  537. \ ['0000-000', 'ppppppp']],
  538. \ [['1111-111', 'FFFFFFF'],
  539. \ ['0011-011', 'FFpFFpp'],
  540. \ ['0000-000', 'ppppppp']]
  541. \ ]
  542. for depth in range(5)
  543. for u in range(3)
  544. VAR l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
  545. exe "lockvar " .. depth .. " l"
  546. if u == 1
  547. exe "unlockvar l"
  548. elseif u == 2
  549. exe "unlockvar " .. depth .. " l"
  550. endif
  551. VAR ps = islocked("l") .. islocked("l[1]") .. islocked("l[1][1]") .. islocked("l[1][1][0]") .. '-' .. islocked("l[2]") .. islocked("l[2]['6']") .. islocked("l[2]['6'][7]")
  552. call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
  553. LET ps = ''
  554. try
  555. LET l[1][1][0] = 99
  556. LET ps ..= 'p'
  557. catch
  558. LET ps ..= 'F'
  559. endtry
  560. try
  561. LET l[1][1] = [99]
  562. LET ps ..= 'p'
  563. catch
  564. LET ps ..= 'F'
  565. endtry
  566. try
  567. LET l[1] = [99]
  568. LET ps ..= 'p'
  569. catch
  570. LET ps ..= 'F'
  571. endtry
  572. try
  573. LET l[2]['6'][7] = 99
  574. LET ps ..= 'p'
  575. catch
  576. LET ps ..= 'F'
  577. endtry
  578. try
  579. LET l[2][6] = {99: 99}
  580. LET ps ..= 'p'
  581. catch
  582. LET ps ..= 'F'
  583. endtry
  584. try
  585. LET l[2] = {99: 99}
  586. LET ps ..= 'p'
  587. catch
  588. LET ps ..= 'F'
  589. endtry
  590. try
  591. LET l = [99]
  592. LET ps ..= 'p'
  593. catch
  594. LET ps ..= 'F'
  595. endtry
  596. call assert_equal(expected[depth][u][1], ps, 'depth: ' .. depth)
  597. unlock! l
  598. endfor
  599. endfor
  600. END
  601. call CheckTransLegacySuccess(lines)
  602. call CheckTransVim9Success(lines)
  603. call assert_fails("let x=islocked('a b')", 'E488:')
  604. let mylist = [1, 2, 3]
  605. call assert_fails("let x = islocked('mylist[1:2]')", 'E786:')
  606. let mydict = {'k' : 'v'}
  607. call assert_fails("let x = islocked('mydict.a')", 'E716:')
  608. endfunc
  609. " Unletting locked variables
  610. func Test_list_locked_var_unlet()
  611. " Not tested with Vim9: script and local variables cannot be unlocked
  612. let expected = [
  613. \ [['1000-000', 'ppppppp'],
  614. \ ['0000-000', 'ppppppp'],
  615. \ ['0000-000', 'ppppppp']],
  616. \ [['1000-000', 'ppFppFp'],
  617. \ ['0000-000', 'ppppppp'],
  618. \ ['0000-000', 'ppppppp']],
  619. \ [['1100-100', 'pFFpFFp'],
  620. \ ['0000-000', 'ppppppp'],
  621. \ ['0000-000', 'ppppppp']],
  622. \ [['1110-110', 'FFFFFFp'],
  623. \ ['0010-010', 'FppFppp'],
  624. \ ['0000-000', 'ppppppp']],
  625. \ [['1111-111', 'FFFFFFp'],
  626. \ ['0011-011', 'FppFppp'],
  627. \ ['0000-000', 'ppppppp']]
  628. \ ]
  629. for depth in range(5)
  630. for u in range(3)
  631. unlet! l
  632. let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
  633. exe "lockvar " . depth . " l"
  634. if u == 1
  635. exe "unlockvar l"
  636. elseif u == 2
  637. exe "unlockvar " . depth . " l"
  638. endif
  639. let ps = islocked("l").islocked("l[1]").islocked("l[1][1]").islocked("l[1][1][0]").'-'.islocked("l[2]").islocked("l[2]['6']").islocked("l[2]['6'][7]")
  640. call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
  641. let ps = ''
  642. try
  643. unlet l[2]['6'][7]
  644. let ps .= 'p'
  645. catch
  646. let ps .= 'F'
  647. endtry
  648. try
  649. unlet l[2][6]
  650. let ps .= 'p'
  651. catch
  652. let ps .= 'F'
  653. endtry
  654. try
  655. unlet l[2]
  656. let ps .= 'p'
  657. catch
  658. let ps .= 'F'
  659. endtry
  660. try
  661. unlet l[1][1][0]
  662. let ps .= 'p'
  663. catch
  664. let ps .= 'F'
  665. endtry
  666. try
  667. unlet l[1][1]
  668. let ps .= 'p'
  669. catch
  670. let ps .= 'F'
  671. endtry
  672. try
  673. unlet l[1]
  674. let ps .= 'p'
  675. catch
  676. let ps .= 'F'
  677. endtry
  678. try
  679. unlet l
  680. let ps .= 'p'
  681. catch
  682. let ps .= 'F'
  683. endtry
  684. call assert_equal(expected[depth][u][1], ps)
  685. endfor
  686. endfor
  687. " Deleting a list range with locked items works, but changing the items
  688. " fails.
  689. let l = [1, 2, 3, 4]
  690. lockvar l[1:2]
  691. call assert_fails('let l[1:2] = [8, 9]', 'E741:')
  692. unlet l[1:2]
  693. call assert_equal([1, 4], l)
  694. unlet l
  695. endfunc
  696. " Locked variables and :unlet or list / dict functions
  697. " No :unlet after lock on dict:
  698. func Test_dict_lock_unlet()
  699. let d = {'a': 99, 'b': 100}
  700. lockvar 1 d
  701. call assert_fails('unlet d.a', 'E741:')
  702. endfunc
  703. " unlet after lock on dict item
  704. func Test_dict_item_lock_unlet()
  705. let lines =<< trim END
  706. VAR d = {'a': 99, 'b': 100}
  707. lockvar d.a
  708. unlet d.a
  709. call assert_equal({'b': 100}, d)
  710. END
  711. " TODO: make this work in a :def function
  712. "call CheckLegacyAndVim9Success(lines)
  713. call CheckTransLegacySuccess(lines)
  714. call CheckTransVim9Success(lines)
  715. endfunc
  716. " filter() after lock on dict item
  717. func Test_dict_lock_filter()
  718. let lines =<< trim END
  719. VAR d = {'a': 99, 'b': 100}
  720. lockvar d.a
  721. call filter(d, 'v:key != "a"')
  722. call assert_equal({'b': 100}, d)
  723. END
  724. " TODO: make this work in a :def function
  725. "call CheckLegacyAndVim9Success(lines)
  726. call CheckTransLegacySuccess(lines)
  727. call CheckTransVim9Success(lines)
  728. endfunc
  729. " map() after lock on dict
  730. func Test_dict_lock_map()
  731. let lines =<< trim END
  732. VAR d = {'a': 99, 'b': 100}
  733. lockvar 1 d
  734. call map(d, 'v:val + 200')
  735. call assert_equal({'a': 299, 'b': 300}, d)
  736. END
  737. " This won't work in a :def function
  738. call CheckTransLegacySuccess(lines)
  739. call CheckTransVim9Success(lines)
  740. endfunc
  741. " No extend() after lock on dict item
  742. func Test_dict_lock_extend()
  743. let d = {'a': 99, 'b': 100}
  744. lockvar d.a
  745. call assert_fails("call extend(d, {'a' : 123})", 'E741:')
  746. call assert_equal({'a': 99, 'b': 100}, d)
  747. endfunc
  748. " Cannot use += with a locked dict
  749. func Test_dict_lock_operator()
  750. let d = {}
  751. lockvar d
  752. call assert_fails("let d += {'k' : 10}", 'E741:')
  753. unlockvar d
  754. endfunc
  755. " No remove() of write-protected scope-level variable
  756. func Tfunc1(this_is_a_long_parameter_name)
  757. call assert_fails("call remove(a:, 'this_is_a_long_parameter_name')", 'E742:')
  758. endfunc
  759. func Test_dict_scope_var_remove()
  760. call Tfunc1('testval')
  761. endfunc
  762. " No extend() of write-protected scope-level variable
  763. func Test_dict_scope_var_extend()
  764. call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
  765. endfunc
  766. func Tfunc2(this_is_a_long_parameter_name)
  767. call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
  768. endfunc
  769. func Test_dict_scope_var_extend_overwrite()
  770. call Tfunc2('testval')
  771. endfunc
  772. " No :unlet of variable in locked scope
  773. func Test_lock_var_unlet()
  774. let b:testvar = 123
  775. lockvar 1 b:
  776. call assert_fails('unlet b:testvar', 'E741:')
  777. unlockvar 1 b:
  778. unlet! b:testvar
  779. endfunc
  780. " No :let += of locked list variable
  781. func Test_let_lock_list()
  782. let l = ['a', 'b', 3]
  783. lockvar 1 l
  784. call assert_fails("let l += ['x']", 'E741:')
  785. call assert_equal(['a', 'b', 3], l)
  786. unlet l
  787. let l = [1, 2, 3, 4]
  788. lockvar! l
  789. call assert_equal([1, 2, 3, 4], l)
  790. unlockvar l[1]
  791. call assert_fails('unlet l[0:1]', 'E741:')
  792. call assert_equal([1, 2, 3, 4], l)
  793. call assert_fails('unlet l[1:2]', 'E741:')
  794. call assert_equal([1, 2, 3, 4], l)
  795. unlockvar l[1]
  796. call assert_fails('let l[0:1] = [0, 1]', 'E741:')
  797. call assert_equal([1, 2, 3, 4], l)
  798. call assert_fails('let l[1:2] = [0, 1]', 'E741:')
  799. call assert_equal([1, 2, 3, 4], l)
  800. unlet l
  801. endfunc
  802. " Locking part of the list
  803. func Test_let_lock_list_items()
  804. let l = [1, 2, 3, 4]
  805. lockvar l[2:]
  806. call assert_equal(0, islocked('l[0]'))
  807. call assert_equal(1, islocked('l[2]'))
  808. call assert_equal(1, islocked('l[3]'))
  809. call assert_fails('let l[2] = 10', 'E741:')
  810. call assert_fails('let l[3] = 20', 'E741:')
  811. unlet l
  812. endfunc
  813. " lockvar/islocked() triggering script autoloading
  814. func Test_lockvar_script_autoload()
  815. let old_rtp = &rtp
  816. set rtp+=./sautest
  817. lockvar g:footest#x
  818. unlockvar g:footest#x
  819. call assert_equal(-1, 'g:footest#x'->islocked())
  820. call assert_equal(0, exists('g:footest#x'))
  821. call assert_equal(1, g:footest#x)
  822. let &rtp = old_rtp
  823. endfunc
  824. " a:000 function argument test
  825. func s:arg_list_test(...)
  826. call assert_fails('let a:000 = [1, 2]', 'E46:')
  827. call assert_fails('let a:000[0] = 9', 'E742:')
  828. call assert_fails('let a:000[2] = [9, 10]', 'E742:')
  829. call assert_fails('let a:000[3] = {9 : 10}', 'E742:')
  830. " now the tests that should pass
  831. let a:000[2][1] = 9
  832. call extend(a:000[2], [5, 6])
  833. let a:000[3][5] = 8
  834. let a:000[3]['a'] = 12
  835. call assert_equal([1, 2, [3, 9, 5, 6], {'a': 12, '5': 8}], a:000)
  836. endfunc
  837. func Test_func_arg_list()
  838. call s:arg_list_test(1, 2, [3, 4], {5: 6})
  839. endfunc
  840. " Tests for reverse(), sort(), uniq()
  841. func Test_reverse_sort_uniq()
  842. let lines =<< trim END
  843. VAR l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
  844. call assert_equal(['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5], uniq(copy(l)))
  845. call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(l))
  846. call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(reverse(l)))
  847. if has('float')
  848. call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(l))
  849. call assert_equal([[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0'], reverse(sort(l)))
  850. call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(reverse(sort(l))))
  851. call assert_equal(['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]], uniq(sort(l)))
  852. LET l = [7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four']
  853. call assert_equal([-1, 'one', 'two', 'three', 'four', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255], sort(copy(l), 'n'))
  854. LET l = [7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
  855. call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 'i'))
  856. call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 'i'))
  857. call assert_equal(['BAR', 'Bar', 'FOO', 'FOOBAR', 'Foo', 'bar', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l)))
  858. endif
  859. END
  860. call CheckLegacyAndVim9Success(lines)
  861. call assert_fails('call reverse({})', 'E1252:')
  862. call assert_fails('call uniq([1, 2], {x, y -> []})', 'E745:')
  863. call assert_fails("call sort([1, 2], function('min'), 1)", "E1206:")
  864. call assert_fails("call sort([1, 2], function('invalid_func'))", "E700:")
  865. call assert_fails("call sort([1, 2], function('min'))", "E118:")
  866. let lines =<< trim END
  867. call sort(['a', 'b'], 0)
  868. END
  869. call CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
  870. let lines =<< trim END
  871. call sort(['a', 'b'], 1)
  872. END
  873. call CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
  874. endfunc
  875. " reduce a list, blob or string
  876. func Test_reduce()
  877. let lines =<< trim END
  878. call assert_equal(1, reduce([], LSTART acc, val LMIDDLE acc + val LEND, 1))
  879. call assert_equal(10, reduce([1, 3, 5], LSTART acc, val LMIDDLE acc + val LEND, 1))
  880. call assert_equal(2 * (2 * ((2 * 1) + 2) + 3) + 4, reduce([2, 3, 4], LSTART acc, val LMIDDLE 2 * acc + val LEND, 1))
  881. call assert_equal('a x y z', ['x', 'y', 'z']->reduce(LSTART acc, val LMIDDLE acc .. ' ' .. val LEND, 'a'))
  882. call assert_equal([0, 1, 2, 3], reduce([1, 2, 3], function('add'), [0]))
  883. VAR l = ['x', 'y', 'z']
  884. call assert_equal(42, reduce(l, function('get'), {'x': {'y': {'z': 42 } } }))
  885. call assert_equal(['x', 'y', 'z'], l)
  886. call assert_equal(1, reduce([1], LSTART acc, val LMIDDLE acc + val LEND))
  887. call assert_equal('x y z', reduce(['x', 'y', 'z'], LSTART acc, val LMIDDLE acc .. ' ' .. val LEND))
  888. call assert_equal(120, range(1, 5)->reduce(LSTART acc, val LMIDDLE acc * val LEND))
  889. call assert_equal(1, reduce(0z, LSTART acc, val LMIDDLE acc + val LEND, 1))
  890. call assert_equal(1 + 0xaf + 0xbf + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE acc + val LEND, 1))
  891. call assert_equal(2 * (2 * 1 + 0xaf) + 0xbf, 0zAFBF->reduce(LSTART acc, val LMIDDLE 2 * acc + val LEND, 1))
  892. call assert_equal(0xff, reduce(0zff, LSTART acc, val LMIDDLE acc + val LEND))
  893. call assert_equal(2 * (2 * 0xaf + 0xbf) + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE 2 * acc + val LEND))
  894. call assert_equal('x,y,z', 'xyz'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
  895. call assert_equal('', ''->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, ''))
  896. call assert_equal('あ,い,う,え,お,😊,💕', 'あいうえお😊💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
  897. call assert_equal('😊,あ,い,う,え,お,💕', 'あいうえお💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, '😊'))
  898. call assert_equal('ऊ,ॠ,ॡ', reduce('ऊॠॡ', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
  899. call assert_equal('c,à,t', reduce('càt', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
  900. call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
  901. call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
  902. call assert_equal(',a,b,c', reduce('abc', LSTART acc, val LMIDDLE acc .. ',' .. val LEND, v:_null_string))
  903. call assert_equal(0x7d, reduce([0x30, 0x25, 0x08, 0x61], 'or'))
  904. call assert_equal(0x7d, reduce(0z30250861, 'or'))
  905. call assert_equal('β', reduce('ββββ', 'matchstr'))
  906. END
  907. call CheckLegacyAndVim9Success(lines)
  908. call assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce({ acc, val -> extend(acc, { val: 1 }) }, {}))
  909. " vim9 assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce((acc, val) => extend(acc, {[val]: 1 }), {}))
  910. call assert_fails("call reduce([], { acc, val -> acc + val })", 'E998: Reduce of an empty List with no initial value')
  911. call assert_fails("call reduce(0z, { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value')
  912. call assert_fails("call reduce(v:_null_blob, { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value')
  913. call assert_fails("call reduce('', { acc, val -> acc + val })", 'E998: Reduce of an empty String with no initial value')
  914. call assert_fails("call reduce(v:_null_string, { acc, val -> acc + val })", 'E998: Reduce of an empty String with no initial value')
  915. call assert_fails("call reduce({}, { acc, val -> acc + val }, 1)", 'E1098:')
  916. call assert_fails("call reduce(0, { acc, val -> acc + val }, 1)", 'E1098:')
  917. call assert_fails("call reduce([1, 2], 'Xdoes_not_exist')", 'E117:')
  918. call assert_fails("echo reduce(0z01, { acc, val -> 2 * acc + val }, '')", 'E1210:')
  919. " call assert_fails("vim9 reduce(0, (acc, val) => (acc .. val), '')", 'E1252:')
  920. " call assert_fails("vim9 reduce({}, (acc, val) => (acc .. val), '')", 'E1252:')
  921. " call assert_fails("vim9 reduce(0.1, (acc, val) => (acc .. val), '')", 'E1252:')
  922. " call assert_fails("vim9 reduce(function('tr'), (acc, val) => (acc .. val), '')", 'E1252:')
  923. call assert_fails("call reduce('', { acc, val -> acc + val }, 1)", 'E1174:')
  924. call assert_fails("call reduce('', { acc, val -> acc + val }, {})", 'E1174:')
  925. call assert_fails("call reduce('', { acc, val -> acc + val }, 0.1)", 'E1174:')
  926. call assert_fails("call reduce('', { acc, val -> acc + val }, function('tr'))", 'E1174:')
  927. call assert_fails("call reduce('abc', { a, v -> a10}, '')", 'E121:')
  928. call assert_fails("call reduce(0z0102, { a, v -> a10}, 1)", 'E121:')
  929. call assert_fails("call reduce([1, 2], { a, v -> a10}, '')", 'E121:')
  930. let g:lut = [1, 2, 3, 4]
  931. func EvilRemove()
  932. call remove(g:lut, 1)
  933. return 1
  934. endfunc
  935. call assert_fails("call reduce(g:lut, { acc, val -> EvilRemove() }, 1)", 'E742:')
  936. unlet g:lut
  937. delfunc EvilRemove
  938. call assert_equal(42, reduce(v:_null_list, function('add'), 42))
  939. call assert_equal(42, reduce(v:_null_blob, function('add'), 42))
  940. " should not crash
  941. " Nvim doesn't have null functions
  942. " call assert_fails('echo reduce([1], test_null_function())', 'E1132:')
  943. " Nvim doesn't have null partials
  944. " call assert_fails('echo reduce([1], test_null_partial())', 'E1132:')
  945. endfunc
  946. " splitting a string to a List using split()
  947. func Test_str_split()
  948. let lines =<< trim END
  949. call assert_equal(['aa', 'bb'], split(' aa bb '))
  950. call assert_equal(['aa', 'bb'], split(' aa bb ', '\W\+', 0))
  951. call assert_equal(['', 'aa', 'bb', ''], split(' aa bb ', '\W\+', 1))
  952. call assert_equal(['', '', 'aa', '', 'bb', '', ''], split(' aa bb ', '\W', 1))
  953. call assert_equal(['aa', '', 'bb'], split(':aa::bb:', ':', 0))
  954. call assert_equal(['', 'aa', '', 'bb', ''], split(':aa::bb:', ':', 1))
  955. call assert_equal(['aa', '', 'bb', 'cc', ''], split('aa,,bb, cc,', ',\s*', 1))
  956. call assert_equal(['a', 'b', 'c'], split('abc', '\zs'))
  957. call assert_equal(['', 'a', '', 'b', '', 'c', ''], split('abc', '\zs', 1))
  958. call assert_equal(['abc'], split('abc', '\\%('))
  959. END
  960. call CheckLegacyAndVim9Success(lines)
  961. call assert_fails("call split('abc', [])", 'E730:')
  962. call assert_fails("call split('abc', 'b', [])", 'E745:')
  963. endfunc
  964. " compare recursively linked list and dict
  965. func Test_listdict_compare()
  966. let lines =<< trim END
  967. VAR l = [1, 2, 3, '4']
  968. VAR d = {'1': 1, '2': l, '3': 3}
  969. LET l[1] = d
  970. call assert_true(l == l)
  971. call assert_true(d == d)
  972. call assert_false(l != deepcopy(l))
  973. call assert_false(d != deepcopy(d))
  974. END
  975. call CheckLegacyAndVim9Success(lines)
  976. " comparison errors
  977. call assert_fails('echo [1, 2] =~ {}', 'E691:')
  978. call assert_fails('echo [1, 2] =~ [1, 2]', 'E692:')
  979. call assert_fails('echo {} =~ 5', 'E735:')
  980. call assert_fails('echo {} =~ {}', 'E736:')
  981. endfunc
  982. func Test_recursive_listdict_compare()
  983. let l1 = [0, 1]
  984. let l1[0] = l1
  985. let l2 = [0, 1]
  986. let l2[0] = l2
  987. call assert_true(l1 == l2)
  988. let d1 = {0: 0, 1: 1}
  989. let d1[0] = d1
  990. let d2 = {0: 0, 1: 1}
  991. let d2[0] = d2
  992. call assert_true(d1 == d2)
  993. endfunc
  994. " compare complex recursively linked list and dict
  995. func Test_listdict_compare_complex()
  996. let lines =<< trim END
  997. VAR l = []
  998. call add(l, l)
  999. VAR dict4 = {"l": l}
  1000. call add(dict4.l, dict4)
  1001. VAR lcopy = deepcopy(l)
  1002. VAR dict4copy = deepcopy(dict4)
  1003. call assert_true(l == lcopy)
  1004. call assert_true(dict4 == dict4copy)
  1005. END
  1006. call CheckLegacyAndVim9Success(lines)
  1007. endfunc
  1008. " Test for extending lists and dictionaries
  1009. func Test_listdict_extend()
  1010. " Test extend() with lists
  1011. " Pass the same List to extend()
  1012. let lines =<< trim END
  1013. VAR l = [1, 2, 3]
  1014. call assert_equal([1, 2, 3, 1, 2, 3], extend(l, l))
  1015. call assert_equal([1, 2, 3, 1, 2, 3], l)
  1016. LET l = [1, 2, 3]
  1017. call assert_equal([1, 2, 3, 4, 5, 6], extend(l, [4, 5, 6]))
  1018. call assert_equal([1, 2, 3, 4, 5, 6], l)
  1019. LET l = [1, 2, 3]
  1020. call extend(l, [4, 5, 6], 0)
  1021. call assert_equal([4, 5, 6, 1, 2, 3], l)
  1022. LET l = [1, 2, 3]
  1023. call extend(l, [4, 5, 6], 1)
  1024. call assert_equal([1, 4, 5, 6, 2, 3], l)
  1025. LET l = [1, 2, 3]
  1026. call extend(l, [4, 5, 6], 3)
  1027. call assert_equal([1, 2, 3, 4, 5, 6], l)
  1028. LET l = [1, 2, 3]
  1029. call extend(l, [4, 5, 6], -1)
  1030. call assert_equal([1, 2, 4, 5, 6, 3], l)
  1031. LET l = [1, 2, 3]
  1032. call extend(l, [4, 5, 6], -3)
  1033. call assert_equal([4, 5, 6, 1, 2, 3], l)
  1034. END
  1035. call CheckLegacyAndVim9Success(lines)
  1036. let l = [1, 2, 3]
  1037. call assert_fails("call extend(l, [4, 5, 6], 4)", 'E684:')
  1038. call assert_fails("call extend(l, [4, 5, 6], -4)", 'E684:')
  1039. if has('float')
  1040. call assert_fails("call extend(l, [4, 5, 6], 1.2)", 'E805:')
  1041. endif
  1042. " Test extend() with dictionaries.
  1043. " Pass the same Dict to extend()
  1044. let lines =<< trim END
  1045. VAR d = {'a': {'b': 'B'}, 'x': 9}
  1046. call extend(d, d)
  1047. call assert_equal({'a': {'b': 'B'}, 'x': 9}, d)
  1048. LET d = {'a': 'A', 'b': 9}
  1049. call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, extend(d, {'b': 0, 'c': 'C'}))
  1050. call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
  1051. LET d = {'a': 'A', 'b': 9}
  1052. call extend(d, {'a': 'A', 'b': 0, 'c': 'C'}, "force")
  1053. call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
  1054. LET d = {'a': 'A', 'b': 9}
  1055. call extend(d, {'b': 0, 'c': 'C'}, "keep")
  1056. call assert_equal({'a': 'A', 'b': 9, 'c': 'C'}, d)
  1057. END
  1058. call CheckLegacyAndVim9Success(lines)
  1059. let d = {'a': 'A', 'b': 'B'}
  1060. call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:')
  1061. call assert_fails("call extend(d, {'b': 0}, [])", 'E730:')
  1062. call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:')
  1063. if has('float')
  1064. call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E475:')
  1065. endif
  1066. call assert_equal({'a': 'A', 'b': 'B'}, d)
  1067. call assert_fails("call extend([1, 2], 1)", 'E712:')
  1068. call assert_fails("call extend([1, 2], {})", 'E712:')
  1069. " Extend g: dictionary with an invalid variable name
  1070. call assert_fails("call extend(g:, {'-!' : 10})", 'E461:')
  1071. " Extend a list with itself.
  1072. let lines =<< trim END
  1073. VAR l = [1, 5, 7]
  1074. call extend(l, l, 0)
  1075. call assert_equal([1, 5, 7, 1, 5, 7], l)
  1076. LET l = [1, 5, 7]
  1077. call extend(l, l, 1)
  1078. call assert_equal([1, 1, 5, 7, 5, 7], l)
  1079. LET l = [1, 5, 7]
  1080. call extend(l, l, 2)
  1081. call assert_equal([1, 5, 1, 5, 7, 7], l)
  1082. LET l = [1, 5, 7]
  1083. call extend(l, l, 3)
  1084. call assert_equal([1, 5, 7, 1, 5, 7], l)
  1085. END
  1086. call CheckLegacyAndVim9Success(lines)
  1087. endfunc
  1088. func Test_listdict_extendnew()
  1089. " Test extendnew() with lists
  1090. let l = [1, 2, 3]
  1091. call assert_equal([1, 2, 3, 4, 5], extendnew(l, [4, 5]))
  1092. call assert_equal([1, 2, 3], l)
  1093. lockvar l
  1094. call assert_equal([1, 2, 3, 4, 5], extendnew(l, [4, 5]))
  1095. " Test extendnew() with dictionaries.
  1096. let d = {'a': {'b': 'B'}}
  1097. call assert_equal({'a': {'b': 'B'}, 'c': 'cc'}, extendnew(d, {'c': 'cc'}))
  1098. call assert_equal({'a': {'b': 'B'}}, d)
  1099. lockvar d
  1100. call assert_equal({'a': {'b': 'B'}, 'c': 'cc'}, extendnew(d, {'c': 'cc'}))
  1101. endfunc
  1102. func s:check_scope_dict(x, fixed)
  1103. func s:gen_cmd(cmd, x)
  1104. return substitute(a:cmd, '\<x\ze:', a:x, 'g')
  1105. endfunc
  1106. let cmd = s:gen_cmd('let x:foo = 1', a:x)
  1107. if a:fixed
  1108. call assert_fails(cmd, 'E461')
  1109. else
  1110. exe cmd
  1111. exe s:gen_cmd('call assert_equal(1, x:foo)', a:x)
  1112. endif
  1113. let cmd = s:gen_cmd('let x:["bar"] = 2', a:x)
  1114. if a:fixed
  1115. call assert_fails(cmd, 'E461')
  1116. else
  1117. exe cmd
  1118. exe s:gen_cmd('call assert_equal(2, x:bar)', a:x)
  1119. endif
  1120. let cmd = s:gen_cmd('call extend(x:, {"baz": 3})', a:x)
  1121. if a:fixed
  1122. call assert_fails(cmd, 'E742')
  1123. else
  1124. exe cmd
  1125. exe s:gen_cmd('call assert_equal(3, x:baz)', a:x)
  1126. endif
  1127. if a:fixed
  1128. if a:x ==# 'a'
  1129. call assert_fails('unlet a:x', 'E795')
  1130. call assert_fails('call remove(a:, "x")', 'E742')
  1131. elseif a:x ==# 'v'
  1132. call assert_fails('unlet v:count', 'E795')
  1133. call assert_fails('call remove(v:, "count")', 'E742')
  1134. endif
  1135. else
  1136. exe s:gen_cmd('unlet x:foo', a:x)
  1137. exe s:gen_cmd('unlet x:bar', a:x)
  1138. exe s:gen_cmd('call remove(x:, "baz")', a:x)
  1139. endif
  1140. delfunc s:gen_cmd
  1141. endfunc
  1142. func Test_scope_dict()
  1143. " Test for g:
  1144. call s:check_scope_dict('g', v:false)
  1145. " Test for s:
  1146. call s:check_scope_dict('s', v:false)
  1147. " Test for l:
  1148. call s:check_scope_dict('l', v:false)
  1149. " Test for a:
  1150. call s:check_scope_dict('a', v:true)
  1151. " Test for b:
  1152. call s:check_scope_dict('b', v:false)
  1153. " Test for w:
  1154. call s:check_scope_dict('w', v:false)
  1155. " Test for t:
  1156. call s:check_scope_dict('t', v:false)
  1157. " Test for v:
  1158. call s:check_scope_dict('v', v:true)
  1159. endfunc
  1160. " Test for deep nesting of lists (> 100)
  1161. func Test_deep_nested_list()
  1162. let deep_list = []
  1163. let l = deep_list
  1164. for i in range(102)
  1165. let newlist = []
  1166. call add(l, newlist)
  1167. let l = newlist
  1168. endfor
  1169. call add(l, 102)
  1170. call assert_fails('let m = deepcopy(deep_list)', 'E698:')
  1171. call assert_fails('lockvar 110 deep_list', 'E743:')
  1172. call assert_fails('unlockvar 110 deep_list', 'E743:')
  1173. " Nvim implements :echo very differently
  1174. " call assert_fails('let x = execute("echo deep_list")', 'E724:')
  1175. call test_garbagecollect_now()
  1176. unlet deep_list
  1177. endfunc
  1178. " Test for deep nesting of dicts (> 100)
  1179. func Test_deep_nested_dict()
  1180. let deep_dict = {}
  1181. let d = deep_dict
  1182. for i in range(102)
  1183. let newdict = {}
  1184. let d.k = newdict
  1185. let d = newdict
  1186. endfor
  1187. let d.k = 'v'
  1188. call assert_fails('let m = deepcopy(deep_dict)', 'E698:')
  1189. call assert_fails('lockvar 110 deep_dict', 'E743:')
  1190. call assert_fails('unlockvar 110 deep_dict', 'E743:')
  1191. " Nvim implements :echo very differently
  1192. " call assert_fails('let x = execute("echo deep_dict")', 'E724:')
  1193. call test_garbagecollect_now()
  1194. unlet deep_dict
  1195. endfunc
  1196. " List and dict indexing tests
  1197. func Test_listdict_index()
  1198. call CheckLegacyAndVim9Failure(['echo function("min")[0]'], 'E695:')
  1199. call CheckLegacyAndVim9Failure(['echo v:true[0]'], 'E909:')
  1200. call CheckLegacyAndVim9Failure(['echo v:null[0]'], 'E909:')
  1201. call CheckLegacyAndVim9Failure(['VAR d = {"k": 10}', 'echo d.'], ['E15:', 'E1127:', 'E15:'])
  1202. call CheckLegacyAndVim9Failure(['VAR d = {"k": 10}', 'echo d[1 : 2]'], 'E719:')
  1203. call assert_fails("let v = [4, 6][{-> 1}]", 'E729:')
  1204. call CheckDefAndScriptFailure(['var v = [4, 6][() => 1]'], ['E1012', 'E703:'])
  1205. call CheckLegacyAndVim9Failure(['VAR v = range(5)[2 : []]'], ['E730:', 'E1012:', 'E730:'])
  1206. call assert_fails("let v = range(5)[2:{-> 2}(]", ['E15:', 'E116:'])
  1207. call CheckDefAndScriptFailure(['var v = range(5)[2 : () => 2(]'], 'E15:')
  1208. call CheckLegacyAndVim9Failure(['VAR v = range(5)[2 : 3'], ['E111:', 'E1097:', 'E111:'])
  1209. call CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, 10)'], 'E684:')
  1210. call CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, -10)'], 'E684:')
  1211. call CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, [])'], ['E745:', 'E1013:', 'E1210:'])
  1212. call CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[i] = 3'], ['E121:', 'E1001:', 'E121:'])
  1213. call CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[1.1] = 4'], ['E805:', 'E1012:', 'E805:'])
  1214. call CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[: i] = [4, 5]'], ['E121:', 'E1001:', 'E121:'])
  1215. call CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[: 3.2] = [4, 5]'], ['E805:', 'E1012:', 'E805:'])
  1216. " call CheckLegacyAndVim9Failure(['VAR t = test_unknown()', 'echo t[0]'], ['E685:', 'E909:', 'E685:'])
  1217. endfunc
  1218. " Test for a null list
  1219. func Test_null_list()
  1220. let l = v:_null_list
  1221. call assert_equal('', join(l))
  1222. call assert_equal(0, len(l))
  1223. call assert_equal(1, empty(l))
  1224. call assert_fails('let s = join([1, 2], [])', 'E730:')
  1225. call assert_equal([], split(v:_null_string))
  1226. call assert_equal([], l[:2])
  1227. call assert_true([] == l)
  1228. call assert_equal('[]', string(l))
  1229. " call assert_equal(0, sort(l))
  1230. " call assert_equal(0, sort(l))
  1231. " call assert_equal(0, uniq(l))
  1232. let k = [] + l
  1233. call assert_equal([], k)
  1234. let k = l + []
  1235. call assert_equal([], k)
  1236. call assert_equal(0, len(copy(l)))
  1237. call assert_equal(0, count(l, 5))
  1238. call assert_equal([], deepcopy(l))
  1239. call assert_equal(5, get(l, 2, 5))
  1240. call assert_equal(-1, index(l, 2, 5))
  1241. " call assert_equal(0, insert(l, 2, -1))
  1242. call assert_equal(0, min(l))
  1243. call assert_equal(0, max(l))
  1244. " call assert_equal(0, remove(l, 0, 2))
  1245. call assert_equal([], repeat(l, 2))
  1246. " call assert_equal(0, reverse(l))
  1247. " call assert_equal(0, sort(l))
  1248. call assert_equal('[]', string(l))
  1249. " call assert_equal(0, extend(l, l, 0))
  1250. lockvar l
  1251. call assert_equal(1, islocked('l'))
  1252. unlockvar l
  1253. endfunc
  1254. " Test for a null dict
  1255. func Test_null_dict()
  1256. call assert_equal(v:_null_dict, v:_null_dict)
  1257. let d = v:_null_dict
  1258. call assert_equal({}, d)
  1259. call assert_equal(0, len(d))
  1260. call assert_equal(1, empty(d))
  1261. call assert_equal([], items(d))
  1262. call assert_equal([], keys(d))
  1263. call assert_equal([], values(d))
  1264. call assert_false(has_key(d, 'k'))
  1265. call assert_equal('{}', string(d))
  1266. call assert_fails('let x = d[10]')
  1267. call assert_equal({}, {})
  1268. call assert_equal(0, len(copy(d)))
  1269. call assert_equal(0, count(d, 'k'))
  1270. call assert_equal({}, deepcopy(d))
  1271. call assert_equal(20, get(d, 'k', 20))
  1272. call assert_equal(0, min(d))
  1273. call assert_equal(0, max(d))
  1274. call assert_equal(0, remove(d, 'k'))
  1275. call assert_equal('{}', string(d))
  1276. " call assert_equal(0, extend(d, d, 0))
  1277. lockvar d
  1278. call assert_equal(1, islocked('d'))
  1279. unlockvar d
  1280. endfunc
  1281. " Test for the indexof() function
  1282. func Test_indexof()
  1283. let l = [#{color: 'red'}, #{color: 'blue'}, #{color: 'green'}]
  1284. call assert_equal(0, indexof(l, {i, v -> v.color == 'red'}))
  1285. call assert_equal(2, indexof(l, {i, v -> v.color == 'green'}))
  1286. call assert_equal(-1, indexof(l, {i, v -> v.color == 'grey'}))
  1287. call assert_equal(1, indexof(l, "v:val.color == 'blue'"))
  1288. call assert_equal(-1, indexof(l, "v:val.color == 'cyan'"))
  1289. let l = [#{n: 10}, #{n: 10}, #{n: 20}]
  1290. call assert_equal(0, indexof(l, "v:val.n == 10", #{startidx: 0}))
  1291. call assert_equal(1, indexof(l, "v:val.n == 10", #{startidx: -2}))
  1292. call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: 4}))
  1293. call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
  1294. call assert_equal(0, indexof(l, "v:val.n == 10", v:_null_dict))
  1295. let s = ["a", "b", "c"]
  1296. call assert_equal(2, indexof(s, {_, v -> v == 'c'}))
  1297. call assert_equal(-1, indexof(s, {_, v -> v == 'd'}))
  1298. call assert_equal(-1, indexof(s, {_, v -> "v == 'd'"}))
  1299. call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
  1300. call assert_equal(-1, indexof([1, 2, 3], {_, v -> "v == 2"}))
  1301. call assert_equal(-1, indexof(v:_null_list, {i, v -> v == 'a'}))
  1302. call assert_equal(-1, indexof(l, v:_null_string))
  1303. " Nvim doesn't have null functions
  1304. " call assert_equal(-1, indexof(l, test_null_function()))
  1305. call assert_equal(-1, indexof(l, ""))
  1306. call assert_fails('let i = indexof(l, " ")', 'E15:')
  1307. " failure cases
  1308. call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:')
  1309. call assert_fails('let i = indexof(l, "color == ''cyan''")', 'E121:')
  1310. call assert_fails('let i = indexof(l, {})', 'E1256:')
  1311. call assert_fails('let i = indexof({}, "v:val == 2")', 'E1226:')
  1312. call assert_fails('let i = indexof([], "v:val == 2", [])', 'E1206:')
  1313. func TestIdx(k, v)
  1314. return a:v.n == 20
  1315. endfunc
  1316. call assert_equal(2, indexof(l, function("TestIdx")))
  1317. delfunc TestIdx
  1318. func TestIdx(k, v)
  1319. return {}
  1320. endfunc
  1321. call assert_fails('let i = indexof(l, function("TestIdx"))', 'E728:')
  1322. delfunc TestIdx
  1323. func TestIdx(k, v)
  1324. throw "IdxError"
  1325. endfunc
  1326. call assert_fails('let i = indexof(l, function("TestIdx"))', 'E605:')
  1327. delfunc TestIdx
  1328. endfunc
  1329. func Test_extendnew_leak()
  1330. " This used to leak memory
  1331. for i in range(100) | silent! call extendnew([], [], []) | endfor
  1332. for i in range(100) | silent! call extendnew({}, {}, {}) | endfor
  1333. endfunc
  1334. " Test for comparing deeply nested List/Dict values
  1335. func Test_deep_nested_listdict_compare()
  1336. let lines =<< trim END
  1337. func GetNestedList(sz)
  1338. let l = []
  1339. let x = l
  1340. for i in range(a:sz)
  1341. let y = [1]
  1342. call add(x, y)
  1343. let x = y
  1344. endfor
  1345. return l
  1346. endfunc
  1347. VAR l1 = GetNestedList(1000)
  1348. VAR l2 = GetNestedList(999)
  1349. call assert_false(l1 == l2)
  1350. #" after 1000 nested items, the lists are considered to be equal
  1351. VAR l3 = GetNestedList(1001)
  1352. VAR l4 = GetNestedList(1002)
  1353. call assert_true(l3 == l4)
  1354. END
  1355. call CheckLegacyAndVim9Success(lines)
  1356. let lines =<< trim END
  1357. func GetNestedDict(sz)
  1358. let d = {}
  1359. let x = d
  1360. for i in range(a:sz)
  1361. let y = {}
  1362. let x['a'] = y
  1363. let x = y
  1364. endfor
  1365. return d
  1366. endfunc
  1367. VAR d1 = GetNestedDict(1000)
  1368. VAR d2 = GetNestedDict(999)
  1369. call assert_false(d1 == d2)
  1370. #" after 1000 nested items, the Dicts are considered to be equal
  1371. VAR d3 = GetNestedDict(1001)
  1372. VAR d4 = GetNestedDict(1002)
  1373. call assert_true(d3 == d4)
  1374. END
  1375. call CheckLegacyAndVim9Success(lines)
  1376. endfunc
  1377. " vim: shiftwidth=2 sts=2 expandtab