test_match.vim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. " Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(),
  2. " matchaddpos(), matcharg(), matchdelete(), and setmatches().
  3. source screendump.vim
  4. source check.vim
  5. function Test_match()
  6. highlight MyGroup1 term=bold ctermbg=red guibg=red
  7. highlight MyGroup2 term=italic ctermbg=green guibg=green
  8. highlight MyGroup3 term=underline ctermbg=blue guibg=blue
  9. " --- Check that "matcharg()" returns the correct group and pattern if a match
  10. " --- is defined.
  11. match MyGroup1 /TODO/
  12. 2match MyGroup2 /FIXME/
  13. 3match MyGroup3 /XXX/
  14. call assert_equal(['MyGroup1', 'TODO'], matcharg(1))
  15. call assert_equal(['MyGroup2', 'FIXME'], 2->matcharg())
  16. call assert_equal(['MyGroup3', 'XXX'], matcharg(3))
  17. " --- Check that "matcharg()" returns an empty list if the argument is not 1,
  18. " --- 2 or 3 (only 0 and 4 are tested).
  19. call assert_equal([], matcharg(0))
  20. call assert_equal([], matcharg(4))
  21. " --- Check that "matcharg()" returns ['', ''] if a match is not defined.
  22. match
  23. 2match
  24. 3match
  25. call assert_equal(['', ''], matcharg(1))
  26. call assert_equal(['', ''], matcharg(2))
  27. call assert_equal(['', ''], matcharg(3))
  28. " --- Check that "matchadd()" and "getmatches()" agree on added matches and
  29. " --- that default values apply.
  30. let m1 = matchadd("MyGroup1", "TODO")
  31. let m2 = matchadd("MyGroup2", "FIXME", 42)
  32. let m3 = matchadd("MyGroup3", "XXX", 60, 17)
  33. let ans = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1000},
  34. \ {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 42, 'id': 1001},
  35. \ {'group': 'MyGroup3', 'pattern': 'XXX', 'priority': 60, 'id': 17}]
  36. call assert_equal(ans, getmatches())
  37. " --- Check that "matchdelete()" deletes the matches defined in the previous
  38. " --- test correctly.
  39. call matchdelete(m1)
  40. eval m2->matchdelete()
  41. call matchdelete(m3)
  42. call assert_equal([], getmatches())
  43. " --- Check that "matchdelete()" returns 0 if successful and otherwise -1.
  44. let m = matchadd("MyGroup1", "TODO")
  45. call assert_equal(0, matchdelete(m))
  46. call assert_fails('call matchdelete(42)', 'E803:')
  47. " --- Check that "clearmatches()" clears all matches defined by ":match" and
  48. " --- "matchadd()".
  49. let m1 = matchadd("MyGroup1", "TODO")
  50. let m2 = "MyGroup2"->matchadd("FIXME", 42)
  51. let m3 = matchadd("MyGroup3", "XXX", 60, 17)
  52. match MyGroup1 /COFFEE/
  53. 2match MyGroup2 /HUMPPA/
  54. 3match MyGroup3 /VIM/
  55. call clearmatches()
  56. call assert_equal([], getmatches())
  57. " --- Check that "setmatches()" restores a list of matches saved by
  58. " --- "getmatches()" without changes. (Matches with equal priority must also
  59. " --- remain in the same order.)
  60. let m1 = matchadd("MyGroup1", "TODO")
  61. let m2 = matchadd("MyGroup2", "FIXME", 42)
  62. let m3 = matchadd("MyGroup3", "XXX", 60, 17)
  63. match MyGroup1 /COFFEE/
  64. 2match MyGroup2 /HUMPPA/
  65. 3match MyGroup3 /VIM/
  66. let ml = getmatches()
  67. call clearmatches()
  68. call setmatches(ml)
  69. call assert_equal(ml, getmatches())
  70. call clearmatches()
  71. " --- Check that "setmatches()" will not add two matches with the same ID. The
  72. " --- expected behaviour (for now) is to add the first match but not the
  73. " --- second and to return 0 (even though it is a matter of debate whether
  74. " --- this can be considered successful behaviour).
  75. let data = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1},
  76. \ {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 10, 'id': 1}]
  77. call assert_fails('call setmatches(data)', 'E801:')
  78. call assert_equal([data[0]], getmatches())
  79. call clearmatches()
  80. " --- Check that "setmatches()" returns 0 if successful and otherwise -1.
  81. " --- (A range of valid and invalid input values are tried out to generate the
  82. " --- return values.)
  83. call assert_equal(0, setmatches([]))
  84. call assert_equal(0, setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}]))
  85. call clearmatches()
  86. call assert_fails('call setmatches(0)', 'E714:')
  87. call assert_fails('call setmatches([0])', 'E474:')
  88. call assert_fails("call setmatches([{'wrong key': 'wrong value'}])", 'E474:')
  89. call setline(1, 'abcdefghijklmnopq')
  90. call matchaddpos("MyGroup1", [[1, 5], [1, 8, 3]], 10, 3)
  91. 1
  92. redraw!
  93. let v1 = screenattr(1, 1)
  94. let v5 = screenattr(1, 5)
  95. let v6 = screenattr(1, 6)
  96. let v8 = screenattr(1, 8)
  97. let v10 = screenattr(1, 10)
  98. let v11 = screenattr(1, 11)
  99. call assert_notequal(v1, v5)
  100. call assert_equal(v6, v1)
  101. call assert_equal(v8, v5)
  102. call assert_equal(v10, v5)
  103. call assert_equal(v11, v1)
  104. call assert_equal([{'group': 'MyGroup1', 'id': 3, 'priority': 10, 'pos1': [1, 5, 1], 'pos2': [1, 8, 3]}], getmatches())
  105. call clearmatches()
  106. call setline(1, 'abcdΣabcdef')
  107. eval "MyGroup1"->matchaddpos([[1, 4, 2], [1, 9, 2]], 10, 42)
  108. 1
  109. redraw!
  110. let v1 = screenattr(1, 1)
  111. let v4 = screenattr(1, 4)
  112. let v5 = screenattr(1, 5)
  113. let v6 = screenattr(1, 6)
  114. let v7 = screenattr(1, 7)
  115. let v8 = screenattr(1, 8)
  116. let v9 = screenattr(1, 9)
  117. let v10 = screenattr(1, 10)
  118. call assert_equal([{'group': 'MyGroup1', 'id': 42, 'priority': 10, 'pos1': [1, 4, 2], 'pos2': [1, 9, 2]}], getmatches())
  119. call assert_notequal(v1, v4)
  120. call assert_equal(v5, v4)
  121. call assert_equal(v6, v1)
  122. call assert_equal(v7, v1)
  123. call assert_equal(v8, v4)
  124. call assert_equal(v9, v4)
  125. call assert_equal(v10, v1)
  126. " Check, that setmatches() can correctly restore the matches from matchaddpos()
  127. call matchadd('MyGroup1', '\%2lmatchadd')
  128. let m=getmatches()
  129. call clearmatches()
  130. call setmatches(m)
  131. call assert_equal([{'group': 'MyGroup1', 'id': 42, 'priority': 10, 'pos1': [1, 4, 2], 'pos2': [1,9, 2]}, {'group': 'MyGroup1', 'pattern': '\%2lmatchadd', 'priority': 10, 'id': 1106}], getmatches())
  132. highlight MyGroup1 NONE
  133. highlight MyGroup2 NONE
  134. highlight MyGroup3 NONE
  135. endfunc
  136. func Test_match_error()
  137. call assert_fails('match Error', 'E475:')
  138. call assert_fails('match Error /', 'E475:')
  139. call assert_fails('4match Error /x/', 'E476:')
  140. call assert_fails('match Error /x/ x', 'E488:')
  141. endfunc
  142. func Test_matchadd_error()
  143. call clearmatches()
  144. " Nvim: not an error anymore:
  145. " call assert_fails("call matchadd('GroupDoesNotExist', 'X')", 'E28:')
  146. call matchadd('GroupDoesNotExist', 'X')
  147. call assert_equal([{'group': 'GroupDoesNotExist', 'pattern': 'X', 'priority': 10, 'id': 1206}], getmatches())
  148. call assert_fails("call matchadd('Search', '\\(')", 'E54:')
  149. call assert_fails("call matchadd('Search', 'XXX', 1, 123, 1)", 'E715:')
  150. call assert_fails("call matchadd('Error', 'XXX', 1, 3)", 'E798:')
  151. call assert_fails("call matchadd('Error', 'XXX', 1, 0)", 'E799:')
  152. call assert_fails("call matchadd('Error', 'XXX', [], 0)", 'E745:')
  153. endfunc
  154. func Test_matchaddpos()
  155. syntax on
  156. set hlsearch
  157. call setline(1, ['12345', 'NP'])
  158. call matchaddpos('Error', [[1,2], [1,6], [2,2]])
  159. redraw!
  160. call assert_notequal(screenattr(2,2), 0)
  161. call assert_equal(screenattr(2,2), screenattr(1,2))
  162. call assert_notequal(screenattr(2,2), screenattr(1,6))
  163. 1
  164. call matchadd('Search', 'N\|\n')
  165. redraw!
  166. call assert_notequal(screenattr(2,1), 0)
  167. call assert_equal(screenattr(2,1), screenattr(1,6))
  168. exec "norm! i0\<Esc>"
  169. redraw!
  170. call assert_equal(screenattr(2,2), screenattr(1,6))
  171. " Check overlapping pos
  172. call clearmatches()
  173. call setline(1, ['1234567890', 'NH'])
  174. call matchaddpos('Error', [[1,1,5], [1,3,5], [2,2]])
  175. redraw!
  176. call assert_notequal(screenattr(2,2), 0)
  177. call assert_equal(screenattr(2,2), screenattr(1,5))
  178. call assert_equal(screenattr(2,2), screenattr(1,7))
  179. call assert_notequal(screenattr(2,2), screenattr(1,8))
  180. call clearmatches()
  181. call matchaddpos('Error', [[1], [2,2]])
  182. redraw!
  183. call assert_equal(screenattr(2,2), screenattr(1,1))
  184. call assert_equal(screenattr(2,2), screenattr(1,10))
  185. call assert_notequal(screenattr(2,2), screenattr(1,11))
  186. " Check overlapping pos
  187. call clearmatches()
  188. call setline(1, ['1234567890', 'NH'])
  189. call matchaddpos('Error', [[1,1,5], [1,3,5], [2,2]])
  190. redraw!
  191. call assert_notequal(screenattr(2,2), 0)
  192. call assert_equal(screenattr(2,2), screenattr(1,5))
  193. call assert_equal(screenattr(2,2), screenattr(1,7))
  194. call assert_notequal(screenattr(2,2), screenattr(1,8))
  195. nohl
  196. call clearmatches()
  197. syntax off
  198. set hlsearch&
  199. endfunc
  200. " Add 12 match positions (previously the limit was 8 positions).
  201. func Test_matchaddpos_dump()
  202. CheckScreendump
  203. let lines =<< trim END
  204. call setline(1, ['1234567890123']->repeat(14))
  205. call matchaddpos('Search', range(1, 12)->map({i, v -> [v, v]}))
  206. END
  207. call writefile(lines, 'Xmatchaddpos', 'D')
  208. let buf = RunVimInTerminal('-S Xmatchaddpos', #{rows: 14})
  209. call VerifyScreenDump(buf, 'Test_matchaddpos_1', {})
  210. call StopVimInTerminal(buf)
  211. endfunc
  212. func Test_matchaddpos_otherwin()
  213. syntax on
  214. new
  215. call setline(1, ['12345', 'NP'])
  216. let winid = win_getid()
  217. wincmd w
  218. call matchadd('Search', '4', 10, -1, {'window': winid})
  219. call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid})
  220. redraw!
  221. call assert_notequal(screenattr(1,2), 0)
  222. call assert_notequal(screenattr(1,4), 0)
  223. call assert_notequal(screenattr(2,2), 0)
  224. call assert_equal(screenattr(1,2), screenattr(2,2))
  225. call assert_notequal(screenattr(1,2), screenattr(1,4))
  226. let savematches = getmatches(winid)
  227. let expect = [
  228. \ {'group': 'Search', 'pattern': '4', 'priority': 10, 'id': 1000},
  229. \ {'group': 'Error', 'id': 1001, 'priority': 10, 'pos1': [1, 2, 1], 'pos2': [2, 2, 1]},
  230. \]
  231. call assert_equal(expect, savematches)
  232. eval winid->clearmatches()
  233. call assert_equal([], getmatches(winid))
  234. call setmatches(savematches, winid)
  235. call assert_equal(expect, savematches)
  236. wincmd w
  237. bwipe!
  238. call clearmatches()
  239. syntax off
  240. endfunc
  241. func Test_matchaddpos_using_negative_priority()
  242. set hlsearch
  243. call clearmatches()
  244. call setline(1, 'x')
  245. let @/='x'
  246. redraw!
  247. let search_attr = screenattr(1,1)
  248. let @/=''
  249. call matchaddpos('Error', [1], 10)
  250. redraw!
  251. let error_attr = screenattr(1,1)
  252. call setline(2, '-1 match priority')
  253. call matchaddpos('Error', [2], -1)
  254. redraw!
  255. let negative_match_priority_attr = screenattr(2,1)
  256. call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.")
  257. call assert_equal(negative_match_priority_attr, error_attr)
  258. nohl
  259. set hlsearch&
  260. endfunc
  261. func Test_matchaddpos_error()
  262. call assert_fails("call matchaddpos('Error', 1)", 'E686:')
  263. call assert_fails("call matchaddpos('Error', [1], 1, 1)", 'E798:')
  264. call assert_fails("call matchaddpos('Error', [1], 1, 2)", 'E798:')
  265. call assert_fails("call matchaddpos('Error', [1], 1, 0)", 'E799:')
  266. call assert_fails("call matchaddpos('Error', [1], 1, 123, 1)", 'E715:')
  267. call assert_fails("call matchaddpos('Error', [1], 1, 5, {'window':12345})", 'E957:')
  268. " Why doesn't the following error have an error code E...?
  269. " call assert_fails("call matchaddpos('Error', [{}])", 'E290:')
  270. call assert_fails("call matchaddpos('Error', [{}])", 'E5031:')
  271. call assert_equal(-1, matchaddpos('Error', v:_null_list))
  272. call assert_equal(-1, matchaddpos('Error', []))
  273. call assert_fails("call matchaddpos('Error', [1], [], 1)", 'E745:')
  274. endfunc
  275. func OtherWindowCommon()
  276. let lines =<< trim END
  277. call setline(1, 'Hello Vim world')
  278. let mid = matchadd('Error', 'world', 1)
  279. let winid = win_getid()
  280. new
  281. END
  282. call writefile(lines, 'XscriptMatchCommon')
  283. let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 12})
  284. call TermWait(buf)
  285. return buf
  286. endfunc
  287. func Test_matchdelete_other_window()
  288. CheckScreendump
  289. let buf = OtherWindowCommon()
  290. call term_sendkeys(buf, ":call matchdelete(mid, winid)\<CR>")
  291. call VerifyScreenDump(buf, 'Test_matchdelete_1', {})
  292. call StopVimInTerminal(buf)
  293. call delete('XscriptMatchCommon')
  294. endfunc
  295. func Test_matchdelete_error()
  296. call assert_fails("call matchdelete(0)", 'E802:')
  297. call assert_fails("call matchdelete(1, -1)", 'E957:')
  298. endfunc
  299. func Test_matchclear_other_window()
  300. CheckRunVimInTerminal
  301. let buf = OtherWindowCommon()
  302. call term_sendkeys(buf, ":call clearmatches(winid)\<CR>")
  303. call VerifyScreenDump(buf, 'Test_matchclear_1', {})
  304. call StopVimInTerminal(buf)
  305. call delete('XscriptMatchCommon')
  306. endfunc
  307. func Test_matchadd_other_window()
  308. CheckRunVimInTerminal
  309. let buf = OtherWindowCommon()
  310. call term_sendkeys(buf, ":call matchadd('Search', 'Hello', 1, -1, #{window: winid})\<CR>")
  311. call term_sendkeys(buf, ":\<CR>")
  312. call VerifyScreenDump(buf, 'Test_matchadd_1', {})
  313. call StopVimInTerminal(buf)
  314. call delete('XscriptMatchCommon')
  315. endfunc
  316. func Test_match_in_linebreak()
  317. CheckRunVimInTerminal
  318. let lines =<< trim END
  319. set breakindent linebreak breakat+=]
  320. call printf('%s]%s', repeat('x', 50), repeat('x', 70))->setline(1)
  321. call matchaddpos('ErrorMsg', [[1, 51]])
  322. END
  323. call writefile(lines, 'XscriptMatchLinebreak')
  324. let buf = RunVimInTerminal('-S XscriptMatchLinebreak', #{rows: 10})
  325. call VerifyScreenDump(buf, 'Test_match_linebreak', {})
  326. call StopVimInTerminal(buf)
  327. call delete('XscriptMatchLinebreak')
  328. endfunc
  329. func Test_match_with_incsearch()
  330. CheckRunVimInTerminal
  331. let lines =<< trim END
  332. set incsearch
  333. call setline(1, range(20))
  334. call matchaddpos('ErrorMsg', [3])
  335. END
  336. call writefile(lines, 'XmatchWithIncsearch')
  337. let buf = RunVimInTerminal('-S XmatchWithIncsearch', #{rows: 6})
  338. call VerifyScreenDump(buf, 'Test_match_with_incsearch_1', {})
  339. call term_sendkeys(buf, ":s/0")
  340. call VerifyScreenDump(buf, 'Test_match_with_incsearch_2', {})
  341. call term_sendkeys(buf, "\<CR>")
  342. call StopVimInTerminal(buf)
  343. call delete('XmatchWithIncsearch')
  344. endfunc
  345. " Test for deleting matches outside of the screen redraw top/bottom lines
  346. " This should cause a redraw of those lines.
  347. func Test_matchdelete_redraw()
  348. new
  349. call setline(1, range(1, 500))
  350. call cursor(250, 1)
  351. let m1 = matchaddpos('Search', [[250]])
  352. let m2 = matchaddpos('Search', [[10], [450]])
  353. redraw!
  354. let m3 = matchaddpos('Search', [[240], [260]])
  355. call matchdelete(m2)
  356. let m = getmatches()
  357. call assert_equal(2, len(m))
  358. call assert_equal([250], m[0].pos1)
  359. redraw!
  360. call matchdelete(m1)
  361. call assert_equal(1, len(getmatches()))
  362. bw!
  363. endfunc
  364. func Test_match_tab_with_linebreak()
  365. CheckRunVimInTerminal
  366. let lines =<< trim END
  367. set linebreak
  368. call setline(1, "\tix")
  369. call matchadd('ErrorMsg', '\t')
  370. END
  371. call writefile(lines, 'XscriptMatchTabLinebreak', 'D')
  372. let buf = RunVimInTerminal('-S XscriptMatchTabLinebreak', #{rows: 10})
  373. call VerifyScreenDump(buf, 'Test_match_tab_linebreak', {})
  374. call StopVimInTerminal(buf)
  375. endfunc
  376. " vim: shiftwidth=2 sts=2 expandtab