1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
use strict;
use Test::More;
BEGIN { plan tests => 5 }
use lib qw(..);
use Data::Random qw( rand_date );
# Try to load Date::Calc
eval q{ use Date::Calc };
SKIP: {
# If the module cannot be loaded, skip tests
skip('Date::Calc not installed', 5) if $@;
# Get today's date
my ( $year, $month, $day ) = Date::Calc::Today();
# Test default w/ no params -- should return a date between today and 1 year from now
{
my $pass = 1;
my $max_days =
Date::Calc::Delta_Days( $year, $month, $day,
Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
my $i = 0;
while ( $pass && $i < $max_days ) {
my $date = rand_date();
my $delta =
Date::Calc::Delta_Days( $year, $month, $day, split ( /\-/, $date ) );
$pass = 0 unless $delta >= 0 && $delta <= $max_days;
$i++;
}
ok($pass);
}
# Test min option
{
my $pass = 1;
my $max_days = Date::Calc::Delta_Days( 1978, 9, 21, 1979, 9, 21 );
my $i = 0;
while ( $pass && $i < $max_days ) {
my $date = rand_date( min => '1978-9-21' );
my $delta =
Date::Calc::Delta_Days( 1978, 9, 21, split ( /\-/, $date ) );
$pass = 0 unless $delta >= 0 && $delta <= $max_days;
$i++;
}
ok($pass);
}
# Test max option
{
my $pass = 1;
my $max_days =
Date::Calc::Delta_Days( $year, $month, $day,
Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
my $i = 0;
while ( $pass && $i < $max_days ) {
my $date =
rand_date( max =>
join ( '-',
Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) ) );
my $delta =
Date::Calc::Delta_Days( $year, $month, $day, split ( /\-/, $date ) );
$pass = 0 unless $delta >= 0 && $delta <= $max_days;
$i++;
}
ok($pass);
}
# Test min + max options
{
my $pass = 1;
my $max_days =
Date::Calc::Delta_Days( $year, $month, $day,
Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
my $i = 0;
while ( $pass && $i < $max_days ) {
my $date = rand_date(
min => "$year-$month-$day",
max =>
join ( '-',
Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) )
);
my $delta =
Date::Calc::Delta_Days( $year, $month, $day, split ( /\-/, $date ) );
$pass = 0 unless $delta >= 0 && $delta <= $max_days;
$i++;
}
ok($pass);
}
# Test min + max options using "now"
{
my $pass = 1;
my $date = rand_date( min => 'now', max => 'now' );
my ( $new_year, $new_month, $new_day ) = split ( /\-/, $date );
$pass = 0
unless $new_year == $year && $new_month == $month && $new_day == $day;
ok($pass);
}
}
|