def test_changeFromParent(self): """TreeNode changeFromParent should record change from parent""" t = DndParser('(a,(c,b)x,d)') t.mapAttr({'a':5, 'c':3, 'b':7, 'x':2, 'd':1}) self.assertEqual(t[1][0].changeFromParent('Data'), 1) self.assertEqual(t[1][1].changeFromParent('Data'), 5) f = lambda x: x.Data * 2 self.assertEqual(t[1][0].changeFromParent(f), 2) self.assertEqual(t[1][1].changeFromParent(f), 10)
def test_attrTable(self): """"TreeNode attrTable should return correct table of attributes""" t = DndParser('(a,(c,b)x,d)') f1 = lambda x: x in list('abcx') and x*2 or x f2 = lambda x: x in list('abcx') and x*3 or x f3 = lambda x: x in list('abcx') and x*4 or x t.mapAttr(f1, new_attr='xx') t.mapAttr(f2, new_attr='xxx') t.mapAttr(f3, new_attr='xxxx') table = t.attrTable(['xx','xxxx']) self.assertEqual(table, [[None, None], ['aa','aaaa'],['xx','xxxx'],\ ['cc','cccc'],['bb','bbbb'],['d','d']])
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)