mutmut icon indicating copy to clipboard operation
mutmut copied to clipboard

Add mutation for ternary operators

Open ryanfreckleton opened this issue 4 years ago • 3 comments

Ternary operator errors of not covering both branches can't be detected by coverage, so it would be useful to mutate these to enable/disable each branch. E.g.

mark = "x" if count % 2 else "o"

mutation into:

mark = "x" if count % 2 and False else "o"

and

mark = "x" if count % 2 or True else "o"

This types of mutations aren't as useful for normal if statements, because branch coverage can detect when one of the alternatives isn't covered.

ryanfreckleton avatar Jan 08 '21 21:01 ryanfreckleton

Seems reasonable. Would you like to give it a shot to implement this?

boxed avatar Jan 09 '21 10:01 boxed

Sure! Apologies for the late response.

I’ll post right before I start implementing, in case anyone else wants to take it on.

ryanfreckleton avatar Jan 22 '21 04:01 ryanfreckleton

I would love to see this get done. My workaround is to keep a special git stash of changes to replace all ternaries with expanded if/else blocks. And mutation testing with this stash of changes did give me a surviving mutant in some private code I was working on. (I actually use my own fork with some extra hackage to raise a RuntimeError in case it finds any ternaries in the code to mutate.)

When I tried hacking in the existing code, it looks like the code in __init__.py can handle multiple mutations of single tokens such as operators, but not for anything bigger at this time. I am sure it should be pretty straightforward to update the code to support multiple mutations of larger pieces of code, unfortunately I have not yet succeeded with this.

brody4hire avatar Feb 19 '24 04:02 brody4hire