1"""Test cases for the fnmatch module.""" 2 3from test import test_support 4import unittest 5import os 6 7from fnmatch import (fnmatch, fnmatchcase, translate, filter, 8 _MAXCACHE, _cache, _purge) 9 10 11class FnmatchTestCase(unittest.TestCase): 12 13 def tearDown(self): 14 _purge() 15 16 def check_match(self, filename, pattern, should_match=True, fn=fnmatch): 17 if should_match: 18 self.assertTrue(fn(filename, pattern), 19 "expected %r to match pattern %r" 20 % (filename, pattern)) 21 else: 22 self.assertFalse(fn(filename, pattern), 23 "expected %r not to match pattern %r" 24 % (filename, pattern)) 25 26 def test_fnmatch(self): 27 check = self.check_match 28 check('abc', 'abc') 29 check('abc', '?*?') 30 check('abc', '???*') 31 check('abc', '*???') 32 check('abc', '???') 33 check('abc', '*') 34 check('abc', 'ab[cd]') 35 check('abc', 'ab[!de]') 36 check('abc', 'ab[de]', False) 37 check('a', '??', False) 38 check('a', 'b', False) 39 40 # these test that '\' is handled correctly in character sets; 41 # see SF bug #409651 42 check('\\', r'[\]') 43 check('a', r'[!\]') 44 check('\\', r'[!\]', False) 45 46 # test that filenames with newlines in them are handled correctly. 47 # http://bugs.python.org/issue6665 48 check('foo\nbar', 'foo*') 49 check('foo\nbar\n', 'foo*') 50 check('\nfoo', 'foo*', False) 51 check('\n', '*') 52 53 def test_mix_unicode_str(self): 54 check = self.check_match 55 check('test', u'*') 56 check(u'test', '*') 57 check('test', u'*', fn=fnmatchcase) 58 check(u'test', '*', fn=fnmatchcase) 59 with test_support.check_warnings(("", UnicodeWarning), quiet=True): 60 check('test\xff', u'*\xff') 61 check(u'test\xff', '*\xff') 62 check('test\xff', u'*\xff', fn=fnmatchcase) 63 check(u'test\xff', '*\xff', fn=fnmatchcase) 64 65 def test_fnmatchcase(self): 66 check = self.check_match 67 check('abc', 'abc', True, fnmatchcase) 68 check('AbC', 'abc', False, fnmatchcase) 69 check('abc', 'AbC', False, fnmatchcase) 70 check('AbC', 'AbC', True, fnmatchcase) 71 72 check('usr/bin', 'usr/bin', True, fnmatchcase) 73 check('usr\\bin', 'usr/bin', False, fnmatchcase) 74 check('usr/bin', 'usr\\bin', False, fnmatchcase) 75 check('usr\\bin', 'usr\\bin', True, fnmatchcase) 76 77 def test_cache_clearing(self): 78 # check that caches do not grow too large 79 # http://bugs.python.org/issue7846 80 81 # string pattern cache 82 for i in range(_MAXCACHE + 1): 83 fnmatch('foo', '?' * i) 84 85 self.assertLessEqual(len(_cache), _MAXCACHE) 86 87 @test_support.requires_unicode 88 def test_unicode(self): 89 with test_support.check_warnings(("", UnicodeWarning), quiet=True): 90 self.check_match(u'test', u'te*') 91 self.check_match(u'test\xff', u'te*\xff') 92 self.check_match(u'test'+unichr(0x20ac), u'te*'+unichr(0x20ac)) 93 self.check_match(u'foo\nbar', u'foo*') 94 95 def test_case(self): 96 ignorecase = os.path.normcase('ABC') == os.path.normcase('abc') 97 check = self.check_match 98 check('abc', 'abc') 99 check('AbC', 'abc', ignorecase) 100 check('abc', 'AbC', ignorecase) 101 check('AbC', 'AbC') 102 103 def test_sep(self): 104 normsep = os.path.normcase('\\') == os.path.normcase('/') 105 check = self.check_match 106 check('usr/bin', 'usr/bin') 107 check('usr\\bin', 'usr/bin', normsep) 108 check('usr/bin', 'usr\\bin', normsep) 109 check('usr\\bin', 'usr\\bin') 110 111 112class TranslateTestCase(unittest.TestCase): 113 114 def test_translate(self): 115 self.assertEqual(translate('*'), r'.*\Z(?ms)') 116 self.assertEqual(translate('?'), r'.\Z(?ms)') 117 self.assertEqual(translate('a?b*'), r'a.b.*\Z(?ms)') 118 self.assertEqual(translate('[abc]'), r'[abc]\Z(?ms)') 119 self.assertEqual(translate('[]]'), r'[]]\Z(?ms)') 120 self.assertEqual(translate('[!x]'), r'[^x]\Z(?ms)') 121 self.assertEqual(translate('[^x]'), r'[\^x]\Z(?ms)') 122 self.assertEqual(translate('[x'), r'\[x\Z(?ms)') 123 124 125class FilterTestCase(unittest.TestCase): 126 127 def test_filter(self): 128 self.assertEqual(filter(['Python', 'Ruby', 'Perl', 'Tcl'], 'P*'), 129 ['Python', 'Perl']) 130 self.assertEqual(filter([u'Python', u'Ruby', u'Perl', u'Tcl'], u'P*'), 131 [u'Python', u'Perl']) 132 with test_support.check_warnings(("", UnicodeWarning), quiet=True): 133 self.assertEqual(filter([u'test\xff'], u'*\xff'), [u'test\xff']) 134 135 @test_support.requires_unicode 136 def test_mix_bytes_str(self): 137 with test_support.check_warnings(("", UnicodeWarning), quiet=True): 138 self.assertEqual(filter(['test'], u'*'), ['test']) 139 self.assertEqual(filter([u'test'], '*'), [u'test']) 140 self.assertEqual(filter(['test\xff'], u'*'), ['test\xff']) 141 self.assertEqual(filter([u'test\xff'], '*'), [u'test\xff']) 142 self.assertEqual(filter(['test\xff'], u'*\xff'), ['test\xff']) 143 self.assertEqual(filter([u'test\xff'], '*\xff'), [u'test\xff']) 144 145 def test_case(self): 146 ignorecase = os.path.normcase('P') == os.path.normcase('p') 147 self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.p*'), 148 ['Test.py', 'Test.PL'] if ignorecase else ['Test.py']) 149 self.assertEqual(filter(['Test.py', 'Test.rb', 'Test.PL'], '*.P*'), 150 ['Test.py', 'Test.PL'] if ignorecase else ['Test.PL']) 151 152 def test_sep(self): 153 normsep = os.path.normcase('\\') == os.path.normcase('/') 154 self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr/*'), 155 ['usr/bin', 'usr\\lib'] if normsep else ['usr/bin']) 156 self.assertEqual(filter(['usr/bin', 'usr', 'usr\\lib'], 'usr\\*'), 157 ['usr/bin', 'usr\\lib'] if normsep else ['usr\\lib']) 158 159 160def test_main(): 161 test_support.run_unittest(FnmatchTestCase, TranslateTestCase, FilterTestCase) 162 163 164if __name__ == "__main__": 165 test_main() 166