From eba4a604cbf694fc55d0238a5826b4227b1a79db Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Sat, 10 Mar 2018 12:28:13 +0200 Subject: [PATCH] Added tests for standalone generator --- lark/tools/standalone.py | 15 +++++++----- tests/__main__.py | 1 + tests/test_tools.py | 50 ++++++++++++++++++++++++++++++++++++++++ tests/test_trees.py | 1 - 4 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 tests/test_tools.py diff --git a/lark/tools/standalone.py b/lark/tools/standalone.py index 0444614..cf29cee 100644 --- a/lark/tools/standalone.py +++ b/lark/tools/standalone.py @@ -167,9 +167,8 @@ class TreeBuilderAtoms: print('}') print('parse_tree_builder = ParseTreeBuilder(RULES.values(), Tree)') -def main(fn, start): - with codecs.open(fn, encoding='utf8') as f: - lark_inst = Lark(f, parser="lalr", start=start) +def main(fobj, start): + lark_inst = Lark(fobj, parser="lalr", start=start) lexer_atoms = LexerAtoms(lark_inst.parser.lexer) parser_atoms = ParserAtoms(lark_inst.parser.parser) @@ -178,9 +177,12 @@ def main(fn, start): print('# The file was automatically generated by Lark v%s' % lark.__version__) for pyfile in EXTRACT_STANDALONE_FILES: - print (extract_sections(open(os.path.join(__larkdir__, pyfile)))['standalone']) + with open(os.path.join(__larkdir__, pyfile)) as f: + print (extract_sections(f)['standalone']) + + with open(os.path.join(__larkdir__, 'grammar.py')) as grammar_py: + print(grammar_py.read()) - print(open(os.path.join(__larkdir__, 'grammar.py')).read()) print('Shift = 0') print('Reduce = 1') lexer_atoms.print_python() @@ -200,4 +202,5 @@ if __name__ == '__main__': else: assert False, sys.argv - main(fn, start) + with codecs.open(fn, encoding='utf8') as f: + main(f, start) diff --git a/tests/__main__.py b/tests/__main__.py index 4f7fdf7..eebf0d9 100644 --- a/tests/__main__.py +++ b/tests/__main__.py @@ -4,6 +4,7 @@ import unittest import logging from .test_trees import TestTrees +from .test_tools import TestStandalone try: from .test_nearley.test_nearley import TestNearley diff --git a/tests/test_tools.py b/tests/test_tools.py new file mode 100644 index 0000000..8722f45 --- /dev/null +++ b/tests/test_tools.py @@ -0,0 +1,50 @@ +from __future__ import absolute_import + +import sys +import unittest +from unittest import TestCase + +from lark.tree import Tree + +from lark.tools import standalone + +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +class TestStandalone(TestCase): + def setUp(self): + pass + + def test_simple(self): + grammar = """ + start: NUMBER WORD + + %import common.NUMBER + %import common.WORD + %import common.WS + %ignore WS + + """ + + code_buf = StringIO() + temp = sys.stdout + sys.stdout = code_buf + standalone.main(StringIO(grammar), 'start') + sys.stdout = temp + code = code_buf.getvalue() + + context = {} + exec(code, context) + _Lark = context['Lark_StandAlone'] + + l = _Lark() + x = l.parse('12 elephants') + self.assertEqual(x.children, ['12', 'elephants']) + + +if __name__ == '__main__': + unittest.main() + + diff --git a/tests/test_trees.py b/tests/test_trees.py index 2512c3b..c90cc7d 100644 --- a/tests/test_trees.py +++ b/tests/test_trees.py @@ -2,7 +2,6 @@ from __future__ import absolute_import import unittest from unittest import TestCase -import logging import copy import pickle