zeroclickinfo-goodies/t/HTMLEntitiesEncode.t

118 lines
6.8 KiB
Perl
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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