From caa53fb09589ac7631a5ef9a2e883f55b649499f Mon Sep 17 00:00:00 2001 From: Allison Kaptur Date: Thu, 1 May 2014 16:31:45 -0400 Subject: [PATCH] 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. --- tests/__init__.py | 6 ++++-- tests/compilers/test_ast.py | 15 +++++++++++---- tests/native_tests/native_macros.hy | 15 --------------- tests/native_tests/py3_only_tests.hy | 15 +++++++++++++++ tests/test_bin.py | 16 ++++++++++------ 5 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 tests/native_tests/py3_only_tests.hy diff --git a/tests/__init__.py b/tests/__init__.py index 8f427f8..56ab9fc 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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 diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 1cf5519..9d8cf69 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -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(): diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index 571e824..0fdd87e 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -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) diff --git a/tests/native_tests/py3_only_tests.hy b/tests/native_tests/py3_only_tests.hy new file mode 100644 index 0000000..ba3ecca --- /dev/null +++ b/tests/native_tests/py3_only_tests.hy @@ -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))))) diff --git a/tests/test_bin.py b/tests/test_bin.py index ed74ae8..055a46b 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -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