Skip to content

Commit 2b67c7a

Browse files
[2.7] bpo-30415: Add new tests for the fnmatch module. (GH-1684). (#1696)
(cherry picked from commit 8175547)
1 parent cef8b17 commit 2b67c7a

File tree

1 file changed

+104
-11
lines changed

1 file changed

+104
-11
lines changed

Lib/test/test_fnmatch.py

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
from test import test_support
44
import unittest
5+
import os
56

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)
89

910

1011
class FnmatchTestCase(unittest.TestCase):
1112

1213
def tearDown(self):
1314
_purge()
1415

15-
def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
16+
def check_match(self, filename, pattern, should_match=True, fn=fnmatch):
1617
if should_match:
1718
self.assertTrue(fn(filename, pattern),
1819
"expected %r to match pattern %r"
1920
% (filename, pattern))
2021
else:
21-
self.assertTrue(not fn(filename, pattern),
22+
self.assertFalse(fn(filename, pattern),
2223
"expected %r not to match pattern %r"
2324
% (filename, pattern))
2425

@@ -32,15 +33,15 @@ def test_fnmatch(self):
3233
check('abc', '*')
3334
check('abc', 'ab[cd]')
3435
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)
3839

3940
# these test that '\' is handled correctly in character sets;
4041
# see SF bug #409651
4142
check('\\', r'[\]')
4243
check('a', r'[!\]')
43-
check('\\', r'[!\]', 0)
44+
check('\\', r'[!\]', False)
4445

4546
# test that filenames with newlines in them are handled correctly.
4647
# http://bugs.python.org/issue6665
@@ -49,10 +50,29 @@ def test_fnmatch(self):
4950
check('\nfoo', 'foo*', False)
5051
check('\n', '*')
5152

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+
5265
def test_fnmatchcase(self):
5366
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)
5676

5777
def test_cache_clearing(self):
5878
# check that caches do not grow too large
@@ -64,8 +84,81 @@ def test_cache_clearing(self):
6484

6585
self.assertLessEqual(len(_cache), _MAXCACHE)
6686

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\nbar', 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+
67160
def test_main():
68-
test_support.run_unittest(FnmatchTestCase)
161+
test_support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase)
69162

70163

71164
if __name__ == "__main__":

0 commit comments

Comments
 (0)