robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
10.8 KiB273 linesraw
1local t = require('test.testutil')
2
3local describe, it = t.describe, t.it
4local eq = t.eq
5
6describe('glob', function()
7  local match = function(pattern, str)
8    return require('vim.glob').to_lpeg(pattern):match(str) ~= nil
9  end
10
11  describe('glob matching', function()
12    it('should match literal strings', function()
13      eq(true, match('', ''))
14      eq(false, match('', 'a'))
15      eq(true, match('a', 'a'))
16      eq(true, match('.', '.'))
17      eq(true, match('/', '/'))
18      eq(true, match('abc', 'abc'))
19      eq(false, match('abc', 'abcdef'))
20      eq(false, match('abc', 'a'))
21      eq(false, match('abc', 'bc'))
22      eq(false, match('a', 'b'))
23      eq(false, match('.', 'a'))
24      eq(true, match('$', '$'))
25      eq(true, match('a,b', 'a,b'))
26      eq(true, match('/dir', '/dir'))
27      eq(true, match('dir/', 'dir/'))
28      eq(true, match('dir/subdir', 'dir/subdir'))
29      eq(false, match('dir/subdir', 'subdir'))
30      eq(false, match('dir/subdir', 'dir/subdir/file'))
31      eq(true, match('🤠', '🤠'))
32    end)
33
34    it('should match * wildcards', function()
35      eq(true, match('*', ''))
36      eq(true, match('*', '   '))
37      eq(true, match('*', 'a'))
38      eq(false, match('*', '/'))
39      eq(false, match('*', '/a'))
40      eq(false, match('*', 'a/'))
41      eq(true, match('*', 'aaa'))
42      eq(true, match('*a', 'aa'))
43      eq(true, match('*a', 'abca'))
44      eq(true, match('*.ts', '.ts'))
45      eq(true, match('*.txt', 'file.txt'))
46      eq(false, match('*.txt', 'file.txtxt'))
47      eq(false, match('*.txt', 'dir/file.txt'))
48      eq(false, match('*.txt', '/dir/file.txt'))
49      eq(false, match('*.txt', 'C:/dir/file.txt'))
50      eq(false, match('*.dir', 'test.dir/file'))
51      eq(true, match('file.*', 'file.txt'))
52      eq(false, match('file.*', 'not-file.txt'))
53      eq(true, match('*/file.txt', 'dir/file.txt'))
54      eq(false, match('*/file.txt', 'dir/subdir/file.txt'))
55      eq(false, match('*/file.txt', '/dir/file.txt'))
56      eq(true, match('dir/*', 'dir/file.txt'))
57      eq(false, match('dir/*', 'dir'))
58      eq(false, match('dir/*.txt', 'file.txt'))
59      eq(true, match('dir/*.txt', 'dir/file.txt'))
60      eq(false, match('dir/*.txt', 'dir/subdir/file.txt'))
61      eq(false, match('dir/*/file.txt', 'dir/file.txt'))
62      eq(true, match('dir/*/file.txt', 'dir/subdir/file.txt'))
63      eq(false, match('dir/*/file.txt', 'dir/subdir/subdir/file.txt'))
64      eq(true, match('a*b*c*d*e*', 'axbxcxdxe'))
65      eq(true, match('a*b*c*d*e*', 'axbxcxdxexxx'))
66      eq(true, match('a*b?c*x', 'abxbbxdbxebxczzx'))
67      eq(false, match('a*b?c*x', 'abxbbxdbxebxczzy'))
68      eq(true, match('a*b*[cy]*d*e*', 'axbxcxdxexxx'))
69      eq(true, match('a*b*[cy]*d*e*', 'axbxyxdxexxx'))
70      eq(true, match('a*b*[cy]*d*e*', 'axbxxxyxdxexxx'))
71      eq(true, match('.ps*1', '.ps1'))
72      eq(true, match('.ps*1', '.psaa1'))
73      eq(false, match('.ps*1', '.ps1a'))
74    end)
75
76    it('should match ? wildcards', function()
77      eq(false, match('?', ''))
78      eq(true, match('?', 'a'))
79      eq(false, match('??', 'a'))
80      eq(false, match('?', 'ab'))
81      eq(true, match('??', 'ab'))
82      eq(true, match('a?c', 'abc'))
83      eq(false, match('a?c', 'a/c'))
84      eq(false, match('a/', 'a/.b'))
85      eq(true, match('?/?', 'a/b'))
86      eq(true, match('/??', '/ab'))
87      eq(true, match('/?b', '/ab'))
88      eq(false, match('foo?bar', 'foo/bar'))
89    end)
90
91    it('should match ** wildcards', function()
92      eq(true, match('**', ''))
93      eq(true, match('**', 'a'))
94      eq(true, match('**', '/'))
95      eq(true, match('**', 'a/'))
96      eq(true, match('**', '/a'))
97      eq(true, match('**', 'C:/a'))
98      eq(true, match('**', 'a/a'))
99      eq(true, match('**', 'a/a/a'))
100      eq(false, match('/**', '')) -- /** matches leading / literally
101      eq(true, match('/**', '/'))
102      eq(true, match('/**', '/a/b/c'))
103      eq(true, match('**/', '')) -- **/ absorbs trailing /
104      eq(false, match('**/', '/a/b/c'))
105      eq(true, match('**/**', ''))
106      eq(true, match('**/**', 'a'))
107      eq(false, match('a/**', ''))
108      eq(false, match('a/**', 'a'))
109      eq(true, match('a/**', 'a/b'))
110      eq(true, match('a/**', 'a/b/c'))
111      eq(false, match('a/**', 'b/a'))
112      eq(false, match('a/**', '/a'))
113      eq(false, match('**/a', ''))
114      eq(true, match('**/a', 'a'))
115      eq(false, match('**/a', 'a/b'))
116      eq(true, match('**/a', '/a'))
117      eq(true, match('**/a', '/b/a'))
118      eq(true, match('**/a', '/c/b/a'))
119      eq(true, match('**/a', '/a/a'))
120      eq(true, match('**/a', '/abc/a'))
121      eq(false, match('a/**/c', 'a'))
122      eq(false, match('a/**/c', 'c'))
123      eq(true, match('a/**/c', 'a/c'))
124      eq(true, match('a/**/c', 'a/b/c'))
125      eq(true, match('a/**/c', 'a/b/b/c'))
126      eq(false, match('**/a/**', 'a'))
127      eq(true, match('**/a/**', 'a/'))
128      eq(false, match('**/a/**', '/dir/a'))
129      eq(false, match('**/a/**', 'dir/a'))
130      eq(true, match('**/a/**', 'dir/a/'))
131      eq(true, match('**/a/**', 'a/dir'))
132      eq(true, match('**/a/**', 'dir/a/dir'))
133      eq(true, match('**/a/**', '/a/dir'))
134      eq(true, match('**/a/**', 'C:/a/dir'))
135      eq(false, match('**/a/**', 'a.txt'))
136    end)
137
138    it('should match {} groups', function()
139      eq(true, match('{,}', ''))
140      eq(true, match('{a,}', ''))
141      eq(true, match('{a,}', 'a'))
142      eq(true, match('{a,b}', 'a'))
143      eq(true, match('{a,b}', 'b'))
144      eq(false, match('{a,b}', 'ab'))
145      eq(true, match('{ab,cd}', 'ab'))
146      eq(false, match('{ab,cd}', 'a'))
147      eq(true, match('{ab,cd}', 'cd'))
148      eq(true, match('{a,b,c}', 'c'))
149      eq(true, match('{a,{b,c}}', 'c'))
150      eq(true, match('a{,/}*.txt', 'a.txt'))
151      eq(true, match('a{,/}*.txt', 'ab.txt'))
152      eq(true, match('a{,/}*.txt', 'a/b.txt'))
153      eq(true, match('a{,/}*.txt', 'a/ab.txt'))
154      eq(true, match('a/{a{a,b},b}', 'a/aa'))
155      eq(true, match('a/{a{a,b},b}', 'a/ab'))
156      eq(false, match('a/{a{a,b},b}', 'a/ac'))
157      eq(true, match('a/{a{a,b},b}', 'a/b'))
158      eq(false, match('a/{a{a,b},b}', 'a/c'))
159      eq(true, match('foo{bar,b*z}', 'foobar'))
160      eq(true, match('foo{bar,b*z}', 'foobuzz'))
161      eq(true, match('foo{bar,b*z}', 'foobarz'))
162      eq(true, match('{a,b}/c/{d,e}/**/*est.ts', 'a/c/d/one/two/three.test.ts'))
163      eq(true, match('{a,{d,e}b}/c', 'a/c'))
164      eq(true, match('{**/a,**/b}', 'b'))
165    end)
166
167    it('should match [] groups', function()
168      eq(true, match('[]', '[]')) -- empty [] is a literal
169      eq(false, match('[a-z]', ''))
170      eq(true, match('[a-z]', 'a'))
171      eq(false, match('[a-z]', 'ab'))
172      eq(true, match('[a-z]', 'z'))
173      eq(true, match('[a-z]', 'j'))
174      eq(false, match('[a-f]', 'j'))
175      eq(false, match('[a-z]', '`')) -- 'a' - 1
176      eq(false, match('[a-z]', '{')) -- 'z' + 1
177      eq(false, match('[a-z]', 'A'))
178      eq(false, match('[a-z]', '5'))
179      eq(true, match('[A-Z]', 'A'))
180      eq(true, match('[A-Z]', 'Z'))
181      eq(true, match('[A-Z]', 'J'))
182      eq(false, match('[A-Z]', '@')) -- 'A' - 1
183      eq(false, match('[A-Z]', '[')) -- 'Z' + 1
184      eq(false, match('[A-Z]', 'a'))
185      eq(false, match('[A-Z]', '5'))
186      eq(true, match('[a-zA-Z0-9]', 'z'))
187      eq(true, match('[a-zA-Z0-9]', 'Z'))
188      eq(true, match('[a-zA-Z0-9]', '9'))
189      eq(false, match('[a-zA-Z0-9]', '&'))
190      eq(true, match('[?]', '?'))
191      eq(false, match('[?]', 'a'))
192      eq(true, match('[*]', '*'))
193      eq(false, match('[*]', 'a'))
194      eq(true, match('[\\!]', '!'))
195      eq(true, match('a\\*b', 'a*b'))
196      eq(false, match('a\\*b', 'axb'))
197    end)
198
199    it('should match [!...] groups', function()
200      eq(true, match('[!]', '[!]')) -- [!] is a literal
201      eq(false, match('[!a-z]', ''))
202      eq(false, match('[!a-z]', 'a'))
203      eq(false, match('[!a-z]', 'z'))
204      eq(false, match('[!a-z]', 'j'))
205      eq(true, match('[!a-f]', 'j'))
206      eq(false, match('[!a-f]', 'jj'))
207      eq(true, match('[!a-z]', '`')) -- 'a' - 1
208      eq(true, match('[!a-z]', '{')) -- 'z' + 1
209      eq(false, match('[!a-zA-Z0-9]', 'a'))
210      eq(false, match('[!a-zA-Z0-9]', 'A'))
211      eq(false, match('[!a-zA-Z0-9]', '0'))
212      eq(true, match('[!a-zA-Z0-9]', '!'))
213    end)
214
215    it('should handle long patterns', function()
216      -- lpeg has a recursion limit of 200 by default, make sure the grammar does trigger it on
217      -- strings longer than that
218      local fill_200 = ('a'):rep(200)
219      eq(200, fill_200:len())
220      local long_lit = fill_200 .. 'a'
221      eq(false, match(long_lit, 'b'))
222      eq(true, match(long_lit, long_lit))
223      local long_pat = fill_200 .. 'a/**/*.c'
224      eq(true, match(long_pat, fill_200 .. 'a/b/c/d.c'))
225    end)
226
227    -- New test for unicode patterns from assets
228    it('should match unicode patterns', function()
229      eq(true, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.ts'))
230      eq(true, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.tsx'))
231      eq(true, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.js'))
232      eq(true, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.jsx'))
233      eq(false, match('😎/¢£.{ts,tsx,js,jsx}', '😎/¢£.jsxxxxxxxx'))
234      eq(true, match('*é*', 'café noir'))
235      eq(true, match('caf*noir', 'café noir'))
236      eq(true, match('caf*noir', 'cafeenoir'))
237      eq(true, match('F[ë£a]', 'Fë'))
238      eq(true, match('F[ë£a]', 'F£'))
239      eq(true, match('F[ë£a]', 'Fa'))
240    end)
241
242    it('should match complex patterns', function()
243      eq(false, match('**/*.{c,h}', ''))
244      eq(false, match('**/*.{c,h}', 'c'))
245      eq(false, match('**/*.{c,h}', 'file.m'))
246      eq(true, match('**/*.{c,h}', 'file.c'))
247      eq(true, match('**/*.{c,h}', 'file.h'))
248      eq(true, match('**/*.{c,h}', '/file.c'))
249      eq(true, match('**/*.{c,h}', 'dir/subdir/file.c'))
250      eq(true, match('**/*.{c,h}', 'dir/subdir/file.h'))
251      eq(true, match('**/*.{c,h}', '/dir/subdir/file.c'))
252      eq(true, match('**/*.{c,h}', 'C:/dir/subdir/file.c'))
253      eq(true, match('/dir/**/*.{c,h}', '/dir/file.c'))
254      eq(false, match('/dir/**/*.{c,h}', 'dir/file.c'))
255      eq(true, match('/dir/**/*.{c,h}', '/dir/subdir/subdir/file.c'))
256
257      eq(true, match('{[0-9],[a-z]}', '0'))
258      eq(true, match('{[0-9],[a-z]}', 'a'))
259      eq(false, match('{[0-9],[a-z]}', 'A'))
260
261      -- glob is from willRename filter in typescript-language-server
262      -- https://github.com/typescript-language-server/typescript-language-server/blob/b224b878652438bcdd639137a6b1d1a6630129e4/src/lsp-server.ts#L266
263      eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.js'))
264      eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.ts'))
265      eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.mts'))
266      eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.mjs'))
267      eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.cjs'))
268      eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.cts'))
269      eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.jsx'))
270      eq(true, match('**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', 'test.tsx'))
271    end)
272  end)
273end)