Kaydet (Commit) 5f9ce56e authored tarafından Berker Peksag's avatar Berker Peksag

Add initial test suite.

See #3 for details.

Closes #4.
üst ed735231
language: python
python:
- "pypy"
- "2.6"
- "2.7"
- "3.2"
- "3.3"
- "3.4"
cache:
- $HOME/.pip-cache
install:
- pip install -r requirements-dev.txt --download-cache $HOME/.pip-cache
- if [[ $TRAVIS_PYTHON_VERSION == '2.6' ]]; then pip install unittest2; fi
- pip install --allow-all-external -e .
script: nosetests
wheel==0.23.0 wheel>=0.23.0
nose>=1.3.0
tox>=1.7.1
flake8>=2.1.0
class A(object):
pass
class B(object):
pass
def test():
with A() as a:
with B() as b:
print ("test1")
def test2():
with A() as a, B() as b:
print ("test2")
#!/usr/bin/env python
""" """
Part of the astor library for Python AST manipulation Part of the astor library for Python AST manipulation
License: 3-clause BSD License: 3-clause BSD
Copyright 2012 (c) Patrick Maupin Copyright 2014 (c) Berker Peksag
""" """
import fnmatch
import os
import sys
import ast import ast
import unittest
import textwrap
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import astor import astor
def findpy(root): class CodegenTestCase(unittest.TestCase):
if isinstance(root, str):
if os.path.isfile(root): def assertAstSourceEqual(self, source):
if root.endswith('.py'): self.assertEqual(astor.to_source(ast.parse(source)), source)
yield root
return def test_imports(self):
for fname in os.listdir(root): source = "import ast"
fname = os.path.join(root, fname) self.assertAstSourceEqual(source)
if os.path.isdir(fname): source = "import operator as op"
for fname in findpy(fname): self.assertAstSourceEqual(source)
yield fname source = "from math import floor"
elif fnmatch.fnmatch(fname, '*.py'): self.assertAstSourceEqual(source)
yield fname
else:
for dirname in root:
for fname in findpy(dirname):
yield fname
def testone(fname, f1=None, f2=None): def test_try_expect(self):
try: source = textwrap.dedent("""\
ast1 = astor.parsefile(fname) try:
except (SyntaxError, UnicodeDecodeError): 'spam'[10]
print("IGNORED %s" % fname) except IndexError:
return pass""")
dump1 = astor.dump(ast1) self.assertAstSourceEqual(source)
reconstitute = '# -*- coding: utf-8 -*-\n' + astor.to_source(ast1)
ast2 = ast.parse(reconstitute, fname)
dump2 = astor.dump(ast2)
ok = dump1 == dump2
print('%-8s%s' % ('OK' if dump1 == dump2 else 'FAIL', fname))
if not ok and f1 is not None and f2 is not None:
f1.write('\n\n***************************************\n%s\n***************************************\n\n\n' % fname)
f2.write('\n\n***************************************\n%s\n***************************************\n\n\n' % fname)
f1.write(dump1)
f2.write(dump2)
f = open('bad.txt', 'w')
f.write(reconstitute)
f.close()
return ok
source = textwrap.dedent("""\
try:
'spam'[10]
except IndexError as exc:
sys.stdout.write(exc)""")
self.assertAstSourceEqual(source)
def go(root, stoponfail=True, f1=None, f2=None): def test_del_statement(self):
for fname in findpy(root or os.path.dirname(fnmatch.__file__)): source = "del l[0]"
if not testone(fname, f1, f2) and stoponfail: self.assertAstSourceEqual(source)
break source = "del obj.x"
self.assertAstSourceEqual(source)
if __name__ == '__main__': if __name__ == '__main__':
f1 = open('test_codegen_dump_1.txt', 'w') unittest.main()
f2 = open('test_codegen_dump_2.txt', 'w')
go(sys.argv[1:], False, f1, f2)
#!/usr/bin/env python
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import astor
def TestMe(x, y, width=10, foo=None):
from foo.bar.baz.murgatroyd import sally as bob
a.b = c.d + x.y.z.a.b
m.n = q = (w.w, x.x.y.y) = f(x.x.y.z)
func_ast = astor.codetoast(TestMe)
print(astor.dump(func_ast))
print(astor.to_source(func_ast))
class ConsolidateAttributes(astor.TreeWalk):
def post_Attribute(self):
node = self.cur_node
value = node.value
value.id += '.' + node.attr
self.replace(value)
ConsolidateAttributes(func_ast)
class FindNames(astor.TreeWalk):
def init_Assign(self):
self.assignments = []
self.current_assign = None
def pre_Assign(self):
self.current_assign = [], []
def post_Assign(self):
self.assignments.append(self.current_assign)
self.current_assign = None
def pre_targets_name(self):
self.in_targets = True
def post_targets_name(self):
self.in_targets = False
def post_Name(self):
if self.current_assign:
self.current_assign[self.in_targets].append(self.cur_node.id)
x = FindNames(func_ast)
print(x.assignments)
print('')
print('')
print(astor.dump(func_ast))
print(astor.to_source(func_ast))
[tox]
envlist = py26,py27,pypy,py32,py33,py34
skipsdist = True
[testenv]
commands =
pip install --allow-all-external -e .
nosetests
flake8 astor tests
deps =
-rrequirements-dev.txt
[testenv:py26]
deps =
{[testenv]deps}
unittest2
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