Back to index

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

List of all members.

Public Member Functions

def test_deprecated_prefix_methods
def test_instantiate_base
def test_leaf
def test_leaf_repr
def test_leaf_str
def test_leaf_str_numeric_value
def test_leaf_equality
def test_leaf_prefix
def test_node
def test_node_repr
def test_node_str
def test_node_prefix
def test_get_suffix
def test_node_equality
def test_node_recursive_equality
def test_replace
def test_replace_with_list
def test_leaves
def test_depth
def test_post_order
def test_pre_order
def test_changed
def test_leaf_constructor_prefix
def test_node_constructor_prefix
def test_remove
def test_remove_parentless
def test_node_set_child
def test_node_insert_child
def test_node_append_child
def test_node_next_sibling
def test_leaf_next_sibling
def test_node_prev_sibling
def test_leaf_prev_sibling

Detailed Description

Unit tests for nodes (Base, Leaf, Node).

Definition at line 30 of file test_pytree.py.


Member Function Documentation

Definition at line 218 of file test_pytree.py.

00218 
00219     def test_changed(self):
00220         l1 = pytree.Leaf(100, "f")
00221         self.assertFalse(l1.was_changed)
00222         l1.changed()
00223         self.assertTrue(l1.was_changed)
00224 
00225         l1 = pytree.Leaf(100, "f")
00226         n1 = pytree.Node(1000, [l1])
00227         self.assertFalse(n1.was_changed)
00228         n1.changed()
00229         self.assertTrue(n1.was_changed)
00230 
00231         l1 = pytree.Leaf(100, "foo")
00232         l2 = pytree.Leaf(100, "+")
00233         l3 = pytree.Leaf(100, "bar")
00234         n1 = pytree.Node(1000, [l1, l2, l3])
00235         n2 = pytree.Node(1000, [n1])
00236         self.assertFalse(l1.was_changed)
00237         self.assertFalse(n1.was_changed)
00238         self.assertFalse(n2.was_changed)
00239 
00240         n1.changed()
00241         self.assertTrue(n1.was_changed)
00242         self.assertTrue(n2.was_changed)
00243         self.assertFalse(l1.was_changed)

Here is the call graph for this function:

Definition at line 36 of file test_pytree.py.

00036 
00037         def test_deprecated_prefix_methods(self):
00038             l = pytree.Leaf(100, "foo")
00039             with warnings.catch_warnings(record=True) as w:
00040                 warnings.simplefilter("always", DeprecationWarning)
00041                 self.assertEqual(l.get_prefix(), "")
00042                 l.set_prefix("hi")
00043             self.assertEqual(l.prefix, "hi")
00044             self.assertEqual(len(w), 2)
00045             for warning in w:
00046                 self.assertTrue(warning.category is DeprecationWarning)
00047             self.assertEqual(str(w[0].message), "get_prefix() is deprecated; " \
00048                                  "use the prefix property")
00049             self.assertEqual(str(w[1].message), "set_prefix() is deprecated; " \
00050                                  "use the prefix property")

Here is the call graph for this function:

Definition at line 191 of file test_pytree.py.

00191 
00192     def test_depth(self):
00193         l1 = pytree.Leaf(100, "foo")
00194         l2 = pytree.Leaf(100, "bar")
00195         n2 = pytree.Node(1000, [l1, l2])
00196         n3 = pytree.Node(1000, [])
00197         n1 = pytree.Node(1000, [n2, n3])
00198 
00199         self.assertEqual(l1.depth(), 2)
00200         self.assertEqual(n3.depth(), 1)
00201         self.assertEqual(n1.depth(), 0)

Here is the call graph for this function:

Definition at line 124 of file test_pytree.py.

