aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
authorNick Holden <nick.r.holden@gmail.com>2018-03-06 20:42:49 -0800
committerNick Holden <nick.r.holden@gmail.com>2018-03-06 20:42:49 -0800
commit20fa0d92c36681f6cffc715e08c299ff942352f4 (patch)
tree16c284564bb661a58694bed695b41e9d312bf2cb /activesupport/test
parentb5aa0266d0a4d5f7a02eb31df730fa172d1a845a (diff)
downloadrails-20fa0d92c36681f6cffc715e08c299ff942352f4.tar.gz
rails-20fa0d92c36681f6cffc715e08c299ff942352f4.tar.bz2
rails-20fa0d92c36681f6cffc715e08c299ff942352f4.zip
Add `before?` and `after?` methods to date and time classes
Equality comparisons between dates and times can take some extra time to comprehend. I tend to think of a date or time as "before" or "after" another date or time, but I naturally read `<` and `>` as "less than" and "greater than." This change seeks to make date/time comparisons more human readable.
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb12
-rw-r--r--activesupport/test/core_ext/date_time_ext_test.rb12
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb12
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb14
4 files changed, 50 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index b8652884ce..8d3d575e0f 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -360,6 +360,18 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
end
end
+ def test_before
+ assert_equal false, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 5))
+ assert_equal false, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 6))
+ assert_equal true, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 7))
+ end
+
+ def test_after
+ assert_equal true, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 5))
+ assert_equal false, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 6))
+ assert_equal false, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 7))
+ end
+
def test_current_returns_date_today_when_zone_not_set
with_env_tz "US/Central" do
Time.stub(:now, Time.local(1999, 12, 31, 23)) do
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb
index 894fb80cba..a6124197ae 100644
--- a/activesupport/test/core_ext/date_time_ext_test.rb
+++ b/activesupport/test/core_ext/date_time_ext_test.rb
@@ -285,6 +285,18 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
end
end
+ def test_before
+ assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 11, 59, 59))
+ assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 12, 0, 0))
+ assert_equal true, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 12, 00, 1))
+ end
+
+ def test_after
+ assert_equal true, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 11, 59, 59))
+ assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 12, 0, 0))
+ assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 12, 00, 1))
+ end
+
def test_current_returns_date_today_when_zone_is_not_set
with_env_tz "US/Eastern" do
Time.stub(:now, Time.local(1999, 12, 31, 23, 59, 59)) do
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index e1cb22fda8..0d16e51087 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -736,6 +736,18 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
end
end
+ def test_before
+ assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 11, 59, 59))
+ assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 12, 0, 0))
+ assert_equal true, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 12, 00, 1))
+ end
+
+ def test_after
+ assert_equal true, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 11, 59, 59))
+ assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 12, 0, 0))
+ assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 12, 00, 1))
+ end
+
def test_acts_like_time
assert_predicate Time.new, :acts_like_time?
end
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index 2ea5f0921c..e650209268 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -289,6 +289,20 @@ class TimeWithZoneTest < ActiveSupport::TestCase
end
end
+ def test_before
+ twz = ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 0, 0), @time_zone)
+ assert_equal false, twz.before?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 11, 59, 59), @time_zone))
+ assert_equal false, twz.before?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 0, 0), @time_zone))
+ assert_equal true, twz.before?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 00, 1), @time_zone))
+ end
+
+ def test_after
+ twz = ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 0, 0), @time_zone)
+ assert_equal true, twz.after?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 11, 59, 59), @time_zone))
+ assert_equal false, twz.after?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 0, 0), @time_zone))
+ assert_equal false, twz.after?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 00, 1), @time_zone))
+ end
+
def test_eql?
assert_equal true, @twz.eql?(@twz.dup)
assert_equal true, @twz.eql?(Time.utc(2000))