def _parse_text(clazz, filename, text): root = tree_text_parser.parse(text, strip_comments=True) modules = [] for child in root.children: mod = clazz._parse_module(child) modules.append(mod) return modules
def test_get_text_children_inline(self): tree_text = ''' c_program bin/cut.exe sources main.c int main(int argc, char* argv[]) { char* arg; if (argc < 2) { fprintf(stderr, "Usage: cut.exe args\\n"); return 1; } return 0; ''' expected = '''int main(int argc, char* argv[]) { char* arg; if (argc < 2) { fprintf(stderr, "Usage: cut.exe args\\n"); return 1; } return 0; ''' root = P.parse(tree_text) child = root.find_child_by_text('main.c') self.assertMultiLineEqual(expected, child.get_text(child.CHILDREN_INLINE))
def test_literal_single_line_no_line_break(self): tree_text = '''\ child > this is a single line literal''' root = P.parse(tree_text) expected = r"""_text_node_data(text='root', line_number=0) _text_node_data(text='child', line_number=1) _text_node_data(text='this is a single line literal', line_number=2)""" self.maxDiff = None self.assertMultiLineEqual(expected, root.to_string())
def test_get_text_node_flat_three_nodes(self): tree_text = ''' fruits apple berries blueberries strawberries ''' root = P.parse(tree_text) self.assertEqual('root fruits apple berries blueberries strawberries', root.get_text(root.NODE_FLAT))
def test_get_text_children_flat_three_nodes(self): tree_text = ''' fruits apple berries blueberries strawberries organic conventional ''' root = P.parse(tree_text) child = root.find_child_by_text('blueberries') self.assertEqual('strawberries organic conventional', child.get_text(root.CHILDREN_FLAT))
def test_literals(self): tree_text = '''\ child1 child2 sub2a sub2a1 sub2b > this is a multi line literal that includes \'\'\'whatever\'\'\' sub2c foo sub2d >this is a another multi # line literal sub2e > this is yet another multi # line literal foo sub3d''' root = P.parse(tree_text) expected = r"""_text_node_data(text='root', line_number=0) _text_node_data(text='child1', line_number=1) _text_node_data(text='child2', line_number=3) _text_node_data(text='sub2a', line_number=4) _text_node_data(text='sub2a1', line_number=5) _text_node_data(text='sub2b', line_number=6) _text_node_data(text="this is a multi\nline literal\nthat includes '''whatever'''\n", line_number=7) _text_node_data(text='sub2c', line_number=11) _text_node_data(text='foo', line_number=12) _text_node_data(text='sub2d', line_number=13) _text_node_data(text='this is a another multi\n#\nline literal\n\n', line_number=14) _text_node_data(text='sub2e', line_number=19) _text_node_data(text='this is yet another multi\n\n#\nline literal\nfoo', line_number=20) _text_node_data(text='sub3d', line_number=25)""" self.maxDiff = None self.assertMultiLineEqual(expected, root.to_string())
def from_text(clazz, text, source=None, check_env_vars=True, entry_parser=None, entry_formatter=None, ignore_extends=False, options=None): check.check_string(text) source = source or '<unknown>' root = tree_text_parser.parse(text, strip_comments=True, root_name='root') return clazz.from_node(root, source=source, check_env_vars=check_env_vars, entry_parser=entry_parser, entry_formatter=entry_formatter, ignore_extends=ignore_extends, options=options)
def graph(self): rv = self.call_pipenv([ 'graph' ]) tree = tree_text_parser.parse(rv.stdout, strip_comments = True) print(tree) return tree
def test_empty_one_line(self): root = P.parse('\n') self.assertMultiLineEqual( r"""_text_node_data(text='root', line_number=0)""", root.to_string().strip())
def _parse(clazz, text, strip_comments=False, root_name='root'): root = P.parse(text, strip_comments=strip_comments, root_name=root_name) return root.to_string(data_func=clazz._data_func)
def test_get_text_node_flat(self): tree_text = ''' fruits ''' root = P.parse(tree_text) self.assertEqual('root fruits', root.get_text(root.NODE_FLAT))
def test_get_text_node(self): tree_text = ''' fruits ''' root = P.parse(tree_text) self.assertEqual('root', root.get_text(root.NODE))