2014-06-13 06:59:01 -07:00
#!/usr/bin/env perl
use strict ;
use warnings ;
use Test::More ;
use DDG::Test::Goodie ;
2015-07-19 12:50:40 -07:00
use utf8 ;
2014-06-13 06:59:01 -07:00
zci answer_type = > 'html_entity' ;
2014-09-27 06:42:57 -07:00
zci is_cached = > 1 ;
2014-06-13 06:59:01 -07:00
2015-07-19 12:50:40 -07:00
sub make_structured_answer {
2015-08-31 13:23:32 -07:00
my ( $ qr ) = @ _ ;
return {
2015-07-19 12:50:40 -07:00
input = > '-ANY-' ,
operation = > "HTML Entity Encode" ,
result = > $ qr ,
2015-08-31 13:23:32 -07:00
} ;
2015-07-19 12:50:40 -07:00
}
sub make_record_answer {
2015-08-31 13:23:32 -07:00
my ( $ data , $ keys , $ question ) = @ _ ;
return {
2015-07-19 12:50:40 -07:00
templates = > {
group = > 'list' ,
options = > {
content = > 'record' ,
}
} ,
data = > {
2015-09-02 15:08:19 -07:00
title = > "$question" ,
subtitle = > "HTML Entity Encode" ,
2015-07-19 12:59:17 -07:00
record_data = > $ data ,
record_keys = > $ keys ,
2015-07-19 12:50:40 -07:00
}
} ;
}
2014-06-13 06:59:01 -07:00
ddg_goodie_test (
2014-06-23 22:12:47 -07:00
[ qw( DDG::Goodie::HTMLEntitiesEncode ) ] ,
2014-06-26 15:06:23 -07:00
# Simple tests
2015-07-19 12:50:40 -07:00
'html code em dash' = > test_zci ( "Encoded HTML Entity: —" , structured_answer = > make_structured_answer ( qr/mdash/ ) ) ,
'html em dash' = > test_zci ( "Encoded HTML Entity: —" , structured_answer = > make_structured_answer ( qr/mdash/ ) ) ,
'em-dash html' = > test_zci ( "Encoded HTML Entity: —" , structured_answer = > make_structured_answer ( qr/mdash/ ) ) , # hyphens ignored
'html encode "em-dash"' = > test_zci ( "Encoded HTML Entity: —" , structured_answer = > make_structured_answer ( qr/mdash/ ) ) , # quotes ignored
'ApoSTrophe escapehtml' = > test_zci ( "Encoded HTML Entity: '" , structured_answer = > make_structured_answer ( qr/#39/ ) ) , # mixed cases in query
2014-06-23 22:12:47 -07:00
2014-06-26 15:06:23 -07:00
# Basic varieties in querying accented chars
2015-07-19 12:50:40 -07:00
'html entity A-acute' = > test_zci ( "Encoded HTML Entity: Á" , structured_answer = > make_structured_answer ( qr/Aacute/ ) ) ,
'html entity for E Grave' = > test_zci ( "Encoded HTML Entity: È" , structured_answer = > make_structured_answer ( qr/Egrave/ ) ) ,
'html Ograve' = > test_zci ( "Encoded HTML Entity: Ò" , structured_answer = > make_structured_answer ( qr/Ograve/ ) ) ,
2014-06-26 15:06:23 -07:00
# Single typed-in character queries
2015-07-19 12:50:40 -07:00
'html escape &' = > test_zci ( "Encoded HTML Entity: &" , structured_answer = > make_structured_answer ( qr/amp/ ) ) , # &
'html escape "' = > test_zci ( "Encoded HTML Entity: "" , structured_answer = > make_structured_answer ( qr/quot/ ) ) , # "
'how to html escape &?' = > test_zci ( "Encoded HTML Entity: &" , structured_answer = > make_structured_answer ( qr/amp/ ) ) , # ?
'how to html escape "&"?' = > test_zci ( "Encoded HTML Entity: &" , structured_answer = > make_structured_answer ( qr/amp/ ) ) , # &
'how to html escape ??' = > test_zci ( "Encoded HTML Entity: ?" , structured_answer = > make_structured_answer ( qr/#63/ ) ) , # ?
'how do you html escape a "?"?' = > test_zci ( "Encoded HTML Entity: ?" , structured_answer = > make_structured_answer ( qr/#63/ ) ) , # ?
'html escape """' = > test_zci ( "Encoded HTML Entity: "" , structured_answer = > make_structured_answer ( qr/quot/ ) ) , # "
'$ sign htmlentity' = > test_zci ( "Encoded HTML Entity: $" , structured_answer = > make_structured_answer ( qr/#36/ ) ) , # $
2015-07-19 12:59:17 -07:00
# Return two matching entities for ambiguous query
'pound symbol html encode ' = > test_zci (
2015-08-31 13:23:32 -07:00
"Encoded HTML Entity: £\nEncoded HTML Entity: #" ,
structured_answer = > make_record_answer ( {
"British Pound Sterling (£)" = > "£" ,
"Number sign (#)" = > "#"
} , [ "British Pound Sterling (£)" , "Number sign (#)" ] , "pound" )
) ,
2014-06-23 22:12:47 -07:00
2014-06-26 15:06:23 -07:00
# Ignore words and whitespace
2015-07-19 12:50:40 -07:00
'html code of pilcrow sign' = > test_zci ( "Encoded HTML Entity: ¶" , structured_answer = > make_structured_answer ( qr/#182/ ) ) , # of, sign
'html escape greater than symbol' = > test_zci ( "Encoded HTML Entity: >" , structured_answer = > make_structured_answer ( qr/gt/ ) ) , # symbol
'space html character code' = > test_zci ( "Encoded HTML Entity: " , structured_answer = > make_structured_answer ( qr/nbsp/ ) ) , # Ignore extra whitespace
2014-06-23 22:12:47 -07:00
2014-06-26 15:06:23 -07:00
# Better hash hits substitutions
2014-06-23 22:12:47 -07:00
# 'right angle brackets' should work even though the defined key contains the singular 'bracket'
2015-08-31 13:23:32 -07:00
'right angle brackets htmlencode' = > test_zci (
"Encoded HTML Entity: ›\nEncoded HTML Entity: »" ,
structured_answer = > make_record_answer ( {
"Single right pointing angle quote (› )" = > "›" ,
"Double right pointing angle quote (»)" = > "»"
} , [ "Single right pointing angle quote (› )" , "Double right pointing angle quote (»)" ] , "right angle brackets" )
) ,
2014-06-23 22:12:47 -07:00
# 'double quotes' should work even though the defined key contains the singular 'quote'
2015-07-19 12:50:40 -07:00
'double quotes htmlescape' = > test_zci ( "Encoded HTML Entity: "" , structured_answer = > make_structured_answer ( qr/quot/ ) ) ,
2014-06-23 22:12:47 -07:00
2015-07-19 12:50:40 -07:00
# Should not work
2014-06-23 22:12:47 -07:00
'html encode +' = > undef ,
'html entity &' = > undef ,
2015-07-19 12:50:40 -07:00
'html encode is it magic' = > undef ,
2014-06-26 15:06:23 -07:00
# Natural querying
2015-07-19 12:50:40 -07:00
'What is the html character code for pi' = > test_zci ( "Encoded HTML Entity: π" , structured_answer = > make_structured_answer ( qr/#960/ ) ) ,
'whatis html entity for en-dash' = > test_zci ( "Encoded HTML Entity: –" , structured_answer = > make_structured_answer ( qr/ndash/ ) ) , # whatis spelling
'how do I escape the greater-than symbol html' = > test_zci ( "Encoded HTML Entity: >" , structured_answer = > make_structured_answer ( qr/gt/ ) ) ,
2014-06-26 15:06:23 -07:00
# the "a/A" belonging to "acute" matters, but the "a" immediately after "character" is removed
2015-07-19 12:50:40 -07:00
'How to get a a acute character in html code' = > test_zci ( "Encoded HTML Entity: á" , structured_answer = > make_structured_answer ( qr/aacute/ ) ) ,
'how to get a a-acute character in html code' = > test_zci ( "Encoded HTML Entity: á" , structured_answer = > make_structured_answer ( qr/aacute/ ) ) ,
'how to get a aacute character in html code' = > test_zci ( "Encoded HTML Entity: á" , structured_answer = > make_structured_answer ( qr/aacute/ ) ) ,
'how to get a A acute character in html code' = > test_zci ( "Encoded HTML Entity: Á" , structured_answer = > make_structured_answer ( qr/Aacute/ ) ) ,
'how to get a A-acute character in html code' = > test_zci ( "Encoded HTML Entity: Á" , structured_answer = > make_structured_answer ( qr/Aacute/ ) ) ,
'how to get a Aacute character in html code' = > test_zci ( "Encoded HTML Entity: Á" , structured_answer = > make_structured_answer ( qr/Aacute/ ) ) ,
2014-06-26 15:06:23 -07:00
# Question marks ignored
2015-07-19 12:50:40 -07:00
'the encoded html entity of apostrophe is?' = > test_zci ( "Encoded HTML Entity: '" , structured_answer = > make_structured_answer ( qr/#39/ ) ) ,
'how to encode an apostrophe in html ? ' = > test_zci ( "Encoded HTML Entity: '" , structured_answer = > make_structured_answer ( qr/#39/ ) ) , # spaces around the end
2014-06-26 15:06:23 -07:00
# Question mark matters
2015-07-19 12:50:40 -07:00
'how to encode "?" in html' = > test_zci ( "Encoded HTML Entity: ?" , structured_answer = > make_structured_answer ( qr/#63/ ) ) ,
2014-06-13 06:59:01 -07:00
) ;
done_testing ;