diff options
| author | Alban Peignier | 2017-12-27 21:23:07 +0100 | 
|---|---|---|
| committer | Alban Peignier | 2018-01-05 10:23:29 +0100 | 
| commit | 7a65a9fbe0cefdf504e7d735003065d8bb874f1e (patch) | |
| tree | 0763c07455253e3fdc6ed47d503a2cbbf03c5efc | |
| parent | 97a9a7d921d412fd8300c9d84e719ee494079c3b (diff) | |
| download | chouette-core-7a65a9fbe0cefdf504e7d735003065d8bb874f1e.tar.bz2 | |
Add Range#intersect?. Refs #5299
| -rw-r--r-- | lib/range_ext.rb | 6 | ||||
| -rw-r--r-- | spec/lib/range_ext_spec.rb | 28 | 
2 files changed, 32 insertions, 2 deletions
| diff --git a/lib/range_ext.rb b/lib/range_ext.rb index a6a3bfc5d..e7e0e903f 100644 --- a/lib/range_ext.rb +++ b/lib/range_ext.rb @@ -1,11 +1,15 @@  class Range    def intersection(other) -    return nil if (self.max < other.min or other.max < self.min) +    return nil unless intersect?(other)      [self.min, other.min].max..[self.max, other.max].min    end    alias_method :&, :intersection +  def intersect?(other) +    self.max > other.min and other.max > self.min +  end +    def remove(other)      return self if (self.max < other.min or other.max < self.min) diff --git a/spec/lib/range_ext_spec.rb b/spec/lib/range_ext_spec.rb index cae637e47..eee488c91 100644 --- a/spec/lib/range_ext_spec.rb +++ b/spec/lib/range_ext_spec.rb @@ -1,6 +1,6 @@  require 'range_ext'  RSpec.describe Range do -  context "intersection" do +  describe "#intersection" do      it "is nil (sic) for two distinct ranges" do        expect( (1..2).intersection(3..4) ).to be_nil      end @@ -16,6 +16,32 @@ RSpec.describe Range do      end    end +  describe "intersect?" do +    it 'is true when the given range includes begin' do +      expect( (2..4).intersect? (1..3) ).to be_truthy +    end + +    it 'is true when the given range includes end' do +      expect( (2..4).intersect? (3..5) ).to be_truthy +    end + +    it 'is true when the given range includes both begin and end' do +      expect( (2..4).intersect? (1..5) ).to be_truthy +    end + +    it 'is true when the given range is the same' do +      expect( (2..4).intersect? (2..4) ).to be_truthy +    end + +    it 'is false when the given range is after' do +      expect( (2..4).intersect? (5..7) ).to be_falsey +    end + +    it 'is false when the given range is before' do +      expect( (2..4).intersect? (0..2) ).to be_falsey +    end +  end +    context "remove" do      it "is unchanged when the given range has no intersection" do        expect( (1..2).remove(3..4) ).to eq 1..2 | 
