063_match_and_matchadd_spec.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 pcall_err = helpers.pcall_err
  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('Vim(let):E803: ID not found: 42', pcall_err(command, 'let r2 = matchdelete(42)'))
  60. -- Check that "clearmatches()" clears all matches defined by ":match" and
  61. -- "matchadd()".
  62. command("let m1 = matchadd('MyGroup1', 'TODO')")
  63. command("let m2 = matchadd('MyGroup2', 'FIXME', 42)")
  64. command("let m3 = matchadd('MyGroup3', 'XXX', 60, 17)")
  65. command("match MyGroup1 /COFFEE/")
  66. command("2match MyGroup2 /HUMPPA/")
  67. command("3match MyGroup3 /VIM/")
  68. command("call clearmatches()")
  69. eq({}, eval('getmatches()'))
  70. -- Check that "setmatches()" restores a list of matches saved by
  71. -- "getmatches()" without changes. (Matches with equal priority must also
  72. -- remain in the same order.)
  73. command("let m1 = matchadd('MyGroup1', 'TODO')")
  74. command("let m2 = matchadd('MyGroup2', 'FIXME', 42)")
  75. command("let m3 = matchadd('MyGroup3', 'XXX', 60, 17)")
  76. command("match MyGroup1 /COFFEE/")
  77. command("2match MyGroup2 /HUMPPA/")
  78. command("3match MyGroup3 /VIM/")
  79. command("let ml = getmatches()")
  80. local ml = eval("ml")
  81. command("call clearmatches()")
  82. command("call setmatches(ml)")
  83. eq(ml, eval('getmatches()'))
  84. -- Check that "setmatches()" can correctly restore the matches from matchaddpos()
  85. command("call clearmatches()")
  86. command("call setmatches(ml)")
  87. eq(ml, eval('getmatches()'))
  88. -- Check that "setmatches()" will not add two matches with the same ID. The
  89. -- expected behaviour (for now) is to add the first match but not the
  90. -- second and to return -1.
  91. eq('Vim(let):E801: ID already taken: 1',
  92. pcall_err(command, "let r1 = setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}, {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 10, 'id': 1}])"))
  93. eq({{group = 'MyGroup1', pattern = 'TODO', priority = 10, id = 1}}, eval('getmatches()'))
  94. -- Check that "setmatches()" returns 0 if successful and otherwise -1.
  95. -- (A range of valid and invalid input values are tried out to generate the
  96. -- return values.)
  97. eq(0,eval("setmatches([])"))
  98. eq(0,eval("setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}])"))
  99. command("call clearmatches()")
  100. eq('Vim(let):E714: List required', pcall_err(command, 'let rf1 = setmatches(0)'))
  101. eq('Vim(let):E474: List item 0 is either not a dictionary or an empty one',
  102. pcall_err(command, 'let rf2 = setmatches([0])'))
  103. eq('Vim(let):E474: List item 0 is missing one of the required keys',
  104. pcall_err(command, "let rf3 = setmatches([{'wrong key': 'wrong value'}])"))
  105. -- Check that "matchaddpos()" positions matches correctly
  106. insert('abcdefghijklmnopq')
  107. command("call matchaddpos('MyGroup1', [[1, 5], [1, 8, 3]], 10, 3)")
  108. screen:expect([[
  109. abcd{1:e}fg{1:hij}klmnop^q |
  110. {0:~ }|
  111. {0:~ }|
  112. {0:~ }|
  113. |
  114. ]])
  115. command("call clearmatches()")
  116. command("call setline(1, 'abcdΣabcdef')")
  117. command("call matchaddpos('MyGroup1', [[1, 4, 2], [1, 9, 2]])")
  118. screen:expect([[
  119. abc{1:dΣ}ab{1:cd}e^f |
  120. {0:~ }|
  121. {0:~ }|
  122. {0:~ }|
  123. |
  124. ]])
  125. end)
  126. end)