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

67 lines
2.3 KiB
Python
Raw Normal View History

"""tests covering the Lilith parser."""
2021-08-21 22:44:49 +00:00
from lilith.parser import Apply, Args, Block, GRAMMAR, parse_buffer, parser_with_transformer, Symbol
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 22:44:49 +00:00
("foo: bar", {Symbol("foo"): Symbol("bar")}),
("foo: bar, baz: qux", {Symbol("foo"): Symbol("bar"), Symbol("baz"): Symbol("qux")}),
2021-08-21 17:49:46 +00:00
])
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 22:44:49 +00:00
("foo: bar",
([], {Symbol("foo"): Symbol("bar")})),
("foo: bar, baz: qux",
([], {Symbol("foo"): Symbol("bar"),
Symbol("baz"): Symbol("qux")})),
("1; foo: bar, baz: qux",
([1], {Symbol("foo"): Symbol("bar"),
Symbol("baz"): Symbol("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 22:44:49 +00:00
Block(Apply(Symbol('def'), Args(['syntax'], {})), [])),
2021-08-21 17:49:46 +00:00
('!frag[lang: md]',
2021-08-21 22:44:49 +00:00
Block(Apply(Symbol('frag'), Args([], {'lang': 'md'})), [])),
2021-08-21 17:49:46 +00:00
('!frag[foo; lang: md]',
2021-08-21 22:44:49 +00:00
Block(Apply(Symbol('frag'), Args(['foo'], {'lang': 'md'})), [])),
2021-08-21 19:13:56 +00:00
("!int.add[1, 2]",
2021-08-21 22:44:49 +00:00
Block(Apply(Symbol('int.add'), Args([1, 2], {})), [])),
2021-08-21 19:13:56 +00:00
])
def test_parse_header(header_grammar, example, expected):
assert header_grammar.parse(example) == expected
@pytest.mark.parametrize('example, expected', [
("!frag[lang: md]",
2021-08-21 22:44:49 +00:00
[Block(Apply(Symbol('frag'), Args([], {Symbol("lang"): Symbol("md")})), [])]),
2021-08-21 19:13:56 +00:00
("""!frag[lang: md]\nHello, world!\n\n""",
2021-08-21 22:44:49 +00:00
[Block(Apply(Symbol('frag'), Args([], {Symbol("lang"): Symbol("md")})), ["Hello, world!", ""])]),
2021-08-21 19:13:56 +00:00
("""!frag[lang: md]\nHello, world!\n\n!def[bar]""",
2021-08-21 22:44:49 +00:00
[Block(Apply(Symbol('frag'), Args([], {Symbol("lang"): Symbol("md")})), ["Hello, world!", ""]),
Block(Apply(Symbol('def'), Args([Symbol("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