Skip to content

Commit feefd54

Browse files
authored
Merge pull request #932 from mbj/add/range-overlap
Addrange Mutant::Range.overlap?
2 parents c4de647 + 2ebccf2 commit feefd54

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

lib/mutant.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ module Mutant
182182
require 'mutant/repository'
183183
require 'mutant/variable'
184184
require 'mutant/zombifier'
185+
require 'mutant/range'
185186

186187
module Mutant
187188
WORLD = World.new(

lib/mutant/range.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module Mutant
4+
module Range
5+
# Test if two ranges overlap
6+
#
7+
# @param [Range] left
8+
# @param [Range] right
9+
#
10+
# @return [Boolean]
11+
def self.overlap?(left, right)
12+
left.end >= right.begin && right.end >= left.begin
13+
end
14+
end # Range
15+
end # end

spec/unit/mutant/range_spec.rb

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# frozen_string_literal: true
2+
3+
describe Mutant::Range do
4+
describe '.overlap?' do
5+
def apply
6+
described_class.overlap?(left, right)
7+
end
8+
9+
context 'no overlap left before right' do
10+
# |---|
11+
# |---|
12+
let(:left) { 1..2 }
13+
let(:right) { 3..4 }
14+
15+
it 'returns false' do
16+
expect(apply).to be(false)
17+
end
18+
end
19+
20+
context 'no overlap right before left' do
21+
# |---|
22+
# |---|
23+
let(:left) { 3..4 }
24+
let(:right) { 1..2 }
25+
26+
it 'returns false' do
27+
expect(apply).to be(false)
28+
end
29+
end
30+
31+
context 'left includes right' do
32+
# |----------------|
33+
# |---|
34+
let(:left) { 1..4 }
35+
let(:right) { 2..3 }
36+
37+
it 'returns true' do
38+
expect(apply).to be(true)
39+
end
40+
end
41+
42+
context 'right includes left' do
43+
# |---|
44+
# |----------------|
45+
let(:left) { 2..3 }
46+
let(:right) { 1..4 }
47+
48+
it 'returns true' do
49+
expect(apply).to be(true)
50+
end
51+
end
52+
53+
context 'right starts with left end' do
54+
# |----|
55+
# |----|
56+
let(:left) { 1..2 }
57+
let(:right) { 2..3 }
58+
59+
it 'returns true' do
60+
expect(apply).to be(true)
61+
end
62+
end
63+
64+
context 'left starts with right end' do
65+
# |----|
66+
# |----|
67+
let(:left) { 2..3 }
68+
let(:right) { 1..2 }
69+
70+
it 'returns true' do
71+
expect(apply).to be(true)
72+
end
73+
end
74+
75+
context 'left starts with right start' do
76+
# |----|
77+
# |---------|
78+
let(:left) { 1..2 }
79+
let(:right) { 1..3 }
80+
81+
it 'returns true' do
82+
expect(apply).to be(true)
83+
end
84+
end
85+
86+
context 'left starts with right start' do
87+
# |---------|
88+
# |----|
89+
let(:left) { 1..3 }
90+
let(:right) { 1..2 }
91+
92+
it 'returns true' do
93+
expect(apply).to be(true)
94+
end
95+
end
96+
97+
context 'left ends with right end' do
98+
# |----|
99+
# |---------|
100+
let(:left) { 2..3 }
101+
let(:right) { 1..3 }
102+
103+
it 'returns true' do
104+
expect(apply).to be(true)
105+
end
106+
end
107+
108+
context 'right ends with left end' do
109+
# |---------|
110+
# |----|
111+
let(:left) { 1..3 }
112+
let(:right) { 2..3 }
113+
114+
it 'returns true' do
115+
expect(apply).to be(true)
116+
end
117+
end
118+
119+
context 'left end intersects with right' do
120+
# |---------|
121+
# |----------|
122+
let(:left) { 1..3 }
123+
let(:right) { 2..4 }
124+
125+
it 'returns true' do
126+
expect(apply).to be(true)
127+
end
128+
end
129+
130+
context 'right end intersects with left' do
131+
# |----------|
132+
# |---------|
133+
let(:left) { 2..4 }
134+
let(:right) { 1..2 }
135+
136+
it 'returns true' do
137+
expect(apply).to be(true)
138+
end
139+
end
140+
end
141+
end

0 commit comments

Comments
 (0)