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