00-roles.t: use MockTime for relative time testing.

In order to maintain clean code for actual use, we're not doing time
injection here.
master
Matt Miller 2014-08-15 08:37:05 -04:00
parent dcb3c69643
commit 9b45531bfb
2 changed files with 36 additions and 14 deletions

View File

@ -76,6 +76,7 @@ Math::SigFigs = 1.09
Bit::Vector = 7.3 Bit::Vector = 7.3
[Prereqs / TestRequires] [Prereqs / TestRequires]
Test::MockTime = 0
Test::More = 0.98 Test::More = 0.98
Test::Most = 0 Test::Most = 0
Test::Dirs = 0.03 Test::Dirs = 0.03

View File

@ -2,7 +2,10 @@
use strict; use strict;
use warnings; use warnings;
use Test::MockTime qw( :all );
use Test::Most; use Test::Most;
use DateTime; use DateTime;
subtest 'NumberStyler' => sub { subtest 'NumberStyler' => sub {
@ -238,22 +241,40 @@ subtest 'Dates' => sub {
} }
}; };
subtest 'Vague strings' => sub { subtest 'Vague strings' => sub {
my %strings = ( #TODO: figure out how to know which year is correct without reimplementing the code here. my %time_strings = (
'next december' => qr/^01 Dec \d{4}$/, '2000-08-01T00:00:00Z' => {
'last january' => qr/^01 Jan \d{4}$/, 'next december' => '01 Dec 2000',
'june' => qr/^01 Jun \d{4}$/, 'last january' => '01 Jan 2000',
'december 2015' => qr/^01 Dec 2015$/, 'june' => '01 Jun 2001',
'june 2000' => qr/^01 Jun 2000$/, 'december 2015' => '01 Dec 2015',
'jan' => qr/^01 Jan \d{4}$/, # Is Jan too ambiguous (e.g. Jan Hammer)? 'june 2000' => '01 Jun 2000',
'next jan' => qr/^01 Jan \d{4}$/, 'jan' => '01 Jan 2001',
'last jan' => qr/^01 Jan \d{4}$/, 'next jan' => '01 Jan 2001',
'feb 2038' => qr/^01 Feb 2038$/, 'last jan' => '01 Jan 2000',
'feb 2038' => '01 Feb 2038',
},
'2015-12-01T00:00:00Z' => {
'next december' => '01 Dec 2016',
'last january' => '01 Jan 2015',
'june' => '01 Jun 2016',
'december 2015' => '01 Dec 2015',
'june 2000' => '01 Jun 2000',
'jan' => '01 Jan 2016',
'next jan' => '01 Jan 2016',
'last jan' => '01 Jan 2015',
'feb 2038' => '01 Feb 2038',
},
); );
foreach my $test_date (sort keys %strings) { foreach my $query_time (sort keys %time_strings) {
my $result = RoleTester::parse_vague_string_to_date($test_date); set_fixed_time($query_time);
isa_ok($result, 'DateTime', $test_date); my %strings = %{$time_strings{$query_time}};
like(RoleTester::date_output_string($result), $strings{$test_date}, $test_date); foreach my $test_date (sort keys %strings) {
my $result = RoleTester::parse_vague_string_to_date($test_date);
isa_ok($result, 'DateTime', $test_date);
is(RoleTester::date_output_string($result), $strings{$test_date}, $test_date . ' relative to ' . $query_time);
}
} }
restore_time();
}; };
}; };