2013-03-03 05:47:16 +01:00
|
|
|
# Copyright (c) 2012 Paul Tagliamonte <paultag@debian.org>
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
# copy of this software and associated documentation files (the "Software"),
|
|
|
|
# to deal in the Software without restriction, including without limitation
|
|
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
# Software is furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
# DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2013-03-03 22:26:17 +01:00
|
|
|
from hy.compilers.pyast import HyCompileError
|
|
|
|
from hy.compiler import compile
|
2013-03-03 05:47:16 +01:00
|
|
|
from hy.lex import tokenize
|
2013-03-03 17:18:13 +01:00
|
|
|
import ast
|
2013-03-03 05:47:16 +01:00
|
|
|
|
|
|
|
|
2013-03-03 19:10:50 +01:00
|
|
|
def _ast_spotcheck(arg, root, secondary):
|
|
|
|
if "." in arg:
|
|
|
|
local, full = arg.split(".", 1)
|
|
|
|
return _ast_spotcheck(full,
|
|
|
|
getattr(root, local),
|
|
|
|
getattr(secondary, local))
|
|
|
|
assert getattr(root, arg) == getattr(secondary, arg)
|
|
|
|
|
|
|
|
|
2013-03-03 19:12:23 +01:00
|
|
|
def test_ast_bad_type():
|
|
|
|
try:
|
2013-03-03 22:26:17 +01:00
|
|
|
compile("foo")
|
2013-03-03 19:12:23 +01:00
|
|
|
assert True == False
|
|
|
|
except HyCompileError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-03-03 06:00:55 +01:00
|
|
|
def test_ast_expression_basics():
|
2013-03-03 17:18:13 +01:00
|
|
|
""" Ensure basic AST expression conversion works. """
|
2013-03-03 22:26:17 +01:00
|
|
|
code = compile(tokenize("(foo bar)"))[0]
|
2013-03-03 17:18:13 +01:00
|
|
|
tree = ast.Call(
|
|
|
|
func=ast.Name(
|
|
|
|
id="foo",
|
|
|
|
ctx=ast.Load(),
|
|
|
|
),
|
|
|
|
args=[
|
|
|
|
ast.Name(id="bar", ctx=ast.Load())
|
|
|
|
],
|
|
|
|
keywords=[],
|
|
|
|
starargs=None,
|
|
|
|
kwargs=None,
|
|
|
|
)
|
2013-03-03 19:10:50 +01:00
|
|
|
_ast_spotcheck("func.id", code, tree)
|
|
|
|
_ast_spotcheck("id", code.args[0], tree.args[0])
|