| Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 1 | """Test cases for the fnmatch module.""" | 
 | 2 |  | 
| Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 3 | import unittest | 
 | 4 |  | 
| Antoine Pitrou | 6fdb74f | 2010-08-13 16:26:40 +0000 | [diff] [blame] | 5 | from fnmatch import fnmatch, fnmatchcase, translate, filter | 
| Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 6 |  | 
 | 7 | class FnmatchTestCase(unittest.TestCase): | 
| R. David Murray | 0425a8e | 2010-07-10 13:52:13 +0000 | [diff] [blame] | 8 |  | 
| Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 9 |     def check_match(self, filename, pattern, should_match=1, fn=fnmatch): | 
| Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 10 |         if should_match: | 
| Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 11 |             self.assertTrue(fn(filename, pattern), | 
| Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 12 |                          "expected %r to match pattern %r" | 
 | 13 |                          % (filename, pattern)) | 
 | 14 |         else: | 
| Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 15 |             self.assertTrue(not fn(filename, pattern), | 
| Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 16 |                          "expected %r not to match pattern %r" | 
 | 17 |                          % (filename, pattern)) | 
 | 18 |  | 
 | 19 |     def test_fnmatch(self): | 
 | 20 |         check = self.check_match | 
 | 21 |         check('abc', 'abc') | 
 | 22 |         check('abc', '?*?') | 
 | 23 |         check('abc', '???*') | 
 | 24 |         check('abc', '*???') | 
 | 25 |         check('abc', '???') | 
 | 26 |         check('abc', '*') | 
 | 27 |         check('abc', 'ab[cd]') | 
 | 28 |         check('abc', 'ab[!de]') | 
 | 29 |         check('abc', 'ab[de]', 0) | 
 | 30 |         check('a', '??', 0) | 
 | 31 |         check('a', 'b', 0) | 
 | 32 |  | 
 | 33 |         # these test that '\' is handled correctly in character sets; | 
| Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 34 |         # see SF bug #409651 | 
| Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 35 |         check('\\', r'[\]') | 
 | 36 |         check('a', r'[!\]') | 
 | 37 |         check('\\', r'[!\]', 0) | 
 | 38 |  | 
| Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 39 |         # test that filenames with newlines in them are handled correctly. | 
 | 40 |         # http://bugs.python.org/issue6665 | 
 | 41 |         check('foo\nbar', 'foo*') | 
 | 42 |         check('foo\nbar\n', 'foo*') | 
 | 43 |         check('\nfoo', 'foo*', False) | 
 | 44 |         check('\n', '*') | 
 | 45 |  | 
| Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 46 |     def test_mix_bytes_str(self): | 
 | 47 |         self.assertRaises(TypeError, fnmatch, 'test', b'*') | 
 | 48 |         self.assertRaises(TypeError, fnmatch, b'test', '*') | 
 | 49 |         self.assertRaises(TypeError, fnmatchcase, 'test', b'*') | 
 | 50 |         self.assertRaises(TypeError, fnmatchcase, b'test', '*') | 
 | 51 |  | 
| Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 52 |     def test_fnmatchcase(self): | 
 | 53 |         check = self.check_match | 
| Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 54 |         check('AbC', 'abc', 0, fnmatchcase) | 
 | 55 |         check('abc', 'AbC', 0, fnmatchcase) | 
| Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 56 |  | 
| Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 57 |     def test_bytes(self): | 
 | 58 |         self.check_match(b'test', b'te*') | 
 | 59 |         self.check_match(b'test\xff', b'te*\xff') | 
| Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 60 |         self.check_match(b'foo\nbar', b'foo*') | 
 | 61 |  | 
| Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 62 | class TranslateTestCase(unittest.TestCase): | 
 | 63 |  | 
 | 64 |     def test_translate(self): | 
 | 65 |         self.assertEqual(translate('*'), '.*\Z(?ms)') | 
 | 66 |         self.assertEqual(translate('?'), '.\Z(?ms)') | 
 | 67 |         self.assertEqual(translate('a?b*'), 'a.b.*\Z(?ms)') | 
 | 68 |         self.assertEqual(translate('[abc]'), '[abc]\Z(?ms)') | 
 | 69 |         self.assertEqual(translate('[]]'), '[]]\Z(?ms)') | 
 | 70 |         self.assertEqual(translate('[!x]'), '[^x]\Z(?ms)') | 
 | 71 |         self.assertEqual(translate('[^x]'), '[\\^x]\Z(?ms)') | 
 | 72 |         self.assertEqual(translate('[x'), '\\[x\Z(?ms)') | 
 | 73 |  | 
 | 74 |  | 
 | 75 | class FilterTestCase(unittest.TestCase): | 
 | 76 |  | 
 | 77 |     def test_filter(self): | 
 | 78 |         self.assertEqual(filter(['a', 'b'], 'a'), ['a']) | 
 | 79 |  | 
 | 80 |  | 
| Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 81 | if __name__ == "__main__": | 
| Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 82 |     unittest.main() |