00124 
00125     def test_get_suffix(self):
00126         l1 = pytree.Leaf(100, "foo", prefix="a")
00127         l2 = pytree.Leaf(100, "bar", prefix="b")
00128         n1 = pytree.Node(1000, [l1, l2])
00129 
00130         self.assertEqual(l1.get_suffix(), l2.prefix)
00131         self.assertEqual(l2.get_suffix(), "")
00132         self.assertEqual(n1.get_suffix(), "")
00133 
00134         l3 = pytree.Leaf(100, "bar", prefix="c")
00135         n2 = pytree.Node(1000, [n1, l3])
00136 
00137         self.assertEqual(n1.get_suffix(), l3.prefix)
00138         self.assertEqual(l3.get_suffix(), "")
00139         self.assertEqual(n2.get_suffix(), "")

Here is the call graph for this function:

Definition at line 51 of file test_pytree.py.

00051 
00052     def test_instantiate_base(self):
00053         if __debug__:
00054             # Test that instantiating Base() raises an AssertionError
00055             self.assertRaises(AssertionError, pytree.Base)

Here is the call graph for this function:

Definition at line 56 of file test_pytree.py.

00056 
00057     def test_leaf(self):
00058         l1 = pytree.Leaf(100, "foo")
00059         self.assertEqual(l1.type, 100)
00060         self.assertEqual(l1.value, "foo")

Here is the call graph for this function:

Definition at line 244 of file test_pytree.py.

00244 
00245     def test_leaf_constructor_prefix(self):
00246         for prefix in ("xyz_", ""):
00247             l1 = pytree.Leaf(100, "self", prefix=prefix)
00248             self.assertTrue(str(l1), prefix + "self")
00249             self.assertEqual(l1.prefix, prefix)

Here is the call graph for this function:

Definition at line 78 of file test_pytree.py.

00078 
00079     def test_leaf_equality(self):
00080         l1 = pytree.Leaf(100, "foo")
00081         l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0)))
00082         self.assertEqual(l1, l2)
00083         l3 = pytree.Leaf(101, "foo")
00084         l4 = pytree.Leaf(100, "bar")
00085         self.assertNotEqual(l1, l3)
00086         self.assertNotEqual(l1, l4)

Here is the call graph for this function:

Definition at line 354 of file test_pytree.py.

00354 
00355     def test_leaf_next_sibling(self):
00356         l1 = pytree.Leaf(100, "a")
00357         l2 = pytree.Leaf(100, "b")
00358         p1 = pytree.Node(1000, [l1, l2])
00359 
00360         self.assertTrue(l1.next_sibling is l2)
00361         self.assertEqual(l2.next_sibling, None)
00362         self.assertEqual(p1.next_sibling, None)

Here is the call graph for this function:

Definition at line 87 of file test_pytree.py.

00087 
00088     def test_leaf_prefix(self):
00089         l1 = pytree.Leaf(100, "foo")
00090         self.assertEqual(l1.prefix, "")
00091         self.assertFalse(l1.was_changed)
00092         l1.prefix = "  ##\n\n"
00093         self.assertEqual(l1.prefix, "  ##\n\n")
00094         self.assertTrue(l1.was_changed)

Here is the call graph for this function:

Definition at line 372 of file test_pytree.py.

00372 
00373     def test_leaf_prev_sibling(self):
00374         l1 = pytree.Leaf(100, "a")
00375         l2 = pytree.Leaf(100, "b")
00376         p1 = pytree.Node(1000, [l1, l2])
00377 
00378         self.assertTrue(l2.prev_sibling is l1)
00379         self.assertEqual(l1.prev_sibling, None)
00380         self.assertEqual(p1.prev_sibling, None)
00381 

Here is the call graph for this function:

Definition at line 61 of file test_pytree.py.

00061 
00062     def test_leaf_repr(self):
00063         l1 = pytree.Leaf(100, "foo")
00064         self.assertEqual(repr(l1), "Leaf(100, 'foo')")

Here is the call graph for this function:

Definition at line 65 of file test_pytree.py.

00065 
00066     def test_leaf_str(self):
00067         l1 = pytree.Leaf(100, "foo")
00068         self.assertEqual(str(l1), "foo")
00069         l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1)))
00070         self.assertEqual(str(l2), " foo")

