Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

TimeConverter: Raise exception when start_hour is not in hour_starts #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/sirius/time_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def initialize( hour_starts:, hour_length:)
# @return [Period] Event start and end times
#
def convert_time( start_hour, duration )
raise "Invalid start hour (#{start_hour}) or duration (#{duration})" if start_hour <= 0 || duration <= 0
if start_hour <= 0 || duration <= 0 || !@hour_starts.key?(start_hour - 1)
raise "Invalid start hour (#{start_hour}) or duration (#{duration})"
end
start_time = @hour_starts[start_hour - 1]
end_time = start_time + (@hour_length * duration).minutes
Period.new(start_time, end_time)
Expand Down
6 changes: 5 additions & 1 deletion spec/lib/sirius/time_converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
expect(result).to eq(Period.parse('18:00','20:15'))
end

it 'raises error with invalid start time' do
it 'raises error with zero start_hour' do
expect { converter.convert_time(0, 2) }.to raise_error(RuntimeError)
end

it 'raises error with unknown start_hour' do
expect { converter.convert_time(66, 2) }.to raise_error(RuntimeError)
end

it 'raises error with zero duration' do
expect { converter.convert_time(5, 0) }.to raise_error(RuntimeError)
end
Expand Down