robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
10.2 KiB392 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local eval, command = n.eval, n.command
6local eq, run, stop = t.eq, n.run, n.stop
7local clear = n.clear
8local api = n.api
9
10local function get_prefix(sync)
11  if sync then
12    return 'sync'
13  end
14  return 'async'
15end
16
17local function call(fn, arguments)
18  command('call ' .. fn .. '(' .. arguments .. ')')
19end
20
21local function clear_and_init(init)
22  return function()
23    clear()
24    if init then
25      init()
26    end
27  end
28end
29
30local function runx(sync, handler, on_setup)
31  local function setup_cb(...)
32    on_setup(...)
33    -- need to stop on setup callback because there's two session:request
34    -- calls in `request/testnvim.lua`. The second call will always return
35    -- after pending notification/request callbacks are processed
36    stop()
37  end
38  local function handler_cb(...)
39    return handler(...)
40  end
41  if sync then
42    run(handler_cb, nil, setup_cb)
43  else
44    run(nil, handler_cb, setup_cb)
45  end
46end
47
48local function command_specs_for(fn, sync, first_arg_factory, init)
49  local prefix = get_prefix(sync)
50
51  describe(prefix .. ' command created by', function()
52    before_each(clear_and_init(init))
53
54    describe(fn, function()
55      local args
56
57      before_each(function()
58        args = first_arg_factory() .. ', "test-handler", '
59        if sync then
60          args = args .. '1'
61        else
62          args = args .. '0'
63        end
64        args = args .. ', "RpcCommand"'
65      end)
66
67      it('without options', function()
68        call(fn, args .. ', {}')
69        local function on_setup()
70          command('RpcCommand')
71        end
72
73        local function handler(method)
74          eq('test-handler', method)
75          return ''
76        end
77
78        runx(sync, handler, on_setup)
79      end)
80
81      it('with nargs', function()
82        call(fn, args .. ', {"nargs": "*"}')
83        local function on_setup()
84          command('RpcCommand arg1 arg2 arg3')
85        end
86
87        local function handler(method, arguments)
88          eq('test-handler', method)
89          eq({ 'arg1', 'arg2', 'arg3' }, arguments[1])
90          return ''
91        end
92
93        runx(sync, handler, on_setup)
94      end)
95
96      it('with nargs/double-quote', function()
97        call(fn, args .. ', {"nargs": "*"}')
98        local function on_setup()
99          command('RpcCommand "arg1" "arg2" "arg3"')
100        end
101
102        local function handler(method, arguments)
103          eq('test-handler', method)
104          eq({ '"arg1"', '"arg2"', '"arg3"' }, arguments[1])
105          return ''
106        end
107
108        runx(sync, handler, on_setup)
109      end)
110
111      it('with range', function()
112        call(fn, args .. ', {"range": ""}')
113        local function on_setup()
114          command('1,1RpcCommand')
115        end
116
117        local function handler(method, arguments)
118          eq('test-handler', method)
119          eq({ 1, 1 }, arguments[1])
120          return ''
121        end
122
123        runx(sync, handler, on_setup)
124      end)
125
126      it('with nargs/range', function()
127        call(fn, args .. ', {"nargs": "1", "range": ""}')
128        local function on_setup()
129          command('1,1RpcCommand arg')
130        end
131
132        local function handler(method, arguments)
133          eq('test-handler', method)
134          eq({ 'arg' }, arguments[1])
135          eq({ 1, 1 }, arguments[2])
136          return ''
137        end
138
139        runx(sync, handler, on_setup)
140      end)
141
142      it('with nargs/count', function()
143        call(fn, args .. ', {"nargs": "1", "count": "5"}')
144        local function on_setup()
145          command('5RpcCommand arg')
146        end
147
148        local function handler(method, arguments)
149          eq('test-handler', method)
150          eq({ 'arg' }, arguments[1])
151          eq(5, arguments[2])
152          return ''
153        end
154
155        runx(sync, handler, on_setup)
156      end)
157
158      it('with nargs/count/bang', function()
159        call(fn, args .. ', {"nargs": "1", "count": "5", "bang": ""}')
160        local function on_setup()
161          command('5RpcCommand! arg')
162        end
163
164        local function handler(method, arguments)
165          eq('test-handler', method)
166          eq({ 'arg' }, arguments[1])
167          eq(5, arguments[2])
168          eq(1, arguments[3])
169          return ''
170        end
171
172        runx(sync, handler, on_setup)
173      end)
174
175      it('with nargs/count/bang/register', function()
176        call(fn, args .. ', {"nargs": "1", "count": "5", "bang": "",' .. ' "register": ""}')
177        local function on_setup()
178          command('5RpcCommand! b arg')
179        end
180
181        local function handler(method, arguments)
182          eq('test-handler', method)
183          eq({ 'arg' }, arguments[1])
184          eq(5, arguments[2])
185          eq(1, arguments[3])
186          eq('b', arguments[4])
187          return ''
188        end
189
190        runx(sync, handler, on_setup)
191      end)
192
193      it('with nargs/count/bang/register/eval', function()
194        call(
195          fn,
196          args
197            .. ', {"nargs": "1", "count": "5", "bang": "",'
198            .. ' "register": "", "eval": "@<reg>"}'
199        )
200        local function on_setup()
201          command('let @b = "regb"')
202          command('5RpcCommand! b arg')
203        end
204
205        local function handler(method, arguments)
206          eq('test-handler', method)
207          eq({ 'arg' }, arguments[1])
208          eq(5, arguments[2])
209          eq(1, arguments[3])
210          eq('b', arguments[4])
211          eq('regb', arguments[5])
212          return ''
213        end
214
215        runx(sync, handler, on_setup)
216      end)
217    end)
218  end)
219end
220
221local function autocmd_specs_for(fn, sync, first_arg_factory, init)
222  local prefix = get_prefix(sync)
223
224  describe(prefix .. ' autocmd created by', function()
225    before_each(clear_and_init(init))
226
227    describe(fn, function()
228      local args
229
230      before_each(function()
231        args = first_arg_factory() .. ', "test-handler", '
232        if sync then
233          args = args .. '1'
234        else
235          args = args .. '0'
236        end
237        args = args .. ', "BufEnter"'
238      end)
239
240      it('without options', function()
241        call(fn, args .. ', {}')
242        local function on_setup()
243          command('doautocmd BufEnter x.c')
244        end
245
246        local function handler(method)
247          eq('test-handler', method)
248          return ''
249        end
250
251        runx(sync, handler, on_setup)
252      end)
253
254      it('with eval', function()
255        call(fn, args .. [[, {'eval': 'expand("<afile>")'}]])
256        local function on_setup()
257          command('doautocmd BufEnter x.c')
258        end
259
260        local function handler(method, arguments)
261          eq('test-handler', method)
262          eq('x.c', arguments[1])
263          return ''
264        end
265
266        runx(sync, handler, on_setup)
267      end)
268    end)
269  end)
270end
271
272local function function_specs_for(fn, sync, first_arg_factory, init)
273  local prefix = get_prefix(sync)
274
275  describe(prefix .. ' function created by', function()
276    before_each(clear_and_init(init))
277
278    describe(fn, function()
279      local args
280
281      before_each(function()
282        args = first_arg_factory() .. ', "test-handler", '
283        if sync then
284          args = args .. '1'
285        else
286          args = args .. '0'
287        end
288        args = args .. ', "TestFunction"'
289      end)
290
291      it('without options', function()
292        call(fn, args .. ', {}')
293        local function on_setup()
294          if sync then
295            eq('rv', eval('TestFunction(1, "a", ["b", "c"])'))
296          else
297            eq(1, eval('TestFunction(1, "a", ["b", "c"])'))
298          end
299        end
300
301        local function handler(method, arguments)
302          eq('test-handler', method)
303          eq({ { 1, 'a', { 'b', 'c' } } }, arguments)
304          return 'rv'
305        end
306
307        runx(sync, handler, on_setup)
308      end)
309
310      it('with eval', function()
311        call(fn, args .. [[, {'eval': '2 + 2'}]])
312        local function on_setup()
313          if sync then
314            eq('rv', eval('TestFunction(1, "a", ["b", "c"])'))
315          else
316            eq(1, eval('TestFunction(1, "a", ["b", "c"])'))
317          end
318        end
319
320        local function handler(method, arguments)
321          eq('test-handler', method)
322          eq({ { 1, 'a', { 'b', 'c' } }, 4 }, arguments)
323          return 'rv'
324        end
325
326        runx(sync, handler, on_setup)
327      end)
328
329      it('with range', function()
330        n.insert([[
331          foo
332          bar
333          baz
334          zub]])
335        call(fn, args .. [[, {'range': ''}]])
336        local function on_setup()
337          command('2,3call TestFunction(1, "a", ["b", "c"])')
338        end
339
340        local function handler(method, arguments)
341          eq('test-handler', method)
342          eq({ { 1, 'a', { 'b', 'c' } }, { 2, 3 } }, arguments)
343          return 'rv'
344        end
345
346        runx(sync, handler, on_setup)
347      end)
348
349      it('with eval/range', function()
350        call(fn, args .. [[, {'eval': '4', 'range': ''}]])
351        local function on_setup()
352          command('%call TestFunction(1, "a", ["b", "c"])')
353        end
354
355        local function handler(method, arguments)
356          eq('test-handler', method)
357          eq({ { 1, 'a', { 'b', 'c' } }, { 1, 1 }, 4 }, arguments)
358          return 'rv'
359        end
360
361        runx(sync, handler, on_setup)
362      end)
363    end)
364  end)
365end
366
367local function channel()
368  return api.nvim_get_chan_info(0).id
369end
370
371local function host()
372  return '"busted"'
373end
374
375local function register()
376  eval('remote#host#Register("busted", "busted", ' .. channel() .. ')')
377end
378
379command_specs_for('remote#define#CommandOnChannel', true, channel)
380command_specs_for('remote#define#CommandOnChannel', false, channel)
381command_specs_for('remote#define#CommandOnHost', true, host, register)
382command_specs_for('remote#define#CommandOnHost', false, host, register)
383
384autocmd_specs_for('remote#define#AutocmdOnChannel', true, channel)
385autocmd_specs_for('remote#define#AutocmdOnChannel', false, channel)
386autocmd_specs_for('remote#define#AutocmdOnHost', true, host, register)
387autocmd_specs_for('remote#define#AutocmdOnHost', false, host, register)
388
389function_specs_for('remote#define#FunctionOnChannel', true, channel)
390function_specs_for('remote#define#FunctionOnChannel', false, channel)
391function_specs_for('remote#define#FunctionOnHost', true, host, register)
392function_specs_for('remote#define#FunctionOnHost', false, host, register)