git-timecost/spec/range_spec.cr

211 lines
5.4 KiB
Crystal
Raw Normal View History

2023-07-11 16:52:48 +00:00
require "./spec_helper"
require "../src/timecost/range"
describe TimeCost::Range do
2023-07-11 22:22:07 +00:00
2023-07-11 16:52:48 +00:00
epsilon = 0.5
overlap_time = (epsilon / 2).hours
separate_time = (epsilon * 2).hours
author = TimeCost::Author.new(
name: "Jon Snow",
email: "jon.snow@example.com"
)
commit_base = TimeCost::Commit.new(
2023-07-12 09:27:21 +00:00
commit_hash: Random::Secure.hex(40),
2023-07-11 16:52:48 +00:00
author: author,
datetime: Time.utc,
2023-07-12 09:27:21 +00:00
message: "Commit base"
2023-07-11 16:52:48 +00:00
)
commit_overlap_before = TimeCost::Commit.new(
2023-07-12 09:27:21 +00:00
commit_hash: Random::Secure.hex(40),
2023-07-11 16:52:48 +00:00
author: author,
datetime: Time.utc - overlap_time,
2023-07-12 09:27:21 +00:00
message: "Commit with overlap before"
2023-07-11 16:52:48 +00:00
)
commit_overlap_after = TimeCost::Commit.new(
2023-07-12 09:27:21 +00:00
commit_hash: Random::Secure.hex(40),
2023-07-11 16:52:48 +00:00
author: author,
datetime: Time.utc + overlap_time,
2023-07-12 09:27:21 +00:00
message: "Commit with overlap after"
2023-07-11 16:52:48 +00:00
)
commit_separate_before = TimeCost::Commit.new(
2023-07-12 09:27:21 +00:00
commit_hash: Random::Secure.hex(40),
2023-07-11 16:52:48 +00:00
author: author,
datetime: Time.utc - separate_time,
2023-07-12 09:27:21 +00:00
message: "Commit separate before"
2023-07-11 16:52:48 +00:00
)
commit_separate_after = TimeCost::Commit.new(
2023-07-12 09:27:21 +00:00
commit_hash: Random::Secure.hex(40),
2023-07-11 16:52:48 +00:00
author: author,
datetime: Time.utc + separate_time,
2023-07-12 09:27:21 +00:00
message: "Commit separate after"
2023-07-11 16:52:48 +00:00
)
describe ".new" do
2023-07-11 22:22:07 +00:00
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)
2023-07-11 16:52:48 +00:00
end
end
2023-07-12 09:27:21 +00:00
describe ".to_s" do
it "must display something" do
range_base = TimeCost::Range.new(
commit: commit_base
)
str = range_base.to_s
puts str
end
end
2023-07-11 16:52:48 +00:00
describe ".overlap?" do
2023-07-11 22:22:07 +00:00
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
2023-07-11 16:52:48 +00:00
end
2023-07-11 22:22:07 +00:00
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
2023-07-11 16:52:48 +00:00
end
2023-07-11 22:22:07 +00:00
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
2023-07-11 16:52:48 +00:00
end
end
2023-07-12 09:27:21 +00:00
describe ".add" do
pending "must add the commit to the range"
pending "must not duplicate existing commits"
pending "must change the boundaries"
end
describe ".merge" do
it "must return a merged range with all commits" do
range_base = TimeCost::Range.new(
commit: commit_base
)
range_before = TimeCost::Range.new(
commit: commit_overlap_before
)
range_after = TimeCost::Range.new(
commit: commit_overlap_after
)
range_result1 = range_base.merge(range_before)
range_result1.commits.size.should eq(2)
range_result2 = range_base.merge(range_after)
range_result2.commits.size.should eq(2)
end
pending "must not include a commit twice"
it "must return a merged range with correct boundaries" do
range_base = TimeCost::Range.new(
commit: commit_base
)
range_before = TimeCost::Range.new(
commit: commit_overlap_before
)
range_after = TimeCost::Range.new(
commit: commit_overlap_after
)
range_result1 = range_base.merge(range_before)
range_result1.time_start.should eq(range_before.time_start)
range_result1.time_stop.should eq(range_base.time_stop)
range_result2 = range_base.merge(range_after)
range_result2.time_start.should eq(range_base.time_start)
range_result2.time_stop.should eq(range_after.time_stop)
end
it "must fail with error when separate before" do
range_base = TimeCost::Range.new(
commit: commit_base
)
range_before = TimeCost::Range.new(
commit: commit_separate_before
)
expect_raises(TimeCost::Range::MissingOverlapError) do
range_base.merge(range_before)
end
end
it "must fail with error when separate after" do
range_base = TimeCost::Range.new(
commit: commit_base
)
range_after = TimeCost::Range.new(
commit: commit_separate_after
)
expect_raises(TimeCost::Range::MissingOverlapError) do
range_base.merge(range_after)
end
end
end
2023-07-11 16:52:48 +00:00
end