blib/lib/App/NDTools/NDDiff.pm

filter: Statementstmt 98.6% 298 / 302 Branchbran 100% 122 / 122 Conditioncond 100% 39 / 39 MC/DCmcdc 100% 25 / 25 Subroutinesub 90.7% 39 / 43 Podpod 0.0% 0 / 21 totaltotal 94.7% 523 / 552 CC 82 SCAR 21.7
CC
82
Cov
99%
diff_term
23.0
exec
15.0
configure
9.0
CRAP 8.8
« blib/lib/App/NDTools/Test.pm ↑ Summary blib/lib/App/NDTools/NDProc.pm »
1 package App::NDTools::NDDiff;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use parent 'App::NDTools::NDTool';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Algorithm::Diff qw(compact_diff);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::Patch 0.04 qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use App::NDTools::Slurp qw(s_dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use App::NDTools::Util qw(is_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Log::Log4Cli 0.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Struct::Diff 0.96 qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Struct::Diff::MergePatch qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Struct::Path 0.80 qw(path path_delta);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Struct::Path::PerlStyle 0.80 qw(str2path path2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Term::ANSIColor qw(color);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 our $VERSION = '0.61';
20  
21 3 my $JSON = JSON->new->canonical->allow_nonref;
22 3 my %COLOR;
23  
24 81 ▶sub arg_opts {
Subroutine arg_opts: called CC=1 SCAR=0.0
Pod undocumented
25 81 my $self = shift;
26  
27 return (
28 $self->SUPER::arg_opts(),
29 'A!' => \$self->{OPTS}->{diff}->{A},
30 'N!' => \$self->{OPTS}->{diff}->{N},
31 'O!' => \$self->{OPTS}->{diff}->{O},
32 'R!' => \$self->{OPTS}->{diff}->{R},
33 'U!' => \$self->{OPTS}->{diff}->{U},
34 4 ▶ 'brief' => sub { $self->{OPTS}->{ofmt} = $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 'colors!' => \$self->{OPTS}->{colors},
36 'ctx-text=i' => \$self->{OPTS}->{'ctx-text'},
37 'grep=s@' => \$self->{OPTS}->{grep},
38 9 ▶ 'json' => sub { $self->{OPTS}->{ofmt} = $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 'ignore=s@' => \$self->{OPTS}->{ignore},
40 1 ▶ 'rules' => sub { $self->{OPTS}->{ofmt} = $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 'quiet|q' => \$self->{OPTS}->{quiet},
42 'show' => \$self->{OPTS}->{show},
43 )
44 81 }
45  
46 80 ▶sub check_args {
Subroutine check_args: called CC=4 SCAR=13.9
Pod undocumented
47 80 my $self = shift;
48  
49 80 ▶ if ($self->{OPTS}->{show}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'OPTS'}{'show'}) { } 6 74 80
elsif (@_ < 2) { } 1 73 74
50 6 ▶ die_fatal "At least one argument expected when --show used", 1
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 5 6
51 unless (@_);
52 } elsif (@_ < 2) {
53 1 die_fatal "At least two arguments expected for diff", 1;
54 }
55  
56 78 return $self;
57 }
58  
59 80 ▶sub configure {
Subroutine configure: called CC=9 SCAR=22.0
Pod undocumented
60 80 my $self = shift;
61  
62 80 $self->SUPER::configure();
63  
64 $self->{OPTS}->{colors} = $self->{TTY}
65 78 ▶ unless (defined $self->{OPTS}->{colors});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"OPTS"}{"colors"} 74 4 78
66  
67 # resolve colors
68 78 ▶ while (my ($k, $v) = each %{$self->{OPTS}->{term}->{line}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"OPTS"}{"term"}{"line"};}) 546 78 624
69 546 ▶ if ($self->{OPTS}->{colors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'OPTS'}{'colors'}) { } 28 518 546
70 28 $COLOR{$k} = color($v);
71 28 $COLOR{"B$k"} = color("bold $v");
72 } else {
73 518 $COLOR{$k} = $COLOR{"B$k"} = '';
74 }
75 }
76  
77 $COLOR{head} = $self->{OPTS}->{colors}
78 78 ▶ ? color($self->{OPTS}->{term}->{head}) : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'OPTS'}{'colors'} ? : 4 74 78
79 78 ▶ $COLOR{reset} = $self->{OPTS}->{colors} ? color('reset') : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'OPTS'}{'colors'} ? : 4 74 78
80  
81 # resolve paths
82 78 for (@{$self->{OPTS}->{grep}}, @{$self->{OPTS}->{ignore}}) {
83 7 my $tmp = eval { str2path($_) };
84 7 ▶ die_fatal "Failed to parse '$_'", 4 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 6 7
85 6 $_ = $tmp;
86 }
87  
88 77 $self->{OPTS}->{ofmt} = lc($self->{OPTS}->{ofmt});
89  
90 # Use full diff (JSON Merge Patch does not provide arrays diffs)
91 5 map { $self->{OPTS}->{diff}->{$_} = 1 } keys %{$self->{OPTS}->{diff}},
92 77 ▶ if ($self->{OPTS}->{ofmt} eq 'jsonmergepatch');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"OPTS"}{"ofmt"} eq "jsonmergepatch" 1 76 77
93  
94 77 return $self;
95 }
96  
97 81 ▶sub defaults {
Subroutine defaults: called CC=1 SCAR=0.0
Pod undocumented
98 81 my $self = shift;
99  
100 my $out = {
101 81 %{$self->SUPER::defaults()},
102 'ctx-text' => 3,
103 'diff' => {
104 'A' => 1,
105 'N' => 1,
106 'O' => 1,
107 'R' => 1,
108 'U' => 0,
109 },
110 'ofmt' => 'term',
111 'term' => {
112 'head' => 'yellow',
113 'indt' => ' ',
114 'line' => {
115 'A' => 'green',
116 'D' => 'yellow',
117 'N' => 'green',
118 'O' => 'red',
119 'U' => 'white',
120 'R' => 'red',
121 '@' => 'magenta',
122 },
123 'sign' => {
124 'A' => '+ ',
125 'D' => '! ',
126 'N' => '+ ',
127 'O' => '- ',
128 'U' => ' ',
129 'R' => '- ',
130 '@' => ' ',
131 },
132 },
133 };
134  
135 81 return $out;
136 }
137  
138 70 ▶sub diff {
Subroutine diff: called CC=3 SCAR=11.0
Pod undocumented
139 70 my ($self, $old, $new) = @_;
140  
141 0 ▶ log_debug { "Calculating diff for structure" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
142 my $diff = Struct::Diff::diff(
143 $old, $new,
144 72 map { ("no$_" => 1) } grep { !$self->{OPTS}->{diff}->{$_} }
145 70 keys %{$self->{OPTS}->{diff}},
146 );
147  
148 # retrieve result from wrapper (see load() for more info)
149 70 ▶ if (exists $diff->{D}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $diff->{'D'}) { } 65 5 70
elsif (exists $diff->{'U'}) { } 1 4 5
150 65 $diff = $diff->{D}->[0];
151 } elsif (exists $diff->{U}) {
152 1 $diff->{U} = $diff->{U}->[0];
153 }
154  
155 70 return $diff;
156 }
157  
158 55 ▶sub diff_term {
Subroutine diff_term: called CC=23 SCAR=31.4
Pod undocumented
159 55 my ($self, $diff) = @_;
160  
161 0 ▶ log_debug { "Calculating diffs for text values" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
162  
163 55 my $dref; # ref to diff
164 55 my @list = Struct::Diff::list_diff($diff);
165  
166 55 ▶ while (@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 100 55 155
167 100 (undef, $dref) = splice @list, 0, 2;
168  
169 100 ▶ next unless (exists ${$dref}->{N});
Branch: 1 branch
BranchTrueFalseTotal
unless exists ${$dref;}->{"N"} 34 66 100
170 66 ▶ next unless (defined ${$dref}->{O} and defined ${$dref}->{N});
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$dref;}->{"O"} and defined ${$dref;}->{"N"} 5 61 66
Condition100%3/3
defined ${$dref;}->{"O"} and defined ${$dref;}->{"N"}
!ll&&!rl&&r
4161
MC/DC100%2/2
defined ${$dref;}->{"O"} and defined ${$dref;}->{"N"}
defined ${$dref;}->{"O"} defined ${$dref;}->{"N"}
Truth table100%3/3
A and B
A= defined ${$dref;}->{"O"}B= defined ${$dref;}->{"N"}
ABresult
0X0
100
111
171 61 ▶ next if (ref ${$dref}->{O} or ref ${$dref}->{N});
Branch: 1 branch
BranchTrueFalseTotal
if ref ${$dref;}->{"O"} or ref ${$dref;}->{"N"} 8 53 61
Condition100%3/3
ref ${$dref;}->{"O"} or ref ${$dref;}->{"N"}
l!l&&r!l&&!r
7153
MC/DC100%2/2
ref ${$dref;}->{"O"} or ref ${$dref;}->{"N"}
ref ${$dref;}->{"O"} ref ${$dref;}->{"N"}
Truth table100%3/3
A or B
A= ref ${$dref;}->{"O"}B= ref ${$dref;}->{"N"}
ABresult
1X1
011
000
172 53 ▶ next if (is_number(${$dref}->{O}) or is_number(${$dref}->{N}));
Branch: 1 branch
BranchTrueFalseTotal
if &is_number(${$dref;}->{"O"}) or &is_number(${$dref;}->{"N"}) 13 40 53
Condition100%3/3
&is_number(${$dref;}->{"O"}) or &is_number(${$dref;}->{"N"})
l!l&&r!l&&!r
11240
MC/DC100%2/2
&is_number(${$dref;}->{"O"}) or &is_number(${$dref;}->{"N"})
&is_number(${$dref;}->{"O"}) &is_number(${$dref;}->{"N"})
Truth table100%3/3
A or B
A= &is_number(${$dref;}->{"O"})B= &is_number(${$dref;}->{"N"})
ABresult
1X1
011
000
173  
174 40 my @old = split($/, ${$dref}->{O}, -1);
175 40 my @new = split($/, ${$dref}->{N}, -1);
176  
177 40 ▶ if (@old > 1 or @new > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@old > 1 or @new > 1) 25 15 40
Condition100%3/3
@old > 1 or @new > 1
l!l&&r!l&&!r
24115
MC/DC100%2/2
@old > 1 or @new > 1
@old > 1 @new > 1
Truth table100%3/3
A or B
A= @old > 1B= @new > 1
ABresult
1X1
011
000
178 25 delete ${$dref}->{O};
179 25 delete ${$dref}->{N};
180  
181 25 ▶ if ($old[-1] eq '' and $new[-1] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($old[-1] eq "" and $new[-1] eq "") 14 11 25
Condition100%3/3
$old[-1] eq "" and $new[-1] eq ""
!ll&&!rl&&r
10114
MC/DC100%2/2
$old[-1] eq "" and $new[-1] eq ""
$old[-1] eq "" $new[-1] eq ""
Truth table100%3/3
A and B
A= $old[-1] eq ""B= $new[-1] eq ""
ABresult
0X0
100
111
182 14 pop @old; # because split by newline and -1 for LIMIT
183 14 pop @new; # -"-
184 }
185  
186 25 my @cdiff = compact_diff(\@old, \@new);
187 25 my ($match, $header);
188  
189 25 ▶ while (@cdiff > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cdiff > 2) 110 25 135
190 110 my @del = @old[$cdiff[0] .. $cdiff[2] - 1];
191 110 my @add = @new[$cdiff[1] .. $cdiff[3] - 1];
192  
193 110 ▶ if ($match = !$match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match = !$match) { } 59 51 110
194 # trailing context
195 59 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 34 25 59
196 34 my @tail = splice @del, 0, $self->{OPTS}->{'ctx-text'};
197 34 push @{${$dref}->{T}}, 'U', \@tail;
198  
199 34 $header->[1] += @tail;
200 34 $header->[3] += @tail;
201 }
202  
203 # leading context
204 59 ▶ if (@cdiff > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cdiff > 4) 51 8 59
205 my @rest = splice @del, 0, $self->{OPTS}->{'ctx-text'}
206 51 ▶ ? $self->{OPTS}->{'ctx-text'} * -1 : scalar @del;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'OPTS'}{'ctx-text'} ? : 41 10 51
207  
208 51 ▶ if (@rest or !$header) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest or not $header) 32 19 51
Condition100%3/3
@rest or not $header
l!l&&r!l&&!r
161619
MC/DC100%2/2
@rest or not $header
@rest not $header
Truth table100%3/3
A or not(B)
A= @restB= not $header
ABresult
1X1
011
000
209 32 push @{${$dref}->{T}}, '@', $header = [
210 $cdiff[2] - @del + 1, 0,
211 $cdiff[3] - @del + 1, 0,
212 ];
213 }
214  
215 51 ▶ if (@del) {
Branch: 1 branch
BranchTrueFalseTotal
if (@del) 18 33 51
216 18 push @{${$dref}->{T}}, 'U', \@del;
217 18 $header->[1] += @del;
218 18 $header->[3] += @del;
219 }
220 }
221 } else {
222 51 ▶ if (@del) {
Branch: 1 branch
BranchTrueFalseTotal
if (@del) 43 8 51
223 43 push @{${$dref}->{T}}, 'R', \@del;
224 43 $header->[1] += @del;
225 }
226  
227 51 ▶ if (@add) {
Branch: 1 branch
BranchTrueFalseTotal
if (@add) 42 9 51
228 42 push @{${$dref}->{T}}, 'A', \@add;
229 42 $header->[3] += @add;
230 }
231 }
232  
233 110 splice @cdiff, 0, 2;
234 }
235 }
236 }
237  
238 55 return $self;
239 }
240  
241 71 ▶sub dump {
Subroutine dump: called CC=2 SCAR=7.0
Pod undocumented
242 71 my ($self, $diff) = @_;
243  
244 0 ▶ log_debug { "Dumping results" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
245  
246 71 my %formats = (
247 brief => \&dump_brief,
248 jsonmergepatch => \&dump_json_merge_patch,
249 jsonpatch => \&dump_json_patch,
250 rules => \&dump_rules,
251 term => \&dump_term,
252 );
253  
254 71 ▶ if (my $dump = $formats{$self->{OPTS}->{ofmt}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dump = $formats{$self->{'OPTS'}{'ofmt'}}) { } 61 10 71
255 61 $dump->($self, $diff);
256 } else {
257 s_dump(\*STDOUT, $self->{OPTS}->{ofmt},
258 10 {pretty => $self->{OPTS}->{pretty}}, $diff);
259 }
260  
261 71 return $self;
262 }
263  
264 4 ▶sub dump_brief {
Subroutine dump_brief: called CC=4 SCAR=13.9
Pod undocumented
265 4 my ($self, $diff) = @_;
266  
267 4 my ($path, $dref, $tag);
268 4 my @list = Struct::Diff::list_diff($diff, sort => 1);
269  
270 4 ▶ while (@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 10 4 14
271 10 ($path, $dref) = splice @list, 0, 2;
272 10 for $tag (qw{R N A}) {
273 $self->print_brief_block($path, $tag)
274 30 ▶ if (exists ${$dref}->{$tag});
Branch: 1 branch
BranchTrueFalseTotal
if exists ${$dref;}->{$tag} 10 20 30
275 }
276 }
277 }
278  
279 1 ▶sub dump_json_merge_patch {
Subroutine dump_json_merge_patch: called CC=1 SCAR=0.0
Pod undocumented
280 1 my ($self, $diff) = @_;
281  
282 s_dump(
283 \*STDOUT, 'JSON',
284 {pretty => $self->{OPTS}->{pretty}},
285 1 Struct::Diff::MergePatch::diff($diff)
286 );
287 }
288  
289 1 ▶sub dump_json_patch {
Subroutine dump_json_patch: called CC=1 SCAR=0.0
Pod undocumented
290 1 my ($self, $diff) = @_;
291  
292 s_dump(
293 \*STDOUT, 'JSON',
294 {pretty => $self->{OPTS}->{pretty}},
295 1 JSON::Patch::diff($diff)
296 );
297 }
298  
299  
300 2 ▶sub dump_rules {
Subroutine dump_rules: called CC=5 SCAR=16.1
Pod undocumented
301 2 my ($self, $diff) = @_;
302  
303 2 my ($path, $dref, $item, @out);
304 2 my @list = Struct::Diff::list_diff($diff, sort => 1);
305  
306 2 ▶ while (@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 8 2 10
307 8 ($path, $dref) = splice @list, 0, 2;
308 8 for (qw{R N A}) {
309 24 ▶ next unless (exists ${$dref}->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists ${$dref;}->{$_} 16 8 24
310 unshift @out, {
311 modname => $_ eq "R" ? "Remove" : "Insert",
312 path => $self->dump_rules_path($path),
313 8 ▶ value => ${$dref}->{$_}
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'R' ? : 2 6 8
314 };
315 }
316 }
317  
318 2 s_dump(\*STDOUT, 'JSON', {pretty => $self->{OPTS}->{pretty}}, \@out);
319 }
320  
321 8 ▶sub dump_rules_path { # to be able to override
Subroutine dump_rules_path: called CC=1 SCAR=0.0
Pod undocumented
322 8 return path2str($_[1]);
323 }
324  
325 53 ▶sub dump_term {
Subroutine dump_term: called CC=4 SCAR=13.9
Pod undocumented
326 53 my ($self, $diff) = @_;
327  
328 53 my ($path, $dref, $tag);
329 53 my @list = Struct::Diff::list_diff($diff, sort => 1);
330  
331 53 ▶ while (@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) 98 53 151
332 98 ($path, $dref) = splice @list, 0, 2;
333 98 for $tag (qw{R O N A T U}) {
334 134 $self->print_term_block(${$dref}->{$tag}, $path, $tag)
335 588 ▶ if (exists ${$dref}->{$tag});
Branch: 1 branch
BranchTrueFalseTotal
if exists ${$dref;}->{$tag} 134 454 588
336 }
337 }
338 }
339  
340 72 ▶sub exec {
Subroutine exec: called CC=15 SCAR=27.1
Pod undocumented
341 72 my $self = shift;
342 72 my (@diffs, @files);
343  
344 72 for (@{$self->{ARGV}}) {
345 139 push @files, { data => $self->load($_), name => $_ };
346  
347 139 ▶ if ($self->{OPTS}->{show}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'OPTS'}{'show'}) { } 5 134 139
348 5 ▶ if (ref $files[0]->{data}->[0] eq 'ARRAY') { # ndproc's blame
Branch: 1 branch
BranchTrueFalseTotal
if (ref $files[0]{'data'}[0] eq 'ARRAY') { } 1 4 5
349 1 for (@{$files[0]->{data}->[0]}) {
350 push @diffs, $_->{diff},
351 2 [ $files[0]->{name} . ', rule #' . $_->{rule_id} ];
352 }
353 } else { # regular diff dump
354 4 push @diffs, $files[0]->{data}->[0], [ $files[0]->{name} ];
355 }
356 } else { # one of the files to diff
357 134 ▶ next unless (@files > 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @files > 1 67 67 134
358 67 push @diffs, $self->diff($files[0]->{data}, $files[1]->{data});
359 67 push @diffs, [ $files[0]->{name}, $files[1]->{name} ];
360 }
361  
362 72 shift @files;
363  
364 72 ▶ while (@diffs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@diffs) 73 71 144
365 73 my ($diff, $hdrs) = splice @diffs, 0, 2;
366  
367 73 $self->print_term_header(@{$hdrs});
368  
369 73 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTS"}{"show"} and my(@errs) = &Struct::Diff::valid_diff($diff)) 1 72 73
Condition100%3/3
$self->{"OPTS"}{"show"} and my(@errs) = &Struct::Diff::valid_diff($diff)
!ll&&!rl&&r
6751
MC/DC100%2/2
$self->{"OPTS"}{"show"} and my(@errs) = &Struct::Diff::valid_diff($diff)
$self->{"OPTS"}{"show"} my(@errs) = &Struct::Diff::valid_diff($diff)
Truth table100%3/3
A and B
A= $self->{"OPTS"}{"show"}B= my(@errs) = &Struct::Diff::valid_diff($diff)
ABresult
0X0
100
111
370 $self->{OPTS}->{show} and
371 my @errs = Struct::Diff::valid_diff($diff)
372 ) {
373 1 ▶ while (@errs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errs) 2 1 3
374 2 my ($path, $type) = splice @errs, 0, 2;
375 2 ▶ log_error { "$type " . path2str($path) };
Subroutine __ANON__: called CC=1 SCAR=0.0
376 }
377  
378 1 die_fatal "Diff validation failed", 1;
379 }
380  
381 72 ▶ unless ($self->{OPTS}->{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"OPTS"}{"quiet"}) 71 1 72
382 71 ▶ $self->diff_term($diff) if ($self->{OPTS}->{ofmt} eq 'term');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"OPTS"}{"ofmt"} eq "term" 53 18 71
383 71 $self->dump($diff);
384 }
385  
386 $self->{status} = 8
387 72 ▶ unless (not keys %{$diff} or exists $diff->{U});
Branch: 1 branch
BranchTrueFalseTotal
unless not keys %{$diff;} or exists $diff->{"U"} 68 4 72
Condition100%3/3
not keys %{$diff;} or exists $diff->{"U"}
l!l&&r!l&&!r
3168
MC/DC100%2/2
not keys %{$diff;} or exists $diff->{"U"}
not keys %{$diff;} exists $diff->{"U"}
Truth table100%3/3
not(A) or B
A= not keys %{$diff;}B= exists $diff->{"U"}
ABresult
1X1
011
000
388 }
389 }
390  
391 71 ▶ die_info "All done, no difference found", 0 unless ($self->{status});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"status"} 4 67 71
392 67 die_info "Difference found", 8;
393 }
394  
395 145 ▶sub load {
Subroutine load: called CC=4 SCAR=13.9
Pod undocumented
396 145 my $self = shift;
397  
398 145 my @data = $self->load_struct($_[0], $self->{OPTS}->{ifmt});
399  
400 # array used to indicate absent value for grep result
401 @data = $self->grep($self->{OPTS}->{grep}, $data[0])
402 145 ▶ if (@{$self->{OPTS}->{grep}});
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"OPTS"}{"grep"};} 6 139 145
403  
404 145 ▶ if (@data and ref $data[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data and ref $data[0]) 128 17 145
Condition100%3/3
@data and ref $data[0]
!ll&&!rl&&r
116128
MC/DC100%2/2
@data and ref $data[0]
@data ref $data[0]
Truth table100%3/3
A and B
A= @dataB= ref $data[0]
ABresult
0X0
100
111
405 128 map { path($data[0], $_, delete => 1) } @{$self->{OPTS}->{ignore}}
406 }
407  
408 145 return \@data;
409 }
410  
411 10 ▶sub print_brief_block {
Subroutine print_brief_block: called CC=2 SCAR=6.9
Pod undocumented
412 10 my ($self, $path, $status) = @_;
413  
414 10 ▶ $status = 'D' if ($status eq 'N');
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "N" 6 4 10
415  
416 print $self->{OPTS}->{term}->{sign}->{$status} . $COLOR{U} .
417 10 path2str([splice @{$path}, 0, -1]) . $COLOR{reset} .
418 10 $COLOR{"B$status"} . path2str($path) . $COLOR{reset} . "\n";
419 }
420  
421 134 ▶sub print_term_block {
Subroutine print_term_block: called CC=8 SCAR=20.8
Pod undocumented
422 134 my ($self, $value, $path, $status) = @_;
423  
424 0 ▶ log_trace { "'" . path2str($path) . "' ($status)" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
425  
426 134 my @lines;
427 134 my $dsign = $self->{OPTS}->{term}->{sign}->{$status};
428 134 my $indent = $self->{OPTS}->{term}->{indt};
429  
430 # diff for path
431 134 ▶ if (@{$path} and my @delta = path_delta($self->{'hdr_path'}, $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$path;} and my(@delta) = &path_delta($self->{"hdr_path"}, $path)) 91 43 134
Condition100%3/3
@{$path;} and my(@delta) = &path_delta($self->{"hdr_path"}, $path)
!ll&&!rl&&r
53891
MC/DC100%2/2
@{$path;} and my(@delta) = &path_delta($self->{"hdr_path"}, $path)
@{$path;} my(@delta) = &path_delta($self->{"hdr_path"}, $path)
Truth table100%3/3
A and B
A= @{$path;}B= my(@delta) = &path_delta($self->{"hdr_path"}, $path)
ABresult
0X0
100
111
432 91 $self->{'hdr_path'} = [@{$path}];
433 91 ▶ my $s = $self->{OPTS}->{pretty} ? @{$path} - @delta : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'OPTS'}{'pretty'} ? : 86 5 91
434  
435 91 ▶ while ($s < @{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s < @{$path;}) 136 91 227
436 136 my $line = $indent x $s . path2str([$path->[$s]]);
437  
438 136 ▶ if (($status eq 'A' or $status eq 'R') and $s == $#{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq 'A' || $status eq 'R' and $s == $#{$path;}) { } 22 114 136
Condition100%6/6
$status eq "A" || $status eq "R" and $s == $#{$path;}
!ll&&!rl&&r
922222
$status eq "A" or $status eq "R"
l!l&&r!l&&!r
321292
MC/DC100%3/3
$status eq "A" || $status eq "R" and $s == $#{$path;}
$status eq "A" $status eq "R" $s == $#{$path;}
Truth table100%5/5
A or B and C
A= $status eq "A"B= $status eq "R"C= $s == $#{$path;}
ABCresult
00X0
1X00
0100
1X11
0111
439 22 $line = $COLOR{"B$status"} . $dsign . $line . $COLOR{reset};
440 } else {
441 114 substr($line, 0, 0, $indent);
442 }
443  
444 136 push @lines, $line;
445 136 $s++;
446 }
447 }
448  
449 # diff for value
450 134 push @lines, $self->term_value_diff($value, $status, $indent x @{$path});
451  
452 134 print join("\n", @lines) . "\n";
453 }
454  
455 78 ▶sub print_term_header {
Subroutine print_term_header: called CC=4 SCAR=13.9
Pod undocumented
456 78 my ($self, @names) = @_;
457  
458 78 ▶ if ($self->{TTY} and not $self->{OPTS}->{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"TTY"} and not $self->{"OPTS"}{"quiet"}) 3 75 78
Condition100%3/3
$self->{"TTY"} and not $self->{"OPTS"}{"quiet"}
!ll&&!rl&&r
7413
MC/DC100%2/2
$self->{"TTY"} and not $self->{"OPTS"}{"quiet"}
$self->{"TTY"} not $self->{"OPTS"}{"quiet"}
Truth table100%3/3
A and not(B)
A= $self->{"TTY"}B= not $self->{"OPTS"}{"quiet"}
ABresult
0X0
100
111
459 print $COLOR{head} .
460 (@names == 1 ? "!!! $names[0]" : "--- $names[0]\n+++ $names[1]") .
461 3 ▶ $COLOR{reset}. "\n";
Branch: 1 branch
BranchTrueFalseTotal
@names == 1 ? : 1 2 3
462 }
463 }
464  
465 134 ▶sub term_value_diff {
Subroutine term_value_diff: called CC=2 SCAR=6.9
Pod undocumented
466 134 my ($self, $value, $status, $indent) = @_;
467  
468 134 ▶ return $self->term_value_diff_text($value, $indent)
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "T" 23 111 134
469 if ($status eq 'T');
470  
471 111 return $self->term_value_diff_default($value, $status, $indent);
472 }
473  
474 111 ▶sub term_value_diff_default {
Subroutine term_value_diff_default: called CC=3 SCAR=11.0
Pod undocumented
475 111 my ($self, $value, $status, $indent) = @_;
476 111 my @out;
477  
478 111 ▶ $value = $JSON->pretty($self->{OPTS}->{pretty})->encode($value)
Branch: 1 branch
BranchTrueFalseTotal
unless &is_number($value) 82 29 111
479 unless (is_number($value));
480  
481 111 for my $line (split($/, $value)) {
482 130 substr($line, 0, 0, $self->{OPTS}->{term}->{sign}->{$status} . $indent);
483 130 push @out, $COLOR{$status} . $line . $COLOR{reset};
484 }
485  
486 111 return @out;
487 }
488  
489 23 ▶sub term_value_diff_text {
Subroutine term_value_diff_text: called CC=5 SCAR=16.1
Pod undocumented
490 23 my ($self, $diff, $indent) = @_;
491 23 my (@hdr, $lines, @out, $pfx, $sfx, $status);
492  
493 23 $sfx = $COLOR{reset};
494  
495 23 ▶ while (@{$diff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$diff;}) 163 23 186
496 163 ($status, $lines) = splice @{$diff}, 0, 2;
497  
498 163 $pfx = $COLOR{$status} . $self->{OPTS}->{term}->{sign}->{$status} .
499 $indent;
500  
501 163 ▶ if ($status eq '@') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "\@") 30 133 163
502 30 @hdr = splice @{$lines};
503 30 ▶ $lines->[0] = "@@ -$hdr[0]" . ($hdr[1] > 1 ? ",$hdr[1] " : "") .
Branch: 2 branches
BranchTrueFalseTotal
$hdr[1] > 1 ? : 21 9 30
$hdr[3] > 1 ? : 20 10 30
504 " +$hdr[2]" . ($hdr[3] > 1 ? ",$hdr[3] @@" : " @@");
505 }
506  
507 163 map { substr($_ , 0, 0, $pfx); $_ .= $sfx; push @out, $_ } @{$lines};
508 }
509  
510 23 return @out;
511 }
512  
513 3 1; # End of App::NDTools::NDDiff
« blib/lib/App/NDTools/Test.pm ↑ Summary blib/lib/App/NDTools/NDProc.pm »