GithubHelp home page GithubHelp logo

worthmine / string-iscomparable-withnum Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 14 KB

to ignore difference between '==' and 'eq' in Perl

License: Other

Perl 100.00%
perl equality compare-strings

string-iscomparable-withnum's Introduction

NAME

String::isComparable::WithNum - It's a test implement to ignore the difference between <=> and cmp

SYNOPSIS

use String::isComparable::WithNum;
my $str = String::isComparable::WithNum->new( value => 'strings' );

say q|Succeeded in comparing with strings by 'eq'| if $str eq 'strings';            
say q|Succeeded in comparing with Int by 'eq'| if $str eq 100;            
say q|Succeeded in comparing with Int by '=='|     if $str == 100;
say q|Succeeded in comparing with strings by '=='| if $str == 'strings';

DESCRIPTION

I have solved The Issue#1

LICENSE

Copyright (C) worthmine.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

worthmine [email protected]

string-iscomparable-withnum's People

Contributors

worthmine avatar

Stargazers

 avatar

Watchers

 avatar  avatar

string-iscomparable-withnum's Issues

where is my mistake?

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.