Home | History | Annotate | Download | only in test
      1 """Test cases for the fnmatch module."""
      2 
      3 import unittest
      4 import os
      5 import warnings
      6 
      7 from fnmatch import fnmatch, fnmatchcase, translate, filter
      8 
      9 class FnmatchTestCase(unittest.TestCase):
     10 
     11     def check_match(self, filename, pattern, should_match=True, fn=fnmatch):
     12         if should_match:
     13             self.assertTrue(fn(filename, pattern),
     14                          "expected %r to match pattern %r"
     15                          % (filename, pattern))
     16         else:
     17             self.assertFalse(fn(filename, pattern),
     18                          "expected %r not to match pattern %r"
     19                          % (filename, pattern))
     20 
     21     def test_fnmatch(self):
     22         check = self.check_match
     23         check('abc', 'abc')
     24         check('abc', '?*?')
     25         check('abc', '???*')
     26         check('abc', '*???')
     27         check('abc', '???')
     28         check('abc', '*')
     29         check('abc', 'ab[cd]')
     30         check('abc', 'ab[!de]')
     31         check('abc', 'ab[de]', False)
     32         check('a', '??', False)
     33         check('a', 'b', False)
     34 
     35         # these test that '\' is handled correctly in character sets;
     36         # see SF bug #409651
     37         check('\\', r'[\]')
     38         check('a', r'[!\]')
     39         check('\\', r'[!\]', False)
     40 
     41         # test that filenames with newlines in them are handled correctly.
     42         # http://bugs.python.org/issue6665
     43         check('foo\nbar', 'foo*')
     44         check('foo\nbar\n', 'foo*')
     45         check('\nfoo', 'foo*', False)
     46         check('\n', '*')
     47 
     48     def test_mix_bytes_str(self):
     49         self.assertRaises(TypeError, fnmatch, 'test', b'*')
     50         self.assertRaises(TypeError, fnmatch, b'test', '*')
     51         self.assertRaises(TypeError, fnmatchcase, 'test', b'*')
     52         self.assertRaises(TypeError, fnmatchcase, b'test', '*')
     53 
     54     def test_fnmatchcase(self):
     55         check = self.check_match
     56         check('abc', 'abc', True, fnmatchcase)
     57         check('AbC', 'abc', False, fnmatchcase)
     58         check('abc', 'AbC', False, fnmatchcase)
     59         check('AbC', 'AbC', True, fnmatchcase)
     60 
     61         check('usr/bin', 'usr/bin', True, fnmatchcase)
     62         check('usr\\bin', 'usr/bin', False, fnmatchcase)
     63         check('usr/bin', 'usr\\bin', False, fnmatchcase)
     64         check('usr\\bin', 'usr\\bin', True, fnmatchcase)
     65 
     66     def test_bytes(self):
     67         self.check_match(b'test', b'te*')
     68         self.check_match(b'test\xff', b'te*\xff')
     69         self.check_match(b'foo\nbar', b'foo*')
     70 
     71     def test_case(self):
     72         ignorecase = os.path.normcase('ABC') == os.path.normcase('abc')
     73         check = self.check_match
     74         check('abc', 'abc')
     75         check('AbC', 'abc', ignorecase)
     76         check('abc', 'AbC', ignorecase)
     77         check('AbC', 'AbC')
     78 
     79     def test_sep(self):
     80         normsep = os.path.normcase('\\') == os.path.normcase('/')
     81         check = self.check_match
     82         check('usr/bin', 'usr/bin')
     83         check('usr\\bin', 'usr/bin', normsep)
     84         check('usr/bin', 'usr\\bin', normsep)
     85         check('usr\\bin', 'usr\\bin')
     86 
     87     def test_warnings(self):
     88         with warnings.catch_warnings():
     89             warnings.simplefilter('error', Warning)
     90             check = self.check_match
     91             check('[', '[[]')
     92             check('&', '[a&&b]')
     93             check('|', '[a||b]')
     94             check('~', '[a~~b]')
     95             check(',', '[a-z+--A-Z]')
     96             check('.', '[a-z--/A-Z]')
     97 
     98 
     99 class TranslateTestCase(unittest.TestCase):
    100 
    101     def test_translate(self):
    102         self.assertEqual(translate('*'), r'(?s:.*)\Z')
    103         self.assertEqual(translate('?'), r'(?s:.)\Z')
    104         self.assertEqual(translate('a?b*'), r'(?s:a.b.*)\Z')
    105         self.assertEqual(translate('[abc]'), r'(?s:[abc])\Z')
    106         self.assertEqual(translate('[]]'), r'(?s:[]])\Z')
    107         self.assertEqual(translate('[!x]'), r'(?s:[^x])\Z')
    108         self.assertEqual(translate('[^x]'), r'(?s:[\^x])\Z')
    109         self.assertEqual(translate('[x'), r'(?s:\[x)\Z')
    110 
    111 
    112 class FilterTestCase(unittest.TestCase):
    113 
    114     def test_filter(self):
    115         self.assertEqual(filter(['Python', 'Ruby', 'Perl', 'Tcl'], 'P*'),
    116                          ['Python', 'Perl'])
    117         self.assertEqual(filter([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*'),
    118                          [b'Python', b'Perl'])
    119 
    120     def test_mix_bytes_str(self):
    121         self.assertRaises(TypeError, filter, ['test'], b'*')
    122         self.assertRaises(TypeError, filter, [b'test'], '*')
    123 
    124     def test_case(self):
    125         ignorecase = os.path.normcase('P') == os.path.normcase('p')
    126         self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'),
    127                          ['Test.py', 'Test.PL'] if ignorecase else ['Test.py'])
    128         self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.P*'),
    129                          ['Test.py', 'Test.PL'] if ignorecase else ['Test.PL'])
    130 
    131     def test_sep(self):
    132         normsep = os.path.normcase('\\') == os.path.normcase('/')
    133         self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr/*'),
    134                          ['usr/bin', 'usr\\lib'] if normsep else ['usr/bin'])
    135         self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr\\*'),
    136                          ['usr/bin', 'usr\\lib'] if normsep else ['usr\\lib'])
    137 
    138 
    139 if __name__ == "__main__":
    140     unittest.main()
    141