HTMLEntitiesDecode: Converting to Text Template

master
Rob Emery 2016-05-12 13:30:55 +01:00
parent 3b32b27bc8
commit 4d46a1e103
2 changed files with 36 additions and 21 deletions

View File

@ -44,12 +44,18 @@ handle remainder => sub {
$decoded = "Special character (no visual representation)";
}
# Make answer
return "Decoded HTML Entity: $decoded",
structured_answer => {
input => [$_],
result => $decoded,
operation => "HTML Entity Decode"
data => {
title => html_enc($decoded),
subtitle => 'HTML Entity Decode: '.html_enc($_)
},
templates => {
group => 'text',
options => {
content => 'DDH.html_entity.content'
}
}
};
};

View File

@ -9,11 +9,19 @@ use DDG::Test::Goodie;
zci answer_type => 'html_entity';
zci is_cached => 1;
sub any_structured_answer {
sub build_structured_answer {
my ($title, $subtitle) = @_;
return {
input => ignore(),
result => ignore(),
operation => "HTML Entity Decode"
data => {
title => $title,
subtitle => $subtitle,
},
templates => {
group => 'text',
options => {
content => 'DDH.html_entity.content'
}
}
};
}
@ -21,32 +29,32 @@ ddg_goodie_test(
[qw(DDG::Goodie::HTMLEntitiesDecode)],
# Simple decimal test
'html decode !' => test_zci("Decoded HTML Entity: !", structured_answer => any_structured_answer()),
'html decode !' => test_zci("Decoded HTML Entity: !", structured_answer => build_structured_answer("!", "HTML Entity Decode: !")),
# Simple text test
'html entity &' => test_zci("Decoded HTML Entity: &", structured_answer => any_structured_answer()),
'html entity &' => test_zci("Decoded HTML Entity: &", structured_answer => build_structured_answer("&","HTML Entity Decode: &")),
# Another simple text test
'decode html for >' => test_zci("Decoded HTML Entity: >", structured_answer => any_structured_answer()),
'decode html for >' => test_zci("Decoded HTML Entity: >", structured_answer => build_structured_answer(">","HTML Entity Decode: >")),
# Simple hex test
'&#x21 htmlentity' => test_zci("Decoded HTML Entity: !", structured_answer => any_structured_answer()),
'&#x21 htmlentity' => test_zci("Decoded HTML Entity: !", structured_answer => build_structured_answer("!","HTML Entity Decode: &#x21")),
# No "&" and ";" in decimal input
'#36 html decode' => test_zci("Decoded HTML Entity: \$", structured_answer => any_structured_answer()),
'#36 html decode' => test_zci('Decoded HTML Entity: $', structured_answer => build_structured_answer('$',"HTML Entity Decode: #36")),
# Variety in hex queries
'&#X22 decodehtml' => test_zci("Decoded HTML Entity: \"", structured_answer => any_structured_answer()),
'&#X22 decodehtml' => test_zci('Decoded HTML Entity: "', structured_answer => build_structured_answer('"',"HTML Entity Decode: &#X22")),
# More variety in hex queries
'htmlentity for #x3c' => test_zci("Decoded HTML Entity: <", structured_answer => any_structured_answer()),
'htmlentity for #x3c' => test_zci("Decoded HTML Entity: <", structured_answer => build_structured_answer("&lt;","HTML Entity Decode: #x3c")),
# "&cent;" succeeds
'html decode &cent;' => test_zci(re(qr/.*/), structured_answer => any_structured_answer()),
'html decode &cent;' => test_zci(qr/.*/, structured_answer => build_structured_answer("&cent;","HTML Entity Decode: &amp;cent;")),
# "&cent" also succeeds (missing back ";" is OK)
'html decode &cent' => test_zci(re(qr/.*/), structured_answer => any_structured_answer()),
'html decode &cent' => test_zci(qr/.*/, structured_answer => build_structured_answer("&cent;","HTML Entity Decode: &amp;cent")),
# "cent" fails during the regex match because of the missing front "&" (stricter for text to eliminate false positive encoding hits)
'html decode cent' => undef,
# "cent;" fails during the regex match for the same reasons as above
'html decode cent;' => undef,
# "&#20;" has no visual representation
'html entity of &#20;' => test_zci("Decoded HTML Entity: Unicode control character (no visual representation)", structured_answer => any_structured_answer()),
'html entity of &#20;' => test_zci("Decoded HTML Entity: Unicode control character (no visual representation)", structured_answer => build_structured_answer("Unicode control character (no visual representation)","HTML Entity Decode: &amp;#20;")),
# Querying for "&bunnyrabbit;" should fail
'html decode &bunnyrabbit;' => undef,
@ -56,11 +64,12 @@ ddg_goodie_test(
'html decode apostrophe' => undef,
# natural querying
'What is the decoded html entity for &#960;?' => test_zci(re(qr/.*/), structured_answer => any_structured_answer()),
'What is the decoded html entity for &#960;?' => test_zci(qr/.*/, structured_answer => build_structured_answer("&pi;","HTML Entity Decode: &amp;#960;")),
# natural querying
'what is decoded html entity for #960 ?' => test_zci(re(qr/.*/), structured_answer => any_structured_answer()),
'what is decoded html entity for #960 ?' => test_zci(qr/.*/, structured_answer => build_structured_answer("&pi;","HTML Entity Decode: #960")),
# no "html" in query
'the decoded entity for &#333; is?' => test_zci(re(qr/.*/), structured_answer => any_structured_answer()),
'the decoded entity for &#333; is?' => test_zci(qr/.*/, structured_answer => build_structured_answer("&#x14D;","HTML Entity Decode: &amp;#333;")),
);
done_testing;