Use test functions instead of unittest.TestCase

This commit is contained in:
Kodi Arfer 2017-04-17 15:49:02 -07:00
parent 55c205f87e
commit 8afd13cb16

View File

@ -20,33 +20,25 @@
# DEALINGS IN THE SOFTWARE. # DEALINGS IN THE SOFTWARE.
import ast import ast
import sys
from hy import compiler from hy import compiler
from hy.models import HyExpression, HyList, HySymbol, HyInteger from hy.models import HyExpression, HyList, HySymbol, HyInteger
from hy._compat import PY3 from hy._compat import PY3
if sys.version_info[0] <= 2 and sys.version_info[1] <= 6:
import unittest2 as unittest def test_builds_with_dash():
assert callable(compiler.builds("foobar"))
assert callable(compiler.builds("foo_bar"))
assert callable(compiler.builds("-"))
try:
compiler.builds("foobar-with-dash-")
except TypeError as e:
assert "*post* translated strings" in str(e)
else: else:
import unittest assert False
class CompilerTest(unittest.TestCase): def make_expression(*args):
def test_builds_with_dash(self):
self.assert_(callable(compiler.builds("foobar")))
self.assert_(callable(compiler.builds("foo_bar")))
self.assert_(callable(compiler.builds("-")))
self.assertRaisesRegexp(TypeError,
r"\*post\* translated strings",
compiler.builds, "foobar-with-dash-")
class HyASTCompilerTest(unittest.TestCase):
@staticmethod
def _make_expression(*args):
h = HyExpression(args) h = HyExpression(args)
h.start_line = 1 h.start_line = 1
h.end_line = 1 h.end_line = 1
@ -54,33 +46,30 @@ class HyASTCompilerTest(unittest.TestCase):
h.end_column = 1 h.end_column = 1
return h.replace(h) return h.replace(h)
def setUp(self):
self.c = compiler.HyASTCompiler('test')
def test_compiler_bare_names(self): def test_compiler_bare_names():
""" """
Check that the compiler doesn't drop bare names from code branches Check that the compiler doesn't drop bare names from code branches
""" """
ret = self.c.compile(self._make_expression(HySymbol("do"), e = make_expression(HySymbol("do"),
HySymbol("a"), HySymbol("a"),
HySymbol("b"), HySymbol("b"),
HySymbol("c"))) HySymbol("c"))
ret = compiler.HyASTCompiler('test').compile(e)
# We expect two statements and a final expr. # We expect two statements and a final expr.
self.assertEqual(len(ret.stmts), 2)
stmt = ret.stmts[0]
self.assertIsInstance(stmt, ast.Expr)
self.assertIsInstance(stmt.value, ast.Name)
self.assertEqual(stmt.value.id, "a")
stmt = ret.stmts[1]
self.assertIsInstance(stmt, ast.Expr)
self.assertIsInstance(stmt.value, ast.Name)
self.assertEqual(stmt.value.id, "b")
expr = ret.expr
self.assertIsInstance(expr, ast.Name)
self.assertEqual(expr.id, "c")
def test_compiler_yield_return(self): assert len(ret.stmts) == 2
for stmt, symbol in zip(ret.stmts, "ab"):
assert isinstance(stmt, ast.Expr)
assert isinstance(stmt.value, ast.Name)
assert stmt.value.id == symbol
assert isinstance(ret.expr, ast.Name)
assert ret.expr.id == "c"
def test_compiler_yield_return():
""" """
Check that the compiler correctly generates return statements for Check that the compiler correctly generates return statements for
a generator function. In Python versions prior to 3.3, the return a generator function. In Python versions prior to 3.3, the return
@ -88,28 +77,28 @@ class HyASTCompilerTest(unittest.TestCase):
should not generate a return statement. From 3.3 onwards a return should not generate a return statement. From 3.3 onwards a return
value should be generated. value should be generated.
""" """
ret = self.c.compile_function_def( e = make_expression(HySymbol("fn"),
self._make_expression(HySymbol("fn"),
HyList(), HyList(),
HyExpression([HySymbol("yield"), HyExpression([HySymbol("yield"),
HyInteger(2)]), HyInteger(2)]),
HyExpression([HySymbol("+"), HyExpression([HySymbol("+"),
HyInteger(1), HyInteger(1),
HyInteger(1)]))) HyInteger(1)]))
ret = compiler.HyASTCompiler('test').compile_function_def(e)
self.assertEqual(len(ret.stmts), 1) assert len(ret.stmts) == 1
stmt = ret.stmts[0] stmt, = ret.stmts
self.assertIsInstance(stmt, ast.FunctionDef) assert isinstance(stmt, ast.FunctionDef)
body = stmt.body body = stmt.body
self.assertEquals(len(body), 2) assert len(body) == 2
self.assertIsInstance(body[0], ast.Expr) assert isinstance(body[0], ast.Expr)
self.assertIsInstance(body[0].value, ast.Yield) assert isinstance(body[0].value, ast.Yield)
if PY3: if PY3:
# From 3.3+, the final statement becomes a return value # From 3.3+, the final statement becomes a return value
self.assertIsInstance(body[1], ast.Return) assert isinstance(body[1], ast.Return)
self.assertIsInstance(body[1].value, ast.BinOp) assert isinstance(body[1].value, ast.BinOp)
else: else:
# In earlier versions, the expression is not returned # In earlier versions, the expression is not returned
self.assertIsInstance(body[1], ast.Expr) assert isinstance(body[1], ast.Expr)
self.assertIsInstance(body[1].value, ast.BinOp) assert isinstance(body[1].value, ast.BinOp)