Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Sep 10, 2020
1 parent fa23b45 commit 024937e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/DateTime/Format/Genealogy.pm
Expand Up @@ -103,7 +103,6 @@ sub parse_datetime {
$params{'date'} = shift;
}
my $quiet = $params{'quiet'};
my $strict = $params{'strict'};

if(my $date = $params{'date'}) {
# TODO: Needs much more sanity checking
Expand All @@ -130,8 +129,11 @@ sub parse_datetime {
}
return;
}

if($date !~ /^\d{3,4}$/) {
if($date =~ /^(\d{1,2})\s+([A-Z]{4,}+)\s+(\d{3,4})$/i) {
my $strict = $params{'strict'};

if((!$strict) && (my $abbrev = $months{$2})) {
$date = "$1 $abbrev $3";
} else {
Expand All @@ -142,10 +144,11 @@ sub parse_datetime {
if(($date =~ /^\d/) && (my $d = $self->_date_parser_cached($date))) {
return $dfn->parse_datetime($d->{'canonical'});
}
if(($date !~ /^(Abt|ca?)/i) && ($date =~ /^[\w\s]+$/)) {
if(($date !~ /^(Abt|ca?)/i) && ($date =~ /^[\w\s,]+$/)) {
# ACOM exports full month names and non-standard format dates e.g. U.S. format MMM, DD YYYY
# TODO: allow that when mot in strict mode
# TODO: allow that when not in strict mode
if(my $rc = $dfn->parse_datetime($date)) {
# This pretty much always succeeds even with total garbage input
return $rc;
}
Carp::croak("Can't parse date '$date'");
Expand Down
10 changes: 9 additions & 1 deletion t/error.t
Expand Up @@ -12,19 +12,27 @@ ERROR: {
if($@) {
plan(skip_all => 'Test::Carp needed to check error messages');
} else {
plan(tests => 12);
plan(tests => 14);

my $f = new_ok('DateTime::Format::Genealogy');
does_carp_that_matches(sub { $f->parse_datetime('29 SepX 1939') }, qr/^Unparseable date/);
does_carp_that_matches(sub { $f->parse_datetime('31 Nov 1939') }, qr/^31 Nov 1939/);
does_croak_that_matches(sub { $f->parse_datetime(['29 Sep 1939']) }, qr/^Usage:/);
does_croak_that_matches(sub { $f->parse_datetime({ datex => '30 Sep 1939' }) }, qr/^Usage:/);
does_carp_that_matches(sub { $f->parse_datetime('Bef 29 Sep 1939') }, qr/invalid/);
does_carp_that_matches(sub { $f->parse_datetime(date => 'bef 29 Sep 1939', strict => 1) }, qr/need an exact date/);
does_carp_that_matches(sub { $f->parse_datetime('Aft 1 Jan 2000') }, qr/invalid/);
is($f->parse_datetime(date => 'Aft 1 Jan 2000', quiet => 1), undef, 'quiet does not carp');
does_croak_that_matches(sub { $f->parse_datetime() }, qr/^Usage:/);
does_croak_that_matches(sub { $f->parse_datetime(date => undef) }, qr/^Usage:/);
does_carp_that_matches(sub { $f->parse_datetime({ date => '28 Jul 1914 - 11 Nov 1918' }) }, qr/Changing date/);
does_carp_that_matches(sub { $f->parse_datetime(date => '12 June 2020', strict => 1) }, qr/^Unparseable date/);

# Note that this always seems to succeed, see the comment in the source code
TODO: {
local $TODO = 'Always passes, should fail - see DateTime::Format::Natural';

ok(!defined($f->parse_datetime(date => 'Zzz 55, 2020', strict => 1)));
}
}
}

0 comments on commit 024937e

Please sign in to comment.