summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-10-15 22:32:10 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-10-16 01:42:34 +0900
commit929d5fd3b99c1413f737ff16cf0680698036e60f (patch)
tree396461f5fe1b6492d2246dd36b52aa83d9abe080 /spec
parent375cf129189f32f7be76ac525035bcde691a63e7 (diff)
Comparable#clamp with a range [Feature #14784]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2556
Diffstat (limited to 'spec')
-rw-r--r--spec/ruby/core/comparable/clamp_spec.rb48
1 files changed, 44 insertions, 4 deletions
diff --git a/spec/ruby/core/comparable/clamp_spec.rb b/spec/ruby/core/comparable/clamp_spec.rb
index 6d216220d4..393496fc76 100644
--- a/spec/ruby/core/comparable/clamp_spec.rb
+++ b/spec/ruby/core/comparable/clamp_spec.rb
@@ -2,10 +2,12 @@ require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe 'Comparable#clamp' do
- it 'raises an Argument error unless given 2 parameters' do
- c = ComparableSpecs::Weird.new(0)
- -> { c.clamp(c) }.should raise_error(ArgumentError)
- -> { c.clamp(c, c, c) }.should raise_error(ArgumentError)
+ ruby_version_is ""..."2.7" do
+ it 'raises an Argument error unless given 2 parameters' do
+ c = ComparableSpecs::Weird.new(0)
+ -> { c.clamp(c) }.should raise_error(ArgumentError)
+ -> { c.clamp(c, c, c) }.should raise_error(ArgumentError)
+ end
end
it 'raises an Argument error unless the 2 parameters are correctly ordered' do
@@ -45,4 +47,42 @@ describe 'Comparable#clamp' do
c.clamp(one, two).should equal(two)
end
+
+ ruby_version_is "2.7" do
+ it 'returns self if within the given range parameters' do
+ one = ComparableSpecs::WithOnlyCompareDefined.new(1)
+ two = ComparableSpecs::WithOnlyCompareDefined.new(2)
+ 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)
+ end
+
+ it 'returns the minimum value of the range parameters if smaller 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)
+ end
+
+ it 'returns the maximum value of the range parameters if greater than it' do
+ one = ComparableSpecs::WithOnlyCompareDefined.new(1)
+ two = ComparableSpecs::WithOnlyCompareDefined.new(2)
+ c = ComparableSpecs::Weird.new(3)
+
+ c.clamp(one..two).should equal(two)
+ end
+
+ it 'raises an Argument error if the range parameter is exclusive' do
+ one = ComparableSpecs::WithOnlyCompareDefined.new(1)
+ two = ComparableSpecs::WithOnlyCompareDefined.new(2)
+ c = ComparableSpecs::Weird.new(3)
+
+ -> { c.clamp(one...two) }.should raise_error(ArgumentError)
+ end
+ end
end