Data-Comparable
view release on metacpan or search on metacpan
sub prepare_comparable {
my $self = shift;
$self->SUPER::prepare_comparable(@_);
delete $self->{some_temp_value};
$self->items; # autovivify;
}
# in some test file:
use Test::Differences;
my $x = '...';
my $y = '...';
eq_or_diff($x->comparable, $y->comparable, 'objects are equal');
DESCRIPTION
When you define a class, it may not be so straightforward to compare two
objects of that class. For example, you want to compare object $x to
object $y. You would like to use "is_deeply()" from Test::More, but it
complains that some hash keys are undef in one object but completely
missing in the other. That is easily solved by autovivifying the keys in
question. Also, some hash keys might be irrelevant to comparison - that
is, you still consider two objects to be equal even though they differ
in some hash keys.
This is where Data::Comparable can help. It enables you to define how
your object wants to look like when it is being passed to some deep
comparison function like "Test::More::is_deeply()" or
"Test::Differences::eq_or_diff()". If your class inherits from
Data::Comparable, it gets a method called "comparable()", which you can
call when comparing it. That is, you don't compare the actual objects,
but their comparable versions.
To define the comparable version of your object, your class has to
implement the "prepare_comparable()" method. There you can autovivify
any hash keys you like or tweak your object in any way you need to make
it comparable.
METHODS
lib/Data/Comparable.pm view on Meta::CPAN
sub prepare_comparable {
my $self = shift;
$self->SUPER::prepare_comparable(@_);
delete $self->{some_temp_value};
$self->items; # autovivify;
}
# in some test file:
use Test::Differences;
my $x = '...';
my $y = '...';
eq_or_diff($x->comparable, $y->comparable, 'objects are equal');
=head1 DESCRIPTION
When you define a class, it may not be so straightforward to compare two
objects of that class. For example, you want to compare object C<$x> to object
C<$y>. You would like to use C<is_deeply()> from Test::More, but it complains
that some hash keys are undef in one object but completely missing in the
other. That is easily solved by autovivifying the keys in question. Also, some
hash keys might be irrelevant to comparison - that is, you still consider two
objects to be equal even though they differ in some hash keys.
This is where Data::Comparable can help. It enables you to define how your
object wants to look like when it is being passed to some deep comparison
function like C<Test::More::is_deeply()> or
C<Test::Differences::eq_or_diff()>. If your class inherits from
Data::Comparable, it gets a method called C<comparable()>, which you can call
when comparing it. That is, you don't compare the actual objects, but their
comparable versions.
To define the comparable version of your object, your class has to implement
the C<prepare_comparable()> method. There you can autovivify any hash keys you
like or tweak your object in any way you need to make it comparable.
=head1 METHODS
( run in 3.343 seconds using v1.01-cache-2.11-cpan-2398b32b56e )