@@ -250,6 +250,75 @@ def test_translate(self):
250250 self .assertTrue (re .match (fatre , 'cbabcaxc' ))
251251 self .assertFalse (re .match (fatre , 'dabccbad' ))
252252
253+ def test_translate_wildcards (self ):
254+ for pattern , expect in [
255+ ('ab*' , r'(?s:ab.*)\Z' ),
256+ ('ab*cd' , r'(?s:ab.*cd)\Z' ),
257+ ('ab*cd*' , r'(?s:ab(?>.*?cd).*)\Z' ),
258+ ('ab*cd*12' , r'(?s:ab(?>.*?cd).*12)\Z' ),
259+ ('ab*cd*12*' , r'(?s:ab(?>.*?cd)(?>.*?12).*)\Z' ),
260+ ('ab*cd*12*34' , r'(?s:ab(?>.*?cd)(?>.*?12).*34)\Z' ),
261+ ('ab*cd*12*34*' , r'(?s:ab(?>.*?cd)(?>.*?12)(?>.*?34).*)\Z' ),
262+ ]:
263+ with self .subTest (pattern ):
264+ translated = translate (pattern )
265+ self .assertEqual (translated , expect , pattern )
266+
267+ for pattern , expect in [
268+ ('*ab' , r'(?s:.*ab)\Z' ),
269+ ('*ab*' , r'(?s:(?>.*?ab).*)\Z' ),
270+ ('*ab*cd' , r'(?s:(?>.*?ab).*cd)\Z' ),
271+ ('*ab*cd*' , r'(?s:(?>.*?ab)(?>.*?cd).*)\Z' ),
272+ ('*ab*cd*12' , r'(?s:(?>.*?ab)(?>.*?cd).*12)\Z' ),
273+ ('*ab*cd*12*' , r'(?s:(?>.*?ab)(?>.*?cd)(?>.*?12).*)\Z' ),
274+ ('*ab*cd*12*34' , r'(?s:(?>.*?ab)(?>.*?cd)(?>.*?12).*34)\Z' ),
275+ ('*ab*cd*12*34*' , r'(?s:(?>.*?ab)(?>.*?cd)(?>.*?12)(?>.*?34).*)\Z' ),
276+ ]:
277+ with self .subTest (pattern ):
278+ translated = translate (pattern )
279+ self .assertEqual (translated , expect , pattern )
280+
281+ def test_translate_expressions (self ):
282+ for pattern , expect in [
283+ ('[' , r'(?s:\[)\Z' ),
284+ ('[!' , r'(?s:\[!)\Z' ),
285+ ('[]' , r'(?s:\[\])\Z' ),
286+ ('[abc' , r'(?s:\[abc)\Z' ),
287+ ('[!abc' , r'(?s:\[!abc)\Z' ),
288+ ('[abc]' , r'(?s:[abc])\Z' ),
289+ ('[!abc]' , r'(?s:[^abc])\Z' ),
290+ ('[!abc][!def]' , r'(?s:[^abc][^def])\Z' ),
291+ # with [[
292+ ('[[' , r'(?s:\[\[)\Z' ),
293+ ('[[a' , r'(?s:\[\[a)\Z' ),
294+ ('[[]' , r'(?s:[\[])\Z' ),
295+ ('[[]a' , r'(?s:[\[]a)\Z' ),
296+ ('[[]]' , r'(?s:[\[]\])\Z' ),
297+ ('[[]a]' , r'(?s:[\[]a\])\Z' ),
298+ ('[[a]' , r'(?s:[\[a])\Z' ),
299+ ('[[a]]' , r'(?s:[\[a]\])\Z' ),
300+ ('[[a]b' , r'(?s:[\[a]b)\Z' ),
301+ # backslashes
302+ ('[\\ ' , r'(?s:\[\\)\Z' ),
303+ (r'[\]' , r'(?s:[\\])\Z' ),
304+ (r'[\\]' , r'(?s:[\\\\])\Z' ),
305+ ]:
306+ with self .subTest (pattern ):
307+ translated = translate (pattern )
308+ self .assertEqual (translated , expect , pattern )
309+
310+ def test_star_indices_locations (self ):
311+ from fnmatch import _translate
312+
313+ blocks = ['a^b' , '***' , '?' , '?' , '[a-z]' , '[1-9]' , '*' , '++' , '[[a' ]
314+ parts , star_indices = _translate ('' .join (blocks ), '*' , '.' )
315+ expect_parts = ['a' , r'\^' , 'b' , '*' ,
316+ '.' , '.' , '[a-z]' , '[1-9]' , '*' ,
317+ r'\+' , r'\+' , r'\[' , r'\[' , 'a' ]
318+ self .assertListEqual (parts , expect_parts )
319+ self .assertListEqual (star_indices , [3 , 8 ])
320+
321+
253322class FilterTestCase (unittest .TestCase ):
254323
255324 def test_filter (self ):
0 commit comments