robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2 3local describe , it =t . describe , t . it 4local eq =t . eq 5 6local grammar =require ( 'gen.luacats_grammar' ) 7 8describe ( 'luacats grammar' , function () 9--- @param text string 10--- @param exp table<string,string> 11local function test ( text , exp ) 12it ( string . format ( 'can parse %q' , text ), function () 13eq ( exp , grammar : match ( text )) 14end ) 15end 16 17test ( '@param hello vim.type' , { 18kind ='param' , 19name ='hello' , 20type ='vim.type' , 21} ) 22 23test ( '@param hello vim.type this is a description' , { 24kind ='param' , 25name ='hello' , 26type ='vim.type' , 27desc ='this is a description' , 28} ) 29 30test ( '@param hello vim.type|string this is a description' , { 31kind ='param' , 32name ='hello' , 33type ='vim.type|string' , 34desc ='this is a description' , 35} ) 36 37test ( 38'@overload fun<T, Q>(opts: vim.list.bisect.Opts<T|Q> & { key: string & keyof T & keyof Q }): integer' , 39{ 40kind ='overload' , 41type ='fun<T, Q>(opts: vim.list.bisect.Opts<T|Q> & { key: string & keyof T & keyof Q }): integer' , 42} 43) 44 45test ( '@param hello vim.type?|string? this is a description' , { 46kind ='param' , 47name ='hello' , 48type ='vim.type?|string?' , 49desc ='this is a description' , 50} ) 51 52test ( '@return string hello this is a description' , { 53kind ='return' , 54{ 55name ='hello' , 56type ='string' , 57} , 58desc ='this is a description' , 59} ) 60 61test ( '@return fun() hello this is a description' , { 62kind ='return' , 63{ 64name ='hello' , 65type ='fun()' , 66} , 67desc ='this is a description' , 68} ) 69 70test ( '@return fun(a: string[]): string hello this is a description' , { 71kind ='return' , 72{ 73name ='hello' , 74type ='fun(a: string[]): string' , 75} , 76desc ='this is a description' , 77} ) 78 79test ( '@return fun(a: table<string,any>): string hello this is a description' , { 80kind ='return' , 81{ 82name ='hello' , 83type ='fun(a: table<string,any>): string' , 84} , 85desc ='this is a description' , 86} ) 87 88test ( '@param ... string desc' , { 89kind ='param' , 90name ='...' , 91type ='string' , 92desc ='desc' , 93} ) 94 95test ( '@param level (integer|string) desc' , { 96kind ='param' , 97name ='level' , 98type ='integer|string' , 99desc ='desc' , 100} ) 101 102test ( '@return (string command) the command and arguments' , { 103kind ='return' , 104{ 105name ='command' , 106type ='string' , 107} , 108desc ='the command and arguments' , 109} ) 110 111test ( '@return (string command, string[] args) the command and arguments' , { 112kind ='return' , 113{ 114name ='command' , 115type ='string' , 116} , 117{ 118name ='args' , 119type ='string[]' , 120} , 121desc ='the command and arguments' , 122} ) 123 124test ( '@param rfc "rfc2396" | "rfc2732" | "rfc3986" | nil' , { 125kind ='param' , 126name ='rfc' , 127type ='"rfc2396" | "rfc2732" | "rfc3986" | nil' , 128} ) 129 130test ( '@param position_encoding "utf-8" | "utf-16" | "utf-32" | nil' , { 131kind ='param' , 132name ='position_encoding' , 133type ='"utf-8" | "utf-16" | "utf-32" | nil' , 134} ) 135 136-- handle a : after the param type 137test ( '@param a b: desc' , { 138kind ='param' , 139name ='a' , 140type ='b' , 141desc ='desc' , 142} ) 143 144test ( 145'@field prefix? string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)' , 146{ 147kind ='field' , 148name ='prefix?' , 149type ='string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)' , 150} 151) 152 153test ( '@field [integer] integer' , { 154kind ='field' , 155name ='[integer]' , 156type ='integer' , 157} ) 158 159test ( '@field [1] integer' , { 160kind ='field' , 161name ='[1]' , 162type ='integer' , 163} ) 164 165test ( '@param type `T` this is a generic type' , { 166desc ='this is a generic type' , 167kind ='param' , 168name ='type' , 169type ='`T`' , 170} ) 171 172test ( '@param type [number,string,"good"|"bad"] this is a tuple type' , { 173desc ='this is a tuple type' , 174kind ='param' , 175name ='type' , 176type ='[number,string,"good"|"bad"]' , 177} ) 178 179test ( '@class vim.diagnostic.JumpOpts' , { 180kind ='class' , 181name ='vim.diagnostic.JumpOpts' , 182} ) 183 184test ( '@class vim.diagnostic.JumpOpts : vim.diagnostic.GetOpts' , { 185kind ='class' , 186name ='vim.diagnostic.JumpOpts' , 187parent ='vim.diagnostic.GetOpts' , 188} ) 189 190test ( '@class vim.Iter<V1, V...>' , { 191kind ='class' , 192name ='vim.Iter' , 193generics ={ 'V1' , 'V...' } , 194} ) 195 196test ( '@class vim.IterArray<T> : vim.Iter<T, never>' , { 197kind ='class' , 198name ='vim.IterArray' , 199generics ={ 'T' } , 200parent ='vim.Iter' , 201parent_generics ={ 'T' , 'never' } , 202} ) 203 204test ( '@class vim.lsp.Client.Progress: vim.Ringbuf<{token: integer|string, value: any}>' , { 205kind ='class' , 206name ='vim.lsp.Client.Progress' , 207parent ='vim.Ringbuf' , 208parent_generics ={ '{token: integer|string, value: any}' } , 209} ) 210 211test ( '@overload fun<V1, V2, V...>(self: vim.Iter<V1, V2, V...>): [V1, V2, V...][]' , { 212kind ='overload' , 213type ='fun<V1, V2, V...>(self: vim.Iter<V1, V2, V...>): [V1, V2, V...][]' , 214} ) 215 216test ( '@param opt? { cmd?: string[] } Options' , { 217kind ='param' , 218name ='opt?' , 219type ='{ cmd?: string[] }' , 220desc ='Options' , 221} ) 222 223---@type [string, string?][] 224local test_cases ={ 225{ 'foo' } , 226{ 'foo ' , 'foo' } , -- trims whitespace 227{ 'true' } , 228{ 'vim.type' } , 229{ 'vim-type' } , 230{ 'vim_type' } , 231{ 'foo.bar-baz_baz' } , 232{ '`ABC`' } , 233{ '42' } , 234{ '-42' } , 235{ '(foo)' , 'foo' } , -- removes unnecessary parens 236{ 'true?' } , 237{ '(true)?' } , 238{ 'string[]' } , 239{ 'string|number' } , 240{ '(string)[]' } , 241{ '(string|number)[]' } , 242{ 'coalesce??' , 'coalesce?' } , -- removes unnecessary ? 243{ 'number?|string' } , 244{ "'foo'|'bar'|'baz'" } , 245{ '"foo"|"bar"|"baz"' } , 246{ '(number)?|string' } , -- 247{ 'number[]|string' } , 248{ 'string[]?' } , 249{ 'foo?[]' } , 250{ 'vim.type?|string? ' , 'vim.type?|string?' } , 251{ 'number[][]' } , 252{ 'number[][][]' } , 253{ 'number[][]?' } , 254{ 'string|integer[][]?' } , 255{ 'vim.type & { key: string|function }' } , 256{ '(vim.type & { key: string })|nil' } , 257 258-- tuples 259{ '[string]' } , 260{ '[1]' } , 261{ '[string, number]' } , 262{ '[string, number]?' } , 263{ '[string, number][]' } , 264{ '[string, number]|string' } , 265{ '[string|number, number?]' } , 266{ 'string|[string, number]' } , 267{ '(true)?|[foo]' } , 268{ '[fun(a: string):boolean]' } , 269 270-- dict 271{ '{[string]:string}' } , 272{ '{ [ string ] : string }' } , 273{ '{ [ string|any ] : string }' } , 274{ '{[string]: string, [number]: boolean}' } , 275 276-- key-value table 277{ 'table<string,any>' } , 278{ 'table' } , 279{ 'string|table|boolean' } , 280{ 'string|table|(boolean)' } , 281 282-- table literal 283{ '{foo: number}' } , 284{ '{foo: string, bar: [number, boolean]?}' } , 285{ 'boolean|{reverse?:boolean}' } , 286{ '{ cmd?: string[] }' } , 287 288-- function 289{ 'fun(a: string, b:foo|bar): string' } , 290{ 'fun(a?: string): string' } , 291{ 'fun(a?: string): number?,string?' } , 292{ '(fun(a: string, b:foo|bar): string)?' } , 293{ 'fun(a: string, b:foo|bar): string, string' } , 294{ 'fun(a: string, b:foo|bar)' } , 295{ 'fun(_, foo, bar): string' } , 296{ 'fun(...): number' } , 297{ 'fun( ... ): number' } , 298{ 'fun(...:number): number' } , 299{ 'fun( ... : number): number' } , 300 301-- generics 302{ 'elem_or_list<string>' } , 303{ 304'elem_or_list<fun(client: vim.lsp.Client, init_result: lsp.InitializeResult)>' , 305nil , 306} , 307} 308 309for _ , tc in ipairs ( test_cases ) do 310local ty , exp_ty =tc [ 1 ], tc [ 2 ] 311if exp_ty == nil then 312exp_ty =ty 313end 314 315local var , desc ='x' , 'some desc' 316local param =string . format ( '@param %s %s %s' , var , ty , desc ) 317test ( param , { 318kind ='param' , 319name =var , 320type =exp_ty , 321desc =desc , 322} ) 323end 324end )