Fixed PercentOf.t and minor bugs on .pm file

master
puskin94 2015-01-09 18:32:40 +01:00
parent 4e1eb737ea
commit c271c7c660
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,49 @@
package DDG::Goodie::PercentOf;
# Operations with percentuals
use DDG::Goodie;
zci answer_type => "percent_of";
zci is_cached => 1;
name "PercentOf";
description "Makes Operations with percentuals";
primary_example_queries "4-50%", "349*16%";
code_url "https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/PercentOf/PercentOf.pm";
attribution github => ["puskin94", "puskin"],
twitter => "twitterhandle";
my $result;
triggers query_nowhitespace => qr/\d{1,3}\%$/;
handle query_nowhitespace => sub {
my $input = $_;
return unless $input =~ qr/(\d+)(\+|\*|\/|\-)(\d+)\%/;
if ($2 eq '-') {
$result = ( $1 - (($1 * $3) / 100) );
} elsif ($2 eq '+') {
$result = ( $1 + (($1 * $3) / 100) );
} elsif ($2 eq '*') {
$result = ( $1 * (($1 * $3) / 100) );
} elsif ($2 eq '/') {
$result = ( $1 / (($1 * $3) / 100) );
}
my $text = "Result: $result";
return $text,
structured_answer => {
input => [$input],
operation => 'Calculate',
result => $result
};
};
1;

33
t/PercentOf.t Normal file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "percent_of";
zci is_cached => 1;
ddg_goodie_test(
[qw( DDG::Goodie::PercentOf )],
'4+50%' => test_zci('Result: 6',
structured_answer => {
input => ["4+50%"],
operation => "Calculate",
result => 6
}),
'456+120%' => test_zci('Result: 1003.2',
structured_answer => {
input => ["456+120%"],
operation => "Calculate",
result => 1003.2
}),
'urldecode hello%20there' => undef,
'34$+16' => undef,
'12+5t%' => undef
);
done_testing;