Add validation

This commit is contained in:
Anton Vakhrushev 2019-11-06 21:05:46 +03:00
parent 81da30fb01
commit 0f01d2ad01
3 changed files with 31 additions and 1 deletions

View File

@ -74,5 +74,25 @@ module Dayoff::Test
expected = 8 * 3 - 10 * 2 expected = 8 * 3 - 10 * 2
expected.should eq span.total_hours expected.should eq span.total_hours
end end
it "not start twice" do
prof = create_profile
start_time = t(3, 10, 0)
prof.start start_time
expect_raises(AlreadyStarted) do
prof.start start_time
end
end
it "not finish twice" do
prof = create_profile
start_time = t(3, 10, 0)
finish_time = t(3, 20, 0)
prof.start start_time
prof.finish finish_time
expect_raises(StartedRecordNotFound) do
prof.finish finish_time
end
end
end end
end end

View File

@ -4,4 +4,7 @@ module Dayoff
class StartedRecordNotFound < Exception class StartedRecordNotFound < Exception
end end
class AlreadyStarted < Exception
end
end end

View File

@ -35,6 +35,9 @@ module Dayoff
end end
def start(time : Time) : Nil def start(time : Time) : Nil
if started_point
raise AlreadyStarted.new
end
@wrecords.each do |wr| @wrecords.each do |wr|
if time <= wr.start || time <= wr.finish! if time <= wr.start || time <= wr.finish!
raise CrossedTimeSpan.new raise CrossedTimeSpan.new
@ -46,7 +49,7 @@ module Dayoff
end end
def finish(time : Time) : Nil def finish(time : Time) : Nil
started = @wrecords.find { |x| x.started? } started = started_point
if started.nil? if started.nil?
raise StartedRecordNotFound.new raise StartedRecordNotFound.new
end end
@ -59,5 +62,9 @@ module Dayoff
worked = get_work_hours on_time worked = get_work_hours on_time
planned - worked planned - worked
end end
private def started_point
@wrecords.find { |x| x.started? }
end
end end
end end