123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- 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)
|