File: 07-parse_datetime_future.t

package info (click to toggle)
libdatetime-format-natural-perl 1.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 616 kB
  • sloc: perl: 9,587; makefile: 2
file content (62 lines) | stat: -rwxr-xr-x 1,375 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

use strict;
use warnings;
use boolean qw(true);

use Test::MockTime::HiRes qw(set_fixed_time);
use DateTime;
use DateTime::Format::Natural;
use DateTime::Format::Natural::Test ':set';
use Test::More;

set_fixed_time(
    '02.01.2006 00:00:00',
    '%d.%m.%Y %H:%M:%S',
);

my @simple = (
    { '12am'        => '04.01.2006 00:00:00' },
    { 'monday'      => '09.01.2006 00:00:00' },
    { '2nd january' => '02.01.2007 00:00:00' },
);

_run_tests(3, [ [ \@simple ] ], \&compare);

sub compare
{
    my $aref = shift;

    foreach my $href (@$aref) {
        my $key = (keys %$href)[0];
        foreach my $string ($case_strings->($key)) {
            compare_strings($string, $href->{$key});
        }
    }
}

sub compare_strings
{
    my ($string, $result) = @_;

#   my $parser = DateTime::Format::Natural->new(prefer_future => true); # must FAIL
    my $parser = DateTime::Format::Natural->new(
        datetime => DateTime->new(
            year   => 2006,
            month  => 1,
            day    => 3,
            hour   => 1,
            minute => 0,
            second => 0,
        ),
        prefer_future => true,
    );
    my $dt = $parser->parse_datetime($string);

    if ($parser->success && $parser->_get_truncated) {
        is(_result_string($dt), $result, _message($string));
    }
    else {
        fail(_message($string));
    }
}