match_functions_spec.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local eq = t.eq
  5. local clear = n.clear
  6. local fn = n.fn
  7. local command = n.command
  8. local exc_exec = n.exc_exec
  9. before_each(clear)
  10. describe('setmatches()', function()
  11. it('correctly handles case when both group and pattern entries are numbers', function()
  12. command('hi def link 1 PreProc')
  13. eq(0, fn.setmatches({ { group = 1, pattern = 2, id = 3, priority = 4 } }))
  14. eq({
  15. {
  16. group = '1',
  17. pattern = '2',
  18. id = 3,
  19. priority = 4,
  20. },
  21. }, fn.getmatches())
  22. eq(0, fn.setmatches({ { group = 1, pattern = 2, id = 3, priority = 4, conceal = 5 } }))
  23. eq({
  24. {
  25. group = '1',
  26. pattern = '2',
  27. id = 3,
  28. priority = 4,
  29. conceal = '5',
  30. },
  31. }, fn.getmatches())
  32. eq(
  33. 0,
  34. fn.setmatches({
  35. { group = 1, pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = 5 },
  36. })
  37. )
  38. eq({
  39. {
  40. group = '1',
  41. pos1 = { 2 },
  42. pos2 = { 6 },
  43. id = 3,
  44. priority = 4,
  45. conceal = '5',
  46. },
  47. }, fn.getmatches())
  48. end)
  49. it('does not fail if highlight group is not defined', function()
  50. eq(0, fn.setmatches { { group = 1, pattern = 2, id = 3, priority = 4 } })
  51. eq({ { group = '1', pattern = '2', id = 3, priority = 4 } }, fn.getmatches())
  52. eq(
  53. 0,
  54. fn.setmatches {
  55. { group = 1, pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = 5 },
  56. }
  57. )
  58. eq(
  59. { { group = '1', pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = '5' } },
  60. fn.getmatches()
  61. )
  62. end)
  63. end)
  64. describe('matchadd()', function()
  65. it('correctly works when first two arguments and conceal are numbers at once', function()
  66. command('hi def link 1 PreProc')
  67. eq(4, fn.matchadd(1, 2, 3, 4, { conceal = 5 }))
  68. eq({
  69. {
  70. group = '1',
  71. pattern = '2',
  72. priority = 3,
  73. id = 4,
  74. conceal = '5',
  75. },
  76. }, fn.getmatches())
  77. end)
  78. end)
  79. describe('matchaddpos()', function()
  80. it('errors out on invalid input', function()
  81. command('hi clear PreProc')
  82. eq(
  83. 'Vim(let):E5030: Empty list at position 0',
  84. exc_exec('let val = matchaddpos("PreProc", [[]])')
  85. )
  86. eq(
  87. 'Vim(let):E5030: Empty list at position 1',
  88. exc_exec('let val = matchaddpos("PreProc", [1, v:_null_list])')
  89. )
  90. eq(
  91. 'Vim(let):E5031: List or number required at position 1',
  92. exc_exec('let val = matchaddpos("PreProc", [1, v:_null_dict])')
  93. )
  94. end)
  95. it('works with 0 lnum', function()
  96. command('hi clear PreProc')
  97. eq(4, fn.matchaddpos('PreProc', { 1 }, 3, 4))
  98. eq({
  99. {
  100. group = 'PreProc',
  101. pos1 = { 1 },
  102. priority = 3,
  103. id = 4,
  104. },
  105. }, fn.getmatches())
  106. fn.matchdelete(4)
  107. eq(4, fn.matchaddpos('PreProc', { { 0 }, 1 }, 3, 4))
  108. eq({
  109. {
  110. group = 'PreProc',
  111. pos1 = { 1 },
  112. priority = 3,
  113. id = 4,
  114. },
  115. }, fn.getmatches())
  116. fn.matchdelete(4)
  117. eq(4, fn.matchaddpos('PreProc', { 0, 1 }, 3, 4))
  118. eq({
  119. {
  120. group = 'PreProc',
  121. pos1 = { 1 },
  122. priority = 3,
  123. id = 4,
  124. },
  125. }, fn.getmatches())
  126. end)
  127. it('works with negative numbers', function()
  128. command('hi clear PreProc')
  129. eq(4, fn.matchaddpos('PreProc', { -10, 1 }, 3, 4))
  130. eq({
  131. {
  132. group = 'PreProc',
  133. pos1 = { 1 },
  134. priority = 3,
  135. id = 4,
  136. },
  137. }, fn.getmatches())
  138. fn.matchdelete(4)
  139. eq(4, fn.matchaddpos('PreProc', { { -10 }, 1 }, 3, 4))
  140. eq({
  141. {
  142. group = 'PreProc',
  143. pos1 = { 1 },
  144. priority = 3,
  145. id = 4,
  146. },
  147. }, fn.getmatches())
  148. fn.matchdelete(4)
  149. eq(4, fn.matchaddpos('PreProc', { { 2, -1 }, 1 }, 3, 4))
  150. eq({
  151. {
  152. group = 'PreProc',
  153. pos1 = { 1 },
  154. priority = 3,
  155. id = 4,
  156. },
  157. }, fn.getmatches())
  158. fn.matchdelete(4)
  159. eq(4, fn.matchaddpos('PreProc', { { 2, 0, -1 }, 1 }, 3, 4))
  160. eq({
  161. {
  162. group = 'PreProc',
  163. pos1 = { 1 },
  164. priority = 3,
  165. id = 4,
  166. },
  167. }, fn.getmatches())
  168. end)
  169. it('works with zero length', function()
  170. local screen = Screen.new(40, 5)
  171. fn.setline(1, 'abcdef')
  172. command('hi PreProc guifg=Red')
  173. eq(4, fn.matchaddpos('PreProc', { { 1, 2, 0 } }, 3, 4))
  174. eq({
  175. {
  176. group = 'PreProc',
  177. pos1 = { 1, 2, 0 },
  178. priority = 3,
  179. id = 4,
  180. },
  181. }, fn.getmatches())
  182. screen:expect(
  183. [[
  184. ^a{1:b}cdef |
  185. {2:~ }|*3
  186. |
  187. ]],
  188. {
  189. [1] = { foreground = Screen.colors.Red },
  190. [2] = { bold = true, foreground = Screen.colors.Blue1 },
  191. }
  192. )
  193. end)
  194. end)