File: 006_locales.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 (122 lines) | stat: -rw-r--r-- 3,529 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!perl -w

# t/002_basic.t - check module dates in various formats

use Test::More 0.88;

#use Test::More qw/no_plan/;
use DateTime::Format::Strptime;
use DateTime;

my @locales = qw/en ga pt de/;

#diag("\nChecking Day Names");
my $pattern = "%Y-%m-%d %A";
foreach my $locale (@locales) {
    foreach my $day ( 1 .. 7 ) {
        my $dt = DateTime->now( locale => $locale )->set( day => $day );
        my $input = $dt->strftime($pattern);
        eval {
            $strptime = DateTime::Format::Strptime->new(
                pattern  => $pattern,
                locale   => $locale,
                on_error => 'croak',
            );
        };
        ok( $@ eq '', "Constructor with Day Name" );

        my $parsed;
        eval { $parsed = $strptime->parse_datetime($input); } unless $@;
        diag("[$@]") if $@ ne '';
        ok( $@ eq '', "Parsed with Day Name" );

        is( $parsed->strftime($pattern), $input, "Matched with Day Name" );
    }

    #	diag( $locale );
}

#diag("\nChecking Month Names");
$pattern = "%Y-%m-%d %B";
foreach my $locale (@locales) {
    foreach my $month ( 1 .. 12 ) {
        my $dt = DateTime->now( locale => $locale )
            ->set( month => $month, day => 20 );
        my $input = $dt->strftime($pattern);
        eval {
            $strptime = DateTime::Format::Strptime->new(
                pattern  => $pattern,
                locale   => $locale,
                on_error => 'croak',
            );
        };
        ok( $@ eq '', "Constructor with Month Name" );

        my $parsed;
        eval { $parsed = $strptime->parse_datetime($input); } unless $@;
        diag("[$@]") if $@ ne '';
        ok( $@ eq '', "Parsed with Month Name" );

        is( $parsed->strftime($pattern), $input, "Matched with Month Name" );
    }

    #	diag( $locale );
}

#diag("\nChecking AM/PM tokens");
$pattern = "%Y-%m-%d %H:%M %p";
foreach my $locale (@locales) {
    foreach my $hour ( 11, 12 ) {
        my $dt = DateTime->now( locale => $locale )->set( hour => $hour );
        my $input = $dt->strftime($pattern);
        eval {
            $strptime = DateTime::Format::Strptime->new(
                pattern  => $pattern,
                locale   => $locale,
                on_error => 'croak',
            );
        };
        ok( $@ eq '', "Constructor with Meridian" );

        my $parsed;
        eval { $parsed = $strptime->parse_datetime($input); } unless $@;
        diag("[$@]") if $@ ne '';
        ok( $@ eq '', "Parsed with Meridian" );

        is( $parsed->strftime($pattern), $input, "Matched with Meridian" );
    }

    #	diag( $locale );
}

#diag("\nChecking format_datetime honors strptime's locale rather than the dt's");
{

    # Create a parser that has locale 'fr'
    my $dmy_format = new DateTime::Format::Strptime(
        pattern => '%d/%m/%Y',
        locale  => 'fr'
    );
    is( $dmy_format->locale, 'fr' );

    # So, therefore, will a $dt created using it.
    my $dt = $dmy_format->parse_datetime('03/08/2004');
    is( $dt->locale->id, 'fr' );

    # Now we create a new strptime for formatting, but in a different locale
    my $pt_format = new DateTime::Format::Strptime(
        pattern => '%B/%Y',
        locale  => 'pt'
    );
    is( $pt_format->locale, 'pt' );

    my $string = $pt_format->format_datetime($dt);

    # Make sure the format honored the locale in the strptime
    is( $string, "agosto/2004" );

    # Make sure the datetime, however, retained its own locale
    is( $dt->locale->id, 'fr' )
}

done_testing();