ยปCore Development>Code coverage>Lib/distutils/tests/test_text_file.py

Python code coverage for Lib/distutils/tests/test_text_file.py

#countcontent
1n/a"""Tests for distutils.text_file."""
2n/aimport os
3n/aimport unittest
4n/afrom distutils.text_file import TextFile
5n/afrom distutils.tests import support
6n/afrom test.support import run_unittest
7n/a
8n/aTEST_DATA = """# test file
9n/a
10n/aline 3 \\
11n/a# intervening comment
12n/a continues on next line
13n/a"""
14n/a
15n/aclass TextFileTestCase(support.TempdirManager, unittest.TestCase):
16n/a
17n/a def test_class(self):
18n/a # old tests moved from text_file.__main__
19n/a # so they are really called by the buildbots
20n/a
21n/a # result 1: no fancy options
22n/a result1 = ['# test file\n', '\n', 'line 3 \\\n',
23n/a '# intervening comment\n',
24n/a ' continues on next line\n']
25n/a
26n/a # result 2: just strip comments
27n/a result2 = ["\n",
28n/a "line 3 \\\n",
29n/a " continues on next line\n"]
30n/a
31n/a # result 3: just strip blank lines
32n/a result3 = ["# test file\n",
33n/a "line 3 \\\n",
34n/a "# intervening comment\n",
35n/a " continues on next line\n"]
36n/a
37n/a # result 4: default, strip comments, blank lines,
38n/a # and trailing whitespace
39n/a result4 = ["line 3 \\",
40n/a " continues on next line"]
41n/a
42n/a # result 5: strip comments and blanks, plus join lines (but don't
43n/a # "collapse" joined lines
44n/a result5 = ["line 3 continues on next line"]
45n/a
46n/a # result 6: strip comments and blanks, plus join lines (and
47n/a # "collapse" joined lines
48n/a result6 = ["line 3 continues on next line"]
49n/a
50n/a def test_input(count, description, file, expected_result):
51n/a result = file.readlines()
52n/a self.assertEqual(result, expected_result)
53n/a
54n/a tmpdir = self.mkdtemp()
55n/a filename = os.path.join(tmpdir, "test.txt")
56n/a out_file = open(filename, "w")
57n/a try:
58n/a out_file.write(TEST_DATA)
59n/a finally:
60n/a out_file.close()
61n/a
62n/a in_file = TextFile(filename, strip_comments=0, skip_blanks=0,
63n/a lstrip_ws=0, rstrip_ws=0)
64n/a try:
65n/a test_input(1, "no processing", in_file, result1)
66n/a finally:
67n/a in_file.close()
68n/a
69n/a in_file = TextFile(filename, strip_comments=1, skip_blanks=0,
70n/a lstrip_ws=0, rstrip_ws=0)
71n/a try:
72n/a test_input(2, "strip comments", in_file, result2)
73n/a finally:
74n/a in_file.close()
75n/a
76n/a in_file = TextFile(filename, strip_comments=0, skip_blanks=1,
77n/a lstrip_ws=0, rstrip_ws=0)
78n/a try:
79n/a test_input(3, "strip blanks", in_file, result3)
80n/a finally:
81n/a in_file.close()
82n/a
83n/a in_file = TextFile(filename)
84n/a try:
85n/a test_input(4, "default processing", in_file, result4)
86n/a finally:
87n/a in_file.close()
88n/a
89n/a in_file = TextFile(filename, strip_comments=1, skip_blanks=1,
90n/a join_lines=1, rstrip_ws=1)
91n/a try:
92n/a test_input(5, "join lines without collapsing", in_file, result5)
93n/a finally:
94n/a in_file.close()
95n/a
96n/a in_file = TextFile(filename, strip_comments=1, skip_blanks=1,
97n/a join_lines=1, rstrip_ws=1, collapse_join=1)
98n/a try:
99n/a test_input(6, "join lines with collapsing", in_file, result6)
100n/a finally:
101n/a in_file.close()
102n/a
103n/adef test_suite():
104n/a return unittest.makeSuite(TextFileTestCase)
105n/a
106n/aif __name__ == "__main__":
107n/a run_unittest(test_suite())