Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Test-Simple CPAN distribution.

Report information
The Basics
Id: 58341
Status: resolved
Priority: 0/
Queue: Test-Simple

People
Owner: Nobody in particular
Requestors: leonerd-cpan [...] leonerd.org.uk
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Support an is()-alike test for referential identity of objects
Hi there, Up until Test::More 0.94 I could use (though, possibly incorrectly ;) ) is( $e->string, $str, '$e->string' ); to check the referential identity of the object returned by that method. You reported me a bug that this no longer works on 0.95_01. I'm currently fixing up my tests. So far, the best thing I've found for referential identity is use Scalar::Util qw( refaddr ); is( refaddr $e->string, refaddr $str, '$e->string' ); but this doesn't look very neat. Perhaps this little piece of magic could be wrapped in e.g. is_sameref( $e->string, $str, '$e->string' ); or somesuch? -- Paul Evans
Subject: Re: [rt.cpan.org #58341] Support an is()-alike test for referential identity of objects
Date: Fri, 11 Jun 2010 16:09:42 -0700
To: bug-Test-Simple [...] rt.cpan.org
From: Michael G Schwern <schwern [...] pobox.com>
On 2010.6.11 11:20 AM, Paul Evans via RT wrote: Show quoted text
> Up until Test::More 0.94 I could use (though, possibly incorrectly ;) ) > > is( $e->string, $str, '$e->string' ); > > to check the referential identity of the object returned by that method. > > You reported me a bug that this no longer works on 0.95_01. I'm > currently fixing up my tests.
If that doesn't work with 0.95_01 I'm not sure how it ever worked. It implies an object that has eq overloaded but not stringification? Show quoted text
> So far, the best thing I've found for referential identity is > > use Scalar::Util qw( refaddr ); > is( refaddr $e->string, refaddr $str, '$e->string' ); > > but this doesn't look very neat. > > Perhaps this little piece of magic could be wrapped in e.g. > > is_sameref( $e->string, $str, '$e->string' ); > > or somesuch?
I really doubt it will go into Test::More. Its fairly trivial and something that doesn't come up all that often. I like to keep Test::More's interface slim. Fortunately, you can wrap it up like so: use Scalar::Util qw(refaddr); sub is_same { my($have, $want, name) = @_; local $Test::Builder::Level = $Test::Builder::Level + 1; return is( refaddr $have, refaddr $want ); } Or you can make use of the handy UNIVERSAL::Object::ID module to give your objects universal unique identifiers. Then you can write. use UNIVERSAL::Object::ID; is( $e->string->object_id, $str->object_id ); -- 164. There is no such thing as a were-virgin. -- The 213 Things Skippy Is No Longer Allowed To Do In The U.S. Army http://skippyslist.com/list/
CC: leonerd-cpan [...] leonerd.org.uk
Subject: Re: [rt.cpan.org #58341] Support an is()-alike test for referential identity of objects
Date: Fri, 18 Jun 2010 11:22:38 +0100
To: Michael G Schwern via RT <bug-Test-Simple [...] rt.cpan.org>
From: Paul LeoNerd Evans <leonerd [...] leonerd.org.uk>
On Fri, Jun 11, 2010 at 07:09:56PM -0400, Michael G Schwern via RT wrote: Show quoted text
> If that doesn't work with 0.95_01 I'm not sure how it ever worked. It implies > an object that has eq overloaded but not stringification?
It has stringification, but no eq. The original report is here https://rt.cpan.org/Ticket/Display.html?id=57762 Show quoted text
> I really doubt it will go into Test::More. Its fairly trivial and something > that doesn't come up all that often. I like to keep Test::More's interface slim.
Really? Given we have is(), isnt(), like(), unlike(), is_deeply(), .... adding one more for referential identity isn't much more. Show quoted text
> Fortunately, you can wrap it up like so: > > use Scalar::Util qw(refaddr); > sub is_same { > my($have, $want, name) = @_; > > local $Test::Builder::Level = $Test::Builder::Level + 1; > return is( refaddr $have, refaddr $want ); > }
I've found the following behaves sanely: use Scalar::Util qw( refaddr ); sub identical { my ( $got, $expected, $name ) = @_; ok( refaddr $got == refaddr $expected, $name ) or diag( "Expected $got and $expected to refer to the same object" ); } It adds the diagnostic when it fails also, which might be helpful. I'd be quite happy to write it up properly with tests and docs as a patch, if you think it might be acceptable.. -- Paul "LeoNerd" Evans leonerd@leonerd.org.uk ICQ# 4135350 | Registered Linux# 179460 http://www.leonerd.org.uk/
Download signature.asc
application/pgp-signature 190b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #58341] Support an is()-alike test for referential identity of objects
Date: Fri, 18 Jun 2010 13:11:41 -0700
To: bug-Test-Simple [...] rt.cpan.org
From: Michael G Schwern <schwern [...] pobox.com>
On 2010.6.18 3:22 AM, Paul LeoNerd Evans via RT wrote: Show quoted text
> On Fri, Jun 11, 2010 at 07:09:56PM -0400, Michael G Schwern via RT wrote:
>> If that doesn't work with 0.95_01 I'm not sure how it ever worked. It implies >> an object that has eq overloaded but not stringification?
> > It has stringification, but no eq. > > The original report is here > > https://rt.cpan.org/Ticket/Display.html?id=57762
In that case, I don't understand how this ever tested object equality: is( $e->string, $str, '$e->string' ); <= 0.94 that was equivalent to: my $have = $e->string; is( "$have", "$str", '$e->string' ); If $str or $e->string is string overloaded, that's testing the string overloading, not object equality. So I'm a bit lost. Show quoted text
>> I really doubt it will go into Test::More. Its fairly trivial and something >> that doesn't come up all that often. I like to keep Test::More's interface slim.
> > Really? Given we have is(), isnt(), like(), unlike(), is_deeply(), .... > adding one more for referential identity isn't much more.
Its always just one more. I prefer to keep Test::More down to relatively straightforward (you'd be surprised to find how not straightforward is() and like() can get at times) and broadly useful functions to keep the API slim and the implementation solid. String equality, string matching and complex structure equality all match that. The other criterion is that the function produce useful diagnostics that would otherwise be annoying to get. For example, isnt() exists because ok( $have ne $want ) doesn't tell you what $have was when it failed and there's no easy way to negate is(). (Also it was amusing) Finally, it has to have interesting corner cases that make it less than trivial to write. is() handles things like undef and object overloading. An object equality function doesn't appear to have any of these traits. That's my superficial reading of it. I encourage you to argue otherwise. Show quoted text
>> Fortunately, you can wrap it up like so: >> >> use Scalar::Util qw(refaddr); >> sub is_same { >> my($have, $want, name) = @_; >> >> local $Test::Builder::Level = $Test::Builder::Level + 1; >> return is( refaddr $have, refaddr $want ); >> }
> > I've found the following behaves sanely: > > use Scalar::Util qw( refaddr ); > sub identical > { > my ( $got, $expected, $name ) = @_; > > ok( refaddr $got == refaddr $expected, $name ) or > diag( "Expected $got and $expected to refer to the same object" ); > } > > It adds the diagnostic when it fails also, which might be helpful.
You want to increment $Test::Builder::Level so the ok() failure is reported at the point where identical() is called instead of ok(). Show quoted text
> I'd be quite happy to write it up properly with tests and docs as a > patch, if you think it might be acceptable..
At this point, no, but you can write it up as a module. There is, imho, an opportunity to write a test module which covers detailed class and object tests better than isa_ok, can_ok and new_ok [1] serve. -- I'm pale as Formica, social skills stunted small. But I'm accurate like a pica, I know the capital of Nepal. I'm the nemesis of error, dreadful diction fears my skills, more inquisitive than Jim Lehrer, snottier than Beverly Hills. -- I.L.O.P. Secret Rap http://goats.com/archive/020830.html
Closing this out as its unlikely to happen in Test::More and I'm cleaning up the RT queue. Sorry.