063_match_and_matchadd_spec.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. -- Tests for adjusting window and contents
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local Screen = require('test.functional.ui.screen')
  4. local eval, clear, command = helpers.eval, helpers.clear, helpers.command
  5. local eq, neq = helpers.eq, helpers.neq
  6. local insert = helpers.insert
  7. local redir_exec = helpers.redir_exec
  8. describe('063: Test for ":match", "matchadd()" and related functions', function()
  9. setup(clear)
  10. it('is working', function()
  11. local screen = Screen.new(40, 5)
  12. screen:attach()
  13. screen:set_default_attr_ids({
  14. [0] = {bold = true, foreground = Screen.colors.Blue},
  15. [1] = {background = Screen.colors.Red},
  16. })
  17. -- Check that "matcharg()" returns the correct group and pattern if a match
  18. -- is defined.
  19. command("highlight MyGroup1 term=bold ctermbg=red guibg=red")
  20. command("highlight MyGroup2 term=italic ctermbg=green guibg=green")
  21. command("highlight MyGroup3 term=underline ctermbg=blue guibg=blue")
  22. command("match MyGroup1 /TODO/")
  23. command("2match MyGroup2 /FIXME/")
  24. command("3match MyGroup3 /XXX/")
  25. eq({'MyGroup1', 'TODO'}, eval('matcharg(1)'))
  26. eq({'MyGroup2', 'FIXME'}, eval('matcharg(2)'))
  27. eq({'MyGroup3', 'XXX'}, eval('matcharg(3)'))
  28. -- Check that "matcharg()" returns an empty list if the argument is not 1,
  29. -- 2 or 3 (only 0 and 4 are tested).
  30. eq({}, eval('matcharg(0)'))
  31. eq({}, eval('matcharg(4)'))
  32. -- Check that "matcharg()" returns ['', ''] if a match is not defined.
  33. command("match")
  34. command("2match")
  35. command("3match")
  36. eq({'', ''}, eval('matcharg(1)'))
  37. eq({'', ''}, eval('matcharg(2)'))
  38. eq({'', ''}, eval('matcharg(3)'))
  39. -- Check that "matchadd()" and "getmatches()" agree on added matches and
  40. -- that default values apply.
  41. command("let m1 = matchadd('MyGroup1', 'TODO')")
  42. command("let m2 = matchadd('MyGroup2', 'FIXME', 42)")
  43. command("let m3 = matchadd('MyGroup3', 'XXX', 60, 17)")
  44. eq({{group = 'MyGroup1', pattern = 'TODO', priority = 10, id = 4},
  45. {group = 'MyGroup2', pattern = 'FIXME', priority = 42, id = 5},
  46. {group = 'MyGroup3', pattern = 'XXX', priority = 60, id = 17}},
  47. eval('getmatches()'))
  48. -- Check that "matchdelete()" deletes the matches defined in the previous
  49. -- test correctly.
  50. command("call matchdelete(m1)")
  51. command("call matchdelete(m2)")
  52. command("call matchdelete(m3)")
  53. eq({}, eval('getmatches()'))
  54. --- Check that "matchdelete()" returns 0 if successful and otherwise -1.
  55. command("let m = matchadd('MyGroup1', 'TODO')")
  56. eq(0, eval('matchdelete(m)'))
  57. -- matchdelete throws error and returns -1 on failure
  58. neq(true, pcall(function() eval('matchdelete(42)') end))
  59. eq('\nE803: ID not found: 42',
  60. redir_exec("let r2 = matchdelete(42)"))
  61. eq(-1, eval('r2'))
  62. -- Check that "clearmatches()" clears all matches defined by ":match" and
  63. -- "matchadd()".
  64. command("let m1 = matchadd('MyGroup1', 'TODO')")
  65. command("let m2 = matchadd('MyGroup2', 'FIXME', 42)")
  66. command("let m3 = matchadd('MyGroup3', 'XXX', 60, 17)")
  67. command("match MyGroup1 /COFFEE/")
  68. command("2match MyGroup2 /HUMPPA/")
  69. command("3match MyGroup3 /VIM/")
  70. command("call clearmatches()")
  71. eq({}, eval('getmatches()'))
  72. -- Check that "setmatches()" restores a list of matches saved by
  73. -- "getmatches()" without changes. (Matches with equal priority must also
  74. -- remain in the same order.)
  75. command("let m1 = matchadd('MyGroup1', 'TODO')")
  76. command("let m2 = matchadd('MyGroup2', 'FIXME', 42)")
  77. command("let m3 = matchadd('MyGroup3', 'XXX', 60, 17)")
  78. command("match MyGroup1 /COFFEE/")
  79. command("2match MyGroup2 /HUMPPA/")
  80. command("3match MyGroup3 /VIM/")
  81. command("let ml = getmatches()")
  82. local ml = eval("ml")
  83. command("call clearmatches()")
  84. command("call setmatches(ml)")
  85. eq(ml, eval('getmatches()'))
  86. -- Check that "setmatches()" can correctly restore the matches from matchaddpos()
  87. command("call clearmatches()")
  88. command("call setmatches(ml)")
  89. eq(ml, eval('getmatches()'))
  90. -- Check that "setmatches()" will not add two matches with the same ID. The
  91. -- expected behaviour (for now) is to add the first match but not the
  92. -- second and to return -1.
  93. eq('\nE801: ID already taken: 1',
  94. redir_exec("let r1 = setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}, {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 10, 'id': 1}])"))
  95. eq(-1, eval("r1"))
  96. eq({{group = 'MyGroup1', pattern = 'TODO', priority = 10, id = 1}}, eval('getmatches()'))
  97. -- Check that "setmatches()" returns 0 if successful and otherwise -1.
  98. -- (A range of valid and invalid input values are tried out to generate the
  99. -- return values.)
  100. eq(0,eval("setmatches([])"))
  101. eq(0,eval("setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}])"))
  102. command("call clearmatches()")
  103. eq('\nE714: List required', redir_exec("let rf1 = setmatches(0)"))
  104. eq(-1, eval('rf1'))
  105. eq('\nE474: List item 0 is either not a dictionary or an empty one',
  106. redir_exec("let rf2 = setmatches([0])"))
  107. eq(-1, eval('rf2'))
  108. eq('\nE474: List item 0 is missing one of the required keys',
  109. redir_exec("let rf3 = setmatches([{'wrong key': 'wrong value'}])"))
  110. eq(-1, eval('rf3'))
  111. -- Check that "matchaddpos()" positions matches correctly
  112. insert('abcdefghijklmnopq')
  113. command("call matchaddpos('MyGroup1', [[1, 5], [1, 8, 3]], 10, 3)")
  114. screen:expect([[
  115. abcd{1:e}fg{1:hij}klmnop^q |
  116. {0:~ }|
  117. {0:~ }|
  118. {0:~ }|
  119. |
  120. ]])
  121. command("call clearmatches()")
  122. command("call setline(1, 'abcdΣabcdef')")
  123. command("call matchaddpos('MyGroup1', [[1, 4, 2], [1, 9, 2]])")
  124. screen:expect([[
  125. abc{1:dΣ}ab{1:cd}e^f |
  126. {0:~ }|
  127. {0:~ }|
  128. {0:~ }|
  129. |
  130. ]])
  131. end)
  132. end)