210 lines
5.4 KiB
Crystal
210 lines
5.4 KiB
Crystal
|
|
require "./spec_helper"
|
|
|
|
require "../src/timecost/range"
|
|
|
|
describe TimeCost::Range do
|
|
|
|
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(
|
|
commit_hash: Random::Secure.hex(40),
|
|
author: author,
|
|
datetime: Time.utc,
|
|
message: "Commit base"
|
|
)
|
|
commit_overlap_before = TimeCost::Commit.new(
|
|
commit_hash: Random::Secure.hex(40),
|
|
author: author,
|
|
datetime: Time.utc - overlap_time,
|
|
message: "Commit with overlap before"
|
|
)
|
|
commit_overlap_after = TimeCost::Commit.new(
|
|
commit_hash: Random::Secure.hex(40),
|
|
author: author,
|
|
datetime: Time.utc + overlap_time,
|
|
message: "Commit with overlap after"
|
|
)
|
|
|
|
commit_separate_before = TimeCost::Commit.new(
|
|
commit_hash: Random::Secure.hex(40),
|
|
author: author,
|
|
datetime: Time.utc - separate_time,
|
|
message: "Commit separate before"
|
|
)
|
|
commit_separate_after = TimeCost::Commit.new(
|
|
commit_hash: Random::Secure.hex(40),
|
|
author: author,
|
|
datetime: Time.utc + separate_time,
|
|
message: "Commit separate after"
|
|
)
|
|
|
|
|
|
describe ".new" do
|
|
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 ".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
|
|
|
|
describe ".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 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 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
|
|
|
|
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
|
|
end
|
|
|