blob: fbcbe70b82847c999a6ce8838d2d5cf42376b59e [file] [log] [blame]
Fred Drake91751142001-03-21 18:29:25 +00001"""Test cases for the fnmatch module."""
2
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test import test_support
Fred Drake91751142001-03-21 18:29:25 +00004import unittest
5
6from fnmatch import fnmatch, fnmatchcase
7
8
9class FnmatchTestCase(unittest.TestCase):
10 def check_match(self, filename, pattern, should_match=1):
11 if should_match:
Benjamin Peterson5c8da862009-06-30 22:57:08 +000012 self.assertTrue(fnmatch(filename, pattern),
Fred Drake91751142001-03-21 18:29:25 +000013 "expected %r to match pattern %r"
14 % (filename, pattern))
15 else:
Benjamin Peterson5c8da862009-06-30 22:57:08 +000016 self.assertTrue(not fnmatch(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. Smithb98d6b22009-08-16 18:52:58 +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. Smithb98d6b22009-08-16 18:52:58 +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
Fred Drake91751142001-03-21 18:29:25 +000047
Fred Drake2e2be372001-09-20 21:33:42 +000048def test_main():
49 test_support.run_unittest(FnmatchTestCase)
50
51
52if __name__ == "__main__":
53 test_main()