diff --git a/spec/profile_spec.cr b/spec/profile_spec.cr index 272584f..5f0206c 100644 --- a/spec/profile_spec.cr +++ b/spec/profile_spec.cr @@ -74,5 +74,25 @@ module Dayoff::Test expected = 8 * 3 - 10 * 2 expected.should eq span.total_hours 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 diff --git a/src/dayoff/exceptions.cr b/src/dayoff/exceptions.cr index 0399476..5e5eb22 100644 --- a/src/dayoff/exceptions.cr +++ b/src/dayoff/exceptions.cr @@ -4,4 +4,7 @@ module Dayoff class StartedRecordNotFound < Exception end + + class AlreadyStarted < Exception + end end diff --git a/src/dayoff/profile.cr b/src/dayoff/profile.cr index 02f6eca..dba2698 100644 --- a/src/dayoff/profile.cr +++ b/src/dayoff/profile.cr @@ -35,6 +35,9 @@ module Dayoff end def start(time : Time) : Nil + if started_point + raise AlreadyStarted.new + end @wrecords.each do |wr| if time <= wr.start || time <= wr.finish! raise CrossedTimeSpan.new @@ -46,7 +49,7 @@ module Dayoff end def finish(time : Time) : Nil - started = @wrecords.find { |x| x.started? } + started = started_point if started.nil? raise StartedRecordNotFound.new end @@ -59,5 +62,9 @@ module Dayoff worked = get_work_hours on_time planned - worked end + + private def started_point + @wrecords.find { |x| x.started? } + end end end