Merge pull request #1547 from Kodiologist/hy2py-testing

Improve testing of hy2py
This commit is contained in:
Kodi Arfer 2018-04-08 20:46:17 -07:00 committed by GitHub
commit 736426fc12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 269 additions and 18 deletions

View File

@ -30,6 +30,8 @@ New Features
Bug Fixes
------------------------------
* `hy2py` should now output legal Python code equivalent to the input Hy
code in all cases, modulo some outstanding bugs in `astor`
* Fix `(return)` so it works correctly to exit a Python 2 generator
* Fixed a case where `->` and `->>` duplicated an argument
* Fixed bugs that caused `defclass` to drop statements or crash

147
tests/resources/pydemo.hy Normal file
View File

@ -0,0 +1,147 @@
;; Copyright 2018 the authors.
;; This file is part of Hy, which is free software licensed under the Expat
;; license. See the LICENSE.
;; This Hy module is intended to concisely demonstrate all of
;; Python's major syntactic features for the purpose of testing hy2py.
(setv mystring (* "foo" 3))
(setv long-string "This is a very long string literal, which would surely exceed any limitations on how long a line or a string literal can be. The string literal alone exceeds 256 characters. It also has a character outside the Basic Multilingual Plane: 😂. Here's a double quote: \". Here are some escaped newlines:\n\n\nHere is a literal newline:
Call me Ishmael. Some years agonever mind how long preciselyhaving little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking peoples hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me.")
(setv identifier-that-has💯-to-be-mangled "ponies")
(setv mynumber (+ 1 2))
(setv myhex 0x123)
(setv mylong 1234567890987654321234567890987654321)
(setv myfloat 3.34e15)
(setv mynan NaN)
(setv pinf Inf)
(setv ninf -Inf)
;(setv mycomplex -Inf+5j) ; https://github.com/berkerpeksag/astor/issues/100
(setv num-expr (+ 3 (* 5 2) (- 6 (// 8 2)) (* (+ 1 2) (- 3 5)))) ; = 9
(setv mylist [1 2 3])
(setv mytuple (, "a" "b" "c"))
(setv myset #{4 5 6})
(setv mydict {7 8 9 900 10 15})
(setv mylistcomp (list-comp x [x (range 10)] (% x 2)))
(setv mysetcomp (set-comp x [x (range 5)] (not (% x 2))))
(setv mydictcomp (dict-comp k (.upper k) [k "abcde"] (!= k "c")))
(setv mygenexpr (genexpr x [x (cycle [1 2 3])] (!= x 2)))
(setv attr-ref str.upper)
(setv subscript (get "hello" 2))
(setv myslice (cut "hello" 1 None 2))
(setv call (len "hello"))
(setv comparison (< "a" "b" "c"))
(setv boolexpr (and (or True False) (not (and True False))))
(setv condexpr (if "" "x" "y"))
(setv mylambda (fn [x] (+ x "z")))
(setv augassign 103)
(//= augassign 4)
(setv delstatement ["a" "b" "c" "d" "e"])
(del (get delstatement 1))
(import math)
(import [math [sqrt]])
(import [math [sin :as sine]])
(import [datetime [*]])
(setv if-block "")
(if 0
(do
(+= if-block "a")
(+= if-block "b"))
(do
(+= if-block "c")
(+= if-block "d")))
(setv counter 4)
(setv while-block "")
(while counter
(+= while-block "x")
(-= counter 1)
(else
(+= while-block "e")))
(setv counter2 8)
(setv cont-and-break "")
(while counter2
(+= cont-and-break "x")
(-= counter2 1)
(when (= counter2 5)
(continue))
(+= cont-and-break "y")
(when (= counter2 3)
(break))
(+= cont-and-break "z"))
(setv for-block "")
(for [x ["fo" "fi" "fu"]]
(setv for-block (+ x for-block)))
(try
(assert (= 1 0))
(except [_ AssertionError]
(setv caught-assertion True))
(finally
(setv ran-finally True)))
(try
(raise (ValueError "payload"))
(except [e ValueError]
(setv myraise (str e))))
(try
1
(except [e ValueError]
(raise))
(else
(setv ran-try-else True)))
(defn fun [a b &optional [c 9] [d 10] &rest args &kwargs kwargs]
"function docstring"
[a b c d args (sorted (.items kwargs))])
(setv funcall1 (fun 1 2 3 4 "a" "b" "c" :k1 "v1" :k2 "v2"))
(setv funcall2 (fun 7 8 #* [9 10 11] #** {"x1" "y1" "x2" "y2"}))
(defn returner []
(return 1)
(raise (ValueError))
2)
(setv myret (returner))
(defn generator []
(for [x "abc"]
(yield x)))
(setv myyield (list (generator)))
(with-decorator (fn [f] (setv f.newattr "hello") f)
(defn mydecorated []))
(setv myglobal 102)
(defn set-global []
(global myglobal)
(+= myglobal 1))
(set-global)
(defclass C1 []) ; Force the creation of a `pass` statement.
(defclass C2 [C1]
"class docstring"
[attr1 5 attr2 6]
(setv attr3 7))
(import [contextlib [closing]])
(setv closed [])
(defclass Closeable []
[close (fn [self] (.append closed self.x))])
(with [c1 (closing (Closeable)) c2 (closing (Closeable))]
(setv c1.x "v1")
(setv c2.x "v2"))

View File

@ -248,24 +248,6 @@ def test_bin_hyc_missing_file():
assert "[Errno 2]" in err
def test_hy2py():
i = 0
for dirpath, dirnames, filenames in os.walk("tests/native_tests"):
for f in filenames:
if f.endswith(".hy"):
if "py3_only" in f and not PY3:
continue
if "py35_only" in f and not PY35:
continue
if "py36_only" in f and not PY36:
continue
i += 1
output, err = run_cmd("hy2py -s -a " + quote(os.path.join(dirpath, f)))
assert len(output) > 1, f
assert len(err) == 0, f
assert i
def test_bin_hy_builtins():
# hy.cmdline replaces builtins.exit and builtins.quit
# for use by hy's repl.

120
tests/test_hy2py.py Normal file
View File

@ -0,0 +1,120 @@
# -*- encoding: utf-8 -*-
# Copyright 2018 the authors.
# This file is part of Hy, which is free software licensed under the Expat
# license. See the LICENSE.
import math, itertools
from hy import mangle
from hy._compat import PY36, PY37
def test_direct_import():
import tests.resources.pydemo
assert_stuff(tests.resources.pydemo)
def test_hy2py_import(tmpdir):
import subprocess
python_code = subprocess.check_output(
["hy2py", "tests/resources/pydemo.hy"]).decode("UTF-8")
path = tmpdir.join("pydemo.py")
path.write(python_code)
assert_stuff(import_from_path("pydemo", path))
def assert_stuff(m):
astor_issue101_workaround = PY37
assert m.mystring == "foofoofoo"
assert m.long_string == u"This is a very long string literal, which would surely exceed any limitations on how long a line or a string literal can be. The string literal alone exceeds 256 characters. It also has a character outside the Basic Multilingual Plane: 😂. Here's a double quote: \". Here are some escaped newlines:\n\n\nHere is a literal newline:\nCall me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking peoples hats off—then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me."
assert getattr(m, mangle(u"identifier-that-has☝💯☝-to-be-mangled")) == "ponies"
assert m.mynumber == 3
assert m.myhex == 0x123
assert m.mylong - 1234567890987654321234567890987654320 == 1
assert m.myfloat == 3.34e15
assert math.isnan(m.mynan)
assert math.isinf(m.pinf)
assert m.pinf > 0
assert math.isinf(m.ninf)
assert m.ninf < 0
assert m.num_expr == 9
assert m.mylist == [1, 2, 3]
assert m.mytuple == ("a", "b", "c")
assert m.myset == {4, 5, 6}
assert m.mydict == {7: 8, 9: 900, 10: 15}
assert m.mylistcomp == [1, 3, 5, 7, 9]
assert m.mysetcomp == {0, 2, 4}
assert m.mydictcomp == dict(a="A", b="B", d="D", e="E")
assert type(m.mygenexpr) is type( (x for x in [1, 2, 3]) )
assert list(itertools.islice(m.mygenexpr, 5)) == [1, 3, 1, 3, 1]
assert m.attr_ref is str.upper
assert m.subscript == "l"
assert m.myslice == "el"
assert m.call == 5
assert m.comparison is True
assert m.boolexpr is True
assert m.condexpr == "y"
assert type(m.mylambda) is type(lambda x: x + "z")
assert m.mylambda("a") == "az"
assert m.augassign == 25
assert m.delstatement == ["a", "c", "d", "e"]
assert m.math is math
assert m.sqrt is math.sqrt
assert m.sine is math.sin
import datetime
assert m.timedelta is datetime.timedelta
assert m.if_block == "cd"
assert m.while_block == "xxxxe"
assert m.cont_and_break == "xyzxyzxxyzxy"
assert m.for_block == "fufifo"
assert m.caught_assertion is True
assert m.ran_finally is True
assert m.myraise == "payload"
assert m.ran_try_else is True
assert type(m.fun) is type(lambda x: x)
if not astor_issue101_workaround:
assert m.fun.__doc__ == "function docstring"
assert m.funcall1 == [
1, 2, 3, 4, ("a", "b", "c"), [("k1", "v1"), ("k2", "v2")]]
assert m.funcall2 == [
7, 8, 9, 10, (11,), [("x1", "y1"), ("x2", "y2")]]
assert m.myret == 1
assert m.myyield == ["a", "b", "c"]
assert m.mydecorated.newattr == "hello"
assert m.myglobal == 103
class C: pass
assert type(m.C1) is type(C)
if not astor_issue101_workaround:
assert m.C2.__doc__ == "class docstring"
assert issubclass(m.C2, m.C1)
assert (m.C2.attr1, m.C2.attr2, m.C2.attr3) == (5, 6, 7)
assert m.closed == ["v2", "v1"]
def import_from_path(name, path):
if PY36:
import importlib.util
spec = importlib.util.spec_from_file_location(name, path)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
else:
import imp
m = imp.load_source(name, str(path))
return m