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:
parent
1499d49878
commit
caa53fb095
@ -1,7 +1,6 @@
|
|||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
import hy # noqa
|
import hy # noqa
|
||||||
|
from hy._compat import PY3
|
||||||
from .native_tests.cons import * # noqa
|
from .native_tests.cons import * # noqa
|
||||||
from .native_tests.defclass import * # noqa
|
from .native_tests.defclass import * # noqa
|
||||||
from .native_tests.math 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.walk import * # noqa
|
||||||
from .native_tests.contrib.multi import * # noqa
|
from .native_tests.contrib.multi import * # noqa
|
||||||
from .native_tests.contrib.curry import * # noqa
|
from .native_tests.contrib.curry import * # noqa
|
||||||
|
|
||||||
|
if PY3:
|
||||||
|
from .native_tests.py3_only_tests import * # noqa
|
||||||
|
@ -27,6 +27,7 @@ from hy.compiler import hy_compile
|
|||||||
from hy.errors import HyCompileError, HyTypeError
|
from hy.errors import HyCompileError, HyTypeError
|
||||||
from hy.lex.exceptions import LexException
|
from hy.lex.exceptions import LexException
|
||||||
from hy.lex import tokenize
|
from hy.lex import tokenize
|
||||||
|
from hy._compat import PY3
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
@ -111,23 +112,29 @@ def test_ast_good_do():
|
|||||||
def test_ast_good_throw():
|
def test_ast_good_throw():
|
||||||
"Make sure AST can compile valid throw"
|
"Make sure AST can compile valid throw"
|
||||||
can_compile("(throw)")
|
can_compile("(throw)")
|
||||||
can_compile("(throw 1)")
|
can_compile("(throw Exception)")
|
||||||
|
|
||||||
|
|
||||||
def test_ast_bad_throw():
|
def test_ast_bad_throw():
|
||||||
"Make sure AST can't compile invalid 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():
|
def test_ast_good_raise():
|
||||||
"Make sure AST can compile valid raise"
|
"Make sure AST can compile valid raise"
|
||||||
can_compile("(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():
|
def test_ast_bad_raise():
|
||||||
"Make sure AST can't compile invalid 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():
|
def test_ast_good_try():
|
||||||
|
@ -100,21 +100,6 @@
|
|||||||
(assert initialized)
|
(assert initialized)
|
||||||
(assert (test-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 []
|
(defn test-if-python2 []
|
||||||
(import sys)
|
(import sys)
|
||||||
(assert (= (get sys.version_info 0)
|
(assert (= (get sys.version_info 0)
|
||||||
|
15
tests/native_tests/py3_only_tests.hy
Normal file
15
tests/native_tests/py3_only_tests.hy
Normal 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)))))
|
@ -23,6 +23,7 @@
|
|||||||
# DEALINGS IN THE SOFTWARE.
|
# DEALINGS IN THE SOFTWARE.
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from hy._compat import PY3
|
||||||
|
|
||||||
|
|
||||||
def run_cmd(cmd, stdin_data=None):
|
def run_cmd(cmd, stdin_data=None):
|
||||||
@ -128,6 +129,9 @@ def test_hy2py():
|
|||||||
for dirpath, dirnames, filenames in os.walk("tests/native_tests"):
|
for dirpath, dirnames, filenames in os.walk("tests/native_tests"):
|
||||||
for f in filenames:
|
for f in filenames:
|
||||||
if f.endswith(".hy"):
|
if f.endswith(".hy"):
|
||||||
|
if f == "py3_only_tests.hy" and not PY3:
|
||||||
|
continue
|
||||||
|
else:
|
||||||
i += 1
|
i += 1
|
||||||
ret = run_cmd("hy2py -s -a "
|
ret = run_cmd("hy2py -s -a "
|
||||||
+ os.path.join(dirpath, f))
|
+ os.path.join(dirpath, f))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user