ParseCron: Switching to text template

master
Rob 2016-05-22 21:08:09 +01:00
parent 8e74d245c7
commit 3c2936c6c6
2 changed files with 61 additions and 340 deletions

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

@ -303,9 +303,13 @@ handle remainder => sub {
$time .= ' ' . $date unless $time =~ /^every / && $date eq 'every day';
return $time, structured_answer => {
input => [$line],
operation => 'Crontab',
result => $time
data => {
title => $time,
subtitle => "Crontab: $line"
},
templates => {
group => 'text'
}
};
} catch {
return;

391
t/ParseCron.t Normal file → Executable file
View File

@ -9,253 +9,66 @@ use DDG::Test::Goodie;
zci answer_type => 'parsecron';
zci is_cached => 0;
sub build_test {
my ($answer, $input) = @_;
return test_zci($answer, structured_answer => {
data => {
title => $answer,
subtitle => "Crontab: $input"
},
templates => {
group => 'text'
}
});
}
ddg_goodie_test(
[qw(
DDG::Goodie::ParseCron
)],
# Time
'cron * * * * *' => test_zci('every minute',
structured_answer => {
input => ['* * * * *'],
operation => 'Crontab',
result => 'every minute'
}),
'cron 5 0 * * *' => test_zci('at 12:05am every day',
structured_answer => {
input => ['5 0 * * *'],
operation => 'Crontab',
result => 'at 12:05am every day'
}),
'cron 0 */2 * * *' => test_zci('every other hour',
structured_answer => {
input => ['0 */2 * * *'],
operation => 'Crontab',
result => 'every other hour'
}),
'cron 0 0-23/2 * * *' => test_zci('every other hour from 12am to 11pm',
structured_answer => {
input => ['0 0-23/2 * * *'],
operation => 'Crontab',
result => 'every other hour from 12am to 11pm'
}),
'cron 15 0-23/2,10,14 * * *' => test_zci('15 minutes after every other hour from 12am to 11pm, 10am, and 2pm every day',
structured_answer => {
input => ['15 0-23/2,10,14 * * *'],
operation => 'Crontab',
result => '15 minutes after every other hour from 12am to 11pm, 10am, and 2pm every day'
}),
'cron 0,15,30,45 4 * * *' => test_zci('at 4:00am, 4:15am, 4:30am, and 4:45am every day',
structured_answer => {
input => ['0,15,30,45 4 * * *'],
operation => 'Crontab',
result => 'at 4:00am, 4:15am, 4:30am, and 4:45am every day'
}), # exact times are returned
'cron 45,15,30,15,0 4 * * *' => test_zci('at 4:00am, 4:15am, 4:30am, and 4:45am every day',
structured_answer => {
input => ['45,15,30,15,0 4 * * *'],
operation => 'Crontab',
result => 'at 4:00am, 4:15am, 4:30am, and 4:45am every day'
}),
'cron 0,15,30,45 4,6,7,8 * * *' => test_zci('0, 15, 30, and 45 minutes after 4am, 6am, 7am, and 8am every day',
structured_answer => {
input => ['0,15,30,45 4,6,7,8 * * *'],
operation => 'Crontab',
result => '0, 15, 30, and 45 minutes after 4am, 6am, 7am, and 8am every day'
}), # too many exact times
'cron 45,15,30,15,0 4,6,7,8 * * *' => test_zci('45, 15, 30, 15, and 0 minutes after 4am, 6am, 7am, and 8am every day',
structured_answer => {
input => ['45,15,30,15,0 4,6,7,8 * * *'],
operation => 'Crontab',
result => '45, 15, 30, 15, and 0 minutes after 4am, 6am, 7am, and 8am every day'
}),
'cron 5 1,4,6-7 * * *' => test_zci('at 1:05am, 4:05am, 6:05am, and 7:05am every day',
structured_answer => {
input => ['5 1,4,6-7 * * *'],
operation => 'Crontab',
result => 'at 1:05am, 4:05am, 6:05am, and 7:05am every day'
}),
'cron 5 13,5 * * *' => test_zci('at 5:05am and 1:05pm every day',
structured_answer => {
input => ['5 13,5 * * *'],
operation => 'Crontab',
result => 'at 5:05am and 1:05pm every day'
}),
'cron 1-9/2 * * * *' => test_zci('every other minute from 1 to 9 minutes after every hour',
structured_answer => {
input => ['1-9/2 * * * *'],
operation => 'Crontab',
result => 'every other minute from 1 to 9 minutes after every hour'
}),
'cron */5 4 * * *' => test_zci('every 5 minutes of 4am',
structured_answer => {
input => ['*/5 4 * * *'],
operation => 'Crontab',
result => 'every 5 minutes of 4am'
}),
'cron */5 * * * *' => test_zci('every 5 minutes',
structured_answer => {
input => ['*/5 * * * *'],
operation => 'Crontab',
result => 'every 5 minutes'
}),
'crontab * */3 * * *' => test_zci('every minute of every 3 hours',
structured_answer => {
input => ['* */3 * * *'],
operation => 'Crontab',
result => 'every minute of every 3 hours'
}),
'cron * * * * *' => build_test('every minute', '* * * * *'),
'cron 5 0 * * *' => build_test('at 12:05am every day', '5 0 * * *'),
'cron 0 */2 * * *' => build_test('every other hour', '0 */2 * * *'),
'cron 0 0-23/2 * * *' => build_test('every other hour from 12am to 11pm', '0 0-23/2 * * *'),
'cron 15 0-23/2,10,14 * * *' => build_test('15 minutes after every other hour from 12am to 11pm, 10am, and 2pm every day', '15 0-23/2,10,14 * * *'),
'cron 0,15,30,45 4 * * *' => build_test('at 4:00am, 4:15am, 4:30am, and 4:45am every day', '0,15,30,45 4 * * *'), # exact times are returned
'cron 45,15,30,15,0 4 * * *' => build_test('at 4:00am, 4:15am, 4:30am, and 4:45am every day', '45,15,30,15,0 4 * * *'),
'cron 0,15,30,45 4,6,7,8 * * *' => build_test('0, 15, 30, and 45 minutes after 4am, 6am, 7am, and 8am every day', '0,15,30,45 4,6,7,8 * * *'), # too many exact times
'cron 45,15,30,15,0 4,6,7,8 * * *' => build_test('45, 15, 30, 15, and 0 minutes after 4am, 6am, 7am, and 8am every day', '45,15,30,15,0 4,6,7,8 * * *'),
'cron 5 1,4,6-7 * * *' => build_test('at 1:05am, 4:05am, 6:05am, and 7:05am every day', '5 1,4,6-7 * * *'),
'cron 5 13,5 * * *' => build_test('at 5:05am and 1:05pm every day', '5 13,5 * * *'),
'cron 1-9/2 * * * *' => build_test('every other minute from 1 to 9 minutes after every hour', '1-9/2 * * * *'),
'cron */5 4 * * *' => build_test('every 5 minutes of 4am', '*/5 4 * * *'),
'cron */5 * * * *' => build_test('every 5 minutes', '*/5 * * * *'),
'crontab * */3 * * *' => build_test('every minute of every 3 hours', '* */3 * * *'),
# Dates
'cron 0 9 */3 * *' => test_zci('at 9:00am every 3 days',
structured_answer => {
input => ['0 9 */3 * *'],
operation => 'Crontab',
result => 'at 9:00am every 3 days'
}),
'cron 0 9 */1 * *' => test_zci('at 9:00am every day',
structured_answer => {
input => ['0 9 */1 * *'],
operation => 'Crontab',
result => 'at 9:00am every day'
}),
'cron 0 9 1-15/2 * *' => test_zci('at 9:00am every other day from 1st to 15th of every month',
structured_answer => {
input => ['0 9 1-15/2 * *'],
operation => 'Crontab',
result => 'at 9:00am every other day from 1st to 15th of every month'
}),
'cron 0 9 * 6-8 *' => test_zci('at 9:00am every day of June through August',
structured_answer => {
input => ['0 9 * 6-8 *'],
operation => 'Crontab',
result => 'at 9:00am every day of June through August'
}),
'cron 0 9 */5 6-8 *' => test_zci('at 9:00am every 5 days of June through August',
structured_answer => {
input => ['0 9 */5 6-8 *'],
operation => 'Crontab',
result => 'at 9:00am every 5 days of June through August'
}),
'cron 0 12 * */2 *' => test_zci('at 12:00pm every day of every other month',
structured_answer => {
input => ['0 12 * */2 *'],
operation => 'Crontab',
result => 'at 12:00pm every day of every other month'
}),
'cron 0 12 * * */2' => test_zci('at 12:00pm every other day of the week',
structured_answer => {
input => ['0 12 * * */2'],
operation => 'Crontab',
result => 'at 12:00pm every other day of the week'
}),
'cron 0 0 23 * *' => test_zci('at midnight on the 23rd of every month',
structured_answer => {
input => ['0 0 23 * *'],
operation => 'Crontab',
result => 'at midnight on the 23rd of every month'
}), # test ordinal suffixes
'cron 0 0 13 * *' => test_zci('at midnight on the 13th of every month',
structured_answer => {
input => ['0 0 13 * *'],
operation => 'Crontab',
result => 'at midnight on the 13th of every month'
}),
'cron 0 0 22 * *' => test_zci('at midnight on the 22nd of every month',
structured_answer => {
input => ['0 0 22 * *'],
operation => 'Crontab',
result => 'at midnight on the 22nd of every month'
}),
'cron 0 0 21 * *' => test_zci('at midnight on the 21st of every month',
structured_answer => {
input => ['0 0 21 * *'],
operation => 'Crontab',
result => 'at midnight on the 21st of every month'
}),
'cron 0 0 20 * *' => test_zci('at midnight on the 20th of every month',
structured_answer => {
input => ['0 0 20 * *'],
operation => 'Crontab',
result => 'at midnight on the 20th of every month'
}),
'cron 0 0 1,14 1-11 *' => test_zci('at midnight on the 1st and 14th of January through November',
structured_answer => {
input => ['0 0 1,14 1-11 *'],
operation => 'Crontab',
result => 'at midnight on the 1st and 14th of January through November'
}),
'cron 0 0 * * 1-5' => test_zci('at midnight Monday through Friday',
structured_answer => {
input => ['0 0 * * 1-5'],
operation => 'Crontab',
result => 'at midnight Monday through Friday'
}),
'cron 0 0 * 12 1-5' => test_zci('at midnight Monday through Friday in December',
structured_answer => {
input => ['0 0 * 12 1-5'],
operation => 'Crontab',
result => 'at midnight Monday through Friday in December'
}),
'cron 0 0 23 * 1-5' => test_zci('at midnight on the 23rd and Monday through Friday',
structured_answer => {
input => ['0 0 23 * 1-5'],
operation => 'Crontab',
result => 'at midnight on the 23rd and Monday through Friday'
}),
'cron 0 0 23 1 1-5' => test_zci('at midnight on the 23rd and Monday through Friday in January',
structured_answer => {
input => ['0 0 23 1 1-5'],
operation => 'Crontab',
result => 'at midnight on the 23rd and Monday through Friday in January'
}),
'cron 0 0 * * 1-5' => test_zci('at midnight Monday through Friday',
structured_answer => {
input => ['0 0 * * 1-5'],
operation => 'Crontab',
result => 'at midnight Monday through Friday'
}),
'cron 0 0 * * Sat-Sun' => test_zci('at midnight Saturday through Sunday',
structured_answer => {
input => ['0 0 * * Sat-Sun'],
operation => 'Crontab',
result => 'at midnight Saturday through Sunday'
}),
'cron 0 0 * DEC,Jan-feb Sat-Sun' => test_zci('at midnight Saturday through Sunday in December and January through February',
structured_answer => {
input => ['0 0 * DEC,Jan-feb Sat-Sun'],
operation => 'Crontab',
result => 'at midnight Saturday through Sunday in December and January through February'
}),
'cron 0 0 * * 0' => test_zci('at midnight on Sunday',
structured_answer => {
input => ['0 0 * * 0'],
operation => 'Crontab',
result => 'at midnight on Sunday'
}),
'cron 0 0 * * 7' => test_zci('at midnight on Sunday',
structured_answer => {
input => ['0 0 * * 7'],
operation => 'Crontab',
result => 'at midnight on Sunday'
}),
'cron 0/10 0/4 * * 0' => test_zci('0, 10, 20, 30, 40, and 50 minutes after 12am, 4am, 8am, 12pm, 4pm, and 8pm on Sunday',
structured_answer => {
input => ['0/10 0/4 * * 0'],
operation => 'Crontab',
result => '0, 10, 20, 30, 40, and 50 minutes after 12am, 4am, 8am, 12pm, 4pm, and 8pm on Sunday'
}),
'cron 0/10 0/4 1/3 1/2 0/2' => test_zci('0, 10, 20, 30, 40, and 50 minutes after 12am, 4am, 8am, 12pm, 4pm, and 8pm on the 1st, 4th, 7th, 10th, 13th, 16th, 19th, 22nd, 25th, 28th, and 31st and on Sunday, Tuesday, Thursday, and Saturday in January, March, May, July, September, and November',
structured_answer => {
input => ['0/10 0/4 1/3 1/2 0/2'],
operation => 'Crontab',
result => '0, 10, 20, 30, 40, and 50 minutes after 12am, 4am, 8am, 12pm, 4pm, and 8pm on the 1st, 4th, 7th, 10th, 13th, 16th, 19th, 22nd, 25th, 28th, and 31st and on Sunday, Tuesday, Thursday, and Saturday in January, March, May, July, September, and November'
}),
'cron * * * * 3/2' => test_zci('every minute on Wednesday and Friday',
structured_answer => {
input => ['* * * * 3/2'],
operation => 'Crontab',
result => 'every minute on Wednesday and Friday'
}),
'cron 0 9 */3 * *' => build_test('at 9:00am every 3 days', '0 9 */3 * *'),
'cron 0 9 */1 * *' => build_test('at 9:00am every day', '0 9 */1 * *'),
'cron 0 9 1-15/2 * *' => build_test('at 9:00am every other day from 1st to 15th of every month', '0 9 1-15/2 * *'),
'cron 0 9 * 6-8 *' => build_test('at 9:00am every day of June through August', '0 9 * 6-8 *'),
'cron 0 9 */5 6-8 *' => build_test('at 9:00am every 5 days of June through August', '0 9 */5 6-8 *'),
'cron 0 12 * */2 *' => build_test('at 12:00pm every day of every other month', '0 12 * */2 *'),
'cron 0 12 * * */2' => build_test('at 12:00pm every other day of the week', '0 12 * * */2'),
'cron 0 0 23 * *' => build_test('at midnight on the 23rd of every month', '0 0 23 * *'),# test ordinal suffixes
'cron 0 0 13 * *' => build_test('at midnight on the 13th of every month', '0 0 13 * *'),
'cron 0 0 22 * *' => build_test('at midnight on the 22nd of every month', '0 0 22 * *'),
'cron 0 0 21 * *' => build_test('at midnight on the 21st of every month', '0 0 21 * *'),
'cron 0 0 20 * *' => build_test('at midnight on the 20th of every month', '0 0 20 * *'),
'cron 0 0 1,14 1-11 *' => build_test('at midnight on the 1st and 14th of January through November', '0 0 1,14 1-11 *'),
'cron 0 0 * * 1-5' => build_test('at midnight Monday through Friday', '0 0 * * 1-5'),
'cron 0 0 * 12 1-5' => build_test('at midnight Monday through Friday in December', '0 0 * 12 1-5'),
'cron 0 0 23 * 1-5' => build_test('at midnight on the 23rd and Monday through Friday', '0 0 23 * 1-5'),
'cron 0 0 23 1 1-5' => build_test('at midnight on the 23rd and Monday through Friday in January', '0 0 23 1 1-5'),
'cron 0 0 * * 1-5' => build_test('at midnight Monday through Friday', '0 0 * * 1-5'),
'cron 0 0 * * Sat-Sun' => build_test('at midnight Saturday through Sunday', '0 0 * * Sat-Sun'),
'cron 0 0 * DEC,Jan-feb Sat-Sun' => build_test('at midnight Saturday through Sunday in December and January through February', '0 0 * DEC,Jan-feb Sat-Sun'),
'cron 0 0 * * 0' => build_test('at midnight on Sunday', '0 0 * * 0'),
'cron 0 0 * * 7' => build_test('at midnight on Sunday', '0 0 * * 7'),
'cron 0/10 0/4 * * 0' => build_test('0, 10, 20, 30, 40, and 50 minutes after 12am, 4am, 8am, 12pm, 4pm, and 8pm on Sunday', '0/10 0/4 * * 0'),
'cron 0/10 0/4 1/3 1/2 0/2' => build_test('0, 10, 20, 30, 40, and 50 minutes after 12am, 4am, 8am, 12pm, 4pm, and 8pm on the 1st, 4th, 7th, 10th, 13th, 16th, 19th, 22nd, 25th, 28th, and 31st and on Sunday, Tuesday, Thursday, and Saturday in January, March, May, July, September, and November', '0/10 0/4 1/3 1/2 0/2'),
'cron * * * * 3/2' => build_test('every minute on Wednesday and Friday', '* * * * 3/2'),
# Syntax errors
'cron 0 0 * *' => undef,
@ -269,104 +82,8 @@ ddg_goodie_test(
'cron 0/10 0/4 0/3 1/2 0/2' => undef,
'cron 0/10 0/4 1/3 0/2 0/2' => undef,
# 'cron 96 4 * * *' => test_zci('Invalid minute 96',
# structured_answer => {
# input => ['96 4 * * *'],
# operation => 'Crontab',
# result => 'Invalid minute 96'
# }),
# 'cron 6 45 * * *' => test_zci('Invalid hour 45',
# structured_answer => {
# input => ['6 45 * * *'],
# operation => 'Crontab',
# result => 'Invalid hour 45'
# }),
# 'cron 15,1-93 5 * * *' => test_zci('Invalid minute 93',
# structured_answer => {
# input => ['15,1-93 5 * * *'],
# operation => 'Crontab',
# result => 'Invalid minute 93'
# }),
# 'cron 15,93-7 5 * * *' => test_zci('Invalid minute 93',
# structured_answer => {
# input => ['15,93-7 5 * * *'],
# operation => 'Crontab',
# result => 'Invalid minute 93'
# }),
# 'cron 1 50,16 * * *' => test_zci('Invalid hour 50',
# structured_answer => {
# input => ['1 50,16 * * *'],
# operation => 'Crontab',
# result => 'Invalid hour 50'
# }),
# 'cron 0 0 32 * *' => test_zci('Invalid day 32',
# structured_answer => {
# input => ['0 0 32 * *'],
# operation => 'Crontab',
# result => 'Invalid day 32'
# }),
# 'cron 0 0 0 * *' => test_zci('Invalid day 0',
# structured_answer => {
# input => ['0 0 0 * *'],
# operation => 'Crontab',
# result => 'Invalid day 0'
# }),
# 'cron 0 0 2 0 *' => test_zci('Invalid month 0',
# structured_answer => {
# input => ['0 0 2 0 *'],
# operation => 'Crontab',
# result => 'Invalid month 0'
# }),
# 'cron 0 0 * * 8' => test_zci('Invalid day of the week 8',
# structured_answer => {
# input => ['0 0 * * 8'],
# operation => 'Crontab',
# result => 'Invalid day of the week 8'
# }),
# 'cron 0 0 * * -1' => test_zci('Invalid day of the week -1',
# structured_answer => {
# input => ['0 0 * * -1'],
# operation => 'Crontab',
# result => 'Invalid day of the week -1'
# }),
# 'cron 0 0 * ABC *' => test_zci('Invalid month ABC',
# structured_answer => {
# input => ['0 0 * ABC *'],
# operation => 'Crontab',
# result => 'Invalid month ABC'
# }),
# 'cron 0 0 ABC * *' => test_zci('Invalid day ABC',
# structured_answer => {
# input => ['0 0 ABC * *'],
# operation => 'Crontab',
# result => 'Invalid day ABC'
# }),
# 'cron ! * * * *' => test_zci('Invalid minute !',
# structured_answer => {
# input => ['! * * * *'],
# operation => 'Crontab',
# result => 'Invalid minute !'
# }),
# 'cron 0 9 */90 * *' => test_zci('Invalid day 90',
# structured_answer => {
# input => ['0 9 */90 * *'],
# operation => 'Crontab',
# result => 'Invalid day 90'
# }),
# 'cron 0 9 */0 * *' => test_zci('Invalid day 0',
# structured_answer => {
# input => ['0 9 */0 * *'],
# operation => 'Crontab',
# result => 'Invalid day 0'
# }),
# Complex examples
'crontab 42 12 3 Feb Sat' => test_zci('at 12:42pm on the 3rd and on Saturday in February',
structured_answer => {
input => ['42 12 3 Feb Sat'],
operation => 'Crontab',
result => 'at 12:42pm on the 3rd and on Saturday in February'
}),
'crontab 42 12 3 Feb Sat' => build_test('at 12:42pm on the 3rd and on Saturday in February', '42 12 3 Feb Sat'),
);
done_testing;