blob: a93558c7dd378dc2649423ecdac1faa60a663394 [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
Brett Cannon4b16e132010-07-23 16:23:13 +00006from fnmatch import (fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb, purge,
7 translate, filter)
Fred Drake91751142001-03-21 18:29:25 +00008
9
10class FnmatchTestCase(unittest.TestCase):
R. David Murray0425a8e2010-07-10 13:52:13 +000011
12 def tearDown(self):
13 purge()
14
Georg Brandlc0e22b72010-03-14 10:51:01 +000015 def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
Fred Drake91751142001-03-21 18:29:25 +000016 if should_match:
Georg Brandlc0e22b72010-03-14 10:51:01 +000017 self.assertTrue(fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000018 "expected %r to match pattern %r"
19 % (filename, pattern))
20 else:
Georg Brandlc0e22b72010-03-14 10:51:01 +000021 self.assertTrue(not fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000022 "expected %r not to match pattern %r"
23 % (filename, pattern))
24
25 def test_fnmatch(self):
26 check = self.check_match
27 check('abc', 'abc')
28 check('abc', '?*?')
29 check('abc', '???*')
30 check('abc', '*???')
31 check('abc', '???')
32 check('abc', '*')
33 check('abc', 'ab[cd]')
34 check('abc', 'ab[!de]')
35 check('abc', 'ab[de]', 0)
36 check('a', '??', 0)
37 check('a', 'b', 0)
38
39 # these test that '\' is handled correctly in character sets;
Gregory P. Smith01099702009-08-16 18:58:46 +000040 # see SF bug #409651
Fred Drake91751142001-03-21 18:29:25 +000041 check('\\', r'[\]')
42 check('a', r'[!\]')
43 check('\\', r'[!\]', 0)
44
Gregory P. Smith01099702009-08-16 18:58:46 +000045 # test that filenames with newlines in them are handled correctly.
46 # http://bugs.python.org/issue6665
47 check('foo\nbar', 'foo*')
48 check('foo\nbar\n', 'foo*')
49 check('\nfoo', 'foo*', False)
50 check('\n', '*')
51
Guido van Rossumf0af3e32008-10-02 18:55:37 +000052 def test_mix_bytes_str(self):
53 self.assertRaises(TypeError, fnmatch, 'test', b'*')
54 self.assertRaises(TypeError, fnmatch, b'test', '*')
55 self.assertRaises(TypeError, fnmatchcase, 'test', b'*')
56 self.assertRaises(TypeError, fnmatchcase, b'test', '*')
57
Georg Brandl89fad142010-03-14 10:23:39 +000058 def test_fnmatchcase(self):
59 check = self.check_match
Georg Brandlc0e22b72010-03-14 10:51:01 +000060 check('AbC', 'abc', 0, fnmatchcase)
61 check('abc', 'AbC', 0, fnmatchcase)
Georg Brandl89fad142010-03-14 10:23:39 +000062
Guido van Rossumf0af3e32008-10-02 18:55:37 +000063 def test_bytes(self):
64 self.check_match(b'test', b'te*')
65 self.check_match(b'test\xff', b'te*\xff')
Gregory P. Smith01099702009-08-16 18:58:46 +000066 self.check_match(b'foo\nbar', b'foo*')
67
R. David Murrayea340a32010-07-09 12:23:21 +000068 def test_cache_clearing(self):
69 # check that caches do not grow too large
70 # http://bugs.python.org/issue7846
71
72 # string pattern cache
73 for i in range(_MAXCACHE + 1):
74 fnmatch('foo', '?' * i)
75
76 self.assertLessEqual(len(_cache), _MAXCACHE)
77
78 # bytes pattern cache
79 for i in range(_MAXCACHE + 1):
80 fnmatch(b'foo', b'?' * i)
81 self.assertLessEqual(len(_cacheb), _MAXCACHE)
82
Fred Drake91751142001-03-21 18:29:25 +000083
Brett Cannon4b16e132010-07-23 16:23:13 +000084class TranslateTestCase(unittest.TestCase):
85
86 def test_translate(self):
87 self.assertEqual(translate('*'), '.*\Z(?ms)')
88 self.assertEqual(translate('?'), '.\Z(?ms)')
89 self.assertEqual(translate('a?b*'), 'a.b.*\Z(?ms)')
90 self.assertEqual(translate('[abc]'), '[abc]\Z(?ms)')
91 self.assertEqual(translate('[]]'), '[]]\Z(?ms)')
92 self.assertEqual(translate('[!x]'), '[^x]\Z(?ms)')
93 self.assertEqual(translate('[^x]'), '[\\^x]\Z(?ms)')
94 self.assertEqual(translate('[x'), '\\[x\Z(?ms)')
95
96
97class FilterTestCase(unittest.TestCase):
98
99 def test_filter(self):
100 self.assertEqual(filter(['a', 'b'], 'a'), ['a'])
101
102
Fred Drake2e2be372001-09-20 21:33:42 +0000103def test_main():
Brett Cannon4b16e132010-07-23 16:23:13 +0000104 support.run_unittest(FnmatchTestCase,
105 TranslateTestCase,
106 FilterTestCase)
Fred Drake2e2be372001-09-20 21:33:42 +0000107
108
109if __name__ == "__main__":
110 test_main()