File Coverage

blib/lib/Data/Compare/Plugins/Set/Object.pm
Criterion Covered Total %
statement 31 31 100.0
branch 3 6 50.0
condition n/a
subroutine 10 10 100.0
pod n/a
total 44 47 93.6


line stmt bran cond sub pod time code
1             package Data::Compare::Plugins::Set::Object;
2              
3             # ABSTRACT: plugin for Data::Compare to handle Set::Object objects
4              
5             #pod =head1 DESCRIPTION
6             #pod
7             #pod Enables L<Data::Compare|Data::Compare> to Do The Right Thing for
8             #pod L<Set::Object|Set::Object> objects. Set::Object already has an
9             #pod C<equals()> method, but it only returns true if objects within two sets
10             #pod are exactly equal (i.e. have the same references, referring to the same
11             #pod object instance). When using Data::Compare in conjunction with this
12             #pod plugin, objects in sets are considered the same if their B<contents> are
13             #pod the same. This extends down to sets that contain arrays, hashes, or
14             #pod other objects supported by Data::Compare plugins.
15             #pod
16             #pod =cut
17              
18 1     1   62928 use 5.010;
  1         7  
19 1     1   314 use utf8;
  1         11  
  1         4  
20 1     1   25 use strict;
  1         1  
  1         16  
21 1     1   4 use warnings;
  1         2  
  1         41  
22              
23             our $VERSION = '1.001'; # VERSION
24 1     1   237 use English '-no_match_vars';
  1         2489  
  1         5  
25 1     1   289 use Data::Compare 0.06;
  1         14  
  1         6  
26 1     1   3863 use List::Util 'first';
  1         2  
  1         220  
