Home | History | Annotate | Download | only in test
      1 """Test cases for the fnmatch module."""
      2 
      3 from test import test_support
      4 import unittest
      5 
      6 from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache
      7 from fnmatch import fnmatch, fnmatchcase, _MAXCACHE, _cache, _purge
      8 
      9 
     10 class FnmatchTestCase(unittest.TestCase):
     11 
     12     def tearDown(self):
     13         _purge()
     14 
     15     def check_match(self, filename, pattern, should_match=1, fn=fnmatch):
     16         if should_match:
     17             self.assertTrue(fn(filename, pattern),
     18                          "expected %r to match pattern %r"
     19                          % (filename, pattern))
     20         else:
     21             self.assertTrue(not fn(filename, pattern),
     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;
     40         # see SF bug #409651
     41         check('\\', r'[\]')
     42         check('a', r'[!\]')
     43         check('\\', r'[!\]', 0)
     44 
     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 
     52     def test_fnmatchcase(self):
     53         check = self.check_match
     54         check('AbC', 'abc', 0, fnmatchcase)
     55         check('abc', 'AbC', 0, fnmatchcase)
     56 
     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)
     66 
     67 def test_main():
     68     test_support.run_unittest(FnmatchTestCase)
     69 
     70 
     71 if __name__ == "__main__":
     72     test_main()
     73