PICA/Patch.pm

filter: Statementstmt 95.6% 87 / 91 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.5% 155 / 175 CC 30 SCAR 24.1
CC
30
Cov
91%
pica_patch
18.0
pica_diff
10.0
sorted_fields
4.0
CRAP 11.1
« PICA/Writer/Base.pm ↑ Summary PICA/Parser/Plain.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.12';
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 30 ▶sub cmp_fields {
Subroutine cmp_fields: called CC=2 SCAR=6.9
Pod undocumented
16 30 my @a = @{$_[0]};
17 30 my @b = @{$_[1]};
18 30 ▶ pop @b if @b % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @b % 2 11 19 30
19 30 return join("\t", @a) cmp join("\t", @b);
20 }
21  
22 41 ▶sub sorted_fields {
Subroutine sorted_fields: called CC=4 SCAR=13.9
Pod undocumented
23 41 my $fields = PICA::Data::pica_fields(PICA::Data::pica_sort(shift));
24  
25 41 ▶ if (@$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fields) 37 4 41
26 37 my $level = substr $fields->[0][0], 0, 1;
27 37 for (@$fields) {
28 52 ▶ die "diff/patch only allowed on atomic records\n"
Branch: 1 branch
BranchTrueFalseTotal
if $level ne substr($_->[0], 0, 1) 1 51 52
29 if $level ne substr $_->[0], 0, 1;
30 }
31 }
32  
33 40 $fields = [sort {cmp_fields($a, $b)} @$fields];
34  
35 40 return $fields;
36 }
37  
38 16 *annotation = *PICA::Data::pica_annotation;
39  
40 8 ▶sub pica_diff {
Subroutine pica_diff: called CC=10 SCAR=23.1
Pod documented
41 8 my $a = sorted_fields(shift);
42 8 my $b = sorted_fields(shift);
43 8 my %options = @_;
44  
45 8 my (@diff, $i, $j);
46  
47 9 ▶ my $changed = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 9 my $field = PICA::Data::Field->new(@{$_[0]});
49 9 $field->annotation($_[1]);
50 9 push @diff, $field;
51 8 };
52  
53 8 ▶ while ($i < @$a && $j < @$b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$a and $j < @$b) 7 8 15
Condition100%3/3
$i < @$a and $j < @$b
!ll&&!rl&&r
447
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
54 7 my $cmp = cmp_fields($a->[$i], $b->[$j]);
55  
56 7 ▶ if ($cmp < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp < 0) { } 1 6 7
elsif ($cmp > 0) { } 0 6 6
57 1 $changed->($a->[$i++], '-');
58 }
59 elsif ($cmp > 0) {
60 0 $changed->($b->[$j++], '+');
61 }
62 else {
63 6 ▶ push @diff, $a->[$i] if $options{keep};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"keep"} 0 6 6
64 6 $i++;
65 6 $j++;
66 }
67 }
68 8 ▶ while ($i < @$a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$a) 5 8 13
69 5 $changed->($a->[$i++], '-');
70 }
71 8 ▶ while ($j < @$b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$b) 3 8 11
72 3 $changed->($b->[$j++], '+');
73 }
74  
75 # remove identical fields (could also be done in sort_fields)
76 8 ▶ for (my $i = 0; $i < $#diff;) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#diff) 2 8 10
77 2 ▶ if ($diff[$i]->equal($diff[$i + 1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff[$i]->equal($diff[$i + 1])) { } 1 1 2
78 1 splice @diff, $i + 1, 1;
79 }
80 else {
81 1 $i++;
82 }
83 }
84  
85 8 bless {record => \@diff}, 'PICA::Data';
86 }
87  
88 1 ▶sub no_match {
Subroutine no_match: called CC=1 SCAR=0.0
Pod undocumented
89 1 my $field = shift;
90 1 annotation($field, undef);
91 1 die "records don't match, expected: " . PICA::Data::pica_string([$field]);
92 }
93  
94 13 ▶sub pica_patch {
Subroutine pica_patch: called CC=17 SCAR=28.9
Pod documented
95 13 my $fields = sorted_fields(shift);
96 12 my $diff = sorted_fields(shift);
97  
98 12 for (map {annotation($_)} @$diff) {
99 14 ▶ die "invalid PICA Patch annotation: $_\n" if $_ !~ /^[ +-]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /^[ +-]$/u 0 14 14
100 }
101  
102 12 my ($i, $j) = (0, 0);
103 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
104 11 my $cur;
105 11 my $next = field_identifier($diff->[$j]);
106 11 my $ann = annotation($diff->[$j]);
107  
108 # while current field is behind or same
109 11 ▶ while (($cur = field_identifier($fields->[$i])) le $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (($cur = field_identifier($fields->[$i])) le $next) 13 1 14
110 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
111 7 ▶ if ($ann eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ann eq '-') { } 4 3 7
112 4 splice @$fields, $i, 1;
113 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
114  
115 }
116 else {
117 3 $i++;
118 3 $j++;
119 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
120 }
121  
122 0 $next = field_identifier($diff->[$j]);
123 0 $ann = annotation($diff->[$j]);
124  
125 }
126 else {
127  
128 # keep current field
129 6 ▶ last PATCH if ++$i == @$fields;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i == @$fields 3 3 6
130 }
131 }
132  
133 # current field is ahead
134 1 ▶ if ($ann eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ann eq '+') { } 1 0 1
135 1 my $add = $diff->[$j++];
136 1 annotation($add, undef);
137 1 splice @$fields, $i++, 0, $add;
138 }
139 else {
140 0 no_match($diff->[$j]);
141 }
142 }
143  
144 12 ▶ while ($j < @$diff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$diff) 6 11 17
145 6 ▶ if (annotation($diff->[$j]) eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (annotation($diff->[$j]) eq '+') { } 5 1 6
146 5 $fields->[$i] = $diff->[$j++];
147 5 annotation($fields->[$i++], undef);
148 }
149 else {
150 1 no_match($diff->[$j]);
151 }
152 }
153  
154 11 bless {record => $fields}, 'PICA::Data';
155 }
156 16 1;
157 __END__
« PICA/Writer/Base.pm ↑ Summary PICA/Parser/Plain.pm »