|
| 1 | +import pytest |
| 2 | + |
| 3 | +from wemake_python_styleguide.violations.consistency import ( |
| 4 | + SimplifiableMatchViolation, |
| 5 | +) |
| 6 | +from wemake_python_styleguide.visitors.ast.conditions import ( |
| 7 | + SimplifiableMatchVisitor, |
| 8 | +) |
| 9 | + |
| 10 | +# Wrong: |
| 11 | +simplifiable_match_match = """ |
| 12 | +match subject: |
| 13 | + case {0}{1}: |
| 14 | + pass |
| 15 | + case _: |
| 16 | + pass |
| 17 | +""" |
| 18 | + |
| 19 | +simplifiable_union_match_match = """ |
| 20 | +match subject: |
| 21 | + case {0} | {1}{2}: |
| 22 | + pass |
| 23 | + case _: |
| 24 | + pass |
| 25 | +""" |
| 26 | + |
| 27 | +simplifiable_guard_match = """ |
| 28 | +match subject: |
| 29 | + case x if x > 0: |
| 30 | + pass |
| 31 | + case _: |
| 32 | + pass |
| 33 | +""" |
| 34 | + |
| 35 | +# Correct: |
| 36 | +complex_match = """ |
| 37 | +match subject: |
| 38 | + case State.FIRST: |
| 39 | + pass |
| 40 | + case State.SECOND: |
| 41 | + pass |
| 42 | + case _: |
| 43 | + pass |
| 44 | +""" |
| 45 | + |
| 46 | +no_wildcard_match = """ |
| 47 | +match subject: |
| 48 | + case State.FIRST: |
| 49 | + pass |
| 50 | + case State.SECOND: |
| 51 | + pass |
| 52 | +""" |
| 53 | + |
| 54 | +guard_match = """ |
| 55 | +match subject: |
| 56 | + case [x] if x > 0: |
| 57 | + pass |
| 58 | + case _: |
| 59 | + pass |
| 60 | +""" |
| 61 | + |
| 62 | +class_with_args_match = """ |
| 63 | +match subject: |
| 64 | + case SomeClass(x): |
| 65 | + pass |
| 66 | + case _: |
| 67 | + pass |
| 68 | +""" |
| 69 | + |
| 70 | +class_with_kwargs_match = """ |
| 71 | +match subject: |
| 72 | + case SomeClass(name=x): |
| 73 | + pass |
| 74 | + case _: |
| 75 | + pass |
| 76 | +""" |
| 77 | + |
| 78 | +sequences_match = """ |
| 79 | +match subject: |
| 80 | + case [x, *rest]: |
| 81 | + pass |
| 82 | + case _: |
| 83 | + pass |
| 84 | +""" |
| 85 | + |
| 86 | +mappings_match = """ |
| 87 | +match subject: |
| 88 | + case {"key": x}: |
| 89 | + pass |
| 90 | + case _: |
| 91 | + pass |
| 92 | +""" |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.parametrize( |
| 96 | + 'code', |
| 97 | + [ |
| 98 | + '1', |
| 99 | + 'True', |
| 100 | + 'None', |
| 101 | + '"string"', |
| 102 | + 'ns.CONST', |
| 103 | + 'State.REJECTED', |
| 104 | + ], |
| 105 | +) |
| 106 | +@pytest.mark.parametrize( |
| 107 | + 'as_binding', |
| 108 | + ['', ' as x'], |
| 109 | +) |
| 110 | +def test_simplifiable_single_match( |
| 111 | + code, |
| 112 | + as_binding, |
| 113 | + assert_errors, |
| 114 | + parse_ast_tree, |
| 115 | + default_options, |
| 116 | +): |
| 117 | + """Test that simple single-case match raises a violation.""" |
| 118 | + tree = parse_ast_tree(simplifiable_match_match.format(code, as_binding)) |
| 119 | + visitor = SimplifiableMatchVisitor(default_options, tree=tree) |
| 120 | + visitor.run() |
| 121 | + assert_errors(visitor, [SimplifiableMatchViolation]) |
| 122 | + |
| 123 | + |
| 124 | +def test_simplifiable_guarded_match( |
| 125 | + assert_errors, |
| 126 | + parse_ast_tree, |
| 127 | + default_options, |
| 128 | +): |
| 129 | + """Test that guarded irrefutable match is simplified.""" |
| 130 | + tree = parse_ast_tree(simplifiable_guard_match) |
| 131 | + visitor = SimplifiableMatchVisitor(default_options, tree=tree) |
| 132 | + visitor.run() |
| 133 | + assert_errors(visitor, [SimplifiableMatchViolation]) |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.parametrize( |
| 137 | + ('left', 'right'), |
| 138 | + [ |
| 139 | + ('1', '2'), |
| 140 | + ('True', 'False'), |
| 141 | + ('"a"', '"b"'), |
| 142 | + ('State.OK', 'State.ERROR'), |
| 143 | + ('"first" | "second"', '"third"'), |
| 144 | + ], |
| 145 | +) |
| 146 | +@pytest.mark.parametrize( |
| 147 | + 'as_binding', |
| 148 | + ['', ' as x'], |
| 149 | +) |
| 150 | +def test_simplifiable_union_match( |
| 151 | + left, |
| 152 | + right, |
| 153 | + as_binding, |
| 154 | + assert_errors, |
| 155 | + parse_ast_tree, |
| 156 | + default_options, |
| 157 | +): |
| 158 | + """Test that union pattern raises violation.""" |
| 159 | + tree = parse_ast_tree( |
| 160 | + simplifiable_union_match_match.format(left, right, as_binding) |
| 161 | + ) |
| 162 | + visitor = SimplifiableMatchVisitor(default_options, tree=tree) |
| 163 | + visitor.run() |
| 164 | + assert_errors(visitor, [SimplifiableMatchViolation]) |
| 165 | + |
| 166 | + |
| 167 | +@pytest.mark.parametrize( |
| 168 | + 'template', |
| 169 | + [ |
| 170 | + complex_match, |
| 171 | + no_wildcard_match, |
| 172 | + guard_match, |
| 173 | + class_with_args_match, |
| 174 | + class_with_kwargs_match, |
| 175 | + sequences_match, |
| 176 | + mappings_match, |
| 177 | + ], |
| 178 | +) |
| 179 | +def test_not_simplifiable_match_templates( |
| 180 | + template, |
| 181 | + assert_errors, |
| 182 | + parse_ast_tree, |
| 183 | + default_options, |
| 184 | +): |
| 185 | + """Test that complex or non-simplifiable matches do not raise violations.""" |
| 186 | + tree = parse_ast_tree(template) |
| 187 | + visitor = SimplifiableMatchVisitor(default_options, tree=tree) |
| 188 | + visitor.run() |
| 189 | + assert_errors(visitor, []) |
0 commit comments