Tests for explicit exception chaining

This also breaks out the PY3 only tests into their own file.  We need to do this because raise from is a syntax error in PY2, so we can't rely on the previous hack of catching a HyCompileError - it would compile fine through Hy and then be a syntax error in Python.
This commit is contained in:
Allison Kaptur 2014-05-01 16:31:45 -04:00
parent 1499d49878
commit caa53fb095
5 changed files with 40 additions and 27 deletions

View File

@ -1,7 +1,6 @@
#
import hy # noqa
from hy._compat import PY3
from .native_tests.cons import * # noqa
from .native_tests.defclass import * # noqa
from .native_tests.math import * # noqa
@ -20,3 +19,6 @@ from .native_tests.contrib.meth import * # noqa
from .native_tests.contrib.walk import * # noqa
from .native_tests.contrib.multi import * # noqa
from .native_tests.contrib.curry import * # noqa
if PY3:
from .native_tests.py3_only_tests import * # noqa

View File

@ -27,6 +27,7 @@ from hy.compiler import hy_compile
from hy.errors import HyCompileError, HyTypeError
from hy.lex.exceptions import LexException
from hy.lex import tokenize
from hy._compat import PY3
import ast
@ -111,23 +112,29 @@ def test_ast_good_do():
def test_ast_good_throw():
"Make sure AST can compile valid throw"
can_compile("(throw)")
can_compile("(throw 1)")
can_compile("(throw Exception)")
def test_ast_bad_throw():
"Make sure AST can't compile invalid throw"
cant_compile("(raise 1 2 3)")
cant_compile("(throw Exception Exception)")
def test_ast_good_raise():
"Make sure AST can compile valid raise"
can_compile("(raise)")
can_compile("(raise 1)")
can_compile("(raise Exception)")
can_compile("(raise e)")
if PY3:
def test_ast_raise_from():
can_compile("(raise Exception :from NameError)")
def test_ast_bad_raise():
"Make sure AST can't compile invalid raise"
cant_compile("(raise 1 2 3)")
cant_compile("(raise Exception Exception)")
def test_ast_good_try():

View File

@ -100,21 +100,6 @@
(assert initialized)
(assert (test-initialized))
(defn test-yield-from []
"NATIVE: testing yield from"
(try
(eval
'(do (defn yield-from-test []
(for* [i (range 3)]
(yield i))
(yield-from [1 2 3]))
(assert (= (list (yield-from-test)) [0 1 2 1 2 3]))))
(catch [e HyCompileError]
;; Yup, this should happen on non-Python3.3 thingies
(assert (not PY33)))
(else (assert PY33))))
(defn test-if-python2 []
(import sys)
(assert (= (get sys.version_info 0)

View File

@ -0,0 +1,15 @@
;; Tests where the emited code relies on Python 3.
;; Conditionally included in nosetests runs.
(defn test-yield-from []
"NATIVE: testing yield from"
(do (defn yield-from-test []
(for* [i (range 3)]
(yield i))
(yield-from [1 2 3]))
(assert (= (list (yield-from-test)) [0 1 2 1 2 3]))))
(defn test-exception-cause []
(try (raise ValueError :from NameError)
(except [e [ValueError]]
(assert (= (type (. e __cause__)) NameError)))))

View File

@ -23,6 +23,7 @@
# DEALINGS IN THE SOFTWARE.
import os
import subprocess
from hy._compat import PY3
def run_cmd(cmd, stdin_data=None):
@ -128,12 +129,15 @@ def test_hy2py():
for dirpath, dirnames, filenames in os.walk("tests/native_tests"):
for f in filenames:
if f.endswith(".hy"):
i += 1
ret = run_cmd("hy2py -s -a "
+ os.path.join(dirpath, f))
assert ret[0] == 0, f
assert len(ret[1]) > 1, f
assert len(ret[2]) == 0, f
if f == "py3_only_tests.hy" and not PY3:
continue
else:
i += 1
ret = run_cmd("hy2py -s -a "
+ os.path.join(dirpath, f))
assert ret[0] == 0, f
assert len(ret[1]) > 1, f
assert len(ret[2]) == 0, f
assert i