Palindrome: Convert to Structured (#4352)
* Convert palindrome to structured, tabs, negative tests * remove comments, simplify subtitle functionmaster
parent
db434a22d0
commit
b082c3013e
|
@ -10,21 +10,28 @@ triggers any => 'palindrome';
|
|||
zci is_cached => 1;
|
||||
|
||||
handle query => sub {
|
||||
#Remove the trigger text from the query.
|
||||
return unless /^(?:is\s+|)(.*?)\s+an?\s*palindrome\??$/i;
|
||||
return if /^(what is*)/;
|
||||
|
||||
my $palindrome = $1;
|
||||
|
||||
my $is_palindrome = 0;
|
||||
|
||||
# Clean up.
|
||||
my $clean_palindrome = lc $palindrome;
|
||||
$clean_palindrome =~ s/[^a-z0-9]+//g;
|
||||
|
||||
$is_palindrome = 1 if $clean_palindrome eq scalar reverse $clean_palindrome;
|
||||
|
||||
#Check to see if it is a palindrome.
|
||||
return $is_palindrome ? qq("$palindrome" is a palindrome.) : qq("$palindrome" is not a palindrome.);
|
||||
my $title = $is_palindrome ? "Yes" : "No";
|
||||
my $subtitle = "\"$palindrome\" is ".($is_palindrome ? "":"not ")."a palindrome.";
|
||||
return $subtitle, structured_answer => {
|
||||
data => {
|
||||
title => $title,
|
||||
subtitle => $subtitle,
|
||||
},
|
||||
templates => {
|
||||
group => 'text'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
|
@ -9,17 +9,37 @@ use DDG::Test::Goodie;
|
|||
zci answer_type => 'palindrome';
|
||||
zci is_cached => 1;
|
||||
|
||||
sub build_sum_answer {
|
||||
my($title, $subtitle) = @_;
|
||||
return $subtitle,
|
||||
structured_answer => {
|
||||
data => {
|
||||
title => $title,
|
||||
subtitle => $subtitle
|
||||
},
|
||||
templates => {
|
||||
group => 'text',
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
sub build_sum_test { test_zci(build_sum_answer(@_)) }
|
||||
|
||||
ddg_goodie_test(
|
||||
[qw(
|
||||
DDG::Goodie::Palindrome
|
||||
)],
|
||||
'is foo a palindrome?' => test_zci('"foo" is not a palindrome.'),
|
||||
'foo a palindrome?' => test_zci('"foo" is not a palindrome.'),
|
||||
'is foof a palindrome?' => test_zci('"foof" is a palindrome.'),
|
||||
'foof a palindrome?' => test_zci('"foof" is a palindrome.'),
|
||||
'is A dank, sad nap. Eels sleep and ask nada. a palindrome?' => test_zci('"A dank, sad nap. Eels sleep and ask nada." is a palindrome.'),
|
||||
'is a dank, sad nap. eels sleep and ask nada. a palindrome?' => test_zci('"a dank, sad nap. eels sleep and ask nada." is a palindrome.'),
|
||||
'is dad a palindrome?' => test_zci('"dad" is a palindrome.'),
|
||||
'is foo a palindrome?' => build_sum_test('No', '"foo" is not a palindrome.'),
|
||||
'foo a palindrome?' => build_sum_test('No', '"foo" is not a palindrome.'),
|
||||
'foof a palindrome?' => build_sum_test('Yes', '"foof" is a palindrome.'),
|
||||
'is foof a palindrome?' => build_sum_test('Yes', '"foof" is a palindrome.'),
|
||||
'is A dank, sad nap. Eels sleep and ask nada. a palindrome?' => build_sum_test('Yes', '"A dank, sad nap. Eels sleep and ask nada." is a palindrome.'),
|
||||
'is a dank, sad nap. eels sleep and ask nada. a palindrome?' => build_sum_test('Yes', '"a dank, sad nap. eels sleep and ask nada." is a palindrome.'),
|
||||
'is dad a palindrome?' => build_sum_test('Yes', '"dad" is a palindrome.'),
|
||||
|
||||
# Invalid input
|
||||
'what is a palindrome?' => undef,
|
||||
'I Palindrome I' => undef,
|
||||
);
|
||||
|
||||
done_testing;
|
||||
|
|
Loading…
Reference in New Issue