Skip to content

Commit 3a1cb67

Browse files
committed
Fix match or patterns by only mutating operator BitOr
1 parent 1a0cd06 commit 3a1cb67

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

mutmut/node_mutation.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""This module contains the mutations for indidvidual nodes, e.g. replacing a != b with a == b."""
22
import re
3-
from typing import Any, Union
3+
from typing import Any, Union, cast
44
from collections.abc import Callable, Iterable, Sequence
55
import libcst as cst
66
import libcst.matchers as m
@@ -218,7 +218,11 @@ def operator_name(node: cst.Name) -> Iterable[cst.CSTNode]:
218218
def operator_swap_op(
219219
node: cst.CSTNode
220220
) -> Iterable[cst.CSTNode]:
221-
yield from _simple_mutation_mapping(node, _operator_mapping)
221+
if m.matches(node, m.BinaryOperation() | m.UnaryOperation() | m.BooleanOperation() | m.ComparisonTarget() | m.AugAssign()):
222+
typed_node = cast(Union[cst.BinaryOperation, cst.UnaryOperation, cst.BooleanOperation, cst.ComparisonTarget, cst.AugAssign], node)
223+
operator = typed_node.operator
224+
for new_operator in _simple_mutation_mapping(operator, _operator_mapping):
225+
yield node.with_changes(operator=new_operator)
222226

223227

224228
def operator_augmented_assignment(

tests/test_mutation.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,19 @@ def test_match_case():
284284

285285
assert sorted(mutants) == sorted(expected)
286286

287+
def test_mach_case_does_not_mutate_bitor():
288+
source = """
289+
def concat():
290+
match x:
291+
case A() | B():
292+
pass
293+
"""
294+
295+
mutants = mutants_for_source(source)
296+
297+
assert sorted(mutants) == []
298+
299+
287300
def test_basic_class():
288301
source = """
289302
class Foo:
@@ -309,8 +322,8 @@ def test_function_with_annotation():
309322
print(mutated_code)
310323

311324
expected_defs = [
312-
'def x_capitalize__mutmut_1(s : str):\n return s[1].title() + s[1:] if s else s',
313-
'def x_capitalize__mutmut_2(s : str):\n return s[0].title() - s[1:] if s else s',
325+
'def x_capitalize__mutmut_1(s : str):\n return s[0].title() - s[1:] if s else s',
326+
'def x_capitalize__mutmut_2(s : str):\n return s[1].title() + s[1:] if s else s',
314327
'def x_capitalize__mutmut_3(s : str):\n return s[0].title() + s[2:] if s else s',
315328
]
316329

0 commit comments

Comments
 (0)