source/projects/lilith/test/python/test_parser.py

61 lines
2 KiB
Python
Raw Normal View History

"""tests covering the Lilith parser."""
2021-08-21 19:13:56 +00:00
from lilith.parser import Apply, Args, Block, GRAMMAR, parse_buffer, parser_with_transformer
import pytest
2021-08-21 17:49:46 +00:00
2021-08-21 19:13:56 +00:00
@pytest.mark.parametrize('example, expected', [
("1", [1]),
("1, 2", [1, 2]),
("1, 2, 3", [1, 2, 3]),
])
2021-08-21 19:13:56 +00:00
def test_parse_args(args_grammar, example, expected):
assert args_grammar.parse(example) == expected
2021-08-21 19:13:56 +00:00
@pytest.mark.parametrize('example, expected', [
2021-08-21 17:49:46 +00:00
("foo: bar", {"foo": "bar"}),
("foo: bar, baz: qux", {"foo": "bar", "baz": "qux"}),
])
2021-08-21 19:13:56 +00:00
def test_parse_kwargs(kwargs_grammar, example, expected):
assert kwargs_grammar.parse(example) == expected
2021-08-21 17:49:46 +00:00
2021-08-21 19:13:56 +00:00
@pytest.mark.parametrize('example, expected', [
("1", ([1], {})),
("1, 2", ([1, 2], {})),
("1, 2, 3", ([1, 2, 3], {})),
2021-08-21 17:49:46 +00:00
("foo: bar", ([], {"foo": "bar"})),
("foo: bar, baz: qux", ([], {"foo": "bar", "baz": "qux"})),
2021-08-21 19:13:56 +00:00
("1; foo: bar, baz: qux", ([1], {"foo": "bar", "baz": "qux"})),
2021-08-21 17:49:46 +00:00
])
2021-08-21 19:13:56 +00:00
def test_parse_arguments(arguments_grammar, example, expected):
assert arguments_grammar.parse(example) == expected
2021-08-21 17:49:46 +00:00
2021-08-21 19:13:56 +00:00
@pytest.mark.parametrize('example, expected', [
2021-08-21 17:49:46 +00:00
('!def[syntax]',
2021-08-21 19:13:56 +00:00
Block(Apply('def', Args(['syntax'], {})), [])),
2021-08-21 17:49:46 +00:00
('!frag[lang: md]',
2021-08-21 19:13:56 +00:00
Block(Apply('frag', Args([], {'lang': 'md'})), [])),
2021-08-21 17:49:46 +00:00
('!frag[foo; lang: md]',
2021-08-21 19:13:56 +00:00
Block(Apply('frag', Args(['foo'], {'lang': 'md'})), [])),
("!int.add[1, 2]",
Block(Apply('int.add', Args([1, 2], {})), [])),
])
def test_parse_header(header_grammar, example, expected):
assert header_grammar.parse(example) == expected
@pytest.mark.parametrize('example, expected', [
("!frag[lang: md]",
[Block(Apply('frag', Args([], {"lang": "md"})), [])]),
("""!frag[lang: md]\nHello, world!\n\n""",
[Block(Apply('frag', Args([], {"lang": "md"})), ["Hello, world!", ""])]),
("""!frag[lang: md]\nHello, world!\n\n!def[bar]""",
[Block(Apply('frag', Args([], {"lang": "md"})), ["Hello, world!", ""]),
Block(Apply('def', Args(["bar"], {})), [])]),
2021-08-21 17:49:46 +00:00
])
2021-08-21 19:13:56 +00:00
def test_block_parser(example, expected):
assert parse_buffer(example) == expected