diff options
Diffstat (limited to 'spec/ruby/core/comparable')
| -rw-r--r-- | spec/ruby/core/comparable/clamp_spec.rb | 179 | ||||
| -rw-r--r-- | spec/ruby/core/comparable/equal_value_spec.rb | 10 | ||||
| -rw-r--r-- | spec/ruby/core/comparable/fixtures/classes.rb | 1 | ||||
| -rw-r--r-- | spec/ruby/core/comparable/gt_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/comparable/gte_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/core/comparable/lt_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/core/comparable/lte_spec.rb | 2 |
7 files changed, 174 insertions, 28 deletions
diff --git a/spec/ruby/core/comparable/clamp_spec.rb b/spec/ruby/core/comparable/clamp_spec.rb index 796d4a18c1..eb1dc1ff98 100644 --- a/spec/ruby/core/comparable/clamp_spec.rb +++ b/spec/ruby/core/comparable/clamp_spec.rb @@ -7,9 +7,9 @@ describe 'Comparable#clamp' do two = ComparableSpecs::WithOnlyCompareDefined.new(2) c = ComparableSpecs::Weird.new(3) - -> { c.clamp(two, one) }.should raise_error(ArgumentError) + -> { c.clamp(two, one) }.should.raise(ArgumentError) one.should_receive(:<=>).any_number_of_times.and_return(nil) - -> { c.clamp(one, two) }.should raise_error(ArgumentError) + -> { c.clamp(one, two) }.should.raise(ArgumentError) end it 'returns self if within the given parameters' do @@ -18,18 +18,18 @@ describe 'Comparable#clamp' do three = ComparableSpecs::WithOnlyCompareDefined.new(3) c = ComparableSpecs::Weird.new(2) - c.clamp(one, two).should equal(c) - c.clamp(two, two).should equal(c) - c.clamp(one, three).should equal(c) - c.clamp(two, three).should equal(c) + c.clamp(one, two).should.equal?(c) + c.clamp(two, two).should.equal?(c) + c.clamp(one, three).should.equal?(c) + c.clamp(two, three).should.equal?(c) end - it 'returns the min parameter if smaller than it' do + it 'returns the min parameter if less than it' do one = ComparableSpecs::WithOnlyCompareDefined.new(1) two = ComparableSpecs::WithOnlyCompareDefined.new(2) c = ComparableSpecs::Weird.new(0) - c.clamp(one, two).should equal(one) + c.clamp(one, two).should.equal?(one) end it 'returns the max parameter if greater than it' do @@ -37,7 +37,40 @@ describe 'Comparable#clamp' do two = ComparableSpecs::WithOnlyCompareDefined.new(2) c = ComparableSpecs::Weird.new(3) - c.clamp(one, two).should equal(two) + c.clamp(one, two).should.equal?(two) + end + + context 'max is nil' do + it 'returns min if less than it' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + c = ComparableSpecs::Weird.new(0) + c.clamp(one, nil).should.equal?(one) + end + + it 'always returns self if greater than min' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + c = ComparableSpecs::Weird.new(2) + c.clamp(one, nil).should.equal?(c) + end + end + + context 'min is nil' do + it 'returns max if greater than it' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + c = ComparableSpecs::Weird.new(2) + c.clamp(nil, one).should.equal?(one) + end + + it 'always returns self if less than max' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + c = ComparableSpecs::Weird.new(0) + c.clamp(nil, one).should.equal?(c) + end + end + + it 'always returns self when min is nil and max is nil' do + c = ComparableSpecs::Weird.new(1) + c.clamp(nil, nil).should.equal?(c) end it 'returns self if within the given range parameters' do @@ -46,18 +79,18 @@ describe 'Comparable#clamp' do three = ComparableSpecs::WithOnlyCompareDefined.new(3) c = ComparableSpecs::Weird.new(2) - c.clamp(one..two).should equal(c) - c.clamp(two..two).should equal(c) - c.clamp(one..three).should equal(c) - c.clamp(two..three).should equal(c) + c.clamp(one..two).should.equal?(c) + c.clamp(two..two).should.equal?(c) + c.clamp(one..three).should.equal?(c) + c.clamp(two..three).should.equal?(c) end - it 'returns the minimum value of the range parameters if smaller than it' do + it 'returns the minimum value of the range parameters if less than it' do one = ComparableSpecs::WithOnlyCompareDefined.new(1) two = ComparableSpecs::WithOnlyCompareDefined.new(2) c = ComparableSpecs::Weird.new(0) - c.clamp(one..two).should equal(one) + c.clamp(one..two).should.equal?(one) end it 'returns the maximum value of the range parameters if greater than it' do @@ -65,7 +98,7 @@ describe 'Comparable#clamp' do two = ComparableSpecs::WithOnlyCompareDefined.new(2) c = ComparableSpecs::Weird.new(3) - c.clamp(one..two).should equal(two) + c.clamp(one..two).should.equal?(two) end it 'raises an Argument error if the range parameter is exclusive' do @@ -73,6 +106,118 @@ describe 'Comparable#clamp' do two = ComparableSpecs::WithOnlyCompareDefined.new(2) c = ComparableSpecs::Weird.new(3) - -> { c.clamp(one...two) }.should raise_error(ArgumentError) + -> { c.clamp(one...two) }.should.raise(ArgumentError) + end + + context 'with nil as the max argument' do + it 'returns min argument if less than it' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + zero = ComparableSpecs::WithOnlyCompareDefined.new(0) + c = ComparableSpecs::Weird.new(0) + + c.clamp(one, nil).should.equal?(one) + c.clamp(zero, nil).should.equal?(c) + end + + it 'always returns self if greater than min argument' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + two = ComparableSpecs::WithOnlyCompareDefined.new(2) + c = ComparableSpecs::Weird.new(2) + + c.clamp(one, nil).should.equal?(c) + c.clamp(two, nil).should.equal?(c) + end + end + + context 'with endless range' do + it 'returns minimum value of the range parameters if less than it' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + zero = ComparableSpecs::WithOnlyCompareDefined.new(0) + c = ComparableSpecs::Weird.new(0) + + c.clamp(one..).should.equal?(one) + c.clamp(zero..).should.equal?(c) + end + + it 'always returns self if greater than minimum value of the range parameters' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + two = ComparableSpecs::WithOnlyCompareDefined.new(2) + c = ComparableSpecs::Weird.new(2) + + c.clamp(one..).should.equal?(c) + c.clamp(two..).should.equal?(c) + end + + it 'works with exclusive range' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + c = ComparableSpecs::Weird.new(2) + + c.clamp(one...).should.equal?(c) + end + end + + context 'with nil as the min argument' do + it 'returns max argument if greater than it' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + c = ComparableSpecs::Weird.new(2) + + c.clamp(nil, one).should.equal?(one) + end + + it 'always returns self if less than max argument' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + zero = ComparableSpecs::WithOnlyCompareDefined.new(0) + c = ComparableSpecs::Weird.new(0) + + c.clamp(nil, one).should.equal?(c) + c.clamp(nil, zero).should.equal?(c) + end + end + + context 'with beginless range' do + it 'returns maximum value of the range parameters if greater than it' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + c = ComparableSpecs::Weird.new(2) + + c.clamp(..one).should.equal?(one) + end + + it 'always returns self if less than maximum value of the range parameters' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + zero = ComparableSpecs::WithOnlyCompareDefined.new(0) + c = ComparableSpecs::Weird.new(0) + + c.clamp(..one).should.equal?(c) + c.clamp(..zero).should.equal?(c) + end + + it 'raises an Argument error if the range parameter is exclusive' do + one = ComparableSpecs::WithOnlyCompareDefined.new(1) + c = ComparableSpecs::Weird.new(0) + + -> { c.clamp(...one) }.should.raise(ArgumentError) + end + end + + context 'with nil as the min and the max argument' do + it 'always returns self' do + c = ComparableSpecs::Weird.new(1) + + c.clamp(nil, nil).should.equal?(c) + end + end + + context 'with beginless-and-endless range' do + it 'always returns self' do + c = ComparableSpecs::Weird.new(1) + + c.clamp(nil..nil).should.equal?(c) + end + + it 'works with exclusive range' do + c = ComparableSpecs::Weird.new(2) + + c.clamp(nil...nil).should.equal?(c) + end end end diff --git a/spec/ruby/core/comparable/equal_value_spec.rb b/spec/ruby/core/comparable/equal_value_spec.rb index ddcc03cb41..3af40d1c7e 100644 --- a/spec/ruby/core/comparable/equal_value_spec.rb +++ b/spec/ruby/core/comparable/equal_value_spec.rb @@ -39,7 +39,7 @@ describe "Comparable#==" do end it "returns false" do - (a == b).should be_false + (a == b).should == false end end @@ -49,7 +49,7 @@ describe "Comparable#==" do end it "raises an ArgumentError" do - -> { (a == b) }.should raise_error(ArgumentError) + -> { (a == b) }.should.raise(ArgumentError) end end @@ -60,7 +60,7 @@ describe "Comparable#==" do end it "lets it go through" do - -> { (a == b) }.should raise_error(StandardError) + -> { (a == b) }.should.raise(StandardError) end end @@ -71,13 +71,13 @@ describe "Comparable#==" do end it "lets it go through" do - -> { (a == b) }.should raise_error(TypeError) + -> { (a == b) }.should.raise(TypeError) end end it "lets it go through if it is not a StandardError" do a.should_receive(:<=>).once.and_raise(Exception) - -> { (a == b) }.should raise_error(Exception) + -> { (a == b) }.should.raise(Exception) end end diff --git a/spec/ruby/core/comparable/fixtures/classes.rb b/spec/ruby/core/comparable/fixtures/classes.rb index 4239a47d2f..2bdabbf014 100644 --- a/spec/ruby/core/comparable/fixtures/classes.rb +++ b/spec/ruby/core/comparable/fixtures/classes.rb @@ -7,6 +7,7 @@ module ComparableSpecs end def <=>(other) + return nil if other.nil? self.value <=> other.value end end diff --git a/spec/ruby/core/comparable/gt_spec.rb b/spec/ruby/core/comparable/gt_spec.rb index 150e653dc7..cebb5464ad 100644 --- a/spec/ruby/core/comparable/gt_spec.rb +++ b/spec/ruby/core/comparable/gt_spec.rb @@ -38,6 +38,6 @@ describe "Comparable#>" do b = ComparableSpecs::Weird.new(20) a.should_receive(:<=>).any_number_of_times.and_return(nil) - -> { (a > b) }.should raise_error(ArgumentError) + -> { (a > b) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/comparable/gte_spec.rb b/spec/ruby/core/comparable/gte_spec.rb index 328f58c66c..16da81b3ea 100644 --- a/spec/ruby/core/comparable/gte_spec.rb +++ b/spec/ruby/core/comparable/gte_spec.rb @@ -42,6 +42,6 @@ describe "Comparable#>=" do b = ComparableSpecs::Weird.new(20) a.should_receive(:<=>).any_number_of_times.and_return(nil) - -> { (a >= b) }.should raise_error(ArgumentError) + -> { (a >= b) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/comparable/lt_spec.rb b/spec/ruby/core/comparable/lt_spec.rb index bca95f8d25..175646d0d7 100644 --- a/spec/ruby/core/comparable/lt_spec.rb +++ b/spec/ruby/core/comparable/lt_spec.rb @@ -38,12 +38,12 @@ describe "Comparable#<" do b = ComparableSpecs::Weird.new(20) a.should_receive(:<=>).any_number_of_times.and_return(nil) - -> { (a < b) }.should raise_error(ArgumentError) + -> { (a < b) }.should.raise(ArgumentError) end it "raises an argument error with a message containing the value" do - -> { ("foo" < 7) }.should raise_error(ArgumentError) { |e| - e.message.should == "comparison of String with 7 failed" + -> { ("foo" < 7) }.should.raise(ArgumentError) { |e| + e.message.should.include? "String with 7 failed" } end end diff --git a/spec/ruby/core/comparable/lte_spec.rb b/spec/ruby/core/comparable/lte_spec.rb index b5cb9cc4e7..8cbbd5ebb4 100644 --- a/spec/ruby/core/comparable/lte_spec.rb +++ b/spec/ruby/core/comparable/lte_spec.rb @@ -41,6 +41,6 @@ describe "Comparable#<=" do b = ComparableSpecs::Weird.new(20) a.should_receive(:<=>).any_number_of_times.and_return(nil) - -> { (a <= b) }.should raise_error(ArgumentError) + -> { (a <= b) }.should.raise(ArgumentError) end end |