27              
28             sub _register {
29 1     1   10 return [ 'Set::Object', \&_so_set_compare ];
30             }
31              
32             ## no critic (Subroutines::RequireArgUnpacking)
33             sub _so_set_compare {
34 1     1   1184 my @sets = splice @_, 0, 2;
35              
36             # quick optimizations if sets aren't of equal size or are directly equal
37 1 50       6 return 0 if $sets[0]->size() != $sets[1]->size();
38 1 50       4 return 1 if $sets[0] == $sets[1];
39              
40             # loop over each of the first set's elements
41             # looking for a match in the second set
42 1         13 for my $element ( $sets[0]->elements() ) {
43 3     3   7 my $matched_element = first { Data::Compare::Compare( $element, $_ ) }
44 3         17 grep { ref eq ref $element } $sets[1]->elements();
  6         27  
45              
46             # return false if not found
47 3 50       241 return 0 if not defined $matched_element;
48              
49             # otherwise remove from copy of second set and keep going
50 3         9 $sets[1]->remove($matched_element);
51             }
52              
53             # sets are equal only if we've exhausted the second set
54 1         5 return $sets[1]->is_null();
55             }
56              
57             # Data::Compare::Plugins interface requires modules to return an arrayref
58             ## no critic (RequireEndWithOne, Lax::RequireEndWithTrueConst)
59             _register();
60              
61             __END__
62              
63             =pod
64              
65             =encoding utf8
66              
67             =for :stopwords Mark Gardner cpan testmatrix url annocpan anno bugtracker rt cpants
68             kwalitee diff irc mailto metadata placeholders metacpan
69              
70             =head1 NAME
71              
72             Data::Compare::Plugins::Set::Object - plugin for Data::Compare to handle Set::Object objects
73              
74             =head1 VERSION
75              
76             version 1.001
77              
78             =head1 SYNOPSIS
79              
80             use 5.010;
81             use Set::Object 'set';
82             use Data::Compare;
83              
84             my $foo = {
85             list => [ qw(one two three) ],
86             set => set( [1], [2], [3] ),
87             };
88             my $bar = {
89             list => [ qw(one two three) ],
90             set => set( [1], [2], [3] ),
91             };
92              
93             say 'Sets in $foo and $bar are ',
94             $foo->{set} == $bar->{set} ? '' : 'NOT ', 'identical.';
95             say 'Data within $foo and $bar are ',
96             Compare($foo, $bar) ? '' : 'NOT ', 'equal.';
97              
98             =head1 DESCRIPTION
99              
100             Enables L<Data::Compare|Data::Compare> to Do The Right Thing for
101             L<Set::Object|Set::Object> objects. Set::Object already has an
102             C<equals()> method, but it only returns true if objects within two sets
103             are exactly equal (i.e. have the same references, referring to the same
104             object instance). When using Data::Compare in conjunction with this
105             plugin, objects in sets are considered the same if their B<contents> are
106             the same. This extends down to sets that contain arrays, hashes, or
107             other objects supported by Data::Compare plugins.
108              
109             =head1 SUBROUTINES/METHODS
110              
111             As a plugin to Data::Compare, the interface is the same as Data::Compare
112             itself: pass the reference to two data structures to the C<Compare>
113             function, which for historical reasons is exported by default.
114              
115             Set::Object also can export certain functions, and overloads comparison
116             operators pertaining to sets. Consult the
117             L<Set::Object documentation|Set::Object> for more information.
118              
119             =head1 SUPPORT
120              
121             =head2 Perldoc
122              
123             You can find documentation for this module with the perldoc command.
124              
125             perldoc Data::Compare::Plugins::Set::Object
126              
127             =head2 Websites
128              
129             The following websites have more information about this module, and may be of help to you. As always,
130             in addition to those websites please use your favorite search engine to discover more resources.
131              
132             =over 4
133              
134             =item *
135              
136             Search CPAN
137              
138             The default CPAN search engine, useful to view POD in HTML format.
139              
140             L<http://search.cpan.org/dist/Data-Compare-Plugins-Set-Object>
141              
142             =item *
143              
144             AnnoCPAN
145              
146             The AnnoCPAN is a website that allows community annotations of Perl module documentation.
147              
148             L<http://annocpan.org/dist/Data-Compare-Plugins-Set-Object>
149              
150             =item *
151              
152             CPAN Ratings
153              
154             The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
155              
156             L<http://cpanratings.perl.org/d/Data-Compare-Plugins-Set-Object>
157              
158             =item *
159              
160             CPANTS
161              
162             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
163              
164             L<http://cpants.cpanauthors.org/dist/Data-Compare-Plugins-Set-Object>
165              
166             =item *
167              
168             CPAN Testers
169              
170             The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
171              
172             L<http://www.cpantesters.org/distro/D/Data-Compare-Plugins-Set-Object>
173              
174             =item *
175              
176             CPAN Testers Matrix
177              
178             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
179              
180             L<http://matrix.cpantesters.org/?dist=Data-Compare-Plugins-Set-Object>
181              
182             =item *
183              
184             CPAN Testers Dependencies
185              
186             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
187              
188             L<http://deps.cpantesters.org/?module=Data::Compare::Plugins::Set::Object>
189              
190             =back
191              
192             =head2 Bugs / Feature Requests
193              
194             Please report any bugs or feature requests through the web
195             interface at L<https://github.com/mjgardner/Data-Compare-Plugins-Set-Object/issues>. You will be automatically notified of any
196             progress on the request by the system.
197              
198             =head2 Source Code
199              
200             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
201             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
202             from your repository :)
203              
204             L<https://github.com/mjgardner/Data-Compare-Plugins-Set-Object>
205              
206             git clone git://github.com/mjgardner/Data-Compare-Plugins-Set-Object.git
207              
208             =head1 AUTHOR
209              
210             Mark Gardner <mjgardner@cpan.org>
211              
212             =head1 COPYRIGHT AND LICENSE
213              
214             This software is copyright (c) 2017 by Mark Gardner.
215              
216             This is free software; you can redistribute it and/or modify it under
217             the same terms as the Perl 5 programming language system itself.
218              
219             =cut