1 | n/a | """ Test suite for the code in fixer_util """ |
---|
2 | n/a | |
---|
3 | n/a | # Testing imports |
---|
4 | n/a | from . import support |
---|
5 | n/a | |
---|
6 | n/a | # Local imports |
---|
7 | n/a | from lib2to3.pytree import Node, Leaf |
---|
8 | n/a | from lib2to3 import fixer_util |
---|
9 | n/a | from lib2to3.fixer_util import Attr, Name, Call, Comma |
---|
10 | n/a | from lib2to3.pgen2 import token |
---|
11 | n/a | |
---|
12 | n/a | def parse(code, strip_levels=0): |
---|
13 | n/a | # The topmost node is file_input, which we don't care about. |
---|
14 | n/a | # The next-topmost node is a *_stmt node, which we also don't care about |
---|
15 | n/a | tree = support.parse_string(code) |
---|
16 | n/a | for i in range(strip_levels): |
---|
17 | n/a | tree = tree.children[0] |
---|
18 | n/a | tree.parent = None |
---|
19 | n/a | return tree |
---|
20 | n/a | |
---|
21 | n/a | class MacroTestCase(support.TestCase): |
---|
22 | n/a | def assertStr(self, node, string): |
---|
23 | n/a | if isinstance(node, (tuple, list)): |
---|
24 | n/a | node = Node(fixer_util.syms.simple_stmt, node) |
---|
25 | n/a | self.assertEqual(str(node), string) |
---|
26 | n/a | |
---|
27 | n/a | |
---|
28 | n/a | class Test_is_tuple(support.TestCase): |
---|
29 | n/a | def is_tuple(self, string): |
---|
30 | n/a | return fixer_util.is_tuple(parse(string, strip_levels=2)) |
---|
31 | n/a | |
---|
32 | n/a | def test_valid(self): |
---|
33 | n/a | self.assertTrue(self.is_tuple("(a, b)")) |
---|
34 | n/a | self.assertTrue(self.is_tuple("(a, (b, c))")) |
---|
35 | n/a | self.assertTrue(self.is_tuple("((a, (b, c)),)")) |
---|
36 | n/a | self.assertTrue(self.is_tuple("(a,)")) |
---|
37 | n/a | self.assertTrue(self.is_tuple("()")) |
---|
38 | n/a | |
---|
39 | n/a | def test_invalid(self): |
---|
40 | n/a | self.assertFalse(self.is_tuple("(a)")) |
---|
41 | n/a | self.assertFalse(self.is_tuple("('foo') % (b, c)")) |
---|
42 | n/a | |
---|
43 | n/a | |
---|
44 | n/a | class Test_is_list(support.TestCase): |
---|
45 | n/a | def is_list(self, string): |
---|
46 | n/a | return fixer_util.is_list(parse(string, strip_levels=2)) |
---|
47 | n/a | |
---|
48 | n/a | def test_valid(self): |
---|
49 | n/a | self.assertTrue(self.is_list("[]")) |
---|
50 | n/a | self.assertTrue(self.is_list("[a]")) |
---|
51 | n/a | self.assertTrue(self.is_list("[a, b]")) |
---|
52 | n/a | self.assertTrue(self.is_list("[a, [b, c]]")) |
---|
53 | n/a | self.assertTrue(self.is_list("[[a, [b, c]],]")) |
---|
54 | n/a | |
---|
55 | n/a | def test_invalid(self): |
---|
56 | n/a | self.assertFalse(self.is_list("[]+[]")) |
---|
57 | n/a | |
---|
58 | n/a | |
---|
59 | n/a | class Test_Attr(MacroTestCase): |
---|
60 | n/a | def test(self): |
---|
61 | n/a | call = parse("foo()", strip_levels=2) |
---|
62 | n/a | |
---|
63 | n/a | self.assertStr(Attr(Name("a"), Name("b")), "a.b") |
---|
64 | n/a | self.assertStr(Attr(call, Name("b")), "foo().b") |
---|
65 | n/a | |
---|
66 | n/a | def test_returns(self): |
---|
67 | n/a | attr = Attr(Name("a"), Name("b")) |
---|
68 | n/a | self.assertEqual(type(attr), list) |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | class Test_Name(MacroTestCase): |
---|
72 | n/a | def test(self): |
---|
73 | n/a | self.assertStr(Name("a"), "a") |
---|
74 | n/a | self.assertStr(Name("foo.foo().bar"), "foo.foo().bar") |
---|
75 | n/a | self.assertStr(Name("a", prefix="b"), "ba") |
---|
76 | n/a | |
---|
77 | n/a | |
---|
78 | n/a | class Test_Call(MacroTestCase): |
---|
79 | n/a | def _Call(self, name, args=None, prefix=None): |
---|
80 | n/a | """Help the next test""" |
---|
81 | n/a | children = [] |
---|
82 | n/a | if isinstance(args, list): |
---|
83 | n/a | for arg in args: |
---|
84 | n/a | children.append(arg) |
---|
85 | n/a | children.append(Comma()) |
---|
86 | n/a | children.pop() |
---|
87 | n/a | return Call(Name(name), children, prefix) |
---|
88 | n/a | |
---|
89 | n/a | def test(self): |
---|
90 | n/a | kids = [None, |
---|
91 | n/a | [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 2), |
---|
92 | n/a | Leaf(token.NUMBER, 3)], |
---|
93 | n/a | [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 3), |
---|
94 | n/a | Leaf(token.NUMBER, 2), Leaf(token.NUMBER, 4)], |
---|
95 | n/a | [Leaf(token.STRING, "b"), Leaf(token.STRING, "j", prefix=" ")] |
---|
96 | n/a | ] |
---|
97 | n/a | self.assertStr(self._Call("A"), "A()") |
---|
98 | n/a | self.assertStr(self._Call("b", kids[1]), "b(1,2,3)") |
---|
99 | n/a | self.assertStr(self._Call("a.b().c", kids[2]), "a.b().c(1,3,2,4)") |
---|
100 | n/a | self.assertStr(self._Call("d", kids[3], prefix=" "), " d(b, j)") |
---|
101 | n/a | |
---|
102 | n/a | |
---|
103 | n/a | class Test_does_tree_import(support.TestCase): |
---|
104 | n/a | def _find_bind_rec(self, name, node): |
---|
105 | n/a | # Search a tree for a binding -- used to find the starting |
---|
106 | n/a | # point for these tests. |
---|
107 | n/a | c = fixer_util.find_binding(name, node) |
---|
108 | n/a | if c: return c |
---|
109 | n/a | for child in node.children: |
---|
110 | n/a | c = self._find_bind_rec(name, child) |
---|
111 | n/a | if c: return c |
---|
112 | n/a | |
---|
113 | n/a | def does_tree_import(self, package, name, string): |
---|
114 | n/a | node = parse(string) |
---|
115 | n/a | # Find the binding of start -- that's what we'll go from |
---|
116 | n/a | node = self._find_bind_rec('start', node) |
---|
117 | n/a | return fixer_util.does_tree_import(package, name, node) |
---|
118 | n/a | |
---|
119 | n/a | def try_with(self, string): |
---|
120 | n/a | failing_tests = (("a", "a", "from a import b"), |
---|
121 | n/a | ("a.d", "a", "from a.d import b"), |
---|
122 | n/a | ("d.a", "a", "from d.a import b"), |
---|
123 | n/a | (None, "a", "import b"), |
---|
124 | n/a | (None, "a", "import b, c, d")) |
---|
125 | n/a | for package, name, import_ in failing_tests: |
---|
126 | n/a | n = self.does_tree_import(package, name, import_ + "\n" + string) |
---|
127 | n/a | self.assertFalse(n) |
---|
128 | n/a | n = self.does_tree_import(package, name, string + "\n" + import_) |
---|
129 | n/a | self.assertFalse(n) |
---|
130 | n/a | |
---|
131 | n/a | passing_tests = (("a", "a", "from a import a"), |
---|
132 | n/a | ("x", "a", "from x import a"), |
---|
133 | n/a | ("x", "a", "from x import b, c, a, d"), |
---|
134 | n/a | ("x.b", "a", "from x.b import a"), |
---|
135 | n/a | ("x.b", "a", "from x.b import b, c, a, d"), |
---|
136 | n/a | (None, "a", "import a"), |
---|
137 | n/a | (None, "a", "import b, c, a, d")) |
---|
138 | n/a | for package, name, import_ in passing_tests: |
---|
139 | n/a | n = self.does_tree_import(package, name, import_ + "\n" + string) |
---|
140 | n/a | self.assertTrue(n) |
---|
141 | n/a | n = self.does_tree_import(package, name, string + "\n" + import_) |
---|
142 | n/a | self.assertTrue(n) |
---|
143 | n/a | |
---|
144 | n/a | def test_in_function(self): |
---|
145 | n/a | self.try_with("def foo():\n\tbar.baz()\n\tstart=3") |
---|
146 | n/a | |
---|
147 | n/a | class Test_find_binding(support.TestCase): |
---|
148 | n/a | def find_binding(self, name, string, package=None): |
---|
149 | n/a | return fixer_util.find_binding(name, parse(string), package) |
---|
150 | n/a | |
---|
151 | n/a | def test_simple_assignment(self): |
---|
152 | n/a | self.assertTrue(self.find_binding("a", "a = b")) |
---|
153 | n/a | self.assertTrue(self.find_binding("a", "a = [b, c, d]")) |
---|
154 | n/a | self.assertTrue(self.find_binding("a", "a = foo()")) |
---|
155 | n/a | self.assertTrue(self.find_binding("a", "a = foo().foo.foo[6][foo]")) |
---|
156 | n/a | self.assertFalse(self.find_binding("a", "foo = a")) |
---|
157 | n/a | self.assertFalse(self.find_binding("a", "foo = (a, b, c)")) |
---|
158 | n/a | |
---|
159 | n/a | def test_tuple_assignment(self): |
---|
160 | n/a | self.assertTrue(self.find_binding("a", "(a,) = b")) |
---|
161 | n/a | self.assertTrue(self.find_binding("a", "(a, b, c) = [b, c, d]")) |
---|
162 | n/a | self.assertTrue(self.find_binding("a", "(c, (d, a), b) = foo()")) |
---|
163 | n/a | self.assertTrue(self.find_binding("a", "(a, b) = foo().foo[6][foo]")) |
---|
164 | n/a | self.assertFalse(self.find_binding("a", "(foo, b) = (b, a)")) |
---|
165 | n/a | self.assertFalse(self.find_binding("a", "(foo, (b, c)) = (a, b, c)")) |
---|
166 | n/a | |
---|
167 | n/a | def test_list_assignment(self): |
---|
168 | n/a | self.assertTrue(self.find_binding("a", "[a] = b")) |
---|
169 | n/a | self.assertTrue(self.find_binding("a", "[a, b, c] = [b, c, d]")) |
---|
170 | n/a | self.assertTrue(self.find_binding("a", "[c, [d, a], b] = foo()")) |
---|
171 | n/a | self.assertTrue(self.find_binding("a", "[a, b] = foo().foo[a][foo]")) |
---|
172 | n/a | self.assertFalse(self.find_binding("a", "[foo, b] = (b, a)")) |
---|
173 | n/a | self.assertFalse(self.find_binding("a", "[foo, [b, c]] = (a, b, c)")) |
---|
174 | n/a | |
---|
175 | n/a | def test_invalid_assignments(self): |
---|
176 | n/a | self.assertFalse(self.find_binding("a", "foo.a = 5")) |
---|
177 | n/a | self.assertFalse(self.find_binding("a", "foo[a] = 5")) |
---|
178 | n/a | self.assertFalse(self.find_binding("a", "foo(a) = 5")) |
---|
179 | n/a | self.assertFalse(self.find_binding("a", "foo(a, b) = 5")) |
---|
180 | n/a | |
---|
181 | n/a | def test_simple_import(self): |
---|
182 | n/a | self.assertTrue(self.find_binding("a", "import a")) |
---|
183 | n/a | self.assertTrue(self.find_binding("a", "import b, c, a, d")) |
---|
184 | n/a | self.assertFalse(self.find_binding("a", "import b")) |
---|
185 | n/a | self.assertFalse(self.find_binding("a", "import b, c, d")) |
---|
186 | n/a | |
---|
187 | n/a | def test_from_import(self): |
---|
188 | n/a | self.assertTrue(self.find_binding("a", "from x import a")) |
---|
189 | n/a | self.assertTrue(self.find_binding("a", "from a import a")) |
---|
190 | n/a | self.assertTrue(self.find_binding("a", "from x import b, c, a, d")) |
---|
191 | n/a | self.assertTrue(self.find_binding("a", "from x.b import a")) |
---|
192 | n/a | self.assertTrue(self.find_binding("a", "from x.b import b, c, a, d")) |
---|
193 | n/a | self.assertFalse(self.find_binding("a", "from a import b")) |
---|
194 | n/a | self.assertFalse(self.find_binding("a", "from a.d import b")) |
---|
195 | n/a | self.assertFalse(self.find_binding("a", "from d.a import b")) |
---|
196 | n/a | |
---|
197 | n/a | def test_import_as(self): |
---|
198 | n/a | self.assertTrue(self.find_binding("a", "import b as a")) |
---|
199 | n/a | self.assertTrue(self.find_binding("a", "import b as a, c, a as f, d")) |
---|
200 | n/a | self.assertFalse(self.find_binding("a", "import a as f")) |
---|
201 | n/a | self.assertFalse(self.find_binding("a", "import b, c as f, d as e")) |
---|
202 | n/a | |
---|
203 | n/a | def test_from_import_as(self): |
---|
204 | n/a | self.assertTrue(self.find_binding("a", "from x import b as a")) |
---|
205 | n/a | self.assertTrue(self.find_binding("a", "from x import g as a, d as b")) |
---|
206 | n/a | self.assertTrue(self.find_binding("a", "from x.b import t as a")) |
---|
207 | n/a | self.assertTrue(self.find_binding("a", "from x.b import g as a, d")) |
---|
208 | n/a | self.assertFalse(self.find_binding("a", "from a import b as t")) |
---|
209 | n/a | self.assertFalse(self.find_binding("a", "from a.d import b as t")) |
---|
210 | n/a | self.assertFalse(self.find_binding("a", "from d.a import b as t")) |
---|
211 | n/a | |
---|
212 | n/a | def test_simple_import_with_package(self): |
---|
213 | n/a | self.assertTrue(self.find_binding("b", "import b")) |
---|
214 | n/a | self.assertTrue(self.find_binding("b", "import b, c, d")) |
---|
215 | n/a | self.assertFalse(self.find_binding("b", "import b", "b")) |
---|
216 | n/a | self.assertFalse(self.find_binding("b", "import b, c, d", "c")) |
---|
217 | n/a | |
---|
218 | n/a | def test_from_import_with_package(self): |
---|
219 | n/a | self.assertTrue(self.find_binding("a", "from x import a", "x")) |
---|
220 | n/a | self.assertTrue(self.find_binding("a", "from a import a", "a")) |
---|
221 | n/a | self.assertTrue(self.find_binding("a", "from x import *", "x")) |
---|
222 | n/a | self.assertTrue(self.find_binding("a", "from x import b, c, a, d", "x")) |
---|
223 | n/a | self.assertTrue(self.find_binding("a", "from x.b import a", "x.b")) |
---|
224 | n/a | self.assertTrue(self.find_binding("a", "from x.b import *", "x.b")) |
---|
225 | n/a | self.assertTrue(self.find_binding("a", "from x.b import b, c, a, d", "x.b")) |
---|
226 | n/a | self.assertFalse(self.find_binding("a", "from a import b", "a")) |
---|
227 | n/a | self.assertFalse(self.find_binding("a", "from a.d import b", "a.d")) |
---|
228 | n/a | self.assertFalse(self.find_binding("a", "from d.a import b", "a.d")) |
---|
229 | n/a | self.assertFalse(self.find_binding("a", "from x.y import *", "a.b")) |
---|
230 | n/a | |
---|
231 | n/a | def test_import_as_with_package(self): |
---|
232 | n/a | self.assertFalse(self.find_binding("a", "import b.c as a", "b.c")) |
---|
233 | n/a | self.assertFalse(self.find_binding("a", "import a as f", "f")) |
---|
234 | n/a | self.assertFalse(self.find_binding("a", "import a as f", "a")) |
---|
235 | n/a | |
---|
236 | n/a | def test_from_import_as_with_package(self): |
---|
237 | n/a | # Because it would take a lot of special-case code in the fixers |
---|
238 | n/a | # to deal with from foo import bar as baz, we'll simply always |
---|
239 | n/a | # fail if there is an "from ... import ... as ..." |
---|
240 | n/a | self.assertFalse(self.find_binding("a", "from x import b as a", "x")) |
---|
241 | n/a | self.assertFalse(self.find_binding("a", "from x import g as a, d as b", "x")) |
---|
242 | n/a | self.assertFalse(self.find_binding("a", "from x.b import t as a", "x.b")) |
---|
243 | n/a | self.assertFalse(self.find_binding("a", "from x.b import g as a, d", "x.b")) |
---|
244 | n/a | self.assertFalse(self.find_binding("a", "from a import b as t", "a")) |
---|
245 | n/a | self.assertFalse(self.find_binding("a", "from a import b as t", "b")) |
---|
246 | n/a | self.assertFalse(self.find_binding("a", "from a import b as t", "t")) |
---|
247 | n/a | |
---|
248 | n/a | def test_function_def(self): |
---|
249 | n/a | self.assertTrue(self.find_binding("a", "def a(): pass")) |
---|
250 | n/a | self.assertTrue(self.find_binding("a", "def a(b, c, d): pass")) |
---|
251 | n/a | self.assertTrue(self.find_binding("a", "def a(): b = 7")) |
---|
252 | n/a | self.assertFalse(self.find_binding("a", "def d(b, (c, a), e): pass")) |
---|
253 | n/a | self.assertFalse(self.find_binding("a", "def d(a=7): pass")) |
---|
254 | n/a | self.assertFalse(self.find_binding("a", "def d(a): pass")) |
---|
255 | n/a | self.assertFalse(self.find_binding("a", "def d(): a = 7")) |
---|
256 | n/a | |
---|
257 | n/a | s = """ |
---|
258 | n/a | def d(): |
---|
259 | n/a | def a(): |
---|
260 | n/a | pass""" |
---|
261 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
262 | n/a | |
---|
263 | n/a | def test_class_def(self): |
---|
264 | n/a | self.assertTrue(self.find_binding("a", "class a: pass")) |
---|
265 | n/a | self.assertTrue(self.find_binding("a", "class a(): pass")) |
---|
266 | n/a | self.assertTrue(self.find_binding("a", "class a(b): pass")) |
---|
267 | n/a | self.assertTrue(self.find_binding("a", "class a(b, c=8): pass")) |
---|
268 | n/a | self.assertFalse(self.find_binding("a", "class d: pass")) |
---|
269 | n/a | self.assertFalse(self.find_binding("a", "class d(a): pass")) |
---|
270 | n/a | self.assertFalse(self.find_binding("a", "class d(b, a=7): pass")) |
---|
271 | n/a | self.assertFalse(self.find_binding("a", "class d(b, *a): pass")) |
---|
272 | n/a | self.assertFalse(self.find_binding("a", "class d(b, **a): pass")) |
---|
273 | n/a | self.assertFalse(self.find_binding("a", "class d: a = 7")) |
---|
274 | n/a | |
---|
275 | n/a | s = """ |
---|
276 | n/a | class d(): |
---|
277 | n/a | class a(): |
---|
278 | n/a | pass""" |
---|
279 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
280 | n/a | |
---|
281 | n/a | def test_for(self): |
---|
282 | n/a | self.assertTrue(self.find_binding("a", "for a in r: pass")) |
---|
283 | n/a | self.assertTrue(self.find_binding("a", "for a, b in r: pass")) |
---|
284 | n/a | self.assertTrue(self.find_binding("a", "for (a, b) in r: pass")) |
---|
285 | n/a | self.assertTrue(self.find_binding("a", "for c, (a,) in r: pass")) |
---|
286 | n/a | self.assertTrue(self.find_binding("a", "for c, (a, b) in r: pass")) |
---|
287 | n/a | self.assertTrue(self.find_binding("a", "for c in r: a = c")) |
---|
288 | n/a | self.assertFalse(self.find_binding("a", "for c in a: pass")) |
---|
289 | n/a | |
---|
290 | n/a | def test_for_nested(self): |
---|
291 | n/a | s = """ |
---|
292 | n/a | for b in r: |
---|
293 | n/a | for a in b: |
---|
294 | n/a | pass""" |
---|
295 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
296 | n/a | |
---|
297 | n/a | s = """ |
---|
298 | n/a | for b in r: |
---|
299 | n/a | for a, c in b: |
---|
300 | n/a | pass""" |
---|
301 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
302 | n/a | |
---|
303 | n/a | s = """ |
---|
304 | n/a | for b in r: |
---|
305 | n/a | for (a, c) in b: |
---|
306 | n/a | pass""" |
---|
307 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
308 | n/a | |
---|
309 | n/a | s = """ |
---|
310 | n/a | for b in r: |
---|
311 | n/a | for (a,) in b: |
---|
312 | n/a | pass""" |
---|
313 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
314 | n/a | |
---|
315 | n/a | s = """ |
---|
316 | n/a | for b in r: |
---|
317 | n/a | for c, (a, d) in b: |
---|
318 | n/a | pass""" |
---|
319 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
320 | n/a | |
---|
321 | n/a | s = """ |
---|
322 | n/a | for b in r: |
---|
323 | n/a | for c in b: |
---|
324 | n/a | a = 7""" |
---|
325 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
326 | n/a | |
---|
327 | n/a | s = """ |
---|
328 | n/a | for b in r: |
---|
329 | n/a | for c in b: |
---|
330 | n/a | d = a""" |
---|
331 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
332 | n/a | |
---|
333 | n/a | s = """ |
---|
334 | n/a | for b in r: |
---|
335 | n/a | for c in a: |
---|
336 | n/a | d = 7""" |
---|
337 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
338 | n/a | |
---|
339 | n/a | def test_if(self): |
---|
340 | n/a | self.assertTrue(self.find_binding("a", "if b in r: a = c")) |
---|
341 | n/a | self.assertFalse(self.find_binding("a", "if a in r: d = e")) |
---|
342 | n/a | |
---|
343 | n/a | def test_if_nested(self): |
---|
344 | n/a | s = """ |
---|
345 | n/a | if b in r: |
---|
346 | n/a | if c in d: |
---|
347 | n/a | a = c""" |
---|
348 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
349 | n/a | |
---|
350 | n/a | s = """ |
---|
351 | n/a | if b in r: |
---|
352 | n/a | if c in d: |
---|
353 | n/a | c = a""" |
---|
354 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
355 | n/a | |
---|
356 | n/a | def test_while(self): |
---|
357 | n/a | self.assertTrue(self.find_binding("a", "while b in r: a = c")) |
---|
358 | n/a | self.assertFalse(self.find_binding("a", "while a in r: d = e")) |
---|
359 | n/a | |
---|
360 | n/a | def test_while_nested(self): |
---|
361 | n/a | s = """ |
---|
362 | n/a | while b in r: |
---|
363 | n/a | while c in d: |
---|
364 | n/a | a = c""" |
---|
365 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
366 | n/a | |
---|
367 | n/a | s = """ |
---|
368 | n/a | while b in r: |
---|
369 | n/a | while c in d: |
---|
370 | n/a | c = a""" |
---|
371 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
372 | n/a | |
---|
373 | n/a | def test_try_except(self): |
---|
374 | n/a | s = """ |
---|
375 | n/a | try: |
---|
376 | n/a | a = 6 |
---|
377 | n/a | except: |
---|
378 | n/a | b = 8""" |
---|
379 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
380 | n/a | |
---|
381 | n/a | s = """ |
---|
382 | n/a | try: |
---|
383 | n/a | b = 8 |
---|
384 | n/a | except: |
---|
385 | n/a | a = 6""" |
---|
386 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
387 | n/a | |
---|
388 | n/a | s = """ |
---|
389 | n/a | try: |
---|
390 | n/a | b = 8 |
---|
391 | n/a | except KeyError: |
---|
392 | n/a | pass |
---|
393 | n/a | except: |
---|
394 | n/a | a = 6""" |
---|
395 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
396 | n/a | |
---|
397 | n/a | s = """ |
---|
398 | n/a | try: |
---|
399 | n/a | b = 8 |
---|
400 | n/a | except: |
---|
401 | n/a | b = 6""" |
---|
402 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
403 | n/a | |
---|
404 | n/a | def test_try_except_nested(self): |
---|
405 | n/a | s = """ |
---|
406 | n/a | try: |
---|
407 | n/a | try: |
---|
408 | n/a | a = 6 |
---|
409 | n/a | except: |
---|
410 | n/a | pass |
---|
411 | n/a | except: |
---|
412 | n/a | b = 8""" |
---|
413 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
414 | n/a | |
---|
415 | n/a | s = """ |
---|
416 | n/a | try: |
---|
417 | n/a | b = 8 |
---|
418 | n/a | except: |
---|
419 | n/a | try: |
---|
420 | n/a | a = 6 |
---|
421 | n/a | except: |
---|
422 | n/a | pass""" |
---|
423 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
424 | n/a | |
---|
425 | n/a | s = """ |
---|
426 | n/a | try: |
---|
427 | n/a | b = 8 |
---|
428 | n/a | except: |
---|
429 | n/a | try: |
---|
430 | n/a | pass |
---|
431 | n/a | except: |
---|
432 | n/a | a = 6""" |
---|
433 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
434 | n/a | |
---|
435 | n/a | s = """ |
---|
436 | n/a | try: |
---|
437 | n/a | try: |
---|
438 | n/a | b = 8 |
---|
439 | n/a | except KeyError: |
---|
440 | n/a | pass |
---|
441 | n/a | except: |
---|
442 | n/a | a = 6 |
---|
443 | n/a | except: |
---|
444 | n/a | pass""" |
---|
445 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
446 | n/a | |
---|
447 | n/a | s = """ |
---|
448 | n/a | try: |
---|
449 | n/a | pass |
---|
450 | n/a | except: |
---|
451 | n/a | try: |
---|
452 | n/a | b = 8 |
---|
453 | n/a | except KeyError: |
---|
454 | n/a | pass |
---|
455 | n/a | except: |
---|
456 | n/a | a = 6""" |
---|
457 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
458 | n/a | |
---|
459 | n/a | s = """ |
---|
460 | n/a | try: |
---|
461 | n/a | b = 8 |
---|
462 | n/a | except: |
---|
463 | n/a | b = 6""" |
---|
464 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
465 | n/a | |
---|
466 | n/a | s = """ |
---|
467 | n/a | try: |
---|
468 | n/a | try: |
---|
469 | n/a | b = 8 |
---|
470 | n/a | except: |
---|
471 | n/a | c = d |
---|
472 | n/a | except: |
---|
473 | n/a | try: |
---|
474 | n/a | b = 6 |
---|
475 | n/a | except: |
---|
476 | n/a | t = 8 |
---|
477 | n/a | except: |
---|
478 | n/a | o = y""" |
---|
479 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
480 | n/a | |
---|
481 | n/a | def test_try_except_finally(self): |
---|
482 | n/a | s = """ |
---|
483 | n/a | try: |
---|
484 | n/a | c = 6 |
---|
485 | n/a | except: |
---|
486 | n/a | b = 8 |
---|
487 | n/a | finally: |
---|
488 | n/a | a = 9""" |
---|
489 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
490 | n/a | |
---|
491 | n/a | s = """ |
---|
492 | n/a | try: |
---|
493 | n/a | b = 8 |
---|
494 | n/a | finally: |
---|
495 | n/a | a = 6""" |
---|
496 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
497 | n/a | |
---|
498 | n/a | s = """ |
---|
499 | n/a | try: |
---|
500 | n/a | b = 8 |
---|
501 | n/a | finally: |
---|
502 | n/a | b = 6""" |
---|
503 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
504 | n/a | |
---|
505 | n/a | s = """ |
---|
506 | n/a | try: |
---|
507 | n/a | b = 8 |
---|
508 | n/a | except: |
---|
509 | n/a | b = 9 |
---|
510 | n/a | finally: |
---|
511 | n/a | b = 6""" |
---|
512 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
513 | n/a | |
---|
514 | n/a | def test_try_except_finally_nested(self): |
---|
515 | n/a | s = """ |
---|
516 | n/a | try: |
---|
517 | n/a | c = 6 |
---|
518 | n/a | except: |
---|
519 | n/a | b = 8 |
---|
520 | n/a | finally: |
---|
521 | n/a | try: |
---|
522 | n/a | a = 9 |
---|
523 | n/a | except: |
---|
524 | n/a | b = 9 |
---|
525 | n/a | finally: |
---|
526 | n/a | c = 9""" |
---|
527 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
528 | n/a | |
---|
529 | n/a | s = """ |
---|
530 | n/a | try: |
---|
531 | n/a | b = 8 |
---|
532 | n/a | finally: |
---|
533 | n/a | try: |
---|
534 | n/a | pass |
---|
535 | n/a | finally: |
---|
536 | n/a | a = 6""" |
---|
537 | n/a | self.assertTrue(self.find_binding("a", s)) |
---|
538 | n/a | |
---|
539 | n/a | s = """ |
---|
540 | n/a | try: |
---|
541 | n/a | b = 8 |
---|
542 | n/a | finally: |
---|
543 | n/a | try: |
---|
544 | n/a | b = 6 |
---|
545 | n/a | finally: |
---|
546 | n/a | b = 7""" |
---|
547 | n/a | self.assertFalse(self.find_binding("a", s)) |
---|
548 | n/a | |
---|
549 | n/a | class Test_touch_import(support.TestCase): |
---|
550 | n/a | |
---|
551 | n/a | def test_after_docstring(self): |
---|
552 | n/a | node = parse('"""foo"""\nbar()') |
---|
553 | n/a | fixer_util.touch_import(None, "foo", node) |
---|
554 | n/a | self.assertEqual(str(node), '"""foo"""\nimport foo\nbar()\n\n') |
---|
555 | n/a | |
---|
556 | n/a | def test_after_imports(self): |
---|
557 | n/a | node = parse('"""foo"""\nimport bar\nbar()') |
---|
558 | n/a | fixer_util.touch_import(None, "foo", node) |
---|
559 | n/a | self.assertEqual(str(node), '"""foo"""\nimport bar\nimport foo\nbar()\n\n') |
---|
560 | n/a | |
---|
561 | n/a | def test_beginning(self): |
---|
562 | n/a | node = parse('bar()') |
---|
563 | n/a | fixer_util.touch_import(None, "foo", node) |
---|
564 | n/a | self.assertEqual(str(node), 'import foo\nbar()\n\n') |
---|
565 | n/a | |
---|
566 | n/a | def test_from_import(self): |
---|
567 | n/a | node = parse('bar()') |
---|
568 | n/a | fixer_util.touch_import("html", "escape", node) |
---|
569 | n/a | self.assertEqual(str(node), 'from html import escape\nbar()\n\n') |
---|
570 | n/a | |
---|
571 | n/a | def test_name_import(self): |
---|
572 | n/a | node = parse('bar()') |
---|
573 | n/a | fixer_util.touch_import(None, "cgi", node) |
---|
574 | n/a | self.assertEqual(str(node), 'import cgi\nbar()\n\n') |
---|
575 | n/a | |
---|
576 | n/a | class Test_find_indentation(support.TestCase): |
---|
577 | n/a | |
---|
578 | n/a | def test_nothing(self): |
---|
579 | n/a | fi = fixer_util.find_indentation |
---|
580 | n/a | node = parse("node()") |
---|
581 | n/a | self.assertEqual(fi(node), "") |
---|
582 | n/a | node = parse("") |
---|
583 | n/a | self.assertEqual(fi(node), "") |
---|
584 | n/a | |
---|
585 | n/a | def test_simple(self): |
---|
586 | n/a | fi = fixer_util.find_indentation |
---|
587 | n/a | node = parse("def f():\n x()") |
---|
588 | n/a | self.assertEqual(fi(node), "") |
---|
589 | n/a | self.assertEqual(fi(node.children[0].children[4].children[2]), " ") |
---|
590 | n/a | node = parse("def f():\n x()\n y()") |
---|
591 | n/a | self.assertEqual(fi(node.children[0].children[4].children[4]), " ") |
---|