flowmetal/components/milkshake/test/python/test_examples.py

101 lines
3.3 KiB
Python

#!/usr/bin/env python3
from milkshake import slurp
from lark import Tree, Token
import pytest
@pytest.mark.parametrize(
"input, val",
[
("()", Tree("list", [])),
("nil", nil := Tree("symbol", ["nil"])),
("(nil nil nil)", Tree("list", [nil, nil, nil])),
(
"(/ + - * % ^ \\ & # @ ! = |)",
Tree(
"list",
[
Tree("symbol", ["/"]),
Tree("symbol", ["+"]),
Tree("symbol", ["-"]),
Tree("symbol", ["*"]),
Tree("symbol", ["%"]),
Tree("symbol", ["^"]),
Tree("symbol", ["\\"]),
Tree("symbol", ["&"]),
Tree("symbol", ["#"]),
Tree("symbol", ["@"]),
Tree("symbol", ["!"]),
Tree("symbol", ["="]),
Tree("symbol", ["|"]),
],
),
),
("1.0", Tree("number", ["1.0"])),
("+1.0", Tree("number", ["+1.0"])),
("-1.0", Tree("number", ["-1.0"])),
("-10,000,000.0", Tree("number", ["-10,000,000.0"])),
("-10.000.000,0", Tree("number", ["-10.000.000,0"])),
("-10_000_000,0", Tree("number", ["-10_000_000,0"])),
("-10_000_000.0", Tree("number", ["-10_000_000.0"])),
("1e50", Tree("number", ["1e50"])),
("-1e-50", Tree("number", ["-1e-50"])),
("-1e+50", Tree("number", ["-1e+50"])),
(
"(+inf -inf inf nan +nan -nan)",
Tree(
"list",
[
Tree("symbol", ["+inf"]),
Tree("symbol", ["-inf"]),
Tree("symbol", ["inf"]),
Tree("symbol", ["nan"]),
Tree("symbol", ["+nan"]),
Tree("symbol", ["-nan"]),
],
),
),
("'()", Tree("quote", [Tree("list", [])])),
(
"`(nil ~'() ~@'())",
Tree(
"quasiquote",
[
Tree(
"list",
[
Tree("symbol", ["nil"]),
Tree(
"unquote",
[
Tree(
"quote",
[
Tree("list", []),
],
),
],
),
Tree(
"unquote_splicing",
[
Tree(
"quote",
[
Tree("list", []),
],
),
],
),
],
),
],
),
),
(r"/foo\S+/", Tree("pattern", [r"/foo\S+/"])),
],
)
def test_examples(input, val):
assert slurp(input, start="expr") == val