Util-Underscore

 view release on metacpan or  search on metacpan

t/02-numbers.t  view on Meta::CPAN

#!perl

use strict;
use warnings;

use Test::More tests => 5;

use Util::Underscore;

BEGIN {

    package Local::Numeric;

    use overload '0+' => sub {
        my ($self) = @_;
        return $$self;
    };

    sub new {
        my ($class, $val) = @_;
        return bless \$val => $class;
    }
}

subtest '_::is_numeric' => sub {
    plan tests => 6;
    my $numy_good = Local::Numeric->new(42);
    my $numy_bad  = Local::Numeric->new("foo");

    subtest 'positive numbers' => sub {
        plan tests => 7;
        ok _::is_numeric 24,           "positive int";
        ok _::is_numeric 24.2,         "positive float";
        ok _::is_numeric "24",         "positive int string";
        ok _::is_numeric "24.2",       "positive float string";
        ok _::is_numeric "2E12",       "positive engineering notation";
        ok _::is_numeric "-02.12E-01", "positive complicated float";
        ok _::is_numeric "042",        "positive not octal";
    };

    subtest 'positive infinity' => sub {
        plan tests => 6;
        ok _::is_numeric "inf",       "positive inf";
        ok _::is_numeric "-inf",      "positive -inf";
        ok _::is_numeric "Inf",       "positive Inf";
        ok _::is_numeric "-Inf",      "positive -Inf";
        ok _::is_numeric "Infinity",  "positive Infinity";
        ok _::is_numeric "-Infinity", "positive -Infinity";
    };

    subtest 'positive NaN' => sub {
        plan tests => 4;
        ok _::is_numeric "nan",  "positive nan";
        ok _::is_numeric "-nan", "positive -nan";
        ok _::is_numeric "NaN",  "positive NaN";
        ok _::is_numeric "-NaN", "positive -NaN";
    };

    subtest 'negative' => sub {
        plan tests => 6;
        ok !_::is_numeric undef,   "negative undef";
        ok !_::is_numeric '',      "negative empty string";
        ok !_::is_numeric "0xFF",  "negative hex";
        ok !_::is_numeric "42abc", "negative trailing letters";
        ok !_::is_numeric "abc",   "negative letters";
        ok !_::is_numeric [], "negative reference";
    };

    subtest 'overloaded objects' => sub {
        plan tests => 2;
        ok !_::is_numeric $numy_bad, "negative overloaded object";
        ok _::is_numeric $numy_good, "positive overloaded object";
    };

    subtest 'implicit argument' => sub {
        plan tests => 2;
        ok _::is_numeric,  "positive" for 42;
        ok !_::is_numeric, "negative" for undef;
    };
};

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.549 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )