TER1 (Statement): 100.00%
TER2 (Branch): 80.77%
TER3 (LCSAJ): 100.0% (3/3)
Approximate LCSAJ segments: 27
โ Covered โ this LCSAJ path was executed during testing.
โ Not covered โ this LCSAJ path was never executed. These are the paths to focus on.
Multiple dots on a line indicate that multiple control-flow paths begin at that line. Hovering over any dot shows:
start โ end โ jump
Uncovered paths show [NOT COVERED] in the tooltip.
1: package App::Test::Generator::Mutation::BooleanNegation; 2: 3: use strict; 4: use warnings; 5: 6: use Carp qw(carp); 7: use parent 'App::Test::Generator::Mutation::Base'; 8: use App::Test::Generator::Mutant; 9: use PPI; 10: 11: our $VERSION = '0.52'; 12: 13: =head1 NAME 14: 15: App::Test::Generator::Mutation::BooleanNegation - Negate boolean return 16: expressions to expose missing assertion coverage 17: 18: =head1 VERSION 19: 20: Version 0.52 21: 22: =head1 METHODS 23: 24: =head2 applies_to 25: 26: Return true if the given document contains at least one return 27: statement this mutation strategy could mutate. Used by 28: L<App::Test::Generator::Mutator> to pre-filter strategies before 29: calling C<mutate>, so a document with nothing to mutate skips the 30: walk entirely. 31: 32: my $applies = $mutation->applies_to($doc); 33: 34: =head3 Arguments 35: 36: =over 4 37: 38: =item * C<$doc> 39: 40: A L<PPI::Document> object to inspect. 41: 42: =back 43: 44: =head3 Returns 45: 46: True if the document contains a C<return> statement (PPI::Statement::Break 47: whose first token is C<return>), false otherwise. 48: 49: =head3 API specification 50: 51: =head4 input 52: 53: { 54: self => { type => OBJECT, isa => 'App::Test::Generator::Mutation::BooleanNegation' }, 55: doc => { type => OBJECT, isa => 'PPI::Document' }, 56: } 57: 58: =head4 output 59: 60: { type => SCALAR } 61: 62: =cut 63: 64: sub applies_to { 65: my ($self, $doc) = @_;Mutants (Total: 2, Killed: 2, Survived: 0)
66: 67: # PPI >= 1.270 classifies return as PPI::Statement::Break rather 68: # than PPI::Statement::Return -- scan the whole document for at 69: # least one qualifying return statement. This must match the
Mutants (Total: 2, Killed: 2, Survived: 0)
70: # document-level pre-filter contract used by Mutator::generate_mutants 71: # (and documented in Mutation::Base) rather than testing a single node, 72: # otherwise every call from generate_mutants would see $doc itself, 73: # which is never a PPI::Statement::Break, and mutate() would never run. 74: my $returns = $doc->find(sub { 75: my $node = $_[1]; 76: return 0 unless $node->isa('PPI::Statement::Break'); 77: my $first = $node->schild(0) or return 0; 78: return $first->content eq 'return'; 79: }) || []; 80: 81: return @{$returns} ? 1 : 0; 82: } 83: 84: =head2 mutate 85: 86: Walk a PPI document and generate one mutant for each return statement 87: whose expression can be negated. For example, C<return $ok> becomes 88: C<return !($ok)>. 89: 90: my $mutation = App::Test::Generator::Mutation::BooleanNegation->new; 91: my $doc = PPI::Document->new(\$source); 92: my @mutants = $mutation->mutate($doc); 93: 94: for my $m (@mutants) { 95: print $m->id, ': ', $m->description, "\n"; 96: } 97: 98: =head3 Arguments 99: 100: =over 4 101: 102: =item * C<$self> 103: 104: An instance of C<App::Test::Generator::Mutation::BooleanNegation>. 105: 106: =item * C<$doc> 107: 108: A L<PPI::Document> object representing the parsed source to mutate. 109: The document is not modified by this method. 110: 111: =back 112: 113: =head3 Returns 114: 115: A list of L<App::Test::Generator::Mutant> objects, one per qualifying 116: return statement found in the document. Returns an empty list if no 117: return statements with expressions are found. 118: 119: Each mutant carries a C<transform> closure that when called with a 120: fresh L<PPI::Document> copy will wrap the targeted return expression 121: in C<!( )>, negating its boolean value. 122: 123: =head3 Notes 124: 125: Mutant IDs include both line and column number to ensure uniqueness 126: when multiple return statements appear on different lines of the same 127: source file. 128: 129: Only return statements that have an expression child (i.e. not bare 130: C<return;> statements) are mutated. 131: 132: Each mutant's optional C<context> field is set to C<conditional> if 133: the return statement sits inside (or is itself the keyword of) an 134: C<if>/C<unless>/C<while>/C<until> compound statement, or C<statement> 135: otherwise; its C<line_content> field holds the raw source text of the 136: mutated line. Both are consumed by 137: L<App::Test::Generator::Mutator>'s fast-mode dedup. 138: 139: =head3 API specification 140: 141: =head4 input 142: 143: { 144: self => { 145: type => OBJECT, 146: isa => 'App::Test::Generator::Mutation::BooleanNegation', 147: }, 148: doc => { 149: type => OBJECT, 150: isa => 'PPI::Document', 151: }, 152: } 153: 154: =head4 output 155: 156: { 157: type => ARRAYREF,
Mutants (Total: 2, Killed: 2, Survived: 0)
158: elements => { 159: type => OBJECT, 160: isa => 'App::Test::Generator::Mutant', 161: },
Mutants (Total: 2, Killed: 2, Survived: 0)
162: } 163: 164: =cut 165: 166: sub mutate { โ167 โ 185 โ 272 167: my ($self, $doc) = @_; 168: 169: # PPI >= 1.270 classifies return statements as PPI::Statement::Break 170: # (alongside last/next/redo) rather than PPI::Statement::Return. 171: # Use a custom predicate to match only 'return' Break nodes. 172: my $returns = $doc->find(sub { 173: my $node = $_[1]; 174: # Must be a Break statement -- the parent class for return in 175: # newer PPI versions 176: return 0 unless $node->isa('PPI::Statement::Break'); 177: # Distinguish return from last/next/redo by checking the 178: # first significant child token 179: my $first = $node->schild(0) or return 0; 180: return $first->content eq 'return'; 181: }) || []; 182: 183: my @mutants; 184: 185: for my $ret (@{$returns}) { 186: # Skip bare return statements with no expression to negate, 187: # and bare returns with only a postfix conditional/loop 188: # modifier (return if $cond; has nothing to negate) 189: my @expr = _return_expr_span($ret); 190: next unless @expr; 191: 192: # Skip a lone structure node (e.g. return ($x, $y) gives a 193: # single PPI::Structure::List child) â wrapping it is not 194: # useful and there is nothing simple to splice around 195: next if @expr == 1 && !$expr[0]->isa('PPI::Token'); 196: 197: # Capture location so the transform closure targets the 198: # exact statement rather than the first match on that line 199: my $line = $ret->location->[0]; 200: my $col = $ret->location->[1]; 201: 202: # Build a unique ID from line and column so multiple return 203: # statements in the same file never collide 204: my $id = "BOOL_NEGATE_${line}_${col}"; 205: 206: my $mutant = eval { 207: App::Test::Generator::Mutant->new( 208: id => $id, 209: group => "BOOL_NEGATE:$line", 210: description => 'Negate boolean return expression',
211: original => $ret->content, 212: line => $line, 213: type => 'boolean',Mutants (Total: 2, Killed: 0, Survived: 2)
- BOOL_NEGATE_210_7: Negate boolean return expression
MEDIUM: Add tests asserting both true and false outcomes๐งช Suggested Test# Boolean branch test suggestion ok( !func(INPUT), 'Verify boolean branch behaviour' );- RETURN_UNDEF_210_7: Replace return expression with undef
LOW: Mutation survived, but impact may be minor๐งช Suggested Test# Return value assertion is( func(INPUT), EXPECTED, 'Verify correct return value' );Mutants (Total: 2, Killed: 2, Survived: 0)
214: context => $self->_in_conditional($ret) ? 'conditional' : 'statement', 215: line_content => $self->_line_content($doc, $line), 216: 217: # The transform closure captures line and col so it 218: # targets precisely the right return statement in the 219: # document copy it receives at test time
Mutants (Total: 1, Killed: 1, Survived: 0)
220: transform => sub {
Mutants (Total: 1, Killed: 1, Survived: 0)
221: my $doc = $_[0]; 222: 223: # Locate all return statements in the fresh document copy using 224: # the same PPI::Statement::Break predicate as the outer find -- 225: # PPI >= 1.270 no longer uses PPI::Statement::Return 226: my $rets = $doc->find(sub { 227: my $node = $_[1]; 228: # Match Break nodes only -- covers return/last/next/redo 229: return 0 unless $node->isa('PPI::Statement::Break'); 230: # Filter to return specifically by inspecting the first token 231: my $first = $node->schild(0) or return 0; 232: return $first->content eq 'return'; 233: }) || []; 234: 235: for my $ret (@{$rets}) { 236: # Match by line and column to avoid mutating 237: # the wrong return statement 238: next unless $ret->line_number == $line; 239: next unless $ret->column_number == $col; 240: 241: # Skip bare returns with no expression 242: my @expr = _return_expr_span($ret); 243: last unless @expr; 244:
Mutants (Total: 1, Killed: 1, Survived: 0)
245: # Skip a lone structure node 246: last if @expr == 1 && !$expr[0]->isa('PPI::Token'); 247: 248: # Wrap the whole expression span in !(...) rather 249: # than just its first token -- $self->{x} is three 250: # significant children (Symbol, Operator, Structure) 251: # and wrapping only the leading $self produced the 252: # broken mutant 'return !($self)->{x};'
Mutants (Total: 2, Killed: 2, Survived: 0)
253: $expr[0]->insert_before(PPI::Token::Operator->new('!')); 254: $expr[0]->insert_before(PPI::Token::Structure->new('(')); 255: $expr[-1]->insert_after(PPI::Token::Structure->new(')')); 256: last; 257: } 258: }, 259: ); 260: }; 261: 262: # If the Mutant construction fails, report clearly rather than 263: # silently dropping the mutant from the results 264: if($@ || !$mutant) { 265: carp "Failed to construct mutant $id: $@" if $@; 266: next; 267: } 268: 269: push @mutants, $mutant; 270: } 271: 272: return @mutants; 273: } 274: 275: # -------------------------------------------------- 276: # Purpose: identify the PPI elements making up the expression 277: # being returned by a 'return' statement, excluding 278: # the leading 'return' keyword, the trailing statement 279: # terminator, and any postfix conditional/loop modifier 280: # (if/unless/while/until/for/foreach) and its condition. 281: # Entry: a PPI::Statement::Break node already confirmed to be 282: # a 'return' statement. 283: # Exit: a list of the significant child elements making up 284: # the return expression, or an empty list for a bare 285: # return (with or without a postfix modifier). 286: # Side effects: none. 287: # -------------------------------------------------- 288: sub _return_expr_span { โ289 โ 294 โ 298 289: my ($ret) = @_; 290: 291: my @children = $ret->schildren; 292: shift @children; 293: 294: if(@children && $children[-1]->isa('PPI::Token::Structure') && $children[-1]->content eq ';') { 295: pop @children; 296: } 297: โ298 โ 298 โ 306 298: for my $i (0 .. $#children) { 299: my $child = $children[$i]; 300: next unless $child->isa('PPI::Token::Word'); 301: next unless $child->content =~ /^(?:if|unless|while|until|for|foreach)$/; 302: @children = @children[0 .. $i - 1]; 303: last; 304: } 305: 306: return @children; 307: } 308: 309: =head1 AUTHOR 310: 311: Nigel Horne, C<< <njh at nigelhorne.com> >> 312: 313: =head1 LICENCE AND COPYRIGHT 314: 315: Copyright 2026 Nigel Horne. 316: 317: Usage is subject to licence terms. 318: 319: The licence terms of this software are as follows: 320: 321: =over 4 322: 323: =item * Personal single user, single computer use: GPL2 324: 325: =item * All other users (including Commercial, Charity, Educational, 326: Government) must apply in writing for a licence for use from Nigel Horne 327: at the above e-mail. 328: 329: =back 330: 331: =cut 332: 333: 1;