Kaydet (Commit) acc8ec87 authored tarafından Patrick Maupin's avatar Patrick Maupin Kaydeden (comit) GitHub

Refactor tests (#74)

* Refactor tests
 - Make it easier to test against other packages

* Split out optional tests and use assertXXX naming conventions

* Fix Python 3 test intrapackage import

* Fix Python 3 test intrapackage import
üst a957d588
import ast
try:
import unittest2 as unittest
except ImportError:
import unittest
import test_code_gen
import astunparse
class MyTests(test_code_gen.CodegenTestCase):
to_source = staticmethod(astunparse.unparse)
# Just see if it'll do anything good at all
assertSrcRoundtrips = test_code_gen.CodegenTestCase.assertAstRoundtrips
# Don't look for exact comparison; see if ASTs match
def assertSrcEqual(self, src1, src2):
self.assertAstEqual(ast.parse(src1), ast.parse(src2))
if __name__ == '__main__':
unittest.main()
This diff is collapsed.
import ast
import sys
import warnings
try:
import unittest2 as unittest
except ImportError:
......@@ -17,5 +17,46 @@ class GetSymbolTestCase(unittest.TestCase):
self.assertEqual('@', astor.get_op_symbol(ast.MatMult()))
class DeprecationTestCase(unittest.TestCase):
def test_deprecation(self):
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
ast1 = astor.code_to_ast.parse_file(__file__)
src1 = astor.to_source(ast1)
ast2 = astor.parsefile(__file__)
src2 = astor.codegen.to_source(ast2)
self.assertEqual(len(w), 2)
w = [warnings.formatwarning(x.message, x.category,
x.filename, x.lineno) for x in w]
w = [x.rsplit(':', 1)[-1].strip() for x in w]
self.assertEqual(w[0], 'astor.parsefile is deprecated. '
'Please use astor.code_to_ast.parse_file.\n'
' ast2 = astor.parsefile(__file__)')
self.assertEqual(w[1], 'astor.codegen is deprecated. '
'Please use astor.code_gen.\n'
' src2 = astor.codegen.to_source(ast2)')
self.assertEqual(src1, src2)
class FastCompareTestCase(unittest.TestCase):
def test_fast_compare(self):
fast_compare = astor.node_util.fast_compare
def check(a, b):
ast_a = ast.parse(a)
ast_b = ast.parse(b)
dump_a = astor.dump_tree(ast_a)
dump_b = astor.dump_tree(ast_b)
self.assertEqual(dump_a == dump_b, fast_compare(ast_a, ast_b))
check('a = 3', 'a = 3')
check('a = 3', 'a = 5')
check('a = 3 - (3, 4, 5)', 'a = 3 - (3, 4, 5)')
check('a = 3 - (3, 4, 5)', 'a = 3 - (3, 4, 6)')
if __name__ == '__main__':
unittest.main()
"""
Part of the astor library for Python AST manipulation
License: 3-clause BSD
Copyright (c) 2014 Berker Peksag
Copyright (c) 2015, 2017 Patrick Maupin
Use this by putting a link to astunparse's common.py test file.
"""
try:
import unittest2 as unittest
except ImportError:
import unittest
try:
from test_code_gen import Comparisons
except ImportError:
from .test_code_gen import Comparisons
try:
from astunparse_common import AstunparseCommonTestCase
except ImportError:
AstunparseCommonTestCase = None
if AstunparseCommonTestCase is not None:
class UnparseTestCase(AstunparseCommonTestCase, unittest.TestCase,
Comparisons):
def check_roundtrip(self, code1, mode=None):
self.assertAstRoundtrips(code1)
def test_files(self):
""" Don't bother -- we do this manually and more thoroughly """
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment