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