Here is the call graph for this function:

Definition at line 71 of file test_pytree.py.

00071 
00072     def test_leaf_str_numeric_value(self):
00073         # Make sure that the Leaf's value is stringified. Failing to
00074         #  do this can cause a TypeError in certain situations.
00075         l1 = pytree.Leaf(2, 5)
00076         l1.prefix = "foo_"
00077         self.assertEqual(str(l1), "foo_5")

Here is the call graph for this function:

Definition at line 181 of file test_pytree.py.

00181 
00182     def test_leaves(self):
00183         l1 = pytree.Leaf(100, "foo")
00184         l2 = pytree.Leaf(100, "bar")
00185         l3 = pytree.Leaf(100, "fooey")
00186         n2 = pytree.Node(1000, [l1, l2])
00187         n3 = pytree.Node(1000, [l3])
00188         n1 = pytree.Node(1000, [n2, n3])
00189 
00190         self.assertEqual(list(n1.leaves()), [l1, l2, l3])

Here is the call graph for this function:

Definition at line 95 of file test_pytree.py.

00095 
00096     def test_node(self):
00097         l1 = pytree.Leaf(100, "foo")
00098         l2 = pytree.Leaf(200, "bar")
00099         n1 = pytree.Node(1000, [l1, l2])
00100         self.assertEqual(n1.type, 1000)
00101         self.assertEqual(n1.children, [l1, l2])

Here is the call graph for this function:

Definition at line 329 of file test_pytree.py.

00329 
00330     def test_node_append_child(self):
00331         n1 = pytree.Node(1000, [])
00332 
00333         l1 = pytree.Leaf(100, "foo")
00334         n1.append_child(l1)
00335         self.assertEqual(l1.parent, n1)
00336         self.assertEqual(n1.children, [l1])
00337 
00338         l2 = pytree.Leaf(100, "bar")
00339         n1.append_child(l2)
00340         self.assertEqual(l2.parent, n1)
00341         self.assertEqual(n1.children, [l1, l2])
00342 
00343         # I don't care what it raises, so long as it's an exception
00344         self.assertRaises(Exception, n1.append_child, list)

Here is the call graph for this function:

Definition at line 250 of file test_pytree.py.

00250 
00251     def test_node_constructor_prefix(self):
00252         for prefix in ("xyz_", ""):
00253             l1 = pytree.Leaf(100, "self")
00254             l2 = pytree.Leaf(100, "foo", prefix="_")
00255             n1 = pytree.Node(1000, [l1, l2], prefix=prefix)
00256             self.assertTrue(str(n1), prefix + "self_foo")
00257             self.assertEqual(n1.prefix, prefix)
00258             self.assertEqual(l1.prefix, prefix)
00259             self.assertEqual(l2.prefix, "_")

Here is the call graph for this function:

Definition at line 140 of file test_pytree.py.

00140 
00141     def test_node_equality(self):
00142         n1 = pytree.Node(1000, ())
00143         n2 = pytree.Node(1000, [], context=(" ", (1, 0)))
00144         self.assertEqual(n1, n2)
00145         n3 = pytree.Node(1001, ())
00146         self.assertNotEqual(n1, n3)

Here is the call graph for this function:

Definition at line 313 of file test_pytree.py.

00313 
00314     def test_node_insert_child(self):
00315         l1 = pytree.Leaf(100, "foo")
00316         n1 = pytree.Node(1000, [l1])
00317 
00318         l2 = pytree.Leaf(100, "bar")
00319         n1.insert_child(0, l2)
00320         self.assertEqual(l2.parent, n1)
00321         self.assertEqual(n1.children, [l2, l1])
00322 
00323         l3 = pytree.Leaf(100, "abc")
00324         n1.insert_child(2, l3)
00325         self.assertEqual(n1.children, [l2, l1, l3])
00326 
00327         # I don't care what it raises, so long as it's an exception
00328         self.assertRaises(Exception, n1.insert_child, 0, list)

