def test_find_branches(self): DATA = """ C1 C11 XYZ C121 C122 C13 XYZ C21 C211 C3 XYZ C1 """ tree = graph.parse_text_tree(DATA) # find non-exist branches # current implementation think it is better to return ['NOT EXIST'] than [] branches = graph.find_branches(tree, 'NOT EXIST') children = [node.data for node,_ in branches.dfs()] self.assertEqual(children, ['NOT EXIST']) # find XYZ and its children branches = graph.find_branches(tree, 'XYZ') children = [node.data for node,_ in branches.dfs()] self.assertEqual(children, [ 'XYZ', 'C121', 'C122', 'C21', 'C211', ])
def test_deformed(self): tree = graph.parse_text_tree(self.DEFORMED) expected = Node('', [ Node('C1', [ Node('C11',[]), Node('C12',[ Node('C121',[]), Node('C122x',[]), # child of C12, although it has smaller indent than sibling C121 ]), Node('C13x',[ # child of C1, although it has smaller indent than sibling C12 Node('C131',[]), # C131 is child of C13x, although it has same indent as uncle C12 ]), ]), ]) self.assertEqual(tree, expected) # it looks odd, but they are siblings self.assertEqual(graph.parse_text_tree(""" C1 C2""" ), Node('', [Node('C1',[]), Node('C2',[])]))
def test_dfs(self): tree = graph.parse_text_tree(self.DATA) result = [(node.data, len(p)) for node, p in tree.dfs()] expected = [ ('',0), ('C1',1), ('C11',2), ('C12',2), ('C121',3), ('C122',3), ('C13',2), ('C2',1), ('C21',2), ('C211',3), ('C3',1), ('C31',2), ('C1',2), ] self.assertEqual(result, expected)
def test_simple(self): tree = graph.parse_text_tree(self.DATA) expected = Node('', [ Node('C1', [ Node('C11',[]), Node('C12',[ Node('C121',[]), Node('C122',[]), ]), Node('C13',[]), ]), Node('C2', [ Node('C21', [ Node('C211',[]), ]), ]), Node('C3', [ Node('C31',[]), Node('C1',[]), ]), ]) self.assertEqual(tree, expected)
def _compile(self, description=None): """ Build root from category_description """ # try a description parameter instead of using self.description because # sometimes we need to call _compile() before changing the description if description == None: description = self.getDescription() self.root = graph.parse_text_tree(description) # walk the parsed tree # - create new tags # - convert string to tag for node, p in self.root.dfs(): name = node.data tag = self.wlib.tags.getByName(name) if (not tag) and name: tag = Tag(name=name) tag = self.wlib.store.writeTag(tag) log.debug(u'Add tag from category: %s' % unicode(tag)) node.data = tag
def test0(self): self.assertEqual(graph.parse_text_tree(''), Node('')) self.assertEqual(graph.parse_text_tree('\n'), Node(''))