File: 04base_overflow.t

package info (click to toggle)
libdatetime-incomplete-perl 0.08-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 188 kB
  • sloc: perl: 1,470; makefile: 2
file content (105 lines) | stat: -rw-r--r-- 2,768 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/perl -w

use strict;

use Test::More tests => 12;
use DateTime;
use DateTime::Incomplete;

# Test: require "february" 
{
    my $base = DateTime->new( year => 1970 );

    my $dt_19700131 = $base->clone;
    $dt_19700131->set( month => 1, day => 31 );

    my $dti;
    $dti = DateTime::Incomplete->new( 
        month =>  2,
    );

    my $dt1;
    $dt1 = $dti->next( $dt_19700131 );

    is( $dt1->datetime , '1970-02-01T00:00:00', 
        'next - first date in february is ok' );

    $dt1->set( month => 3, day => 31 );
    eval { $dt1 = $dti->previous( $dt1 ) };
    ok( $dt1 && ( $dt1->datetime eq '1970-02-28T23:59:59' ),
            'previous - last day in february' );
}

# bug reported by Alexandre Masselot
# 29 feb 2009 -> to_datetime fails...
{
    my $dti = DateTime::Incomplete->new( month => 2, day => 29, year=>2000);
    ok($dti->ymd(), "2000-02-29");
    ok($dti->to_datetime(), "29 feb 2000");
}

# Test: require day=30
{
    my $base = DateTime->new( year => 1970 );

    my $dt_19700131 = $base->clone;
    $dt_19700131->set( day => 31 );

    my $dt_19700129 = $base->clone;
    $dt_19700129->set( day => 29 );

    my $dt_19700128 = $base->clone;
    $dt_19700128->set( day => 28 );

    my $dti;
    $dti = DateTime::Incomplete->new(
        day =>  30,
    );

    my $dt1;

    { $dt1 = $dti->next( $dt_19700131 ) };
    is( $dt1->datetime , '1970-03-30T00:00:00',
        'next - skips invalid date (01-31)' );

    { $dt1 = $dti->next( $dt_19700129 ) };
    is( $dt1->datetime , '1970-01-30T00:00:00',
        'next - skips invalid date (01-29)' );

    { $dt1 = $dti->next( $dt_19700128 ) };
    is( $dt1->datetime , '1970-01-30T00:00:00',
        'next - skips invalid date (01-28)' );

    $dt1->set( month => 3, day => 10 );
    $dt1 = $dti->previous( $dt1 );
    is( $dt1->datetime , '1970-01-30T23:59:59',
            'previous - skips invalid date' );

    my $dt_19700220 = $base->clone;
    $dt_19700220->set( month => 2, day => 20 );
    eval { $dt1 = $dti->next( $dt_19700220 ) };
    is( $dt1->datetime , '1970-03-30T00:00:00',
            'next - skips invalid date (02-20)' );

    $dti = DateTime::Incomplete->new(
            month => 2, day =>  30,
    );
    eval { $dt1 = $dti->next( $base ) };
    ok( ! defined $dt1 , 
            'next - invalid incomplete datetime (02-30)' );
    warn "#     ".$dt1->datetime if defined $dt1;

    eval { $dt1 = $dti->previous( $base ) };
    ok( ! defined $dt1 ,
            'previous - invalid incomplete datetime (02-30)' );
    warn "#     ".$dt1->datetime if defined $dt1;

    eval { $dt1 = $dti->closest( $base ) };
    ok( ! defined $dt1 ,
            'closest - invalid incomplete datetime (02-30)' );
    warn "#     ".$dt1->datetime if defined $dt1;

}

1;