[Pdx-pm] is_deeply and objects

Michael G Schwern schwern at pobox.com
Fri Feb 25 16:00:59 PST 2005

On Fri, Feb 25, 2005 at 03:48:50PM -0800, Josh Heumann wrote:
> <quote who="Michael G Schwern">
> > Also, what about this?
> >
> >   package Foo;
> >   sub new { return bless { foo => 42 } }
> >
> >   package Bar;
> >   @ISA = qw(Foo);
> >
> >   package main;
> >   use Test::More tests => 1;
> >
> >   my $foo = Foo->new;
> >   my $bar = Bar->new;
> >
> >   is_deeply( $foo, $bar );
> >
> > If is_deeply were object-aware should that pass or fail?  Maybe, maybe
> > not.
> Advocating devilishly, what are the arguments for it failing?  If there
> are two objects, blessed with the same class and containing the same data,
> are they really different?

They're not the same class.  Or rather they weren't supposed to be.
I wrote the constructor hastily.  It should have been:

	sub new {
		my $class = shift;
		return bless { foo => 42 }, $class;

So its two objects with the same internal structure where one inherits
from the other's class.  Should they be considered equal?  I can come up
with arguments both ways:

Yes, they should be since Bar inherits from Foo it should have the same
behaviors.  In theory I should be able to replace $foo with $bar in the
program and have it continue to work.  The blackbox is preserved.

No, if we're looking at the internal structure of an object we're into
glassbox testing and thus want exact internal equality.

So I just punt the whole issue over in Fergal's direction.  Test::Deep
allows you to customize the comparison to do whatever you like.

Now for the real reason is_deeply() doesn't acknowledge objects or pierce
overloading:  I didn't think of it when I wrote it and it was simplier to
do without.  All of the above is retrograde reasoning for why it works the 
way it does.  Fortunately it comes out pretty sensible in the end.

> >> This question is posed in honor of Schwern's arrival.  Schwern: who
> >> loves ya, baby?
> >
> > According to my spam folder, Jeanna11111 is hot and waiting for me right
> > now.
> Lucky you.  And the other 42,000 people.

That dirty ho... as advertised.

More information about the Pdx-pm-list mailing list