File: 009_regexp.t

package info (click to toggle)
libdatetime-format-strptime-perl 1.5600-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 284 kB
  • ctags: 15
  • sloc: perl: 1,453; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,644 bytes parent folder | download
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
#!perl -w

# t/009_regexp.t - Patterns as regular expressions

use Test::More 0.88;
use DateTime;
use DateTime::Format::Strptime;

test(
    pattern => qr/%Y-%m-%d/,
    input   => '2009-07-13',
    output  => { year => 2009, month => 7, day => 13 }
);

test(
    pattern => qr/%Y-%m-%d Static Text/,
    input   => '2009-07-13 Static Text',
    output  => { year => 2009, month => 7, day => 13 }
);

test(
    pattern => qr/%Y-%m-%d \w+\s\w+/,
    input   => '2009-07-13 Static Text',
    output  => { year => 2009, month => 7, day => 13 }
);

test(
    pattern => qr/^%Y-%m-%d \w+\s\w+$/,
    input   => '2009-07-13 Static Text',
    output  => { year => 2009, month => 7, day => 13 }
);

eval {
    my $strptime = DateTime::Format::Strptime->new(
        pattern  => qr/^%Y-%m-%d \s+$/,
        on_error => 'croak',
    );
    my $parsed = $strptime->parse_datetime('2009-07-13 Static Text');
};
is( substr( $@, 0, 42 ), "Your datetime does not match your pattern.",
    "The strp pattern is OK, but the regex doesn't match the input." );

sub test {
    my %arg = @_;

    my $strptime = DateTime::Format::Strptime->new(
        pattern    => $arg{pattern}    || '%F %T',
        locale     => $arg{locale}     || 'en',
        time_zone  => $arg{time_zone}  || 'UTC',
        diagnostic => $arg{diagnostic} || 0,
        on_error   => $arg{on_error}   || 'undef',
    );
    isa_ok( $strptime, 'DateTime::Format::Strptime' );

    my $parsed = $strptime->parse_datetime( $arg{input} );
    isa_ok( $parsed, 'DateTime' );

    foreach my $k ( keys %{ $arg{output} } ) {
        is( $parsed->$k, $arg{output}{$k} );
    }
}

done_testing();