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

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

#countcontent
1n/a# Copyright 2006 Google, Inc. All Rights Reserved.
2n/a# Licensed to PSF under a Contributor Agreement.
3n/a
4n/a"""Unit tests for pytree.py.
5n/a
6n/aNOTE: Please *don't* add doc strings to individual test methods!
7n/aIn verbose mode, printing of the module, class and method name is much
8n/amore helpful than printing of (the first line of) the docstring,
9n/aespecially when debugging a test.
10n/a"""
11n/a
12n/a# Testing imports
13n/afrom . import support
14n/a
15n/afrom lib2to3 import pytree
16n/a
17n/atry:
18n/a sorted
19n/aexcept NameError:
20n/a def sorted(lst):
21n/a l = list(lst)
22n/a l.sort()
23n/a return l
24n/a
25n/aclass TestNodes(support.TestCase):
26n/a
27n/a """Unit tests for nodes (Base, Leaf, Node)."""
28n/a
29n/a def test_instantiate_base(self):
30n/a if __debug__:
31n/a # Test that instantiating Base() raises an AssertionError
32n/a self.assertRaises(AssertionError, pytree.Base)
33n/a
34n/a def test_leaf(self):
35n/a l1 = pytree.Leaf(100, "foo")
36n/a self.assertEqual(l1.type, 100)
37n/a self.assertEqual(l1.value, "foo")
38n/a
39n/a def test_leaf_repr(self):
40n/a l1 = pytree.Leaf(100, "foo")
41n/a self.assertEqual(repr(l1), "Leaf(100, 'foo')")
42n/a
43n/a def test_leaf_str(self):
44n/a l1 = pytree.Leaf(100, "foo")
45n/a self.assertEqual(str(l1), "foo")
46n/a l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1)))
47n/a self.assertEqual(str(l2), " foo")
48n/a
49n/a def test_leaf_str_numeric_value(self):
50n/a # Make sure that the Leaf's value is stringified. Failing to
51n/a # do this can cause a TypeError in certain situations.
52n/a l1 = pytree.Leaf(2, 5)
53n/a l1.prefix = "foo_"
54n/a self.assertEqual(str(l1), "foo_5")
55n/a
56n/a def test_leaf_equality(self):
57n/a l1 = pytree.Leaf(100, "foo")
58n/a l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0)))
59n/a self.assertEqual(l1, l2)
60n/a l3 = pytree.Leaf(101, "foo")
61n/a l4 = pytree.Leaf(100, "bar")
62n/a self.assertNotEqual(l1, l3)
63n/a self.assertNotEqual(l1, l4)
64n/a
65n/a def test_leaf_prefix(self):
66n/a l1 = pytree.Leaf(100, "foo")
67n/a self.assertEqual(l1.prefix, "")
68n/a self.assertFalse(l1.was_changed)
69n/a l1.prefix = " ##\n\n"
70n/a self.assertEqual(l1.prefix, " ##\n\n")
71n/a self.assertTrue(l1.was_changed)
72n/a
73n/a def test_node(self):
74n/a l1 = pytree.Leaf(100, "foo")
75n/a l2 = pytree.Leaf(200, "bar")
76n/a n1 = pytree.Node(1000, [l1, l2])
77n/a self.assertEqual(n1.type, 1000)
78n/a self.assertEqual(n1.children, [l1, l2])
79n/a
80n/a def test_node_repr(self):
81n/a l1 = pytree.Leaf(100, "foo")
82n/a l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
83n/a n1 = pytree.Node(1000, [l1, l2])
84n/a self.assertEqual(repr(n1),
85n/a "Node(1000, [%s, %s])" % (repr(l1), repr(l2)))
86n/a
87n/a def test_node_str(self):
88n/a l1 = pytree.Leaf(100, "foo")
89n/a l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
90n/a n1 = pytree.Node(1000, [l1, l2])
91n/a self.assertEqual(str(n1), "foo bar")
92n/a
93n/a def test_node_prefix(self):
94n/a l1 = pytree.Leaf(100, "foo")
95n/a self.assertEqual(l1.prefix, "")
96n/a n1 = pytree.Node(1000, [l1])
97n/a self.assertEqual(n1.prefix, "")
98n/a n1.prefix = " "
99n/a self.assertEqual(n1.prefix, " ")
100n/a self.assertEqual(l1.prefix, " ")
101n/a
102n/a def test_get_suffix(self):
103n/a l1 = pytree.Leaf(100, "foo", prefix="a")
104n/a l2 = pytree.Leaf(100, "bar", prefix="b")
105n/a n1 = pytree.Node(1000, [l1, l2])
106n/a
107n/a self.assertEqual(l1.get_suffix(), l2.prefix)
108n/a self.assertEqual(l2.get_suffix(), "")
109n/a self.assertEqual(n1.get_suffix(), "")
110n/a
111n/a l3 = pytree.Leaf(100, "bar", prefix="c")
112n/a n2 = pytree.Node(1000, [n1, l3])
113n/a
114n/a self.assertEqual(n1.get_suffix(), l3.prefix)
115n/a self.assertEqual(l3.get_suffix(), "")
116n/a self.assertEqual(n2.get_suffix(), "")
117n/a
118n/a def test_node_equality(self):
119n/a n1 = pytree.Node(1000, ())
120n/a n2 = pytree.Node(1000, [], context=(" ", (1, 0)))
121n/a self.assertEqual(n1, n2)
122n/a n3 = pytree.Node(1001, ())
123n/a self.assertNotEqual(n1, n3)
124n/a
125n/a def test_node_recursive_equality(self):
126n/a l1 = pytree.Leaf(100, "foo")
127n/a l2 = pytree.Leaf(100, "foo")
128n/a n1 = pytree.Node(1000, [l1])
129n/a n2 = pytree.Node(1000, [l2])
130n/a self.assertEqual(n1, n2)
131n/a l3 = pytree.Leaf(100, "bar")
132n/a n3 = pytree.Node(1000, [l3])
133n/a self.assertNotEqual(n1, n3)
134n/a
135n/a def test_replace(self):
136n/a l1 = pytree.Leaf(100, "foo")
137n/a l2 = pytree.Leaf(100, "+")
138n/a l3 = pytree.Leaf(100, "bar")
139n/a n1 = pytree.Node(1000, [l1, l2, l3])
140n/a self.assertEqual(n1.children, [l1, l2, l3])
141n/a self.assertIsInstance(n1.children, list)
142n/a self.assertFalse(n1.was_changed)
143n/a l2new = pytree.Leaf(100, "-")
144n/a l2.replace(l2new)
145n/a self.assertEqual(n1.children, [l1, l2new, l3])
146n/a self.assertIsInstance(n1.children, list)
147n/a self.assertTrue(n1.was_changed)
148n/a
149n/a def test_replace_with_list(self):
150n/a l1 = pytree.Leaf(100, "foo")
151n/a l2 = pytree.Leaf(100, "+")
152n/a l3 = pytree.Leaf(100, "bar")
153n/a n1 = pytree.Node(1000, [l1, l2, l3])
154n/a
155n/a l2.replace([pytree.Leaf(100, "*"), pytree.Leaf(100, "*")])
156n/a self.assertEqual(str(n1), "foo**bar")
157n/a self.assertIsInstance(n1.children, list)
158n/a
159n/a def test_leaves(self):
160n/a l1 = pytree.Leaf(100, "foo")
161n/a l2 = pytree.Leaf(100, "bar")
162n/a l3 = pytree.Leaf(100, "fooey")
163n/a n2 = pytree.Node(1000, [l1, l2])
164n/a n3 = pytree.Node(1000, [l3])
165n/a n1 = pytree.Node(1000, [n2, n3])
166n/a
167n/a self.assertEqual(list(n1.leaves()), [l1, l2, l3])
168n/a
169n/a def test_depth(self):
170n/a l1 = pytree.Leaf(100, "foo")
171n/a l2 = pytree.Leaf(100, "bar")
172n/a n2 = pytree.Node(1000, [l1, l2])
173n/a n3 = pytree.Node(1000, [])
174n/a n1 = pytree.Node(1000, [n2, n3])
175n/a
176n/a self.assertEqual(l1.depth(), 2)
177n/a self.assertEqual(n3.depth(), 1)
178n/a self.assertEqual(n1.depth(), 0)
179n/a
180n/a def test_post_order(self):
181n/a l1 = pytree.Leaf(100, "foo")
182n/a l2 = pytree.Leaf(100, "bar")
183n/a l3 = pytree.Leaf(100, "fooey")
184n/a c1 = pytree.Node(1000, [l1, l2])
185n/a n1 = pytree.Node(1000, [c1, l3])
186n/a self.assertEqual(list(n1.post_order()), [l1, l2, c1, l3, n1])
187n/a
188n/a def test_pre_order(self):
189n/a l1 = pytree.Leaf(100, "foo")
190n/a l2 = pytree.Leaf(100, "bar")
191n/a l3 = pytree.Leaf(100, "fooey")
192n/a c1 = pytree.Node(1000, [l1, l2])
193n/a n1 = pytree.Node(1000, [c1, l3])
194n/a self.assertEqual(list(n1.pre_order()), [n1, c1, l1, l2, l3])
195n/a
196n/a def test_changed(self):
197n/a l1 = pytree.Leaf(100, "f")
198n/a self.assertFalse(l1.was_changed)
199n/a l1.changed()
200n/a self.assertTrue(l1.was_changed)
201n/a
202n/a l1 = pytree.Leaf(100, "f")
203n/a n1 = pytree.Node(1000, [l1])
204n/a self.assertFalse(n1.was_changed)
205n/a n1.changed()
206n/a self.assertTrue(n1.was_changed)
207n/a
208n/a l1 = pytree.Leaf(100, "foo")
209n/a l2 = pytree.Leaf(100, "+")
210n/a l3 = pytree.Leaf(100, "bar")
211n/a n1 = pytree.Node(1000, [l1, l2, l3])
212n/a n2 = pytree.Node(1000, [n1])
213n/a self.assertFalse(l1.was_changed)
214n/a self.assertFalse(n1.was_changed)
215n/a self.assertFalse(n2.was_changed)
216n/a
217n/a n1.changed()
218n/a self.assertTrue(n1.was_changed)
219n/a self.assertTrue(n2.was_changed)
220n/a self.assertFalse(l1.was_changed)
221n/a
222n/a def test_leaf_constructor_prefix(self):
223n/a for prefix in ("xyz_", ""):
224n/a l1 = pytree.Leaf(100, "self", prefix=prefix)
225n/a self.assertTrue(str(l1), prefix + "self")
226n/a self.assertEqual(l1.prefix, prefix)
227n/a
228n/a def test_node_constructor_prefix(self):
229n/a for prefix in ("xyz_", ""):
230n/a l1 = pytree.Leaf(100, "self")
231n/a l2 = pytree.Leaf(100, "foo", prefix="_")
232n/a n1 = pytree.Node(1000, [l1, l2], prefix=prefix)
233n/a self.assertTrue(str(n1), prefix + "self_foo")
234n/a self.assertEqual(n1.prefix, prefix)
235n/a self.assertEqual(l1.prefix, prefix)
236n/a self.assertEqual(l2.prefix, "_")
237n/a
238n/a def test_remove(self):
239n/a l1 = pytree.Leaf(100, "foo")
240n/a l2 = pytree.Leaf(100, "foo")
241n/a n1 = pytree.Node(1000, [l1, l2])
242n/a n2 = pytree.Node(1000, [n1])
243n/a
244n/a self.assertEqual(n1.remove(), 0)
245n/a self.assertEqual(n2.children, [])
246n/a self.assertEqual(l1.parent, n1)
247n/a self.assertEqual(n1.parent, None)
248n/a self.assertEqual(n2.parent, None)
249n/a self.assertFalse(n1.was_changed)
250n/a self.assertTrue(n2.was_changed)
251n/a
252n/a self.assertEqual(l2.remove(), 1)
253n/a self.assertEqual(l1.remove(), 0)
254n/a self.assertEqual(n1.children, [])
255n/a self.assertEqual(l1.parent, None)
256n/a self.assertEqual(n1.parent, None)
257n/a self.assertEqual(n2.parent, None)
258n/a self.assertTrue(n1.was_changed)
259n/a self.assertTrue(n2.was_changed)
260n/a
261n/a def test_remove_parentless(self):
262n/a n1 = pytree.Node(1000, [])
263n/a n1.remove()
264n/a self.assertEqual(n1.parent, None)
265n/a
266n/a l1 = pytree.Leaf(100, "foo")
267n/a l1.remove()
268n/a self.assertEqual(l1.parent, None)
269n/a
270n/a def test_node_set_child(self):
271n/a l1 = pytree.Leaf(100, "foo")
272n/a n1 = pytree.Node(1000, [l1])
273n/a
274n/a l2 = pytree.Leaf(100, "bar")
275n/a n1.set_child(0, l2)
276n/a self.assertEqual(l1.parent, None)
277n/a self.assertEqual(l2.parent, n1)
278n/a self.assertEqual(n1.children, [l2])
279n/a
280n/a n2 = pytree.Node(1000, [l1])
281n/a n2.set_child(0, n1)
282n/a self.assertEqual(l1.parent, None)
283n/a self.assertEqual(n1.parent, n2)
284n/a self.assertEqual(n2.parent, None)
285n/a self.assertEqual(n2.children, [n1])
286n/a
287n/a self.assertRaises(IndexError, n1.set_child, 4, l2)
288n/a # I don't care what it raises, so long as it's an exception
289n/a self.assertRaises(Exception, n1.set_child, 0, list)
290n/a
291n/a def test_node_insert_child(self):
292n/a l1 = pytree.Leaf(100, "foo")
293n/a n1 = pytree.Node(1000, [l1])
294n/a
295n/a l2 = pytree.Leaf(100, "bar")
296n/a n1.insert_child(0, l2)
297n/a self.assertEqual(l2.parent, n1)
298n/a self.assertEqual(n1.children, [l2, l1])
299n/a
300n/a l3 = pytree.Leaf(100, "abc")
301n/a n1.insert_child(2, l3)
302n/a self.assertEqual(n1.children, [l2, l1, l3])
303n/a
304n/a # I don't care what it raises, so long as it's an exception
305n/a self.assertRaises(Exception, n1.insert_child, 0, list)
306n/a
307n/a def test_node_append_child(self):
308n/a n1 = pytree.Node(1000, [])
309n/a
310n/a l1 = pytree.Leaf(100, "foo")
311n/a n1.append_child(l1)
312n/a self.assertEqual(l1.parent, n1)
313n/a self.assertEqual(n1.children, [l1])
314n/a
315n/a l2 = pytree.Leaf(100, "bar")
316n/a n1.append_child(l2)
317n/a self.assertEqual(l2.parent, n1)
318n/a self.assertEqual(n1.children, [l1, l2])
319n/a
320n/a # I don't care what it raises, so long as it's an exception
321n/a self.assertRaises(Exception, n1.append_child, list)
322n/a
323n/a def test_node_next_sibling(self):
324n/a n1 = pytree.Node(1000, [])
325n/a n2 = pytree.Node(1000, [])
326n/a p1 = pytree.Node(1000, [n1, n2])
327n/a
328n/a self.assertIs(n1.next_sibling, n2)
329n/a self.assertEqual(n2.next_sibling, None)
330n/a self.assertEqual(p1.next_sibling, None)
331n/a
332n/a def test_leaf_next_sibling(self):
333n/a l1 = pytree.Leaf(100, "a")
334n/a l2 = pytree.Leaf(100, "b")
335n/a p1 = pytree.Node(1000, [l1, l2])
336n/a
337n/a self.assertIs(l1.next_sibling, l2)
338n/a self.assertEqual(l2.next_sibling, None)
339n/a self.assertEqual(p1.next_sibling, None)
340n/a
341n/a def test_node_prev_sibling(self):
342n/a n1 = pytree.Node(1000, [])
343n/a n2 = pytree.Node(1000, [])
344n/a p1 = pytree.Node(1000, [n1, n2])
345n/a
346n/a self.assertIs(n2.prev_sibling, n1)
347n/a self.assertEqual(n1.prev_sibling, None)
348n/a self.assertEqual(p1.prev_sibling, None)
349n/a
350n/a def test_leaf_prev_sibling(self):
351n/a l1 = pytree.Leaf(100, "a")
352n/a l2 = pytree.Leaf(100, "b")
353n/a p1 = pytree.Node(1000, [l1, l2])
354n/a
355n/a self.assertIs(l2.prev_sibling, l1)
356n/a self.assertEqual(l1.prev_sibling, None)
357n/a self.assertEqual(p1.prev_sibling, None)
358n/a
359n/a
360n/aclass TestPatterns(support.TestCase):
361n/a
362n/a """Unit tests for tree matching patterns."""
363n/a
364n/a def test_basic_patterns(self):
365n/a # Build a tree
366n/a l1 = pytree.Leaf(100, "foo")
367n/a l2 = pytree.Leaf(100, "bar")
368n/a l3 = pytree.Leaf(100, "foo")
369n/a n1 = pytree.Node(1000, [l1, l2])
370n/a n2 = pytree.Node(1000, [l3])
371n/a root = pytree.Node(1000, [n1, n2])
372n/a # Build a pattern matching a leaf
373n/a pl = pytree.LeafPattern(100, "foo", name="pl")
374n/a r = {}
375n/a self.assertFalse(pl.match(root, results=r))
376n/a self.assertEqual(r, {})
377n/a self.assertFalse(pl.match(n1, results=r))
378n/a self.assertEqual(r, {})
379n/a self.assertFalse(pl.match(n2, results=r))
380n/a self.assertEqual(r, {})
381n/a self.assertTrue(pl.match(l1, results=r))
382n/a self.assertEqual(r, {"pl": l1})
383n/a r = {}
384n/a self.assertFalse(pl.match(l2, results=r))
385n/a self.assertEqual(r, {})
386n/a # Build a pattern matching a node
387n/a pn = pytree.NodePattern(1000, [pl], name="pn")
388n/a self.assertFalse(pn.match(root, results=r))
389n/a self.assertEqual(r, {})
390n/a self.assertFalse(pn.match(n1, results=r))
391n/a self.assertEqual(r, {})
392n/a self.assertTrue(pn.match(n2, results=r))
393n/a self.assertEqual(r, {"pn": n2, "pl": l3})
394n/a r = {}
395n/a self.assertFalse(pn.match(l1, results=r))
396n/a self.assertEqual(r, {})
397n/a self.assertFalse(pn.match(l2, results=r))
398n/a self.assertEqual(r, {})
399n/a
400n/a def test_wildcard(self):
401n/a # Build a tree for testing
402n/a l1 = pytree.Leaf(100, "foo")
403n/a l2 = pytree.Leaf(100, "bar")
404n/a l3 = pytree.Leaf(100, "foo")
405n/a n1 = pytree.Node(1000, [l1, l2])
406n/a n2 = pytree.Node(1000, [l3])
407n/a root = pytree.Node(1000, [n1, n2])
408n/a # Build a pattern
409n/a pl = pytree.LeafPattern(100, "foo", name="pl")
410n/a pn = pytree.NodePattern(1000, [pl], name="pn")
411n/a pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
412n/a r = {}
413n/a self.assertFalse(pw.match_seq([root], r))
414n/a self.assertEqual(r, {})
415n/a self.assertFalse(pw.match_seq([n1], r))
416n/a self.assertEqual(r, {})
417n/a self.assertTrue(pw.match_seq([n2], r))
418n/a # These are easier to debug
419n/a self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
420n/a self.assertEqual(r["pl"], l1)
421n/a self.assertEqual(r["pn"], n2)
422n/a self.assertEqual(r["pw"], [n2])
423n/a # But this is equivalent
424n/a self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
425n/a r = {}
426n/a self.assertTrue(pw.match_seq([l1, l3], r))
427n/a self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
428n/a self.assertIs(r["pl"], l3)
429n/a r = {}
430n/a
431n/a def test_generate_matches(self):
432n/a la = pytree.Leaf(1, "a")
433n/a lb = pytree.Leaf(1, "b")
434n/a lc = pytree.Leaf(1, "c")
435n/a ld = pytree.Leaf(1, "d")
436n/a le = pytree.Leaf(1, "e")
437n/a lf = pytree.Leaf(1, "f")
438n/a leaves = [la, lb, lc, ld, le, lf]
439n/a root = pytree.Node(1000, leaves)
440n/a pa = pytree.LeafPattern(1, "a", "pa")
441n/a pb = pytree.LeafPattern(1, "b", "pb")
442n/a pc = pytree.LeafPattern(1, "c", "pc")
443n/a pd = pytree.LeafPattern(1, "d", "pd")
444n/a pe = pytree.LeafPattern(1, "e", "pe")
445n/a pf = pytree.LeafPattern(1, "f", "pf")
446n/a pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
447n/a [pa, pb], [pc, pd], [pe, pf]],
448n/a min=1, max=4, name="pw")
449n/a self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
450n/a [3, 5, 2, 4, 6])
451n/a pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
452n/a matches = list(pytree.generate_matches([pr], [root]))
453n/a self.assertEqual(len(matches), 1)
454n/a c, r = matches[0]
455n/a self.assertEqual(c, 1)
456n/a self.assertEqual(str(r["pr"]), "abcdef")
457n/a self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
458n/a for c in "abcdef":
459n/a self.assertEqual(r["p" + c], pytree.Leaf(1, c))
460n/a
461n/a def test_has_key_example(self):
462n/a pattern = pytree.NodePattern(331,
463n/a (pytree.LeafPattern(7),
464n/a pytree.WildcardPattern(name="args"),
465n/a pytree.LeafPattern(8)))
466n/a l1 = pytree.Leaf(7, "(")
467n/a l2 = pytree.Leaf(3, "x")
468n/a l3 = pytree.Leaf(8, ")")
469n/a node = pytree.Node(331, [l1, l2, l3])
470n/a r = {}
471n/a self.assertTrue(pattern.match(node, r))
472n/a self.assertEqual(r["args"], [l2])