1
1
"""Test cases for the fnmatch module."""
2
2
3
3
import unittest
4
+ import os
4
5
5
6
from fnmatch import fnmatch , fnmatchcase , translate , filter
6
7
7
8
class FnmatchTestCase (unittest .TestCase ):
8
9
9
- def check_match (self , filename , pattern , should_match = 1 , fn = fnmatch ):
10
+ def check_match (self , filename , pattern , should_match = True , fn = fnmatch ):
10
11
if should_match :
11
12
self .assertTrue (fn (filename , pattern ),
12
13
"expected %r to match pattern %r"
13
14
% (filename , pattern ))
14
15
else :
15
- self .assertTrue ( not fn (filename , pattern ),
16
+ self .assertFalse ( fn (filename , pattern ),
16
17
"expected %r not to match pattern %r"
17
18
% (filename , pattern ))
18
19
@@ -26,15 +27,15 @@ def test_fnmatch(self):
26
27
check ('abc' , '*' )
27
28
check ('abc' , 'ab[cd]' )
28
29
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 )
32
33
33
34
# these test that '\' is handled correctly in character sets;
34
35
# see SF bug #409651
35
36
check ('\\ ' , r'[\]' )
36
37
check ('a' , r'[!\]' )
37
- check ('\\ ' , r'[!\]' , 0 )
38
+ check ('\\ ' , r'[!\]' , False )
38
39
39
40
# test that filenames with newlines in them are handled correctly.
40
41
# http://bugs.python.org/issue6665
@@ -51,14 +52,38 @@ def test_mix_bytes_str(self):
51
52
52
53
def test_fnmatchcase (self ):
53
54
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 )
56
64
57
65
def test_bytes (self ):
58
66
self .check_match (b'test' , b'te*' )
59
67
self .check_match (b'test\xff ' , b'te*\xff ' )
60
68
self .check_match (b'foo\n bar' , b'foo*' )
61
69
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
+
62
87
class TranslateTestCase (unittest .TestCase ):
63
88
64
89
def test_translate (self ):
@@ -75,7 +100,28 @@ def test_translate(self):
75
100
class FilterTestCase (unittest .TestCase ):
76
101
77
102
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' ])
79
125
80
126
81
127
if __name__ == "__main__" :
0 commit comments