Back to index

python3.2  3.2.2
Public Member Functions
lib2to3.tests.test_pytree.TestPatterns Class Reference

List of all members.

Public Member Functions

def test_basic_patterns
def test_wildcard
def test_generate_matches
def test_has_key_example

Detailed Description

Unit tests for tree matching patterns.

Definition at line 382 of file test_pytree.py.


Member Function Documentation

Definition at line 386 of file test_pytree.py.

00386 
00387     def test_basic_patterns(self):
00388         # Build a tree
00389         l1 = pytree.Leaf(100, "foo")
00390         l2 = pytree.Leaf(100, "bar")
00391         l3 = pytree.Leaf(100, "foo")
00392         n1 = pytree.Node(1000, [l1, l2])
00393         n2 = pytree.Node(1000, [l3])
00394         root = pytree.Node(1000, [n1, n2])
00395         # Build a pattern matching a leaf
00396         pl = pytree.LeafPattern(100, "foo", name="pl")
00397         r = {}
00398         self.assertFalse(pl.match(root, results=r))
00399         self.assertEqual(r, {})
00400         self.assertFalse(pl.match(n1, results=r))
00401         self.assertEqual(r, {})
00402         self.assertFalse(pl.match(n2, results=r))
00403         self.assertEqual(r, {})
00404         self.assertTrue(pl.match(l1, results=r))
00405         self.assertEqual(r, {"pl": l1})
00406         r = {}
00407         self.assertFalse(pl.match(l2, results=r))
00408         self.assertEqual(r, {})
00409         # Build a pattern matching a node
00410         pn = pytree.NodePattern(1000, [pl], name="pn")
00411         self.assertFalse(pn.match(root, results=r))
00412         self.assertEqual(r, {})
00413         self.assertFalse(pn.match(n1, results=r))
00414         self.assertEqual(r, {})
00415         self.assertTrue(pn.match(n2, results=r))
00416         self.assertEqual(r, {"pn": n2, "pl": l3})
00417         r = {}
00418         self.assertFalse(pn.match(l1, results=r))
00419         self.assertEqual(r, {})
00420         self.assertFalse(pn.match(l2, results=r))
00421         self.assertEqual(r, {})

Here is the call graph for this function:

Definition at line 453 of file test_pytree.py.

00453 
00454     def test_generate_matches(self):
00455         la = pytree.Leaf(1, "a")
00456         lb = pytree.Leaf(1, "b")
00457         lc = pytree.Leaf(1, "c")
00458         ld = pytree.Leaf(1, "d")
00459         le = pytree.Leaf(1, "e")
00460         lf = pytree.Leaf(1, "f")
00461         leaves = [la, lb, lc, ld, le, lf]
00462         root = pytree.Node(1000, leaves)
00463         pa = pytree.LeafPattern(1, "a", "pa")
00464         pb = pytree.LeafPattern(1, "b", "pb")
00465         pc = pytree.LeafPattern(1, "c", "pc")
00466         pd = pytree.LeafPattern(1, "d", "pd")
00467         pe = pytree.LeafPattern(1, "e", "pe")
00468         pf = pytree.LeafPattern(1, "f", "pf")
00469         pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
00470                                      [pa, pb], [pc, pd], [pe, pf]],
00471                                     min=1, max=4, name="pw")
00472         self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
00473                          [3, 5, 2, 4, 6])
00474         pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
00475         matches = list(pytree.generate_matches([pr], [root]))
00476         self.assertEqual(len(matches), 1)
00477         c, r = matches[0]
00478         self.assertEqual(c, 1)
00479         self.assertEqual(str(r["pr"]), "abcdef")
00480         self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
00481         for c in "abcdef":
00482             self.assertEqual(r["p" + c], pytree.Leaf(1, c))

Here is the call graph for this function:

Definition at line 483 of file test_pytree.py.

00483 
00484     def test_has_key_example(self):
00485         pattern = pytree.NodePattern(331,
00486                                      (pytree.LeafPattern(7),
00487                                       pytree.WildcardPattern(name="args"),
00488                                       pytree.LeafPattern(8)))
00489         l1 = pytree.Leaf(7, "(")
00490         l2 = pytree.Leaf(3, "x")
00491         l3 = pytree.Leaf(8, ")")
00492         node = pytree.Node(331, [l1, l2, l3])
00493         r = {}
00494         self.assertTrue(pattern.match(node, r))
00495         self.assertEqual(r["args"], [l2])

Here is the call graph for this function:

Definition at line 422 of file test_pytree.py.

00422 
00423     def test_wildcard(self):
00424         # Build a tree for testing
00425         l1 = pytree.Leaf(100, "foo")
00426         l2 = pytree.Leaf(100, "bar")
00427         l3 = pytree.Leaf(100, "foo")
00428         n1 = pytree.Node(1000, [l1, l2])
00429         n2 = pytree.Node(1000, [l3])
00430         root = pytree.Node(1000, [n1, n2])
00431         # Build a pattern
00432         pl = pytree.LeafPattern(100, "foo", name="pl")
00433         pn = pytree.NodePattern(1000, [pl], name="pn")
00434         pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
00435         r = {}
00436         self.assertFalse(pw.match_seq([root], r))
00437         self.assertEqual(r, {})
00438         self.assertFalse(pw.match_seq([n1], r))
00439         self.assertEqual(r, {})
00440         self.assertTrue(pw.match_seq([n2], r))
00441         # These are easier to debug
00442         self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
00443         self.assertEqual(r["pl"], l1)
00444         self.assertEqual(r["pn"], n2)
00445         self.assertEqual(r["pw"], [n2])
00446         # But this is equivalent
00447         self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
00448         r = {}
00449         self.assertTrue(pw.match_seq([l1, l3], r))
00450         self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
00451         self.assertTrue(r["pl"] is l3)
00452         r = {}

Here is the call graph for this function:


The documentation for this class was generated from the following file: