From 90e9ecf5097ab48418d2340960f697fb133e7892 Mon Sep 17 00:00:00 2001 From: Glenn Date: Wed, 12 Jul 2023 00:22:07 +0200 Subject: [PATCH] refactor: class range --- spec/range_spec.cr | 73 +++++++++++++++++++++++++++++++++++++------ src/timecost/range.cr | 16 +++++----- 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/spec/range_spec.cr b/spec/range_spec.cr index 7406760..4d946bb 100644 --- a/spec/range_spec.cr +++ b/spec/range_spec.cr @@ -4,6 +4,7 @@ require "./spec_helper" require "../src/timecost/range" describe TimeCost::Range do + epsilon = 0.5 overlap_time = (epsilon / 2).hours separate_time = (epsilon * 2).hours @@ -39,25 +40,79 @@ describe TimeCost::Range do datetime: Time.utc + separate_time, ) - rangeA = TimeCost::Range.new( - commit: commit_base, - epsilon: epsilon - ) describe ".new" do - it "can be created from " do - assert_instance_of TimeCost::Range, rangeA + it "can be created without epsilon" do + range_base = TimeCost::Range.new( + commit: commit_base + ) + range_base.should be_a(TimeCost::Range) + end + + it "can be created with epsilon" do + range_base = TimeCost::Range.new( + commit: commit_base, + epsilon: epsilon + ) + range_base.should be_a(TimeCost::Range) end end describe ".overlap?" do - it "must respond to .overlap?" do + it "must return true when range overlaps range before" do + range_base = TimeCost::Range.new( + commit: commit_base + ) + range_before = TimeCost::Range.new( + commit: commit_overlap_before + ) + overlap1 = range_base.overlap? range_before + overlap1.should be_true + + overlap2 = range_before.overlap? range_base + overlap2.should be_true end - it "must return false when ranges are not overlapping" do + it "must return true when range overlaps range after" do + range_base = TimeCost::Range.new( + commit: commit_base + ) + range_after = TimeCost::Range.new( + commit: commit_overlap_after + ) + overlap1 = range_base.overlap? range_after + overlap1.should be_true + + overlap2 = range_after.overlap? range_base + overlap2.should be_true end - it "must return true when ranges are overlapping" do + it "must return false when range does not overlap range before" do + range_base = TimeCost::Range.new( + commit: commit_base + ) + range_before = TimeCost::Range.new( + commit: commit_separate_before + ) + overlap1 = range_base.overlap? range_before + overlap1.should be_false + + overlap2 = range_before.overlap? range_base + overlap2.should be_false + end + + it "must return false when range does not overlap range after" do + range_base = TimeCost::Range.new( + commit: commit_base + ) + range_after = TimeCost::Range.new( + commit: commit_separate_after + ) + overlap1 = range_base.overlap? range_after + overlap1.should be_false + + overlap2 = range_after.overlap? range_base + overlap2.should be_false end end end diff --git a/src/timecost/range.cr b/src/timecost/range.cr index 29d760a..ef7a090 100644 --- a/src/timecost/range.cr +++ b/src/timecost/range.cr @@ -78,26 +78,26 @@ module TimeCost stop_after_start = (range.time_stop >= @time_start) # A case - if (start_before_start and start_before_stop and - stop_after_start and stop_before_stop) + if (start_before_start && start_before_stop && + stop_after_start && stop_before_stop) result = true end # B case - if (start_after_start and start_before_stop and - stop_after_start and stop_after_stop) + if (start_after_start && start_before_stop && + stop_after_start && stop_after_stop) result = true end # C case - if (start_before_start and start_before_stop and - stop_after_start and stop_after_stop) + if (start_before_start && start_before_stop && + stop_after_start && stop_after_stop) result = true end # D case - if (start_after_start and start_before_stop and - stop_after_start and stop_before_stop) + if (start_after_start && start_before_stop && + stop_after_start && stop_before_stop) result = true end