From 6948fff1128b6da4c1fc45a16037a8461353a5f8 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Fri, 3 May 2013 22:50:50 +0200 Subject: [PATCH] Add a first test for the compiler Signed-off-by: Julien Danjou --- tests/compilers/test_compiler.py | 65 ++++++++++++++++++++++++++++++++ tox.ini | 6 +++ 2 files changed, 71 insertions(+) create mode 100644 tests/compilers/test_compiler.py diff --git a/tests/compilers/test_compiler.py b/tests/compilers/test_compiler.py new file mode 100644 index 0000000..b5b3846 --- /dev/null +++ b/tests/compilers/test_compiler.py @@ -0,0 +1,65 @@ +# Copyright (c) 2013 Julien Danjou +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +import ast +import sys + +if sys.version_info[0] <= 2 and sys.version_info[1] <= 6: + import unittest2 as unittest +else: + import unittest + + +from hy.models.expression import HyExpression +from hy.models.list import HyList +from hy.compiler import HyASTCompiler + + +class HyASTCompilerTest(unittest.TestCase): + + @staticmethod + def _make_expression(*args): + h = HyExpression(args) + h.start_line = 1 + h.end_line = 1 + h.start_column = 1 + h.end_column = 1 + return h + + def setUp(self): + self.c = HyASTCompiler() + + def test_fn_compiler_empty_function(self): + ret = self.c.compile_function_def( + self._make_expression("fn", HyList())) + self.assertEqual(ret.imports, {}) + + self.assertEqual(len(ret.stmts), 1) + stmt = ret.stmts[0] + self.assertIsInstance(stmt, ast.FunctionDef) + self.assertIsInstance(stmt.args, ast.arguments) + self.assertEqual(stmt.args.vararg, None) + self.assertEqual(stmt.args.kwarg, None) + self.assertEqual(stmt.args.defaults, []) + self.assertEqual(stmt.decorator_list, []) + self.assertEqual(len(stmt.body), 1) + self.assertIsInstance(stmt.body[0], ast.Pass) + + self.assertIsInstance(ret.expr, ast.Name) diff --git a/tox.ini b/tox.ini index 1cf91e8..8e4f011 100644 --- a/tox.ini +++ b/tox.ini @@ -6,6 +6,12 @@ deps = nose setuptools +[testenv:py26] +deps = + nose + setuptools + unittest2 + [testenv:flake8] deps = flake8 commands = flake8 hy bin tests