blob: bde4ee809795a575526b44b8cc59a00afaf4b2c9 [file] [log] [blame]
Fred Drake91751142001-03-21 18:29:25 +00001"""Test cases for the fnmatch module."""
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Fred Drake91751142001-03-21 18:29:25 +00004import unittest
5
R. David Murrayca126a02010-07-10 14:00:10 +00006from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb, _purge
Fred Drake91751142001-03-21 18:29:25 +00007
8
9class FnmatchTestCase(unittest.TestCase):
R. David Murrayca126a02010-07-10 14:00:10 +000010
11 def tearDown(self):
12 _purge()
13
Georg Brandld6abb722010-10-06 07:55:35 +000014 def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
Fred Drake91751142001-03-21 18:29:25 +000015 if should_match:
Georg Brandld6abb722010-10-06 07:55:35 +000016 self.assertTrue(fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000017 "expected %r to match pattern %r"
18 % (filename, pattern))
19 else:
Georg Brandld6abb722010-10-06 07:55:35 +000020 self.assertTrue(not fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000021 "expected %r not to match pattern %r"
22 % (filename, pattern))
23
24 def test_fnmatch(self):
25 check = self.check_match
26 check('abc', 'abc')
27 check('abc', '?*?')
28 check('abc', '???*')
29 check('abc', '*???')
30 check('abc', '???')
31 check('abc', '*')
32 check('abc', 'ab[cd]')
33 check('abc', 'ab[!de]')
34 check('abc', 'ab[de]', 0)
35 check('a', '??', 0)
36 check('a', 'b', 0)
37
38 # these test that '\' is handled correctly in character sets;
Gregory P. Smith6c4a7252009-11-01 20:36:24 +000039 # see SF bug #409651
Fred Drake91751142001-03-21 18:29:25 +000040 check('\\', r'[\]')
41 check('a', r'[!\]')
42 check('\\', r'[!\]', 0)
43
Gregory P. Smith6c4a7252009-11-01 20:36:24 +000044 # test that filenames with newlines in them are handled correctly.
45 # http://bugs.python.org/issue6665
46 check('foo\nbar', 'foo*')
47 check('foo\nbar\n', 'foo*')
48 check('\nfoo', 'foo*', False)
49 check('\n', '*')
50
Guido van Rossumf0af3e32008-10-02 18:55:37 +000051 def test_mix_bytes_str(self):
52 self.assertRaises(TypeError, fnmatch, 'test', b'*')
53 self.assertRaises(TypeError, fnmatch, b'test', '*')
54 self.assertRaises(TypeError, fnmatchcase, 'test', b'*')
55 self.assertRaises(TypeError, fnmatchcase, b'test', '*')
56
Georg Brandld6abb722010-10-06 07:55:35 +000057 def test_fnmatchcase(self):
58 check = self.check_match
59 check('AbC', 'abc', 0, fnmatchcase)
60 check('abc', 'AbC', 0, fnmatchcase)
61
Guido van Rossumf0af3e32008-10-02 18:55:37 +000062 def test_bytes(self):
63 self.check_match(b'test', b'te*')
64 self.check_match(b'test\xff', b'te*\xff')
Gregory P. Smith6c4a7252009-11-01 20:36:24 +000065 self.check_match(b'foo\nbar', b'foo*')
66
R. David Murrayead883a2010-07-09 13:16:26 +000067 def test_cache_clearing(self):
68 # check that caches do not grow too large
69 # http://bugs.python.org/issue7846
70
71 # string pattern cache
72 for i in range(_MAXCACHE + 1):
73 fnmatch('foo', '?' * i)
74
75 self.assertLessEqual(len(_cache), _MAXCACHE)
76
77 # bytes pattern cache
78 for i in range(_MAXCACHE + 1):
79 fnmatch(b'foo', b'?' * i)
80 self.assertLessEqual(len(_cacheb), _MAXCACHE)
81
Fred Drake91751142001-03-21 18:29:25 +000082
Fred Drake2e2be372001-09-20 21:33:42 +000083def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000084 support.run_unittest(FnmatchTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +000085
86
87if __name__ == "__main__":
88 test_main()