ยปCore Development>Code coverage>Lib/lib2to3/tests/test_util.py

Python code coverage for Lib/lib2to3/tests/test_util.py

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