use structured answer

master
Usman Raza 2014-11-21 20:42:08 +05:00
parent dea345fef0
commit 74bf7a88d2
2 changed files with 63 additions and 40 deletions

View File

@ -4,9 +4,7 @@ package DDG::Goodie::ISO639;
use DDG::Goodie;
use Locale::Language;
use constant WPHREF => "https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes";
triggers start => "iso 639", "iso639";
triggers start => "iso 639", "iso639", "language code";
zci answer_type => "iso639";
zci is_cached => 1;
@ -23,24 +21,26 @@ attribution github => [ 'http://github.com/tantalor', 'tantalor' ],
handle remainder => sub {
my ($lang, $code) = langpair(shift) or return;
my $text = sprintf qq(ISO 639: %s - %s), $lang, $code;
my $html = sprintf qq(<a href="%s">ISO 639</a>: %s - %s), WPHREF, $lang, $code;
return $text, html => $html;
my ($lang, $code) = langpair(shift) or return;
return 'ISO 639: '. $lang .' - '. $code,
structured_answer => {
input => [$lang],
operation => 'ISO 639 Language code',
result => ($code),
};
};
sub langpair {
if (my $lang = code2language($_)) {
return ($lang, language2code($lang));
}
if (my $lang = code2language($_,'alpha-3')) {
return ($lang, language2code($lang));
}
if (my $code = language2code($_)) {
return (code2language($code), $code);
}
return;
if (my $lang = code2language($_)) {
return ($lang, language2code($lang));
}
if (my $lang = code2language($_,'alpha-3')) {
return ($lang, language2code($lang));
}
if (my $code = language2code($_)) {
return (code2language($code), $code);
}
return;
}
1;

View File

@ -1,35 +1,58 @@
#!/usr/bin/env perl
use utf8;
use strict;
use warnings;
use Test::More;
use DDG::Test::Goodie;
zci answer_type => "iso639";
zci is_cached => 1;
ddg_goodie_test(
["DDG::Goodie::ISO639"],
"iso639 ab" => test_zci(
qq(ISO 639: Abkhazian - ab),
html => qq(<a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639</a>: Abkhazian - ab),
answer_type => "iso639"
),
"iso639 english" => test_zci(
qq(ISO 639: English - en),
html => qq(<a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639</a>: English - en),
answer_type => "iso639"
),
"iso639 eng" => test_zci(
qq(ISO 639: English - en),
html => qq(<a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639</a>: English - en),
answer_type => "iso639"
),
"iso-639 en" => test_zci(
qq(ISO 639: English - en),
html => qq(<a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639</a>: English - en),
answer_type => "iso639"
),
"iso639 xyz" => undef,
["DDG::Goodie::ISO639"],
"iso639 ab" => test_zci(
qq(ISO 639: Abkhazian - ab),
structured_answer => {
input => ['Abkhazian'],
operation => 'ISO 639 Language code',
result => 'ab',
}
),
"iso639 english" => test_zci(
qq(ISO 639: English - en),
structured_answer => {
input => ['English'],
operation => 'ISO 639 Language code',
result => 'en',
}
),
"iso639 eng" => test_zci(
qq(ISO 639: English - en),
structured_answer => {
input => ['English'],
operation => 'ISO 639 Language code',
result => 'en',
}
),
"iso-639 en" => test_zci(
qq(ISO 639: English - en),
structured_answer => {
input => ['English'],
operation => 'ISO 639 Language code',
result => 'en',
}
),
"language code french" => test_zci(
qq(ISO 639: French - fr),
structured_answer => {
input => ['French'],
operation => 'ISO 639 Language code',
result => 'fr',
}
),
"iso639 xyz" => undef,
);
done_testing;