2
2
3
3
from test import test_support
4
4
import unittest
5
+ import os
5
6
6
- from fnmatch import fnmatch , fnmatchcase , _MAXCACHE , _cache
7
- from fnmatch import fnmatch , fnmatchcase , _MAXCACHE , _cache , _purge
7
+ from fnmatch import ( fnmatch , fnmatchcase , translate , filter ,
8
+ _MAXCACHE , _cache , _purge )
8
9
9
10
10
11
class FnmatchTestCase (unittest .TestCase ):
11
12
12
13
def tearDown (self ):
13
14
_purge ()
14
15
15
- def check_match (self , filename , pattern , should_match = 1 , fn = fnmatch ):
16
+ def check_match (self , filename , pattern , should_match = True , fn = fnmatch ):
16
17
if should_match :
17
18
self .assertTrue (fn (filename , pattern ),
18
19
"expected %r to match pattern %r"
19
20
% (filename , pattern ))
20
21
else :
21
- self .assertTrue ( not fn (filename , pattern ),
22
+ self .assertFalse ( fn (filename , pattern ),
22
23
"expected %r not to match pattern %r"
23
24
% (filename , pattern ))
24
25
@@ -32,15 +33,15 @@ def test_fnmatch(self):
32
33
check ('abc' , '*' )
33
34
check ('abc' , 'ab[cd]' )
34
35
check ('abc' , 'ab[!de]' )
35
- check ('abc' , 'ab[de]' , 0 )
36
- check ('a' , '??' , 0 )
37
- check ('a' , 'b' , 0 )
36
+ check ('abc' , 'ab[de]' , False )
37
+ check ('a' , '??' , False )
38
+ check ('a' , 'b' , False )
38
39
39
40
# these test that '\' is handled correctly in character sets;
40
41
# see SF bug #409651
41
42
check ('\\ ' , r'[\]' )
42
43
check ('a' , r'[!\]' )
43
- check ('\\ ' , r'[!\]' , 0 )
44
+ check ('\\ ' , r'[!\]' , False )
44
45
45
46
# test that filenames with newlines in them are handled correctly.
46
47
# http://bugs.python.org/issue6665
@@ -49,10 +50,29 @@ def test_fnmatch(self):
49
50
check ('\n foo' , 'foo*' , False )
50
51
check ('\n ' , '*' )
51
52
53
+ def test_mix_unicode_str (self ):
54
+ check = self .check_match
55
+ check ('test' , u'*' )
56
+ check (u'test' , '*' )
57
+ check ('test' , u'*' , fn = fnmatchcase )
58
+ check (u'test' , '*' , fn = fnmatchcase )
59
+ with test_support .check_warnings (("" , UnicodeWarning ), quiet = True ):
60
+ check ('test\xff ' , u'*\xff ' )
61
+ check (u'test\xff ' , '*\xff ' )
62
+ check ('test\xff ' , u'*\xff ' , fn = fnmatchcase )
63
+ check (u'test\xff ' , '*\xff ' , fn = fnmatchcase )
64
+
52
65
def test_fnmatchcase (self ):
53
66
check = self .check_match
54
- check ('AbC' , 'abc' , 0 , fnmatchcase )
55
- check ('abc' , 'AbC' , 0 , fnmatchcase )
67
+ check ('abc' , 'abc' , True , fnmatchcase )
68
+ check ('AbC' , 'abc' , False , fnmatchcase )
69
+ check ('abc' , 'AbC' , False , fnmatchcase )
70
+ check ('AbC' , 'AbC' , True , fnmatchcase )
71
+
72
+ check ('usr/bin' , 'usr/bin' , True , fnmatchcase )
73
+ check ('usr\\ bin' , 'usr/bin' , False , fnmatchcase )
74
+ check ('usr/bin' , 'usr\\ bin' , False , fnmatchcase )
75
+ check ('usr\\ bin' , 'usr\\ bin' , True , fnmatchcase )
56
76
57
77
def test_cache_clearing (self ):
58
78
# check that caches do not grow too large
@@ -64,8 +84,81 @@ def test_cache_clearing(self):
64
84
65
85
self .assertLessEqual (len (_cache ), _MAXCACHE )
66
86
87
+ @test_support .requires_unicode
88
+ def test_unicode (self ):
89
+ with test_support .check_warnings (("" , UnicodeWarning ), quiet = True ):
90
+ self .check_match (u'test' , u'te*' )
91
+ self .check_match (u'test\xff ' , u'te*\xff ' )
92
+ self .check_match (u'test' + unichr (0x20ac ), u'te*' + unichr (0x20ac ))
93
+ self .check_match (u'foo\n bar' , u'foo*' )
94
+
95
+ def test_case (self ):
96
+ ignorecase = os .path .normcase ('ABC' ) == os .path .normcase ('abc' )
97
+ check = self .check_match
98
+ check ('abc' , 'abc' )
99
+ check ('AbC' , 'abc' , ignorecase )
100
+ check ('abc' , 'AbC' , ignorecase )
101
+ check ('AbC' , 'AbC' )
102
+
103
+ def test_sep (self ):
104
+ normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
105
+ check = self .check_match
106
+ check ('usr/bin' , 'usr/bin' )
107
+ check ('usr\\ bin' , 'usr/bin' , normsep )
108
+ check ('usr/bin' , 'usr\\ bin' , normsep )
109
+ check ('usr\\ bin' , 'usr\\ bin' )
110
+
111
+
112
+ class TranslateTestCase (unittest .TestCase ):
113
+
114
+ def test_translate (self ):
115
+ self .assertEqual (translate ('*' ), r'.*\Z(?ms)' )
116
+ self .assertEqual (translate ('?' ), r'.\Z(?ms)' )
117
+ self .assertEqual (translate ('a?b*' ), r'a.b.*\Z(?ms)' )
118
+ self .assertEqual (translate ('[abc]' ), r'[abc]\Z(?ms)' )
119
+ self .assertEqual (translate ('[]]' ), r'[]]\Z(?ms)' )
120
+ self .assertEqual (translate ('[!x]' ), r'[^x]\Z(?ms)' )
121
+ self .assertEqual (translate ('[^x]' ), r'[\^x]\Z(?ms)' )
122
+ self .assertEqual (translate ('[x' ), r'\[x\Z(?ms)' )
123
+
124
+
125
+ class FilterTestCase (unittest .TestCase ):
126
+
127
+ def test_filter (self ):
128
+ self .assertEqual (filter (['Python' , 'Ruby' , 'Perl' , 'Tcl' ], 'P*' ),
129
+ ['Python' , 'Perl' ])
130
+ self .assertEqual (filter ([u'Python' , u'Ruby' , u'Perl' , u'Tcl' ], u'P*' ),
131
+ [u'Python' , u'Perl' ])
132
+ with test_support .check_warnings (("" , UnicodeWarning ), quiet = True ):
133
+ self .assertEqual (filter ([u'test\xff ' ], u'*\xff ' ), [u'test\xff ' ])
134
+
135
+ @test_support .requires_unicode
136
+ def test_mix_bytes_str (self ):
137
+ with test_support .check_warnings (("" , UnicodeWarning ), quiet = True ):
138
+ self .assertEqual (filter (['test' ], u'*' ), ['test' ])
139
+ self .assertEqual (filter ([u'test' ], '*' ), [u'test' ])
140
+ self .assertEqual (filter (['test\xff ' ], u'*' ), ['test\xff ' ])
141
+ self .assertEqual (filter ([u'test\xff ' ], '*' ), [u'test\xff ' ])
142
+ self .assertEqual (filter (['test\xff ' ], u'*\xff ' ), ['test\xff ' ])
143
+ self .assertEqual (filter ([u'test\xff ' ], '*\xff ' ), [u'test\xff ' ])
144
+
145
+ def test_case (self ):
146
+ ignorecase = os .path .normcase ('P' ) == os .path .normcase ('p' )
147
+ self .assertEqual (filter (['Test.py' , 'Test.rb' , 'Test.PL' ], '*.p*' ),
148
+ ['Test.py' , 'Test.PL' ] if ignorecase else ['Test.py' ])
149
+ self .assertEqual (filter (['Test.py' , 'Test.rb' , 'Test.PL' ], '*.P*' ),
150
+ ['Test.py' , 'Test.PL' ] if ignorecase else ['Test.PL' ])
151
+
152
+ def test_sep (self ):
153
+ normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
154
+ self .assertEqual (filter (['usr/bin' , 'usr' , 'usr\\ lib' ], 'usr/*' ),
155
+ ['usr/bin' , 'usr\\ lib' ] if normsep else ['usr/bin' ])
156
+ self .assertEqual (filter (['usr/bin' , 'usr' , 'usr\\ lib' ], 'usr\\ *' ),
157
+ ['usr/bin' , 'usr\\ lib' ] if normsep else ['usr\\ lib' ])
158
+
159
+
67
160
def test_main ():
68
- test_support .run_unittest (FnmatchTestCase )
161
+ test_support .run_unittest (FnmatchTestCase , TranslateTestCase , FilterTestCase )
69
162
70
163
71
164
if __name__ == "__main__" :
0 commit comments