blob: 81b9ce68389f262ea04f9ed22bfb12e703120777 [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 Murrayea340a32010-07-09 12:23:21 +00006from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb
Fred Drake91751142001-03-21 18:29:25 +00007
8
9class FnmatchTestCase(unittest.TestCase):
Georg Brandlc0e22b72010-03-14 10:51:01 +000010 def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
Fred Drake91751142001-03-21 18:29:25 +000011 if should_match:
Georg Brandlc0e22b72010-03-14 10:51:01 +000012 self.assertTrue(fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000013 "expected %r to match pattern %r"
14 % (filename, pattern))
15 else:
Georg Brandlc0e22b72010-03-14 10:51:01 +000016 self.assertTrue(not fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000017 "expected %r not to match pattern %r"
18 % (filename, pattern))
19
20 def test_fnmatch(self):
21 check = self.check_match
22 check('abc', 'abc')
23 check('abc', '?*?')
24 check('abc', '???*')
25 check('abc', '*???')
26 check('abc', '???')
27 check('abc', '*')
28 check('abc', 'ab[cd]')
29 check('abc', 'ab[!de]')
30 check('abc', 'ab[de]', 0)
31 check('a', '??', 0)
32 check('a', 'b', 0)
33
34 # these test that '\' is handled correctly in character sets;
Gregory P. Smith01099702009-08-16 18:58:46 +000035 # see SF bug #409651
Fred Drake91751142001-03-21 18:29:25 +000036 check('\\', r'[\]')
37 check('a', r'[!\]')
38 check('\\', r'[!\]', 0)
39
Gregory P. Smith01099702009-08-16 18:58:46 +000040 # test that filenames with newlines in them are handled correctly.
41 # http://bugs.python.org/issue6665
42 check('foo\nbar', 'foo*')
43 check('foo\nbar\n', 'foo*')
44 check('\nfoo', 'foo*', False)
45 check('\n', '*')
46
Guido van Rossumf0af3e32008-10-02 18:55:37 +000047 def test_mix_bytes_str(self):
48 self.assertRaises(TypeError, fnmatch, 'test', b'*')
49 self.assertRaises(TypeError, fnmatch, b'test', '*')
50 self.assertRaises(TypeError, fnmatchcase, 'test', b'*')
51 self.assertRaises(TypeError, fnmatchcase, b'test', '*')
52
Georg Brandl89fad142010-03-14 10:23:39 +000053 def test_fnmatchcase(self):
54 check = self.check_match
Georg Brandlc0e22b72010-03-14 10:51:01 +000055 check('AbC', 'abc', 0, fnmatchcase)
56 check('abc', 'AbC', 0, fnmatchcase)
Georg Brandl89fad142010-03-14 10:23:39 +000057
Guido van Rossumf0af3e32008-10-02 18:55:37 +000058 def test_bytes(self):
59 self.check_match(b'test', b'te*')
60 self.check_match(b'test\xff', b'te*\xff')
Gregory P. Smith01099702009-08-16 18:58:46 +000061 self.check_match(b'foo\nbar', b'foo*')
62
R. David Murrayea340a32010-07-09 12:23:21 +000063 def test_cache_clearing(self):
64 # check that caches do not grow too large
65 # http://bugs.python.org/issue7846
66
67 # string pattern cache
68 for i in range(_MAXCACHE + 1):
69 fnmatch('foo', '?' * i)
70
71 self.assertLessEqual(len(_cache), _MAXCACHE)
72
73 # bytes pattern cache
74 for i in range(_MAXCACHE + 1):
75 fnmatch(b'foo', b'?' * i)
76 self.assertLessEqual(len(_cacheb), _MAXCACHE)
77
Fred Drake91751142001-03-21 18:29:25 +000078
Fred Drake2e2be372001-09-20 21:33:42 +000079def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000080 support.run_unittest(FnmatchTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +000081
82
83if __name__ == "__main__":
84 test_main()