Skip to content

Commit b7b4f90

Browse files
authored
Add .upper() .lower() and .capitalize() as mutations (#384)
* feat(str_mut): add .upper() .lower() and .capitalize() as mutations + tests * fix(str_mut): add check on useless mutants * style(str_mut): clear code and fix tests
1 parent b41c2b3 commit b7b4f90

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

mutmut/node_mutation.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,18 @@ def operator_string(
3737
# that mutation is meaningless for
3838
return
3939

40-
yield node.with_changes(value=f"{prefix}{value[0]}XX{value[1:-1]}XX{value[-1]}")
40+
supported_str_mutations: list[Callable[[str], str]] = [
41+
lambda x: "XX" + x + "XX",
42+
lambda x: x.lower(),
43+
lambda x: x.upper(),
44+
lambda x: x.capitalize(),
45+
]
46+
47+
for mut_func in supported_str_mutations:
48+
new_value = f"{prefix}{value[0]}{mut_func(value[1:-1])}{value[-1]}"
49+
if new_value == value:
50+
continue
51+
yield node.with_changes(value=new_value)
4152

4253

4354
def operator_lambda(

tests/e2e/snapshots/config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"mutants/config_pkg/__init__.py.meta": {
3-
"config_pkg.x_hello__mutmut_1": 1
3+
"config_pkg.x_hello__mutmut_1": 1,
4+
"config_pkg.x_hello__mutmut_2": 1,
5+
"config_pkg.x_hello__mutmut_3": 1
46
},
57
"mutants/config_pkg/math.py.meta": {
68
"config_pkg.math.x_add__mutmut_1": 0,

tests/e2e/snapshots/my_lib.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
{
22
"mutants/src/my_lib/__init__.py.meta": {
33
"my_lib.x_hello__mutmut_1": 1,
4+
"my_lib.x_hello__mutmut_2": 1,
5+
"my_lib.x_hello__mutmut_3": 1,
46
"my_lib.x_badly_tested__mutmut_1": 0,
7+
"my_lib.x_badly_tested__mutmut_2": 0,
8+
"my_lib.x_badly_tested__mutmut_3": 0,
59
"my_lib.x_untested__mutmut_1": 33,
10+
"my_lib.x_untested__mutmut_2": 33,
11+
"my_lib.x_untested__mutmut_3": 33,
612
"my_lib.x_make_greeter__mutmut_1": 1,
713
"my_lib.x_make_greeter__mutmut_2": 1,
8-
"my_lib.x_make_greeter__mutmut_3": 0,
14+
"my_lib.x_make_greeter__mutmut_3": 1,
15+
"my_lib.x_make_greeter__mutmut_4": 1,
16+
"my_lib.x_make_greeter__mutmut_5": 0,
17+
"my_lib.x_make_greeter__mutmut_6": 0,
18+
"my_lib.x_make_greeter__mutmut_7": 0,
919
"my_lib.x_fibonacci__mutmut_1": 1,
1020
"my_lib.x_fibonacci__mutmut_2": 0,
1121
"my_lib.x_fibonacci__mutmut_3": 0,

tests/test_mutation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def mutated_module(source: str) -> str:
5858
('x: list[A | None]', []),
5959
('a: Optional[int] = None', 'a: Optional[int] = ""'),
6060
('a: int = 1', ['a: int = 2', 'a: int = None']),
61-
('a: str = "foo"', ['a: str = "XXfooXX"', 'a: str = None']),
61+
('a: str = "FoO"', ['a: str = "XXFoOXX"', 'a: str = "foo"', 'a: str = "FOO"', 'a: str = "Foo"', 'a: str = None']),
6262
('lambda: 0', ['lambda: 1', 'lambda: None']),
6363
("1 in (1, 2)", ['2 in (1, 2)', '1 not in (1, 2)', '1 in (2, 2)', '1 in (1, 3)']),
6464
('1+1', ['2+1', '1 - 1', '1+2']),
@@ -81,9 +81,9 @@ def mutated_module(source: str) -> str:
8181
('1e-3', '1.001'),
8282
('True', 'False'),
8383
('False', 'True'),
84-
('"foo"', '"XXfooXX"'),
85-
("'foo'", "'XXfooXX'"),
86-
("u'foo'", "u'XXfooXX'"),
84+
('"FoO"', ['"XXFoOXX"', '"foo"', '"FOO"', '"Foo"']),
85+
("'FoO'", ["'XXFoOXX'", "'foo'", "'FOO'", "'Foo'"]),
86+
("u'FoO'", ["u'XXFoOXX'", "u'foo'", "u'FOO'", "u'Foo'"]),
8787
("10", "11"),
8888
("10.", "11.0"),
8989
("0o10", "9"),

0 commit comments

Comments
 (0)