11"""Test cases for the fnmatch module."""
22
33import unittest
4+ import os
45
56from fnmatch import fnmatch , fnmatchcase , translate , filter
67
78class FnmatchTestCase (unittest .TestCase ):
89
9- def check_match (self , filename , pattern , should_match = 1 , fn = fnmatch ):
10+ def check_match (self , filename , pattern , should_match = True , fn = fnmatch ):
1011 if should_match :
1112 self .assertTrue (fn (filename , pattern ),
1213 "expected %r to match pattern %r"
1314 % (filename , pattern ))
1415 else :
15- self .assertTrue ( not fn (filename , pattern ),
16+ self .assertFalse ( fn (filename , pattern ),
1617 "expected %r not to match pattern %r"
1718 % (filename , pattern ))
1819
@@ -26,15 +27,15 @@ def test_fnmatch(self):
2627 check ('abc' , '*' )
2728 check ('abc' , 'ab[cd]' )
2829 check ('abc' , 'ab[!de]' )
29- check ('abc' , 'ab[de]' , 0 )
30- check ('a' , '??' , 0 )
31- check ('a' , 'b' , 0 )
30+ check ('abc' , 'ab[de]' , False )
31+ check ('a' , '??' , False )
32+ check ('a' , 'b' , False )
3233
3334 # these test that '\' is handled correctly in character sets;
3435 # see SF bug #409651
3536 check ('\\ ' , r'[\]' )
3637 check ('a' , r'[!\]' )
37- check ('\\ ' , r'[!\]' , 0 )
38+ check ('\\ ' , r'[!\]' , False )
3839
3940 # test that filenames with newlines in them are handled correctly.
4041 # http://bugs.python.org/issue6665
@@ -51,14 +52,38 @@ def test_mix_bytes_str(self):
5152
5253 def test_fnmatchcase (self ):
5354 check = self .check_match
54- check ('AbC' , 'abc' , 0 , fnmatchcase )
55- check ('abc' , 'AbC' , 0 , fnmatchcase )
55+ check ('abc' , 'abc' , True , fnmatchcase )
56+ check ('AbC' , 'abc' , False , fnmatchcase )
57+ check ('abc' , 'AbC' , False , fnmatchcase )
58+ check ('AbC' , 'AbC' , True , fnmatchcase )
59+
60+ check ('usr/bin' , 'usr/bin' , True , fnmatchcase )
61+ check ('usr\\ bin' , 'usr/bin' , False , fnmatchcase )
62+ check ('usr/bin' , 'usr\\ bin' , False , fnmatchcase )
63+ check ('usr\\ bin' , 'usr\\ bin' , True , fnmatchcase )
5664
5765 def test_bytes (self ):
5866 self .check_match (b'test' , b'te*' )
5967 self .check_match (b'test\xff ' , b'te*\xff ' )
6068 self .check_match (b'foo\n bar' , b'foo*' )
6169
70+ def test_case (self ):
71+ ignorecase = os .path .normcase ('ABC' ) == os .path .normcase ('abc' )
72+ check = self .check_match
73+ check ('abc' , 'abc' )
74+ check ('AbC' , 'abc' , ignorecase )
75+ check ('abc' , 'AbC' , ignorecase )
76+ check ('AbC' , 'AbC' )
77+
78+ def test_sep (self ):
79+ normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
80+ check = self .check_match
81+ check ('usr/bin' , 'usr/bin' )
82+ check ('usr\\ bin' , 'usr/bin' , normsep )
83+ check ('usr/bin' , 'usr\\ bin' , normsep )
84+ check ('usr\\ bin' , 'usr\\ bin' )
85+
86+
6287class TranslateTestCase (unittest .TestCase ):
6388
6489 def test_translate (self ):
@@ -75,7 +100,28 @@ def test_translate(self):
75100class FilterTestCase (unittest .TestCase ):
76101
77102 def test_filter (self ):
78- self .assertEqual (filter (['a' , 'b' ], 'a' ), ['a' ])
103+ self .assertEqual (filter (['Python' , 'Ruby' , 'Perl' , 'Tcl' ], 'P*' ),
104+ ['Python' , 'Perl' ])
105+ self .assertEqual (filter ([b'Python' , b'Ruby' , b'Perl' , b'Tcl' ], b'P*' ),
106+ [b'Python' , b'Perl' ])
107+
108+ def test_mix_bytes_str (self ):
109+ self .assertRaises (TypeError , filter , ['test' ], b'*' )
110+ self .assertRaises (TypeError , filter , [b'test' ], '*' )
111+
112+ def test_case (self ):
113+ ignorecase = os .path .normcase ('P' ) == os .path .normcase ('p' )
114+ self .assertEqual (filter (['Test.py' , 'Test.rb' , 'Test.PL' ], '*.p*' ),
115+ ['Test.py' , 'Test.PL' ] if ignorecase else ['Test.py' ])
116+ self .assertEqual (filter (['Test.py' , 'Test.rb' , 'Test.PL' ], '*.P*' ),
117+ ['Test.py' , 'Test.PL' ] if ignorecase else ['Test.PL' ])
118+
119+ def test_sep (self ):
120+ normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
121+ self .assertEqual (filter (['usr/bin' , 'usr' , 'usr\\ lib' ], 'usr/*' ),
122+ ['usr/bin' , 'usr\\ lib' ] if normsep else ['usr/bin' ])
123+ self .assertEqual (filter (['usr/bin' , 'usr' , 'usr\\ lib' ], 'usr\\ *' ),
124+ ['usr/bin' , 'usr\\ lib' ] if normsep else ['usr\\ lib' ])
79125
80126
81127if __name__ == "__main__" :
0 commit comments