Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 1 | """Test cases for the fnmatch module.""" |
| 2 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 3 | from test import test_support |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 4 | import unittest |
| 5 | |
R. David Murray | abd4553 | 2010-07-09 13:16:00 +0000 | [diff] [blame] | 6 | from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache |
R. David Murray | 2ab02f0 | 2010-07-10 14:06:51 +0000 | [diff] [blame] | 7 | from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _purge |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class FnmatchTestCase(unittest.TestCase): |
R. David Murray | 2ab02f0 | 2010-07-10 14:06:51 +0000 | [diff] [blame] | 11 | |
| 12 | def tearDown(self): |
| 13 | _purge() |
| 14 | |
Georg Brandl | 6eedef6 | 2010-02-08 00:04:54 +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 | 6eedef6 | 2010-02-08 00:04:54 +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 | 6eedef6 | 2010-02-08 00:04:54 +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 | b98d6b2 | 2009-08-16 18:52:58 +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 | b98d6b2 | 2009-08-16 18:52:58 +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 | |
Georg Brandl | 308e18b | 2010-02-07 12:34:26 +0000 | [diff] [blame] | 52 | def test_fnmatchcase(self): |
| 53 | check = self.check_match |
Georg Brandl | 6eedef6 | 2010-02-08 00:04:54 +0000 | [diff] [blame] | 54 | check('AbC', 'abc', 0, fnmatchcase) |
| 55 | check('abc', 'AbC', 0, fnmatchcase) |
Georg Brandl | 308e18b | 2010-02-07 12:34:26 +0000 | [diff] [blame] | 56 | |
R. David Murray | abd4553 | 2010-07-09 13:16:00 +0000 | [diff] [blame] | 57 | def test_cache_clearing(self): |
| 58 | # check that caches do not grow too large |
| 59 | # http://bugs.python.org/issue7846 |
| 60 | |
| 61 | # string pattern cache |
| 62 | for i in range(_MAXCACHE + 1): |
| 63 | fnmatch('foo', '?' * i) |
| 64 | |
| 65 | self.assertLessEqual(len(_cache), _MAXCACHE) |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 66 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 67 | def test_main(): |
| 68 | test_support.run_unittest(FnmatchTestCase) |
| 69 | |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | test_main() |