File Coverage

blib/lib/Data/Compare/Plugins/Set/Object.pm
Criterion Covered Total %
statement 29 29 100.0
branch 3 6 50.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 41 44 93.1


line stmt bran cond sub pod time code
1             package Data::Compare::Plugins::Set::Object;
2              
3 3     3   113840 use strict;
  3         11  
  3         72  
4 3     3   15 use warnings;
  3         4  
  3         72  
5 3     3   637 use version 0.77; our $VERSION = qv('v1.0_5');
  3         3895  
  3         18  
6 3     3   980 use English qw(-no_match_vars);
  3         5875  
  3         13  
7 3     3   1423 use Data::Compare 0.06;
  3         15345  
  3         17  
8 3     3   7904 use List::Util qw(first);
  3         6  
  3         637  
9              
10             sub _register {
11 3     3   536 return [ 'Set::Object', \&_so_set_compare ];
12             }
13              
14             sub _so_set_compare {
15 1     1   1180 my @sets = splice @ARG, 0, 2;
16              
17             # quick optimizations if sets aren't of equal size or are directly equal
18 1 50       7 return 0 if $sets[0]->size() != $sets[1]->size();
19 1 50       3 return 1 if $sets[0] == $sets[1];
20              
21             # loop over each of the first set's elements
22             # looking for a match in the second set
23 1         15 for my $element ( $sets[0]->elements() ) {
24             my $matched_element =
25 5     5   167 first { Data::Compare::Compare( $element, $ARG ) }
26 3         18 grep { ref eq ref $element } $sets[1]->elements();
  6         26  
27              
28             # return false if not found
29 3 50       210 return 0 if not defined $matched_element;
30              
31             # otherwise remove from copy of second set and keep going
32 3         10 $sets[1]->remove($matched_element);
33             }
34              
35             # sets are equal only if we've exhausted the second set
36 1         7 return $sets[1]->is_null();
37             }
38              
39             # Data::Compare::Plugins interface requires modules to return an arrayref
40             ## no critic (RequireEndWithOne)
41             _register();
42              
43             __END__
44              
45             =head1 NAME
46              
47             Data::Compare::Plugins::Set::Object - plugin for Data::Compare to handle
48             Set::Object objects
49              
50             =head1 VERSION
51              
52             This document describes Data::Compare::Plugins::Set::Object version 1.0_5
53              
54             =head1 SYNOPSIS
55              
56             use Set::Object 'set';
57             use Data::Compare;
58              
59             my $foo = {
60             list => [ qw(one two three) ],
61             set => set( [1], [2], [3] ),
62             };
63             my $bar = {
64             list => [ qw(one two three) ],
65             set => set( [1], [2], [3] ),
66             };
67              
68             say 'Sets in $foo and $bar are ',
69             $foo->{set} == $bar->{set} ? '' : 'NOT ', 'identical.';
70             say 'Data within $foo and $bar are ',
71             Compare($foo, $bar) ? '' : 'NOT ', 'equal.';
72              
73             =head1 DESCRIPTION
74              
75             Enables L<Data::Compare> to Do The Right Thing for L<Set::Object> objects.
76             Set::Object already has an C<equals()> method, but it only returns true if
77             objects within two sets are exactly equal (i.e. have the same references,
78             referring to the same object instance). When using Data::Compare in
79             conjuction with this plugin, objects in sets are considered the same if their
80             B<contents> are the same. This extends down to sets that contain arrays,
81             hashes, or other objects supported by Data::Compare plugins.
82              
83             =head1 SUBROUTINES/METHODS
84              
85             As a plugin to Data::Compare, the interface is the same as Data::Compare
86             itself: pass the reference to two data structures to the C<Compare> function,
87             which for historical reasons is exported by default.
88              
89             Set::Object also can export certain functions, and overloads comparison
90             operators pertaining to sets. Consult the
91             L<Set::Object documentation|Set::Object> for more information.
92              
93             =head1 DIAGNOSTICS
94              
95             See the L<documentation for Data::Compare|Data::Compare>.
96              
97             =head1 CONFIGURATION AND ENVIRONMENT
98              
99             Data::Compare::Plugins::Set::Object requires no configuration files or environment variables.
100              
101             =head1 DEPENDENCIES
102              
103             =over
104              
105             =item L<Data::Compare> >= 0.06 (must be installed separately)
106              
107             =item L<Set::Object> (must be installed separately)
108              
109             =item L<English> (part of the standard Perl 5 distribution)
110              
111             =item L<List::Util> (part of the standard Perl 5 distribution)
112              
113             =item L<version> >= 0.77 (part of the standard Perl 5.10.1 distribution)
114              
115             =back
116              
117             =head1 INCOMPATIBILITIES
118              
119             None reported.
120              
121             =head1 BUGS AND LIMITATIONS
122              
123             No bugs have been reported.
124              
125             Please report any bugs or feature requests to
126             C<bug-data-compare-plugins-set-object@rt.cpan.org>, or through the web
127             interface at L<http://rt.cpan.org>.
128              
129             =head1 AUTHOR
130              
131             Mark Gardner C<< <mjgardner@cpan.org> >>
132              
133             =head1 LICENSE AND COPYRIGHT
134              
135             Copyright (c) 2009, Mark Gardner C<< <mjgardner@cpan.org> >>. All rights
136             reserved.
137              
138             This module is free software; you can redistribute it and/or
139             modify it under the same terms as Perl 5.10.1 itself.
140              
141             =head1 DISCLAIMER OF WARRANTY
142              
143             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
144             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
145             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
146             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
147             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
148             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
149             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
150             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
151             NECESSARY SERVICING, REPAIR, OR CORRECTION.
152              
153             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
154             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
155             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
156             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
157             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
158             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
159             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
160             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
161             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
162             SUCH DAMAGES.