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 |
Serhiy Storchaka | 8175547 | 2017-05-21 08:57:00 +0300 | [diff] [blame] | 4 | import os |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 5 | |
Antoine Pitrou | 6fdb74f | 2010-08-13 16:26:40 +0000 | [diff] [blame] | 6 | from fnmatch import fnmatch, fnmatchcase, translate, filter |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 7 | |
| 8 | class FnmatchTestCase(unittest.TestCase): |
R. David Murray | 0425a8e | 2010-07-10 13:52:13 +0000 | [diff] [blame] | 9 | |
Serhiy Storchaka | 8175547 | 2017-05-21 08:57:00 +0300 | [diff] [blame] | 10 | def check_match(self, filename, pattern, should_match=True, fn=fnmatch): |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 11 | if should_match: |
Georg Brandl | c0e22b7 | 2010-03-14 10:51:01 +0000 | [diff] [blame] | 12 | self.assertTrue(fn(filename, pattern), |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 13 | "expected %r to match pattern %r" |
| 14 | % (filename, pattern)) |
| 15 | else: |
Serhiy Storchaka | 8175547 | 2017-05-21 08:57:00 +0300 | [diff] [blame] | 16 | self.assertFalse(fn(filename, pattern), |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 17 | "expected %r not to match pattern %r" |
| 18 | % (filename, pattern)) |
| 19 | |
| 20 | def test_fnmatch(self): |
| 21 | check = self.check_match |
| 22 | check('abc', 'abc') |
| 23 | check('abc', '?*?') |
| 24 | check('abc', '???*') |
| 25 | check('abc', '*???') |
| 26 | check('abc', '???') |
| 27 | check('abc', '*') |
| 28 | check('abc', 'ab[cd]') |
| 29 | check('abc', 'ab[!de]') |
Serhiy Storchaka | 8175547 | 2017-05-21 08:57:00 +0300 | [diff] [blame] | 30 | check('abc', 'ab[de]', False) |
| 31 | check('a', '??', False) |
| 32 | check('a', 'b', False) |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 33 | |
| 34 | # these test that '\' is handled correctly in character sets; |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 35 | # see SF bug #409651 |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 36 | check('\\', r'[\]') |
| 37 | check('a', r'[!\]') |
Serhiy Storchaka | 8175547 | 2017-05-21 08:57:00 +0300 | [diff] [blame] | 38 | check('\\', r'[!\]', False) |
Fred Drake | 9175114 | 2001-03-21 18:29:25 +0000 | [diff] [blame] | 39 | |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 40 | # test that filenames with newlines in them are handled correctly. |
| 41 | # http://bugs.python.org/issue6665 |
| 42 | check('foo\nbar', 'foo*') |
| 43 | check('foo\nbar\n', 'foo*') |
| 44 | check('\nfoo', 'foo*', False) |
| 45 | check('\n', '*') |
| 46 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 47 | def test_mix_bytes_str(self): |
| 48 | self.assertRaises(TypeError, fnmatch, 'test', b'*') |
| 49 | self.assertRaises(TypeError, fnmatch, b'test', '*') |
| 50 | self.assertRaises(TypeError, fnmatchcase, 'test', b'*') |
| 51 | self.assertRaises(TypeError, fnmatchcase, b'test', '*') |
| 52 | |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 53 | def test_fnmatchcase(self): |
| 54 | check = self.check_match |
Serhiy Storchaka | 8175547 | 2017-05-21 08:57:00 +0300 | [diff] [blame] | 55 | check('abc', 'abc', True, fnmatchcase) |
| 56 | check('AbC', 'abc', False, fnmatchcase) |
| 57 | check('abc', 'AbC', False, fnmatchcase) |
| 58 | check('AbC', 'AbC', True, fnmatchcase) |
| 59 | |
| 60 | check('usr/bin', 'usr/bin', True, fnmatchcase) |
| 61 | check('usr\\bin', 'usr/bin', False, fnmatchcase) |
| 62 | check('usr/bin', 'usr\\bin', False, fnmatchcase) |
| 63 | check('usr\\bin', 'usr\\bin', True, fnmatchcase) |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 65 | def test_bytes(self): |
| 66 | self.check_match(b'test', b'te*') |
| 67 | self.check_match(b'test\xff', b'te*\xff') |
Gregory P. Smith | 0109970 | 2009-08-16 18:58:46 +0000 | [diff] [blame] | 68 | self.check_match(b'foo\nbar', b'foo*') |
| 69 | |
Serhiy Storchaka | 8175547 | 2017-05-21 08:57:00 +0300 | [diff] [blame] | 70 | def test_case(self): |
| 71 | ignorecase = os.path.normcase('ABC') == os.path.normcase('abc') |
| 72 | check = self.check_match |
| 73 | check('abc', 'abc') |
| 74 | check('AbC', 'abc', ignorecase) |
| 75 | check('abc', 'AbC', ignorecase) |
| 76 | check('AbC', 'AbC') |
| 77 | |
| 78 | def test_sep(self): |
| 79 | normsep = os.path.normcase('\\') == os.path.normcase('/') |
| 80 | check = self.check_match |
| 81 | check('usr/bin', 'usr/bin') |
| 82 | check('usr\\bin', 'usr/bin', normsep) |
| 83 | check('usr/bin', 'usr\\bin', normsep) |
| 84 | check('usr\\bin', 'usr\\bin') |
| 85 | |
| 86 | |
Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 87 | class TranslateTestCase(unittest.TestCase): |
| 88 | |
| 89 | def test_translate(self): |
Serhiy Storchaka | bd48d27 | 2016-09-11 12:50:02 +0300 | [diff] [blame] | 90 | self.assertEqual(translate('*'), r'(?s:.*)\Z') |
| 91 | self.assertEqual(translate('?'), r'(?s:.)\Z') |
| 92 | self.assertEqual(translate('a?b*'), r'(?s:a.b.*)\Z') |
| 93 | self.assertEqual(translate('[abc]'), r'(?s:[abc])\Z') |
| 94 | self.assertEqual(translate('[]]'), r'(?s:[]])\Z') |
| 95 | self.assertEqual(translate('[!x]'), r'(?s:[^x])\Z') |
| 96 | self.assertEqual(translate('[^x]'), r'(?s:[\^x])\Z') |
| 97 | self.assertEqual(translate('[x'), r'(?s:\[x)\Z') |
Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 98 | |
| 99 | |
| 100 | class FilterTestCase(unittest.TestCase): |
| 101 | |
| 102 | def test_filter(self): |
Serhiy Storchaka | 8175547 | 2017-05-21 08:57:00 +0300 | [diff] [blame] | 103 | self.assertEqual(filter(['Python', 'Ruby', 'Perl', 'Tcl'], 'P*'), |
| 104 | ['Python', 'Perl']) |
| 105 | self.assertEqual(filter([b'Python', b'Ruby', b'Perl', b'Tcl'], b'P*'), |
| 106 | [b'Python', b'Perl']) |
| 107 | |
| 108 | def test_mix_bytes_str(self): |
| 109 | self.assertRaises(TypeError, filter, ['test'], b'*') |
| 110 | self.assertRaises(TypeError, filter, [b'test'], '*') |
| 111 | |
| 112 | def test_case(self): |
| 113 | ignorecase = os.path.normcase('P') == os.path.normcase('p') |
| 114 | self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'), |
| 115 | ['Test.py', 'Test.PL'] if ignorecase else ['Test.py']) |
| 116 | self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.P*'), |
| 117 | ['Test.py', 'Test.PL'] if ignorecase else ['Test.PL']) |
| 118 | |
| 119 | def test_sep(self): |
| 120 | normsep = os.path.normcase('\\') == os.path.normcase('/') |
| 121 | self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr/*'), |
| 122 | ['usr/bin', 'usr\\lib'] if normsep else ['usr/bin']) |
| 123 | self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr\\*'), |
| 124 | ['usr/bin', 'usr\\lib'] if normsep else ['usr\\lib']) |
Brett Cannon | 4b16e13 | 2010-07-23 16:23:13 +0000 | [diff] [blame] | 125 | |
| 126 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 127 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 128 | unittest.main() |