def test_nodedata(self): """DndParser should assign Data to internal nodes correctly""" t = DndParser(nodedata) self.assertEqual(len(t), 2) self.assertEqual(len(t[0]), 0) # first child is terminal self.assertEqual(len(t[1]), 2) # second child has two children self.assertEqual(str(t), "(abc:3.0,(def:4.0,ghi:5.0)jkl:6.0)") info_dict = {} for node in t.traverse(): info_dict[node.Data] = node.BranchLength self.assertEqual(info_dict["abc"], 3.0) self.assertEqual(info_dict["def"], 4.0) self.assertEqual(info_dict["ghi"], 5.0) self.assertEqual(info_dict["jkl"], 6.0)
def test_mapAttr(self): """Tree mapattr should correctly change the attributes in-place""" t = DndParser('(a,(c,b)x,d)') t.mapAttr({'a':'aa','b':'bb','c':'cc','x':'xx'}) # d left out self.assertEqual(str(t), '(aa,(cc,bb)xx,d)') t = DndParser('(a,(c,b)x,d)') f = lambda x: x in list('abcx') and x*3 or x t.mapAttr(f) # d left out self.assertEqual(str(t), '(aaa,(ccc,bbb)xxx,d)') #check that it works if you set a different attribute than you read t = DndParser('(a,(c,b)x,d)') t.mapAttr(f, new_attr='xxx') for n in t.traverse(self_before=True, self_after=False): if n in list('abcx'): self.assertEqual(n.xxx, n.Data*3) else: self.assertEqual(n.xxx, n.Data)