Subject: | cmp_ok not using overloaded '==' |
The documentation for Test::More gives the code sample:
# ok( $this == $that );
cmp_ok( $this, '==', $that, 'this == that' );
However, it seems that cmp_ok really does something more like
ok( $this+0 == $that+0 );
For example, if $this and $that overload '==' and '0+' in certain ways,
the result of cmp_ok with '==' can be different from ok with '=='.
The attached test demonstrates the problem.
I've tested the problem with version 0.62, 0.64 and 0.66. This happens
on Linux with Perl 5.8.8
Subject: | test.pl |
use strict;
use warnings;
use Test::More tests => 2;
my $foo = Thing->new(1);
my $bar = Thing->new(3);
ok( $foo == $bar, '$foo == $bar' );
cmp_ok( $foo, '==', $bar, q{cmp_ok($foo, '==', $bar)} );
package Thing;
use overload
'==' => \&numeric_equality,
'0+' => \&numeric
;
sub new {
my ($class, $value) = @_;
bless \$value, $class;
}
sub numeric_equality {
my ($self, $other) = @_;
return 1;
}
sub numeric {
my ($self, $other) = @_;
return $$self;
}