Move implementation of before? and after? to DateAndTime::Calculations

This prevents duplication of code.

Prevent duplication of tests by moving them to `DateAndTimeBehavior`.

Related to #32185.
This commit is contained in:
bogdanvlviv 2018-03-31 18:16:32 +03:00
parent b1a0ab179f
commit 4e68b159d5
No known key found for this signature in database
GPG Key ID: E4ACD76A6DB6DFDD
8 changed files with 22 additions and 42 deletions

@ -142,6 +142,4 @@ def compare_with_coercion(other)
end
alias_method :compare_without_coercion, :<=>
alias_method :<=>, :compare_with_coercion
alias_method :before?, :<
alias_method :after?, :>
end

@ -60,6 +60,16 @@ def on_weekday?
!WEEKEND_DAYS.include?(wday)
end
# Returns true if the date/time before <tt>date_or_time</tt>.
def before?(date_or_time)
self < date_or_time
end
# Returns true if the date/time after <tt>date_or_time</tt>.
def after?(date_or_time)
self > date_or_time
end
# Returns a new date/time the specified number of days ago.
def days_ago(days)
advance(days: -days)

@ -208,6 +208,4 @@ def <=>(other)
super
end
end
alias_method :before?, :<
alias_method :after?, :>
end

@ -302,8 +302,6 @@ def compare_with_coercion(other)
end
alias_method :compare_without_coercion, :<=>
alias_method :<=>, :compare_with_coercion
alias_method :before?, :<
alias_method :after?, :>
# Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances
# can be eql? to an equivalent Time

@ -385,6 +385,18 @@ def test_on_weekday_on_monday
assert_predicate date_time_init(2015, 1, 5, 15, 15, 10), :on_weekday?
end
def test_before
assert_equal false, date_time_init(2017, 3, 6, 12, 0, 0).before?(date_time_init(2017, 3, 5, 12, 0, 0))
assert_equal false, date_time_init(2017, 3, 6, 12, 0, 0).before?(date_time_init(2017, 3, 6, 12, 0, 0))
assert_equal true, date_time_init(2017, 3, 6, 12, 0, 0).before?(date_time_init(2017, 3, 7, 12, 0, 0))
end
def test_after
assert_equal true, date_time_init(2017, 3, 6, 12, 0, 0).after?(date_time_init(2017, 3, 5, 12, 0, 0))
assert_equal false, date_time_init(2017, 3, 6, 12, 0, 0).after?(date_time_init(2017, 3, 6, 12, 0, 0))
assert_equal false, date_time_init(2017, 3, 6, 12, 0, 0).after?(date_time_init(2017, 3, 7, 12, 0, 0))
end
def with_bw_default(bw = :monday)
old_bw = Date.beginning_of_week
Date.beginning_of_week = bw

@ -360,18 +360,6 @@ def test_future
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

@ -285,18 +285,6 @@ def test_future_without_offset
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

@ -736,18 +736,6 @@ def test_future_with_time_current_as_time_with_zone
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