calendarevent: check range for start also without repetition
[pve-common.git] / test / calendar_event_test.pl
1 #!/usr/bin/perl
2
3 use lib '../src';
4 use strict;
5 use warnings;
6 use Data::Dumper;
7 use Time::Local;
8 use Test::More;
9
10 use PVE::CalendarEvent;
11
12 my $alldays = [0,1,2,3,4,5,6];
13 my $tests = [
14 [
15 '*',
16 { h => '*', m => '*', dow => $alldays },
17 [
18 [0, 60],
19 [30, 60],
20 [59, 60],
21 [60, 120],
22 ]
23 ],
24 [
25 '*/10',
26 { h => '*', m => [0, 10, 20, 30, 40, 50], dow => $alldays },
27 [
28 [0, 600],
29 [599, 600],
30 [600, 1200],
31 [50*60, 60*60]
32 ]
33 ],
34 [
35 '*/12:0' ,
36 { h => [0, 12], m => [0], dow => $alldays },
37 [
38 [ 10, 43200],
39 [ 13*3600, 24*3600],
40 ]
41 ],
42 [
43 '1/12:0/15' ,
44 { h => [1, 13], m => [0, 15, 30, 45], dow => $alldays },
45 [
46 [0, 3600],
47 [3600, 3600+15*60],
48 [3600+16*60, 3600+30*60 ],
49 [3600+30*60, 3600+45*60 ],
50 [3600+45*60, 3600+12*3600],
51 [13*3600 + 1, 13*3600+15*60],
52 [13*3600 + 15*60, 13*3600+30*60],
53 [13*3600 + 30*60, 13*3600+45*60],
54 [13*3600 + 45*60, 25*3600],
55 ],
56 ],
57 [
58 '1,4,6',
59 { h => '*', m => [1, 4, 6], dow => $alldays},
60 [
61 [0, 60],
62 [60, 4*60],
63 [4*60+60, 6*60],
64 [6*60, 3600+60],
65 ]
66 ],
67 [
68 '0..3',
69 { h => '*', m => [ 0, 1, 2, 3 ], dow => $alldays },
70 ],
71 [
72 '23..23:0..3',
73 { h => [ 23 ], m => [ 0, 1, 2, 3 ], dow => $alldays },
74 ],
75 [
76 'Mon',
77 { h => [0], m => [0], dow => [1] },
78 [
79 [0, 4*86400], # Note: Epoch 0 is Thursday, 1. January 1970
80 [4*86400, 11*86400],
81 [11*86400, 18*86400],
82 ],
83 ],
84 [
85 'sat..sun',
86 { h => [0], m => [0], dow => [0, 6] },
87 [
88 [0, 2*86400],
89 [2*86400, 3*86400],
90 [3*86400, 9*86400],
91 ]
92 ],
93 [
94 'sun..sat',
95 { h => [0], m => [0], dow => $alldays },
96 ],
97 [
98 'Fri..Mon',
99 { error => "wrong order in range 'Fri..Mon'" },
100 ],
101 [
102 'wed,mon..tue,fri',
103 { h => [0], m => [0], dow => [ 1, 2, 3, 5] },
104 ],
105 [
106 'mon */15',
107 { h => '*', m => [0, 15, 30, 45], dow => [1]},
108 ],
109 [
110 '22/1:0',
111 { h => [22, 23], m => [0], dow => $alldays },
112 [
113 [0, 22*60*60],
114 [22*60*60, 23*60*60],
115 [22*60*60 + 59*60, 23*60*60]
116 ],
117 ],
118 [
119 '*/2:*',
120 { h => [0,2,4,6,8,10,12,14,16,18,20,22], m => '*', dow => $alldays },
121 [
122 [0, 60],
123 [60*60, 2*60*60],
124 [2*60*60, 2*60*60 + 60]
125 ]
126 ],
127 [
128 '20..22:*/30',
129 { h => [20,21,22], m => [0,30], dow => $alldays },
130 [
131 [0, 20*60*60],
132 [20*60*60, 20*60*60 + 30*60],
133 [22*60*60 + 30*60, 44*60*60]
134 ]
135 ],
136 [
137 '61',
138 { error => "value '61' out of range" },
139 ],
140 [
141 '*/61',
142 { error => "repetition '61' out of range" },
143 ],
144 [
145 '0..80',
146 { error => "range end '80' out of range" },
147 ],
148 ];
149
150 foreach my $test (@$tests) {
151 my ($t, $expect, $nextsync) = @$test;
152
153 my $timespec;
154 eval { $timespec = PVE::CalendarEvent::parse_calendar_event($t); };
155 my $err = $@;
156
157 if ($expect->{error}) {
158 chomp $err if $err;
159 $timespec = { error => $err } if $err;
160 is_deeply($timespec, $expect, "expect parse error on '$t' - $expect->{error}");
161 die "unable to execute nextsync tests" if $nextsync;
162 } else {
163 is_deeply($timespec, $expect, "parse '$t'");
164 }
165
166 next if !$nextsync;
167
168 foreach my $nt (@$nextsync) {
169 my ($last, $expect_next) = @$nt;
170
171 my $msg = "next event '$t' $last => ${expect_next}";
172 my $next = PVE::CalendarEvent::compute_next_event($timespec, $last, 1);
173 is($next, $expect_next, $msg);
174 }
175 };
176
177 done_testing();