summaryrefslogtreecommitdiff
path: root/spec/ruby/core/range/shared/cover.rb
blob: 33d416fef52dc920995d9d7edf89b8a828f6e630 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- encoding: binary -*-
require_relative '../../../spec_helper'
require_relative '../fixtures/classes'

describe :range_cover, shared: true do
  it "uses the range element's <=> to make the comparison" do
    a = mock('a')
    a.should_receive(:<=>).twice.and_return(-1,-1)
    (a..'z').send(@method, 'b').should be_true
  end

  it "uses a continuous inclusion test" do
    ('a'..'f').send(@method, 'aa').should be_true
    ('a'..'f').send(@method, 'babe').should be_true
    ('a'..'f').send(@method, 'baby').should be_true
    ('a'..'f').send(@method, 'ga').should be_false
    (-10..-2).send(@method, -2.5).should be_true
  end

  describe "on string elements" do
    it "returns true if other is matched by element.succ" do
      ('a'..'c').send(@method, 'b').should be_true
      ('a'...'c').send(@method, 'b').should be_true
    end

    it "returns true if other is not matched by element.succ" do
      ('a'..'c').send(@method, 'bc').should be_true
      ('a'...'c').send(@method, 'bc').should be_true
    end
  end

  describe "with weird succ" do
    describe "when included end value" do
      before :each do
        @range = RangeSpecs::TenfoldSucc.new(1)..RangeSpecs::TenfoldSucc.new(99)
      end

      it "returns false if other is less than first element" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should be_false
      end

      it "returns true if other is equal as first element" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
      end

      it "returns true if other is matched by element.succ" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
      end

      it "returns true if other is not matched by element.succ" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_true
      end

      it "returns true if other is equal as last element but not matched by element.succ" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(99)).should be_true
      end

      it "returns false if other is greater than last element but matched by element.succ" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should be_false
      end
    end

    describe "when excluded end value" do
      before :each do
        @range = RangeSpecs::TenfoldSucc.new(1)...RangeSpecs::TenfoldSucc.new(99)
      end

      it "returns false if other is less than first element" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(0)).should be_false
      end

      it "returns true if other is equal as first element" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(1)).should be_true
      end

      it "returns true if other is matched by element.succ" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(10)).should be_true
      end

      it "returns true if other is not matched by element.succ" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(2)).should be_true
      end

      it "returns false if other is equal as last element but not matched by element.succ" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(99)).should be_false
      end

      it "returns false if other is greater than last element but matched by element.succ" do
        @range.send(@method, RangeSpecs::TenfoldSucc.new(100)).should be_false
      end
    end
  end

  ruby_version_is "2.6" do
    context "range argument" do
      it "accepts range argument" do
        (0..10).send(@method, (3..7)).should be_true
        (0..10).send(@method, (3..15)).should be_false
        (0..10).send(@method, (-2..7)).should be_false

        (1.1..7.9).send(@method, (2.5..6.5)).should be_true
        (1.1..7.9).send(@method, (2.5..8.5)).should be_false
        (1.1..7.9).send(@method, (0.5..6.5)).should be_false

        ('c'..'i').send(@method, ('d'..'f')).should be_true
        ('c'..'i').send(@method, ('d'..'z')).should be_false
        ('c'..'i').send(@method, ('a'..'f')).should be_false

        range_10_100 = RangeSpecs::TenfoldSucc.new(10)..RangeSpecs::TenfoldSucc.new(100)
        range_20_90 = RangeSpecs::TenfoldSucc.new(20)..RangeSpecs::TenfoldSucc.new(90)
        range_20_110 = RangeSpecs::TenfoldSucc.new(20)..RangeSpecs::TenfoldSucc.new(110)
        range_0_90 = RangeSpecs::TenfoldSucc.new(0)..RangeSpecs::TenfoldSucc.new(90)

        range_10_100.send(@method, range_20_90).should be_true
        range_10_100.send(@method, range_20_110).should be_false
        range_10_100.send(@method, range_0_90).should be_false
      end

      it "supports boundaries of different comparable types" do
        (0..10).send(@method, (3.1..7.9)).should be_true
        (0..10).send(@method, (3.1..15.9)).should be_false
        (0..10).send(@method, (-2.1..7.9)).should be_false
      end

      it "returns false if types are not comparable" do
        (0..10).send(@method, ('a'..'z')).should be_false
        (0..10).send(@method, (RangeSpecs::TenfoldSucc.new(0)..RangeSpecs::TenfoldSucc.new(100))).should be_false
      end

      it "honors exclusion of right boundary (:exclude_end option)" do
        # Integer
        (0..10).send(@method, (0..10)).should be_true
        (0...10).send(@method, (0...10)).should be_true

        (0..10).send(@method, (0...10)).should be_true
        (0...10).send(@method, (0..10)).should be_false

        (0...11).send(@method, (0..10)).should be_true
        (0..10).send(@method, (0...11)).should be_true

        # Float
        (0..10.1).send(@method, (0..10.1)).should be_true
        (0...10.1).send(@method, (0...10.1)).should be_true

        (0..10.1).send(@method, (0...10.1)).should be_true
        (0...10.1).send(@method, (0..10.1)).should be_false

        (0...11.1).send(@method, (0..10.1)).should be_true
        (0..10.1).send(@method, (0...11.1)).should be_false
      end
    end
  end
end