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 | |
Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 6 | from fnmatch import (fnmatch, fnmatchcase, _MAXCACHE, _cache, _cacheb, purge, |
| 7 | translate, filter) |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class FnmatchTestCase(unittest.TestCase): |
R. David Murray | 0425a8e | 2010-07-10 13:52:13 +0000 | [diff] [blame] | 11 | |
| 12 | def tearDown(self): |
| 13 | purge() |
| 14 | |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 15 | def check_match(self, filename, pattern, should_match=1, fn=fnmatch): |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 16 | if should_match: |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 17 | self.assertTrue(fn(filename, pattern), |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 18 | "expected %r to match pattern %r" |
| 19 | % (filename, pattern)) |
| 20 | else: |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 21 | self.assertTrue(not fn(filename, pattern), |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 22 | "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. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 40 | # see SF bug #409651 |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 41 | check('\\', r'[\]') |
| 42 | check('a', r'[!\]') |
| 43 | check('\\', r'[!\]', 0) |
| 44 | |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 45 | # 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 Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 52 | 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 Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 58 | def test_fnmatchcase(self): |
| 59 | check = self.check_match |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 60 | check('AbC', 'abc', 0, fnmatchcase) |
| 61 | check('abc', 'AbC', 0, fnmatchcase) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 63 | def test_bytes(self): |
| 64 | self.check_match(b'test', b'te*') |
| 65 | self.check_match(b'test\xff', b'te*\xff') |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 66 | self.check_match(b'foo\nbar', b'foo*') |
| 67 | |
R. David Murray | ea340a3 | 2010-07-09 12:23:21 +0000 | [diff] [blame] | 68 | 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 Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 83 | |
Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 84 | class 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 | |
| 97 | class FilterTestCase(unittest.TestCase): |
| 98 | |
| 99 | def test_filter(self): |
| 100 | self.assertEqual(filter(['a', 'b'], 'a'), ['a']) |
| 101 | |
| 102 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 103 | def test_main(): |
Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 104 | support.run_unittest(FnmatchTestCase, |
| 105 | TranslateTestCase, |
| 106 | FilterTestCase) |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | if __name__ == "__main__": |
| 110 | test_main() |