File: 20spanset_week_wrapped_recurrence.t

package info (click to toggle)
libdatetime-set-perl 0.3900-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 356 kB
  • sloc: perl: 3,101; makefile: 2
file content (132 lines) | stat: -rw-r--r-- 3,115 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

use DateTime;
use DateTime::SpanSet;


# Check that SpanSets return Spans with the correct endpoints given
# the same week recurrence as its starting and ending sets.

BEGIN {
    if (eval 'use DateTime::Event::Recurrence; 1') {
        plan tests => 60;
    }
    else {
        plan skip_all => 'DateTime::Event::Recurrence required for this test.';
    }
}


my $recurrence =
    DateTime::Event::Recurrence->weekly(
        days => 1, hours => 8, minutes => 30,
    );

my $base_span_set =
    DateTime::SpanSet
        ->from_sets(start_set => $recurrence, end_set => $recurrence);

my $test_time_zone = 'Australia/Adelaide';

test_end_points(
    $base_span_set,
    'no time zone changes',
    undef,
);
test_end_points(
    $base_span_set
        ->clone()
        ->set_time_zone($test_time_zone),
    'time zone specified',
    $test_time_zone,
);
test_end_points(
    $base_span_set
        ->clone()
        ->set_time_zone('floating')
        ->set_time_zone($test_time_zone),
    'intermediary floating time zone',
    $test_time_zone,
);

sub test_end_points {
    my ($span_set, $name, $time_zone) = @_;

    foreach my $hour (6..7) {
        test_end_points_for_hour($span_set, $name, $time_zone, $hour, 8);
    }
    foreach my $hour (8..10) {
        test_end_points_for_hour($span_set, $name, $time_zone, $hour, 15);
    }

    return;
}


sub test_end_points_for_hour {
    my ($span_set, $name, $time_zone, $hour, $expected_start_day_of_month) = @_;

    my $current_time   = new_test_time(15, $hour, $time_zone);
    my $expected_start =
        new_test_time($expected_start_day_of_month,      8,    $time_zone);
    my $expected_end   =
        new_test_time($expected_start_day_of_month + 7,  8,    $time_zone);

    my $span = $span_set->current($current_time)->span();

    test_span_end_point(
        'start', $name, $span->start(), $expected_start, $current_time,
    );
    test_span_end_point(
        'end',   $name, $span->end(),   $expected_end,   $current_time,
    );

    return;
}


sub new_test_time {
    my ($day_of_month, $hour, $time_zone) = @_;

    my %constructor_arguments = (
        year    => 2008,
        month   => 12,
        day     => $day_of_month,
        hour    => $hour,
        minute  => 30,
    );

    if ($time_zone) {
        $constructor_arguments{time_zone} = $time_zone;
    }

    return DateTime->new(%constructor_arguments);
}


sub test_span_end_point {
    my ($end_point_name, $spanset_name, $end_point, $expected_time, $test_input_time) = @_;

    my $expected_ymd      = $expected_time->ymd();
    my $expected_hms      = $expected_time->hms();
    my $test_input_string =
        $test_input_time->ymd() . q< > . $test_input_time->hms();

    is(
        $end_point->ymd(),
        $expected_ymd,
        "Date for $end_point_name of SpanSet with $spanset_name at $test_input_string.",
    );
    is(
        $end_point->hms(),
        $expected_hms,
        "Time of day for $end_point_name of SpanSet with $spanset_name at $test_input_string.",
    );

    return;
}