if: emit error on too many args

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-04-05 17:37:55 +02:00
parent 485c98c799
commit b36018981a
2 changed files with 12 additions and 1 deletions

View File

@ -176,8 +176,10 @@ class HyASTCompiler(object):
raise TypeError("if expects at least 2 arguments, got 1")
body = self._code_branch(self.compile(body))
orel = []
if len(expr) > 0:
if len(expr) == 1:
orel = self._code_branch(self.compile(expr.pop(0)))
elif len(expr) > 1:
raise TypeError("if expects 2 or 3 arguments, got %d" % (len(expr) + 2))
return ast.If(test=test,
body=body,

View File

@ -61,6 +61,15 @@ def test_ast_bad_if_1_arg():
pass
def test_ast_bad_if_too_much_arg():
"Make sure AST can't compile invalid if"
try:
hy_compile(tokenize("(if 1 2 3 4 5)"))
assert False
except TypeError:
pass
def test_ast_valid_if():
"Make sure AST can't compile invalid if"
hy_compile(tokenize("(if foo bar)"))