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

filter: Statementstmt 100% 61 / 61 Branchbran 85.0% 17 / 20 Conditioncond 57.1% 12 / 21 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 84.6% 105 / 124 CC 23 SCAR 20.6
CC
23
Cov
88%
mutate
12.2
applies_to
7.7
__ANON__
6.1
CRAP 7.8
« lib/App/Test/Generator/Mutation/ConditionalInversion.pm ↑ Summary bin/pod-example-tester »
1 package App::Test::Generator::Mutation::NumericBoundary;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use parent 'App::Test::Generator::Mutation::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use App::Test::Generator::Mutant;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 our $VERSION = '0.49';
11  
12 =head1 VERSION
13  
14 Version 0.49
15  
16 =cut
17  
18 # --------------------------------------------------
19 # Mapping of each comparison operator to the list of
20 # operators it should be flipped to when mutating.
21 # Both directions are covered so that e.g. != can be
22 # mutated to == and vice versa.
23 # --------------------------------------------------
24 22 my %FLIP = (
25 '>' => [ '<', '>=', '<=' ],
26 '<' => [ '>', '<=', '>=' ],
27 '>=' => [ '>', '<', '<=' ],
28 '<=' => [ '<', '>', '>=' ],
29 '==' => [ '!=' ],
30 '!=' => [ '==' ],
31 );
32  
33 =head2 applies_to
34  
35 Returns true if the document contains any comparison operators that this
36 mutator can target (C<E<gt>>, C<E<lt>>, C<E<gt>=>, C<E<lt>=>, C<==>,
37 C<!=>).
38  
39 =head3 Arguments
40  
41 =over 4
42  
43 =item * C<$doc>
44  
45 A L<PPI::Document> object to inspect.
46  
47 =back
48  
49 =head3 Returns
50  
51 A boolean.
52  
53 =head3 API specification
54  
55 =head4 input
56  
57 {
58 self => { type => OBJECT, isa => 'App::Test::Generator::Mutation::NumericBoundary' },
59 doc => { type => OBJECT, isa => 'PPI::Document' },
60 }
61  
62 =head4 output
63  
64 { type => SCALAR }
65  
66 =cut
67  
68 72 ▶sub applies_to {
Subroutine applies_to: called CC=7 SCAR=20.4
Pod documented
69 72 my ($self, $doc) = @_;
70 72 ▶ my $ops = $doc->find('PPI::Token::Operator') || [];
Condition100%2/2
$doc->find('PPI::Token::Operator') || []
l!l
5418
MC/DC100%1/1
$doc->find('PPI::Token::Operator') || []
$doc->find('PPI::Token::Operator')
Truth table100%2/2
A
A= $doc->find('PPI::Token::Operator')
Aresult
11
00
71 72 for my $op (@{$ops}) {
72 196 ▶ next unless exists $FLIP{$op->content()};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $FLIP{$op->content} 151 45 196
73 45 my $parent = $op->parent();
74 45 ▶ next unless $parent->isa('PPI::Statement')
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->isa("PPI::Statement") or $parent->isa("PPI::Structure::Condition") or $parent->isa("PPI::Structure::Block") 0 45 45
Condition33%2/6
$parent->isa("PPI::Statement") or $parent->isa("PPI::Structure::Condition") or $parent->isa("PPI::Structure::Block")
l!l&&r!l&&!r
4500
$parent->isa("PPI::Statement") or $parent->isa("PPI::Structure::Condition")
l!l&&r!l&&!r
4500
MC/DC0%0/3
$parent->isa("PPI::Statement") or $parent->isa("PPI::Structure::Condition") or $parent->isa("PPI::Structure::Block")
$parent->isa("PPI::Statement") $parent->isa("PPI::Structure::Condition") $parent->isa("PPI::Structure::Block")
Truth table25%1/4
A or B or C
A= $parent->isa("PPI::Statement")B= $parent->isa("PPI::Structure::Condition")C= $parent->isa("PPI::Structure::Block")
ABCresult
1XX1
01X1
0011
0000
75 || $parent->isa('PPI::Structure::Condition')
76 || $parent->isa('PPI::Structure::Block');
77 45 return 1;
78 }
79 27 return 0;
80 }
81  
82 =head2 mutate
83  
84 Walk a PPI document and generate one mutant for each comparison operator
85 that can be flipped to reveal a boundary condition not caught by the test
86 suite. For example, C<E<gt>=> is flipped to C<E<gt>>, C<E<lt>>, and
87 C<E<lt>=> in turn, producing three independent mutants.
88  
89 my $mutation = App::Test::Generator::Mutation::NumericBoundary->new;
90 my $doc = PPI::Document->new(\$source);
91 my @mutants = $mutation->mutate($doc);
92  
93 for my $m (@mutants) {
94 print $m->id, ': ', $m->description, "\n";
95 }
96  
97 =head3 Arguments
98  
99 =over 4
100  
101 =item * C<$self>
102  
103 An instance of C<App::Test::Generator::Mutation::NumericBoundary>.
104  
105 =item * C<$doc>
106  
107 A L<PPI::Document> object representing the parsed source file to mutate.
108 The document is not modified by this method.
109  
110 =back
111  
112 =head3 Returns
113  
114 A list of L<App::Test::Generator::Mutant> objects, one per
115 (operator, flip) pair found in the document. Returns an empty list if no
116 qualifying comparison operators are found.
117  
118 Each mutant carries a C<transform> closure that when called with a fresh
119 L<PPI::Document> copy will replace the targeted operator with its flipped
120 equivalent, targeting the exact operator by line and column number to
121 ensure that multiple comparison operators on the same source line are each
122 mutated independently.
123  
124 =head3 Notes
125  
126 The following operators and their flips are supported:
127  
128 > flips to < >= <=
129 < flips to > <= >=
130 >= flips to > < <=
131 <= flips to < > >=
132 == flips to !=
133 != flips to ==
134  
135 Mutant IDs include line number, column number, and the flip target to
136 ensure uniqueness even when multiple operators share a source line.
137  
138 Each mutant's optional C<context> field is set to C<conditional> if
139 the operator sits inside (or is itself the keyword of) an
140 C<if>/C<unless>/C<while>/C<until> compound statement, or C<expression>
141 otherwise; its C<line_content> field holds the raw source text of the
142 mutated line. Both are consumed by
143 L<App::Test::Generator::Mutator>'s fast-mode dedup.
144  
145 =head3 API specification
146  
147 =head4 input
148  
149 {
150 self => {
151 type => OBJECT,
152 isa => 'App::Test::Generator::Mutation::NumericBoundary',
153 },
154 doc => {
155 type => OBJECT,
156 isa => 'PPI::Document',
157 },
158 }
159  
160 =head4 output
161  
162 {
163 type => ARRAYREF,
164 elements => {
165 type => OBJECT,
166 isa => 'App::Test::Generator::Mutant',
167 },
168 }
169  
170 =cut
171  
172 83 ▶sub mutate {
Subroutine mutate: called CC=12 SCAR=25.0
Pod documented
173 83 my ($self, $doc) = @_;
174  
175 # Find all operator tokens in the document
176 83 ▶ my $ops = $doc->find('PPI::Token::Operator') || [];
Condition100%2/2
$doc->find('PPI::Token::Operator') || []
l!l
812
MC/DC100%1/1
$doc->find('PPI::Token::Operator') || []
$doc->find('PPI::Token::Operator')
Truth table100%2/2
A
A= $doc->find('PPI::Token::Operator')
Aresult
11
00
177 83 my @mutants;
178  
179 83 for my $op (@{$ops}) {
180 455 my $original = $op->content();
181  
182 # Only process comparison operators that have defined flips
183 455 ▶ next unless exists $FLIP{$original};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $FLIP{$original} 293 162 455
184  
185 # Only mutate operators that are direct children of
186 # a condition or expression, not list arguments
187 162 my $parent = $op->parent();
188 162 ▶ next unless $parent->isa('PPI::Statement')
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->isa("PPI::Statement") or $parent->isa("PPI::Structure::Condition") or $parent->isa("PPI::Structure::Block") 0 162 162
Condition33%2/6
$parent->isa("PPI::Statement") or $parent->isa("PPI::Structure::Condition") or $parent->isa("PPI::Structure::Block")
l!l&&r!l&&!r
16200
$parent->isa("PPI::Statement") or $parent->isa("PPI::Structure::Condition")
l!l&&r!l&&!r
16200
MC/DC0%0/3
$parent->isa("PPI::Statement") or $parent->isa("PPI::Structure::Condition") or $parent->isa("PPI::Structure::Block")
$parent->isa("PPI::Statement") $parent->isa("PPI::Structure::Condition") $parent->isa("PPI::Structure::Block")
Truth table25%1/4
A or B or C
A= $parent->isa("PPI::Statement")B= $parent->isa("PPI::Structure::Condition")C= $parent->isa("PPI::Structure::Block")
ABCresult
1XX1
01X1
0011
0000
189 || $parent->isa('PPI::Structure::Condition')
190 || $parent->isa('PPI::Structure::Block');
191  
192 # Capture location so the transform closure targets the
193 # exact operator rather than the first match on that line
194 162 my $line = $op->location->[0];
195 162 my $col = $op->location->[1];
196  
197 # PPI always wraps a condition's content in a
198 # PPI::Statement::Expression, so the operator's immediate
199 # parent is never literally PPI::Structure::Condition --
200 # use the shared ancestor-walking helper instead, as
201 # BooleanNegation and ReturnUndef do, to correctly detect
202 # operators inside if/unless/while/until conditions
203 162 ▶ my $context = $self->_in_conditional($op) ? 'conditional' : 'expression';
Branch: 1 branch
BranchTrueFalseTotal
$self->_in_conditional($op) ? : 114 48 162
204  
205 # Generate one mutant per flip of this operator
206 162 for my $change (@{ $FLIP{$original} }) {
207 # Build a unique id from location and the specific flip
208 # so multiple operators on the same line don't collide
209 458 my $id = "NUM_BOUNDARY_${line}_${col}_${change}";
210  
211 458 my $mutant = eval {
212 App::Test::Generator::Mutant->new(
213 id => $id,
214 group => "NUM_BOUNDARY:$line",
215 description => "Numeric boundary flip $original to $change",
216 original => $original,
217 line => $line,
218 type => 'comparison',
219 context => $context,
220 line_content => $self->_line_content($doc, $line),
221  
222 # The transform closure captures line, col, original
223 # and change so it targets precisely the right operator
224 # in the document copy it receives at test time
225 15 ▶ transform => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
226 15 my $doc = $_[0];
227 15 ▶ my $ops = $doc->find('PPI::Token::Operator') || [];
Condition50%1/2
$doc->find('PPI::Token::Operator') || []
l!l
150
MC/DC0%0/1
$doc->find('PPI::Token::Operator') || []
$doc->find('PPI::Token::Operator')
Truth table50%1/2
A
A= $doc->find('PPI::Token::Operator')
Aresult
11
00
228  
229 15 for my $op (@{$ops}) {
230 24 ▶ next unless $op->line_number == $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $op->line_number == $line 1 23 24
231 23 ▶ next unless $op->column_number == $col;
Branch: 1 branch
BranchTrueFalseTotal
unless $op->column_number == $col 8 15 23
232 15 ▶ next unless $op->content eq $original;
Branch: 1 branch
BranchTrueFalseTotal
unless $op->content eq $original 0 15 15
233  
234 15 $op->set_content($change);
235 15 last;
236 }
237 },
238 458 );
239 };
240  
241 # If the Mutant construction fails, report clearly rather than
242 # silently dropping the mutant from the results
243 458 ▶ if($@ || !$mutant) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $mutant) 6 452 458
Condition100%3/3
$@ or not $mutant
l!l&&r!l&&!r
33452
MC/DC100%2/2
$@ or not $mutant
$@ not $mutant
Truth table100%3/3
A or not(B)
A= $@B= not $mutant
ABresult
1X1
011
000
244 6 ▶ carp "Failed to construct mutant $id: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 3 6
245 6 next;
246 }
247  
248 452 push @mutants, $mutant;
249 }
250 }
251  
252 83 return @mutants;
253 }
254  
255 22 1;
« lib/App/Test/Generator/Mutation/ConditionalInversion.pm ↑ Summary bin/pod-example-tester »