Modify/Rule.pm

filter: Statementstmt 64.7% 88 / 136 Branchbran 53.7% 43 / 80 Conditioncond 75.0% 12 / 16 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 83.3% 10 / 12 Podpod 100% 5 / 5 totaltotal 63.1% 163 / 258 CC 52 SCAR 39.0
CC
52
Cov
62%
process
77.5
new
52.0
getModificationStats
5.0
CRAP 49.2
↑ Summary Buffer.pm »
1 package Text::Modify::Rule;
2  
3 # TODO Concept change to support blocks/insert/addIfMissing options
4 # maybe this has to be moved outside of rule, as a rule has no scope of work only a single line
5 # the concept has to be extended to working on the whole file/block, with a special concept to
6 # handle large files (>100KB) with autodetection of file size (slow but working)
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Text::Buffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 $VERSION="0.4";
14 }
15  
16 #====================================================
17 # Possible usage and params:
18 # replace=>'texttoreplace',with=>'anothertext'
19 # optional:
20 # ifMissing=>'insert|append|warn|fail'
21 # match=>'first' (last not implemented yet)
22 #====================================================
23 11 ▶sub new {
Subroutine new: called CC=23 SCAR=39.5
Pod documented
24 11 my $class = shift;
25 11 my $self = {
26 addcount => 0,
27 deletecount => 0,
28 matchcount => 0,
29 replacecount => 0,
30 ignorecase => 1,
31 dryrun => 0,
32 matchfirst => 65535,
33 _debug => 0
34 };
35 11 bless $self, $class;
36 11 $self->_clearError();
37 11 my %opts = @_;
38 11 ▶ if ( $opts{debug} ) { $self->{_debug} = $opts{debug}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"debug"}) 5 6 11
39 11 $self->{'type'} = undef;
40 11 ▶ if ( $opts{replace} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($opts{'replace'}) { } 10 1 11
elsif ($opts{'insert'}) { } 0 1 1
elsif ($opts{'delete'}) { } 1 0 1
elsif ($opts{'move'}) { } 0 0 0
41  
42 10 ▶ if ( defined( $opts{with} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"with"}) 10 0 10
43 10 $self->{type} = 'replace';
44 # TODO need to distinguish between string, wildcard, regex here
45 10 ▶ $self->{replacetype} = $opts{type} || "regex";
Condition100%2/2
$opts{'type'} || 'regex'
l!l
82
MC/DC100%1/1
$opts{'type'} || 'regex'
$opts{'type'}
Truth table100%2/2
A
A= $opts{'type'}
Aresult
11
00
46 10 ▶ if ($self->{replacetype} eq "wildcard") {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'replacetype'} eq 'wildcard') { } 1 9 10
elsif ($self->{'replacetype'} eq 'string') { } 2 7 9
47 1 $self->{regex} = Text::Buffer->convertWildcardToRegex($opts{replace});
48 }
49 elsif ($self->{replacetype} eq "string") {
50 2 $self->{regex} = Text::Buffer->convertStringToRegex($opts{replace});
51 } else {
52 7 $self->{regex} = $opts{replace};
53 }
54 # Set available options
55 10 foreach (qw(replace string wildcard with dryrun ignorecase matchfirst ifmissing)) {
56 80 ▶ $self->{$_} = $opts{$_} if ( defined( $opts{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{$_} 24 56 80
57 }
58 10 $self->{with} =~ s?(^|[^\\])/?$1\\/?g;
59 10 $self->_debug(sprintf("after escape: type=%s regex='%s' with='%s' (orig='%s')", $self->{replacetype}, $self->{regex}, $self->{with}, $opts{replace}));
60
61  
62 # Create the regex options from params
63 10 ▶ $self->{opts} .= ( $self->{ignorecase} ? "i" : "" );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ignorecase'} ? : 10 0 10
64 }
65 }
66 elsif ( $opts{insert} ) {
67 0 ▶ if ( defined( $opts{at} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"at"}) 0 0 0
68 0 $self->{type} = 'insert';
69 0 $self->{regex} = "";
70 0 $self->{with} = $opts{insert};
71  
72 # Set available options
73 0 foreach (qw(insert at dryrun ignorecase ifmissing)) {
74 0 ▶ $self->{$_} = $opts{$_} if ( defined( $opts{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{$_} 0 0 0
75 }
76 }
77 }
78 elsif ( $opts{delete} ) {
79 1 $self->{type} = 'delete';
80 1 $self->{regex} = $opts{delete};
81  
82 # Set available options
83 1 foreach (qw(dryrun ignorecase matchfirst)) {
84 3 ▶ $self->{$_} = $opts{$_} if ( defined( $opts{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{$_} 0 3 3
85 }
86 }
87 elsif ( $opts{move} ) {
88  
89 # TODO move option not implemented
90 0 ▶ if ( defined( $opts{to} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"to"}) 0 0 0
91 0 $self->{type} = 'move';
92 0 $self->{regex} = $opts{move};
93  
94 # Set available options
95 0 foreach (qw(move to dryrun ignorecase matchfirst ifmissing)) {
96 0 ▶ $self->{$_} = $opts{$_} if ( defined( $opts{$_} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{$_} 0 0 0
97 }
98 }
99 }
100 11 ▶ if ( !$self->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"type"}) 0 11 11
101 0 $self->_debug( "Unknown type" );
102 0 $self->_setError("Unknown Rule type");
103 0 return undef;
104 }
105 11 ▶ if ( !defined( $self->{opts} ) ) { $self->{opts} = ""; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"opts"}) 1 10 11
106 11 return $self;
107 }
108  
109 11 ▶sub getModificationStats {
Subroutine getModificationStats: called CC=5 SCAR=16.1
Pod documented
110 11 my $self = shift;
111 return (($self->{matchcount} || 0),
112 ($self->{addcount} || 0),
113 ($self->{deletecount} || 0),
114 11 ▶ ($self->{replacecount} || 0));
Condition87%7/8
$self->{'matchcount'} || 0
l!l
92
$self->{'addcount'} || 0
l!l
110
$self->{'deletecount'} || 0
l!l
011
$self->{'replacecount'} || 0
l!l
92
MC/DC100%1/1
$self->{'matchcount'} || 0
$self->{'matchcount'}
MC/DC100%1/1
$self->{'addcount'} || 0
$self->{'addcount'}
MC/DC0%0/1
$self->{'deletecount'} || 0
$self->{'deletecount'}
MC/DC100%1/1
$self->{'replacecount'} || 0
$self->{'replacecount'}
Truth table100%2/2
A
A= $self->{'matchcount'}
Aresult
11
00
Truth table100%2/2
A
A= $self->{'addcount'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'deletecount'}
Aresult
11
00
Truth table100%2/2
A
A= $self->{'replacecount'}
Aresult
11
00
115 }
116  
117 #==================================
118 # Process block of lines
119 #==================================
120 11 ▶sub process {
Subroutine process: called CC=24 SCAR=43.5
Pod documented
121 11 my $self = shift;
122 11 my $txt = shift;
123 0 ▶ if ( !( $txt && $txt->isa("Text::Buffer") ) ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($txt and $txt->isa("Text::Buffer")) 0 11 11
Condition33%1/3
$txt and $txt->isa("Text::Buffer")
!ll&&!rl&&r
0011
MC/DC0%0/2
$txt and $txt->isa("Text::Buffer")
$txt $txt->isa("Text::Buffer")
Truth table33%1/3
A and B
A= $txtB= $txt->isa("Text::Buffer")
ABresult
0X0
100
111
124 11 my @insertblock;
125 my @appendblock;
126  
127 # Start processing
128 $self->_debug( "processing rule of type $self->{type}, regex is " .
129 (defined($self->{regex}) ? $self->{regex} : "undef" ) .
130 11 ▶ ", with is " . (defined($self->{with}) ? $self->{with} : "undef" ));
Branch: 2 branches
BranchTrueFalseTotal
defined $self->{'regex'} ? : 11 0 11
defined $self->{'with'} ? : 10 1 11
131 11 my $i = 0;
132 11 my $abs = 0;
133 11 my ( $match, $opts ) = ( $self->{regex}, $self->{opts} );
134 11 my $found = 0;
135 11 my $rc = 1; # Return code for this function
136 11 $txt->goto('top');
137 11 my $string = $txt->get();
138  
139 11 ▶ if ($self->{type} ne "insert") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} ne "insert") 11 0 11
140 11 ▶ while ( defined($string) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $string) 78 11 89
141 78 $abs++;
142 78 ▶ if ( $self->{matchcount} >= $self->{matchfirst} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"matchcount"} >= $self->{"matchfirst"}) 0 78 78
143 0 $self->_debug( "First matches reached, ignoring rest for this rule" );
144 0 last;
145 }
146 78 eval "\$found = (\$string =~ /$match/$opts);";
147 78 $self->_debug( "Eval: \$found = ('$string' =~ /$match/$opts) = $found" );
148 78 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 12 66 78
149 12 $self->{matchcount}++;
150
151 # TODO complete all functionality here (replace,insert,delete,move)
152 12 $self->_debug( "Found match on line $abs (rel $i): $string" );
153 12 ▶ if ( $self->{type} eq "delete" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'type'} eq 'delete') { } 0 12 12
elsif ($self->{'type'} eq 'move') { } 0 12 12
elsif ($self->{'type'} eq 'replace') { } 12 0 12
154 0 $self->{deletecount}++;
155
156 # Should be deleted from array
157 0 $self->_debug( "deleting line" );
158 0 $txt->delete();
159 0 $string = $txt->get();
160 0 next;
161 }
162 elsif ( $self->{type} eq "move" ) {
163
164 # Should be deleted from array
165 0 $self->{addcount}++;
166 0 $self->{deletecount}++;
167 0 $self->_debug( "moving line" );
168 0 ▶ if ( $self->{to} eq "top" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'to'} eq 'top') { } 0 0 0
169 0 $txt->insert($string);
170 }
171 else {
172 0 $txt->append($string);
173 }
174 0 $txt->delete();
175 0 $string = $txt->get();
176 0 next;
177 }
178 elsif ( $self->{type} eq "replace" ) {
179 12 $self->_debug( "replacing with $self->{'with'}" );
180 12 my $tmp = $string;
181 12 eval "\$tmp =~ s/$match/$self->{with}/g$opts";
182 12 ▶ if ( $tmp ne $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp ne $string) 12 0 12
183 12 $self->{replacecount}++;
184 }
185 12 $txt->set($tmp);
186 }
187 else {
188 0 $self->_setError("not processed by any rule");
189 0 return 0;
190 }
191 }
192 78 $string = $txt->next();
193 }
194 }
195  
196 11 ▶ if ( $self->{type} eq "insert" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "insert") 0 11 11
197  
198 # Should be deleted from array
199 0 $self->{addcount}++;
200 0 ▶ if ( $self->{at} eq "insert" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'at'} eq 'insert') { } 0 0 0
201 0 $self->_debug( "inserting line:" . $self->{with});
202 0 $txt->insert( $self->{with} );
203 }
204 else {
205 0 $self->_debug( "appending line" . $self->{with} );
206 0 $txt->append( $self->{with} );
207 }
208 }
209  
210 # process missing elements
211 $self->_debug(
212 "Processing ifmissing: ifmissing="
213 . ( $self->{ifmissing} ? $self->{ifmissing} : "unset" )
214 . " matches="
215 . $self->{matchcount}
216 11 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ifmissing'} ? : 1 10 11
217 11 ▶ if ( $self->{ifmissing} && $self->{matchcount} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ifmissing"} and $self->{"matchcount"} == 0) 1 10 11
Condition66%2/3
$self->{"ifmissing"} and $self->{"matchcount"} == 0
!ll&&!rl&&r
1001
MC/DC50%1/2
$self->{"ifmissing"} and $self->{"matchcount"} == 0
$self->{"ifmissing"} $self->{"matchcount"} == 0
Truth table66%2/3
A and B
A= $self->{"ifmissing"}B= $self->{"matchcount"} == 0
ABresult
0X0
100
111
218  
219 # Add the missing element now
220 1 $self->{addcount}++;
221 1 ▶ if ( $self->{ifmissing} eq "insert" ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'ifmissing'} eq 'insert') { } 1 0 1
elsif ($self->{'ifmissing'} eq 'append') { } 0 0 0
elsif ($self->{'ifmissing'} eq 'ignore') { } 0 0 0
elsif ($self->{'ifmissing'} eq 'error') { } 0 0 0
222 1 $self->_debug( "inserting missing line" );
223 1 $txt->insert( $self->{with} );
224 }
225 elsif ( $self->{ifmissing} eq "append" ) {
226 0 $self->_debug( "appending missing line" );
227 0 $txt->append( $self->{with} );
228 }
229 elsif ( $self->{ifmissing} eq "ignore" ) {
230 0 $self->_debug( "ignoring missing line" );
231 }
232 elsif ( $self->{ifmissing} eq "error" ) {
233 0 $self->_setError("Required line $match not found");
234 0 $rc = 0;
235 }
236 }
237  
238 11 ▶ if ( $self->{_debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_debug"}) 5 6 11
239 5 $self->_debug( "=== OUT ===\n" . $txt->dumpAsString() . "=== EOF ===" );
240 }
241  
242 11 return $rc;
243 }
244  
245 11 ▶sub isError { my $self = shift; return ( $self->{error} ne "" ); }
Subroutine isError: called CC=1 SCAR=0.0
Pod documented
246 0 ▶sub getError { return shift->{error}; }
Subroutine getError: not called CC=1 SCAR=6.9
Pod documented
247 11 ▶sub _clearError { my $self = shift; $self->{error} = ""; }
Subroutine _clearError: called CC=1 SCAR=0.0
248 0 ▶sub _setError { my $self = shift; $self->{error} = shift; }
Subroutine _setError: not called CC=1 SCAR=6.9
249  
250 140 ▶sub _debug {
Subroutine _debug: called CC=3 SCAR=11.4
251 140 my $self = shift;
252 140 ▶ if ($#_ == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == -1) { } 0 140 140
elsif ($self->{'_debug'}) { } 64 76 140
253 0 return $self->{_debug};
254 }
255 elsif ( $self->{_debug} ) {
256 64 print "[DEBUG] @_\n";
257 }
258 }
259  
260 3 1;
261  
262 __END__
↑ Summary Buffer.pm »