Raise an exception when trying to use an :at with the cron shortcuts.

This commit is contained in:
Javan Makhmali 2009-04-30 16:45:07 -07:00
parent f2307605d8
commit 801c2ad7e1
2 changed files with 40 additions and 2 deletions

View File

@ -27,7 +27,7 @@ module Whenever
protected
def parse_symbol
case @time
shortcut = case @time
when :reboot then '@reboot'
when :year, :yearly then '@annually'
when :day, :daily then '@daily'
@ -35,7 +35,16 @@ module Whenever
when :month, :monthly then '@monthly'
when :week, :weekly then '@weekly'
when :hour, :hourly then '@hourly'
else parse_as_string
end
if shortcut
if @at > 0
raise ArgumentError, "You cannot specify an ':at' when using the shortcuts for times."
else
return shortcut
end
else
parse_as_string
end
end

View File

@ -166,6 +166,35 @@ class CronTest < Test::Unit::TestCase
assert_equal '0 7 * * sat,sun', parse_time('Weekends', nil, "7am")
end
end
context "When parsing time using the cron shortcuts" do
should "parse a :symbol into the correct shortcut" do
assert_equal '@reboot', parse_time(:reboot)
assert_equal '@annually', parse_time(:year)
assert_equal '@annually', parse_time(:yearly)
assert_equal '@daily', parse_time(:day)
assert_equal '@daily', parse_time(:daily)
assert_equal '@midnight', parse_time(:midnight)
assert_equal '@monthly', parse_time(:month)
assert_equal '@monthly', parse_time(:monthly)
assert_equal '@hourly', parse_time(:hour)
assert_equal '@hourly', parse_time(:hourly)
end
should "raise an exception if a valid shortcut is given but also an :at" do
assert_raises ArgumentError do
parse_time(:hour, nil, "1:00 am")
end
assert_raises ArgumentError do
parse_time(:reboot, nil, 5)
end
assert_raises ArgumentError do
parse_time(:day, nil, '4:20pm')
end
end
end
private