|
| 1 | +require_relative '../../spec_helper' |
| 2 | + |
| 3 | +describe 'Range#minmax' do |
| 4 | + before(:each) do |
| 5 | + @x = mock('x') |
| 6 | + @y = mock('y') |
| 7 | + |
| 8 | + @x.should_receive(:<=>).with(@y).any_number_of_times.and_return(-1) |
| 9 | + @x.should_receive(:<=>).with(@x).any_number_of_times.and_return(0) |
| 10 | + @y.should_receive(:<=>).with(@x).any_number_of_times.and_return(1) |
| 11 | + @y.should_receive(:<=>).with(@y).any_number_of_times.and_return(0) |
| 12 | + end |
| 13 | + |
| 14 | + describe 'by enumerating the range' do |
| 15 | + before(:each) do |
| 16 | + @x.should_receive(:succ).once.and_return(@y) |
| 17 | + end |
| 18 | + |
| 19 | + ruby_version_is ''...'2.7' do |
| 20 | + it 'should return the minimum and maximum elements' do |
| 21 | + (@x..@y).minmax.should == [@x, @y] |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + it 'should not return the end of the range as maximum if exclusive range' do |
| 26 | + (@x...@y).minmax.should == [@x, @x] |
| 27 | + end |
| 28 | + |
| 29 | + it 'should respect custom comparison block' do |
| 30 | + (@x..@y).minmax { |x, y| - (x <=> y) }.should == [@y, @x] |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + describe 'without enumerating the range' do |
| 35 | + before(:each) do |
| 36 | + # Prevent enumeration, but allow members to respond_to? :succ |
| 37 | + @x.should_not_receive(:succ) |
| 38 | + @y.should_not_receive(:succ) |
| 39 | + end |
| 40 | + |
| 41 | + ruby_version_is '2.7' do |
| 42 | + it 'should return the minimum and maximum elements' do |
| 43 | + (@x..@y).minmax.should == [@x, @y] |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + it 'should return nil pair if empty inclusive range' do |
| 48 | + (@y..@x).minmax.should == [nil, nil] |
| 49 | + end |
| 50 | + |
| 51 | + it 'should return nil pair if empty exclusive range' do |
| 52 | + (@x...@x).minmax.should == [nil, nil] |
| 53 | + end |
| 54 | + end |
| 55 | +end |
0 commit comments