Skip to content

Commit 118c30d

Browse files
committed
DRY in TestAssertionRewrite
1 parent e49a9ba commit 118c30d

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

testing/test_assertrewrite.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,33 @@ 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 (
79-
sys.version_info < (3, 8) or (3, 7) <= sys.version_info <= (3, 7, 0, "beta", 4)
80-
)
81+
if sys.version_info < (3, 8) or (3, 7) <= sys.version_info <= (3, 7, 0, "beta", 4):
82+
assert len(m.body) > 1
83+
assert isinstance(m.body[0], ast.Expr)
84+
assert isinstance(m.body[0].value, ast.Str)
85+
del m.body[0]
86+
return m
8187

8288

8389
class TestAssertionRewrite(object):
8490

8591
def test_place_initial_imports(self):
8692
s = """'Doc string'\nother = stuff"""
8793
m = rewrite(s)
88-
# Module docstrings in some new Python versions are part of Module node
89-
# It's not in the body so we remove it so the following body items have
90-
# the same indexes on all Python versions:
91-
if python_version_has_docstring_in_module_node():
92-
assert isinstance(m.body[0], ast.Expr)
93-
assert isinstance(m.body[0].value, ast.Str)
94-
del m.body[0]
94+
m = adjust_body_for_new_docstring_in_module_node(m)
9595
for imp in m.body[0:2]:
9696
assert isinstance(imp, ast.Import)
9797
assert imp.lineno == 2
@@ -107,21 +107,15 @@ def test_place_initial_imports(self):
107107
assert isinstance(m.body[3], ast.Expr)
108108
s = """'doc string'\nfrom __future__ import with_statement"""
109109
m = rewrite(s)
110-
if python_version_has_docstring_in_module_node():
111-
assert isinstance(m.body[0], ast.Expr)
112-
assert isinstance(m.body[0].value, ast.Str)
113-
del m.body[0]
110+
m = adjust_body_for_new_docstring_in_module_node(m)
114111
assert isinstance(m.body[0], ast.ImportFrom)
115112
for imp in m.body[1:3]:
116113
assert isinstance(imp, ast.Import)
117114
assert imp.lineno == 2
118115
assert imp.col_offset == 0
119116
s = """'doc string'\nfrom __future__ import with_statement\nother"""
120117
m = rewrite(s)
121-
if python_version_has_docstring_in_module_node():
122-
assert isinstance(m.body[0], ast.Expr)
123-
assert isinstance(m.body[0].value, ast.Str)
124-
del m.body[0]
118+
m = adjust_body_for_new_docstring_in_module_node(m)
125119
assert isinstance(m.body[0], ast.ImportFrom)
126120
for imp in m.body[1:3]:
127121
assert isinstance(imp, ast.Import)
@@ -139,13 +133,8 @@ def test_place_initial_imports(self):
139133
def test_dont_rewrite(self):
140134
s = """'PYTEST_DONT_REWRITE'\nassert 14"""
141135
m = rewrite(s)
142-
if python_version_has_docstring_in_module_node():
143-
assert len(m.body) == 2
144-
assert isinstance(m.body[0], ast.Expr)
145-
assert isinstance(m.body[0].value, ast.Str)
146-
del m.body[0]
147-
else:
148-
assert len(m.body) == 1
136+
m = adjust_body_for_new_docstring_in_module_node(m)
137+
assert len(m.body) == 1
149138
assert m.body[0].msg is None
150139

151140
def test_dont_rewrite_plugin(self, testdir):

0 commit comments

Comments
 (0)