summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range/overlap_spec.rb
blob: 9b6fc1349342081d75d79b9735c1d5ea2cb14572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require_relative '../../spec_helper'

ruby_version_is '3.3' do
  describe "Range#overlap?" do
    it "returns true if other Range overlaps self" do
      (0..2).overlap?(1..3).should == true
      (1..3).overlap?(0..2).should == true
      (0..2).overlap?(0..2).should == true
      (0..3).overlap?(1..2).should == true
      (1..2).overlap?(0..3).should == true

      ('a'..'c').overlap?('b'..'d').should == true
    end

    it "returns false if other Range does not overlap self" do
      (0..2).overlap?(3..4).should == false
      (0..2).overlap?(-4..-1).should == false

      ('a'..'c').overlap?('d'..'f').should == false
    end

    it "raises TypeError when called with non-Range argument" do
      -> {
        (0..2).overlap?(1)
      }.should raise_error(TypeError, "wrong argument type Integer (expected Range)")
    end

    it "returns true when beginningless and endless Ranges overlap" do
      (0..2).overlap?(..3).should == true
      (0..2).overlap?(..1).should == true
      (0..2).overlap?(..0).should == true

      (..3).overlap?(0..2).should == true
      (..1).overlap?(0..2).should == true
      (..0).overlap?(0..2).should == true

      (0..2).overlap?(-1..).should == true
      (0..2).overlap?(1..).should == true
      (0..2).overlap?(2..).should == true

      (-1..).overlap?(0..2).should == true
      (1..).overlap?(0..2).should == true
      (2..).overlap?(0..2).should == true

      (0..).overlap?(2..).should == true
      (..0).overlap?(..2).should == true
    end

    it "returns false when beginningless and endless Ranges do not overlap" do
      (0..2).overlap?(..-1).should == false
      (0..2).overlap?(3..).should == false

      (..-1).overlap?(0..2).should == false
      (3..).overlap?(0..2).should == false
    end

    it "returns false when Ranges are not compatible" do
      (0..2).overlap?('a'..'d').should == false
    end

    it "return false when self is empty" do
      (2..0).overlap?(1..3).should == false
      (2...2).overlap?(1..3).should == false
      (1...1).overlap?(1...1).should == false
      (2..0).overlap?(2..0).should == false

      ('c'..'a').overlap?('b'..'d').should == false
      ('a'...'a').overlap?('b'..'d').should == false
      ('b'...'b').overlap?('b'...'b').should == false
      ('c'...'a').overlap?('c'...'a').should == false
    end

    it "return false when other Range is empty" do
      (1..3).overlap?(2..0).should == false
      (1..3).overlap?(2...2).should == false

      ('b'..'d').overlap?('c'..'a').should == false
      ('b'..'d').overlap?('c'...'c').should == false
    end

    it "takes into account exclusive end" do
      (0...2).overlap?(2..4).should == false
      (2..4).overlap?(0...2).should == false

      ('a'...'c').overlap?('c'..'e').should == false
      ('c'..'e').overlap?('a'...'c').should == false
    end
  end
end