blib/lib/Toolforge/MixNMatch/Diff.pm

filter: Statementstmt 77.9% 67 / 86 Branchbran 50.0% 14 / 28 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 67.3% 93 / 138 CC 23 SCAR 33.8
CC
23
Cov
67%
diff
53.1
_which_catalog_is_new
5.3
_warn
2.1
CRAP 29.5
↑ Summary
1 package Toolforge::MixNMatch::Diff;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Toolforge::MixNMatch::Object::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Toolforge::MixNMatch::Object::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Toolforge::MixNMatch::Object::YearMonth;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = 0.02;
12  
13 4 ▶sub diff {
Subroutine diff: called CC=17 SCAR=39.7
Pod documented
14 4 my ($catalog1, $catalog2, $warn) = @_;
15  
16 4 ▶ $warn //= 0;
Condition50%1/2
$warn //= 0
l!l
04
MC/DC0%0/1
$warn //= 0
$warn
Truth table50%1/2
A
A= $warn
Aresult
11
00
17  
18 4 ▶ if ($catalog1->type ne $catalog2->type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($catalog1->type ne $catalog2->type) 0 4 4
19 0 _warn('Different type of catalogs.', $warn);
20 }
21  
22 4 ▶ if ($catalog1->count != $catalog2->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($catalog1->count != $catalog2->count) 0 4 4
23 0 _warn('Count values of catalogs are different.', $warn);
24 }
25  
26 4 my ($new_cat, $old_cat) = _which_catalog_is_new($catalog1, $catalog2, $warn);
27  
28 # Diff of users.
29 4 my $users_ar = [];
30 4 foreach my $new_cat_user (@{$new_cat->users}) {
31 3 my $old_cat_user;
32 3 foreach my $user_iter (@{$old_cat->users}) {
33 1 ▶ if ($user_iter->username eq $new_cat_user->username) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_iter->username eq $new_cat_user->username) 1 0 1
34  
35 1 ▶ if ($user_iter->uid ne $new_cat_user->uid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user_iter->uid ne $new_cat_user->uid) 0 1 1
36 0 err 'Something wrong with uids in catalogs.';
37 }
38 1 $old_cat_user = $user_iter;
39 1 last;
40 }
41 }
42 3 ▶ my $count = defined $old_cat_user ?
Branch: 1 branch
BranchTrueFalseTotal
defined $old_cat_user ? : 1 2 3
43 ($new_cat_user->count - $old_cat_user->count)
44 : $new_cat_user->count;
45 3 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 3 0 3
46 3 push @{$users_ar}, Toolforge::MixNMatch::Object::User->new(
47 'count' => $count,
48 'uid' => $new_cat_user->uid,
49 'username' => $new_cat_user->username,
50 );
51 }
52 }
53  
54 # Diff of dates.
55 4 my $year_months_ar = [];
56 0 ▶ foreach my $new_cat_year_month (sort { $a->year <=> $b->year || $a->month <=> $b->month }
Condition0%0/3
$a->year <=> $b->year || $a->month <=> $b->month
l!l&&r!l&&!r
000
MC/DC0%0/2
$a->year <=> $b->year || $a->month <=> $b->month
$a->year <=> $b->year $a->month <=> $b->month
Truth table0%0/3
A || B
A= $a->year <=> $b->yearB= $a->month <=> $b->month
ABresult
1X1
011
000
57 4 @{$new_cat->year_months}) {
58  
59 0 my $old_cat_year_month;
60 0 foreach my $year_month_iter (@{$old_cat->year_months}) {
61 0 ▶ if ($year_month_iter->year eq $new_cat_year_month->year
Branch: 1 branch
BranchTrueFalseTotal
if ($year_month_iter->year eq $new_cat_year_month->year and $year_month_iter->month eq $new_cat_year_month->month) 0 0 0
Condition0%0/3
$year_month_iter->year eq $new_cat_year_month->year and $year_month_iter->month eq $new_cat_year_month->month
!ll&&!rl&&r
000
MC/DC0%0/2
$year_month_iter->year eq $new_cat_year_month->year and $year_month_iter->month eq $new_cat_year_month->month
$year_month_iter->year eq $new_cat_year_month->year $year_month_iter->month eq $new_cat_year_month->month
Truth table0%0/3
A and B
A= $year_month_iter->year eq $new_cat_year_month->yearB= $year_month_iter->month eq $new_cat_year_month->month
ABresult
0X0
100
111
62 && $year_month_iter->month eq $new_cat_year_month->month) {
63  
64 0 $old_cat_year_month = $year_month_iter;
65 0 last;
66 }
67 }
68 0 ▶ my $count = defined $old_cat_year_month ?
Branch: 1 branch
BranchTrueFalseTotal
defined $old_cat_year_month ? : 0 0 0
69 ($new_cat_year_month->count - $old_cat_year_month->count)
70 : $new_cat_year_month->count;
71 0 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
72 0 push @{$year_months_ar}, Toolforge::MixNMatch::Object::YearMonth->new(
73 'year' => $new_cat_year_month->year,
74 'month' => $new_cat_year_month->month,
75 'count' => $count,
76 );
77 }
78 }
79  
80 4 my $catalog_diff = Toolforge::MixNMatch::Object::Catalog->new(
81 'count' => $new_cat->count,
82 'type' => $new_cat->type,
83 'users' => $users_ar,
84 'year_months' => $year_months_ar,
85 );
86  
87 4 return $catalog_diff;
88 }
89  
90 8 ▶sub _compute_users_count {
Subroutine _compute_users_count: called CC=2 SCAR=6.9
91 8 my $catalog = shift;
92  
93 8 my $count = 0;
94 8 foreach my $user (@{$catalog->users}) {
95 4 $count += $user->count;
96 }
97  
98 8 return $count;
99 }
100  
101 1 ▶sub _warn {
Subroutine _warn: called CC=2 SCAR=7.6
102 1 my ($mess, $warn) = @_;
103  
104 1 ▶ if ($warn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn) 0 1 1
105 0 print "WARNING: $mess\n";
106 }
107  
108 1 return;
109 }
110  
111 4 ▶sub _which_catalog_is_new {
Subroutine _which_catalog_is_new: called CC=5 SCAR=16.7
112 4 my ($catalog1, $catalog2, $warn) = @_;
113  
114 4 my ($new_cat, $old_cat);
115  
116 # Different total counts.
117 4 ▶ if ($catalog1->count > $catalog2->count) {
Branch: 2 branches
BranchTrueFalseTotal
if ($catalog1->count > $catalog2->count) { } 0 4 4
elsif ($catalog2->count > $catalog1->count) { } 0 4 4
118 0 $new_cat = $catalog1;
119 0 $old_cat = $catalog2;
120 } elsif ($catalog2->count > $catalog1->count) {
121 0 $new_cat = $catalog2;
122 0 $old_cat = $catalog1;
123  
124 # Total counts are same.
125 } else {
126 4 my $users_count1 = _compute_users_count($catalog1);
127 4 my $users_count2 = _compute_users_count($catalog2);
128 4 ▶ if ($users_count1 > $users_count2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($users_count1 > $users_count2) { } 1 3 4
elsif ($users_count2 > $users_count1) { } 2 1 3
129 1 $new_cat = $catalog1;
130 1 $old_cat = $catalog2;
131 } elsif ($users_count2 > $users_count1) {
132 2 $new_cat = $catalog2;
133 2 $old_cat = $catalog1;
134  
135 # All counts are same.
136 } else {
137 1 _warn('Catalogs are same in counts.', $warn);
138 1 $new_cat = $catalog2;
139 1 $old_cat = $catalog1;
140 }
141 }
142  
143 4 return ($new_cat, $old_cat);
144 }
145  
146 3 1;
147  
148 __END__
↑ Summary