1
1
"""Test cases for the fnmatch module."""
2
2
3
- import unittest
4
3
import os
5
4
import string
5
+ import unittest
6
6
import warnings
7
+ from fnmatch import fnmatch , fnmatchcase , translate , filter , filterfalse
8
+
9
+
10
+ IGNORECASE = os .path .normcase ('P' ) == os .path .normcase ('p' )
11
+ NORMSEP = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
7
12
8
- from fnmatch import fnmatch , fnmatchcase , translate , filter
9
13
10
14
class FnmatchTestCase (unittest .TestCase ):
11
15
@@ -77,35 +81,32 @@ def test_bytes(self):
77
81
self .check_match (b'foo\n bar' , b'foo*' )
78
82
79
83
def test_case (self ):
80
- ignorecase = os .path .normcase ('ABC' ) == os .path .normcase ('abc' )
81
84
check = self .check_match
82
85
check ('abc' , 'abc' )
83
- check ('AbC' , 'abc' , ignorecase )
84
- check ('abc' , 'AbC' , ignorecase )
86
+ check ('AbC' , 'abc' , IGNORECASE )
87
+ check ('abc' , 'AbC' , IGNORECASE )
85
88
check ('AbC' , 'AbC' )
86
89
87
90
def test_sep (self ):
88
- normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
89
91
check = self .check_match
90
92
check ('usr/bin' , 'usr/bin' )
91
- check ('usr\\ bin' , 'usr/bin' , normsep )
92
- check ('usr/bin' , 'usr\\ bin' , normsep )
93
+ check ('usr\\ bin' , 'usr/bin' , NORMSEP )
94
+ check ('usr/bin' , 'usr\\ bin' , NORMSEP )
93
95
check ('usr\\ bin' , 'usr\\ bin' )
94
96
95
97
def test_char_set (self ):
96
- ignorecase = os .path .normcase ('ABC' ) == os .path .normcase ('abc' )
97
98
check = self .check_match
98
99
tescases = string .ascii_lowercase + string .digits + string .punctuation
99
100
for c in tescases :
100
101
check (c , '[az]' , c in 'az' )
101
102
check (c , '[!az]' , c not in 'az' )
102
103
# Case insensitive.
103
104
for c in tescases :
104
- check (c , '[AZ]' , (c in 'az' ) and ignorecase )
105
- check (c , '[!AZ]' , (c not in 'az' ) or not ignorecase )
105
+ check (c , '[AZ]' , (c in 'az' ) and IGNORECASE )
106
+ check (c , '[!AZ]' , (c not in 'az' ) or not IGNORECASE )
106
107
for c in string .ascii_uppercase :
107
- check (c , '[az]' , (c in 'AZ' ) and ignorecase )
108
- check (c , '[!az]' , (c not in 'AZ' ) or not ignorecase )
108
+ check (c , '[az]' , (c in 'AZ' ) and IGNORECASE )
109
+ check (c , '[!az]' , (c not in 'AZ' ) or not IGNORECASE )
109
110
# Repeated same character.
110
111
for c in tescases :
111
112
check (c , '[aa]' , c == 'a' )
@@ -120,8 +121,6 @@ def test_char_set(self):
120
121
check ('[!]' , '[!]' )
121
122
122
123
def test_range (self ):
123
- ignorecase = os .path .normcase ('ABC' ) == os .path .normcase ('abc' )
124
- normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
125
124
check = self .check_match
126
125
tescases = string .ascii_lowercase + string .digits + string .punctuation
127
126
for c in tescases :
@@ -131,11 +130,11 @@ def test_range(self):
131
130
check (c , '[!b-dx-z]' , c not in 'bcdxyz' )
132
131
# Case insensitive.
133
132
for c in tescases :
134
- check (c , '[B-D]' , (c in 'bcd' ) and ignorecase )
135
- check (c , '[!B-D]' , (c not in 'bcd' ) or not ignorecase )
133
+ check (c , '[B-D]' , (c in 'bcd' ) and IGNORECASE )
134
+ check (c , '[!B-D]' , (c not in 'bcd' ) or not IGNORECASE )
136
135
for c in string .ascii_uppercase :
137
- check (c , '[b-d]' , (c in 'BCD' ) and ignorecase )
138
- check (c , '[!b-d]' , (c not in 'BCD' ) or not ignorecase )
136
+ check (c , '[b-d]' , (c in 'BCD' ) and IGNORECASE )
137
+ check (c , '[!b-d]' , (c not in 'BCD' ) or not IGNORECASE )
139
138
# Upper bound == lower bound.
140
139
for c in tescases :
141
140
check (c , '[b-b]' , c == 'b' )
@@ -144,7 +143,7 @@ def test_range(self):
144
143
check (c , '[!-#]' , c not in '-#' )
145
144
check (c , '[!--.]' , c not in '-.' )
146
145
check (c , '[^-`]' , c in '^_`' )
147
- if not (normsep and c == '/' ):
146
+ if not (NORMSEP and c == '/' ):
148
147
check (c , '[[-^]' , c in r'[\]^' )
149
148
check (c , r'[\-^]' , c in r'\]^' )
150
149
check (c , '[b-]' , c in '-b' )
@@ -160,47 +159,45 @@ def test_range(self):
160
159
check (c , '[d-bx-z]' , c in 'xyz' )
161
160
check (c , '[!d-bx-z]' , c not in 'xyz' )
162
161
check (c , '[d-b^-`]' , c in '^_`' )
163
- if not (normsep and c == '/' ):
162
+ if not (NORMSEP and c == '/' ):
164
163
check (c , '[d-b[-^]' , c in r'[\]^' )
165
164
166
165
def test_sep_in_char_set (self ):
167
- normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
168
166
check = self .check_match
169
167
check ('/' , r'[/]' )
170
168
check ('\\ ' , r'[\]' )
171
- check ('/' , r'[\]' , normsep )
172
- check ('\\ ' , r'[/]' , normsep )
169
+ check ('/' , r'[\]' , NORMSEP )
170
+ check ('\\ ' , r'[/]' , NORMSEP )
173
171
check ('[/]' , r'[/]' , False )
174
172
check (r'[\\]' , r'[/]' , False )
175
173
check ('\\ ' , r'[\t]' )
176
- check ('/' , r'[\t]' , normsep )
174
+ check ('/' , r'[\t]' , NORMSEP )
177
175
check ('t' , r'[\t]' )
178
176
check ('\t ' , r'[\t]' , False )
179
177
180
178
def test_sep_in_range (self ):
181
- normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
182
179
check = self .check_match
183
- check ('a/b' , 'a[.-0]b' , not normsep )
180
+ check ('a/b' , 'a[.-0]b' , not NORMSEP )
184
181
check ('a\\ b' , 'a[.-0]b' , False )
185
- check ('a\\ b' , 'a[Z-^]b' , not normsep )
182
+ check ('a\\ b' , 'a[Z-^]b' , not NORMSEP )
186
183
check ('a/b' , 'a[Z-^]b' , False )
187
184
188
- check ('a/b' , 'a[/-0]b' , not normsep )
185
+ check ('a/b' , 'a[/-0]b' , not NORMSEP )
189
186
check (r'a\b' , 'a[/-0]b' , False )
190
187
check ('a[/-0]b' , 'a[/-0]b' , False )
191
188
check (r'a[\-0]b' , 'a[/-0]b' , False )
192
189
193
190
check ('a/b' , 'a[.-/]b' )
194
- check (r'a\b' , 'a[.-/]b' , normsep )
191
+ check (r'a\b' , 'a[.-/]b' , NORMSEP )
195
192
check ('a[.-/]b' , 'a[.-/]b' , False )
196
193
check (r'a[.-\]b' , 'a[.-/]b' , False )
197
194
198
195
check (r'a\b' , r'a[\-^]b' )
199
- check ('a/b' , r'a[\-^]b' , normsep )
196
+ check ('a/b' , r'a[\-^]b' , NORMSEP )
200
197
check (r'a[\-^]b' , r'a[\-^]b' , False )
201
198
check ('a[/-^]b' , r'a[\-^]b' , False )
202
199
203
- check (r'a\b' , r'a[Z-\]b' , not normsep )
200
+ check (r'a\b' , r'a[Z-\]b' , not NORMSEP )
204
201
check ('a/b' , r'a[Z-\]b' , False )
205
202
check (r'a[Z-\]b' , r'a[Z-\]b' , False )
206
203
check ('a[Z-/]b' , r'a[Z-\]b' , False )
@@ -332,18 +329,41 @@ def test_mix_bytes_str(self):
332
329
self .assertRaises (TypeError , filter , [b'test' ], '*' )
333
330
334
331
def test_case (self ):
335
- ignorecase = os .path .normcase ('P' ) == os .path .normcase ('p' )
336
332
self .assertEqual (filter (['Test.py' , 'Test.rb' , 'Test.PL' ], '*.p*' ),
337
- ['Test.py' , 'Test.PL' ] if ignorecase else ['Test.py' ])
333
+ ['Test.py' , 'Test.PL' ] if IGNORECASE else ['Test.py' ])
338
334
self .assertEqual (filter (['Test.py' , 'Test.rb' , 'Test.PL' ], '*.P*' ),
339
- ['Test.py' , 'Test.PL' ] if ignorecase else ['Test.PL' ])
335
+ ['Test.py' , 'Test.PL' ] if IGNORECASE else ['Test.PL' ])
340
336
341
337
def test_sep (self ):
342
- normsep = os .path .normcase ('\\ ' ) == os .path .normcase ('/' )
343
338
self .assertEqual (filter (['usr/bin' , 'usr' , 'usr\\ lib' ], 'usr/*' ),
344
- ['usr/bin' , 'usr\\ lib' ] if normsep else ['usr/bin' ])
339
+ ['usr/bin' , 'usr\\ lib' ] if NORMSEP else ['usr/bin' ])
345
340
self .assertEqual (filter (['usr/bin' , 'usr' , 'usr\\ lib' ], 'usr\\ *' ),
346
- ['usr/bin' , 'usr\\ lib' ] if normsep else ['usr\\ lib' ])
341
+ ['usr/bin' , 'usr\\ lib' ] if NORMSEP else ['usr\\ lib' ])
342
+
343
+
344
+ class FilterFalseTestCase (unittest .TestCase ):
345
+
346
+ def test_filterfalse (self ):
347
+ actual = filterfalse (['Python' , 'Ruby' , 'Perl' , 'Tcl' ], 'P*' )
348
+ self .assertListEqual (actual , ['Ruby' , 'Tcl' ])
349
+ actual = filterfalse ([b'Python' , b'Ruby' , b'Perl' , b'Tcl' ], b'P*' )
350
+ self .assertListEqual (actual , [b'Ruby' , b'Tcl' ])
351
+
352
+ def test_mix_bytes_str (self ):
353
+ self .assertRaises (TypeError , filterfalse , ['test' ], b'*' )
354
+ self .assertRaises (TypeError , filterfalse , [b'test' ], '*' )
355
+
356
+ def test_case (self ):
357
+ self .assertEqual (filterfalse (['Test.py' , 'Test.rb' , 'Test.PL' ], '*.p*' ),
358
+ ['Test.rb' ] if IGNORECASE else ['Test.rb' , 'Test.PL' ])
359
+ self .assertEqual (filterfalse (['Test.py' , 'Test.rb' , 'Test.PL' ], '*.P*' ),
360
+ ['Test.rb' ] if IGNORECASE else ['Test.py' , 'Test.rb' ,])
361
+
362
+ def test_sep (self ):
363
+ self .assertEqual (filterfalse (['usr/bin' , 'usr' , 'usr\\ lib' ], 'usr/*' ),
364
+ ['usr' ] if NORMSEP else ['usr' , 'usr\\ lib' ])
365
+ self .assertEqual (filterfalse (['usr/bin' , 'usr' , 'usr\\ lib' ], 'usr\\ *' ),
366
+ ['usr' ] if NORMSEP else ['usr/bin' , 'usr' ])
347
367
348
368
349
369
if __name__ == "__main__" :
0 commit comments