check both days remaining types
[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 { IronMunger::Post->new(at => $_->days->ago) } @$case;
27   foreach my $test (sort keys %$expect) {
28     named_eq($name, $test, $expect->{$test}, @posts);
29   }
30 }
31
32 sub expect (%expect) {
33   +{
34     successful_sequential_posts => $expect{sequential},
35     days_remaining_to_post => $expect{remaining},
36   };
37 }
38
39 case two_posts_ok =>
40   [ 5, 13 ],
41   expect
42     sequential => 2,
43     remaining => 5;
44
45 case two_posts_too_far_apart =>
46   [ 5, 20 ],
47   expect
48     sequential => 1,
49     remaining => 5;
50
51 case five_posts_ok_last_needed =>
52   [ 4, 11, 18, 25, 32 ],
53   expect
54     sequential => 5,
55     remaining => 6;
56
57 case five_posts_ok_aperture_needed =>
58   [ 4, 11, 18, 28, 32 ],
59   expect
60     sequential => 5,
61     remaining => 4;