|
| 1 | +import pytest |
| 2 | + |
| 3 | +from wemake_python_styleguide.violations.consistency import ( |
| 4 | + NotInWithUnaryOpViolation, |
| 5 | +) |
| 6 | +from wemake_python_styleguide.visitors.ast.compares import ( |
| 7 | + NotInUnaryVisitor, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize( |
| 12 | + 'code', |
| 13 | + [ |
| 14 | + 'if not x in items: ...', |
| 15 | + 'if not (x in items): ...', |
| 16 | + 'result = not value in data', |
| 17 | + 'flag = not (value in data)', |
| 18 | + 'while not a in b: ...', |
| 19 | + ], |
| 20 | +) |
| 21 | +def test_not_in_unary_detects( |
| 22 | + assert_errors, |
| 23 | + parse_ast_tree, |
| 24 | + default_options, |
| 25 | + code, |
| 26 | +): |
| 27 | + """Ensures that ``not a in b`` forms are reported.""" |
| 28 | + tree = parse_ast_tree(code) |
| 29 | + |
| 30 | + visitor = NotInUnaryVisitor(default_options, tree=tree) |
| 31 | + visitor.run() |
| 32 | + |
| 33 | + assert_errors(visitor, [NotInWithUnaryOpViolation]) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + 'code', |
| 38 | + [ |
| 39 | + 'if x not in items: ...', |
| 40 | + 'result = x not in data', |
| 41 | + 'if (not x) in items: ...', |
| 42 | + 'if not_called() in seq: ...', |
| 43 | + 'if y in items: ...', |
| 44 | + 'if not a is b: ...', |
| 45 | + 'if not (a is b): ...', |
| 46 | + 'if not (x in y in z): ...', |
| 47 | + ], |
| 48 | +) |
| 49 | +def test_not_in_unary_ok( |
| 50 | + assert_errors, |
| 51 | + parse_ast_tree, |
| 52 | + default_options, |
| 53 | + code, |
| 54 | +): |
| 55 | + """Ensures that correct forms are not reported.""" |
| 56 | + tree = parse_ast_tree(code) |
| 57 | + |
| 58 | + visitor = NotInUnaryVisitor(default_options, tree=tree) |
| 59 | + visitor.run() |
| 60 | + |
| 61 | + assert_errors(visitor, []) |
0 commit comments