ยปCore Development>Code coverage>Lib/test/test_fnmatch.py

Python code coverage for Lib/test/test_fnmatch.py

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