hy/tests/macros/test_macro_processor.py

64 lines
1.9 KiB
Python
Raw Normal View History

2019-02-07 14:57:35 +01:00
# Copyright 2019 the authors.
# This file is part of Hy, which is free software licensed under the Expat
# license. See the LICENSE.
2013-03-08 04:52:47 +01:00
from hy.macros import macro, macroexpand
2013-03-09 00:46:51 +01:00
from hy.lex import tokenize
2013-03-08 04:52:47 +01:00
from hy.models import HyString, HyList, HySymbol, HyExpression, HyFloat
from hy.errors import HyMacroExpansionError
2013-03-08 04:52:47 +01:00
from hy.compiler import HyASTCompiler
import pytest
2013-03-08 04:52:47 +01:00
@macro("test")
def tmac(ETname, *tree):
2013-03-08 04:52:47 +01:00
""" Turn an expression into a list """
return HyList(tree)
2013-03-08 04:52:47 +01:00
def test_preprocessor_simple():
""" Test basic macro expansion """
obj = macroexpand(tokenize('(test "one" "two")')[0],
__name__,
HyASTCompiler(__name__))
2013-03-08 04:52:47 +01:00
assert obj == HyList(["one", "two"])
assert type(obj) == HyList
def test_preprocessor_expression():
""" Test that macro expansion doesn't recurse"""
obj = macroexpand(tokenize('(test (test "one" "two"))')[0],
__name__,
HyASTCompiler(__name__))
2013-03-08 04:52:47 +01:00
assert type(obj) == HyList
assert type(obj[0]) == HyExpression
2013-03-08 04:52:47 +01:00
assert obj[0] == HyExpression([HySymbol("test"),
HyString("one"),
HyString("two")])
2013-03-08 05:04:20 +01:00
obj = HyList([HyString("one"), HyString("two")])
2013-03-09 00:46:51 +01:00
obj = tokenize('(shill ["one" "two"])')[0][1]
assert obj == macroexpand(obj, __name__, HyASTCompiler(__name__))
def test_preprocessor_exceptions():
""" Test that macro expansion raises appropriate exceptions"""
with pytest.raises(HyMacroExpansionError) as excinfo:
macroexpand(tokenize('(defn)')[0], __name__, HyASTCompiler(__name__))
assert "_hy_anon_fn_" not in excinfo.value.message
assert "TypeError" not in excinfo.value.message
def test_macroexpand_nan():
# https://github.com/hylang/hy/issues/1574
import math
NaN = float('nan')
x = macroexpand(HyFloat(NaN), __name__, HyASTCompiler(__name__))
assert type(x) is HyFloat
assert math.isnan(x)