2018-01-01 16:38:33 +01:00
|
|
|
# Copyright 2018 the authors.
|
2017-04-27 23:16:57 +02:00
|
|
|
# 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
|
|
|
|
2013-09-29 17:55:15 +02: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
|
|
|
|
2017-02-17 04:43:00 +01:00
|
|
|
from hy.models import HyString, HyList, HySymbol, HyExpression
|
2015-08-30 20:14:16 +02:00
|
|
|
from hy.errors import HyMacroExpansionError
|
2013-03-08 04:52:47 +01:00
|
|
|
|
2015-12-23 21:13:18 +01:00
|
|
|
from hy.compiler import HyASTCompiler
|
|
|
|
|
2017-09-23 20:55:44 +02:00
|
|
|
import pytest
|
|
|
|
|
2013-03-08 04:52:47 +01:00
|
|
|
|
|
|
|
@macro("test")
|
2017-09-23 20:55:44 +02:00
|
|
|
def tmac(ETname, *tree):
|
2013-03-08 04:52:47 +01:00
|
|
|
""" Turn an expression into a list """
|
2013-05-11 09:09:34 +02:00
|
|
|
return HyList(tree)
|
2013-03-08 04:52:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_preprocessor_simple():
|
2013-05-16 15:34:14 +02:00
|
|
|
""" Test basic macro expansion """
|
2015-12-23 21:13:18 +01:00
|
|
|
obj = macroexpand(tokenize('(test "one" "two")')[0],
|
|
|
|
HyASTCompiler(__name__))
|
2013-03-08 04:52:47 +01:00
|
|
|
assert obj == HyList(["one", "two"])
|
|
|
|
assert type(obj) == HyList
|
|
|
|
|
|
|
|
|
|
|
|
def test_preprocessor_expression():
|
2013-07-06 20:00:11 +02:00
|
|
|
""" Test that macro expansion doesn't recurse"""
|
2015-12-23 21:13:18 +01:00
|
|
|
obj = macroexpand(tokenize('(test (test "one" "two"))')[0],
|
|
|
|
HyASTCompiler(__name__))
|
2013-03-08 04:52:47 +01:00
|
|
|
|
|
|
|
assert type(obj) == HyList
|
2013-07-06 20:00:11 +02:00
|
|
|
assert type(obj[0]) == HyExpression
|
2013-03-08 04:52:47 +01:00
|
|
|
|
2013-07-06 20:00:11 +02: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]
|
2015-12-23 21:13:18 +01:00
|
|
|
assert obj == macroexpand(obj, HyASTCompiler(""))
|
2015-08-30 20:14:16 +02:00
|
|
|
|
|
|
|
|
2017-09-23 20:55:44 +02:00
|
|
|
@pytest.mark.xfail
|
2015-08-30 20:14:16 +02:00
|
|
|
def test_preprocessor_exceptions():
|
|
|
|
""" Test that macro expansion raises appropriate exceptions"""
|
|
|
|
try:
|
2015-12-23 21:13:18 +01:00
|
|
|
macroexpand(tokenize('(defn)')[0], HyASTCompiler(__name__))
|
2015-08-30 20:14:16 +02:00
|
|
|
assert False
|
|
|
|
except HyMacroExpansionError as e:
|
|
|
|
assert "_hy_anon_fn_" not in str(e)
|
|
|
|
assert "TypeError" not in str(e)
|