robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
256a88d0ac
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , setup =t . describe , t . it , t . before_each , t . setup 5local clear , fn , eq =n . clear , n . fn , t . eq 6local api =n . api 7local matches =t . matches 8local pcall_err =t . pcall_err 9 10local function read_mpack_file ( fname ) 11if vim . fn . filereadable ( fname ) == 0 then 12return nil 13end 14return vim . mpack . Unpacker ()( vim . fn . readblob ( fname )) 15end 16 17describe ( "api_info()['version']" , function () 18before_each ( clear ) 19 20it ( 'returns API level' , function () 21local version =fn . api_info ()[ 'version' ] 22local current =version [ 'api_level' ] 23local compat =version [ 'api_compatible' ] 24eq ( 'number' , type ( current )) 25eq ( 'number' , type ( compat )) 26assert ( current >= compat ) 27end ) 28 29it ( 'returns Nvim version' , function () 30local version =fn . api_info ()[ 'version' ] 31local major =version [ 'major' ] 32local minor =version [ 'minor' ] 33local patch =version [ 'patch' ] 34local prerelease =version [ 'prerelease' ] 35local build =version [ 'build' ] 36eq ( 'number' , type ( major )) 37eq ( 'number' , type ( minor )) 38eq ( 'number' , type ( patch )) 39eq ( 'boolean' , type ( prerelease )) 40eq ( 1 , fn . has ( 'nvim-' .. major .. '.' .. minor .. '.' .. patch )) 41eq ( 0 , fn . has ( 'nvim-' .. major .. '.' .. minor .. '.' .. ( patch + 1 ))) 42eq ( 0 , fn . has ( 'nvim-' .. major .. '.' .. ( minor + 1 ) .. '.' .. patch )) 43eq ( 0 , fn . has ( 'nvim-' .. ( major + 1 ) .. '.' .. minor .. '.' .. patch )) 44assert ( build == vim . NIL or type ( build ) == 'string' ) 45end ) 46end ) 47 48describe ( 'api metadata' , function () 49local function name_table ( entries ) 50local by_name ={} 51for _ , e in ipairs ( entries ) do 52by_name [ e . name ] =e 53end 54return by_name 55end 56 57--- Remove or patch metadata that is not essential to backwards-compatibility. 58--- @param f gen_api_dispatch.Function.Exported 59local function normalize_func_metadata ( f ) 60-- Dictionary was renamed to Dict. That doesn't break back-compat because it names the 61-- same RPC map type. 62f . return_type =f . return_type : gsub ( 'Dictionary' , 'Dict' ) 63f . return_type =f . return_type : gsub ( '^ArrayOf%(.*' , 'Array' ) 64 65f . deprecated_since =nil 66for idx , _ in ipairs ( f . parameters ) do 67-- Dictionary was renamed to Dict. That doesn't break back-compat because it names the 68-- same RPC map type. 69f . parameters [ idx ][ 1 ] =f . parameters [ idx ][ 1 ]: gsub ( 'Dictionary' , 'Dict' ) 70f . parameters [ idx ][ 1 ] =f . parameters [ idx ][ 1 ]: gsub ( 'ArrayOf%(.*' , 'Array' ) 71 72f . parameters [ idx ][ 2 ] ='' -- Remove parameter name. 73-- Strip the `optional` flag: `assert_func_backcompat` checks it asymmetrically (relaxing a 74-- param to optional is allowed, the reverse is breaking). 75f . parameters [ idx ][ 3 ] =nil 76end 77 78if string . sub ( f . name , 1 , 4 ) ~= 'nvim' then 79f . method =nil 80end 81return f 82end 83 84--- Checks that the current signature of a function is backwards-compatible with the previous 85--- version, per ":help api-contract". 86--- @param old_fn gen_api_dispatch.Function.Exported 87--- @param new_fn gen_api_dispatch.Function.Exported 88local function assert_func_backcompat ( old_fn , new_fn ) 89-- Optional param must stay optional. 90for idx , old_param in ipairs ( old_fn . parameters ) do 91local new_param =new_fn . parameters [ idx ] 92if old_param [ 3 ] and new_param and not new_param [ 3 ] then 93error (( '"%s": parameter %d was optional, now required' ): format ( old_fn . name , idx )) 94end 95end 96while # new_fn . parameters > # old_fn . parameters and new_fn . parameters [ # new_fn . parameters ][ 3 ] do 97table . remove ( new_fn . parameters ) 98end 99old_fn =normalize_func_metadata ( old_fn ) 100new_fn =normalize_func_metadata ( new_fn ) 101if old_fn . return_type == 'void' then 102old_fn . return_type =new_fn . return_type 103end 104eq ( old_fn , new_fn ) 105end 106 107local function check_ui_event_compatible ( old_e , new_e ) 108-- check types of existing params are the same 109-- adding parameters is ok, but removing params is not (gives nil error) 110eq ( old_e . since , new_e . since , old_e . name ) 111for i , p in ipairs ( old_e . parameters ) do 112eq ( new_e . parameters [ i ][ 1 ], p [ 1 ], old_e . name ) 113end 114end 115 116--- Level 0 represents methods from 0.1.5 and earlier, when 'since' was not 117--- yet defined, and metadata was not filtered of internal keys like 'async'. 118--- 119--- @param metadata { functions: gen_api_dispatch.Function[] } 120local function clean_level_0 ( metadata ) 121for _ , f in ipairs ( metadata . functions ) do 122f . can_fail =nil 123f . async =nil -- XXX: renamed to "fast". 124f . receives_channel_id =nil 125f . since =0 126end 127end 128 129local api_info --[[@type table]] 130local compat --[[@type integer]] 131local stable --[[@type integer]] 132local api_level --[[@type integer]] 133local old_api ={} ---@type { functions: gen_api_dispatch.Function[] }[] 134setup ( function () 135clear () -- Ensure a session before requesting api_info. 136--[[@type { functions: gen_api_dispatch.Function[], version: {api_compatible: integer, api_level: integer, api_prerelease: boolean} }]] 137api_info =api . nvim_get_api_info ()[ 2 ] 138compat =api_info . version . api_compatible 139api_level =api_info . version . api_level 140stable =api_info . version . api_prerelease and api_level - 1 or api_level 141 142for level =compat , stable do 143local path =( 'test/functional/fixtures/api_level_' .. tostring ( level ) .. '.mpack' ) 144old_api [ level ] =read_mpack_file ( path ) 145if old_api [ level ] == nil then 146local errstr ='missing metadata fixture for stable level ' .. level .. '. ' 147if level == api_level and not api_info . version . api_prerelease then 148errstr =( 149errstr 150.. 'If NVIM_API_CURRENT was bumped, ' 151.. "don't forget to set NVIM_API_PRERELEASE to true." 152) 153end 154error ( errstr ) 155end 156 157if level == 0 then 158clean_level_0 ( old_api [ level ]) 159end 160end 161-- No Nvim session will be used in the following tests. 162n . check_close () 163end ) 164 165it ( 'preserves ArrayOf type metadata' , function () 166local funcs =name_table ( api_info . functions ) 167eq ( 'ArrayOf(String)' , funcs . nvim_list_runtime_paths . return_type ) 168eq ( 'ArrayOf(Integer, 2)' , funcs . nvim_buf_get_mark . return_type ) 169eq ( 'ArrayOf(String)' , funcs . nvim_buf_set_lines . parameters [ 5 ][ 1 ]) 170end ) 171 172it ( 'function parameters' , function () 173local funcs =name_table ( api_info . functions ) 174eq ( { 'String' , 'src' , false } , funcs . nvim_exec2 . parameters [ 1 ]) 175eq ( { 'Dict' , 'opts' , true } , funcs . nvim_exec2 . parameters [ 2 ]) 176eq ( { 'Dict' , 'opts' , true } , funcs . nvim_get_context . parameters [ 1 ]) 177eq ( { 'String' , 'name' , false } , funcs . nvim_get_var . parameters [ 1 ]) 178end ) 179 180it ( 'functions are compatible with old metadata or have new level' , function () 181local funcs_new =name_table ( api_info . functions ) 182local funcs_compat ={} 183for level =compat , stable do 184for _ , f in ipairs ( old_api [ level ]. functions ) do 185if funcs_new [ f . name ] == nil then 186if f . since >= compat then 187local msg = 188'function "%s" was removed but exists in level %s which Nvim claims to be compatible with' 189error (( msg ): format ( f . name , f . since )) 190end 191else 192assert_func_backcompat ( f --[[@as any]] , funcs_new [ f . name ]) 193end 194end 195funcs_compat [ level ] =name_table ( old_api [ level ]. functions ) 196end 197 198for _ , f in ipairs ( api_info . functions ) do 199if f . since <= stable then 200local f_old =funcs_compat [ f . since ][ f . name ] 201if f_old == nil then 202if string . sub ( f . name , 1 , 4 ) == 'nvim' then 203local errstr =( 'function "%s" has too low `since` value. For new functions set it to "%s".' ): format ( 204f . name , 205( stable + 1 ) 206) 207if not api_info . version . api_prerelease then 208errstr =( 209errstr 210.. ' Also bump NVIM_API_CURRENT and set ' 211.. 'NVIM_API_PRERELEASE to true in CMakeLists.txt.' 212) 213end 214error ( errstr ) 215else 216error ( "function name '" .. f . name .. "' doesn't begin with 'nvim_'" ) 217end 218end 219elseif f . since > api_level then 220if api_info . version . api_prerelease then 221error ( 'New function ' .. f . name .. ' should use since value ' .. api_level ) 222else 223error ( 224'function ' 225.. f . name 226.. ' has since value > api_level. ' 227.. 'Bump NVIM_API_CURRENT and set ' 228.. 'NVIM_API_PRERELEASE to true in CMakeLists.txt.' 229) 230end 231end 232end 233end ) 234 235it ( 'param may not change from optional to required' , function () 236local function example_fn ( opts_optional ) 237return { 238name ='nvim_example' , 239method =false , 240since =1 , 241return_type ='void' , 242parameters ={ { 'String' , 'src' , false } , { 'Dict' , 'opts' , opts_optional } } , 243} 244end 245-- "Required -> Optional" is allowed. 246assert_func_backcompat ( example_fn ( false ), example_fn ( true )) 247-- "Optional -> Required" is breaking. 248matches ( 249'"nvim_example": parameter 2 was optional, now required' , 250pcall_err ( assert_func_backcompat , example_fn ( true ), example_fn ( false )) 251) 252end ) 253 254it ( 'optional param may be added' , function () 255assert_func_backcompat ( { 256name ='nvim_example' , 257method =false , 258since =1 , 259return_type ='void' , 260parameters ={ { 'String' , 'src' , false } } , 261} , { 262name ='nvim_example' , 263method =false , 264since =1 , 265return_type ='void' , 266parameters ={ { 'String' , 'src' , false } , { 'Dict' , 'opts' , true } } , 267} ) 268end ) 269 270it ( 'UI events are compatible with old metadata or have new level' , function () 271local ui_events_new =name_table ( api_info . ui_events ) 272local ui_events_compat ={} 273 274-- UI events were formalized in level 3 275for level =3 , stable do 276for _ , e in ipairs ( old_api [ level ]. ui_events ) do 277local new_e =ui_events_new [ e . name ] 278if new_e ~= nil then 279check_ui_event_compatible ( e , new_e ) 280end 281end 282ui_events_compat [ level ] =name_table ( old_api [ level ]. ui_events ) 283end 284 285for _ , e in ipairs ( api_info . ui_events ) do 286if e . since <= stable then 287local e_old =ui_events_compat [ e . since ][ e . name ] 288if e_old == nil then 289local errstr =( 290'UI event ' 291.. e . name 292.. ' has too low since value. ' 293.. 'For new events set it to ' 294.. ( stable + 1 ) 295.. '.' 296) 297if not api_info . version . api_prerelease then 298errstr =( 299errstr 300.. ' Also bump NVIM_API_CURRENT and set ' 301.. 'NVIM_API_PRERELEASE to true in CMakeLists.txt.' 302) 303end 304error ( errstr ) 305end 306elseif e . since > api_level then 307if api_info . version . api_prerelease then 308error ( 'New UI event ' .. e . name .. ' should use since value ' .. api_level ) 309else 310error ( 311'UI event ' 312.. e . name 313.. ' has since value > api_level. ' 314.. 'Bump NVIM_API_CURRENT and set ' 315.. 'NVIM_API_PRERELEASE to true in CMakeLists.txt.' 316) 317end 318end 319end 320end ) 321 322it ( 'ui_options are preserved from older levels' , function () 323local available_options ={} 324for _ , option in ipairs ( api_info . ui_options ) do 325available_options [ option ] =true 326end 327-- UI options were versioned from level 4 328for level =4 , stable do 329for _ , option in ipairs ( old_api [ level ]. ui_options ) do 330if not available_options [ option ] then 331error ( 'UI option ' .. option .. ' from stable metadata is missing' ) 332end 333end 334end 335end ) 336end ) 337 338describe ( 'api: optional parameters' , function () 339before_each ( clear ) 340 341it ( 'may be omitted' , function () 342eq ( 'table' , type ( api . nvim_get_context ( {} ))) 343eq ( 'table' , type ( api . nvim_get_context ())) 344 345api . nvim_exec2 ( 'let g:x = 41' ) 346eq ( 41 , api . nvim_get_var ( 'x' )) 347eq ( { output ='hi' } , api . nvim_exec2 ( 'echo "hi"' , { output =true } )) 348 349-- Lua bridge: vim.api 350n . exec_lua ( [[vim.api.nvim_exec2('let g:y = 7')]] ) 351eq ( 7 , api . nvim_get_var ( 'y' )) 352eq ( 'table' , n . exec_lua ( 'return type(vim.api.nvim_get_context())' )) 353end ) 354 355it ( 'omitted is equivalent to empty dict' , function () 356eq ( n . parse_context ( api . nvim_get_context ( {} )), n . parse_context ( api . nvim_get_context ())) -- RPC path 357eq ( 358n . parse_context ( n . exec_lua ( 'return vim.api.nvim_get_context({})' )), 359n . parse_context ( n . exec_lua ( 'return vim.api.nvim_get_context()' )) 360) -- Lua-binding path 361end ) 362 363it ( 'validation' , function () 364matches ( 365'Wrong number of arguments: expecting 1 to 2 but got 3' , 366pcall_err ( api . nvim_exec2 , 'echo 1' , {} , 'surplus' ) 367) 368matches ( 369'Wrong number of arguments: expecting at most 1 but got 2' , 370pcall_err ( api . nvim_get_context , {} , 'surplus' ) 371) 372matches ( 'Wrong number of arguments: expecting 1 to 2 but got 0' , pcall_err ( api . nvim_exec2 )) 373 374-- Lua bridge: vim.api 375matches ( 376'Expected 1 to 2 arguments' , 377pcall_err ( n . exec_lua , [[vim.api.nvim_exec2('echo 1', {}, 'surplus')]] ) 378) 379matches ( 'Expected 1 to 2 arguments' , pcall_err ( n . exec_lua , [[vim.api.nvim_exec2()]] )) 380matches ( 381'Expected at most 1 argument' , 382pcall_err ( n . exec_lua , [[vim.api.nvim_get_context({}, 'surplus')]] ) 383) 384end ) 385end )