Compare/Plugins/Scalar/Properties.pm

filter: Statementstmt 57.6% 15 / 26 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 3 totaltotal 41.6% 20 / 48 CC 7 SCAR 25.4
CC
7
Cov
39%
sp_scalar_compare
20.0
sp_sp_compare
20.0
BEGIN
1.0
CRAP 12.7
« Compare.pm ↑ Summary
1 package Data::Compare::Plugins::Scalar::Properties;
2  
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 $VERSION = 1.25;
9  
10 14 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
11 return [
12 14 ['Scalar::Properties', \&sp_scalar_compare],
13 ['', 'Scalar::Properties', \&sp_scalar_compare],
14 ];
15 }
16  
17 # note that when S::Ps are involved we can't use Data::Compare's default
18 # Compare function, so we use eq to check that values are the same. But
19 # we *do* use D::C::Compare whenever possible.
20  
21 # Compare a S::P and a scalar, or if we figure out that we've got two
22 # S::Ps, call sp_sp_compare instead
23  
24 0 ▶sub sp_scalar_compare {
Subroutine sp_scalar_compare: not called CC=4 SCAR=30.0
Pod undocumented
25 0 my($scalar, $sp) = @_;
26  
27 # we don't care what order the two params are, so swap if necessary
28 0 ▶ ($scalar, $sp) = ($sp, $scalar) if(ref($scalar));
Branch: 1 branch
BranchTrueFalseTotal
if ref $scalar 0 0 0
29  
30 # got two S::Ps?
31 0 ▶ return sp_sp_compare($scalar, $sp) if(ref($scalar));
Branch: 1 branch
BranchTrueFalseTotal
if ref $scalar 0 0 0
32  
33 # we've really got a scalar and an S::P, so just compare values
34 0 ▶ return 1 if($scalar eq $sp);
Branch: 1 branch
BranchTrueFalseTotal
if $scalar eq $sp 0 0 0
35 0 return 0;
36 }
37  
38 # Compare two S::Ps
39  
40 0 ▶sub sp_sp_compare {
Subroutine sp_sp_compare: not called CC=4 SCAR=30.0
Pod undocumented
41 0 my($sp1, $sp2) = @_;
42  
43 # first check the values
44 0 ▶ return 0 unless($sp1 eq $sp2);
Branch: 1 branch
BranchTrueFalseTotal
unless $sp1 eq $sp2 0 0 0
45
46 # now check that we have all the same properties
47 0 ▶ return 0 unless(Data::Compare::Compare([sort $sp1->get_props()], [sort $sp2->get_props()]));
Branch: 1 branch
BranchTrueFalseTotal
unless Data::Compare::Compare([sort($sp1->get_props)], [sort($sp2->get_props)]) 0 0 0
48  
49 # and that all properties have the same values
50 return 0 if(
51 0 ▶ grep { !Data::Compare::Compare(eval "\$sp1->$_()", eval "\$sp2->$_()") } $sp1->get_props()
Branch: 1 branch
BranchTrueFalseTotal
if grep {not Data::Compare::Compare(eval "\$sp1->$_()", eval "\$sp2->$_()");} $sp1->get_props 0 0 0
52 );
53  
54 # if we get here, all is tickety-boo
55 0 return 1;
56 }
57  
58 14 register();
59  
60 =head1 NAME
61  
62 Data::Compare::Plugin::Scalar::Properties - plugin for Data::Compare to
63 handle Scalar::Properties objects.
64  
65 =head1 DESCRIPTION
66  
67 Enables Data::Compare to Do The Right Thing for Scalar::Properties
68 objects.
69  
70 =over 4
71  
72 =item comparing a Scalar::Properties object and an ordinary scalar
73  
74 If you compare
75 a scalar and a Scalar::Properties, then they will be considered the same
76 if the two values are the same, regardless of the presence of properties.
77  
78 =item comparing two Scalar::Properties objects
79  
80 If you compare two Scalar::Properties objects, then they will only be
81 considered the same if the values and the properties match.
82  
83 =back
84  
85 =head1 AUTHOR
86  
87 Copyright (c) 2004 David Cantrell. All rights reserved.
88 This program is free software; you can redistribute it and/or
89 modify it under the same terms as Perl itself.
90  
91 =head1 SEE ALSO
92  
93 L<Data::Compare>
94  
95 =cut
« Compare.pm ↑ Summary