Get symbols working, styleguide

This commit is contained in:
Reid 'arrdem' McKenzie 2021-08-21 16:58:59 -06:00
commit 8db42aa17d
8 changed files with 181 additions and 102 deletions

View file

@ -21,6 +21,7 @@ def kwargs_grammar():
def arguments_grammar():
return parser_with_transformer(GRAMMAR, "arguments")
@pytest.fixture
def header_grammar():
return parser_with_transformer(GRAMMAR, "header")

View file

@ -4,31 +4,40 @@
from lilith.interpreter import Bindings, Runtime, eval
from lilith.reader import Module
from lilith.parser import Args, Apply
from lilith.parser import Args, Apply, Symbol
import pytest
@pytest.mark.parametrize('expr, expected', [
(1, 1),
([1, 2], [1, 2]),
({"foo": "bar"}, {"foo": "bar"}),
])
@pytest.mark.parametrize(
"expr, expected",
[
(1, 1),
([1, 2], [1, 2]),
({"foo": "bar"}, {"foo": "bar"}),
],
)
def test_eval(expr, expected):
assert eval(
Runtime("test", dict()),
Module("__repl__", dict()),
Bindings("__root__", None),
expr
) == expected
assert (
eval(
Runtime("test", dict()),
Module("__repl__", dict()),
Bindings("__root__", None),
expr,
)
== expected
)
def test_hello_world(capsys):
assert eval(
Runtime("test", {}),
Module("__repl__", {"print": print}),
Bindings("__root__", None),
Apply("print", Args(["hello, world"], {}))
) is None
assert (
eval(
Runtime("test", {}),
Module("__repl__", {Symbol("print"): print}),
Bindings("__root__", None),
Apply(Symbol("print"), Args(["hello, world"], {})),
)
is None
)
captured = capsys.readouterr()
assert captured.out == "hello, world\n"

View file

@ -1,66 +1,121 @@
"""tests covering the Lilith parser."""
from lilith.parser import Apply, Args, Block, GRAMMAR, parse_buffer, parser_with_transformer, Symbol
from lilith.parser import (
Apply,
Args,
Block,
GRAMMAR,
parse_buffer,
parser_with_transformer,
Symbol,
)
import pytest
@pytest.mark.parametrize('example, expected', [
("1", [1]),
("1, 2", [1, 2]),
("1, 2, 3", [1, 2, 3]),
])
@pytest.mark.parametrize(
"example, expected",
[
("1", [1]),
("1, 2", [1, 2]),
("1, 2, 3", [1, 2, 3]),
],
)
def test_parse_args(args_grammar, example, expected):
assert args_grammar.parse(example) == expected
@pytest.mark.parametrize('example, expected', [
("foo: bar", {Symbol("foo"): Symbol("bar")}),
("foo: bar, baz: qux", {Symbol("foo"): Symbol("bar"), Symbol("baz"): Symbol("qux")}),
])
@pytest.mark.parametrize(
"example, expected",
[
("foo: bar", {Symbol("foo"): Symbol("bar")}),
(
"foo: bar, baz: qux",
{Symbol("foo"): Symbol("bar"), Symbol("baz"): Symbol("qux")},
),
],
)
def test_parse_kwargs(kwargs_grammar, example, expected):
assert kwargs_grammar.parse(example) == expected
@pytest.mark.parametrize('example, expected', [
("1", ([1], {})),
("1, 2", ([1, 2], {})),
("1, 2, 3", ([1, 2, 3], {})),
("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")})),
])
@pytest.mark.parametrize(
"example, expected",
[
("1", ([1], {})),
("1, 2", ([1, 2], {})),
("1, 2, 3", ([1, 2, 3], {})),
("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")}),
),
],
)
def test_parse_arguments(arguments_grammar, example, expected):
assert arguments_grammar.parse(example) == expected
@pytest.mark.parametrize('example, expected', [
('!def[syntax]',
Block(Apply(Symbol('def'), Args(['syntax'], {})), [])),
('!frag[lang: md]',
Block(Apply(Symbol('frag'), Args([], {'lang': 'md'})), [])),
('!frag[foo; lang: md]',
Block(Apply(Symbol('frag'), Args(['foo'], {'lang': 'md'})), [])),
("!int.add[1, 2]",
Block(Apply(Symbol('int.add'), Args([1, 2], {})), [])),
])
@pytest.mark.parametrize(
"example, expected",
[
("!def[syntax]", Block(Apply(Symbol("def"), Args([Symbol("syntax")], {})), [])),
(
"!frag[lang: md]",
Block(Apply(Symbol("frag"), Args([], {Symbol("lang"): Symbol("md")})), []),
),
(
"!frag[foo; lang: md]",
Block(
Apply(
Symbol("frag"),
Args([Symbol("foo")], {Symbol("lang"): Symbol("md")}),
),
[],
),
),
("!int.add[1, 2]", Block(Apply(Symbol("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(Symbol('frag'), Args([], {Symbol("lang"): Symbol("md")})), [])]),
("""!frag[lang: md]\nHello, world!\n\n""",
[Block(Apply(Symbol('frag'), Args([], {Symbol("lang"): Symbol("md")})), ["Hello, world!", ""])]),
("""!frag[lang: md]\nHello, world!\n\n!def[bar]""",
[Block(Apply(Symbol('frag'), Args([], {Symbol("lang"): Symbol("md")})), ["Hello, world!", ""]),
Block(Apply(Symbol('def'), Args([Symbol("bar")], {})), [])]),
])
@pytest.mark.parametrize(
"example, expected",
[
(
"!frag[lang: md]",
[
Block(
Apply(Symbol("frag"), Args([], {Symbol("lang"): Symbol("md")})), []
)
],
),
(
"""!frag[lang: md]\nHello, world!\n\n""",
[
Block(
Apply(Symbol("frag"), Args([], {Symbol("lang"): Symbol("md")})),
["Hello, world!", ""],
)
],
),
(
"""!frag[lang: md]\nHello, world!\n\n!def[bar]""",
[
Block(
Apply(Symbol("frag"), Args([], {Symbol("lang"): Symbol("md")})),
["Hello, world!", ""],
),
Block(Apply(Symbol("def"), Args([Symbol("bar")], {})), []),
],
),
],
)
def test_block_parser(example, expected):
assert parse_buffer(example) == expected

View file

@ -1,15 +1,28 @@
"""Tests covering the reader."""
from lilith.parser import Apply, Args, Block
from lilith.parser import Apply, Args, Block, Symbol
from lilith.reader import Module, read_buffer
import pytest
@pytest.mark.parametrize('example, expected', [
("""!def[main, lang[lil]]\nprint["hello, world"]\n""",
Module("&buff", {"main": Block(Apply('lang', Args(["lil"], {})), ["print[\"hello, world\"]"])}))
])
@pytest.mark.parametrize(
"example, expected",
[
(
"""!def[main, lang[lil]]\nprint["hello, world"]\n""",
Module(
"&buff",
{
Symbol("main"): Block(
Apply(Symbol("lang"), Args([Symbol("lil")], {})),
['print["hello, world"]'],
)
},
),
)
],
)
def test_read(example, expected):
got = read_buffer(example)
assert got == expected