Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Config file for automatic testing at travis-ci.org

sudo: false
language: python

python:
Expand All @@ -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
35 changes: 34 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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])]
Expand Down Expand Up @@ -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}"')
Expand Down Expand Up @@ -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
23 changes: 18 additions & 5 deletions tests/test_dump.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ast
import re
import sys
if sys.version_info < (2, 7):
Expand All @@ -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
Expand All @@ -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
22 changes: 18 additions & 4 deletions tests/test_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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