From c271c7c66077994ba89c45fca00612c9517747bf Mon Sep 17 00:00:00 2001 From: puskin94 Date: Fri, 9 Jan 2015 18:32:40 +0100 Subject: [PATCH] Fixed PercentOf.t and minor bugs on .pm file --- lib/DDG/Goodie/PercentOf.pm | 49 +++++++++++++++++++++++++++++++++++++ t/PercentOf.t | 33 +++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 lib/DDG/Goodie/PercentOf.pm create mode 100644 t/PercentOf.t diff --git a/lib/DDG/Goodie/PercentOf.pm b/lib/DDG/Goodie/PercentOf.pm new file mode 100644 index 000000000..34e9dfdd4 --- /dev/null +++ b/lib/DDG/Goodie/PercentOf.pm @@ -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; + diff --git a/t/PercentOf.t b/t/PercentOf.t new file mode 100644 index 000000000..e29af4884 --- /dev/null +++ b/t/PercentOf.t @@ -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;