diff --git a/.travis.yml b/.travis.yml index 4e9f813..3b973d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ # Config file for automatic testing at travis-ci.org - +sudo: false language: python python: @@ -9,10 +9,14 @@ python: - "3.3" - "3.4" - "3.5" - - "3.6" + - "3.6.0" + - "3.6.1" # command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors -install: pip install -r requirements.txt +install: + - pip install -r requirements.txt + - pip install typed_ast || true # command to run tests, e.g. python setup.py test script: python setup.py test +cache: pip diff --git a/tests/common.py b/tests/common.py index f930308..05e4323 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1,6 +1,14 @@ import codecs import os import sys +import ast + +if sys.version_info >= (3, 3): + from typed_ast import ast3, ast27 +else: + ast3 = None + ast27 = None + if sys.version_info < (2, 7): import unittest2 as unittest else: @@ -162,7 +170,7 @@ async def f(): suite1 """ -class AstunparseCommonTestCase: +class AstunparseCommonTestCase(object): # Tests for specific bugs found in earlier versions of unparse def assertASTEqual(self, dump1, dump2): @@ -171,6 +179,9 @@ def assertASTEqual(self, dump1, dump2): def check_roundtrip(self, code1, filename="internal", mode="exec"): raise NotImplementedError() + def compile(self, code, filename, mode): + return self.ast.parse(code, filename, mode) + test_directories = [ os.path.join(getattr(sys, 'real_prefix', sys.prefix), 'lib', 'python%s.%s' % sys.version_info[:2])] @@ -293,6 +304,9 @@ def test_joined_str(self): self.check_roundtrip('f"{key}={value!s}"') self.check_roundtrip('f"{key}={value!r}"') self.check_roundtrip('f"{key}={value!a}"') + + @unittest.skipIf(sys.version_info != (3, 6, 0), "Only supported on 3.6.0") + def test_joined_str_361(self): self.check_roundtrip('f"{key:4}={value!s}"') self.check_roundtrip('f"{key:02}={value!r}"') self.check_roundtrip('f"{key:6}={value!a}"') @@ -400,3 +414,22 @@ def test_async_with(self): @unittest.skipIf(sys.version_info < (3, 5), "Not supported < 3.5") def test_async_with_as(self): self.check_roundtrip(async_with_as) + + +class ASTTestCase(AstunparseCommonTestCase): + ast = ast + + +if ast3: + class AST3TestCase(AstunparseCommonTestCase): + ast = ast3 +else: + AST3TestCase = None + + +if ast27: + class AST2TestCase(AstunparseCommonTestCase): + ast = ast27 + +else: + AST2TestCase = None diff --git a/tests/test_dump.py b/tests/test_dump.py index dbad2da..e000d55 100644 --- a/tests/test_dump.py +++ b/tests/test_dump.py @@ -1,4 +1,3 @@ -import ast import re import sys if sys.version_info < (2, 7): @@ -7,9 +6,9 @@ import unittest import astunparse -from tests.common import AstunparseCommonTestCase +from tests.common import ASTTestCase, AST3TestCase, AST2TestCase -class DumpTestCase(AstunparseCommonTestCase, unittest.TestCase): +class DumpTestCase(object): def assertASTEqual(self, dump1, dump2): # undo the pretty-printing @@ -18,7 +17,21 @@ def assertASTEqual(self, dump1, dump2): self.assertEqual(dump1, dump2) def check_roundtrip(self, code1, filename="internal", mode="exec"): - ast_ = compile(str(code1), filename, mode, ast.PyCF_ONLY_AST) + ast_ = self.compile(str(code1), filename, mode) dump1 = astunparse.dump(ast_) - dump2 = ast.dump(ast_) + dump2 = self.ast.dump(ast_) self.assertASTEqual(dump1, dump2) + + +class ASTDumpTestCase(DumpTestCase, ASTTestCase, unittest.TestCase): + pass + + +if AST3TestCase: + class AST3DumpTestCase(DumpTestCase, AST3TestCase, unittest.TestCase): + pass + + +if AST2TestCase: + class AST3DumpTestCase(DumpTestCase, AST2TestCase, unittest.TestCase): + pass diff --git a/tests/test_unparse.py b/tests/test_unparse.py index 774bd7c..71a22c5 100644 --- a/tests/test_unparse.py +++ b/tests/test_unparse.py @@ -6,15 +6,29 @@ import unittest import astunparse -from tests.common import AstunparseCommonTestCase +from tests.common import ASTTestCase, AST3TestCase, AST2TestCase -class UnparseTestCase(AstunparseCommonTestCase, unittest.TestCase): +class UnparseTestCase(object): def assertASTEqual(self, ast1, ast2): self.assertEqual(ast.dump(ast1), ast.dump(ast2)) def check_roundtrip(self, code1, filename="internal", mode="exec"): - ast1 = compile(str(code1), filename, mode, ast.PyCF_ONLY_AST) + ast1 = self.compile(str(code1), filename, mode) code2 = astunparse.unparse(ast1) - ast2 = compile(code2, filename, mode, ast.PyCF_ONLY_AST) + ast2 = self.compile(code2, filename, mode) self.assertASTEqual(ast1, ast2) + + +class ASTUnparseTestCase(UnparseTestCase, ASTTestCase, unittest.TestCase): + pass + + +if AST3TestCase: + class AST3UnparseTestCase(UnparseTestCase, AST3TestCase, unittest.TestCase): + pass + + +if AST2TestCase: + class AST3UnparseTestCase(UnparseTestCase, AST2TestCase, unittest.TestCase): + pass