robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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