diff --git a/lib/sirius/time_converter.rb b/lib/sirius/time_converter.rb index b4b94e2..78938b9 100644 --- a/lib/sirius/time_converter.rb +++ b/lib/sirius/time_converter.rb @@ -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) diff --git a/spec/lib/sirius/time_converter_spec.rb b/spec/lib/sirius/time_converter_spec.rb index e187cb8..ee33b6e 100644 --- a/spec/lib/sirius/time_converter_spec.rb +++ b/spec/lib/sirius/time_converter_spec.rb @@ -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