test_assert.vim 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. " Test that the methods used for testing work.
  2. source check.vim
  3. source term_util.vim
  4. func Test_assert_false()
  5. call assert_equal(0, assert_false(0))
  6. call assert_equal(0, assert_false(v:false))
  7. call assert_equal(0, v:false->assert_false())
  8. call assert_equal(1, assert_false(123))
  9. call assert_match("Expected False but got 123", v:errors[0])
  10. call remove(v:errors, 0)
  11. call assert_equal(1, 123->assert_false())
  12. call assert_match("Expected False but got 123", v:errors[0])
  13. call remove(v:errors, 0)
  14. endfunc
  15. func Test_assert_true()
  16. call assert_equal(0, assert_true(1))
  17. call assert_equal(0, assert_true(123))
  18. call assert_equal(0, assert_true(v:true))
  19. call assert_equal(0, v:true->assert_true())
  20. call assert_equal(1, assert_true(0))
  21. call assert_match("Expected True but got 0", v:errors[0])
  22. call remove(v:errors, 0)
  23. call assert_equal(1, 0->assert_true())
  24. call assert_match("Expected True but got 0", v:errors[0])
  25. call remove(v:errors, 0)
  26. endfunc
  27. func Test_assert_equal()
  28. let s = 'foo'
  29. call assert_equal(0, assert_equal('foo', s))
  30. let n = 4
  31. call assert_equal(0, assert_equal(4, n))
  32. let l = [1, 2, 3]
  33. call assert_equal(0, assert_equal([1, 2, 3], l))
  34. call assert_equal(v:_null_list, v:_null_list)
  35. call assert_equal(v:_null_list, [])
  36. call assert_equal([], v:_null_list)
  37. let s = 'foo'
  38. call assert_equal(1, assert_equal('bar', s))
  39. call assert_match("Expected 'bar' but got 'foo'", v:errors[0])
  40. call remove(v:errors, 0)
  41. let s = 'αβγ'
  42. call assert_equal(1, assert_equal('δεζ', s))
  43. call assert_match("Expected 'δεζ' but got 'αβγ'", v:errors[0])
  44. call remove(v:errors, 0)
  45. call assert_equal('XxxxxxxxxxxxxxxxxxxxxxX', 'XyyyyyyyyyyyyyyyyyyyyyyyyyX')
  46. call assert_match("Expected 'X\\\\\\[x occurs 21 times]X' but got 'X\\\\\\[y occurs 25 times]X'", v:errors[0])
  47. call remove(v:errors, 0)
  48. call assert_equal('ΩωωωωωωωωωωωωωωωωωωωωωΩ', 'ΩψψψψψψψψψψψψψψψψψψψψψψψψψΩ')
  49. call assert_match("Expected 'Ω\\\\\\[ω occurs 21 times]Ω' but got 'Ω\\\\\\[ψ occurs 25 times]Ω'", v:errors[0])
  50. call remove(v:errors, 0)
  51. " special characters are escaped
  52. call assert_equal("\b\e\f\n\t\r\\\x01\x7f", 'x')
  53. call assert_match('Expected ''\\b\\e\\f\\n\\t\\r\\\\\\x01\\x7f'' but got ''x''', v:errors[0])
  54. call remove(v:errors, 0)
  55. " many composing characters are handled properly
  56. call setline(1, ' ')
  57. norm 100gr݀
  58. call assert_equal(1, getline(1))
  59. call assert_match("Expected 1 but got '.* occurs 100 times]'", v:errors[0])
  60. call remove(v:errors, 0)
  61. bwipe!
  62. endfunc
  63. func Test_assert_equal_dict()
  64. call assert_equal(0, assert_equal(#{one: 1, two: 2}, #{two: 2, one: 1}))
  65. call assert_equal(1, assert_equal(#{one: 1, two: 2}, #{two: 2, one: 3}))
  66. call assert_match("Expected {'one': 1} but got {'one': 3} - 1 equal item omitted", v:errors[0])
  67. call remove(v:errors, 0)
  68. call assert_equal(1, assert_equal(#{one: 1, two: 2}, #{two: 22, one: 11}))
  69. call assert_match("Expected {'one': 1, 'two': 2} but got {'one': 11, 'two': 22}", v:errors[0])
  70. call remove(v:errors, 0)
  71. call assert_equal(1, assert_equal(#{}, #{two: 2, one: 1}))
  72. call assert_match("Expected {} but got {'one': 1, 'two': 2}", v:errors[0])
  73. call remove(v:errors, 0)
  74. call assert_equal(1, assert_equal(#{two: 2, one: 1}, #{}))
  75. call assert_match("Expected {'one': 1, 'two': 2} but got {}", v:errors[0])
  76. call remove(v:errors, 0)
  77. endfunc
  78. func Test_assert_equalfile()
  79. call assert_equal(1, assert_equalfile('abcabc', 'xyzxyz'))
  80. call assert_match("E485: Can't read file abcabc", v:errors[0])
  81. call remove(v:errors, 0)
  82. let goodtext = ["one", "two", "three"]
  83. call writefile(goodtext, 'Xone', 'D')
  84. call assert_equal(1, 'Xone'->assert_equalfile('xyzxyz'))
  85. call assert_match("E485: Can't read file xyzxyz", v:errors[0])
  86. call remove(v:errors, 0)
  87. call writefile(goodtext, 'Xtwo', 'D')
  88. call assert_equal(0, assert_equalfile('Xone', 'Xtwo'))
  89. call writefile([goodtext[0]], 'Xone')
  90. call assert_equal(1, assert_equalfile('Xone', 'Xtwo'))
  91. call assert_match("first file is shorter", v:errors[0])
  92. call remove(v:errors, 0)
  93. call writefile(goodtext, 'Xone')
  94. call writefile([goodtext[0]], 'Xtwo')
  95. call assert_equal(1, assert_equalfile('Xone', 'Xtwo'))
  96. call assert_match("second file is shorter", v:errors[0])
  97. call remove(v:errors, 0)
  98. call writefile(['1234X89'], 'Xone')
  99. call writefile(['1234Y89'], 'Xtwo')
  100. call assert_equal(1, assert_equalfile('Xone', 'Xtwo'))
  101. call assert_match('difference at byte 4, line 1 after "1234X" vs "1234Y"', v:errors[0])
  102. call remove(v:errors, 0)
  103. call writefile([repeat('x', 234) .. 'X'], 'Xone')
  104. call writefile([repeat('x', 234) .. 'Y'], 'Xtwo')
  105. call assert_equal(1, assert_equalfile('Xone', 'Xtwo'))
  106. let xes = repeat('x', 134)
  107. call assert_match('difference at byte 234, line 1 after "' .. xes .. 'X" vs "' .. xes .. 'Y"', v:errors[0])
  108. call remove(v:errors, 0)
  109. call assert_equal(1, assert_equalfile('Xone', 'Xtwo', 'a message'))
  110. call assert_match("a message: difference at byte 234, line 1 after", v:errors[0])
  111. call remove(v:errors, 0)
  112. endfunc
  113. func Test_assert_notequal()
  114. let n = 4
  115. call assert_equal(0, assert_notequal('foo', n))
  116. let s = 'foo'
  117. call assert_equal(0, assert_notequal([1, 2, 3], s))
  118. call assert_equal(1, assert_notequal('foo', s))
  119. call assert_match("Expected not equal to 'foo'", v:errors[0])
  120. call remove(v:errors, 0)
  121. endfunc
  122. func Test_assert_report()
  123. call assert_equal(1, assert_report('something is wrong'))
  124. call assert_match('something is wrong', v:errors[0])
  125. call remove(v:errors, 0)
  126. call assert_equal(1, 'also wrong'->assert_report())
  127. call assert_match('also wrong', v:errors[0])
  128. call remove(v:errors, 0)
  129. endfunc
  130. func Test_assert_exception()
  131. try
  132. nocommand
  133. catch
  134. call assert_equal(0, assert_exception('E492:'))
  135. endtry
  136. try
  137. nocommand
  138. catch
  139. call assert_equal(1, assert_exception('E12345:'))
  140. endtry
  141. call assert_match("Expected 'E12345:' but got 'Vim:E492: ", v:errors[0])
  142. call remove(v:errors, 0)
  143. try
  144. nocommand
  145. catch
  146. try
  147. " illegal argument, get NULL for error
  148. call assert_equal(1, assert_exception([]))
  149. catch
  150. call assert_equal(0, assert_exception('E730:'))
  151. endtry
  152. endtry
  153. call assert_equal(1, assert_exception('E492:'))
  154. call assert_match('v:exception is not set', v:errors[0])
  155. call remove(v:errors, 0)
  156. endfunc
  157. func Test_wrong_error_type()
  158. let save_verrors = v:errors
  159. let v:['errors'] = {'foo': 3}
  160. call assert_equal('yes', 'no')
  161. let verrors = v:errors
  162. let v:errors = save_verrors
  163. call assert_equal(type([]), type(verrors))
  164. endfunc
  165. func Test_compare_fail()
  166. let s:v = {}
  167. let s:x = {"a": s:v}
  168. let s:v["b"] = s:x
  169. let s:w = {"c": s:x, "d": ''}
  170. try
  171. call assert_equal(s:w, '')
  172. catch
  173. call assert_equal(0, assert_exception('E724:'))
  174. " Nvim: expected value isn't shown as NULL
  175. " call assert_match("Expected NULL but got ''", v:errors[0])
  176. call assert_match("Expected .* but got ''", v:errors[0])
  177. call remove(v:errors, 0)
  178. endtry
  179. endfunc
  180. func Test_match()
  181. call assert_equal(0, assert_match('^f.*b.*r$', 'foobar'))
  182. call assert_equal(1, assert_match('bar.*foo', 'foobar'))
  183. call assert_match("Pattern 'bar.*foo' does not match 'foobar'", v:errors[0])
  184. call remove(v:errors, 0)
  185. call assert_equal(1, assert_match('bar.*foo', 'foobar', 'wrong'))
  186. call assert_match('wrong', v:errors[0])
  187. call remove(v:errors, 0)
  188. call assert_equal(1, 'foobar'->assert_match('bar.*foo', 'wrong'))
  189. call assert_match('wrong', v:errors[0])
  190. call remove(v:errors, 0)
  191. endfunc
  192. func Test_notmatch()
  193. call assert_equal(0, assert_notmatch('foo', 'bar'))
  194. call assert_equal(0, assert_notmatch('^foobar$', 'foobars'))
  195. call assert_equal(1, assert_notmatch('foo', 'foobar'))
  196. call assert_match("Pattern 'foo' does match 'foobar'", v:errors[0])
  197. call remove(v:errors, 0)
  198. call assert_equal(1, 'foobar'->assert_notmatch('foo'))
  199. call assert_match("Pattern 'foo' does match 'foobar'", v:errors[0])
  200. call remove(v:errors, 0)
  201. endfunc
  202. func Test_assert_fail_fails()
  203. call assert_equal(1, assert_fails('xxx', 'E12345'))
  204. call assert_match("Expected 'E12345' but got 'E492:", v:errors[0])
  205. call remove(v:errors, 0)
  206. call assert_equal(1, assert_fails('xxx', 'E9876', 'stupid'))
  207. call assert_match("stupid: Expected 'E9876' but got 'E492:", v:errors[0])
  208. call remove(v:errors, 0)
  209. call assert_equal(1, assert_fails('xxx', ['E9876']))
  210. call assert_match("Expected 'E9876' but got 'E492:", v:errors[0])
  211. call remove(v:errors, 0)
  212. call assert_equal(1, assert_fails('xxx', ['E492:', 'E9876']))
  213. call assert_match("Expected 'E9876' but got 'E492:", v:errors[0])
  214. call remove(v:errors, 0)
  215. call assert_equal(1, assert_fails('echo', '', 'echo command'))
  216. call assert_match("command did not fail: echo command", v:errors[0])
  217. call remove(v:errors, 0)
  218. call assert_equal(1, 'echo'->assert_fails('', 'echo command'))
  219. call assert_match("command did not fail: echo command", v:errors[0])
  220. call remove(v:errors, 0)
  221. try
  222. call assert_equal(1, assert_fails('xxx', []))
  223. catch
  224. let exp = v:exception
  225. endtry
  226. call assert_match("E856: \"assert_fails()\" second argument", exp)
  227. try
  228. call assert_equal(1, assert_fails('xxx', ['1', '2', '3']))
  229. catch
  230. let exp = v:exception
  231. endtry
  232. call assert_match("E856: \"assert_fails()\" second argument", exp)
  233. try
  234. call assert_equal(1, assert_fails('xxx', v:_null_list))
  235. catch
  236. let exp = v:exception
  237. endtry
  238. call assert_match("E856: \"assert_fails()\" second argument", exp)
  239. try
  240. call assert_equal(1, assert_fails('xxx', []))
  241. catch
  242. let exp = v:exception
  243. endtry
  244. call assert_match("E856: \"assert_fails()\" second argument", exp)
  245. try
  246. call assert_equal(1, assert_fails('xxx', #{one: 1}))
  247. catch
  248. let exp = v:exception
  249. endtry
  250. call assert_match("E1222: String or List required for argument 2", exp)
  251. try
  252. call assert_equal(0, assert_fails('xxx', [#{one: 1}]))
  253. catch
  254. let exp = v:exception
  255. endtry
  256. call assert_match("E731: Using a Dictionary as a String", exp)
  257. let exp = ''
  258. try
  259. call assert_equal(0, assert_fails('xxx', ['E492', #{one: 1}]))
  260. catch
  261. let exp = v:exception
  262. endtry
  263. call assert_match("E731: Using a Dictionary as a String", exp)
  264. try
  265. call assert_equal(1, assert_fails('xxx', 'E492', '', 'burp'))
  266. catch
  267. let exp = v:exception
  268. endtry
  269. call assert_match("E1210: Number required for argument 4", exp)
  270. try
  271. call assert_equal(1, assert_fails('xxx', 'E492', '', 54, 123))
  272. catch
  273. let exp = v:exception
  274. endtry
  275. call assert_match("E1174: String required for argument 5", exp)
  276. call assert_equal(1, assert_fails('c0', ['', '\(.\)\1']))
  277. call assert_match("Expected '\\\\\\\\(.\\\\\\\\)\\\\\\\\1' but got 'E939: Positive count required: c0': c0", v:errors[0])
  278. call remove(v:errors, 0)
  279. " Test for matching the line number and the script name in an error message
  280. call writefile(['', 'call Xnonexisting()'], 'Xassertfails.vim', 'D')
  281. call assert_fails('source Xassertfails.vim', 'E117:', '', 10)
  282. call assert_match("Expected 10 but got 2", v:errors[0])
  283. call remove(v:errors, 0)
  284. call assert_fails('source Xassertfails.vim', 'E117:', '', 2, 'Xabc')
  285. call assert_match("Expected 'Xabc' but got .*Xassertfails.vim", v:errors[0])
  286. call remove(v:errors, 0)
  287. endfunc
  288. func Test_assert_wrong_arg_emsg_off()
  289. CheckFeature folding
  290. new
  291. call setline(1, ['foo', 'bar'])
  292. 1,2fold
  293. " This used to crash Vim
  294. let &l:foldtext = 'assert_match({}, {})'
  295. redraw!
  296. let &l:foldtext = 'assert_equalfile({}, {})'
  297. redraw!
  298. bwipe!
  299. endfunc
  300. func Test_assert_fails_in_try_block()
  301. try
  302. call assert_equal(0, assert_fails('throw "error"'))
  303. endtry
  304. endfunc
  305. " Test that assert_fails() in a timer does not cause a hit-enter prompt.
  306. " Requires using a terminal, in regular tests the hit-enter prompt won't be
  307. " triggered.
  308. func Test_assert_fails_in_timer()
  309. CheckRunVimInTerminal
  310. let buf = RunVimInTerminal('', {'rows': 6})
  311. let cmd = ":call timer_start(0, {-> assert_fails('call', 'E471:')})"
  312. call term_sendkeys(buf, cmd)
  313. call WaitForAssert({-> assert_equal(cmd, term_getline(buf, 6))})
  314. call term_sendkeys(buf, "\<CR>")
  315. call TermWait(buf, 100)
  316. call assert_match('E471: Argument required', term_getline(buf, 6))
  317. call StopVimInTerminal(buf)
  318. endfunc
  319. func Test_assert_beeps()
  320. new
  321. call assert_equal(0, assert_beeps('normal h'))
  322. call assert_equal(1, assert_beeps('normal 0'))
  323. call assert_match("command did not beep: normal 0", v:errors[0])
  324. call remove(v:errors, 0)
  325. call assert_equal(0, 'normal h'->assert_beeps())
  326. call assert_equal(1, 'normal 0'->assert_beeps())
  327. call assert_match("command did not beep: normal 0", v:errors[0])
  328. call remove(v:errors, 0)
  329. bwipe
  330. endfunc
  331. func Test_assert_nobeep()
  332. call assert_equal(1, assert_nobeep('normal! cr'))
  333. call assert_match("command did beep: normal! cr", v:errors[0])
  334. call remove(v:errors, 0)
  335. endfunc
  336. func Test_assert_inrange()
  337. call assert_equal(0, assert_inrange(7, 7, 7))
  338. call assert_equal(0, assert_inrange(5, 7, 5))
  339. call assert_equal(0, assert_inrange(5, 7, 6))
  340. call assert_equal(0, assert_inrange(5, 7, 7))
  341. call assert_equal(1, assert_inrange(5, 7, 4))
  342. call assert_match("Expected range 5 - 7, but got 4", v:errors[0])
  343. call remove(v:errors, 0)
  344. call assert_equal(1, assert_inrange(5, 7, 8))
  345. call assert_match("Expected range 5 - 7, but got 8", v:errors[0])
  346. call remove(v:errors, 0)
  347. call assert_equal(0, 5->assert_inrange(5, 7))
  348. call assert_equal(0, 7->assert_inrange(5, 7))
  349. call assert_equal(1, 8->assert_inrange(5, 7))
  350. call assert_match("Expected range 5 - 7, but got 8", v:errors[0])
  351. call remove(v:errors, 0)
  352. call assert_fails('call assert_inrange(1, 1)', 'E119:')
  353. call assert_equal(0, assert_inrange(7.0, 7, 7))
  354. call assert_equal(0, assert_inrange(7, 7.0, 7))
  355. call assert_equal(0, assert_inrange(7, 7, 7.0))
  356. call assert_equal(0, assert_inrange(5, 7, 5.0))
  357. call assert_equal(0, assert_inrange(5, 7, 6.0))
  358. call assert_equal(0, assert_inrange(5, 7, 7.0))
  359. call assert_equal(1, assert_inrange(5, 7, 4.0))
  360. call assert_match("Expected range 5.0 - 7.0, but got 4.0", v:errors[0])
  361. call remove(v:errors, 0)
  362. call assert_equal(1, assert_inrange(5, 7, 8.0))
  363. call assert_match("Expected range 5.0 - 7.0, but got 8.0", v:errors[0])
  364. call remove(v:errors, 0)
  365. " Use a custom message
  366. call assert_equal(1, assert_inrange(5, 7, 8, "Higher"))
  367. call assert_match("Higher: Expected range 5 - 7, but got 8", v:errors[0])
  368. call remove(v:errors, 0)
  369. call assert_equal(1, assert_inrange(5, 7, 8.0, "Higher"))
  370. call assert_match("Higher: Expected range 5.0 - 7.0, but got 8.0", v:errors[0])
  371. call remove(v:errors, 0)
  372. " Invalid arguments
  373. call assert_fails("call assert_inrange([], 2, 3)", 'E1219:')
  374. call assert_fails("call assert_inrange(1, [], 3)", 'E1219:')
  375. call assert_fails("call assert_inrange(1, 2, [])", 'E1219:')
  376. endfunc
  377. func Test_assert_with_msg()
  378. call assert_equal('foo', 'bar', 'testing')
  379. call assert_match("testing: Expected 'foo' but got 'bar'", v:errors[0])
  380. call remove(v:errors, 0)
  381. endfunc
  382. func Test_override()
  383. throw 'Skipped: Nvim does not support test_override()'
  384. call test_override('char_avail', 1)
  385. eval 1->test_override('redraw')
  386. call test_override('ALL', 0)
  387. call assert_fails("call test_override('xxx', 1)", 'E475:')
  388. call assert_fails("call test_override('redraw', 'yes')", 'E474:')
  389. call assert_fails("call test_override('redraw', 'yes')", 'E1210:')
  390. endfunc
  391. func Test_mouse_position()
  392. let save_mouse = &mouse
  393. set mouse=a
  394. new
  395. call setline(1, ['line one', 'line two'])
  396. call assert_equal([0, 1, 1, 0], getpos('.'))
  397. call Ntest_setmouse(1, 5)
  398. call feedkeys("\<LeftMouse>", "xt")
  399. call assert_equal([0, 1, 5, 0], getpos('.'))
  400. call Ntest_setmouse(2, 20)
  401. call feedkeys("\<LeftMouse>", "xt")
  402. call assert_equal([0, 2, 8, 0], getpos('.'))
  403. call Ntest_setmouse(5, 1)
  404. call feedkeys("\<LeftMouse>", "xt")
  405. call assert_equal([0, 2, 1, 0], getpos('.'))
  406. bwipe!
  407. let &mouse = save_mouse
  408. endfunc
  409. " Test for the test_alloc_fail() function
  410. func Test_test_alloc_fail()
  411. throw 'Skipped: Nvim does not support test_alloc_fail()'
  412. call assert_fails('call test_alloc_fail([], 1, 1)', 'E474:')
  413. call assert_fails('call test_alloc_fail(10, [], 1)', 'E474:')
  414. call assert_fails('call test_alloc_fail(10, 1, [])', 'E474:')
  415. call assert_fails('call test_alloc_fail(999999, 1, 1)', 'E474:')
  416. endfunc
  417. " Test for the test_option_not_set() function
  418. func Test_test_option_not_set()
  419. throw 'Skipped: Nvim does not support test_option_not_set()'
  420. call assert_fails('call test_option_not_set("Xinvalidopt")', 'E475:')
  421. endfunc
  422. " Must be last.
  423. func Test_zz_quit_detected()
  424. " Verify that if a test function ends Vim the test script detects this.
  425. quit
  426. endfunc