Working Lilith block header parser

This commit is contained in:
Reid 'arrdem' McKenzie 2021-08-21 11:49:46 -06:00
commit 014ce0b21d
6 changed files with 134 additions and 55 deletions
projects/lilith/test/python

View file

@ -1,3 +1,26 @@
"""
Pytest fixtures.
"""
from lilith.parser import Block, parser_with_transformer, GRAMMAR
import pytest
@pytest.fixture
def args_grammar():
return parser_with_transformer(GRAMMAR, "args")
@pytest.fixture
def kwargs_grammar():
return parser_with_transformer(GRAMMAR, "kwargs")
@pytest.fixture
def arguments_grammar():
return parser_with_transformer(GRAMMAR, "arguments")
@pytest.fixture
def header_grammar():
return parser_with_transformer(GRAMMAR, "header")

View file

@ -1,26 +1,45 @@
"""tests covering the Lilith parser."""
from lilith.parser import block_grammar, Block
from lilith.parser import Args, Block, parser_with_transformer, GRAMMAR
import pytest
@pytest.mark.parametrize('example, result', [
("1", ["1"]),
("1, 2", ["1", "2"]),
("1, 2, 3", ["1", "2", "3"]),
])
def test_parse_args(args_grammar, example, result):
assert args_grammar.parse(example) == result
@pytest.mark.parametrize('example, result', [
("foo: bar", {"foo": "bar"}),
("foo: bar, baz: qux", {"foo": "bar", "baz": "qux"}),
])
def test_parse_kwargs(kwargs_grammar, example, result):
assert kwargs_grammar.parse(example) == result
@pytest.mark.parametrize('example, result', [
("1", (["1"], {})),
("1, 2", (["1", "2"], {})),
("1, 2, 3", (["1", "2", "3"], {})),
("foo: bar", ([], {"foo": "bar"})),
("foo: bar, baz: qux", ([], {"foo": "bar", "baz": "qux"})),
("1; foo: bar, baz: qux", (["1"], {"foo": "bar", "baz": "qux"})),
])
def test_parse_arguments(arguments_grammar, example, result):
assert arguments_grammar.parse(example) == result
@pytest.mark.parametrize('example, result', [
('!def[syntax]',
Block('def', ['syntax'], None, [])),
Block('def', Args(['syntax'], {}), [])),
('!frag[lang: md]',
Block('frag', None, {'lang': 'md'}, [])),
('!frag[foo, lang: md]',
Block('frag', ['foo'], {'lang': 'md'}, [])),
Block('frag', Args([], {'lang': 'md'}), [])),
('!frag[foo; lang: md]',
Block('frag', Args(['foo'], {'lang': 'md'}), [])),
])
def test_parse_header(example, result):
assert block_grammar.parse(example) == result
(
"""!def[designdoc]
!frag[lang: md]
# Designdoc
A design document""",
None
)
def test_parse_header(header_grammar, example, result):
assert header_grammar.parse(example) == result