Diff/Role/Common.pm

filter: Statementstmt 91.5% 76 / 83 Branchbran 70.3% 38 / 54 Conditioncond 54.5% 18 / 33 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 74.5% 152 / 204 CC 42 SCAR 35.3
CC
42
Cov
78%
_is_eq
72.5
_info_diff
9.2
_coerce_schema_data
7.0
CRAP 34.0
↑ Summary Diff/Filter.pm »
1 package DBIx::Class::Schema::Diff::Role::Common;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Types::Standard qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Array::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use JSON::DWIW;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 30 ▶sub _types_list { qw(
Subroutine _types_list: called CC=1 SCAR=0.0
15 columns
16 relationships
17 constraints
18 table_name
19 isa
20 )}
21  
22  
23 # Adapted from Hash::Diff, but heavily modified and specific to
24 # the unique needs of this module...
25 sub _info_diff {
26 7953 ▶ my ($self, $old, $new) = @_;
Subroutine _info_diff: called CC=9 SCAR=22.2
27
28 7953 my %old_keys = map {$_=>1} keys %$old;
29  
30 7953 my $nh = {};
31  
32 7953 for my $k (keys %$new) {
33 24189 ▶ if (exists $old->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old->{$k}) { } 24189 0 24189
34 24189 delete $old_keys{$k};
35 24189 ▶ if(ref $new->{$k} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new->{$k} eq 'HASH') { } 3795 20394 24189
36 3795 ▶ if(ref $old->{$k} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old->{$k} eq 'HASH') { } 3795 0 3795
37 3795 ▶ my $diff = $self->_info_diff($old->{$k},$new->{$k}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $diff = $self->_info_diff($old->{$k}, $new->{$k}) or next 3773 22 3795
38 22 $nh->{$k} = $diff;
39 }
40 else {
41 0 $nh->{$k} = $new->{$k};
42 }
43 }
44 else {
45 # Test if the non hash values are determined to be "equal"
46 20394 ▶ $nh->{$k} = $new->{$k} unless ($self->_is_eq($old->{$k},$new->{$k}));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_eq($old->{$k}, $new->{$k}) 68 20326 20394
47 }
48 }
49 else {
50 0 $nh->{$k} = $new->{$k};
51 }
52 }
53
54 # fill back in any left over, old keys (i.e. weren't in $new):
55 # TODO: track these separately
56 7953 $nh->{$_} = $old->{$_} for (keys %old_keys);
57  
58 7953 ▶ return undef unless (keys %$nh > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$nh > 0 7863 90 7953
59 90 return $nh;
60 }
61  
62 # test non-hash
63 # Note: since 'SchemaData' was introduced (Github Issue #1) most of
64 # this logic is now redundant/not needed...
65 sub _is_eq {
66 21472 ▶ my ($self, $old, $new) = @_;
Subroutine _is_eq: called CC=24 SCAR=42.8
67
68 # if both undef, they are equal:
69 21472 ▶ return 1 if(!defined $old && !defined $new);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old or defined $new 0 21472 21472
Condition33%1/3
defined $old or defined $new
l!l&&r!l&&!r
2147200
MC/DC0%0/2
defined $old or defined $new
defined $old defined $new
Truth table33%1/3
A or B
A= defined $oldB= defined $new
ABresult
1X1
011
000
70
71 21472 my ($o_ref,$n_ref) = (ref $old,ref $new);
72
73 # one is a ref and the other isn't, obviously not equal:
74 21472 ▶ return 0 if ($n_ref && !$o_ref || $o_ref && !$n_ref);
Branch: 1 branch
BranchTrueFalseTotal
if $n_ref and not $o_ref or $o_ref and not $n_ref 0 21472 21472
Condition55%5/9
$n_ref and not $o_ref or $o_ref and not $n_ref
l!l&&r!l&&!r
0021472
$n_ref and not $o_ref
!ll&&!rl&&r
208676050
$o_ref and not $n_ref
!ll&&!rl&&r
208676050
MC/DC0%0/4
$n_ref and not $o_ref or $o_ref and not $n_ref
$n_ref not $o_ref $o_ref not $n_ref
Truth table28%2/7
A and not(B) or C and not(D)
A= $n_refB= not $o_refC= $o_refD= not $n_ref
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
75
76 # both refs:
77 21472 ▶ if($o_ref && $n_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o_ref and $n_ref) 605 20867 21472
Condition66%2/3
$o_ref and $n_ref
!ll&&!rl&&r
208670605
MC/DC50%1/2
$o_ref and $n_ref
$o_ref $n_ref
Truth table66%2/3
A and B
A= $o_refB= $n_ref
ABresult
0X0
100
111
78 # If they are not the same kind of ref, they obviously aren't equal:
79 605 ▶ return 0 unless ($o_ref eq $n_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $o_ref eq $n_ref 0 605 605
80
81 605 ▶ if($n_ref eq 'CODE') {
Branch: 5 branches
BranchTrueFalseTotal
if ($n_ref eq 'CODE') { } 0 605 605
elsif ($n_ref eq 'SCALAR' or $n_ref eq 'REF') { } 0 605 605
elsif ($n_ref eq 'ARRAY') { } 605 0 605
elsif ($n_ref eq 'HASH') { } 0 0 0
elsif (builtin::blessed($new)) { } 0 0 0
82 # We can't tell the difference between CodeRefs, but we don't want
83 # those cases to show up as changed, so we call them equal:
84 0 return 1;
85 }
86 ▶ elsif($n_ref eq 'SCALAR' || $n_ref eq 'REF') {
Condition33%1/3
$n_ref eq "SCALAR" or $n_ref eq "REF"
l!l&&r!l&&!r
00605
MC/DC0%0/2
$n_ref eq "SCALAR" or $n_ref eq "REF"
$n_ref eq "SCALAR" $n_ref eq "REF"
Truth table33%1/3
A or B
A= $n_ref eq "SCALAR"B= $n_ref eq "REF"
ABresult
1X1
011
000
87 # For ScalarRefs, compare their referants:
88 0 return $self->_is_eq($$old,$$new);
89 }
90 elsif($n_ref eq 'ARRAY') {
91 # If they don't have the same number of elements, they aren't equal:
92 605 ▶ return 0 unless (scalar @$new == scalar @$old);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$new == scalar @$old 11 594 605
93
94 # If they are both empty, they are equal:
95 594 ▶ return 1 if (scalar @$new == 0 && scalar @$old == 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$new == 0 and scalar @$old == 0 0 594 594
Condition33%1/3
scalar @$new == 0 and scalar @$old == 0
!ll&&!rl&&r
59400
MC/DC0%0/2
scalar @$new == 0 and scalar @$old == 0
scalar @$new == 0 scalar @$old == 0
Truth table33%1/3
A and B
A= scalar @$new == 0B= scalar @$old == 0
ABresult
0X0
100
111
96
97 # iterate both sides:
98 594 my $i = 0;
99 594 for my $n_el (@$new) {
100 1078 my $o_el = $old->[$i++];
101 # Return 0 as soon as the first element is not equal:
102 1078 ▶ return 0 unless ($self->_is_eq($o_el,$n_el));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_eq($o_el, $n_el) 22 1056 1078
103 }
104
105 # If we made it here, then all the elements were equal above:
106 572 return 1;
107 }
108 elsif($n_ref eq 'HASH') {
109 # This case will only be called by us for HashRef elements of ArrayRef
110 # (case above). The main _info_diff() function handles HashRef's itself.
111 # Also note that from this point it is a true/false equality -- there
112 # is no more selective merging of hashes, showing only different keys
113 #
114 # If the hashes are equal, the diff should be undef:
115 0 ▶ return $self->_info_diff($old,$new) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_info_diff($old, $new) ? : 0 0 0
116 }
117 elsif(blessed $new) {
118 # If this is an object reference, just compare the classes, since we don't
119 # know how to compare object data and won't try:
120 0 return $self->_is_eq(blessed($old),blessed($new));
121 }
122 else {
123 0 die "Unexpected ref type '$n_ref'";
124 }
125 }
126  
127 # simple scalar value comparison:
128 20867 ▶ return (defined $old && defined $new && "$old" eq "$new");
Condition50%3/6
defined $old && defined $new && "$old" eq "$new"
!ll&&!rl&&r
05720810
defined $old and defined $new
!ll&&!rl&&r
0020867
MC/DC33%1/3
defined $old && defined $new && "$old" eq "$new"
defined $old defined $new "$old" eq "$new"
Truth table50%2/4
A and B && C
A= defined $oldB= defined $newC= "$old" eq "$new"
ABCresult
0XX0
10X0
1100
1111
129 }
130  
131  
132 sub _coerce_list_hash {
133 $_[0] && ! ref($_[0]) ? { $_[0] => 1 } :
134 7 ▶ ref($_[0]) eq 'ARRAY' ? { map {$_=>1} @{$_[0]} } : $_[0];
Subroutine _coerce_list_hash: called CC=4 SCAR=13.9
Branch: 2 branches
BranchTrueFalseTotal
$_[0] && !ref($_[0]) ? : 5 2 7
ref $_[0] eq 'ARRAY' ? : 1 1 2
Condition66%2/3
$_[0] and not ref $_[0]
!ll&&!rl&&r
025
MC/DC50%1/2
$_[0] and not ref $_[0]
$_[0] not ref $_[0]
Truth table66%2/3
A and not(B)
A= $_[0]B= not ref $_[0]
ABresult
0X0
100
111
135 }
136  
137  
138 sub _coerce_schema_diff {
139 89 ▶ blessed $_[0] ? $_[0] : DBIx::Class::Schema::Diff::Schema->new($_[0]);
Subroutine _coerce_schema_diff: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 65 24 89
140 }
141  
142  
143 sub _coerce_schema_data {
144 48 ▶ my ($v) = @_;
Subroutine _coerce_schema_data: called CC=7 SCAR=19.5
145 48 my $rt = ref($v);
146 48 ▶ if($rt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rt) { } 32 16 48
147 32 ▶ if(blessed($v) && blessed($v) eq 'DBIx::Class::Schema::Diff::SchemaData') {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($v) and builtin::blessed($v) eq 'DBIx::Class::Schema::Diff::SchemaData') { } 1 31 32
elsif ($rt eq 'HASH') { } 2 29 31
Condition100%3/3
builtin::blessed($v) and builtin::blessed($v) eq "DBIx::Class::Schema::Diff::SchemaData"
!ll&&!rl&&r
2291
MC/DC100%2/2
builtin::blessed($v) and builtin::blessed($v) eq "DBIx::Class::Schema::Diff::SchemaData"
builtin::blessed($v) builtin::blessed($v) eq "DBIx::Class::Schema::Diff::SchemaData"
Truth table100%3/3
A and B
A= builtin::blessed($v)B= builtin::blessed($v) eq "DBIx::Class::Schema::Diff::SchemaData"
ABresult
0X0
100
111
148 1 return $v;
149 }
150 elsif($rt eq 'HASH') {
151 2 return DBIx::Class::Schema::Diff::SchemaData->new({ data => $v });
152 }
153 else {
154 # Assume all other ref types are schema instances:
155 29 return DBIx::Class::Schema::Diff::SchemaData->new({ schema => $v });
156 }
157 }
158 else {
159 16 ▶ unless(Module::Runtime::is_module_name($v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Module::Runtime::is_module_name($v)) 4 12 16
160 4 my $file = file($v)->absolute;
161 4 ▶ if(-f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 4 0 4
162 # Assume it is a json file and try to decode it:
163 4 my $data = JSON::DWIW::deserialize_file($file);
164 4 return DBIx::Class::Schema::Diff::SchemaData->new({ data => $data });
165 }
166 }
167 12 return DBIx::Class::Schema::Diff::SchemaData->new({ schema => $v });
168 }
169 }
170  
171  
172 4 1;
173  
174  
175 __END__
↑ Summary Diff/Filter.pm »