Here is the call graph for this function:

Definition at line 345 of file test_pytree.py.

00345 
00346     def test_node_next_sibling(self):
00347         n1 = pytree.Node(1000, [])
00348         n2 = pytree.Node(1000, [])
00349         p1 = pytree.Node(1000, [n1, n2])
00350 
00351         self.assertTrue(n1.next_sibling is n2)
00352         self.assertEqual(n2.next_sibling, None)
00353         self.assertEqual(p1.next_sibling, None)

Here is the call graph for this function:

Definition at line 115 of file test_pytree.py.

00115 
00116     def test_node_prefix(self):
00117         l1 = pytree.Leaf(100, "foo")
00118         self.assertEqual(l1.prefix, "")
00119         n1 = pytree.Node(1000, [l1])
00120         self.assertEqual(n1.prefix, "")
00121         n1.prefix = " "
00122         self.assertEqual(n1.prefix, " ")
00123         self.assertEqual(l1.prefix, " ")

Here is the call graph for this function:

Definition at line 363 of file test_pytree.py.

00363 
00364     def test_node_prev_sibling(self):
00365         n1 = pytree.Node(1000, [])
00366         n2 = pytree.Node(1000, [])
00367         p1 = pytree.Node(1000, [n1, n2])
00368 
00369         self.assertTrue(n2.prev_sibling is n1)
00370         self.assertEqual(n1.prev_sibling, None)
00371         self.assertEqual(p1.prev_sibling, None)

Here is the call graph for this function:

Definition at line 147 of file test_pytree.py.

00147 
00148     def test_node_recursive_equality(self):
00149         l1 = pytree.Leaf(100, "foo")
00150         l2 = pytree.Leaf(100, "foo")
00151         n1 = pytree.Node(1000, [l1])
00152         n2 = pytree.Node(1000, [l2])
00153         self.assertEqual(n1, n2)
00154         l3 = pytree.Leaf(100, "bar")
00155         n3 = pytree.Node(1000, [l3])
00156         self.assertNotEqual(n1, n3)

Here is the call graph for this function:

Definition at line 102 of file test_pytree.py.

00102 
00103     def test_node_repr(self):
00104         l1 = pytree.Leaf(100, "foo")
00105         l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
00106         n1 = pytree.Node(1000, [l1, l2])
00107         self.assertEqual(repr(n1),
00108                          "Node(1000, [%s, %s])" % (repr(l1), repr(l2)))

Here is the call graph for this function:

Definition at line 292 of file test_pytree.py.

00292 
00293     def test_node_set_child(self):
00294         l1 = pytree.Leaf(100, "foo")
00295         n1 = pytree.Node(1000, [l1])
00296 
00297         l2 = pytree.Leaf(100, "bar")
00298         n1.set_child(0, l2)
00299         self.assertEqual(l1.parent, None)
00300         self.assertEqual(l2.parent, n1)
00301         self.assertEqual(n1.children, [l2])
00302 
00303         n2 = pytree.Node(1000, [l1])
00304         n2.set_child(0, n1)
00305         self.assertEqual(l1.parent, None)
00306         self.assertEqual(n1.parent, n2)
00307         self.assertEqual(n2.parent, None)
00308         self.assertEqual(n2.children, [n1])
00309 
00310         self.assertRaises(IndexError, n1.set_child, 4, l2)
00311         # I don't care what it raises, so long as it's an exception
00312         self.assertRaises(Exception, n1.set_child, 0, list)

Here is the call graph for this function:

Definition at line 109 of file test_pytree.py.

00109 
00110     def test_node_str(self):
00111         l1 = pytree.Leaf(100, "foo")
00112         l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
00113         n1 = pytree.Node(1000, [l1, l2])
00114         self.assertEqual(str(n1), "foo bar")

Here is the call graph for this function:

Definition at line 202 of file test_pytree.py.

