blob: abf881146199b134c2d1c0985efc408bcd94aebd [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
Fred Drake91751142001-03-21 18:29:25 +000014 def check_match(self, filename, pattern, should_match=1):
15 if should_match:
Georg Brandlab91fde2009-08-13 08:51:18 +000016 self.assertTrue(fnmatch(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000017 "expected %r to match pattern %r"
18 % (filename, pattern))
19 else:
Georg Brandlab91fde2009-08-13 08:51:18 +000020 self.assertTrue(not fnmatch(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
57 def test_bytes(self):
58 self.check_match(b'test', b'te*')
59 self.check_match(b'test\xff', b'te*\xff')
Gregory P. Smith6c4a7252009-11-01 20:36:24 +000060 self.check_match(b'foo\nbar', b'foo*')
61
R. David Murrayead883a2010-07-09 13:16:26 +000062 def test_cache_clearing(self):
63 # check that caches do not grow too large
64 # http://bugs.python.org/issue7846
65
66 # string pattern cache
67 for i in range(_MAXCACHE + 1):
68 fnmatch('foo', '?' * i)
69
70 self.assertLessEqual(len(_cache), _MAXCACHE)
71
72 # bytes pattern cache
73 for i in range(_MAXCACHE + 1):
74 fnmatch(b'foo', b'?' * i)
75 self.assertLessEqual(len(_cacheb), _MAXCACHE)
76
Fred Drake91751142001-03-21 18:29:25 +000077
Fred Drake2e2be372001-09-20 21:33:42 +000078def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000079 support.run_unittest(FnmatchTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +000080
81
82if __name__ == "__main__":
83 test_main()