aboutsummaryrefslogtreecommitdiff
path: root/test/functional/provider/define_spec.lua
blob: 12efbec326bef06b1b61ba5a02696f45f22f595f (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
local helpers = require('test.functional.helpers')(after_each)
local eval, command, nvim = helpers.eval, helpers.command, helpers.nvim
local eq, run, stop = helpers.eq, helpers.run, helpers.stop
local clear = helpers.clear

local function get_prefix(sync)
  if sync then
    return 'sync'
  end
  return 'async'
end

local function call(fn, arguments)
  command('call '..fn..'('..arguments..')')
end

local function clear_and_init(init)
  return function()
    clear()
    if init then
      init()
    end
  end
end

local function runx(sync, handler, on_setup)
  local function setup_cb(...)
    on_setup(...)
    -- need to stop on setup callback because there's two session:request
    -- calls in `request/helpers.lua`. The second call will always return
    -- after pending notification/request callbacks are processed
    stop()
  end
  local function handler_cb(...)
    return handler(...)
  end
  if sync then
    run(handler_cb, nil, setup_cb)
  else
    run(nil, handler_cb, setup_cb)
  end
end

local function command_specs_for(fn, sync, first_arg_factory, init)
  local prefix = get_prefix(sync)

  describe(prefix..' command created by', function()
    before_each(clear_and_init(init))

    describe(fn, function()
      local args

      before_each(function()
        args = first_arg_factory()..', "test-handler", '
        if sync then
          args = args .. '1'
        else
          args = args .. '0'
        end
        args = args..', "RpcCommand"'
      end)

      it('without options', function()
        call(fn, args..', {}')
        local function on_setup()
          command('RpcCommand')
        end

        local function handler(method)
          eq('test-handler', method)
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with nargs', function()
        call(fn, args..', {"nargs": "*"}')
        local function on_setup()
          command('RpcCommand arg1 arg2 arg3')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({'arg1', 'arg2', 'arg3'}, arguments[1])
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with nargs/double-quote', function()
        call(fn, args..', {"nargs": "*"}')
        local function on_setup()
          command('RpcCommand "arg1" "arg2" "arg3"')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({'"arg1"', '"arg2"', '"arg3"'}, arguments[1])
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with range', function()
        call(fn,args..', {"range": ""}')
        local function on_setup()
          command('1,1RpcCommand')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({1, 1}, arguments[1])
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with nargs/range', function()
        call(fn, args..', {"nargs": "1", "range": ""}')
        local function on_setup()
          command('1,1RpcCommand arg')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({'arg'}, arguments[1])
          eq({1, 1}, arguments[2])
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with nargs/count', function()
        call(fn, args..', {"nargs": "1", "count": "5"}')
        local function on_setup()
          command('5RpcCommand arg')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({'arg'}, arguments[1])
          eq(5, arguments[2])
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with nargs/count/bang', function()
        call(fn, args..', {"nargs": "1", "count": "5", "bang": ""}')
        local function on_setup()
          command('5RpcCommand! arg')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({'arg'}, arguments[1])
          eq(5, arguments[2])
          eq(1, arguments[3])
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with nargs/count/bang/register', function()
        call(fn, args..', {"nargs": "1", "count": "5", "bang": "",'..
        ' "register": ""}')
        local function on_setup()
          command('5RpcCommand! b arg')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({'arg'}, arguments[1])
          eq(5, arguments[2])
          eq(1, arguments[3])
          eq('b', arguments[4])
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with nargs/count/bang/register/eval', function()
        call(fn, args..', {"nargs": "1", "count": "5", "bang": "",'..
        ' "register": "", "eval": "@<reg>"}')
        local function on_setup()
          command('let @b = "regb"')
          command('5RpcCommand! b arg')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({'arg'}, arguments[1])
          eq(5, arguments[2])
          eq(1, arguments[3])
          eq('b', arguments[4])
          eq('regb', arguments[5])
          return ''
        end

        runx(sync, handler, on_setup)
      end)
    end)
  end)
end


local function autocmd_specs_for(fn, sync, first_arg_factory, init)
  local prefix = get_prefix(sync)

  describe(prefix..' autocmd created by', function()
    before_each(clear_and_init(init))

    describe(fn, function()
      local args

      before_each(function()
        args = first_arg_factory()..', "test-handler", '
        if sync then
          args = args .. '1'
        else
          args = args .. '0'
        end
        args = args..', "BufEnter"'
      end)

      it('without options', function()
        call(fn, args..', {}')
        local function on_setup()
          command('doautocmd BufEnter x.c')
        end

        local function handler(method)
          eq('test-handler', method)
          return ''
        end

        runx(sync, handler, on_setup)
      end)

      it('with eval', function()
        call(fn, args..[[, {'eval': 'expand("<afile>")'}]])
        local function on_setup()
          command('doautocmd BufEnter x.c')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq('x.c', arguments[1])
          return ''
        end

        runx(sync, handler, on_setup)
      end)
    end)
  end)
end


local function function_specs_for(fn, sync, first_arg_factory, init)
  local prefix = get_prefix(sync)

  describe(prefix..' function created by', function()
    before_each(clear_and_init(init))

    describe(fn, function()
      local args

      before_each(function()
        args = first_arg_factory()..', "test-handler", '
        if sync then
          args = args .. '1'
        else
          args = args .. '0'
        end
        args = args..', "TestFunction"'
      end)

      it('without options', function()
        call(fn, args..', {}')
        local function on_setup()
          if sync then
            eq('rv', eval('TestFunction(1, "a", ["b", "c"])'))
          else
            eq(1, eval('TestFunction(1, "a", ["b", "c"])'))
          end
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({{1, 'a', {'b', 'c'}}}, arguments)
          return 'rv'
        end

        runx(sync, handler, on_setup)
      end)

      it('with eval', function()
        call(fn, args..[[, {'eval': '2 + 2'}]])
        local function on_setup()
          if sync then
            eq('rv', eval('TestFunction(1, "a", ["b", "c"])'))
          else
            eq(1, eval('TestFunction(1, "a", ["b", "c"])'))
          end
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({{1, 'a', {'b', 'c'}}, 4}, arguments)
          return 'rv'
        end

        runx(sync, handler, on_setup)
      end)

      it('with range', function()
        helpers.insert([[
          foo
          bar
          baz
          zub]])
        call(fn, args..[[, {'range': ''}]])
        local function on_setup()
          command('2,3call TestFunction(1, "a", ["b", "c"])')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({{1, 'a', {'b', 'c'}}, {2, 3}}, arguments)
          return 'rv'
        end

        runx(sync, handler, on_setup)
      end)

      it('with eval/range', function()
        call(fn, args..[[, {'eval': '4', 'range': ''}]])
        local function on_setup()
          command('%call TestFunction(1, "a", ["b", "c"])')
        end

        local function handler(method, arguments)
          eq('test-handler', method)
          eq({{1, 'a', {'b', 'c'}}, {1, 1}, 4}, arguments)
          return 'rv'
        end

        runx(sync, handler, on_setup)
      end)
    end)
  end)
end

local function channel()
  return nvim('get_api_info')[1]
end

local function host()
  return '"busted"'
end

local function register()
  eval('remote#host#Register("busted", "busted", '..channel()..')')
end

command_specs_for('remote#define#CommandOnChannel', true, channel)
command_specs_for('remote#define#CommandOnChannel', false, channel)
command_specs_for('remote#define#CommandOnHost', true, host, register)
command_specs_for('remote#define#CommandOnHost', false, host, register)

autocmd_specs_for('remote#define#AutocmdOnChannel', true, channel)
autocmd_specs_for('remote#define#AutocmdOnChannel', false, channel)
autocmd_specs_for('remote#define#AutocmdOnHost', true, host, register)
autocmd_specs_for('remote#define#AutocmdOnHost', false, host, register)

function_specs_for('remote#define#FunctionOnChannel', true, channel)
function_specs_for('remote#define#FunctionOnChannel', false, channel)
function_specs_for('remote#define#FunctionOnHost', true, host, register)
function_specs_for('remote#define#FunctionOnHost', false, host, register)