blib/lib/PICA/Patch.pm

filter: Statementstmt 95.5% 85 / 89 Branchbran 86.3% 38 / 44 Conditioncond 80.0% 12 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 10 / 10 Podpod 40.0% 2 / 5 totaltotal 88.4% 153 / 173 CC 30 SCAR 24.1
CC
30
Cov
91%
pica_patch
18.0
pica_diff
10.0
sorted_fields
4.0
CRAP 11.1
« blib/lib/PICA/Schema/Builder.pm ↑ Summary blib/lib/PICA/Writer/Base.pm »
1 package PICA::Patch;
2 16 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 16 our $VERSION = '2.09';
5  
6 16 ▶use PICA::Schema qw(field_identifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use PICA::Data::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 16 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 our @EXPORT_OK = qw(pica_diff pica_patch);
11 16 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
12  
13 # Compare full fields, ignoring annotation of the latter
14 # note this does not strip occurrence from level 2 records!
15 sub cmp_fields {
16 15 ▶ my @a = @{$_[0]};
Subroutine cmp_fields: called CC=2 SCAR=6.9
Pod undocumented
17 15 my @b = @{$_[1]};
18 15 ▶ pop @b if @b % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @b % 2 9 6 15
19 15 return join("\t", @a) cmp join("\t", @b);
20 }
21  
22 sub sorted_fields {
23 39 ▶ my $fields = PICA::Data::pica_fields(PICA::Data::pica_sort(shift));
Subroutine sorted_fields: called CC=4 SCAR=13.9
Pod undocumented
24  
25 39 ▶ if (@$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fields) 35 4 39
26 35 my $level = substr $fields->[0][0], 0, 1;
27 35 for (@$fields) {
28 49 ▶ die "diff/patch only allowed on atomic records\n"
Branch: 1 branch
BranchTrueFalseTotal
if $level ne substr($_->[0], 0, 1) 1 48 49
29 if $level ne substr $_->[0], 0, 1;
30 }
31 }
32  
33 38 return $fields;
34 }
35  
36 16 *annotation = *PICA::Data::pica_annotation;
37  
38 sub pica_diff {
39 7 ▶ my $a = sorted_fields(shift);
Subroutine pica_diff: called CC=10 SCAR=23.1
Pod documented
40 7 my $b = sorted_fields(shift);
41 7 my %options = @_;
42  
43 7 my (@diff, $i, $j);
44  
45 my $changed = sub {
46 8 ▶ my $field = PICA::Data::Field->new(@{$_[0]});
Subroutine __ANON__: called CC=1 SCAR=0.0
47 8 $field->annotation($_[1]);
48 8 push @diff, $field;
49 7 };
50  
51 7 ▶ while ($i < @$a && $j < @$b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$a and $j < @$b) 6 7 13
Condition100%3/3
$i < @$a and $j < @$b
!ll&&!rl&&r
436
MC/DC100%2/2
$i < @$a and $j < @$b
$i < @$a $j < @$b
Truth table100%3/3
A and B
A= $i < @$aB= $j < @$b
ABresult
0X0
100
111
52 6 my $cmp = cmp_fields($a->[$i], $b->[$j]);
53  
54 6 ▶ if ($cmp < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp < 0) { } 1 5 6
elsif ($cmp > 0) { } 0 5 5
55 1 $changed->($a->[$i++], '-');
56 }
57 elsif ($cmp > 0) {
58 0 $changed->($b->[$j++], '+');
59 }
60 else {
61 5 ▶ push @diff, $a->[$i] if $options{keep};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"keep"} 0 5 5
62 5 $i++;
63 5 $j++;
64 }
65 }
66 7 ▶ while ($i < @$a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$a) 4 7 11
67 4 $changed->($a->[$i++], '-');
68 }
69 7 ▶ while ($j < @$b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$b) 3 7 10
70 3 $changed->($b->[$j++], '+');
71 }
72  
73 # remove identical fields (could also be done in sort_fields)
74 7 ▶ for (my $i = 0; $i < $#diff;) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#diff) 2 7 9
75 2 ▶ if ($diff[$i]->equal($diff[$i + 1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff[$i]->equal($diff[$i + 1])) { } 1 1 2
76 1 splice @diff, $i + 1, 1;
77 }
78 else {
79 1 $i++;
80 }
81 }
82  
83 7 bless {record => \@diff}, 'PICA::Data';
84 }
85  
86 sub no_match {
87 1 ▶ my $field = shift;
Subroutine no_match: called CC=1 SCAR=0.0
Pod undocumented
88 1 annotation($field, undef);
89 1 die "records don't match, expected: " . PICA::Data::pica_string([$field]);
90 }
91  
92 sub pica_patch {
93 13 ▶ my $fields = sorted_fields(shift);
Subroutine pica_patch: called CC=17 SCAR=28.9
Pod documented
94 12 my $diff = sorted_fields(shift);
95  
96 12 for (map {annotation($_)} @$diff) {
97 14 ▶ die "invalid PICA Patch annotation: $_\n" if $_ !~ /^[ +-]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /^[ +-]$/u 0 14 14
98 }
99  
100 12 my ($i, $j) = (0, 0);
101 12 ▶PATCH: while ($i < @$fields && $j < @$diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$fields and $j < @$diff) 11 2 13
Condition100%3/3
$i < @$fields and $j < @$diff
!ll&&!rl&&r
1111
MC/DC100%2/2
$i < @$fields and $j < @$diff
$i < @$fields $j < @$diff
Truth table100%3/3
A and B
A= $i < @$fieldsB= $j < @$diff
ABresult
0X0
100
111
102 11 my $cur;
103 11 my $next = field_identifier($diff->[$j]);
104 11 my $ann = annotation($diff->[$j]);
105  
106 # while current field is behind or same
107 11 ▶ while (($cur = field_identifier($fields->[$i])) le $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (($cur = field_identifier($fields->[$i])) le $next) 13 1 14
108 13 ▶ if ($cur eq $next && !cmp_fields($fields->[$i], $diff->[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur eq $next and not cmp_fields($fields->[$i], $diff->[$j])) { } 7 6 13
Condition100%3/3
$cur eq $next and not cmp_fields($fields->[$i], $diff->[$j])
!ll&&!rl&&r
427
MC/DC100%2/2
$cur eq $next and not cmp_fields($fields->[$i], $diff->[$j])
$cur eq $next not cmp_fields($fields->[$i], $diff->[$j])
Truth table100%3/3
A and not(B)
A= $cur eq $nextB= not cmp_fields($fields->[$i], $diff->[$j])
ABresult
0X0
100
111
109 7 ▶ if ($ann eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ann eq '-') { } 4 3 7
110 4 splice @$fields, $i, 1;
111 4 ▶ last PATCH if $j++ == @$diff or $i == @$fields;
Branch: 1 branch
BranchTrueFalseTotal
if $j++ == @$diff or $i == @$fields 4 0 4
Condition33%1/3
$j++ == @$diff or $i == @$fields
l!l&&r!l&&!r
040
MC/DC0%0/2
$j++ == @$diff or $i == @$fields
$j++ == @$diff $i == @$fields
Truth table33%1/3
A or B
A= $j++ == @$diffB= $i == @$fields
ABresult
1X1
011
000
112  
113 }
114 else {
115 3 $i++;
116 3 $j++;
117 3 ▶ last PATCH if $i >= @$fields or $j >= @$diff;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @$fields or $j >= @$diff 3 0 3
Condition66%2/3
$i >= @$fields or $j >= @$diff
l!l&&r!l&&!r
210
MC/DC0%0/2
$i >= @$fields or $j >= @$diff
$i >= @$fields $j >= @$diff
Truth table66%2/3
A or B
A= $i >= @$fieldsB= $j >= @$diff
ABresult
1X1
011
000
118 }
119  
120 0 $next = field_identifier($diff->[$j]);
121 0 $ann = annotation($diff->[$j]);
122  
123 }
124 else {
125  
126 # keep current field
127 6 ▶ last PATCH if ++$i == @$fields;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i == @$fields 3 3 6
128 }
129 }
130  
131 # current field is ahead
132 1 ▶ if ($ann eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ann eq '+') { } 1 0 1
133 1 my $add = $diff->[$j++];
134 1 annotation($add, undef);
135 1 splice @$fields, $i++, 0, $add;
136 }
137 else {
138 0 no_match($diff->[$j]);
139 }
140 }
141  
142 12 ▶ while ($j < @$diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$diff) 6 11 17
143 6 ▶ if (annotation($diff->[$j]) eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (annotation($diff->[$j]) eq '+') { } 5 1 6
144 5 $fields->[$i] = $diff->[$j++];
145 5 annotation($fields->[$i++], undef);
146 }
147 else {
148 1 no_match($diff->[$j]);
149 }
150 }
151  
152 11 bless {record => $fields}, 'PICA::Data';
153 }
154 16 1;
155 __END__
« blib/lib/PICA/Schema/Builder.pm ↑ Summary blib/lib/PICA/Writer/Base.pm »