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