summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/tests/test_pytree.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lib2to3/tests/test_pytree.py')
-rwxr-xr-xLib/lib2to3/tests/test_pytree.py127
1 files changed, 66 insertions, 61 deletions
diff --git a/Lib/lib2to3/tests/test_pytree.py b/Lib/lib2to3/tests/test_pytree.py
index 9dc6e07..83a9b18 100755
--- a/Lib/lib2to3/tests/test_pytree.py
+++ b/Lib/lib2to3/tests/test_pytree.py
@@ -1,4 +1,3 @@
-#!/usr/bin/env python2.5
# Copyright 2006 Google, Inc. All Rights Reserved.
# Licensed to PSF under a Contributor Agreement.
@@ -10,11 +9,12 @@ more helpful than printing of (the first line of) the docstring,
especially when debugging a test.
"""
+import warnings
+
# Testing imports
from . import support
-# Local imports (XXX should become a package)
-from .. import pytree
+from lib2to3 import pytree
try:
sorted
@@ -28,34 +28,48 @@ class TestNodes(support.TestCase):
"""Unit tests for nodes (Base, Leaf, Node)."""
- def testBaseCantConstruct(self):
+ def test_deprecated_prefix_methods(self):
+ l = pytree.Leaf(100, "foo")
+ with warnings.catch_warnings(record=True) as w:
+ self.assertEqual(l.get_prefix(), "")
+ l.set_prefix("hi")
+ self.assertEqual(l.prefix, "hi")
+ self.assertEqual(len(w), 2)
+ for warning in w:
+ self.assertTrue(warning.category is DeprecationWarning)
+ self.assertEqual(str(w[0].message), "get_prefix() is deprecated; " \
+ "use the prefix property")
+ self.assertEqual(str(w[1].message), "set_prefix() is deprecated; " \
+ "use the prefix property")
+
+ def test_instantiate_base(self):
if __debug__:
# Test that instantiating Base() raises an AssertionError
self.assertRaises(AssertionError, pytree.Base)
- def testLeaf(self):
+ def test_leaf(self):
l1 = pytree.Leaf(100, "foo")
self.assertEqual(l1.type, 100)
self.assertEqual(l1.value, "foo")
- def testLeafRepr(self):
+ def test_leaf_repr(self):
l1 = pytree.Leaf(100, "foo")
self.assertEqual(repr(l1), "Leaf(100, 'foo')")
- def testLeafStr(self):
+ def test_leaf_str(self):
l1 = pytree.Leaf(100, "foo")
self.assertEqual(str(l1), "foo")
l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1)))
self.assertEqual(str(l2), " foo")
- def testLeafStrNumericValue(self):
+ def test_leaf_str_numeric_value(self):
# Make sure that the Leaf's value is stringified. Failing to
# do this can cause a TypeError in certain situations.
l1 = pytree.Leaf(2, 5)
- l1.set_prefix("foo_")
+ l1.prefix = "foo_"
self.assertEqual(str(l1), "foo_5")
- def testLeafEq(self):
+ def test_leaf_equality(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0)))
self.assertEqual(l1, l2)
@@ -64,67 +78,67 @@ class TestNodes(support.TestCase):
self.assertNotEqual(l1, l3)
self.assertNotEqual(l1, l4)
- def testLeafPrefix(self):
+ def test_leaf_prefix(self):
l1 = pytree.Leaf(100, "foo")
- self.assertEqual(l1.get_prefix(), "")
+ self.assertEqual(l1.prefix, "")
self.failIf(l1.was_changed)
- l1.set_prefix(" ##\n\n")
- self.assertEqual(l1.get_prefix(), " ##\n\n")
+ l1.prefix = " ##\n\n"
+ self.assertEqual(l1.prefix, " ##\n\n")
self.failUnless(l1.was_changed)
- def testNode(self):
+ def test_node(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(200, "bar")
n1 = pytree.Node(1000, [l1, l2])
self.assertEqual(n1.type, 1000)
self.assertEqual(n1.children, [l1, l2])
- def testNodeRepr(self):
+ def test_node_repr(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
n1 = pytree.Node(1000, [l1, l2])
self.assertEqual(repr(n1),
"Node(1000, [%s, %s])" % (repr(l1), repr(l2)))
- def testNodeStr(self):
+ def test_node_str(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
n1 = pytree.Node(1000, [l1, l2])
self.assertEqual(str(n1), "foo bar")
- def testNodePrefix(self):
+ def test_node_prefix(self):
l1 = pytree.Leaf(100, "foo")
- self.assertEqual(l1.get_prefix(), "")
+ self.assertEqual(l1.prefix, "")
n1 = pytree.Node(1000, [l1])
- self.assertEqual(n1.get_prefix(), "")
- n1.set_prefix(" ")
- self.assertEqual(n1.get_prefix(), " ")
- self.assertEqual(l1.get_prefix(), " ")
+ self.assertEqual(n1.prefix, "")
+ n1.prefix = " "
+ self.assertEqual(n1.prefix, " ")
+ self.assertEqual(l1.prefix, " ")
- def testGetSuffix(self):
+ def test_get_suffix(self):
l1 = pytree.Leaf(100, "foo", prefix="a")
l2 = pytree.Leaf(100, "bar", prefix="b")
n1 = pytree.Node(1000, [l1, l2])
- self.assertEqual(l1.get_suffix(), l2.get_prefix())
+ self.assertEqual(l1.get_suffix(), l2.prefix)
self.assertEqual(l2.get_suffix(), "")
self.assertEqual(n1.get_suffix(), "")
l3 = pytree.Leaf(100, "bar", prefix="c")
n2 = pytree.Node(1000, [n1, l3])
- self.assertEqual(n1.get_suffix(), l3.get_prefix())
+ self.assertEqual(n1.get_suffix(), l3.prefix)
self.assertEqual(l3.get_suffix(), "")
self.assertEqual(n2.get_suffix(), "")
- def testNodeEq(self):
+ def test_node_equality(self):
n1 = pytree.Node(1000, ())
n2 = pytree.Node(1000, [], context=(" ", (1, 0)))
self.assertEqual(n1, n2)
n3 = pytree.Node(1001, ())
self.assertNotEqual(n1, n3)
- def testNodeEqRecursive(self):
+ def test_node_recursive_equality(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "foo")
n1 = pytree.Node(1000, [l1])
@@ -134,7 +148,7 @@ class TestNodes(support.TestCase):
n3 = pytree.Node(1000, [l3])
self.assertNotEqual(n1, n3)
- def testReplace(self):
+ def test_replace(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "+")
l3 = pytree.Leaf(100, "bar")
@@ -148,7 +162,7 @@ class TestNodes(support.TestCase):
self.failUnless(isinstance(n1.children, list))
self.failUnless(n1.was_changed)
- def testReplaceWithList(self):
+ def test_replace_with_list(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "+")
l3 = pytree.Leaf(100, "bar")
@@ -158,34 +172,30 @@ class TestNodes(support.TestCase):
self.assertEqual(str(n1), "foo**bar")
self.failUnless(isinstance(n1.children, list))
- def testPostOrder(self):
+ def test_post_order(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "bar")
n1 = pytree.Node(1000, [l1, l2])
self.assertEqual(list(n1.post_order()), [l1, l2, n1])
- def testPreOrder(self):
+ def test_pre_order(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "bar")
n1 = pytree.Node(1000, [l1, l2])
self.assertEqual(list(n1.pre_order()), [n1, l1, l2])
- def testChangedLeaf(self):
+ def test_changed(self):
l1 = pytree.Leaf(100, "f")
self.failIf(l1.was_changed)
-
l1.changed()
self.failUnless(l1.was_changed)
- def testChangedNode(self):
l1 = pytree.Leaf(100, "f")
n1 = pytree.Node(1000, [l1])
self.failIf(n1.was_changed)
-
n1.changed()
self.failUnless(n1.was_changed)
- def testChangedRecursive(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "+")
l3 = pytree.Leaf(100, "bar")
@@ -200,23 +210,23 @@ class TestNodes(support.TestCase):
self.failUnless(n2.was_changed)
self.failIf(l1.was_changed)
- def testLeafConstructorPrefix(self):
+ def test_leaf_constructor_prefix(self):
for prefix in ("xyz_", ""):
l1 = pytree.Leaf(100, "self", prefix=prefix)
self.failUnless(str(l1), prefix + "self")
- self.assertEqual(l1.get_prefix(), prefix)
+ self.assertEqual(l1.prefix, prefix)
- def testNodeConstructorPrefix(self):
+ def test_node_constructor_prefix(self):
for prefix in ("xyz_", ""):
l1 = pytree.Leaf(100, "self")
l2 = pytree.Leaf(100, "foo", prefix="_")
n1 = pytree.Node(1000, [l1, l2], prefix=prefix)
self.failUnless(str(n1), prefix + "self_foo")
- self.assertEqual(n1.get_prefix(), prefix)
- self.assertEqual(l1.get_prefix(), prefix)
- self.assertEqual(l2.get_prefix(), "_")
+ self.assertEqual(n1.prefix, prefix)
+ self.assertEqual(l1.prefix, prefix)
+ self.assertEqual(l2.prefix, "_")
- def testRemove(self):
+ def test_remove(self):
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "foo")
n1 = pytree.Node(1000, [l1, l2])
@@ -239,7 +249,7 @@ class TestNodes(support.TestCase):
self.failUnless(n1.was_changed)
self.failUnless(n2.was_changed)
- def testRemoveParentless(self):
+ def test_remove_parentless(self):
n1 = pytree.Node(1000, [])
n1.remove()
self.assertEqual(n1.parent, None)
@@ -248,7 +258,7 @@ class TestNodes(support.TestCase):
l1.remove()
self.assertEqual(l1.parent, None)
- def testNodeSetChild(self):
+ def test_node_set_child(self):
l1 = pytree.Leaf(100, "foo")
n1 = pytree.Node(1000, [l1])
@@ -269,7 +279,7 @@ class TestNodes(support.TestCase):
# I don't care what it raises, so long as it's an exception
self.assertRaises(Exception, n1.set_child, 0, list)
- def testNodeInsertChild(self):
+ def test_node_insert_child(self):
l1 = pytree.Leaf(100, "foo")
n1 = pytree.Node(1000, [l1])
@@ -285,7 +295,7 @@ class TestNodes(support.TestCase):
# I don't care what it raises, so long as it's an exception
self.assertRaises(Exception, n1.insert_child, 0, list)
- def testNodeAppendChild(self):
+ def test_node_append_child(self):
n1 = pytree.Node(1000, [])
l1 = pytree.Leaf(100, "foo")
@@ -301,7 +311,7 @@ class TestNodes(support.TestCase):
# I don't care what it raises, so long as it's an exception
self.assertRaises(Exception, n1.append_child, list)
- def testNodeNextSibling(self):
+ def test_node_next_sibling(self):
n1 = pytree.Node(1000, [])
n2 = pytree.Node(1000, [])
p1 = pytree.Node(1000, [n1, n2])
@@ -310,7 +320,7 @@ class TestNodes(support.TestCase):
self.assertEqual(n2.next_sibling, None)
self.assertEqual(p1.next_sibling, None)
- def testLeafNextSibling(self):
+ def test_leaf_next_sibling(self):
l1 = pytree.Leaf(100, "a")
l2 = pytree.Leaf(100, "b")
p1 = pytree.Node(1000, [l1, l2])
@@ -319,7 +329,7 @@ class TestNodes(support.TestCase):
self.assertEqual(l2.next_sibling, None)
self.assertEqual(p1.next_sibling, None)
- def testNodePrevSibling(self):
+ def test_node_prev_sibling(self):
n1 = pytree.Node(1000, [])
n2 = pytree.Node(1000, [])
p1 = pytree.Node(1000, [n1, n2])
@@ -328,7 +338,7 @@ class TestNodes(support.TestCase):
self.assertEqual(n1.prev_sibling, None)
self.assertEqual(p1.prev_sibling, None)
- def testLeafPrevSibling(self):
+ def test_leaf_prev_sibling(self):
l1 = pytree.Leaf(100, "a")
l2 = pytree.Leaf(100, "b")
p1 = pytree.Node(1000, [l1, l2])
@@ -342,7 +352,7 @@ class TestPatterns(support.TestCase):
"""Unit tests for tree matching patterns."""
- def testBasicPatterns(self):
+ def test_basic_patterns(self):
# Build a tree
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "bar")
@@ -378,7 +388,7 @@ class TestPatterns(support.TestCase):
self.assertFalse(pn.match(l2, results=r))
self.assertEqual(r, {})
- def testWildcardPatterns(self):
+ def test_wildcard(self):
# Build a tree for testing
l1 = pytree.Leaf(100, "foo")
l2 = pytree.Leaf(100, "bar")
@@ -409,7 +419,7 @@ class TestPatterns(support.TestCase):
self.assert_(r["pl"] is l3)
r = {}
- def testGenerateMatches(self):
+ def test_generate_matches(self):
la = pytree.Leaf(1, "a")
lb = pytree.Leaf(1, "b")
lc = pytree.Leaf(1, "c")
@@ -439,7 +449,7 @@ class TestPatterns(support.TestCase):
for c in "abcdef":
self.assertEqual(r["p" + c], pytree.Leaf(1, c))
- def testHasKeyExample(self):
+ def test_has_key_example(self):
pattern = pytree.NodePattern(331,
(pytree.LeafPattern(7),
pytree.WildcardPattern(name="args"),
@@ -451,8 +461,3 @@ class TestPatterns(support.TestCase):
r = {}
self.assert_(pattern.match(node, r))
self.assertEqual(r["args"], [l2])
-
-
-if __name__ == "__main__":
- import __main__
- support.run_all_tests(__main__)