Skip to content

Commit 39ebdab

Browse files
committed
DRY in TestAssertionRewrite
1 parent 9a6fa33 commit 39ebdab

1 file changed

Lines changed: 15 additions & 25 deletions

File tree

testing/test_assertrewrite.py

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,35 @@ def getmsg(f, extra_ns=None, must_pass=False):
6565
pytest.fail("function didn't raise at all")
6666

6767

68-
def python_version_has_docstring_in_module_node():
68+
def adjust_body_for_new_docstring_in_module_node(m):
6969
"""Module docstrings in 3.8 are part of Module node.
7070
This was briefly in 3.7 as well but got reverted in beta 5.
7171
72+
It's not in the body so we remove it so the following body items have
73+
the same indexes on all Python versions:
74+
7275
TODO:
7376
7477
We have a complicated sys.version_info if in here to ease testing on
7578
various Python 3.7 versions, but we should remove the 3.7 check after
7679
3.7 is released as stable to make this check more straightforward.
7780
"""
78-
return (
81+
if (
7982
sys.version_info < (3, 8)
8083
and not ((3, 7) <= sys.version_info <= (3, 7, 0, "beta", 4))
81-
)
84+
):
85+
assert len(m.body) > 1
86+
assert isinstance(m.body[0], ast.Expr)
87+
assert isinstance(m.body[0].value, ast.Str)
88+
del m.body[0]
8289

8390

8491
class TestAssertionRewrite(object):
8592

8693
def test_place_initial_imports(self):
8794
s = """'Doc string'\nother = stuff"""
8895
m = rewrite(s)
89-
# Module docstrings in some new Python versions are part of Module node
90-
# It's not in the body so we remove it so the following body items have
91-
# the same indexes on all Python versions:
92-
if python_version_has_docstring_in_module_node():
93-
assert isinstance(m.body[0], ast.Expr)
94-
assert isinstance(m.body[0].value, ast.Str)
95-
del m.body[0]
96+
adjust_body_for_new_docstring_in_module_node(m)
9697
for imp in m.body[0:2]:
9798
assert isinstance(imp, ast.Import)
9899
assert imp.lineno == 2
@@ -108,21 +109,15 @@ def test_place_initial_imports(self):
108109
assert isinstance(m.body[3], ast.Expr)
109110
s = """'doc string'\nfrom __future__ import with_statement"""
110111
m = rewrite(s)
111-
if python_version_has_docstring_in_module_node():
112-
assert isinstance(m.body[0], ast.Expr)
113-
assert isinstance(m.body[0].value, ast.Str)
114-
del m.body[0]
112+
adjust_body_for_new_docstring_in_module_node(m)
115113
assert isinstance(m.body[0], ast.ImportFrom)
116114
for imp in m.body[1:3]:
117115
assert isinstance(imp, ast.Import)
118116
assert imp.lineno == 2
119117
assert imp.col_offset == 0
120118
s = """'doc string'\nfrom __future__ import with_statement\nother"""
121119
m = rewrite(s)
122-
if python_version_has_docstring_in_module_node():
123-
assert isinstance(m.body[0], ast.Expr)
124-
assert isinstance(m.body[0].value, ast.Str)
125-
del m.body[0]
120+
adjust_body_for_new_docstring_in_module_node(m)
126121
assert isinstance(m.body[0], ast.ImportFrom)
127122
for imp in m.body[1:3]:
128123
assert isinstance(imp, ast.Import)
@@ -140,13 +135,8 @@ def test_place_initial_imports(self):
140135
def test_dont_rewrite(self):
141136
s = """'PYTEST_DONT_REWRITE'\nassert 14"""
142137
m = rewrite(s)
143-
if python_version_has_docstring_in_module_node():
144-
assert len(m.body) == 2
145-
assert isinstance(m.body[0], ast.Expr)
146-
assert isinstance(m.body[0].value, ast.Str)
147-
del m.body[0]
148-
else:
149-
assert len(m.body) == 1
138+
adjust_body_for_new_docstring_in_module_node(m)
139+
assert len(m.body) == 1
150140
assert m.body[0].msg is None
151141

152142
def test_dont_rewrite_plugin(self, testdir):

0 commit comments

Comments
 (0)