Skip to content

Commit

Permalink
Add test for "1 / any number" expression (#786)
Browse files Browse the repository at this point in the history
* Add tests for "1 / any number" expression

* Skip left number if it is 1

* Add test divide by one
  • Loading branch information
serhii73 authored and sobolevn committed Aug 25, 2019
1 parent ccad28d commit cd14300
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/test_visitors/test_ast/test_operators/test_useless_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,44 @@ def test_useful_math(
visitor.run()

assert_errors(visitor, [])


@pytest.mark.parametrize('expression', [
'1 / other',
'1 / 11',
'1 / 1.1',
])
def test_one_to_divide(
assert_errors,
parse_ast_tree,
expression,
default_options,
):
"""Testing that `1 / any number` is the correct expression."""
tree = parse_ast_tree(expression)

visitor = UselessOperatorsVisitor(default_options, tree=tree)
visitor.run()

assert_errors(visitor, [])


@pytest.mark.parametrize('expression', [
'1 / 1',
'2 / 1',
'3.3 / 1',
'other / 1',
])
def test_divide_by_one(
assert_errors,
parse_ast_tree,
expression,
default_options,
):
"""Testing an error when we divide by one."""
tree = parse_ast_tree(expression)

visitor = UselessOperatorsVisitor(default_options, tree=tree)
visitor.run()

assert_errors(visitor, [MeaninglessNumberOperationViolation])
3 changes: 3 additions & 0 deletions wemake_python_styleguide/visitors/ast/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def _check_useless_math_operator(
left: ast.AST,
right: Optional[ast.AST] = None,
) -> None:
if isinstance(left, ast.Num) and right:
if left.n == 1:
left = None
non_negative_numbers = self._get_non_negative_nodes(left, right)

for number in non_negative_numbers:
Expand Down

0 comments on commit cd14300

Please sign in to comment.