test/calendar_event_test.pl: regression tests for PVE::CalendarEvent
[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
111 foreach my $test (@$tests) {
112 my ($t, $expect, $nextsync) = @$test;
113
114 my $timespec;
115 eval { $timespec = PVE::CalendarEvent::parse_calendar_event($t); };
116 my $err = $@;
117
118 if ($expect->{error}) {
119 chomp $err if $err;
120 $timespec = { error => $err } if $err;
121 is_deeply($timespec, $expect, "expect parse error on '$t' - $expect->{error}");
122 die "unable to execute nextsync tests" if $nextsync;
123 } else {
124 is_deeply($timespec, $expect, "parse '$t'");
125 }
126
127 next if !$nextsync;
128
129 foreach my $nt (@$nextsync) {
130 my ($last, $expect_next) = @$nt;
131
132 my $msg = "next event '$t' $last => ${expect_next}";
133 my $next = PVE::CalendarEvent::compute_next_event($timespec, $last, 1);
134 is($next, $expect_next, $msg);
135 }
136 };
137
138 done_testing();