00202 
00203     def test_post_order(self):
00204         l1 = pytree.Leaf(100, "foo")
00205         l2 = pytree.Leaf(100, "bar")
00206         l3 = pytree.Leaf(100, "fooey")
00207         c1 = pytree.Node(1000, [l1, l2])
00208         n1 = pytree.Node(1000, [c1, l3])
00209         self.assertEqual(list(n1.post_order()), [l1, l2, c1, l3, n1])

Here is the call graph for this function:

Definition at line 210 of file test_pytree.py.

00210 
00211     def test_pre_order(self):
00212         l1 = pytree.Leaf(100, "foo")
00213         l2 = pytree.Leaf(100, "bar")
00214         l3 = pytree.Leaf(100, "fooey")
00215         c1 = pytree.Node(1000, [l1, l2])
00216         n1 = pytree.Node(1000, [c1, l3])
00217         self.assertEqual(list(n1.pre_order()), [n1, c1, l1, l2, l3])

Here is the call graph for this function:

Definition at line 260 of file test_pytree.py.

00260 
00261     def test_remove(self):
00262         l1 = pytree.Leaf(100, "foo")
00263         l2 = pytree.Leaf(100, "foo")
00264         n1 = pytree.Node(1000, [l1, l2])
00265         n2 = pytree.Node(1000, [n1])
00266 
00267         self.assertEqual(n1.remove(), 0)
00268         self.assertEqual(n2.children, [])
00269         self.assertEqual(l1.parent, n1)
00270         self.assertEqual(n1.parent, None)
00271         self.assertEqual(n2.parent, None)
00272         self.assertFalse(n1.was_changed)
00273         self.assertTrue(n2.was_changed)
00274 
00275         self.assertEqual(l2.remove(), 1)
00276         self.assertEqual(l1.remove(), 0)
00277         self.assertEqual(n1.children, [])
00278         self.assertEqual(l1.parent, None)
00279         self.assertEqual(n1.parent, None)
00280         self.assertEqual(n2.parent, None)
00281         self.assertTrue(n1.was_changed)
00282         self.assertTrue(n2.was_changed)

Here is the call graph for this function:

Definition at line 283 of file test_pytree.py.

00283 
00284     def test_remove_parentless(self):
00285         n1 = pytree.Node(1000, [])
00286         n1.remove()
00287         self.assertEqual(n1.parent, None)
00288 
00289         l1 = pytree.Leaf(100, "foo")
00290         l1.remove()
00291         self.assertEqual(l1.parent, None)

Here is the call graph for this function:

Definition at line 157 of file test_pytree.py.

00157 
00158     def test_replace(self):
00159         l1 = pytree.Leaf(100, "foo")
00160         l2 = pytree.Leaf(100, "+")
00161         l3 = pytree.Leaf(100, "bar")
00162         n1 = pytree.Node(1000, [l1, l2, l3])
00163         self.assertEqual(n1.children, [l1, l2, l3])
00164         self.assertTrue(isinstance(n1.children, list))
00165         self.assertFalse(n1.was_changed)
00166         l2new = pytree.Leaf(100, "-")
00167         l2.replace(l2new)
00168         self.assertEqual(n1.children, [l1, l2new, l3])
00169         self.assertTrue(isinstance(n1.children, list))
00170         self.assertTrue(n1.was_changed)

Here is the call graph for this function:

Definition at line 171 of file test_pytree.py.

00171 
00172     def test_replace_with_list(self):
00173         l1 = pytree.Leaf(100, "foo")
00174         l2 = pytree.Leaf(100, "+")
00175         l3 = pytree.Leaf(100, "bar")
00176         n1 = pytree.Node(1000, [l1, l2, l3])
00177 
00178         l2.replace([pytree.Leaf(100, "*"), pytree.Leaf(100, "*")])
00179         self.assertEqual(str(n1), "foo**bar")
00180         self.assertTrue(isinstance(n1.children, list))

Here is the call graph for this function:


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