File: 20infinite.t

package info (click to toggle)
libdatetime-perl 2%3A1.50-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,504 kB
  • sloc: perl: 2,964; makefile: 3
file content (211 lines) | stat: -rw-r--r-- 4,798 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use strict;
use warnings;

use Test::More;

use DateTime;
use DateTime::Locale;

my $pos        = DateTime::Infinite::Future->new;
my $neg        = DateTime::Infinite::Past->new;
my $posinf     = DateTime::INFINITY;
my $neginf     = DateTime::NEG_INFINITY;
my $nan_string = DateTime::NAN;

# infinite date math
{
    ok( $pos->is_infinite, 'positive infinity should be infinite' );
    ok( $neg->is_infinite, 'negative infinity should be infinite' );
    ok( !$pos->is_finite,  'positive infinity should not be finite' );
    ok( !$neg->is_finite,  'negative infinity should not be finite' );

    # These methods produce numbers or strings - we want to make sure they all
    # return Inf or -Inf as expected.
    my @ification_methods = qw(
        ymd mdy dmy hms time iso8601 datetime
        year ce_year month day day_of_week
        quarter
        hour hour_1 hour_12 hour_12_0 minute second
        fractional_second
        week week_year week_number
        mjd jd
        nanosecond millisecond microsecond
        epoch
    );

    for my $meth (@ification_methods) {
        is( $pos->$meth, $posinf, "+Infinity $meth returns $posinf" );
        is( $neg->$meth, $neginf, "-Infinity $meth returns $neginf" );
    }

    # that's a long time ago!
    my $long_ago = DateTime->new( year => -100_000 );

    ok(
        $neg < $long_ago,
        'negative infinity is really negative'
    );

    my $far_future = DateTime->new( year => 100_000 );
    ok(
        $pos > $far_future,
        'positive infinity is really positive'
    );

    ok(
        $pos > $neg,
        'positive infinity is bigger than negative infinity'
    );

    my $pos_dur = $pos - $far_future;
    ok(
        $pos_dur->is_positive,
        'infinity - normal = infinity'
    );

    my $pos2 = $long_ago + $pos_dur;
    ok(
        $pos2 == $pos,
        'normal + infinite duration = infinity'
    );

    my $neg_dur = $far_future - $pos;
    ok(
        $neg_dur->is_negative,
        'normal - infinity = neg infinity'
    );

    my $neg2 = $long_ago + $neg_dur;
    ok(
        $neg2 == $neg,
        'normal + neg infinite duration = neg infinity'
    );

    my $dur     = $pos - $pos;
    my %deltas  = $dur->deltas;
    my @compare = qw( days seconds nanoseconds );
    foreach (@compare) {

        # NaN != NaN (but should stringify the same)
        is(
            $deltas{$_} . q{}, $nan_string,
            "infinity - infinity = nan ($_)"
        );
    }

    my $new_pos = $pos->clone->add( days => 10 );
    ok(
        $new_pos == $pos,
        'infinity + normal duration = infinity'
    );

    my $new_pos2 = $pos->clone->subtract( days => 10 );
    ok(
        $new_pos2 == $pos,
        'infinity - normal duration = infinity'
    );

    ok(
        $pos == $posinf,
        'infinity (datetime) == infinity (number)'
    );

    ok(
        $neg == $neginf,
        'neg infinity (datetime) == neg infinity (number)'
    );
}

# This could vary across platforms
my $pos_as_string = $posinf . q{};
my $neg_as_string = $neginf . q{};

# formatting
{
    foreach my $m (
        qw( year month day hour minute second
        microsecond millisecond nanosecond )
    ) {
        is(
            $pos->$m() . q{}, $pos_as_string,
            "pos $m is $pos_as_string"
        );

        is(
            $neg->$m() . q{}, $neg_as_string,
            "neg $m is $pos_as_string"
        );
    }
}

{
    my $now = DateTime->now;

    is(
        DateTime->compare( $pos, $now ), 1,
        'positive infinite is greater than now'
    );
    is(
        DateTime->compare( $neg, $now ), -1,
        'negative infinite is less than now'
    );
}

{
    my $now = DateTime->now;
    my $pos2 = $pos + DateTime::Duration->new( months => 1 );

    ok(
        $pos == $pos2,
        'infinity (datetime) == infinity (datetime)'
    );
}

{
    my $now = DateTime->now;
    my $neg2 = $neg + DateTime::Duration->new( months => 1 );

    ok(
        $neg == $neg2,
        '-infinity (datetime) == -infinity (datetime)'
    );
}

{
    cmp_ok(
        "$pos", 'eq', $posinf,
        'stringified infinity (datetime) eq infinity (number)'
    );
    cmp_ok(
        "$neg", 'eq', $neginf,
        'stringified neg infinity (datetime) eq neg infinity (number)'
    );
}

{
    is(
        $pos->day_name(),
        undef,
        'day_name returns undef',
    );

    is(
        $pos->am_or_pm(),
        undef,
        'am_or_pm returns undef'
    );

    is(
        $pos->locale()->name(),
        'Fake locale for Infinite DateTime objects',
        'locale name for fake locale'
    );

    is(
        $pos->locale()->datetime_format_long(),
        DateTime::Locale->load('en_US')->datetime_format_long(),
        'fake locale returns same format as en_US'
    );
}

done_testing();