Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d751c2ea
Commit
d751c2ea
authored
Jun 29, 2010
by
Mark Dickinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test_unparse.py: Do roundtrip testing for all Python files in Lib and Lib/test.
parent
8042e281
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
8 deletions
+46
-8
Demo/parser/test_unparse.py
Demo/parser/test_unparse.py
+46
-8
No files found.
Demo/parser/test_unparse.py
View file @
d751c2ea
import
unittest
import
test.support
import
io
import
os
import
tokenize
import
ast
import
_ast
import
unparse
def
read_pyfile
(
filename
):
"""Read and return the contents of a Python source file (as a
string), taking into account the file encoding."""
with
open
(
filename
,
"rb"
)
as
pyfile
:
encoding
=
tokenize
.
detect_encoding
(
pyfile
.
readline
)[
0
]
with
open
(
filename
,
"r"
,
encoding
=
encoding
)
as
pyfile
:
source
=
pyfile
.
read
()
return
source
for_else
=
"""
\
def f():
for x in range(10):
...
...
@@ -55,16 +64,20 @@ class_decorator = """\
class Foo: pass
"""
class
UnparseTestCase
(
unittest
.
TestCase
):
# Tests for specific bugs found in earlier versions of unparse
class
ASTTestCase
(
unittest
.
TestCase
):
def
assertASTEqual
(
self
,
ast1
,
ast2
):
self
.
assertEqual
(
ast
.
dump
(
ast1
),
ast
.
dump
(
ast2
))
def
check_roundtrip
(
self
,
code1
,
filename
=
"internal"
):
ast1
=
compile
(
code1
,
filename
,
"exec"
,
_
ast
.
PyCF_ONLY_AST
)
ast1
=
compile
(
code1
,
filename
,
"exec"
,
ast
.
PyCF_ONLY_AST
)
unparse_buffer
=
io
.
StringIO
()
unparse
.
Unparser
(
ast1
,
unparse_buffer
)
code2
=
unparse_buffer
.
getvalue
()
ast2
=
compile
(
code2
,
filename
,
"exec"
,
_ast
.
PyCF_ONLY_AST
)
self
.
assertEqual
(
ast
.
dump
(
ast1
),
ast
.
dump
(
ast2
))
ast2
=
compile
(
code2
,
filename
,
"exec"
,
ast
.
PyCF_ONLY_AST
)
self
.
assertASTEqual
(
ast1
,
ast2
)
class
UnparseTestCase
(
ASTTestCase
):
# Tests for specific bugs found in earlier versions of unparse
def
test_del_statement
(
self
):
self
.
check_roundtrip
(
"del x, y, z"
)
...
...
@@ -143,8 +156,33 @@ class UnparseTestCase(unittest.TestCase):
def
test_class_decorators
(
self
):
self
.
check_roundtrip
(
class_decorator
)
class
DirectoryTestCase
(
ASTTestCase
):
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
# test directories, relative to the root of the distribution
test_directories
=
'Lib'
,
os
.
path
.
join
(
'Lib'
,
'test'
)
def
test_files
(
self
):
# get names of files to test
dist_dir
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
os
.
pardir
,
os
.
pardir
)
names
=
[]
for
d
in
self
.
test_directories
:
test_dir
=
os
.
path
.
join
(
dist_dir
,
d
)
for
n
in
os
.
listdir
(
test_dir
):
if
n
.
endswith
(
'.py'
)
and
not
n
.
startswith
(
'bad'
):
names
.
append
(
os
.
path
.
join
(
test_dir
,
n
))
for
filename
in
names
:
if
test
.
support
.
verbose
:
print
(
'Testing %s'
%
filename
)
source
=
read_pyfile
(
filename
)
self
.
check_roundtrip
(
source
)
def
test_main
():
test
.
support
.
run_unittest
(
UnparseTestCase
)
test
.
support
.
run_unittest
(
UnparseTestCase
,
DirectoryTestCase
)
if
__name__
==
'__main__'
:
test_main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment