2013-03-18 15:27:14 +01:00
|
|
|
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
|
2013-03-03 05:47:16 +01:00
|
|
|
#
|
|
|
|
# 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-05 02:40:23 +01:00
|
|
|
from hy.compiler import hy_compile, HyCompileError
|
2013-03-03 05:47:16 +01:00
|
|
|
from hy.lex import tokenize
|
2013-04-02 01:51:21 +02:00
|
|
|
|
2013-03-03 17:18:13 +01:00
|
|
|
import ast
|
2013-04-02 01:51:21 +02:00
|
|
|
import sys
|
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():
|
2013-03-06 04:15:45 +01:00
|
|
|
"Make sure AST breakage can happen"
|
2013-03-03 19:12:23 +01:00
|
|
|
try:
|
2013-03-04 01:40:46 +01:00
|
|
|
hy_compile("foo")
|
2013-04-02 02:00:37 +02:00
|
|
|
assert True is False
|
2013-03-03 19:12:23 +01:00
|
|
|
except HyCompileError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-04-03 11:25:17 +02:00
|
|
|
def test_ast_bad_if_0_arg():
|
|
|
|
"Make sure AST can't compile invalid if"
|
|
|
|
try:
|
|
|
|
hy_compile(tokenize("(if)"))
|
|
|
|
assert False
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_ast_bad_if_1_arg():
|
|
|
|
"Make sure AST can't compile invalid if"
|
|
|
|
try:
|
|
|
|
hy_compile(tokenize("(if foobar)"))
|
|
|
|
assert False
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_ast_valid_if():
|
|
|
|
"Make sure AST can't compile invalid if"
|
|
|
|
hy_compile(tokenize("(if foo bar)"))
|
|
|
|
|
|
|
|
|
2013-04-03 19:55:09 +02:00
|
|
|
def test_ast_bad_while_0_arg():
|
|
|
|
"Make sure AST can't compile invalid while"
|
|
|
|
try:
|
|
|
|
hy_compile(tokenize("(while)"))
|
|
|
|
assert False
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_ast_bad_while_1_arg():
|
|
|
|
"Make sure AST can't compile invalid while"
|
|
|
|
try:
|
|
|
|
hy_compile(tokenize("(while (true))"))
|
|
|
|
assert False
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_ast_valid_while():
|
|
|
|
"Make sure AST can't compile invalid while"
|
|
|
|
hy_compile(tokenize("(while foo bar)"))
|
|
|
|
|
|
|
|
|
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-04 01:40:46 +01:00
|
|
|
code = hy_compile(tokenize("(foo bar)")).body[0]
|
2013-03-05 01:12:57 +01:00
|
|
|
tree = ast.Expr(value=ast.Call(
|
2013-03-03 17:18:13 +01:00
|
|
|
func=ast.Name(
|
|
|
|
id="foo",
|
|
|
|
ctx=ast.Load(),
|
|
|
|
),
|
|
|
|
args=[
|
|
|
|
ast.Name(id="bar", ctx=ast.Load())
|
|
|
|
],
|
|
|
|
keywords=[],
|
|
|
|
starargs=None,
|
|
|
|
kwargs=None,
|
2013-03-05 01:12:57 +01:00
|
|
|
))
|
|
|
|
|
|
|
|
_ast_spotcheck("value.func.id", code, tree)
|
2013-03-05 15:08:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_ast_anon_fns_basics():
|
|
|
|
""" Ensure anon fns work. """
|
|
|
|
code = hy_compile(tokenize("(fn (x) (* x x))")).body[0]
|
|
|
|
assert type(code) == ast.FunctionDef
|
2013-04-02 01:38:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_ast_non_decoratable():
|
|
|
|
""" Ensure decorating garbage breaks """
|
|
|
|
try:
|
|
|
|
hy_compile(tokenize("(decorate-with (foo) (* x x))"))
|
2013-04-02 02:00:37 +02:00
|
|
|
assert True is False
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_ast_non_kwapplyable():
|
|
|
|
""" Ensure kwapply breaks """
|
|
|
|
code = tokenize("(kwapply foo bar)")
|
|
|
|
code[0][2] = None
|
|
|
|
try:
|
|
|
|
hy_compile(code)
|
|
|
|
assert True is False
|
2013-04-02 01:38:58 +02:00
|
|
|
except TypeError:
|
|
|
|
pass
|
2013-04-02 01:51:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_ast_print():
|
|
|
|
code = hy_compile(tokenize("(print \"foo\")")).body[0]
|
2013-04-02 04:07:05 +02:00
|
|
|
|
2013-04-02 01:51:21 +02:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
assert type(code.value) == ast.Call
|
|
|
|
return
|
|
|
|
assert type(code) == ast.Print
|
2013-04-02 01:53:04 +02:00
|
|
|
|
|
|
|
|
2013-04-02 04:07:05 +02:00
|
|
|
def test_ast_tuple():
|
|
|
|
""" Ensure tuples work. """
|
2013-04-02 01:53:04 +02:00
|
|
|
code = hy_compile(tokenize("(, 1 2 3)")).body[0].value
|
|
|
|
assert type(code) == ast.Tuple
|