Lingua-EN-Inflect

 view release on metacpan or  search on metacpan

t/numwords.t  view on Meta::CPAN


# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

BEGIN { $| = 1; print "1..960\n"; }
END {print "not ok 1\n" unless $loaded;}
use Lingua::EN::Inflect qw( :ALL );
$loaded = 1;
print "ok 1\n";

my $count = 2;
sub ok($;$$)
{
    my $ok = $_[0];
    print "not " unless $ok;
    print "ok $count";
    print "\t# $_[1]" if $_[1];
    print " -> $_[2]" if $_[2];
    print "\n";
    $count++;
    return $ok;
}

sub is ($$;$$) 
{
    my $match = $_[0] eq $_[1];
    ok($match, $_[2], $_[3]);
    if (!$match) {
        print '# Expected: ' . $_[1]. "\n";
        print '# but got:  ' . $_[0]. "\n";
    }
}

sub diag {
    print "# @_\n";
}

######################### End of black magic.

sub test_eq($$)
{
    PL_eq($_[0],$_[1])    ||
    PL_N_eq($_[0],$_[1])  ||
    PL_V_eq($_[0],$_[1])  ||
    PL_ADJ_eq($_[0],$_[1]);
}


for my $thresh (0..20) {
    for my $n (0..20) {
        my $threshed = NUMWORDS($n, threshold=>$thresh);
        my $numwords = NUMWORDS($n);

        if ($n <= $thresh) {
            ok ( $numwords eq $threshed => "Wordified $n (<= $thresh)");
        }
        else {
            $threshed =~ s/\D//gxms;
            ok ( $threshed == $n => "Preserved $n (above $thresh)");
        }
    }
}

ok NUMWORDS(    999, threshold=>500) eq      '999',     ' 999 -> 999';
ok NUMWORDS(   1000, threshold=>500) eq    '1,000',     '1000 -> 1,000';
ok NUMWORDS(  10000, threshold=>500) eq   '10,000',    '10000 -> 10,000';
ok NUMWORDS( 100000, threshold=>500) eq  '100,000',   '100000 -> 100,000';
ok NUMWORDS(1000000, threshold=>500) eq '1,000,000', '1000000 -> 1,000,000';

ok NUMWORDS(    999.3, threshold=>500) eq      '999.3',     ' 999.3 -> 999.3';
ok NUMWORDS(   1000.3, threshold=>500) eq    '1,000.3',     '1000.3 -> 1,000.3';
ok NUMWORDS(  10000.3, threshold=>500) eq   '10,000.3',    '10000.3 -> 10,000.3';



( run in 0.955 second using v1.01-cache-2.11-cpan-5e09290becf )