Merge branch 'tips'
commit
82a7b2d643
|
@ -0,0 +1,30 @@
|
||||||
|
package DDG::Goodie::Tips;
|
||||||
|
use DDG::Goodie;
|
||||||
|
|
||||||
|
triggers any => 'tip', 'tips', '%';
|
||||||
|
|
||||||
|
handle query_lc => sub {
|
||||||
|
return unless my ($p, $is_tip, $sign,$num) = $_ =~/^(\d{1,3})(?: ?%| percent) (?:(tip (?:on|for))|of)(?: an?)? ([\$\-]?)(\d+(\.?)(?(5)\d+))(?: bill)?$/;
|
||||||
|
$p /= 100;
|
||||||
|
my $t = $p*$num;
|
||||||
|
my $tot;
|
||||||
|
|
||||||
|
if ($is_tip) {
|
||||||
|
$tot = $num + $t;
|
||||||
|
}
|
||||||
|
|
||||||
|
$t = sprintf "%.2f", $t;
|
||||||
|
$tot = sprintf "%.2f", $tot if $tot;
|
||||||
|
|
||||||
|
if ($tot) {
|
||||||
|
zci answer_type => 'tip';
|
||||||
|
return "Tip: \$$t Total: \$$tot";
|
||||||
|
}
|
||||||
|
$t = $sign . $t;
|
||||||
|
$tot = $sign . $tot if $tot;
|
||||||
|
zci answer_type => 'percentage';
|
||||||
|
return "$t is ".($p*100)." percent of $sign$num";
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
1;
|
|
@ -0,0 +1,21 @@
|
||||||
|
#!/usr/bin/env perl
|
||||||
|
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use Test::More;
|
||||||
|
use DDG::Test::Goodie;
|
||||||
|
|
||||||
|
zci answer_type => 'tip';
|
||||||
|
zci is_cached => 0;
|
||||||
|
|
||||||
|
ddg_goodie_test(
|
||||||
|
[qw(
|
||||||
|
DDG::Goodie::Tips
|
||||||
|
)],
|
||||||
|
'20% tip on $20' => test_zci('Tip: $4.00 Total: $24.00'),
|
||||||
|
'20% tip on $20 bill' => test_zci('Tip: $4.00 Total: $24.00'),
|
||||||
|
'20% tip for a $20 bill' => test_zci('Tip: $4.00 Total: $24.00'),
|
||||||
|
'20 percent tip on $20' => test_zci('Tip: $4.00 Total: $24.00'),
|
||||||
|
);
|
||||||
|
|
||||||
|
done_testing;
|
Loading…
Reference in New Issue