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