start of stats saver code
[engit/Iron-Munger.git] / t / calculate.t
1 use strict;
2 use warnings;
3 use Test::More qw(no_plan);
4
5 BEGIN {
6   use_ok 'IronMunger::Post';
7   use_ok 'IronMunger::Calculate';
8 }
9
10 use autobox;
11 use autobox::DateTime::Duration;
12 use signatures;
13
14 sub named_eq ($test_name, $sub_name, $expected, @posts) {
15   my $sub_ref = IronMunger::Calculate->can($sub_name);
16   die "Couldn't find $sub_name in IronMunger::Calculate" unless $sub_ref;
17   my $sub_description = join(' ', split '_', $sub_name);
18   cmp_ok(
19     $sub_ref->(@posts), '==', $expected,
20     "${test_name}: ${expected} ${sub_description}",
21   );
22 }
23
24 sub case ($name_spec, $case, $expect) {
25   my $name = join(' ', map ucfirst, split '_', $name_spec);
26   my @posts = map {
27     IronMunger::Post->new(url => 'http://localhost', at => $_->days->ago)
28   } @$case;
29   foreach my $test (sort keys %$expect) {
30     named_eq($name, $test, $expect->{$test}, @posts);
31   }
32 }
33
34 sub expect (%expect) {
35   +{
36     successful_sequential_posts => $expect{sequential},
37     days_remaining_to_post => $expect{remaining},
38   };
39 }
40
41 case two_posts_ok =>
42   [ 5, 13 ],
43   expect
44     sequential => 2,
45     remaining => 5;
46
47 case two_posts_too_far_apart =>
48   [ 5, 20 ],
49   expect
50     sequential => 1,
51     remaining => 5;
52
53 case five_posts_ok_last_needed =>
54   [ 4, 11, 18, 25, 32 ],
55   expect
56     sequential => 5,
57     remaining => 6;
58
59 case five_posts_ok_aperture_needed =>
60   [ 4, 11, 18, 28, 32 ],
61   expect
62     sequential => 5,
63     remaining => 4;
64
65 case five_posts_gap =>
66   [ 4, 11, 23, 25, 32 ],
67   expect
68     sequential => 2,
69     remaining => 6;
70
71 case five_posts_aperture =>
72   [ 1, 11, 21, 31, 41 ],
73   expect
74     sequential => 4,
75     remaining => 1;
76
77 case five_posts_aperture_2 =>
78   [ 4, 14, 24, 34, 44 ],
79   expect
80     sequential => 3,
81     remaining => 6;
82
83 case five_posts_aperture_2 =>
84   [ 4, 13, 22, 31, 40 ],
85   expect
86     sequential => 4,
87     remaining => 1;