diff options
author | bUg <aleks.grebennik@gmail.com> | 2012-08-14 20:28:26 +0200 |
---|---|---|
committer | bUg <aleks.grebennik@gmail.com> | 2013-01-04 02:41:09 +0100 |
commit | 38f28dca3aa16efd6cc3af6453f2e6b9e9655ec1 (patch) | |
tree | 3476dcbf018c506499a04ac3523c369dbbb44c96 /activesupport/test/core_ext/numeric_ext_test.rb | |
parent | 4d240ec20a70122b7e69c790a37d2be14e1ff038 (diff) | |
download | rails-38f28dca3aa16efd6cc3af6453f2e6b9e9655ec1.tar.gz rails-38f28dca3aa16efd6cc3af6453f2e6b9e9655ec1.tar.bz2 rails-38f28dca3aa16efd6cc3af6453f2e6b9e9655ec1.zip |
Added ability to compare date/time with infinity
Date, DateTime, Time and TimeWithZone can now be compared to infinity,
so it's now possible to create ranges with one infinite bound and
date/time object as another bound.
Ex.: @range = Range.new(Date.today, Float::INFINITY)
Also it's possible to check inclusion of date/time in range with
conversion.
Ex.: @range.include?(Time.now + 1.year) # => true
@range.include?(DateTime.now + 1.year) # => true
Ability to create date/time ranges with infinite bound is required
for handling postgresql range types.
Diffstat (limited to 'activesupport/test/core_ext/numeric_ext_test.rb')
-rw-r--r-- | activesupport/test/core_ext/numeric_ext_test.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index 435f4aa5a1..5839aa40e5 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -447,3 +447,67 @@ class NumericExtFormattingTest < ActiveSupport::TestCase assert_equal '1 Million', BigDecimal("1000010").to_s(:human) end end + +class NumericExtBehaviorTest < ActiveSupport::TestCase + def setup + @inf = BigDecimal.new('Infinity') + end + + def test_compare_infinity_with_date + assert_nothing_raised do + assert_equal(-1, -Float::INFINITY <=> Date.today) + assert_equal(1, Float::INFINITY <=> Date.today) + assert_equal(-1, -@inf <=> Date.today) + assert_equal(1, @inf <=> Date.today) + end + end + + def test_compare_infinty_with_infinty + assert_nothing_raised do + assert_equal(-1, -Float::INFINITY <=> Float::INFINITY) + assert_equal(1, Float::INFINITY <=> -Float::INFINITY) + assert_equal(0, Float::INFINITY <=> Float::INFINITY) + assert_equal(0, -Float::INFINITY <=> -Float::INFINITY) + + assert_equal(-1, -Float::INFINITY <=> BigDecimal::INFINITY) + assert_equal(1, Float::INFINITY <=> -BigDecimal::INFINITY) + assert_equal(0, Float::INFINITY <=> BigDecimal::INFINITY) + assert_equal(0, -Float::INFINITY <=> -BigDecimal::INFINITY) + + assert_equal(-1, -BigDecimal::INFINITY <=> Float::INFINITY) + assert_equal(1, BigDecimal::INFINITY <=> -Float::INFINITY) + assert_equal(0, BigDecimal::INFINITY <=> Float::INFINITY) + assert_equal(0, -BigDecimal::INFINITY <=> -Float::INFINITY) + end + end + + def test_compare_infinity_with_time + assert_nothing_raised do + assert_equal(-1, -Float::INFINITY <=> Time.now) + assert_equal(1, Float::INFINITY <=> Time.now) + assert_equal(-1, -@inf <=> Time.now) + assert_equal(1, @inf <=> Time.now) + end + end + + def test_compare_infinity_with_datetime + assert_nothing_raised do + assert_equal(-1, -Float::INFINITY <=> DateTime.now) + assert_equal(1, Float::INFINITY <=> DateTime.now) + assert_equal(-1, -@inf <=> DateTime.now) + assert_equal(1, @inf <=> DateTime.now) + end + end + + def test_compare_infinity_with_twz + time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + twz = ActiveSupport::TimeWithZone.new(Time.now, time_zone) + + assert_nothing_raised do + assert_equal(-1, -Float::INFINITY <=> twz) + assert_equal(1, Float::INFINITY <=> twz) + assert_equal(-1, -@inf <=> twz) + assert_equal(1, @inf <=> twz) + end + end +end |