| 1 |
# time.t |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test::More; |
| 6 |
|
| 7 |
my $mocked_epoch = 1355676244; # "Sun, 16-Dec-2012 16:44:04 GMT" |
| 8 |
|
| 9 |
# The order is important! |
| 10 |
eval { require Test::MockTime; 1; } |
| 11 |
or plan skip_all => 'Test::MockTime not present'; |
| 12 |
|
| 13 |
Test::MockTime::set_fixed_time($mocked_epoch); |
| 14 |
require Dancer2::Core::Time; |
| 15 |
|
| 16 |
my @tests = ( |
| 17 |
[ "1h" => 3600 => "Sun, 16-Dec-2012 17:44:04 GMT" ], |
| 18 |
[ "1 hour" => 3600 => "Sun, 16-Dec-2012 17:44:04 GMT" ], |
| 19 |
[ "+1 hour" => 3600 => "Sun, 16-Dec-2012 17:44:04 GMT" ], |
| 20 |
[ "-1h" => -3600 => "Sun, 16-Dec-2012 15:44:04 GMT" ], |
| 21 |
[ "1 hours" => 3600 => "Sun, 16-Dec-2012 17:44:04 GMT" ], |
| 22 |
|
| 23 |
[ "1d" => ( 3600 * 24 ) => "Mon, 17-Dec-2012 16:44:04 GMT" ], |
| 24 |
[ "1 day" => ( 3600 * 24 ) => "Mon, 17-Dec-2012 16:44:04 GMT" ], |
| 25 |
|
| 26 |
|
| 27 |
); |
| 28 |
|
| 29 |
foreach my $test (@tests) { |
| 30 |
my ( $expr, $secs, $gmt_string ) = @$test; |
| 31 |
|
| 32 |
subtest "Expression: \"$expr\"" => sub { |
| 33 |
my $t = Dancer2::Core::Time->new( expression => $expr ); |
| 34 |
is $t->seconds, $secs, "\"$expr\" is $secs seconds"; |
| 35 |
is $t->epoch, ( $t->seconds + $mocked_epoch ), |
| 36 |
"... its epoch is " . $t->epoch; |
| 37 |
is $t->gmt_string, $gmt_string, |
| 38 |
"... and its GMT string is $gmt_string"; |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
subtest "Forcing another epoch in the object should work" => sub { |
| 43 |
my $t = Dancer2::Core::Time->new( epoch => 1, expression => "1h" ); |
| 44 |
is $t->seconds, 3600, "...1h is still 3600 seconds"; |
| 45 |
is $t->epoch, 1, "... epoch is 1"; |
| 46 |
is $t->gmt_string, 'Thu, 01-Jan-1970 00:00:01 GMT', |
| 47 |
"... and is expressed as Thu, 01-Jan-1970 00:00:01 GMT"; |
| 48 |
}; |
| 49 |
|
| 50 |
subtest "unparsable strings should be kept" => sub { |
| 51 |
for my $t ( |
| 52 |
[ "something silly", "something silly", "something silly" ], |
| 53 |
[ "+2 something", "+2 something", "+2 something" ], |
| 54 |
) |
| 55 |
{ |
| 56 |
my ( $expr, $secs, $gmt ) = @$t; |
| 57 |
my $t = Dancer2::Core::Time->new( expression => $expr ); |
| 58 |
is $t->seconds, $secs, "\"$expr\" is $secs seconds"; |
| 59 |
is $t->epoch, $expr, "... its epoch is $expr"; |
| 60 |
is $t->gmt_string, $gmt, "... and its GMT string is $gmt"; |
| 61 |
} |
| 62 |
}; |
| 63 |
|
| 64 |
done_testing; |