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