diff options
| author | KOBAYASHI Shuji <[email protected]> | 2019-07-17 18:58:21 +0900 |
|---|---|---|
| committer | KOBAYASHI Shuji <[email protected]> | 2019-07-17 18:58:21 +0900 |
| commit | 8cb37804c16b1506d019081bb5d366b078b0b42a (patch) | |
| tree | f0f296041e9b9e4f496e89cd9ea69c61e37e514a /mrbgems/mruby-range-ext | |
| parent | d5939879ccae40ba4623fce05b8f67dd05a465d7 (diff) | |
| download | mruby-8cb37804c16b1506d019081bb5d366b078b0b42a.tar.gz mruby-8cb37804c16b1506d019081bb5d366b078b0b42a.zip | |
Allow `mruby-range-ext` to work with `MRB_WITHOUT_FLOAT`; ref 2add8641
Diffstat (limited to 'mrbgems/mruby-range-ext')
| -rw-r--r-- | mrbgems/mruby-range-ext/mrblib/range.rb | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-range-ext/test/range.rb | 28 |
2 files changed, 20 insertions, 12 deletions
diff --git a/mrbgems/mruby-range-ext/mrblib/range.rb b/mrbgems/mruby-range-ext/mrblib/range.rb index a149a57dc..7c5787fac 100644 --- a/mrbgems/mruby-range-ext/mrblib/range.rb +++ b/mrbgems/mruby-range-ext/mrblib/range.rb @@ -32,7 +32,7 @@ class Range return super if block # fast path for numerics - if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) + if val.kind_of?(Numeric) && last.kind_of?(Numeric) raise TypeError if exclude_end? && !last.kind_of?(Fixnum) return nil if val > last return nil if val == last && exclude_end? @@ -52,7 +52,7 @@ class Range return super if block # fast path for numerics - if (val.kind_of?(Fixnum) || val.kind_of?(Float)) && (last.kind_of?(Fixnum) || last.kind_of?(Float)) + if val.kind_of?(Numeric) && last.kind_of?(Numeric) raise TypeError if exclude_end? && !last.kind_of?(Fixnum) return nil if val > last return nil if val == last && exclude_end? diff --git a/mrbgems/mruby-range-ext/test/range.rb b/mrbgems/mruby-range-ext/test/range.rb index b56d6b58e..e2c549d04 100644 --- a/mrbgems/mruby-range-ext/test/range.rb +++ b/mrbgems/mruby-range-ext/test/range.rb @@ -10,6 +10,8 @@ end assert('Range#first') do assert_equal 10, (10..20).first assert_equal [10, 11, 12], (10..20).first(3) + + skip unless Object.const_defined?(:Float) assert_equal [0, 1, 2], (0..Float::INFINITY).first(3) end @@ -23,12 +25,14 @@ end assert('Range#size') do assert_equal 42, (1..42).size assert_equal 41, (1...42).size + assert_nil ('a'..'z').size + + skip unless Object.const_defined?(:Float) assert_equal 6, (1...6.3).size assert_equal 5, (1...6.0).size assert_equal 5, (1.1...6).size assert_equal 15, (1.0..15.9).size assert_equal Float::INFINITY, (0..Float::INFINITY).size - assert_nil ('a'..'z').size end assert('Range#max') do @@ -37,12 +41,6 @@ assert('Range#max') do assert_equal 9, (1...10).max assert_equal 4294967295, (0...2**32).max - # returns the maximum value in the Float range when called with no arguments - assert_equal 908.1111, (303.20..908.1111).max - - # raises TypeError when called on an exclusive range and a non Integer value - assert_raise(TypeError) { (303.20...908.1111).max } - # returns nil when the endpoint is less than the start point assert_equal nil, (100..10).max @@ -52,6 +50,14 @@ assert('Range#max') do # returns the endpoint when the endpoint equals the start point and the range is inclusive assert_equal 5, (5..5).max + skip unless Object.const_defined?(:Float) + + # returns the maximum value in the Float range when called with no arguments + assert_equal 908.1111, (303.20..908.1111).max + + # raises TypeError when called on an exclusive range and a non Integer value + assert_raise(TypeError) { (303.20...908.1111).max } + # returns nil when the endpoint is less than the start point in a Float range assert_equal nil, (3003.20..908.1111).max end @@ -89,9 +95,6 @@ assert('Range#min') do assert_equal 1, (1..10).min assert_equal 1, (1...10).min - # returns the minimum value in the Float range when called with no arguments - assert_equal 303.20, (303.20..908.1111).min - # returns nil when the start point is greater than the endpoint assert_equal nil, (100..10).min @@ -101,6 +104,11 @@ assert('Range#min') do # returns the endpoint when the endpoint equals the start point and the range is inclusive assert_equal 5, (5..5).max + skip unless Object.const_defined?(:Float) + + # returns the minimum value in the Float range when called with no arguments + assert_equal 303.20, (303.20..908.1111).min + # returns nil when the start point is greater than the endpoint in a Float range assert_equal nil, (3003.20..908.1111).max end |
