blob: fa37f90c27e9a591f942fc4a3ed090f918a35dd6 [file] [log] [blame]
Fred Drake91751142001-03-21 18:29:25 +00001"""Test cases for the fnmatch module."""
2
Fred Drake91751142001-03-21 18:29:25 +00003import unittest
4
Antoine Pitrou6fdb74f2010-08-13 16:26:40 +00005from fnmatch import fnmatch, fnmatchcase, translate, filter
Fred Drake91751142001-03-21 18:29:25 +00006
7class FnmatchTestCase(unittest.TestCase):
R. David Murray0425a8e2010-07-10 13:52:13 +00008
Georg Brandlc0e22b72010-03-14 10:51:01 +00009 def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
Fred Drake91751142001-03-21 18:29:25 +000010 if should_match:
Georg Brandlc0e22b72010-03-14 10:51:01 +000011 self.assertTrue(fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000012 "expected %r to match pattern %r"
13 % (filename, pattern))
14 else:
Georg Brandlc0e22b72010-03-14 10:51:01 +000015 self.assertTrue(not fn(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000016 "expected %r not to match pattern %r"
17 % (filename, pattern))
18
19 def test_fnmatch(self):
20 check = self.check_match
21 check('abc', 'abc')
22 check('abc', '?*?')
23 check('abc', '???*')
24 check('abc', '*???')
25 check('abc', '???')
26 check('abc', '*')
27 check('abc', 'ab[cd]')
28 check('abc', 'ab[!de]')
29 check('abc', 'ab[de]', 0)
30 check('a', '??', 0)
31 check('a', 'b', 0)
32
33 # these test that '\' is handled correctly in character sets;
Gregory P. Smith01099702009-08-16 18:58:46 +000034 # see SF bug #409651
Fred Drake91751142001-03-21 18:29:25 +000035 check('\\', r'[\]')
36 check('a', r'[!\]')
37 check('\\', r'[!\]', 0)
38
Gregory P. Smith01099702009-08-16 18:58:46 +000039 # test that filenames with newlines in them are handled correctly.
40 # http://bugs.python.org/issue6665
41 check('foo\nbar', 'foo*')
42 check('foo\nbar\n', 'foo*')
43 check('\nfoo', 'foo*', False)
44 check('\n', '*')
45
Guido van Rossumf0af3e32008-10-02 18:55:37 +000046 def test_mix_bytes_str(self):
47 self.assertRaises(TypeError, fnmatch, 'test', b'*')
48 self.assertRaises(TypeError, fnmatch, b'test', '*')
49 self.assertRaises(TypeError, fnmatchcase, 'test', b'*')
50 self.assertRaises(TypeError, fnmatchcase, b'test', '*')
51
Georg Brandl89fad142010-03-14 10:23:39 +000052 def test_fnmatchcase(self):
53 check = self.check_match
Georg Brandlc0e22b72010-03-14 10:51:01 +000054 check('AbC', 'abc', 0, fnmatchcase)
55 check('abc', 'AbC', 0, fnmatchcase)
Georg Brandl89fad142010-03-14 10:23:39 +000056
Guido van Rossumf0af3e32008-10-02 18:55:37 +000057 def test_bytes(self):
58 self.check_match(b'test', b'te*')
59 self.check_match(b'test\xff', b'te*\xff')
Gregory P. Smith01099702009-08-16 18:58:46 +000060 self.check_match(b'foo\nbar', b'foo*')
61
Brett Cannon4b16e132010-07-23 16:23:13 +000062class TranslateTestCase(unittest.TestCase):
63
64 def test_translate(self):
65 self.assertEqual(translate('*'), '.*\Z(?ms)')
66 self.assertEqual(translate('?'), '.\Z(?ms)')
67 self.assertEqual(translate('a?b*'), 'a.b.*\Z(?ms)')
68 self.assertEqual(translate('[abc]'), '[abc]\Z(?ms)')
69 self.assertEqual(translate('[]]'), '[]]\Z(?ms)')
70 self.assertEqual(translate('[!x]'), '[^x]\Z(?ms)')
71 self.assertEqual(translate('[^x]'), '[\\^x]\Z(?ms)')
72 self.assertEqual(translate('[x'), '\\[x\Z(?ms)')
73
74
75class FilterTestCase(unittest.TestCase):
76
77 def test_filter(self):
78 self.assertEqual(filter(['a', 'b'], 'a'), ['a'])
79
80
Fred Drake2e2be372001-09-20 21:33:42 +000081if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050082 unittest.main()