diff options
author | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-10-15 22:32:10 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2019-10-16 01:42:34 +0900 |
commit | 929d5fd3b99c1413f737ff16cf0680698036e60f (patch) | |
tree | 396461f5fe1b6492d2246dd36b52aa83d9abe080 /test | |
parent | 375cf129189f32f7be76ac525035bcde691a63e7 (diff) | |
download | ruby-929d5fd3b99c1413f737ff16cf0680698036e60f.tar.gz |
Comparable#clamp with a range [Feature #14784]
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_comparable.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/test/ruby/test_comparable.rb b/test/ruby/test_comparable.rb index 4f1d38a7e6..c363a03a64 100644 --- a/test/ruby/test_comparable.rb +++ b/test/ruby/test_comparable.rb @@ -90,6 +90,29 @@ class TestComparable < Test::Unit::TestCase } end + def test_clamp_with_range + cmp->(x) do 0 <=> x end + assert_equal(1, @o.clamp(1..2)) + assert_equal(-1, @o.clamp(-2..-1)) + assert_equal(@o, @o.clamp(-1..3)) + + assert_equal(1, @o.clamp(1..1)) + assert_equal(@o, @o.clamp(0..0)) + + assert_raise_with_message(ArgumentError, 'cannot clamp with an exclusive range') { + @o.clamp(1...2) + } + assert_raise_with_message(ArgumentError, 'cannot clamp with an exclusive range') { + @o.clamp(1...) + } + assert_raise_with_message(ArgumentError, 'cannot clamp with an exclusive range') { + @o.clamp(...2) + } + assert_raise_with_message(ArgumentError, 'min argument must be smaller than max argument') { + @o.clamp(2..1) + } + end + def test_err assert_raise(ArgumentError) { 1.0 < nil } assert_raise(ArgumentError) { 1.0 < Object.new } |