lib/App/Test/Generator/Mutation/ConditionalInversion.pm

filter: Statementstmt 100% 61 / 61 Branchbran 72.7% 16 / 22 Conditioncond 66.6% 16 / 24 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 83.4% 111 / 133 CC 24 SCAR 21.0
CC
24
Cov
87%
mutate
12.7
__ANON__
7.7
applies_to
7.1
CRAP 8.2
« lib/App/Test/Generator/Analyzer/SideEffect.pm ↑ Summary lib/App/Test/Generator/Mutation/NumericBoundary.pm »
1 package App::Test::Generator::Mutation::ConditionalInversion;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use parent 'App::Test::Generator::Mutation::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use App::Test::Generator::Mutant;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 21 our $VERSION = '0.49';
11  
12 =head1 VERSION
13  
14 Version 0.49
15  
16 =head1 METHODS
17  
18 =head2 applies_to
19  
20 Returns true if the document contains any C<if> or C<unless> compound
21 statements that this mutator can target.
22  
23 =head3 Arguments
24  
25 =over 4
26  
27 =item * C<$doc>
28  
29 A L<PPI::Document> object to inspect.
30  
31 =back
32  
33 =head3 Returns
34  
35 A boolean.
36  
37 =head3 API specification
38  
39 =head4 input
40  
41 {
42 self => { type => OBJECT, isa => 'App::Test::Generator::Mutation::ConditionalInversion' },
43 doc => { type => OBJECT, isa => 'PPI::Document' },
44 }
45  
46 =head4 output
47  
48 { type => SCALAR }
49  
50 =cut
51  
52 70 ▶sub applies_to {
Subroutine applies_to: called CC=7 SCAR=19.7
Pod documented
53 70 my ($self, $doc) = @_;
54 70 ▶ my $compounds = $doc->find('PPI::Statement::Compound') || [];
Condition100%2/2
$doc->find('PPI::Statement::Compound') || []
l!l
3535
MC/DC100%1/1
$doc->find('PPI::Statement::Compound') || []
$doc->find('PPI::Statement::Compound')
Truth table100%2/2
A
A= $doc->find('PPI::Statement::Compound')
Aresult
11
00
55 70 for my $stmt (@{$compounds}) {
56 37 my $first = $stmt->schild(0);
57 37 ▶ next unless $first && $first->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $first and $first->isa("PPI::Token::Word") 0 37 37
Condition33%1/3
$first and $first->isa("PPI::Token::Word")
!ll&&!rl&&r
0037
MC/DC0%0/2
$first and $first->isa("PPI::Token::Word")
$first $first->isa("PPI::Token::Word")
Truth table33%1/3
A and B
A= $firstB= $first->isa("PPI::Token::Word")
ABresult
0X0
100
111
58 37 my $type = $first->content();
59 37 ▶ return 1 if $type eq 'if' || $type eq 'unless';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "if" or $type eq "unless" 35 2 37
Condition100%3/3
$type eq "if" or $type eq "unless"
l!l&&r!l&&!r
3412
MC/DC100%2/2
$type eq "if" or $type eq "unless"
$type eq "if" $type eq "unless"
Truth table100%3/3
A or B
A= $type eq "if"B= $type eq "unless"
ABresult
1X1
011
000
60 }
61 35 return 0;
62 }
63  
64 =head2 mutate
65  
66 Walk a PPI document and generate one mutant for each C<if> or C<unless>
67 statement, inverting the keyword to its opposite. This detects cases where
68 the test suite does not exercise both branches of a conditional.
69  
70 my $mutation = App::Test::Generator::Mutation::ConditionalInversion->new;
71 my $doc = PPI::Document->new(\$source);
72 my @mutants = $mutation->mutate($doc);
73  
74 for my $m (@mutants) {
75 print $m->id, ': ', $m->description, "\n";
76 }
77  
78 =head3 Arguments
79  
80 =over 4
81  
82 =item * C<$self>
83  
84 An instance of C<App::Test::Generator::Mutation::ConditionalInversion>.
85  
86 =item * C<$doc>
87  
88 A L<PPI::Document> object representing the parsed source file to mutate.
89 The document is not modified by this method.
90  
91 =back
92  
93 =head3 Returns
94  
95 A list of L<App::Test::Generator::Mutant> objects, one per C<if> or
96 C<unless> statement found in the document. Returns an empty list if no
97 qualifying statements are found.
98  
99 Each mutant carries a C<transform> closure that when called with a fresh
100 L<PPI::Document> copy will flip the targeted keyword from C<if> to
101 C<unless> or vice versa, targeting the exact statement by line and column
102 number.
103  
104 =head3 Notes
105  
106 Multiple conditionals on the same source line are each mutated
107 independently. Mutant IDs include both line and column number to ensure
108 uniqueness.
109  
110 Each mutant's optional C<context> field is always set to
111 C<conditional> (every mutant produced by this strategy targets an
112 C<if>/C<unless> keyword); its C<line_content> field holds the raw
113 source text of the mutated line. Both are consumed by
114 L<App::Test::Generator::Mutator>'s fast-mode dedup.
115  
116 =head3 API specification
117  
118 =head4 input
119  
120 {
121 self => {
122 type => OBJECT,
123 isa => 'App::Test::Generator::Mutation::ConditionalInversion',
124 },
125 doc => {
126 type => OBJECT,
127 isa => 'PPI::Document',
128 },
129 }
130  
131 =head4 output
132  
133 {
134 type => ARRAYREF,
135 elements => {
136 type => OBJECT,
137 isa => 'App::Test::Generator::Mutant',
138 },
139 }
140  
141 =cut
142  
143 53 ▶sub mutate {
Subroutine mutate: called CC=12 SCAR=25.4
Pod documented
144 53 my ($self, $doc) = @_;
145  
146 # Find all compound statements in the document
147 53 ▶ my $compounds = $doc->find('PPI::Statement::Compound') || [];
Condition100%2/2
$doc->find('PPI::Statement::Compound') || []
l!l
512
MC/DC100%1/1
$doc->find('PPI::Statement::Compound') || []
$doc->find('PPI::Statement::Compound')
Truth table100%2/2
A
A= $doc->find('PPI::Statement::Compound')
Aresult
11
00
148 53 my @mutants;
149  
150 53 for my $stmt (@{$compounds}) {
151 # Only process if and unless statements
152 # Use the actual first token content rather than ->type() since
153 # PPI >= 1.270 returns 'if' for both if and unless via ->type()
154 112 my $first_word = $stmt->schild(0);
155 112 ▶ next unless $first_word && $first_word->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $first_word and $first_word->isa("PPI::Token::Word") 0 112 112
Condition33%1/3
$first_word and $first_word->isa("PPI::Token::Word")
!ll&&!rl&&r
00112
MC/DC0%0/2
$first_word and $first_word->isa("PPI::Token::Word")
$first_word $first_word->isa("PPI::Token::Word")
Truth table33%1/3
A and B
A= $first_wordB= $first_word->isa("PPI::Token::Word")
ABresult
0X0
100
111
156 112 my $type = $first_word->content();
157 112 ▶ next unless $type eq 'if' || $type eq 'unless';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "if" or $type eq "unless" 5 107 112
Condition100%3/3
$type eq "if" or $type eq "unless"
l!l&&r!l&&!r
10255
MC/DC100%2/2
$type eq "if" or $type eq "unless"
$type eq "if" $type eq "unless"
Truth table100%3/3
A or B
A= $type eq "if"B= $type eq "unless"
ABresult
1X1
011
000
158  
159 # Verify the statement has a condition block to invert
160 107 my ($cond) = grep { $_->isa('PPI::Structure::Condition') } $stmt->children;
161 107 ▶ next unless $cond;
Branch: 1 branch
BranchTrueFalseTotal
unless $cond 0 107 107
162  
163 # Capture location for precise targeting in the transform closure
164 107 my $line = $stmt->location->[0];
165 107 my $col = $stmt->location->[1];
166  
167 # Determine what the keyword flips to
168 107 ▶ my $flipped = $type eq 'if' ? 'unless' : 'if';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'if' ? : 102 5 107
169  
170 107 my $mutant = eval {
171 App::Test::Generator::Mutant->new(
172 id => "COND_INV_${line}_${col}",
173 group => "COND_INV:$line",
174 description => "Invert condition $type to $flipped",
175 line => $line,
176 type => 'boolean',
177 original => $cond->content(),
178 context => 'conditional',
179 line_content => $self->_line_content($doc, $line),
180  
181 # Closure captures line, col and flipped so it targets
182 # exactly the right statement in the document copy
183 5 ▶ transform => sub {
Subroutine __ANON__: called CC=7 SCAR=20.4
184 5 my ($doc) = @_;
185 5 ▶ my $stmts = $doc->find('PPI::Statement::Compound') || [];
Condition50%1/2
$doc->find('PPI::Statement::Compound') || []
l!l
50
MC/DC0%0/1
$doc->find('PPI::Statement::Compound') || []
$doc->find('PPI::Statement::Compound')
Truth table50%1/2
A
A= $doc->find('PPI::Statement::Compound')
Aresult
11
00
186  
187 5 for my $stmt (@{$stmts}) {
188 # Match by line and column to avoid mutating
189 # the wrong conditional on the same line
190 6 ▶ next unless $stmt->location->[0] == $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $stmt->location->[0] == $line 1 5 6
191 5 ▶ next unless $stmt->location->[1] == $col;
Branch: 1 branch
BranchTrueFalseTotal
unless $stmt->location->[1] == $col 0 5 5
192  
193 # Flip the leading keyword
194 5 my $first = $stmt->schild(0);
195 5 ▶ next unless $first && $first->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $first and $first->isa("PPI::Token::Word") 0 5 5
Condition33%1/3
$first and $first->isa("PPI::Token::Word")
!ll&&!rl&&r
005
MC/DC0%0/2
$first and $first->isa("PPI::Token::Word")
$first $first->isa("PPI::Token::Word")
Truth table33%1/3
A and B
A= $firstB= $first->isa("PPI::Token::Word")
ABresult
0X0
100
111
196 5 $first->set_content($flipped);
197 5 last;
198 }
199 },
200 107 );
201 };
202  
203 # Report construction failures clearly rather than silently dropping
204 107 ▶ if($@ || !$mutant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $mutant) 2 105 107
Condition66%2/3
$@ or not $mutant
l!l&&r!l&&!r
20105
MC/DC50%1/2
$@ or not $mutant
$@ not $mutant
Truth table66%2/3
A or not(B)
A= $@B= not $mutant
ABresult
1X1
011
000
205 2 ▶ carp "Failed to construct mutant COND_INV_${line}_${col}: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 0 2
206 2 next;
207 }
208  
209 105 push @mutants, $mutant;
210 }
211  
212 53 return @mutants;
213 }
214  
215 21 1;
« lib/App/Test/Generator/Analyzer/SideEffect.pm ↑ Summary lib/App/Test/Generator/Mutation/NumericBoundary.pm »