|
| 1 | +import pytest |
| 2 | + |
| 3 | +from wemake_python_styleguide.violations.best_practices import ( |
| 4 | + MultipleVariablesInitializationViolation, |
| 5 | +) |
| 6 | +from wemake_python_styleguide.visitors.ast.builtins import ( |
| 7 | + MultipleVariablesInitializationVisitor, |
| 8 | +) |
| 9 | + |
| 10 | +# Correct usages: |
| 11 | + |
| 12 | +function_unpacking = 'first, second = some_tuple()' |
| 13 | +swap_assignment = 'first, second = second, first' |
| 14 | +single_assignment = 'constant = 1' |
| 15 | +spread_assignment = 'first, *_, second = [1, 2, 4, 3]' |
| 16 | + |
| 17 | +# Wrong usages: |
| 18 | + |
| 19 | +tuple_assignment = 'first, second = (1, 2)' |
| 20 | +list_unpacking = 'first, second = [], []' |
| 21 | +mixed_unpacking = 'first, second = (1, [])' |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize( |
| 25 | + 'code', |
| 26 | + [ |
| 27 | + single_assignment, |
| 28 | + spread_assignment, |
| 29 | + function_unpacking, |
| 30 | + swap_assignment, |
| 31 | + ], |
| 32 | +) |
| 33 | +def test_correct_assignments( |
| 34 | + assert_errors, |
| 35 | + parse_ast_tree, |
| 36 | + code, |
| 37 | + default_options, |
| 38 | +): |
| 39 | + """Testing that correct assignments work.""" |
| 40 | + tree = parse_ast_tree(code) |
| 41 | + |
| 42 | + visitor = MultipleVariablesInitializationVisitor( |
| 43 | + default_options, tree=tree, |
| 44 | + ) |
| 45 | + visitor.run() |
| 46 | + |
| 47 | + assert_errors(visitor, []) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + 'code', |
| 52 | + [ |
| 53 | + tuple_assignment, |
| 54 | + list_unpacking, |
| 55 | + mixed_unpacking, |
| 56 | + ], |
| 57 | +) |
| 58 | +def test_multiple_variables_initialization( |
| 59 | + assert_errors, |
| 60 | + parse_ast_tree, |
| 61 | + code, |
| 62 | + default_options, |
| 63 | +): |
| 64 | + """Testing that multiple variables initialization is restricted.""" |
| 65 | + tree = parse_ast_tree(code) |
| 66 | + |
| 67 | + visitor = MultipleVariablesInitializationVisitor( |
| 68 | + default_options, tree=tree, |
| 69 | + ) |
| 70 | + visitor.run() |
| 71 | + |
| 72 | + assert_errors(visitor, [MultipleVariablesInitializationViolation]) |
0 commit comments