worthmine/String-isComparable-WithNum

where is my mistake?

worthmine opened this issue · 0 comments

source code is here:

package String::isComparable::WithNum;
use 5.008001;
use strict;
use warnings;

our $VERSION = "0.01";

use overload (
    '<=>' => \&compareWithNum,
    'cmp' => sub { $_[0]->value() eq $_[1] },
    '""'  => sub { $_[0]->value() },
);
use Mouse;

has value => ( is => 'rw', isa => 'Str' );

__PACKAGE__->meta->make_immutable();
no Mouse;

sub compareWithNum {
    my $self = shift;
    return ( $_[0] ^ $_[0] ) eq '0'
        ? $self->value() == $_[0]
        : $self->value() eq $_[0];
}

1;

the test code is here:

use strict;
use warnings;
use Test::More 0.98 tests => 8;

use lib 'lib';

use_ok('String::isComparable::WithNum');    # 1
my $str = new_ok( 'String::isComparable::WithNum', [ value => 'strings' ] );    # 2

note q|where value is a 'strings'|;
cmp_ok( $str, 'eq', 'strings', q|Succeeded in comparing with strings| );            # 3
cmp_ok( $str, '==', 100,       q|Succeeded in comparing with Int| );                # 4
cmp_ok( $str, '==', 'strings', q|Succeeded in comparing with strings by '=='| );    # 5

$str = String::isComparable::WithNum->new( value => '100' );

note q|where value is an 'Int'|;
cmp_ok( $str, 'eq', '100', q|Succeeded in comparing with strings| );                # 6
cmp_ok( $str, '==', 100,   q|Succeeded in comparing with Int| );                    # 7
cmp_ok( $str, '==', '100', q|Succeeded in comparing with strings by '=='| );        # 8

done_testing;

And result is here:

1..8
ok 1 - use String::isComparable::WithNum;
ok 2 - An object of class 'String::isComparable::WithNum' isa 'String::isComparable::WithNum'
# where value is a 'strings'
not ok 3 - Succeeded in comparing with strings
#   Failed test 'Succeeded in comparing with strings'
#   at t/00_compile.t line 11.
#          got: 'strings'
#     expected: 'strings'
Argument "strings" isn't numeric in numeric eq (==) at lib/String/isComparable/WithNum.pm line 22.
ok 4 - Succeeded in comparing with Int
not ok 5 - Succeeded in comparing with strings by '=='
#   Failed test 'Succeeded in comparing with strings by '==''
#   at t/00_compile.t line 13.
#          got: strings
#     expected: strings
# where value is an 'Int'
not ok 6 - Succeeded in comparing with strings
#   Failed test 'Succeeded in comparing with strings'
#   at t/00_compile.t line 18.
#          got: '100'
#     expected: '100'
not ok 7 - Succeeded in comparing with Int
#   Failed test 'Succeeded in comparing with Int'
#   at t/00_compile.t line 19.
#          got: 100
#     expected: 100
not ok 8 - Succeeded in comparing with strings by '=='
#   Failed test 'Succeeded in comparing with strings by '==''
#   at t/00_compile.t line 20.
#          got: 100
#     expected: 100
# Looks like you failed 5 tests of 8.

Where is my mistake?