robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
7.8 KiB324 linesraw
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>
11  local function test(text, exp)
12    it(string.format('can parse %q', text), function()
13      eq(exp, grammar:match(text))
14    end)
15  end
16
17  test('@param hello vim.type', {
18    kind = 'param',
19    name = 'hello',
20    type = 'vim.type',
21  })
22
23  test('@param hello vim.type this is a description', {
24    kind = 'param',
25    name = 'hello',
26    type = 'vim.type',
27    desc = 'this is a description',
28  })
29
30  test('@param hello vim.type|string this is a description', {
31    kind = 'param',
32    name = 'hello',
33    type = 'vim.type|string',
34    desc = 'this is a description',
35  })
36
37  test(
38    '@overload fun<T, Q>(opts: vim.list.bisect.Opts<T|Q> & { key: string & keyof T & keyof Q }): integer',
39    {
40      kind = 'overload',
41      type = 'fun<T, Q>(opts: vim.list.bisect.Opts<T|Q> & { key: string & keyof T & keyof Q }): integer',
42    }
43  )
44
45  test('@param hello vim.type?|string? this is a description', {
46    kind = 'param',
47    name = 'hello',
48    type = 'vim.type?|string?',
49    desc = 'this is a description',
50  })
51
52  test('@return string hello this is a description', {
53    kind = 'return',
54    {
55      name = 'hello',
56      type = 'string',
57    },
58    desc = 'this is a description',
59  })
60
61  test('@return fun() hello this is a description', {
62    kind = 'return',
63    {
64      name = 'hello',
65      type = 'fun()',
66    },
67    desc = 'this is a description',
68  })
69
70  test('@return fun(a: string[]): string hello this is a description', {
71    kind = 'return',
72    {
73      name = 'hello',
74      type = 'fun(a: string[]): string',
75    },
76    desc = 'this is a description',
77  })
78
79  test('@return fun(a: table<string,any>): string hello this is a description', {
80    kind = 'return',
81    {
82      name = 'hello',
83      type = 'fun(a: table<string,any>): string',
84    },
85    desc = 'this is a description',
86  })
87
88  test('@param ... string desc', {
89    kind = 'param',
90    name = '...',
91    type = 'string',
92    desc = 'desc',
93  })
94
95  test('@param level (integer|string) desc', {
96    kind = 'param',
97    name = 'level',
98    type = 'integer|string',
99    desc = 'desc',
100  })
101
102  test('@return (string command) the command and arguments', {
103    kind = 'return',
104    {
105      name = 'command',
106      type = 'string',
107    },
108    desc = 'the command and arguments',
109  })
110
111  test('@return (string command, string[] args) the command and arguments', {
112    kind = 'return',
113    {
114      name = 'command',
115      type = 'string',
116    },
117    {
118      name = 'args',
119      type = 'string[]',
120    },
121    desc = 'the command and arguments',
122  })
123
124  test('@param rfc "rfc2396" | "rfc2732" | "rfc3986" | nil', {
125    kind = 'param',
126    name = 'rfc',
127    type = '"rfc2396" | "rfc2732" | "rfc3986" | nil',
128  })
129
130  test('@param position_encoding "utf-8" | "utf-16" | "utf-32" | nil', {
131    kind = 'param',
132    name = 'position_encoding',
133    type = '"utf-8" | "utf-16" | "utf-32" | nil',
134  })
135
136  -- handle a : after the param type
137  test('@param a b: desc', {
138    kind = 'param',
139    name = 'a',
140    type = 'b',
141    desc = 'desc',
142  })
143
144  test(
145    '@field prefix? string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)',
146    {
147      kind = 'field',
148      name = 'prefix?',
149      type = 'string|table|(fun(diagnostic:vim.Diagnostic,i:integer,total:integer): string, string)',
150    }
151  )
152
153  test('@field [integer] integer', {
154    kind = 'field',
155    name = '[integer]',
156    type = 'integer',
157  })
158
159  test('@field [1] integer', {
160    kind = 'field',
161    name = '[1]',
162    type = 'integer',
163  })
164
165  test('@param type `T` this is a generic type', {
166    desc = 'this is a generic type',
167    kind = 'param',
168    name = 'type',
169    type = '`T`',
170  })
171
172  test('@param type [number,string,"good"|"bad"] this is a tuple type', {
173    desc = 'this is a tuple type',
174    kind = 'param',
175    name = 'type',
176    type = '[number,string,"good"|"bad"]',
177  })
178
179  test('@class vim.diagnostic.JumpOpts', {
180    kind = 'class',
181    name = 'vim.diagnostic.JumpOpts',
182  })
183
184  test('@class vim.diagnostic.JumpOpts : vim.diagnostic.GetOpts', {
185    kind = 'class',
186    name = 'vim.diagnostic.JumpOpts',
187    parent = 'vim.diagnostic.GetOpts',
188  })
189
190  test('@class vim.Iter<V1, V...>', {
191    kind = 'class',
192    name = 'vim.Iter',
193    generics = { 'V1', 'V...' },
194  })
195
196  test('@class vim.IterArray<T> : vim.Iter<T, never>', {
197    kind = 'class',
198    name = 'vim.IterArray',
199    generics = { 'T' },
200    parent = 'vim.Iter',
201    parent_generics = { 'T', 'never' },
202  })
203
204  test('@class vim.lsp.Client.Progress: vim.Ringbuf<{token: integer|string, value: any}>', {
205    kind = 'class',
206    name = 'vim.lsp.Client.Progress',
207    parent = 'vim.Ringbuf',
208    parent_generics = { '{token: integer|string, value: any}' },
209  })
210
211  test('@overload fun<V1, V2, V...>(self: vim.Iter<V1, V2, V...>): [V1, V2, V...][]', {
212    kind = 'overload',
213    type = 'fun<V1, V2, V...>(self: vim.Iter<V1, V2, V...>): [V1, V2, V...][]',
214  })
215
216  test('@param opt? { cmd?: string[] } Options', {
217    kind = 'param',
218    name = 'opt?',
219    type = '{ cmd?: string[] }',
220    desc = 'Options',
221  })
222
223  ---@type [string, string?][]
224  local 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)>',
305      nil,
306    },
307  }
308
309  for _, tc in ipairs(test_cases) do
310    local ty, exp_ty = tc[1], tc[2]
311    if exp_ty == nil then
312      exp_ty = ty
313    end
314
315    local var, desc = 'x', 'some desc'
316    local param = string.format('@param %s %s %s', var, ty, desc)
317    test(param, {
318      kind = 'param',
319      name = var,
320      type = exp_ty,
321      desc = desc,
322    })
323  end
324end)