| 1 | package App::Test::Generator::Mutation::Base; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 29 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 29 | our $VERSION = '0.49'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 VERSION | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Version 0.49 | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Abstract base class for all mutation strategies in | ||||||||||||||||||||||||||||
| 16 | App::Test::Generator. Subclasses must implement both | ||||||||||||||||||||||||||||
| 17 | C<applies_to> and C<mutate>. | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head2 new | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Construct a new mutation strategy object. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $strategy = My::Mutation::Subclass->new; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head3 Arguments | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | None. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head3 Returns | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | A blessed hashref of the subclass type. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head3 API specification | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head4 input | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | {} | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head4 output | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||
| 42 | type => OBJECT, | ||||||||||||||||||||||||||||
| 43 | isa => 'App::Test::Generator::Mutation::Base', | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1665 | ▶ | sub new { bless {}, shift } | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 applies_to | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Return true if this mutation strategy applies to the | ||||||||||||||||||||||||||||
| 53 | given PPI document. Subclasses must override this method. | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | if ($strategy->applies_to($doc)) { | ||||||||||||||||||||||||||||
| 56 | my @mutants = $strategy->mutate($doc); | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head3 Arguments | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =over 4 | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item * C<$doc> | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | A L<PPI::Document> object. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =back | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head3 Returns | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | A boolean. Croaks if called on the base class directly. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head3 API specification | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head4 input | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | { | ||||||||||||||||||||||||||||
| 78 | self => { type => OBJECT, isa => 'App::Test::Generator::Mutation::Base' }, | ||||||||||||||||||||||||||||
| 79 | doc => { type => OBJECT, isa => 'PPI::Document' }, | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head4 output | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | { type => SCALAR } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 8 | ▶ | sub applies_to { | ||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 8 | my ($self, $doc) = @_; | |||||||||||||||||||||||||||
| 90 | 8 | croak ref($self) . '::applies_to() must be implemented by subclass'; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 mutate | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Generate and return a list of mutants for the given PPI | ||||||||||||||||||||||||||||
| 96 | document. Subclasses must override this method. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | my @mutants = $strategy->mutate($doc); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head3 Arguments | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =over 4 | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =item * C<$doc> | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | A L<PPI::Document> object representing the source file | ||||||||||||||||||||||||||||
| 107 | to mutate. Must not be modified by this method. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =back | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head3 Returns | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | A list of L<App::Test::Generator::Mutant> objects. | ||||||||||||||||||||||||||||
| 114 | Croaks if called on the base class directly. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head3 API specification | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head4 input | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | self => { type => OBJECT, isa => 'App::Test::Generator::Mutation::Base' }, | ||||||||||||||||||||||||||||
| 122 | doc => { type => OBJECT, isa => 'PPI::Document' }, | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head4 output | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | type => ARRAYREF, | ||||||||||||||||||||||||||||
| 129 | elements => { type => OBJECT, isa => 'App::Test::Generator::Mutant' }, | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 8 | ▶ | sub mutate { | ||||||||||||||||||||||||||
|
Subroutine
mutate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 8 | my ($self, $doc) = @_; | |||||||||||||||||||||||||||
| 136 | 8 | croak ref($self) . '::mutate() must be implemented by subclass'; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # -------------------------------------------------- | ||||||||||||||||||||||||||||
| 140 | # _line_content | ||||||||||||||||||||||||||||
| 141 | # | ||||||||||||||||||||||||||||
| 142 | # Purpose: Fetch the raw source text of a single line, | ||||||||||||||||||||||||||||
| 143 | # for the optional line_content field on a | ||||||||||||||||||||||||||||
| 144 | # Mutant (used by Mutator::_is_redundant_mutation | ||||||||||||||||||||||||||||
| 145 | # to skip mutations targeting comment-only lines). | ||||||||||||||||||||||||||||
| 146 | # | ||||||||||||||||||||||||||||
| 147 | # Entry: $doc - a PPI::Document. | ||||||||||||||||||||||||||||
| 148 | # $line - a 1-based line number. | ||||||||||||||||||||||||||||
| 149 | # | ||||||||||||||||||||||||||||
| 150 | # Exit: Returns the text of that line, or '' if out | ||||||||||||||||||||||||||||
| 151 | # of range. | ||||||||||||||||||||||||||||
| 152 | # | ||||||||||||||||||||||||||||
| 153 | # Side effects: None. | ||||||||||||||||||||||||||||
| 154 | # -------------------------------------------------- | ||||||||||||||||||||||||||||
| 155 | 1043 | ▶ | sub _line_content { | ||||||||||||||||||||||||||
|
Subroutine
_line_content: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 156 | 1043 | my ($self, $doc, $line) = @_; | |||||||||||||||||||||||||||
| 157 | 1043 | my @lines = split /\n/, $doc->serialize; | |||||||||||||||||||||||||||
| 158 | 1043 | ▶ | return $lines[$line - 1] // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$lines[$line - 1] // ""
MC/DC100%1/1
$lines[$line - 1] // ""
$lines[$line - 1]
Truth table100%2/2
A
A= $lines[$line - 1]
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # -------------------------------------------------- | ||||||||||||||||||||||||||||
| 162 | # _in_conditional | ||||||||||||||||||||||||||||
| 163 | # | ||||||||||||||||||||||||||||
| 164 | # Purpose: Determine whether a PPI node sits inside | ||||||||||||||||||||||||||||
| 165 | # (or is itself the keyword of) an if/unless/ | ||||||||||||||||||||||||||||
| 166 | # while/until compound statement, for the | ||||||||||||||||||||||||||||
| 167 | # optional context field on a Mutant. | ||||||||||||||||||||||||||||
| 168 | # | ||||||||||||||||||||||||||||
| 169 | # Entry: $node - a PPI::Element. | ||||||||||||||||||||||||||||
| 170 | # | ||||||||||||||||||||||||||||
| 171 | # Exit: Returns 1 if an ancestor (or the node itself) | ||||||||||||||||||||||||||||
| 172 | # is an if/unless/while/until compound statement, | ||||||||||||||||||||||||||||
| 173 | # 0 otherwise. | ||||||||||||||||||||||||||||
| 174 | # | ||||||||||||||||||||||||||||
| 175 | # Side effects: None. | ||||||||||||||||||||||||||||
| 176 | # -------------------------------------------------- | ||||||||||||||||||||||||||||
| 177 | 642 | ▶ | sub _in_conditional { | ||||||||||||||||||||||||||
|
Subroutine
_in_conditional: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 178 | 642 | my ($self, $node) = @_; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 642 | ▶ | for(my $parent = $node; $parent; $parent = $parent->parent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 2448 | ▶ | next unless $parent->isa('PPI::Statement::Compound'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 293 | my $first = $parent->schild(0); | |||||||||||||||||||||||||||
| 183 | 293 | ▶ | next unless $first && $first->isa('PPI::Token::Word'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$first and $first->isa("PPI::Token::Word")
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")
| |||||||||||||||||||||||||||||
| 184 | 293 | ▶ | return 1 if $first->content =~ /^(?:if|unless|while|until)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 351 | return 0; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 29 | 1; | |||||||||||||||||||||||||||