From: Matt S Trout Date: Thu, 14 May 2009 23:31:53 +0000 (+0100) Subject: handle failure cases better X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e1738c748aad894bc7bc6d637ece45916ffb98f4;p=engit%2FIron-Munger.git handle failure cases better --- diff --git a/lib/IronMunger/Calculate.pm b/lib/IronMunger/Calculate.pm index 451a26b..1e7c515 100644 --- a/lib/IronMunger/Calculate.pm +++ b/lib/IronMunger/Calculate.pm @@ -22,7 +22,12 @@ sub day_diff ($dt1, $dt2) { sub check_post_gap ($aperture, $days, @posts) { return scalar @posts if @posts <= $aperture; my @next_post = splice(@posts, 0, $aperture); - return 0 if day_diff(DateTime->now, $next_post[-1]) > $days; + if (day_diff(DateTime->now, $next_post[-1]) > $days) { + while (@next_post && day_diff(DateTime->now, $next_post[-1]) > $days) { + pop @next_post; + } + return scalar(@next_post); + } my $success = $aperture; foreach my $post (@posts) { if (day_diff($next_post[0],$post) > $days) { @@ -36,9 +41,12 @@ sub check_post_gap ($aperture, $days, @posts) { } sub check_time_remaining ($aperture, $days, @posts) { - my $cand = (@posts > $aperture ? $posts[$aperture - 1] : $posts[-1]); - my $days_ago = day_diff(DateTime->now, $cand); - return $days - $days_ago; + my @consider = @posts > $aperture ? @posts[0 .. $aperture - 1] : @posts; + foreach my $cand (reverse @consider) { + my $days_ago = day_diff(DateTime->now, $cand); + return $days - $days_ago if $days > $days_ago; + } + return $days; } sub check_both ($check, @posts) { diff --git a/t/calculate.t b/t/calculate.t index 4bc5b09..a4edec5 100644 --- a/t/calculate.t +++ b/t/calculate.t @@ -59,3 +59,27 @@ case five_posts_ok_aperture_needed => expect sequential => 5, remaining => 4; + +case five_posts_gap => + [ 4, 11, 23, 25, 32 ], + expect + sequential => 2, + remaining => 6; + +case five_posts_aperture => + [ 1, 11, 21, 31, 41 ], + expect + sequential => 4, + remaining => 1; + +case five_posts_aperture_2 => + [ 4, 14, 24, 34, 44 ], + expect + sequential => 3, + remaining => 6; + +case five_posts_aperture_2 => + [ 4, 13, 22, 31, 40 ], + expect + sequential => 4, + remaining => 1;