diff --git a/docs/how_to_develop.md b/docs/how_to_develop.md index b3f4299..d69a1e3 100644 --- a/docs/how_to_develop.md +++ b/docs/how_to_develop.md @@ -14,25 +14,25 @@ If you're interested in taking one of these on, let me know and I will provide m ## Unit Tests -If you would like to run all Unit Tests, -all you need is a supported Python Interpreter. -You can consult the list of supported interpreter for unit testing on the `tox.ini` file. -Then, just run the command `python -m tests` +Lark comes with an extensive set of tests. Many of the tests will run several times, once for each parser configuration. -If you would like to run a single Unit Test, -you do not need to use tox, -you can directly run it with your installed Python Interpreter. -First you need to figure out what is the test full name. -For example: -```python -## test_package test_class_name.test_function_name -python -m tests TestLalrStandard.test_lexer_error_recovering +To run the tests, just go to the lark project root, and run the command: +```bash +python -m tests ``` -Equivalent example/way, but unrecommended: -```python -## test_package.tests_module.test_class_name.test_function_name -python -m unittest tests.test_parser.TestLalrStandard.test_lexer_error_recovering +or + +```bash +pypy -m tests +``` + +For a list of supported interpreters, you can consult the `tox.ini` file. + +You can also run a single unittest using its class and method name, for example: +```bash +## test_package test_class_name.test_function_name +python -m tests TestLalrStandard.test_lexer_error_recovering ``` ### tox @@ -45,3 +45,19 @@ run the command `tox` on the root of this project (where the main setup.py file And, for example, if you would like to only run the Unit Tests for Python version 2.7, you can run the command `tox -e py27` + +### pytest + +You can also run the tests using pytest: + +```bash +pytest tests +``` + +### Using setup.py + +Another way to run the tests is using setup.py: + +```bash +python setup.py test +``` \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index e696690..63bdd61 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -6,6 +6,7 @@ pages: - Features: features.md - Parsers: parsers.md - How To Use (Guide): how_to_use.md + - How To Develop (Guide): how_to_develop.md - Grammar Reference: grammar.md - Tree Construction Reference: tree_construction.md - Classes Reference: classes.md diff --git a/tests/test_parser.py b/tests/test_parser.py index a61fc37..03b475d 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -18,7 +18,7 @@ from io import ( logging.basicConfig(level=logging.INFO) from lark.lark import Lark -from lark.exceptions import GrammarError, ParseError, UnexpectedToken, UnexpectedInput +from lark.exceptions import GrammarError, ParseError, UnexpectedToken, UnexpectedInput, UnexpectedCharacters from lark.tree import Tree from lark.visitors import Transformer @@ -388,6 +388,8 @@ def _make_full_earley_test(LEXER): def _make_parser_test(LEXER, PARSER): def _Lark(grammar, **kwargs): return Lark(grammar, lexer=LEXER, parser=PARSER, propagate_positions=True, **kwargs) + def _Lark_open(gfilename, **kwargs): + return Lark.open(gfilename, lexer=LEXER, parser=PARSER, propagate_positions=True, **kwargs) class _TestParser(unittest.TestCase): def test_basic1(self): g = _Lark("""start: a+ b a* "b" a* @@ -817,7 +819,7 @@ def _make_parser_test(LEXER, PARSER): %s""" % (' '.join(tokens), '\n'.join("%s: %s"%x for x in tokens.items()))) def test_float_without_lexer(self): - expected_error = UnexpectedInput if LEXER == 'dynamic' else UnexpectedToken + expected_error = UnexpectedCharacters if LEXER.startswith('dynamic') else UnexpectedToken if PARSER == 'cyk': expected_error = ParseError @@ -997,16 +999,7 @@ def _make_parser_test(LEXER, PARSER): def test_relative_import(self): - grammar = """ - start: NUMBER WORD - - %import .grammars.test.NUMBER - %import common.WORD - %import common.WS - %ignore WS - - """ - l = _Lark(grammar) + l = _Lark_open('test_relative_import.lark', rel_to=__file__) x = l.parse('12 lions') self.assertEqual(x.children, ['12', 'lions']) @@ -1024,14 +1017,7 @@ def _make_parser_test(LEXER, PARSER): def test_relative_multi_import(self): - grammar = """ - start: NUMBER WORD - - %import .grammars.test (NUMBER, WORD, WS) - %ignore WS - - """ - l = _Lark(grammar) + l = _Lark_open("test_relative_multi_import.lark", rel_to=__file__) x = l.parse('12 capybaras') self.assertEqual(x.children, ['12', 'capybaras']) diff --git a/tests/test_relative_import.lark b/tests/test_relative_import.lark new file mode 100644 index 0000000..c614a31 --- /dev/null +++ b/tests/test_relative_import.lark @@ -0,0 +1,7 @@ +start: NUMBER WORD + +%import .grammars.test.NUMBER +%import common.WORD +%import common.WS +%ignore WS + diff --git a/tests/test_relative_multi_import.lark b/tests/test_relative_multi_import.lark new file mode 100644 index 0000000..75c131d --- /dev/null +++ b/tests/test_relative_multi_import.lark @@ -0,0 +1,4 @@ +start: NUMBER WORD + +%import .grammars.test (NUMBER, WORD, WS) +%ignore WS