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

Python code coverage for Lib/test/test_tools/test_pindent.py

#countcontent
1n/a"""Tests for the pindent script in the Tools directory."""
2n/a
3n/aimport os
4n/aimport sys
5n/aimport unittest
6n/aimport subprocess
7n/aimport textwrap
8n/afrom test import support
9n/afrom test.support.script_helper import assert_python_ok
10n/a
11n/afrom test.test_tools import scriptsdir, skip_if_missing
12n/a
13n/askip_if_missing()
14n/a
15n/a
16n/aclass PindentTests(unittest.TestCase):
17n/a script = os.path.join(scriptsdir, 'pindent.py')
18n/a
19n/a def assertFileEqual(self, fn1, fn2):
20n/a with open(fn1) as f1, open(fn2) as f2:
21n/a self.assertEqual(f1.readlines(), f2.readlines())
22n/a
23n/a def pindent(self, source, *args):
24n/a with subprocess.Popen(
25n/a (sys.executable, self.script) + args,
26n/a stdin=subprocess.PIPE, stdout=subprocess.PIPE,
27n/a universal_newlines=True) as proc:
28n/a out, err = proc.communicate(source)
29n/a self.assertIsNone(err)
30n/a return out
31n/a
32n/a def lstriplines(self, data):
33n/a return '\n'.join(line.lstrip() for line in data.splitlines()) + '\n'
34n/a
35n/a def test_selftest(self):
36n/a self.maxDiff = None
37n/a with support.temp_dir() as directory:
38n/a data_path = os.path.join(directory, '_test.py')
39n/a with open(self.script) as f:
40n/a closed = f.read()
41n/a with open(data_path, 'w') as f:
42n/a f.write(closed)
43n/a
44n/a rc, out, err = assert_python_ok(self.script, '-d', data_path)
45n/a self.assertEqual(out, b'')
46n/a self.assertEqual(err, b'')
47n/a backup = data_path + '~'
48n/a self.assertTrue(os.path.exists(backup))
49n/a with open(backup) as f:
50n/a self.assertEqual(f.read(), closed)
51n/a with open(data_path) as f:
52n/a clean = f.read()
53n/a compile(clean, '_test.py', 'exec')
54n/a self.assertEqual(self.pindent(clean, '-c'), closed)
55n/a self.assertEqual(self.pindent(closed, '-d'), clean)
56n/a
57n/a rc, out, err = assert_python_ok(self.script, '-c', data_path)
58n/a self.assertEqual(out, b'')
59n/a self.assertEqual(err, b'')
60n/a with open(backup) as f:
61n/a self.assertEqual(f.read(), clean)
62n/a with open(data_path) as f:
63n/a self.assertEqual(f.read(), closed)
64n/a
65n/a broken = self.lstriplines(closed)
66n/a with open(data_path, 'w') as f:
67n/a f.write(broken)
68n/a rc, out, err = assert_python_ok(self.script, '-r', data_path)
69n/a self.assertEqual(out, b'')
70n/a self.assertEqual(err, b'')
71n/a with open(backup) as f:
72n/a self.assertEqual(f.read(), broken)
73n/a with open(data_path) as f:
74n/a indented = f.read()
75n/a compile(indented, '_test.py', 'exec')
76n/a self.assertEqual(self.pindent(broken, '-r'), indented)
77n/a
78n/a def pindent_test(self, clean, closed):
79n/a self.assertEqual(self.pindent(clean, '-c'), closed)
80n/a self.assertEqual(self.pindent(closed, '-d'), clean)
81n/a broken = self.lstriplines(closed)
82n/a self.assertEqual(self.pindent(broken, '-r', '-e', '-s', '4'), closed)
83n/a
84n/a def test_statements(self):
85n/a clean = textwrap.dedent("""\
86n/a if a:
87n/a pass
88n/a
89n/a if a:
90n/a pass
91n/a else:
92n/a pass
93n/a
94n/a if a:
95n/a pass
96n/a elif:
97n/a pass
98n/a else:
99n/a pass
100n/a
101n/a while a:
102n/a break
103n/a
104n/a while a:
105n/a break
106n/a else:
107n/a pass
108n/a
109n/a for i in a:
110n/a break
111n/a
112n/a for i in a:
113n/a break
114n/a else:
115n/a pass
116n/a
117n/a try:
118n/a pass
119n/a finally:
120n/a pass
121n/a
122n/a try:
123n/a pass
124n/a except TypeError:
125n/a pass
126n/a except ValueError:
127n/a pass
128n/a else:
129n/a pass
130n/a
131n/a try:
132n/a pass
133n/a except TypeError:
134n/a pass
135n/a except ValueError:
136n/a pass
137n/a finally:
138n/a pass
139n/a
140n/a with a:
141n/a pass
142n/a
143n/a class A:
144n/a pass
145n/a
146n/a def f():
147n/a pass
148n/a """)
149n/a
150n/a closed = textwrap.dedent("""\
151n/a if a:
152n/a pass
153n/a # end if
154n/a
155n/a if a:
156n/a pass
157n/a else:
158n/a pass
159n/a # end if
160n/a
161n/a if a:
162n/a pass
163n/a elif:
164n/a pass
165n/a else:
166n/a pass
167n/a # end if
168n/a
169n/a while a:
170n/a break
171n/a # end while
172n/a
173n/a while a:
174n/a break
175n/a else:
176n/a pass
177n/a # end while
178n/a
179n/a for i in a:
180n/a break
181n/a # end for
182n/a
183n/a for i in a:
184n/a break
185n/a else:
186n/a pass
187n/a # end for
188n/a
189n/a try:
190n/a pass
191n/a finally:
192n/a pass
193n/a # end try
194n/a
195n/a try:
196n/a pass
197n/a except TypeError:
198n/a pass
199n/a except ValueError:
200n/a pass
201n/a else:
202n/a pass
203n/a # end try
204n/a
205n/a try:
206n/a pass
207n/a except TypeError:
208n/a pass
209n/a except ValueError:
210n/a pass
211n/a finally:
212n/a pass
213n/a # end try
214n/a
215n/a with a:
216n/a pass
217n/a # end with
218n/a
219n/a class A:
220n/a pass
221n/a # end class A
222n/a
223n/a def f():
224n/a pass
225n/a # end def f
226n/a """)
227n/a self.pindent_test(clean, closed)
228n/a
229n/a def test_multilevel(self):
230n/a clean = textwrap.dedent("""\
231n/a def foobar(a, b):
232n/a if a == b:
233n/a a = a+1
234n/a elif a < b:
235n/a b = b-1
236n/a if b > a: a = a-1
237n/a else:
238n/a print 'oops!'
239n/a """)
240n/a closed = textwrap.dedent("""\
241n/a def foobar(a, b):
242n/a if a == b:
243n/a a = a+1
244n/a elif a < b:
245n/a b = b-1
246n/a if b > a: a = a-1
247n/a # end if
248n/a else:
249n/a print 'oops!'
250n/a # end if
251n/a # end def foobar
252n/a """)
253n/a self.pindent_test(clean, closed)
254n/a
255n/a def test_preserve_indents(self):
256n/a clean = textwrap.dedent("""\
257n/a if a:
258n/a if b:
259n/a pass
260n/a """)
261n/a closed = textwrap.dedent("""\
262n/a if a:
263n/a if b:
264n/a pass
265n/a # end if
266n/a # end if
267n/a """)
268n/a self.assertEqual(self.pindent(clean, '-c'), closed)
269n/a self.assertEqual(self.pindent(closed, '-d'), clean)
270n/a broken = self.lstriplines(closed)
271n/a self.assertEqual(self.pindent(broken, '-r', '-e', '-s', '9'), closed)
272n/a clean = textwrap.dedent("""\
273n/a if a:
274n/a \tif b:
275n/a \t\tpass
276n/a """)
277n/a closed = textwrap.dedent("""\
278n/a if a:
279n/a \tif b:
280n/a \t\tpass
281n/a \t# end if
282n/a # end if
283n/a """)
284n/a self.assertEqual(self.pindent(clean, '-c'), closed)
285n/a self.assertEqual(self.pindent(closed, '-d'), clean)
286n/a broken = self.lstriplines(closed)
287n/a self.assertEqual(self.pindent(broken, '-r'), closed)
288n/a
289n/a def test_escaped_newline(self):
290n/a clean = textwrap.dedent("""\
291n/a class\\
292n/a \\
293n/a A:
294n/a def\
295n/a \\
296n/a f:
297n/a pass
298n/a """)
299n/a closed = textwrap.dedent("""\
300n/a class\\
301n/a \\
302n/a A:
303n/a def\
304n/a \\
305n/a f:
306n/a pass
307n/a # end def f
308n/a # end class A
309n/a """)
310n/a self.assertEqual(self.pindent(clean, '-c'), closed)
311n/a self.assertEqual(self.pindent(closed, '-d'), clean)
312n/a
313n/a def test_empty_line(self):
314n/a clean = textwrap.dedent("""\
315n/a if a:
316n/a
317n/a pass
318n/a """)
319n/a closed = textwrap.dedent("""\
320n/a if a:
321n/a
322n/a pass
323n/a # end if
324n/a """)
325n/a self.pindent_test(clean, closed)
326n/a
327n/a def test_oneline(self):
328n/a clean = textwrap.dedent("""\
329n/a if a: pass
330n/a """)
331n/a closed = textwrap.dedent("""\
332n/a if a: pass
333n/a # end if
334n/a """)
335n/a self.pindent_test(clean, closed)
336n/a
337n/a
338n/aif __name__ == '__main__':
339n/a unittest.main()