aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/range_ext_test.rb
diff options
context:
space:
mode:
authorbUg <aleks.grebennik@gmail.com>2012-08-14 20:28:26 +0200
committerbUg <aleks.grebennik@gmail.com>2013-01-04 02:41:09 +0100
commit38f28dca3aa16efd6cc3af6453f2e6b9e9655ec1 (patch)
tree3476dcbf018c506499a04ac3523c369dbbb44c96 /activesupport/test/core_ext/range_ext_test.rb
parent4d240ec20a70122b7e69c790a37d2be14e1ff038 (diff)
downloadrails-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/range_ext_test.rb')
-rw-r--r--activesupport/test/core_ext/range_ext_test.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb
index f0cdc0bfd4..0051c48984 100644
--- a/activesupport/test/core_ext/range_ext_test.rb
+++ b/activesupport/test/core_ext/range_ext_test.rb
@@ -1,6 +1,7 @@
require 'abstract_unit'
require 'active_support/time'
require 'active_support/core_ext/range'
+require 'active_support/core_ext/numeric'
class RangeTest < ActiveSupport::TestCase
def test_to_s_from_dates
@@ -85,4 +86,28 @@ class RangeTest < ActiveSupport::TestCase
time_range_2 = Time.utc(2005, 12, 10, 17, 31)..Time.utc(2005, 12, 10, 18, 00)
assert !time_range_1.overlaps?(time_range_2)
end
+
+ def test_infinite_bounds
+ time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
+
+ time = Time.now
+ date = Date.today
+ datetime = DateTime.now
+ twz = ActiveSupport::TimeWithZone.new(time, time_zone)
+
+ infinity1 = Float::INFINITY
+ infinity2 = BigDecimal.new('Infinity')
+
+ [infinity1, infinity2].each do |infinity|
+ [time, date, datetime, twz].each do |bound|
+ [time, date, datetime, twz].each do |value|
+ assert Range.new(bound, infinity).include?(value + 10.years)
+ assert Range.new(-infinity, bound).include?(value - 10.years)
+
+ assert !Range.new(bound, infinity).include?(value - 10.years)
+ assert !Range.new(-infinity, bound).include?(value + 10.years)
+ end
+ end
+ end
+ end
end