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

102 lines
3.1 KiB
Python
Raw Permalink Normal View History

2023-05-04 04:46:26 +00:00
#!/usr/bin/env python3
2023-05-08 23:27:04 +00:00
from milkshake import slurp, Symbol
2023-05-04 04:46:26 +00:00
from lark import Tree, Token
import pytest
@pytest.mark.parametrize(
"input, val",
[
("()", Tree("list", [])),
2023-05-08 23:27:04 +00:00
("nil", nil := Symbol("nil")),
2023-05-04 04:46:26 +00:00
("(nil nil nil)", Tree("list", [nil, nil, nil])),
(
"(/ + - * % ^ \\ & # @ ! = |)",
Tree(
"list",
[
2023-05-08 23:27:04 +00:00
Symbol("/"),
Symbol("+"),
Symbol("-"),
Symbol("*"),
Symbol("%"),
Symbol("^"),
Symbol("\\"),
Symbol("&"),
Symbol("#"),
Symbol("@"),
Symbol("!"),
Symbol("="),
Symbol("|"),
2023-05-04 04:46:26 +00:00
],
),
),
("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",
[
2023-05-08 23:27:04 +00:00
Symbol("+inf"),
Symbol("-inf"),
Symbol("inf"),
Symbol("nan"),
Symbol("+nan"),
Symbol("-nan"),
2023-05-04 04:46:26 +00:00
],
),
),
("'()", Tree("quote", [Tree("list", [])])),
(
"`(nil ~'() ~@'())",
Tree(
"quasiquote",
[
Tree(
"list",
[
2023-05-08 23:27:04 +00:00
Symbol("nil"),
2023-05-04 04:46:26 +00:00
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