Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3305 from sam09/fix3300
Browse files Browse the repository at this point in the history
Add Handling Negative Duration in Duration Calculator
  • Loading branch information
GuiltyDolphin authored Jun 29, 2016
2 parents 77baf9f + 911a556 commit 706ac4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/DDG/Goodie/DurationCalculator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ sub get_result {
my ($values1, $values2, $action) = @_;

return format_result ($values1 + $values2) if $action eq '+';
return format_result ($values1 - $values2) if $action eq '-';

my $subtract = format_result ($values1 - $values2) if $action eq '-';
return "-($subtract)" if DateTime::Duration->compare($values1, $values2) < 0;
return $subtract;
}

handle query_lc => sub {
Expand Down
20 changes: 18 additions & 2 deletions t/DurationCalculator.t
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ my $sub_query_minus = "2 days minus 43 hours";
my $value_test = "1 second - 1000000000 nanoseconds";
my $month_test = "4 weeks + 2 days";
my $year_test = "23 months + 30 days";
my $invalid_query = "2 day 2 day + 4 day";
my $neg_query1 = "1 year - 400 days";
my $neg_query2 = "1 day + -24 hours";
my $neg_query3 = "-1 week - 3 days";
my $neg_query4 = "-1 week + 7 days";
my $invalid_query1 = "2 day 2 day + 4 day";
my $invalid_query2 = "1 day + 1 hay";
my $invalid_query3 = "-54minutes - 39";
my $invalid_query4 = "4 hours";


ddg_goodie_test(
[qw( DDG::Goodie::DurationCalculator )],

Expand All @@ -49,7 +58,14 @@ ddg_goodie_test(
$value_test => build_test("no time", $value_test),
$month_test => build_test("1 month", $month_test),
$year_test => build_test("2 years", $year_test),
$invalid_query => undef,
$neg_query1 => build_test("-(1 month and 3 days)", $neg_query1),
$neg_query2 => build_test("no time", $neg_query2),
$neg_query3 => build_test("-(1 week and 3 days)", $neg_query3),
$neg_query4 => build_test("no time", $neg_query4),
$invalid_query1 => undef,
$invalid_query3 =>undef,
$invalid_query2 =>undef,
$invalid_query4 =>undef,
);

done_testing;

0 comments on commit 706ac4d

Please sign in to comment.