|
1 | 1 | require_relative '../../spec_helper' |
2 | 2 |
|
| 3 | +# These specs use Range.new instead of the literal notation so they parse fine on Ruby < 2.6 |
3 | 4 | describe 'Range#minmax' do |
4 | 5 | before(:each) do |
5 | 6 | @x = mock('x') |
|
13 | 14 |
|
14 | 15 | describe 'on an inclusive range' do |
15 | 16 | ruby_version_is '2.6'...'2.7' do |
16 | | - # Endless ranges introduced in 2.6 |
17 | 17 | it 'should try to iterate endlessly on an endless range' do |
18 | 18 | @x.should_receive(:succ).once.and_return(@y) |
19 | | - |
20 | | - # Endless range literal would cause SyntaxError prior to 2.6 |
21 | 19 | range = Range.new(@x, nil) |
22 | 20 |
|
23 | 21 | -> { range.minmax }.should raise_error(NoMethodError, /^undefined method `succ' for/) |
|
28 | 26 | it 'should raise RangeError on an endless range without iterating the range' do |
29 | 27 | @x.should_not_receive(:succ) |
30 | 28 |
|
31 | | - # Endless range literal would cause SyntaxError prior to 2.6 |
32 | 29 | range = Range.new(@x, nil) |
33 | 30 |
|
34 | 31 | -> { range.minmax }.should raise_error(RangeError, 'cannot get the maximum of endless range') |
|
37 | 34 |
|
38 | 35 | ruby_version_is '2.7'...'3.0' do |
39 | 36 | it 'raises ArgumentError on a beginless range' do |
40 | | - # Beginless range literal would cause SyntaxError prior to 2.7 |
41 | 37 | range = Range.new(nil, @x) |
42 | 38 |
|
43 | 39 | -> { range.minmax }.should raise_error(ArgumentError) |
|
46 | 42 |
|
47 | 43 | ruby_version_is '3.0' do |
48 | 44 | it 'should raise RangeError on a beginless range' do |
49 | | - # Beginless range literal would cause SyntaxError prior to 2.7 |
50 | 45 | range = Range.new(nil, @x) |
51 | 46 |
|
52 | 47 | -> { range.minmax }.should raise_error(RangeError, 'cannot get the minimum of beginless range') |
|
107 | 102 | # Endless ranges introduced in 2.6 |
108 | 103 | it 'should try to iterate endlessly on an endless range' do |
109 | 104 | @x.should_receive(:succ).once.and_return(@y) |
110 | | - |
111 | | - # Endless range literal would cause SyntaxError prior to 2.6 |
112 | 105 | range = Range.new(@x, nil, true) |
113 | 106 |
|
114 | 107 | -> { range.minmax }.should raise_error(NoMethodError, /^undefined method `succ' for/) |
|
118 | 111 | ruby_version_is '2.7' do |
119 | 112 | it 'should raise RangeError on an endless range' do |
120 | 113 | @x.should_not_receive(:succ) |
121 | | - |
122 | | - # Endless range literal would cause SyntaxError prior to 2.6 |
123 | 114 | range = Range.new(@x, nil, true) |
124 | 115 |
|
125 | 116 | -> { range.minmax }.should raise_error(RangeError, 'cannot get the maximum of endless range') |
126 | 117 | end |
127 | 118 |
|
128 | | - # Beginless ranges introduced in 2.7 |
129 | 119 | it 'should raise RangeError on a beginless range' do |
130 | | - # Beginless range literal would cause SyntaxError prior to 2.7 |
131 | 120 | range = Range.new(nil, @x, true) |
132 | 121 |
|
133 | 122 | -> { range.minmax }.should raise_error(RangeError, |
|
163 | 152 | it 'should return the minimum and maximum values for a numeric range without iterating the range' do |
164 | 153 | # We cannot set expectations on integers, |
165 | 154 | # so we "prevent" iteration by picking a value that would iterate until the spec times out. |
166 | | - # Since we cannot exclude a non Integer end value, we use a HUGE Integer. |
167 | | - range_end = 123_456_789_012_345_678_901_234_567_890 |
| 155 | + range_end = bignum_value |
168 | 156 |
|
169 | 157 | (1...range_end).minmax.should == [1, range_end - 1] |
170 | 158 | end |
| 159 | + |
| 160 | + it 'raises TypeError if the end value is not an integer' do |
| 161 | + range = (0...Float::INFINITY) |
| 162 | + -> { range.minmax }.should raise_error(TypeError, 'cannot exclude non Integer end value') |
| 163 | + end |
171 | 164 | end |
172 | 165 |
|
173 | 166 | it 'should return the minimum and maximum values according to the provided block by iterating the range' do |
|
0 commit comments