refactor: class range

This commit is contained in:
Glenn Y. Rolland 2023-07-12 00:22:07 +02:00
parent 5d13bc4d5e
commit 90e9ecf509
2 changed files with 72 additions and 17 deletions

View file

@ -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

View file

@ -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