first sketch at calculation test
[engit/Iron-Munger.git] / t / calculate.t
1 use strict;
2 use warnings;
3 use Test::More qw(no_plan);
4
5 use autobox::DateTime::Duration;
6 use signatures;
7
8 BEGIN {
9   use_ok 'IronMunger::Post';
10   use_ok 'IronMunger::Calculate';
11 }
12
13 sub named_eq ($test_name, $sub_name, $expected, @posts) {
14   my $sub_ref = IronMunger::Calculate->can($sub_name);
15   my $sub_description = join(' ', split '_', $sub_name);
16   cmp_ok(
17     $sub_ref->(@posts), '==', $expected,
18     "${test_name}: ${expected} ${sub_description}",
19   );
20 }
21
22 sub case ($name_spec, $case, $expect) {
23   my $name = join(' ', map ucfirst, split '_', $name_spec);
24   my @posts = map { IronMunger::Post->new(at => $_) } @$case;
25   foreach my $test (sort keys %$expect) {
26     named_eq($name, $test, $expect->{$test}, @posts);
27   }
28 }
29
30 sub expect (%expect) {
31   +{
32     successful_sequential_posts => $expect{sequential},
33     days_remaining_to_post => $expect{remaining},
34   };
35 }
36
37 case two_posts_ok =>
38   [ 5->days->ago, 13->days->ago ],
39   expect
40     sequential => 2,
41     remaining => 5;
42
43 case two_posts_too_far_apart =>
44   [ 5->days->ago, 20->days->ago ],
45   expect
46     sequential => 1,
47     remaining => 5;