blob: f95f3666c0cc80988d6964935c1645ab6c69bf9d [file] [log] [blame]
Bill Wendling92290f12018-01-14 16:49:35 -08001# Copyright 2015 Google Inc. All Rights Reserved.
Bill Wendling7d623452015-03-18 13:36:07 -07002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Tests for yapf.pytree_unwrapper."""
15
Bill Wendling7d623452015-03-18 13:36:07 -070016import textwrap
17import unittest
18
Bill Wendling7d623452015-03-18 13:36:07 -070019from yapf.yapflib import pytree_utils
Bill Wendling7d623452015-03-18 13:36:07 -070020
Bill Wendlinga8ebae22016-10-23 16:09:06 -070021from yapftests import yapf_test_helper
Bill Wendling7d623452015-03-18 13:36:07 -070022
Bill Wendling7d623452015-03-18 13:36:07 -070023
Bill Wendlinga8ebae22016-10-23 16:09:06 -070024class PytreeUnwrapperTest(yapf_test_helper.YAPFTest):
Bill Wendling7d623452015-03-18 13:36:07 -070025
26 def _CheckUnwrappedLines(self, uwlines, list_of_expected):
27 """Check that the given UnwrappedLines match expectations.
28
29 Args:
30 uwlines: list of UnwrappedLine
31 list_of_expected: list of (depth, values) pairs. Non-semantic tokens are
32 filtered out from the expected values.
33 """
34 actual = []
35 for uwl in uwlines:
Bill Wendlingeef8fbd2016-10-05 00:34:25 -070036 filtered_values = [
Matthew Suozzoba8f51f2017-10-31 00:33:38 -040037 ft.value
38 for ft in uwl.tokens
Bill Wendlingeef8fbd2016-10-05 00:34:25 -070039 if ft.name not in pytree_utils.NONSEMANTIC_TOKENS
40 ]
Bill Wendling7d623452015-03-18 13:36:07 -070041 actual.append((uwl.depth, filtered_values))
42
43 self.assertEqual(list_of_expected, actual)
44
45 def testSimpleFileScope(self):
46 code = textwrap.dedent(r"""
47 x = 1
48 # a comment
49 y = 2
50 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -070051 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -070052 self._CheckUnwrappedLines(uwlines, [
53 (0, ['x', '=', '1']),
54 (0, ['# a comment']),
Bill Wendlinga2f03422016-10-23 16:36:47 -070055 (0, ['y', '=', '2']),
56 ])
Bill Wendling7d623452015-03-18 13:36:07 -070057
58 def testSimpleMultilineStatement(self):
59 code = textwrap.dedent(r"""
60 y = (1 +
61 x)
62 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -070063 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -070064 self._CheckUnwrappedLines(uwlines, [
Bill Wendlinga2f03422016-10-23 16:36:47 -070065 (0, ['y', '=', '(', '1', '+', 'x', ')']),
66 ])
Bill Wendling7d623452015-03-18 13:36:07 -070067
68 def testFileScopeWithInlineComment(self):
69 code = textwrap.dedent(r"""
70 x = 1 # a comment
71 y = 2
72 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -070073 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -070074 self._CheckUnwrappedLines(uwlines, [
75 (0, ['x', '=', '1', '# a comment']),
Bill Wendlinga2f03422016-10-23 16:36:47 -070076 (0, ['y', '=', '2']),
77 ])
Bill Wendling7d623452015-03-18 13:36:07 -070078
79 def testSimpleIf(self):
80 code = textwrap.dedent(r"""
81 if foo:
82 x = 1
83 y = 2
84 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -070085 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -070086 self._CheckUnwrappedLines(uwlines, [
87 (0, ['if', 'foo', ':']),
88 (1, ['x', '=', '1']),
Bill Wendlinga2f03422016-10-23 16:36:47 -070089 (1, ['y', '=', '2']),
90 ])
Bill Wendling7d623452015-03-18 13:36:07 -070091
92 def testSimpleIfWithComments(self):
93 code = textwrap.dedent(r"""
94 # c1
95 if foo: # c2
96 x = 1
97 y = 2
98 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -070099 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700100 self._CheckUnwrappedLines(uwlines, [
101 (0, ['# c1']),
102 (0, ['if', 'foo', ':', '# c2']),
103 (1, ['x', '=', '1']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700104 (1, ['y', '=', '2']),
105 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700106
107 def testIfWithCommentsInside(self):
108 code = textwrap.dedent(r"""
109 if foo:
110 # c1
111 x = 1 # c2
112 # c3
113 y = 2
114 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700115 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700116 self._CheckUnwrappedLines(uwlines, [
117 (0, ['if', 'foo', ':']),
118 (1, ['# c1']),
119 (1, ['x', '=', '1', '# c2']),
120 (1, ['# c3']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700121 (1, ['y', '=', '2']),
122 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700123
124 def testIfElifElse(self):
125 code = textwrap.dedent(r"""
126 if x:
127 x = 1 # c1
128 elif y: # c2
129 y = 1
130 else:
131 # c3
132 z = 1
133 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700134 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700135 self._CheckUnwrappedLines(uwlines, [
136 (0, ['if', 'x', ':']),
137 (1, ['x', '=', '1', '# c1']),
138 (0, ['elif', 'y', ':', '# c2']),
139 (1, ['y', '=', '1']),
140 (0, ['else', ':']),
141 (1, ['# c3']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700142 (1, ['z', '=', '1']),
143 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700144
145 def testNestedCompoundTwoLevel(self):
146 code = textwrap.dedent(r"""
147 if x:
148 x = 1 # c1
149 while t:
150 # c2
151 j = 1
152 k = 1
153 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700154 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700155 self._CheckUnwrappedLines(uwlines, [
156 (0, ['if', 'x', ':']),
157 (1, ['x', '=', '1', '# c1']),
158 (1, ['while', 't', ':']),
159 (2, ['# c2']),
160 (2, ['j', '=', '1']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700161 (1, ['k', '=', '1']),
162 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700163
164 def testSimpleWhile(self):
165 code = textwrap.dedent(r"""
166 while x > 1: # c1
167 # c2
168 x = 1
169 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700170 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700171 self._CheckUnwrappedLines(uwlines, [
172 (0, ['while', 'x', '>', '1', ':', '# c1']),
173 (1, ['# c2']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700174 (1, ['x', '=', '1']),
175 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700176
177 def testSimpleTry(self):
178 code = textwrap.dedent(r"""
179 try:
180 pass
181 except:
182 pass
183 except:
184 pass
185 else:
186 pass
187 finally:
188 pass
189 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700190 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700191 self._CheckUnwrappedLines(uwlines, [
192 (0, ['try', ':']),
193 (1, ['pass']),
194 (0, ['except', ':']),
195 (1, ['pass']),
196 (0, ['except', ':']),
197 (1, ['pass']),
198 (0, ['else', ':']),
199 (1, ['pass']),
200 (0, ['finally', ':']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700201 (1, ['pass']),
202 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700203
204 def testSimpleFuncdef(self):
205 code = textwrap.dedent(r"""
206 def foo(x): # c1
207 # c2
208 return x
209 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700210 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700211 self._CheckUnwrappedLines(uwlines, [
212 (0, ['def', 'foo', '(', 'x', ')', ':', '# c1']),
213 (1, ['# c2']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700214 (1, ['return', 'x']),
215 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700216
217 def testTwoFuncDefs(self):
218 code = textwrap.dedent(r"""
219 def foo(x): # c1
220 # c2
221 return x
222
223 def bar(): # c3
224 # c4
225 return x
226 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700227 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700228 self._CheckUnwrappedLines(uwlines, [
229 (0, ['def', 'foo', '(', 'x', ')', ':', '# c1']),
230 (1, ['# c2']),
231 (1, ['return', 'x']),
232 (0, ['def', 'bar', '(', ')', ':', '# c3']),
233 (1, ['# c4']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700234 (1, ['return', 'x']),
235 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700236
237 def testSimpleClassDef(self):
238 code = textwrap.dedent(r"""
239 class Klass: # c1
240 # c2
241 p = 1
242 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700243 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700244 self._CheckUnwrappedLines(uwlines, [
245 (0, ['class', 'Klass', ':', '# c1']),
246 (1, ['# c2']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700247 (1, ['p', '=', '1']),
248 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700249
250 def testSingleLineStmtInFunc(self):
251 code = textwrap.dedent(r"""
252 def f(): return 37
253 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700254 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700255 self._CheckUnwrappedLines(uwlines, [
256 (0, ['def', 'f', '(', ')', ':']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700257 (1, ['return', '37']),
258 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700259
260 def testMultipleComments(self):
261 code = textwrap.dedent(r"""
262 # Comment #1
263
264 # Comment #2
265 def f():
266 pass
267 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700268 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700269 self._CheckUnwrappedLines(uwlines, [
270 (0, ['# Comment #1']),
271 (0, ['# Comment #2']),
272 (0, ['def', 'f', '(', ')', ':']),
Bill Wendlinga2f03422016-10-23 16:36:47 -0700273 (1, ['pass']),
274 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700275
Bill Wendling5b419822015-07-06 01:39:50 -0700276 def testSplitListWithComment(self):
277 code = textwrap.dedent(r"""
278 a = [
279 'a',
280 'b',
281 'c', # hello world
282 ]
283 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700284 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendlinga2f03422016-10-23 16:36:47 -0700285 self._CheckUnwrappedLines(uwlines, [(0, [
286 'a', '=', '[', "'a'", ',', "'b'", ',', "'c'", ',', '# hello world', ']'
287 ])])
Bill Wendling5b419822015-07-06 01:39:50 -0700288
Bill Wendling7d623452015-03-18 13:36:07 -0700289
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700290class MatchBracketsTest(yapf_test_helper.YAPFTest):
Bill Wendling7d623452015-03-18 13:36:07 -0700291
292 def _CheckMatchingBrackets(self, uwlines, list_of_expected):
293 """Check that the tokens have the expected matching bracket.
294
295 Arguments:
296 uwlines: list of UnwrappedLine.
297 list_of_expected: list of (index, index) pairs. The matching brackets at
298 the indexes need to match. Non-semantic tokens are filtered out from the
299 expected values.
300 """
301 actual = []
302 for uwl in uwlines:
Matthew Suozzoba8f51f2017-10-31 00:33:38 -0400303 filtered_values = [(ft, ft.matching_bracket)
304 for ft in uwl.tokens
Bill Wendling7d623452015-03-18 13:36:07 -0700305 if ft.name not in pytree_utils.NONSEMANTIC_TOKENS]
306 if filtered_values:
307 actual.append(filtered_values)
308
309 for index, bracket_list in enumerate(list_of_expected):
310 uwline = actual[index]
311 if not bracket_list:
312 for value in uwline:
313 self.assertIsNone(value[1])
314 else:
315 for open_bracket, close_bracket in bracket_list:
316 self.assertEqual(uwline[open_bracket][0], uwline[close_bracket][1])
317 self.assertEqual(uwline[close_bracket][0], uwline[open_bracket][1])
318
319 def testFunctionDef(self):
320 code = textwrap.dedent("""\
Bill Wendling2b0c6312018-01-13 21:53:14 -0800321 def foo(a, b=['w','d'], c=[42, 37]):
Bill Wendling7d623452015-03-18 13:36:07 -0700322 pass
323 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700324 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700325 self._CheckMatchingBrackets(uwlines, [
Bill Wendling2b0c6312018-01-13 21:53:14 -0800326 [(2, 20), (7, 11), (15, 19)],
Bill Wendlinga2f03422016-10-23 16:36:47 -0700327 [],
328 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700329
330 def testDecorator(self):
331 code = textwrap.dedent("""\
332 @bar()
333 def foo(a, b, c):
334 pass
335 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700336 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700337 self._CheckMatchingBrackets(uwlines, [
338 [(2, 3)],
339 [(2, 8)],
Bill Wendlinga2f03422016-10-23 16:36:47 -0700340 [],
341 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700342
343 def testClassDef(self):
344 code = textwrap.dedent("""\
345 class A(B, C, D):
346 pass
347 """)
Bill Wendlinga8ebae22016-10-23 16:09:06 -0700348 uwlines = yapf_test_helper.ParseAndUnwrap(code)
Bill Wendling7d623452015-03-18 13:36:07 -0700349 self._CheckMatchingBrackets(uwlines, [
350 [(2, 8)],
Bill Wendlinga2f03422016-10-23 16:36:47 -0700351 [],
352 ])
Bill Wendling7d623452015-03-18 13:36:07 -0700353
354
Bill Wendling7d623452015-03-18 13:36:07 -0700355if __name__ == '__main__':
356 unittest.main()