Comparator.pm

filter: Statementstmt 97.1% 67 / 69 Branchbran 93.3% 28 / 30 Conditioncond 67.6% 23 / 34 MC/DCmcdc 45.0% 9 / 20 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 5 totaltotal 81.3% 135 / 166 CC 32 SCAR 22.7
CC
32
Cov
89%
data_comparator
16.4
scalar_ref_comparator
7.2
scalar_comparator
5.0
CRAP 9.6
« Transformator.pm ↑ Summary Differences.pm »
1 #!/usr/bin/perl -w
2 #!/usr/bin/perl -d:ptkdb -w
3 #
4 # This is module is based on a module with the same name, implemented
5 # when working for Newtec Cy, located in Belgium,
6 # http://www.newtec.be/.
7 #
8  
9 package Data::Comparator;
10  
11  
12 #
13 # The main entry point for this module is the sub data_comparator().
14 # It compares two sets of (structured) data and reports on the
15 # differences found with a differences describing data structure.
16 #
17 # The algorithm used is of a subtractive kind. It subtracts the first
18 # data structure given from the second one. This means that, since it
19 # not possible to subtract what is not yet there, not all differences
20 # are reported. To have a report of all differences between
21 # structures A and B, first subtract A from B, next subtract B from A.
22 # The two result sets are an exact description of the differences
23 # between A and B (or should be, untested for the moment).
24 #
25  
26  
27 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29  
30 7 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 7 ▶use Data::Differences;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34  
35 # require Exporter;
36  
37 7 our @ISA = qw(Exporter);
38  
39 7 our @EXPORT_OK = qw(
40 array_comparator
41 hash_comparator
42 data_comparator
43 );
44  
45  
46 #
47 # array_comparator()
48 #
49 # Compare two arrays, report on the differences found by returning an
50 # array describing the differences between the two arrays.
51 #
52  
53 sub array_comparator
54 63 ▶{
Subroutine array_comparator: called CC=5 SCAR=16.1
Pod undocumented
55 63 my $array1 = shift;
56  
57 63 my $array2 = shift;
58  
59 # my $result = { adder_operand => [], subtractor_operand => [], };
60  
61 63 my $result = [];
62  
63 63 foreach my $index (0 .. $#$array1)
64 {
65 133 ▶ if (exists $array2->[$index])
Branch: 1 branch
BranchTrueFalseTotal
if (exists $array2->[$index]) { } 128 5 133
66 {
67 128 my $index_result = data_comparator($array1->[$index], $array2->[$index]);
68  
69 128 ▶ if (!$index_result->is_empty())
Branch: 1 branch
BranchTrueFalseTotal
unless ($index_result->is_empty) 4 124 128
70 {
71 4 $result->[$index] = $index_result;
72 }
73 }
74 else
75 {
76 5 $result->[$index]
77 = Data::Differences->new(clone(\$array1->[$index]));
78 }
79 }
80  
81 63 foreach my $index ($#$array1 + 1 .. $#$array2)
82 {
83 1 $result->[$index]
84 = Data::Differences->new(clone(\$array2->[$index]));
85 }
86  
87 63 return Data::Differences->new($result);
88 }
89  
90  
91 =head1 NAME
92  
93 Data::Comparator - recursively compare Perl datatypes
94  
95 =head1 SYNOPSIS
96  
97 use Data::Comparator qw(data_comparator);
98
99 $a = { 'foo' => 'bar', 'move' => 'zig' };
100 $b = [ 'alpha', 'beta', 'gamma', 'vlissides' ];
101  
102 $diff = data_comparator($a, $b);
103  
104 use Data::Dumper;
105  
106 print Dumper($diff);
107  
108 if ($diff->is_empty())
109 {
110 print '$a and $b are alike\n';
111 }
112 else
113 {
114 print '$a and $b are not alike\n';
115 }
116  
117 =head1 DESCRIPTION
118  
119 Compare two sets of (structured) data, report on the differences found
120 with a differences describing data structure. Additionally a set of
121 expected differences may be given in the form of a differences
122 describing data structure.
123  
124 Returns a differences describing data structure, which is empty if no
125 differences are found. The type of the result is the same as the type
126 of the second data structure given.
127  
128 The algorithm used is of a subtractive kind. It subtracts the first
129 data structure given from the second one. This means that, since it
130 is not possible to subtract what is not given in the subtractor, not
131 all differences are reported. To have a report of all differences
132 between structures A and B, first subtract A from B, next subtract B
133 from A, using this module. The two result sets are an exact
134 description of the differences between A and B.
135  
136 It is possible to add any of the methods array_comparator(),
137 hash_comparator(), data_comparator() to an existing object, or to use
138 these as regular subs.
139  
140 =head1 NOTE
141  
142 This module is used in the tests for Data::Merger(3) and
143 Data::Transformator(3).
144  
145 =head1 BUGS
146  
147 Does only work with scalars, hashes and arrays. Does not work on
148 self-referential structures.
149  
150 =head1 AUTHOR
151  
152 Hugo Cornelis, hugo.cornelis@gmail.com
153  
154 Copyright 2007 Hugo Cornelis.
155  
156 This module is free software; you can redistribute it and/or
157 modify it under the same terms as Perl itself.
158  
159 =head1 SEE ALSO
160  
161 Data::Merger(3), Data::Transformator(3), Data::Differences(3),
162 Clone(3)
163  
164 =cut
165  
166 sub data_comparator
167 378 ▶{
Subroutine data_comparator: called CC=16 SCAR=28.0
Pod undocumented
168 378 my $data1 = shift;
169  
170 378 my $data2 = shift;
171  
172 378 my $expected_differences = shift;
173  
174 378 my $result;
175  
176 # get the types for the different arguments
177  
178 378 ▶ my $data_type1 = (ref $data1 && "$data1") || '';
Condition80%4/5
ref $data1 && "$data1" || ''
l!l
217161
ref $data1 and "$data1"
!ll&&!rl&&r
1610217
MC/DC50%1/2
ref $data1 && "$data1" || ''
ref $data1 "$data1"
Truth table66%2/3
A and B
A= ref $data1B= "$data1"
ABresult
111
0X0
100
179  
180 378 ▶ my $data_type2 = (ref $data2 && "$data2") || '';
Condition80%4/5
ref $data2 && "$data2" || ''
l!l
216162
ref $data2 and "$data2"
!ll&&!rl&&r
1620216
MC/DC50%1/2
ref $data2 && "$data2" || ''
ref $data2 "$data2"
Truth table66%2/3
A and B
A= ref $data2B= "$data2"
ABresult
111
0X0
100
181  
182 # first compare comparables
183  
184 # try to compare two hashes
185  
186 378 ▶ if ($data_type1 =~ /HASH/
Branch: 5 branches
BranchTrueFalseTotal
if ($data_type1 =~ /HASH/ and $data_type2 =~ /HASH/) { } 145 233 378
elsif ($data_type1 =~ /ARRAY/ and $data_type2 =~ /ARRAY/) { } 63 170 233
elsif ($data_type1 =~ /SCALAR/ and $data_type2 =~ /SCALAR/) { } 6 164 170
elsif ($data_type1 =~ /REF/ and $data_type2 =~ /REF/) { } 2 162 164
elsif (not $data_type1 || $data_type2) { } 161 1 162
Condition100%3/3
$data_type1 =~ /HASH/ and $data_type2 =~ /HASH/
!ll&&!rl&&r
2321145
MC/DC100%2/2
$data_type1 =~ /HASH/ and $data_type2 =~ /HASH/
$data_type1 =~ /HASH/ $data_type2 =~ /HASH/
Truth table100%3/3
A and B
A= $data_type1 =~ /HASH/B= $data_type2 =~ /HASH/
ABresult
0X0
100
111
187 && $data_type2 =~ /HASH/)
188 {
189 145 $result = hash_comparator($data1, $data2);
190 }
191  
192 # or try to compare two arrays
193  
194 ▶ elsif ($data_type1 =~ /ARRAY/
Condition66%2/3
$data_type1 =~ /ARRAY/ and $data_type2 =~ /ARRAY/
!ll&&!rl&&r
170063
MC/DC50%1/2
$data_type1 =~ /ARRAY/ and $data_type2 =~ /ARRAY/
$data_type1 =~ /ARRAY/ $data_type2 =~ /ARRAY/
Truth table66%2/3
A and B
A= $data_type1 =~ /ARRAY/B= $data_type2 =~ /ARRAY/
ABresult
0X0
100
111
195 && $data_type2 =~ /ARRAY/)
196 {
197 63 $result = array_comparator($data1, $data2);
198 }
199  
200 # or try to compare two scalars
201  
202 ▶ elsif ($data_type1 =~ /SCALAR/
Condition66%2/3
$data_type1 =~ /SCALAR/ and $data_type2 =~ /SCALAR/
!ll&&!rl&&r
16406
MC/DC50%1/2
$data_type1 =~ /SCALAR/ and $data_type2 =~ /SCALAR/
$data_type1 =~ /SCALAR/ $data_type2 =~ /SCALAR/
Truth table66%2/3
A and B
A= $data_type1 =~ /SCALAR/B= $data_type2 =~ /SCALAR/
ABresult
0X0
100
111
203 && $data_type2 =~ /SCALAR/)
204 {
205 6 $result = scalar_ref_comparator($data1, $data2);
206 }
207  
208 # or try to compare two referenced references
209  
210 ▶ elsif ($data_type1 =~ /REF/
Condition66%2/3
$data_type1 =~ /REF/ and $data_type2 =~ /REF/
!ll&&!rl&&r
16202
MC/DC50%1/2
$data_type1 =~ /REF/ and $data_type2 =~ /REF/
$data_type1 =~ /REF/ $data_type2 =~ /REF/
Truth table66%2/3
A and B
A= $data_type1 =~ /REF/B= $data_type2 =~ /REF/
ABresult
0X0
100
111
211 && $data_type2 =~ /REF/)
212 {
213 2 $result = data_comparator($$data1, $$data2);
214 }
215  
216 # or try to compare two non references
217  
218 ▶ elsif (!$data_type1
Condition66%2/3
$data_type1 || $data_type2
l!l&&r!l&&!r
10161
MC/DC50%1/2
$data_type1 || $data_type2
$data_type1 $data_type2
Truth table66%2/3
A || B
A= $data_type1B= $data_type2
ABresult
1X1
011
000
219 && !$data_type2)
220 {
221 161 $result = scalar_comparator($data1, $data2);
222 }
223  
224 # second, for non-comparables
225  
226 else
227 {
228 # simply clone second argument
229  
230 1 $result = Data::Differences->new(clone(\$data2));
231 }
232  
233 # if the user was already expecting differences
234  
235 378 ▶ if (defined $expected_differences)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected_differences) 6 372 378
236 {
237 # compare the result with the expected differences
238  
239 6 $result = data_comparator($expected_differences, $result);
240 }
241  
242 378 return $result;
243 }
244  
245  
246 #
247 # hash_comparator()
248 #
249 # Compare two hashes, report on the differences found by returning an
250 # hash describing the differences between the two hashes.
251 #
252  
253 sub hash_comparator
254 145 ▶{
Subroutine hash_comparator: called CC=5 SCAR=16.1
Pod undocumented
255 145 my $hash1 = shift;
256  
257 145 my $hash2 = shift;
258  
259 145 my $result = {};
260  
261 145 foreach my $key (keys %$hash1)
262 {
263 205 ▶ if (exists $hash2->{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash2->{$key}) 182 23 205
264 {
265 182 my $key_result = data_comparator($hash1->{$key}, $hash2->{$key});
266  
267 182 ▶ if (!$key_result->is_empty())
Branch: 1 branch
BranchTrueFalseTotal
unless ($key_result->is_empty) 3 179 182
268 {
269 3 $result->{$key} = $key_result;
270 }
271 }
272 }
273  
274 145 foreach my $key (grep { !exists $hash1->{$_} } keys %$hash2)
275 {
276 $result->{$key}
277 2 = Data::Differences->new(clone(\$hash2->{$key}));
278 }
279  
280 145 return Data::Differences->new($result);
281 }
282  
283  
284 #
285 # scalar_comparator()
286 #
287 # Compare two scalar values, report on the differences found by
288 # returning the second scalar value if it is different from the first
289 # scalar value.
290 #
291  
292 sub scalar_comparator
293 161 ▶{
Subroutine scalar_comparator: called CC=5 SCAR=16.1
Pod undocumented
294 161 my $scalar1 = shift;
295  
296 161 my $scalar2 = shift;
297  
298 #t two undefs is illegal.
299  
300 161 ▶ if (!defined $scalar1 && !defined $scalar2)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scalar1 or defined $scalar2) 5 156 161
Condition66%2/3
defined $scalar1 or defined $scalar2
l!l&&r!l&&!r
15605
MC/DC50%1/2
defined $scalar1 or defined $scalar2
defined $scalar1 defined $scalar2
Truth table66%2/3
A or B
A= defined $scalar1B= defined $scalar2
ABresult
1X1
011
000
301 {
302 5 return Data::Differences->new(clone(\undef));
303 }
304  
305 156 ▶ if (!defined $scalar2)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scalar2) 5 151 156
306 {
307 5 return Data::Differences->new(clone(\$scalar2));
308 }
309  
310 151 ▶ if (($scalar1 cmp $scalar2) eq 0)
Branch: 1 branch
BranchTrueFalseTotal
if (($scalar1 cmp $scalar2) eq 0) { } 148 3 151
311 {
312 148 return Data::Differences->new(clone(\undef));
313 }
314 else
315 {
316 3 return Data::Differences->new(clone(\$scalar2));
317 }
318 }
319  
320  
321 #
322 # scalar_ref_comparator()
323 #
324 # Compare two references to scalar values, report on the differences
325 # found by returning the second reference if it is different from the
326 # first reference.
327 #
328  
329 sub scalar_ref_comparator
330 6 ▶{
Subroutine scalar_ref_comparator: called CC=5 SCAR=19.7
Pod undocumented
331 6 my $scalar1 = shift;
332  
333 6 my $scalar2 = shift;
334  
335 6 my $value1 = $$scalar1;
336  
337 6 my $value2 = $$scalar2;
338  
339  
340 # for two undefs
341  
342 6 ▶ if (!defined $value1
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value1 || defined $value2) { } 0 6 6
elsif (not defined $value1 && defined $value2) { } 0 6 6
Condition33%1/3
defined $value1 || defined $value2
l!l&&r!l&&!r
600
MC/DC0%0/2
defined $value1 || defined $value2
defined $value1 defined $value2
Truth table33%1/3
A || B
A= defined $value1B= defined $value2
ABresult
1X1
011
000
343 && !defined $value2)
344 {
345 # return equality
346  
347 0 return Data::Differences->new(clone(\undef));
348 }
349  
350 # for one undef
351  
352 ▶ elsif (!defined $value1
Condition33%1/3
defined $value1 && defined $value2
!ll&&!rl&&r
006
MC/DC0%0/2
defined $value1 && defined $value2
defined $value1 defined $value2
Truth table33%1/3
A && B
A= defined $value1B= defined $value2
ABresult
0X0
100
111
353 || !defined $value2)
354 {
355 # return different
356  
357 0 return Data::Differences->new(clone(\$scalar2));
358 }
359  
360 # in other cases
361  
362 else
363 {
364 # do a normal comparison by calling the generic comparator
365  
366 6 return data_comparator($value1, $value2);
367  
368 # if (($value1 cmp $value2) eq 0)
369 # {
370 # return Data::Differences->new(clone(\undef));
371 # }
372 # else
373 # {
374 # return Data::Differences->new(clone(\$scalar2));
375 # }
376 }
377 }
378  
379  
380 7 1;
381  
382  
« Transformator.pm ↑ Summary Differences.pm »