class TestTree(unittest.TestCase): """ Basic tests for instantiation of ``tree`` """ def setUp(self): self.node = "root_node" self.child_1 = Tree(node="child_1", children=["grandchild_1", "grandchild_2", "grandchild_3"]) self.child_2 = Tree(node="child_2") self.child_3 = "child_3" self.children = [self.child_1, self.child_2, self.child_3] self.tree = Tree(node=self.node, children=self.children) def test_node(self): res = self.node self.assertEqual(res, self.tree.node(), "Should return the root node \ of the tree") def test_children(self): res = tuple(self.children) self.assertEqual(res, self.tree.children(), "Should return \ the children of the tree") def test_str_is_string(self): self.assertIsInstance(str(self.tree), str, msg="String expected") def test_str(self): res = ("(root_node, (child_1, grandchild_1, grandchild_2, " "grandchild_3), (child_2), child_3)") self.assertEqual(res, str(self.tree)) def test_repr_is_string(self): self.assertIsInstance(repr(self.tree), str, msg="String expected") def test_repr(self): res = "(%r, %r)" % (self.node, tuple(self.children)) self.assertEqual(res, repr(self.tree)) def test_copy(self): copy = self.tree.copy() self.assertEqual(copy, self.tree, "Copy should be equal") def test_len(self): res = 3 self.assertEqual(res, len(self.tree), "Length of tree should be equal to 6")
class TestLeaves(unittest.TestCase): """ Test case for getting the leaves from a ``tree`` """ def setUp(self): self.node = "node" self.child_1 = Tree(node="child_1", children=["grandchild1-1", "grandchild1-2", "grandchild1-3"]) self.child_2 = Tree(node="child_2") self.child_3 = "child_3" self.tree = Tree(node=self.node, children=[self.child_1, self.child_2, self.child_3]) def test_leaves(self): res = [] # TODO: improve this (better flattening of all lists) res += self.child_1.children() res.append(self.child_2.node()) res.append(self.child_3) self.assertEqual(res, self.tree.leaves())