forked from wemake-services/wemake-python-styleguide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge master into issue-wemake-services#1172
- Loading branch information
Showing
11 changed files
with
276 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
tests/test_visitors/test_ast/test_exceptions/test_finally_in_loops.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
from wemake_python_styleguide.compat.constants import PY38 | ||
from wemake_python_styleguide.violations.best_practices import ( | ||
LoopControlFinallyViolation, | ||
) | ||
from wemake_python_styleguide.visitors.ast.exceptions import ( | ||
WrongTryExceptVisitor, | ||
) | ||
|
||
right_try_example_with_for = """ | ||
def function(): | ||
for element in range(10): | ||
try: | ||
... | ||
except: | ||
... | ||
finally: | ||
... | ||
""" | ||
|
||
right_try_example_with_while = """ | ||
def function(): | ||
while first_element < second_element: | ||
try: | ||
... | ||
except: | ||
... | ||
finally: | ||
... | ||
""" | ||
|
||
right_example_with_for = """ | ||
def function(): | ||
def some(): | ||
for elem in range(10): | ||
if elem > 5: | ||
{0} | ||
""" | ||
|
||
right_example_with_while = """ | ||
def function(): | ||
def some(): | ||
while a < b: | ||
if a == 5: | ||
{0} | ||
""" | ||
|
||
wrong_try_example = """ | ||
def function(): | ||
for element in range(10): | ||
try: | ||
... | ||
except: | ||
... | ||
finally: | ||
{0} | ||
""" | ||
|
||
wrong_try_example_with_while = """ | ||
def function(): | ||
while first_element < second_element: | ||
try: | ||
... | ||
except: | ||
... | ||
finally: | ||
{0} | ||
""" | ||
|
||
|
||
@pytest.mark.parametrize('statement', [ | ||
'break', | ||
'continue', | ||
]) | ||
@pytest.mark.parametrize('code', [ | ||
right_example_with_for, | ||
right_example_with_while, | ||
right_try_example_with_for, | ||
right_try_example_with_while, | ||
]) | ||
def test_right_finally( | ||
assert_errors, | ||
parse_ast_tree, | ||
code, | ||
statement, | ||
default_options, | ||
mode, | ||
): | ||
"""Testing that regular loops and loops with `try` are allowed.""" | ||
tree = parse_ast_tree(mode(code.format(statement))) | ||
|
||
visitor = WrongTryExceptVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, []) | ||
|
||
|
||
@pytest.mark.parametrize('statement', [ | ||
'break', | ||
]) | ||
@pytest.mark.parametrize('code', [ | ||
wrong_try_example, | ||
wrong_try_example_with_while, | ||
]) | ||
def test_finally_with_break( | ||
assert_errors, | ||
parse_ast_tree, | ||
code, | ||
statement, | ||
default_options, | ||
mode, | ||
): | ||
"""Testing that `break` keyword is not allowed in `finally`.""" | ||
tree = parse_ast_tree(mode(code.format(statement))) | ||
|
||
visitor = WrongTryExceptVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [LoopControlFinallyViolation]) | ||
|
||
|
||
@pytest.mark.skipif( | ||
not PY38, | ||
reason='continue in finally works only since python3.8', | ||
) | ||
@pytest.mark.parametrize('statement', [ | ||
'continue', | ||
]) | ||
@pytest.mark.parametrize('code', [ | ||
wrong_try_example, | ||
wrong_try_example_with_while, | ||
]) | ||
def test_finally_with_continue( | ||
assert_errors, | ||
parse_ast_tree, | ||
code, | ||
statement, | ||
default_options, | ||
mode, | ||
): | ||
"""Testing that `continue` keyword is not allowed in `finally`.""" | ||
tree = parse_ast_tree(mode(code.format(statement))) | ||
|
||
visitor = WrongTryExceptVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [LoopControlFinallyViolation]) | ||
|
||
|
||
@pytest.mark.skipif( | ||
PY38, | ||
reason='continue in finally does not work till since python3.8', | ||
) | ||
@pytest.mark.parametrize('statement', [ | ||
'continue', | ||
]) | ||
@pytest.mark.parametrize('code', [ | ||
wrong_try_example, | ||
wrong_try_example_with_while, | ||
]) | ||
def test_finally_with_continue_and_exception( | ||
assert_errors, | ||
parse_ast_tree, | ||
code, | ||
statement, | ||
default_options, | ||
mode, | ||
): | ||
"""Testing using `continue` keyword in python < 3.8 raises an exception.""" | ||
with pytest.raises(SyntaxError): | ||
parse_ast_tree(mode(code.format(statement))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.