aboutsummaryrefslogtreecommitdiff
path: root/test/functional/vimscript/match_functions_spec.lua
blob: 87c57f1c15d97fc709d559753d31d864598afe4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')

local eq = t.eq
local clear = n.clear
local fn = n.fn
local command = n.command
local exc_exec = n.exc_exec

before_each(clear)

describe('setmatches()', function()
  it('correctly handles case when both group and pattern entries are numbers', function()
    command('hi def link 1 PreProc')
    eq(0, fn.setmatches({ { group = 1, pattern = 2, id = 3, priority = 4 } }))
    eq({
      {
        group = '1',
        pattern = '2',
        id = 3,
        priority = 4,
      },
    }, fn.getmatches())
    eq(0, fn.setmatches({ { group = 1, pattern = 2, id = 3, priority = 4, conceal = 5 } }))
    eq({
      {
        group = '1',
        pattern = '2',
        id = 3,
        priority = 4,
        conceal = '5',
      },
    }, fn.getmatches())
    eq(
      0,
      fn.setmatches({
        { group = 1, pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = 5 },
      })
    )
    eq({
      {
        group = '1',
        pos1 = { 2 },
        pos2 = { 6 },
        id = 3,
        priority = 4,
        conceal = '5',
      },
    }, fn.getmatches())
  end)

  it('does not fail if highlight group is not defined', function()
    eq(0, fn.setmatches { { group = 1, pattern = 2, id = 3, priority = 4 } })
    eq({ { group = '1', pattern = '2', id = 3, priority = 4 } }, fn.getmatches())
    eq(
      0,
      fn.setmatches {
        { group = 1, pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = 5 },
      }
    )
    eq(
      { { group = '1', pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = '5' } },
      fn.getmatches()
    )
  end)
end)

describe('matchadd()', function()
  it('correctly works when first two arguments and conceal are numbers at once', function()
    command('hi def link 1 PreProc')
    eq(4, fn.matchadd(1, 2, 3, 4, { conceal = 5 }))
    eq({
      {
        group = '1',
        pattern = '2',
        priority = 3,
        id = 4,
        conceal = '5',
      },
    }, fn.getmatches())
  end)
end)

describe('matchaddpos()', function()
  it('errors out on invalid input', function()
    command('hi clear PreProc')
    eq(
      'Vim(let):E5030: Empty list at position 0',
      exc_exec('let val = matchaddpos("PreProc", [[]])')
    )
    eq(
      'Vim(let):E5030: Empty list at position 1',
      exc_exec('let val = matchaddpos("PreProc", [1, v:_null_list])')
    )
    eq(
      'Vim(let):E5031: List or number required at position 1',
      exc_exec('let val = matchaddpos("PreProc", [1, v:_null_dict])')
    )
  end)
  it('works with 0 lnum', function()
    command('hi clear PreProc')
    eq(4, fn.matchaddpos('PreProc', { 1 }, 3, 4))
    eq({
      {
        group = 'PreProc',
        pos1 = { 1 },
        priority = 3,
        id = 4,
      },
    }, fn.getmatches())
    fn.matchdelete(4)
    eq(4, fn.matchaddpos('PreProc', { { 0 }, 1 }, 3, 4))
    eq({
      {
        group = 'PreProc',
        pos1 = { 1 },
        priority = 3,
        id = 4,
      },
    }, fn.getmatches())
    fn.matchdelete(4)
    eq(4, fn.matchaddpos('PreProc', { 0, 1 }, 3, 4))
    eq({
      {
        group = 'PreProc',
        pos1 = { 1 },
        priority = 3,
        id = 4,
      },
    }, fn.getmatches())
  end)
  it('works with negative numbers', function()
    command('hi clear PreProc')
    eq(4, fn.matchaddpos('PreProc', { -10, 1 }, 3, 4))
    eq({
      {
        group = 'PreProc',
        pos1 = { 1 },
        priority = 3,
        id = 4,
      },
    }, fn.getmatches())
    fn.matchdelete(4)
    eq(4, fn.matchaddpos('PreProc', { { -10 }, 1 }, 3, 4))
    eq({
      {
        group = 'PreProc',
        pos1 = { 1 },
        priority = 3,
        id = 4,
      },
    }, fn.getmatches())
    fn.matchdelete(4)
    eq(4, fn.matchaddpos('PreProc', { { 2, -1 }, 1 }, 3, 4))
    eq({
      {
        group = 'PreProc',
        pos1 = { 1 },
        priority = 3,
        id = 4,
      },
    }, fn.getmatches())
    fn.matchdelete(4)
    eq(4, fn.matchaddpos('PreProc', { { 2, 0, -1 }, 1 }, 3, 4))
    eq({
      {
        group = 'PreProc',
        pos1 = { 1 },
        priority = 3,
        id = 4,
      },
    }, fn.getmatches())
  end)
  it('works with zero length', function()
    local screen = Screen.new(40, 5)
    screen:attach()
    fn.setline(1, 'abcdef')
    command('hi PreProc guifg=Red')
    eq(4, fn.matchaddpos('PreProc', { { 1, 2, 0 } }, 3, 4))
    eq({
      {
        group = 'PreProc',
        pos1 = { 1, 2, 0 },
        priority = 3,
        id = 4,
      },
    }, fn.getmatches())
    screen:expect(
      [[
      ^a{1:b}cdef                                  |
      {2:~                                       }|*3
                                              |
    ]],
      {
        [1] = { foreground = Screen.colors.Red },
        [2] = { bold = true, foreground = Screen.colors.Blue1 },
      }
    )
  end)
end)