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: 41109
Status: resolved
Priority: 0/
Queue: Test-Simple

People
Owner: Nobody in particular
Requestors: TONVOON [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.86
Fixed in: (no value)



Subject: cmp_ok fails with overloaded stringification
The "non-overloaded" changes in 0.85_01 (from http://rt.cpan.org/Ticket/Display.html? id=24186) broke the tests in Nagios::Plugins, but the fix was simple (to overload "eq" as well as ""). However, have noticed that cmp_ok fails if the stringified value is 0. Also, there was a typo with the stringified check, which means the stringification tests fail. I've attached a newer t/overload.t script, with additional tests where the stringified value is 0, and the typo is fixed.
Subject: overload.t
#!/usr/bin/perl -w # $Id$ BEGIN { if( $ENV{PERL_CORE} ) { chdir 't'; @INC = ('../lib', 'lib'); } else { unshift @INC, 't/lib'; } } use strict; use Test::More tests => 15; package Overloaded; use overload q{eq} => sub { $_[0]->{string} }, q{==} => sub { $_[0]->{num} }, q{""} => sub { $_[0]->{stringify}++; $_[0]->{string} }, q{0+} => sub { $_[0]->{numify}++; $_[0]->{num} } ; sub new { my $class = shift; bless { string => shift, num => shift, stringify => 0, numify => 0, }, $class; } package main; local $SIG{__DIE__} = sub { my($call_file, $call_line) = (caller)[1,2]; fail("SIGDIE accidentally called"); diag("From $call_file at $call_line"); }; # String with value = 0 appears to be a special case my $obj = Overloaded->new('0', 42); isa_ok $obj, 'Overloaded'; is $obj, '0', 'is() with string overloading'; cmp_ok $obj, 'eq', '0', 'cmp_ok() ...'; is $obj->{stringify}, 0, 'cmp_ok() eq does not stringify'; $obj = Overloaded->new('foo', 42); isa_ok $obj, 'Overloaded'; is $obj, 'foo', 'is() with string overloading'; cmp_ok $obj, 'eq', 'foo', 'cmp_ok() ...'; is $obj->{stringify}, 0, 'cmp_ok() eq does not stringify'; cmp_ok $obj, '==', 42, 'cmp_ok() with number overloading'; is $obj->{numify}, 0, 'cmp_ok() == does not numify'; is_deeply [$obj], ['foo'], 'is_deeply with string overloading'; ok eq_array([$obj], ['foo']), 'eq_array ...'; ok eq_hash({foo => $obj}, {foo => 'foo'}), 'eq_hash ...'; # rt.cpan.org 13506 is_deeply $obj, 'foo', 'is_deeply with string overloading at the top'; Test::More->builder->is_num($obj, 42); Test::More->builder->is_eq ($obj, "foo"); { # rt.cpan.org 14675 package TestPackage; use overload q{""} => sub { ::fail("This should not be called") }; package Foo; ::is_deeply(['TestPackage'], ['TestPackage']); ::is_deeply({'TestPackage' => 'TestPackage'}, {'TestPackage' => 'TestPackage'}); ::is_deeply('TestPackage', 'TestPackage'); }
On Thu Nov 20 20:33:17 2008, TONVOON wrote: Show quoted text
> The "non-overloaded" changes in 0.85_01 (from > http://rt.cpan.org/Ticket/Display.html? > id=24186) broke the tests in Nagios::Plugins, but the fix was simple > (to overload "eq" as well as > ""). > > However, have noticed that cmp_ok fails if the stringified value is 0. > > Also, there was a typo with the stringified check, which means the > stringification tests fail.
Thanks for the typo fix. The fact that 0 doesn't work is just an artifact of the silly way eq and == are implemented in the Overload test class. They don't actually do a comparison, they just use the stringified or numified value, which is stupid. I'm not sure why I wrote them that way. Anyhow, no real bug but I've improved the tests. http://github.com/schwern/test-more/commit/01ceb34bf2b6b43fdfc3f365662b338f97b87277