1 | n/a | """Test cases for the fnmatch module.""" |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | from fnmatch import fnmatch, fnmatchcase, translate, filter |
---|
6 | n/a | |
---|
7 | n/a | class FnmatchTestCase(unittest.TestCase): |
---|
8 | n/a | |
---|
9 | n/a | def check_match(self, filename, pattern, should_match=1, fn=fnmatch): |
---|
10 | n/a | if should_match: |
---|
11 | n/a | self.assertTrue(fn(filename, pattern), |
---|
12 | n/a | "expected %r to match pattern %r" |
---|
13 | n/a | % (filename, pattern)) |
---|
14 | n/a | else: |
---|
15 | n/a | self.assertTrue(not fn(filename, pattern), |
---|
16 | n/a | "expected %r not to match pattern %r" |
---|
17 | n/a | % (filename, pattern)) |
---|
18 | n/a | |
---|
19 | n/a | def test_fnmatch(self): |
---|
20 | n/a | check = self.check_match |
---|
21 | n/a | check('abc', 'abc') |
---|
22 | n/a | check('abc', '?*?') |
---|
23 | n/a | check('abc', '???*') |
---|
24 | n/a | check('abc', '*???') |
---|
25 | n/a | check('abc', '???') |
---|
26 | n/a | check('abc', '*') |
---|
27 | n/a | check('abc', 'ab[cd]') |
---|
28 | n/a | check('abc', 'ab[!de]') |
---|
29 | n/a | check('abc', 'ab[de]', 0) |
---|
30 | n/a | check('a', '??', 0) |
---|
31 | n/a | check('a', 'b', 0) |
---|
32 | n/a | |
---|
33 | n/a | # these test that '\' is handled correctly in character sets; |
---|
34 | n/a | # see SF bug #409651 |
---|
35 | n/a | check('\\', r'[\]') |
---|
36 | n/a | check('a', r'[!\]') |
---|
37 | n/a | check('\\', r'[!\]', 0) |
---|
38 | n/a | |
---|
39 | n/a | # test that filenames with newlines in them are handled correctly. |
---|
40 | n/a | # http://bugs.python.org/issue6665 |
---|
41 | n/a | check('foo\nbar', 'foo*') |
---|
42 | n/a | check('foo\nbar\n', 'foo*') |
---|
43 | n/a | check('\nfoo', 'foo*', False) |
---|
44 | n/a | check('\n', '*') |
---|
45 | n/a | |
---|
46 | n/a | def test_mix_bytes_str(self): |
---|
47 | n/a | self.assertRaises(TypeError, fnmatch, 'test', b'*') |
---|
48 | n/a | self.assertRaises(TypeError, fnmatch, b'test', '*') |
---|
49 | n/a | self.assertRaises(TypeError, fnmatchcase, 'test', b'*') |
---|
50 | n/a | self.assertRaises(TypeError, fnmatchcase, b'test', '*') |
---|
51 | n/a | |
---|
52 | n/a | def test_fnmatchcase(self): |
---|
53 | n/a | check = self.check_match |
---|
54 | n/a | check('AbC', 'abc', 0, fnmatchcase) |
---|
55 | n/a | check('abc', 'AbC', 0, fnmatchcase) |
---|
56 | n/a | |
---|
57 | n/a | def test_bytes(self): |
---|
58 | n/a | self.check_match(b'test', b'te*') |
---|
59 | n/a | self.check_match(b'test\xff', b'te*\xff') |
---|
60 | n/a | self.check_match(b'foo\nbar', b'foo*') |
---|
61 | n/a | |
---|
62 | n/a | class TranslateTestCase(unittest.TestCase): |
---|
63 | n/a | |
---|
64 | n/a | def test_translate(self): |
---|
65 | n/a | self.assertEqual(translate('*'), r'(?s:.*)\Z') |
---|
66 | n/a | self.assertEqual(translate('?'), r'(?s:.)\Z') |
---|
67 | n/a | self.assertEqual(translate('a?b*'), r'(?s:a.b.*)\Z') |
---|
68 | n/a | self.assertEqual(translate('[abc]'), r'(?s:[abc])\Z') |
---|
69 | n/a | self.assertEqual(translate('[]]'), r'(?s:[]])\Z') |
---|
70 | n/a | self.assertEqual(translate('[!x]'), r'(?s:[^x])\Z') |
---|
71 | n/a | self.assertEqual(translate('[^x]'), r'(?s:[\^x])\Z') |
---|
72 | n/a | self.assertEqual(translate('[x'), r'(?s:\[x)\Z') |
---|
73 | n/a | |
---|
74 | n/a | |
---|
75 | n/a | class FilterTestCase(unittest.TestCase): |
---|
76 | n/a | |
---|
77 | n/a | def test_filter(self): |
---|
78 | n/a | self.assertEqual(filter(['a', 'b'], 'a'), ['a']) |
---|
79 | n/a | |
---|
80 | n/a | |
---|
81 | n/a | if __name__ == "__main__": |
---|
82 | n/a | unittest.main() |
---|