Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 1 | """Test cases for the fnmatch module.""" |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 4 | import unittest |
| 5 | |
Antoine Pitrou | 6fdb74f | 2010-08-13 16:26:40 +0000 | [diff] [blame] | 6 | from fnmatch import fnmatch, fnmatchcase, translate, filter |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 7 | |
| 8 | class FnmatchTestCase(unittest.TestCase): |
R. David Murray | 0425a8e | 2010-07-10 13:52:13 +0000 | [diff] [blame] | 9 | |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 10 | def check_match(self, filename, pattern, should_match=1, fn=fnmatch): |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 11 | if should_match: |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 12 | self.assertTrue(fn(filename, pattern), |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 13 | "expected %r to match pattern %r" |
| 14 | % (filename, pattern)) |
| 15 | else: |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 16 | self.assertTrue(not fn(filename, pattern), |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 17 | "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. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 35 | # see SF bug #409651 |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 36 | check('\\', r'[\]') |
| 37 | check('a', r'[!\]') |
| 38 | check('\\', r'[!\]', 0) |
| 39 | |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 40 | # 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 Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 47 | 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 Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 53 | def test_fnmatchcase(self): |
| 54 | check = self.check_match |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 55 | check('AbC', 'abc', 0, fnmatchcase) |
| 56 | check('abc', 'AbC', 0, fnmatchcase) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 58 | def test_bytes(self): |
| 59 | self.check_match(b'test', b'te*') |
| 60 | self.check_match(b'test\xff', b'te*\xff') |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 61 | self.check_match(b'foo\nbar', b'foo*') |
| 62 | |
Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 63 | class TranslateTestCase(unittest.TestCase): |
| 64 | |
| 65 | def test_translate(self): |
| 66 | self.assertEqual(translate('*'), '.*\Z(?ms)') |
| 67 | self.assertEqual(translate('?'), '.\Z(?ms)') |
| 68 | self.assertEqual(translate('a?b*'), 'a.b.*\Z(?ms)') |
| 69 | self.assertEqual(translate('[abc]'), '[abc]\Z(?ms)') |
| 70 | self.assertEqual(translate('[]]'), '[]]\Z(?ms)') |
| 71 | self.assertEqual(translate('[!x]'), '[^x]\Z(?ms)') |
| 72 | self.assertEqual(translate('[^x]'), '[\\^x]\Z(?ms)') |
| 73 | self.assertEqual(translate('[x'), '\\[x\Z(?ms)') |
| 74 | |
| 75 | |
| 76 | class FilterTestCase(unittest.TestCase): |
| 77 | |
| 78 | def test_filter(self): |
| 79 | self.assertEqual(filter(['a', 'b'], 'a'), ['a']) |
| 80 | |
| 81 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 82 | def test_main(): |
Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 83 | support.run_unittest(FnmatchTestCase, |
| 84 | TranslateTestCase, |
| 85 | FilterTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 86 | |
| 87 | |
| 88 | if __name__ == "__main__": |
| 89 | test_main() |