Convert poker goodie to a structured answer, add negative tests (#4350)

master
Philip Kirkbride 2017-07-13 07:43:13 -04:00 committed by Zaahir Moolla
parent 215857e8dd
commit ab9ce37808
2 changed files with 95 additions and 76 deletions

View File

@ -10,67 +10,67 @@ zci answer_type => "poker";
zci is_cached => 1;
my %odds = (
"royal flush" => "649,739",
"straight flush" => "72,192",
"four of a kind" => "4,164",
"full house" => "693",
"flush" => "508",
"straight" => "254",
"three of a kind" => "46.3",
"two pair" => "20.0",
"one pair" => "1.36",
"no pair" => "0.995",
"high card" => "0.995",
"royal flush" => "649,739",
"straight flush" => "72,192",
"four of a kind" => "4,164",
"full house" => "693",
"flush" => "508",
"straight" => "254",
"three of a kind" => "46.3",
"two pair" => "20.0",
"one pair" => "1.36",
"no pair" => "0.995",
"high card" => "0.995",
);
my %frequency = (
"royal flush" => "4",
"straight flush" => "36",
"four of a kind" => "624",
"full house" => "3,744",
"flush" => "5,108",
"straight" => "10,200",
"three of a kind" => "54,912",
"two pair" => "123,552",
"one pair" => "1,098,240",
"no pair" => "1,302,540",
"high card" => "1,302,540",
"royal flush" => "4",
"straight flush" => "36",
"four of a kind" => "624",
"full house" => "3,744",
"flush" => "5,108",
"straight" => "10,200",
"three of a kind" => "54,912",
"two pair" => "123,552",
"one pair" => "1,098,240",
"no pair" => "1,302,540",
"high card" => "1,302,540",
);
my %probability = (
"royal flush" => "0.000154",
"straight flush" => "0.00139",
"four of a kind" => "0.0240",
"full house" => "0.144",
"flush" => "0.197",
"straight" => "0.392",
"three of a kind" => "2.11",
"two pair" => "4.75",
"one pair" => "42.3",
"no pair" => "50.1",
"high card" => "50.1",
"royal flush" => "0.000154",
"straight flush" => "0.00139",
"four of a kind" => "0.0240",
"full house" => "0.144",
"flush" => "0.197",
"straight" => "0.392",
"three of a kind" => "2.11",
"two pair" => "4.75",
"one pair" => "42.3",
"no pair" => "50.1",
"high card" => "50.1",
);
my %webaddresses = (
"royal flush" => "Royal_flush",
"straight flush" => "Straight_flush",
"four of a kind" => "Four_of_a_kind",
"full house" => "Full_house",
"flush" => "Flush",
"straight" => "Straight",
"three of a kind" => "Three_of_a_kind",
"two pair" => "Two_pair",
"one pair" => "One_pair",
"no pair" => "High_card",
"high card" => "High_card",
"royal flush" => "Royal_flush",
"straight flush" => "Straight_flush",
"four of a kind" => "Four_of_a_kind",
"full house" => "Full_house",
"flush" => "Flush",
"straight" => "Straight",
"three of a kind" => "Three_of_a_kind",
"two pair" => "Two_pair",
"one pair" => "One_pair",
"no pair" => "High_card",
"high card" => "High_card",
);
handle remainder => sub {
#make sure the requested hand is listed
return unless /^(frequency|probability|odds)\s(.+)$/i && ($odds{lc$2});
#make sure the requested hand is listed
return unless /^(frequency|probability|odds)\s(.+)$/i && ($odds{lc$2});
my $query = lc $1;
my $hand = lc $2;
my $hand = lc $2;
my $odds = "The odds of getting a $hand in poker are $odds{$hand} : 1.";
my $freq = "The frequency of a $hand in poker is $frequency{$hand} out of 2,598,960.";
@ -79,14 +79,25 @@ handle remainder => sub {
my $link = qq(More at <a href="https://en.wikipedia.org/wiki/List_of_poker_hands#$webaddresses{$hand}">Wikipedia</a>.);
my %answer = (
'odds' => [$odds, 'html' => "$odds $link"],
'frequency' => [$freq, 'html' => "$freq $link"],
'probability' => [$prob, 'html' => "$prob $link"],
'odds' => $odds,
'frequency' => $freq,
'probability' => $prob,
);
return @{$answer{$query}} if exists $answer{$query};
my %statistic = (
'odds' => $odds{$hand}." : 1",
'frequency' => $frequency{$hand}." out of 2,598,960",
'probability' => $probability{$hand}."%",
);
my $string_answer = $answer{$query};
my $structured_answer = {};
$structured_answer->{templates}->{group} = 'text';
$structured_answer->{data}->{title} = $statistic{$query};
$structured_answer->{data}->{subtitle} = $answer{$query};
return $string_answer, structured_answer => $structured_answer;
return;
};
1;

View File

@ -6,33 +6,41 @@ use Test::More;
use Test::Deep;
use DDG::Test::Goodie;
zci answer_type => "poker";
zci answer_type => 'poker';
zci is_cached => 1;
sub build_answer {
my ($title, $subtitle) = @_;
return $subtitle,
structured_answer => {
data => {
title => $title,
subtitle => $subtitle
},
templates => {
group => 'text',
}
};
}
sub build_test { test_zci(build_answer(@_)) }
ddg_goodie_test(
[qw(
DDG::Goodie::Poker
)],
'poker odds flush' => test_zci(
'The odds of getting a flush in poker are 508 : 1.',
html => 'The odds of getting a flush in poker are 508 : 1. More at <a href="https://en.wikipedia.org/wiki/List_of_poker_hands#Flush">Wikipedia</a>.'
),
'Probability poker Two Pair' => test_zci(
'The probability of getting a two pair in poker is 4.75%.',
html => 'The probability of getting a two pair in poker is 4.75%. More at <a href="https://en.wikipedia.org/wiki/List_of_poker_hands#Two_pair">Wikipedia</a>.'
),
'frequency royal flush poker' => test_zci(
'The frequency of a royal flush in poker is 4 out of 2,598,960.',
html => 'The frequency of a royal flush in poker is 4 out of 2,598,960. More at <a href="https://en.wikipedia.org/wiki/List_of_poker_hands#Royal_flush">Wikipedia</a>.'
),
'poker odds three of a kind' => test_zci(
'The odds of getting a three of a kind in poker are 46.3 : 1.',
html => 'The odds of getting a three of a kind in poker are 46.3 : 1. More at <a href="https://en.wikipedia.org/wiki/List_of_poker_hands#Three_of_a_kind">Wikipedia</a>.'
),
'probability poker flush' => test_zci(
'The probability of getting a flush in poker is 0.197%.',
html => 'The probability of getting a flush in poker is 0.197%. More at <a href="https://en.wikipedia.org/wiki/List_of_poker_hands#Flush">Wikipedia</a>.'
),
[
'DDG::Goodie::Poker'
],
'poker odds flush' => build_test('508 : 1', 'The odds of getting a flush in poker are 508 : 1.'),
'Probability poker Two Pair' => build_test('4.75%', 'The probability of getting a two pair in poker is 4.75%.'),
'frequency royal flush poker' => build_test('4 out of 2,598,960', 'The frequency of a royal flush in poker is 4 out of 2,598,960.'),
'poker odds three of a kind' => build_test('46.3 : 1', 'The odds of getting a three of a kind in poker are 46.3 : 1.'),
'probability poker flush' => build_test('0.197%', 'The probability of getting a flush in poker is 0.197%.'),
# Invalid Input
'poker odds game' => undef,
'odds of winning poker' => undef,
);
done_testing;