TimezoneConverter: Switching to text template

master
Rob 2016-05-22 20:55:37 +01:00
parent 8e74d245c7
commit 8981ffe50c
2 changed files with 64 additions and 363 deletions

10
lib/DDG/Goodie/TimezoneConverter.pm Normal file → Executable file
View File

@ -173,9 +173,13 @@ handle query => sub {
ucfirst $output->{time}, $output->{timezone}, $output->{days};
return $output_string, structured_answer => {
input => [html_enc($input_string)],
operation => 'Convert Timezone',
result => html_enc($output_string),
data => {
title => html_enc($output_string),
subtitle => "Convert Timezone: ".html_enc($input_string)
},
templates => {
type => 'text'
}
};
};

417
t/TimezoneConverter.t Normal file → Executable file
View File

@ -10,178 +10,43 @@ use Test::MockTime qw( :all );
zci answer_type => 'timezone_converter';
zci is_cached => 1;
sub build_test {
my ($text, $input) = @_;
return test_zci($text, structured_answer => {
data => {
title => $text,
subtitle => "Convert Timezone: $input"
},
templates => {
type => 'text'
}
});
}
ddg_goodie_test(
['DDG::Goodie::TimezoneConverter'],
'3:14 UTC in GMT' =>
test_zci('3:14 GMT',
structured_answer => {
input => ['3:14 UTC to GMT'],
operation => 'Convert Timezone',
result => '3:14 GMT',
},
),
'8:10 AM AZOST into CAT' =>
test_zci('11:10 AM CAT',
structured_answer => {
input => ['8:10 AM AZOST (UTC-1) to CAT (UTC+2)'],
operation => 'Convert Timezone',
result => '11:10 AM CAT',
},
),
'1pm EDT into UTC+2' =>
test_zci('7:00 PM UTC+2',
structured_answer => {
input => ['1:00 PM EDT (UTC-4) to UTC+2'],
operation => 'Convert Timezone',
result => '7:00 PM UTC+2',
},
),
'0pm UTC into GMT' =>
test_zci('Noon GMT',
structured_answer => {
input => ['Noon UTC to GMT'],
operation => 'Convert Timezone',
result => 'Noon GMT',
},
),
'0am UTC into UTC' =>
test_zci('Midnight UTC',
structured_answer => {
input => ['Midnight UTC to UTC'],
operation => 'Convert Timezone',
result => 'Midnight UTC',
},
),
'1 UTC into UTC -2 ' =>
test_zci('23:00 UTC-2 (1 day prior)',
structured_answer => {
input => ['1:00 UTC to UTC-2'],
operation => 'Convert Timezone',
result => '23:00 UTC-2 (1 day prior)',
},
),
' 1 UTC into UTC-1' =>
test_zci('0:00 UTC-1',
structured_answer => {
input => ['1:00 UTC to UTC-1'],
operation => 'Convert Timezone',
result => '0:00 UTC-1',
},
),
'21 FNT into EET' =>
test_zci('1:00 EET (1 day after)',
structured_answer => {
input => ['21:00 FNT (UTC-2) to EET (UTC+2)'],
operation => 'Convert Timezone',
result => '1:00 EET (1 day after)',
},
),
'23:00:00 UTC InTo UTC+1' =>
test_zci('0:00 UTC+1 (1 day after)',
structured_answer => {
input => ['23:00 UTC to UTC+1'],
operation => 'Convert Timezone',
result => '0:00 UTC+1 (1 day after)',
},
),
'23:00:01 UTC Into UTC+1' =>
test_zci('0:00:01 UTC+1 (1 day after)',
structured_answer => {
input => ['23:00:01 UTC to UTC+1'],
operation => 'Convert Timezone',
result => '0:00:01 UTC+1 (1 day after)',
},
),
'13:15:00 UTC-0:30 into UTC+0:30' =>
test_zci('13:15 UTC+0:30',
structured_answer => {
input => ['13:15 UTC-0:30 to UTC+0:30'],
operation => 'Convert Timezone',
result => '13:15 UTC+0:30',
},
),
'3:14 UTC in GMT' => build_test('3:14 GMT', '3:14 UTC to GMT'),
'8:10 AM AZOST into CAT' => build_test('11:10 AM CAT', '8:10 AM AZOST (UTC-1) to CAT (UTC+2)'),
'1pm EDT into UTC+2' => build_test('7:00 PM UTC+2', '1:00 PM EDT (UTC-4) to UTC+2'),
'0pm UTC into GMT' => build_test('Noon GMT', 'Noon UTC to GMT'),
'0am UTC into UTC' => build_test('Midnight UTC', 'Midnight UTC to UTC'),
'1 UTC into UTC -2 ' => build_test('23:00 UTC-2 (1 day prior)', '1:00 UTC to UTC-2'),
' 1 UTC into UTC-1' => build_test('0:00 UTC-1', '1:00 UTC to UTC-1'),
'21 FNT into EET' => build_test('1:00 EET (1 day after)', '21:00 FNT (UTC-2) to EET (UTC+2)'),
'23:00:00 UTC InTo UTC+1' => build_test('0:00 UTC+1 (1 day after)', '23:00 UTC to UTC+1'),
'23:00:01 UTC Into UTC+1' => build_test('0:00:01 UTC+1 (1 day after)', '23:00:01 UTC to UTC+1'),
'13:15:00 UTC-0:30 into UTC+0:30' => build_test('13:15 UTC+0:30', '13:15 UTC-0:30 to UTC+0:30'),
# ok, this is unlikely to happen without trying to do that
'19:42:42 BIT into GMT+100' =>
test_zci('11:42:42 GMT+100 (5 days after)',
structured_answer => {
input => ['19:42:42 BIT (UTC-12) to GMT+100'],
operation => 'Convert Timezone',
result => '11:42:42 GMT+100 (5 days after)',
},
),
'19:42:42 CHADT into GMT-100' =>
test_zci('1:57:42 GMT-100 (4 days prior)',
structured_answer => {
input => ['19:42:42 CHADT (UTC+13:45) to GMT-100'],
operation => 'Convert Timezone',
result => '1:57:42 GMT-100 (4 days prior)',
},
),
'10:00AM MST to PST' =>
test_zci('9:00 AM PST',
structured_answer => {
input => ['10:00 AM MST (UTC-7) to PST (UTC-8)'],
operation => 'Convert Timezone',
result => '9:00 AM PST',
},
),
'19:00 UTC to EST' =>
test_zci('14:00 EST',
structured_answer => {
input => ['19:00 UTC to EST (UTC-5)'],
operation => 'Convert Timezone',
result => '14:00 EST',
},
),
'1am UTC to PST' =>
test_zci('5:00 PM PST (1 day prior)',
structured_answer => {
input => ['1:00 AM UTC to PST (UTC-8)'],
operation => 'Convert Timezone',
result => '5:00 PM PST (1 day prior)',
},
),
'12:40pm PST into JST' =>
test_zci('5:40 AM JST (1 day after)',
structured_answer => {
input => ['12:40 PM PST (UTC-8) to JST (UTC+9)'],
operation => 'Convert Timezone',
result => '5:40 AM JST (1 day after)',
},
),
'12:40 pm from PST to JST' =>
test_zci('5:40 AM JST (1 day after)',
structured_answer => {
input => ['12:40 PM PST (UTC-8) to JST (UTC+9)'],
operation => 'Convert Timezone',
result => '5:40 AM JST (1 day after)',
},
),
'11:22am est in utc' =>
test_zci('4:22 PM UTC',
structured_answer => {
input => ['11:22 AM EST (UTC-5) to UTC'],
operation => 'Convert Timezone',
result => '4:22 PM UTC',
},
),
'1600 UTC in BST' =>
test_zci('17:00 BST',
structured_answer => {
input => ['16:00 UTC to BST (UTC+1)'],
operation => 'Convert Timezone',
result => '17:00 BST',
},
),
'12:00 GMT in PST' =>
test_zci('4:00 PST',
structured_answer => {
input => ['12:00 GMT to PST (UTC-8)'],
operation => 'Convert Timezone',
result => '4:00 PST',
},
),
'19:42:42 BIT into GMT+100' => build_test('11:42:42 GMT+100 (5 days after)', '19:42:42 BIT (UTC-12) to GMT+100'),
'19:42:42 CHADT into GMT-100' => build_test('1:57:42 GMT-100 (4 days prior)', '19:42:42 CHADT (UTC+13:45) to GMT-100'),
'10:00AM MST to PST' => build_test('9:00 AM PST', '10:00 AM MST (UTC-7) to PST (UTC-8)'),
'19:00 UTC to EST' => build_test('14:00 EST', '19:00 UTC to EST (UTC-5)'),
'1am UTC to PST' => build_test('5:00 PM PST (1 day prior)', '1:00 AM UTC to PST (UTC-8)'),
'12:40pm PST into JST' => build_test('5:40 AM JST (1 day after)', '12:40 PM PST (UTC-8) to JST (UTC+9)'),
'12:40 pm from PST to JST' => build_test('5:40 AM JST (1 day after)', '12:40 PM PST (UTC-8) to JST (UTC+9)'),
'11:22am est in utc' => build_test('4:22 PM UTC', '11:22 AM EST (UTC-5) to UTC'),
'1600 UTC in BST' => build_test('17:00 BST', '16:00 UTC to BST (UTC+1)'),
'12:00 GMT in PST' => build_test('4:00 PST', '12:00 GMT to PST (UTC-8)'),
# Intentional non-answers
'12 in binary' => undef,
@ -194,102 +59,18 @@ set_fixed_time("2014-10-14T00:00:00");
ddg_goodie_test(
['DDG::Goodie::TimezoneConverter'],
# Location-specific tests (variable with DST)
'13:00 GMT in my time' =>
test_zci(q/9:00 EDT/,
structured_answer => {
input => [qq/13:00 GMT to $test_location_tz/],
operation => 'Convert Timezone',
result => q/9:00 EDT/,
},
),
'13:00 GMT' =>
test_zci(q/9:00 EDT/,
structured_answer => {
input => [qq/13:00 GMT to $test_location_tz/],
operation => 'Convert Timezone',
result => q/9:00 EDT/,
},
),
'11:22am cest in my timezone' =>
test_zci(re(qr/5:22 AM EDT/),
structured_answer => {
input => [qq/11:22 AM CEST (UTC+2) to $test_location_tz/],
operation => 'Convert Timezone',
result => re(qr/5:22 AM EDT/),
},
),
'11:22am cest in localtime' =>
test_zci(re(qr/5:22 AM EDT/),
structured_answer => {
input => [qq/11:22 AM CEST (UTC+2) to $test_location_tz/],
operation => 'Convert Timezone',
result => re(qr/5:22 AM EDT/),
},
),
'11:22am cest in my local timezone' =>
test_zci(re(qr/5:22 AM EDT/),
structured_answer => {
input => [qq/11:22 AM CEST (UTC+2) to $test_location_tz/],
operation => 'Convert Timezone',
result => re(qr/5:22 AM EDT/),
},
),
'11:22am cest' =>
test_zci(re(qr/5:22 AM EDT/),
structured_answer => {
input => [qq/11:22 AM CEST (UTC+2) to $test_location_tz/],
operation => 'Convert Timezone',
result => re(qr/5:22 AM EDT/),
},
),
'12pm my time in CEST' =>
test_zci(q/6:00 PM CEST/,
structured_answer => {
input => [qq/Noon $test_location_tz to CEST (UTC+2)/],
operation => 'Convert Timezone',
result => q/6:00 PM CEST/,
},
),
'12pm local timezone in CEST' =>
test_zci(q/6:00 PM CEST/,
structured_answer => {
input => [qq/Noon $test_location_tz to CEST (UTC+2)/],
operation => 'Convert Timezone',
result => q/6:00 PM CEST/,
},
),
'12pm in CEST' =>
test_zci(q/6:00 PM CEST/,
structured_answer => {
input => [qq/Noon $test_location_tz to CEST (UTC+2)/],
operation => 'Convert Timezone',
result => q/6:00 PM CEST/,
},
),
'12am my timezone in UTC' =>
test_zci(q/4:00 AM UTC/,
structured_answer => {
input => [qq/Midnight $test_location_tz to UTC/],
operation => 'Convert Timezone',
result => q/4:00 AM UTC/,
},
),
'12am local time in UTC' =>
test_zci(q/4:00 AM UTC/,
structured_answer => {
input => [qq/Midnight $test_location_tz to UTC/],
operation => 'Convert Timezone',
result => q/4:00 AM UTC/,
},
),
'12am in UTC' =>
test_zci(q/4:00 AM UTC/,
structured_answer => {
input => [qq/Midnight $test_location_tz to UTC/],
operation => 'Convert Timezone',
result => q/4:00 AM UTC/,
},
)
'13:00 GMT in my time' => build_test(q/9:00 EDT/, qq/13:00 GMT to $test_location_tz/),
'13:00 GMT' => build_test(q/9:00 EDT/, qq/13:00 GMT to $test_location_tz/),
'11:22am cest in my timezone' => build_test(re(qr/5:22 AM EDT/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest in localtime' => build_test(re(qr/5:22 AM EDT/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest in my local timezone' => build_test(re(qr/5:22 AM EDT/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest' => build_test(re(qr/5:22 AM EDT/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'12pm my time in CEST' => build_test(q/6:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12pm local timezone in CEST' => build_test(q/6:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12pm in CEST' => build_test(q/6:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12am my timezone in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight $test_location_tz to UTC/),
'12am local time in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight $test_location_tz to UTC/),
'12am in UTC' => build_test(q/4:00 AM UTC/, qq/Midnight $test_location_tz to UTC/)
);
restore_time();
@ -298,102 +79,18 @@ $test_location_tz = 'EST (UTC-5)';
ddg_goodie_test(
['DDG::Goodie::TimezoneConverter'],
# Location-specific tests (variable with DST)
'13:00 GMT in my time' =>
test_zci(qq/8:00 EST/,
structured_answer => {
input => [qq/13:00 GMT to $test_location_tz/],
operation => 'Convert Timezone',
result => q/8:00 EST/,
},
),
'13:00 GMT' =>
test_zci(qq/8:00 EST/,
structured_answer => {
input => [qq/13:00 GMT to $test_location_tz/],
operation => 'Convert Timezone',
result => q/8:00 EST/,
},
),
'11:22am cest in my timezone' =>
test_zci(re(qr/4:22 AM EST/),
structured_answer => {
input => [qq/11:22 AM CEST (UTC+2) to $test_location_tz/],
operation => 'Convert Timezone',
result => re(qr/4:22 AM EST/),
},
),
'11:22am cest in localtime' =>
test_zci(re(qr/4:22 AM EST/),
structured_answer => {
input => [qq/11:22 AM CEST (UTC+2) to $test_location_tz/],
operation => 'Convert Timezone',
result => re(qr/4:22 AM EST/),
},
),
'11:22am cest in my local timezone' =>
test_zci(q/4:22 AM EST/,
structured_answer => {
input => [qq/11:22 AM CEST (UTC+2) to $test_location_tz/],
operation => 'Convert Timezone',
result => q/4:22 AM EST/,
},
),
'11:22am cest' =>
test_zci(q/4:22 AM EST/,
structured_answer => {
input => [qq/11:22 AM CEST (UTC+2) to $test_location_tz/],
operation => 'Convert Timezone',
result => q/4:22 AM EST/,
},
),
'12pm my time in CEST' =>
test_zci(q/7:00 PM CEST/,
structured_answer => {
input => [qq/Noon $test_location_tz to CEST (UTC+2)/],
operation => 'Convert Timezone',
result => q/7:00 PM CEST/,
},
),
'12pm local timezone in CEST' =>
test_zci(q/7:00 PM CEST/,
structured_answer => {
input => [qq/Noon $test_location_tz to CEST (UTC+2)/],
operation => 'Convert Timezone',
result => q/7:00 PM CEST/,
},
),
'12pm in CEST' =>
test_zci(q/7:00 PM CEST/,
structured_answer => {
input => [qq/Noon $test_location_tz to CEST (UTC+2)/],
operation => 'Convert Timezone',
result => q/7:00 PM CEST/,
},
),
'12am my timezone in UTC' =>
test_zci(re(qr/5:00 AM UTC/),
structured_answer => {
input => [qq/Midnight $test_location_tz to UTC/],
operation => 'Convert Timezone',
result => q/5:00 AM UTC/,
},
),
'12am local time in UTC' =>
test_zci(re(qr/5:00 AM UTC/),
structured_answer => {
input => [qq/Midnight $test_location_tz to UTC/],
operation => 'Convert Timezone',
result => q/5:00 AM UTC/,
},
),
'12am in UTC' =>
test_zci(re(qr/5:00 AM UTC/),
structured_answer => {
input => [qq/Midnight $test_location_tz to UTC/],
operation => 'Convert Timezone',
result => q/5:00 AM UTC/,
},
),
'13:00 GMT in my time' => build_test(qq/8:00 EST/, qq/13:00 GMT to $test_location_tz/),
'13:00 GMT' => build_test(qq/8:00 EST/, qq/13:00 GMT to $test_location_tz/),
'11:22am cest in my timezone' => build_test(re(qr/4:22 AM EST/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest in localtime' => build_test(re(qr/4:22 AM EST/), qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest in my local timezone' => build_test(q/4:22 AM EST/, qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'11:22am cest' => build_test(q/4:22 AM EST/, qq/11:22 AM CEST (UTC+2) to $test_location_tz/),
'12pm my time in CEST' => build_test(q/7:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12pm local timezone in CEST' => build_test(q/7:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12pm in CEST' => build_test(q/7:00 PM CEST/, qq/Noon $test_location_tz to CEST (UTC+2)/),
'12am my timezone in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight $test_location_tz to UTC/),
'12am local time in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight $test_location_tz to UTC/),
'12am in UTC' => build_test(re(qr/5:00 AM UTC/), qq/Midnight $test_location_tz to UTC/),
);
restore_time();