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