diff options
author | Pan Thomakos <pan.thomakos@gmail.com> | 2012-07-10 17:10:23 -0700 |
---|---|---|
committer | Pan Thomakos <pan.thomakos@gmail.com> | 2012-08-04 16:13:03 -0700 |
commit | b530fd1083c0dc71606e13c30bf0b713ad416213 (patch) | |
tree | 6e708492caa7539db109a948edabda49f547671f /activesupport/test | |
parent | c5807728d52c98b76e9badd95093471044b98d07 (diff) | |
download | rails-b530fd1083c0dc71606e13c30bf0b713ad416213.tar.gz rails-b530fd1083c0dc71606e13c30bf0b713ad416213.tar.bz2 rails-b530fd1083c0dc71606e13c30bf0b713ad416213.zip |
Refactored common date and time calculations.
* Added the `DateAndTime::Calculations` module that is included in Time
and Date. It houses common calculations to reduce duplicated code.
* Simplified and cleaned-up the calculation code.
* Removed duplication in tests by adding a behavior module for shared
tests. I also added some missing tests.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/core_ext/date_and_time_behavior.rb | 186 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_ext_test.rb | 173 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 143 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 182 |
4 files changed, 215 insertions, 469 deletions
diff --git a/activesupport/test/core_ext/date_and_time_behavior.rb b/activesupport/test/core_ext/date_and_time_behavior.rb new file mode 100644 index 0000000000..014935b0c0 --- /dev/null +++ b/activesupport/test/core_ext/date_and_time_behavior.rb @@ -0,0 +1,186 @@ +require 'abstract_unit' + +module DateAndTimeBehavior + def test_yesterday + assert_equal date_time_init(2005,2,21,10,10,10), date_time_init(2005,2,22,10,10,10).yesterday + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,3,2,10,10,10).yesterday.yesterday + end + + def test_tomorrow + assert_equal date_time_init(2005,2,23,10,10,10), date_time_init(2005,2,22,10,10,10).tomorrow + assert_equal date_time_init(2005,3,2,10,10,10), date_time_init(2005,2,28,10,10,10).tomorrow.tomorrow + end + + def test_days_ago + assert_equal date_time_init(2005,6,4,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(1) + assert_equal date_time_init(2005,5,31,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(5) + end + + def test_days_since + assert_equal date_time_init(2005,6,6,10,10,10), date_time_init(2005,6,5,10,10,10).days_since(1) + assert_equal date_time_init(2005,1,1,10,10,10), date_time_init(2004,12,31,10,10,10).days_since(1) + end + + def test_weeks_ago + assert_equal date_time_init(2005,5,29,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(1) + assert_equal date_time_init(2005,5,1,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(5) + assert_equal date_time_init(2005,4,24,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(6) + assert_equal date_time_init(2005,2,27,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(14) + assert_equal date_time_init(2004,12,25,10,10,10), date_time_init(2005,1,1,10,10,10).weeks_ago(1) + end + + def test_weeks_since + assert_equal date_time_init(2005,7,14,10,10,10), date_time_init(2005,7,7,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,7,14,10,10,10), date_time_init(2005,7,7,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,7,4,10,10,10), date_time_init(2005,6,27,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,1,4,10,10,10), date_time_init(2004,12,28,10,10,10).weeks_since(1) + end + + def test_months_ago + assert_equal date_time_init(2005,5,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(1) + assert_equal date_time_init(2004,11,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(7) + assert_equal date_time_init(2004,12,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(6) + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(12) + assert_equal date_time_init(2003,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(24) + end + + def test_months_since + assert_equal date_time_init(2005,7,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(1) + assert_equal date_time_init(2006,1,5,10,10,10), date_time_init(2005,12,5,10,10,10).months_since(1) + assert_equal date_time_init(2005,12,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(6) + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,12,5,10,10,10).months_since(6) + assert_equal date_time_init(2006,1,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(7) + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(12) + assert_equal date_time_init(2007,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(24) + assert_equal date_time_init(2005,4,30,10,10,10), date_time_init(2005,3,31,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,29,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,30,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,31,10,10,10).months_since(1) + end + + def test_years_ago + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_ago(1) + assert_equal date_time_init(1998,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_ago(7) + assert_equal date_time_init(2003,2,28,10,10,10), date_time_init(2004,2,29,10,10,10).years_ago(1) # 1 year ago from leap day + end + + def test_years_since + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(1) + assert_equal date_time_init(2012,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(7) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2004,2,29,10,10,10).years_since(1) # 1 year since leap day + assert_equal date_time_init(2182,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(177) + end + + def test_beginning_of_month + assert_equal date_time_init(2005,2,1,0,0,0), date_time_init(2005,2,22,10,10,10).beginning_of_month + end + + def test_beginning_of_quarter + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,2,15,10,10,10).beginning_of_quarter + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,1,1,0,0,0).beginning_of_quarter + assert_equal date_time_init(2005,10,1,0,0,0), date_time_init(2005,12,31,10,10,10).beginning_of_quarter + assert_equal date_time_init(2005,4,1,0,0,0), date_time_init(2005,6,30,23,59,59).beginning_of_quarter + end + + def test_end_of_quarter + assert_equal date_time_init(2007,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,2,15,10,10,10).end_of_quarter + assert_equal date_time_init(2007,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,3,31,0,0,0).end_of_quarter + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,21,10,10,10).end_of_quarter + assert_equal date_time_init(2007,6,30,23,59,59,Rational(999999999, 1000)), date_time_init(2007,4,1,0,0,0).end_of_quarter + assert_equal date_time_init(2008,6,30,23,59,59,Rational(999999999, 1000)), date_time_init(2008,5,31,0,0,0).end_of_quarter + end + + def test_beginning_of_year + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,2,22,10,10,10).beginning_of_year + end + + def test_next_week + assert_equal date_time_init(2005,2,28,0,0,0), date_time_init(2005,2,22,15,15,10).next_week + assert_equal date_time_init(2005,3,4,0,0,0), date_time_init(2005,2,22,15,15,10).next_week(:friday) + assert_equal date_time_init(2006,10,30,0,0,0), date_time_init(2006,10,23,0,0,0).next_week + assert_equal date_time_init(2006,11,1,0,0,0), date_time_init(2006,10,23,0,0,0).next_week(:wednesday) + end + + def test_next_month_on_31st + assert_equal date_time_init(2005,9,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_month + end + + def test_next_quarter_on_31st + assert_equal date_time_init(2005,11,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_quarter + end + + def test_next_year + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).next_year + end + + def test_prev_week + assert_equal date_time_init(2005,2,21,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week + assert_equal date_time_init(2005,2,22,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week(:tuesday) + assert_equal date_time_init(2005,2,25,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week(:friday) + assert_equal date_time_init(2006,10,30,0,0,0), date_time_init(2006,11,6,0,0,0).prev_week + assert_equal date_time_init(2006,11,15,0,0,0), date_time_init(2006,11,23,0,0,0).prev_week(:wednesday) + end + + def test_prev_month_on_31st + assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,3,31,10,10,10).prev_month + end + + def test_prev_quarter_on_31st + assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,5,31,10,10,10).prev_quarter + end + + def test_prev_year + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).prev_year + end + + def test_days_to_week_start + assert_equal 0, date_time_init(2011,11,01,0,0,0).days_to_week_start(:tuesday) + assert_equal 1, date_time_init(2011,11,02,0,0,0).days_to_week_start(:tuesday) + assert_equal 2, date_time_init(2011,11,03,0,0,0).days_to_week_start(:tuesday) + assert_equal 3, date_time_init(2011,11,04,0,0,0).days_to_week_start(:tuesday) + assert_equal 4, date_time_init(2011,11,05,0,0,0).days_to_week_start(:tuesday) + assert_equal 5, date_time_init(2011,11,06,0,0,0).days_to_week_start(:tuesday) + assert_equal 6, date_time_init(2011,11,07,0,0,0).days_to_week_start(:tuesday) + + assert_equal 3, date_time_init(2011,11,03,0,0,0).days_to_week_start(:monday) + assert_equal 3, date_time_init(2011,11,04,0,0,0).days_to_week_start(:tuesday) + assert_equal 3, date_time_init(2011,11,05,0,0,0).days_to_week_start(:wednesday) + assert_equal 3, date_time_init(2011,11,06,0,0,0).days_to_week_start(:thursday) + assert_equal 3, date_time_init(2011,11,07,0,0,0).days_to_week_start(:friday) + assert_equal 3, date_time_init(2011,11,8,0,0,0).days_to_week_start(:saturday) + assert_equal 3, date_time_init(2011,11,9,0,0,0).days_to_week_start(:sunday) + end + + def test_beginning_of_week + assert_equal date_time_init(2005,1,31,0,0,0), date_time_init(2005,2,4,10,10,10).beginning_of_week + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,28,0,0,0).beginning_of_week #monday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,29,0,0,0).beginning_of_week #tuesday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,30,0,0,0).beginning_of_week #wednesday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,01,0,0,0).beginning_of_week #thursday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,02,0,0,0).beginning_of_week #friday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,03,0,0,0).beginning_of_week #saturday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,04,0,0,0).beginning_of_week #sunday + end + + def test_end_of_week + assert_equal date_time_init(2008,1,6,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,31,10,10,10).end_of_week + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,27,0,0,0).end_of_week #monday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,28,0,0,0).end_of_week #tuesday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,29,0,0,0).end_of_week #wednesday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,30,0,0,0).end_of_week #thursday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,31,0,0,0).end_of_week #friday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,9,01,0,0,0).end_of_week #saturday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,9,02,0,0,0).end_of_week #sunday + end + + def test_end_of_month + assert_equal date_time_init(2005,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2005,3,20,10,10,10).end_of_month + assert_equal date_time_init(2005,2,28,23,59,59,Rational(999999999, 1000)), date_time_init(2005,2,20,10,10,10).end_of_month + assert_equal date_time_init(2005,4,30,23,59,59,Rational(999999999, 1000)), date_time_init(2005,4,20,10,10,10).end_of_month + end + + def test_end_of_year + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,2,22,10,10,10).end_of_year + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,31,10,10,10).end_of_year + end +end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 088b74a29a..7ae1f67785 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -1,7 +1,22 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class DateExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,*args) + Date.new(year,month,day) + end + + include DateAndTimeBehavior + + def test_yesterday_in_calendar_reform + assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday + end + + def test_tomorrow_in_calendar_reform + assert_equal Date.new(1582,10,15), Date.new(1582,10,4).tomorrow + end + def test_to_s date = Date.new(2005, 2, 21) assert_equal "2005-02-21", date.to_s @@ -46,22 +61,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2005,6,22), Date.new(2005,2,22).change(:month => 6) end - def test_beginning_of_week - assert_equal Date.new(2005,1,31), Date.new(2005,2,4).beginning_of_week - assert_equal Date.new(2005,11,28), Date.new(2005,11,28).beginning_of_week #monday - assert_equal Date.new(2005,11,28), Date.new(2005,11,29).beginning_of_week #tuesday - assert_equal Date.new(2005,11,28), Date.new(2005,11,30).beginning_of_week #wednesday - assert_equal Date.new(2005,11,28), Date.new(2005,12,01).beginning_of_week #thursday - assert_equal Date.new(2005,11,28), Date.new(2005,12,02).beginning_of_week #friday - assert_equal Date.new(2005,11,28), Date.new(2005,12,03).beginning_of_week #saturday - assert_equal Date.new(2005,11,28), Date.new(2005,12,04).beginning_of_week #sunday - end - - def test_monday - assert_equal Date.new(2005,11,28), Date.new(2005,11,28).monday - assert_equal Date.new(2005,11,28), Date.new(2005,12,01).monday - end - def test_sunday assert_equal Date.new(2008,3,2), Date.new(2008,3,02).sunday assert_equal Date.new(2008,3,2), Date.new(2008,2,29).sunday @@ -71,41 +70,10 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,1), Date.new(1582,10,15).beginning_of_week #friday end - def test_beginning_of_month - assert_equal Date.new(2005,2,1), Date.new(2005,2,22).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal Date.new(2005,1,1), Date.new(2005,2,15).beginning_of_quarter - assert_equal Date.new(2005,1,1), Date.new(2005,1,1).beginning_of_quarter - assert_equal Date.new(2005,10,1), Date.new(2005,12,31).beginning_of_quarter - assert_equal Date.new(2005,4,1), Date.new(2005,6,30).beginning_of_quarter - end - - def test_end_of_week - assert_equal Date.new(2008,2,24), Date.new(2008,2,22).end_of_week - assert_equal Date.new(2008,3,2), Date.new(2008,2,25).end_of_week #monday - assert_equal Date.new(2008,3,2), Date.new(2008,2,26).end_of_week #tuesday - assert_equal Date.new(2008,3,2), Date.new(2008,2,27).end_of_week #wednesday - assert_equal Date.new(2008,3,2), Date.new(2008,2,28).end_of_week #thursday - assert_equal Date.new(2008,3,2), Date.new(2008,2,29).end_of_week #friday - assert_equal Date.new(2008,3,2), Date.new(2008,3,01).end_of_week #saturday - assert_equal Date.new(2008,3,2), Date.new(2008,3,02).end_of_week #sunday - end - def test_end_of_week_in_calendar_reform assert_equal Date.new(1582,10,17), Date.new(1582,10,4).end_of_week #thursday end - def test_end_of_quarter - assert_equal Date.new(2008,3,31), Date.new(2008,2,15).end_of_quarter - assert_equal Date.new(2008,3,31), Date.new(2008,3,31).end_of_quarter - assert_equal Date.new(2008,12,31), Date.new(2008,10,8).end_of_quarter - assert_equal Date.new(2008,6,30), Date.new(2008,4,14).end_of_quarter - assert_equal Date.new(2008,6,30), Date.new(2008,5,31).end_of_quarter - assert_equal Date.new(2008,9,30), Date.new(2008,8,21).end_of_quarter - end - def test_end_of_year assert_equal Date.new(2008,12,31).to_s, Date.new(2008,2,22).end_of_year.to_s end @@ -116,57 +84,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2005,4,30), Date.new(2005,4,20).end_of_month end - def test_beginning_of_year - assert_equal Date.new(2005,1,1).to_s, Date.new(2005,2,22).beginning_of_year.to_s - end - - def test_weeks_ago - assert_equal Date.new(2005,5,10), Date.new(2005,5,17).weeks_ago(1) - assert_equal Date.new(2005,5,10), Date.new(2005,5,24).weeks_ago(2) - assert_equal Date.new(2005,5,10), Date.new(2005,5,31).weeks_ago(3) - assert_equal Date.new(2005,5,10), Date.new(2005,6,7).weeks_ago(4) - assert_equal Date.new(2006,12,31), Date.new(2007,2,4).weeks_ago(5) - end - - def test_months_ago - assert_equal Date.new(2005,5,5), Date.new(2005,6,5).months_ago(1) - assert_equal Date.new(2004,11,5), Date.new(2005,6,5).months_ago(7) - assert_equal Date.new(2004,12,5), Date.new(2005,6,5).months_ago(6) - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).months_ago(12) - assert_equal Date.new(2003,6,5), Date.new(2005,6,5).months_ago(24) - end - - def test_months_since - assert_equal Date.new(2005,7,5), Date.new(2005,6,5).months_since(1) - assert_equal Date.new(2006,1,5), Date.new(2005,12,5).months_since(1) - assert_equal Date.new(2005,12,5), Date.new(2005,6,5).months_since(6) - assert_equal Date.new(2006,6,5), Date.new(2005,12,5).months_since(6) - assert_equal Date.new(2006,1,5), Date.new(2005,6,5).months_since(7) - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).months_since(12) - assert_equal Date.new(2007,6,5), Date.new(2005,6,5).months_since(24) - assert_equal Date.new(2005,4,30), Date.new(2005,3,31).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,29).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,30).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,31).months_since(1) - end - - def test_years_ago - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).years_ago(1) - assert_equal Date.new(1998,6,5), Date.new(2005,6,5).years_ago(7) - assert_equal Date.new(2003,2,28), Date.new(2004,2,29).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).years_since(1) - assert_equal Date.new(2012,6,5), Date.new(2005,6,5).years_since(7) - assert_equal Date.new(2182,6,5), Date.new(2005,6,5).years_since(177) - assert_equal Date.new(2005,2,28), Date.new(2004,2,29).years_since(1) # 1 year since leap day - end - - def test_prev_year - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).prev_year - end - def test_prev_year_in_leap_years assert_equal Date.new(1999,2,28), Date.new(2000,2,29).prev_year end @@ -187,10 +104,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year end - def test_next_year - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).next_year - end - def test_next_year_in_leap_years assert_equal Date.new(2001,2,28), Date.new(2000,2,29).next_year end @@ -199,24 +112,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,4), Date.new(1581,10,10).next_year end - def test_yesterday - assert_equal Date.new(2005,2,21), Date.new(2005,2,22).yesterday - assert_equal Date.new(2005,2,28), Date.new(2005,3,2).yesterday.yesterday - end - - def test_yesterday_in_calendar_reform - assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday - end - - def test_tomorrow - assert_equal Date.new(2005,2,23), Date.new(2005,2,22).tomorrow - assert_equal Date.new(2005,3,2), Date.new(2005,2,28).tomorrow.tomorrow - end - - def test_tomorrow_in_calendar_reform - assert_equal Date.new(1582,10,15), Date.new(1582,10,4).tomorrow - end - def test_advance assert_equal Date.new(2006,2,28), Date.new(2005,2,28).advance(:years => 1) assert_equal Date.new(2005,6,28), Date.new(2005,2,28).advance(:months => 4) @@ -249,14 +144,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_prev_week - assert_equal Date.new(2005,5,9), Date.new(2005,5,17).prev_week - assert_equal Date.new(2006,12,25), Date.new(2007,1,7).prev_week - assert_equal Date.new(2010,2,12), Date.new(2010,2,19).prev_week(:friday) - assert_equal Date.new(2010,2,13), Date.new(2010,2,19).prev_week(:saturday) - assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday) - end - def test_last_week assert_equal Date.new(2005,5,9), Date.new(2005,5,17).last_week assert_equal Date.new(2006,12,25), Date.new(2007,1,7).last_week @@ -265,38 +152,15 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2010,2,27), Date.new(2010,3,4).last_week(:saturday) end - def test_next_week - assert_equal Date.new(2005,2,28), Date.new(2005,2,22).next_week - assert_equal Date.new(2005,3,4), Date.new(2005,2,22).next_week(:friday) - assert_equal Date.new(2006,10,30), Date.new(2006,10,23).next_week - assert_equal Date.new(2006,11,1), Date.new(2006,10,23).next_week(:wednesday) - end - def test_next_week_in_calendar_reform assert_equal Date.new(1582,10,15), Date.new(1582,9,30).next_week(:friday) assert_equal Date.new(1582,10,18), Date.new(1582,10,4).next_week end - def test_next_month_on_31st - assert_equal Date.new(2005, 9, 30), Date.new(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month end - def test_next_quarter_on_31st - assert_equal Date.new(2005, 11, 30), Date.new(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).last_quarter end @@ -420,13 +284,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_today - Date.stubs(:current).returns(Date.new(2000, 1, 1)) - assert_equal false, Date.new(1999, 12, 31).today? - assert_equal true, Date.new(2000,1,1).today? - assert_equal false, Date.new(2000,1,2).today? - end - def test_past Date.stubs(:current).returns(Date.new(2000, 1, 1)) assert_equal true, Date.new(1999, 12, 31).past? diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 21b7efdc73..b1d1e8ecb4 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -1,7 +1,14 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class DateTimeExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,hour,minute,second,*args) + DateTime.civil(year,month,day,hour,minute,second) + end + + include DateAndTimeBehavior + def test_to_s datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0) assert_equal "2005-02-21 14:30:00", datetime.to_s(:db) @@ -54,35 +61,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal 86399,DateTime.civil(2005,1,1,23,59,59).seconds_since_midnight end - def test_days_to_week_start - assert_equal 0, Time.local(2011,11,01,0,0,0).days_to_week_start(:tuesday) - assert_equal 1, Time.local(2011,11,02,0,0,0).days_to_week_start(:tuesday) - assert_equal 2, Time.local(2011,11,03,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 4, Time.local(2011,11,05,0,0,0).days_to_week_start(:tuesday) - assert_equal 5, Time.local(2011,11,06,0,0,0).days_to_week_start(:tuesday) - assert_equal 6, Time.local(2011,11,07,0,0,0).days_to_week_start(:tuesday) - - assert_equal 3, Time.local(2011,11,03,0,0,0).days_to_week_start(:monday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,05,0,0,0).days_to_week_start(:wednesday) - assert_equal 3, Time.local(2011,11,06,0,0,0).days_to_week_start(:thursday) - assert_equal 3, Time.local(2011,11,07,0,0,0).days_to_week_start(:friday) - assert_equal 3, Time.local(2011,11,8,0,0,0).days_to_week_start(:saturday) - assert_equal 3, Time.local(2011,11,9,0,0,0).days_to_week_start(:sunday) - end - - def test_beginning_of_week - assert_equal DateTime.civil(2005,1,31), DateTime.civil(2005,2,4,10,10,10).beginning_of_week - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,28,0,0,0).beginning_of_week #monday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,29,0,0,0).beginning_of_week #tuesday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,30,0,0,0).beginning_of_week #wednesday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,01,0,0,0).beginning_of_week #thursday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,02,0,0,0).beginning_of_week #friday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,03,0,0,0).beginning_of_week #saturday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,04,0,0,0).beginning_of_week #sunday - end - def test_beginning_of_day assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day end @@ -99,82 +77,16 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2005,2,4,19,59,59), DateTime.civil(2005,2,4,19,30,10).end_of_hour end - def test_beginning_of_month - assert_equal DateTime.civil(2005,2,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,15,10,10,10).beginning_of_quarter - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,1,1,0,0,0).beginning_of_quarter - assert_equal DateTime.civil(2005,10,1,0,0,0), DateTime.civil(2005,12,31,10,10,10).beginning_of_quarter - assert_equal DateTime.civil(2005,4,1,0,0,0), DateTime.civil(2005,6,30,23,59,59).beginning_of_quarter - end - def test_end_of_month assert_equal DateTime.civil(2005,3,31,23,59,59), DateTime.civil(2005,3,20,10,10,10).end_of_month assert_equal DateTime.civil(2005,2,28,23,59,59), DateTime.civil(2005,2,20,10,10,10).end_of_month assert_equal DateTime.civil(2005,4,30,23,59,59), DateTime.civil(2005,4,20,10,10,10).end_of_month end - def test_beginning_of_year - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_year - end - - def test_weeks_ago - assert_equal DateTime.civil(2005,5,29,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(1) - assert_equal DateTime.civil(2005,5,1,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(5) - assert_equal DateTime.civil(2005,4,24,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(6) - assert_equal DateTime.civil(2005,2,27,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(14) - assert_equal DateTime.civil(2004,12,25,10), DateTime.civil(2005,1,1,10,0,0).weeks_ago(1) - end - - def test_months_ago - assert_equal DateTime.civil(2005,5,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(1) - assert_equal DateTime.civil(2004,11,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(7) - assert_equal DateTime.civil(2004,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(6) - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(12) - assert_equal DateTime.civil(2003,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(24) - end - - def test_months_since - assert_equal DateTime.civil(2005,7,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(1) - assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(6) - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(6) - assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(7) - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(12) - assert_equal DateTime.civil(2007,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(24) - assert_equal DateTime.civil(2005,4,30,10), DateTime.civil(2005,3,31,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,29,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,30,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,31,10,0,0).months_since(1) - end - - def test_years_ago - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(1) - assert_equal DateTime.civil(1998,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(7) - assert_equal DateTime.civil(2003,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(1) - assert_equal DateTime.civil(2012,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(7) - assert_equal DateTime.civil(2182,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(177) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_since(1) # 1 year since leap day - end - - def test_prev_year - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year - end - def test_last_year assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year end - def test_next_year - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).next_year - end - def test_ago assert_equal DateTime.civil(2005,2,22,10,10,9), DateTime.civil(2005,2,22,10,10,10).ago(1) assert_equal DateTime.civil(2005,2,22,9,10,10), DateTime.civil(2005,2,22,10,10,10).ago(3600) @@ -191,16 +103,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2005,2,22,10,10,12), DateTime.civil(2005,2,22,10,10,10).since(1.667) end - def test_yesterday - assert_equal DateTime.civil(2005,2,21,10,10,10), DateTime.civil(2005,2,22,10,10,10).yesterday - assert_equal DateTime.civil(2005,2,28,10,10,10), DateTime.civil(2005,3,2,10,10,10).yesterday.yesterday - end - - def test_tomorrow - assert_equal DateTime.civil(2005,2,23,10,10,10), DateTime.civil(2005,2,22,10,10,10).tomorrow - assert_equal DateTime.civil(2005,3,2,10,10,10), DateTime.civil(2005,2,28,10,10,10).tomorrow.tomorrow - end - def test_change assert_equal DateTime.civil(2006,2,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:year => 2006) assert_equal DateTime.civil(2005,6,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:month => 6) @@ -236,14 +138,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 22, 58, 59).advance(:months => 1, :hours => 1, :minutes => 1, :seconds => 1) end - def test_prev_week - assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).prev_week - assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).prev_week(:tuesday) - assert_equal DateTime.civil(2005,2,25), DateTime.civil(2005,3,1,15,15,10).prev_week(:friday) - assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,11,6,0,0,0).prev_week - assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).prev_week(:wednesday) - end - def test_last_week assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).last_week assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).last_week(:tuesday) @@ -252,33 +146,10 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).last_week(:wednesday) end - def test_next_week - assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week - assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday) - assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,10,23,0,0,0).next_week - assert_equal DateTime.civil(2006,11,1), DateTime.civil(2006,10,23,0,0,0).next_week(:wednesday) - end - - def test_next_month_on_31st - assert_equal DateTime.civil(2005, 9, 30), DateTime.civil(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month end - def test_next_quarter_on_31st - assert_equal DateTime.civil(2005, 11, 30), DateTime.civil(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).last_quarter end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 412aef9301..6d6757a1b6 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -1,7 +1,14 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class TimeExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,hour,minute,second,usec=0) + Time.local(year,month,day,hour,minute,second,usec) + end + + include DateAndTimeBehavior + def test_seconds_since_midnight assert_equal 1,Time.local(2005,1,1,0,0,1).seconds_since_midnight assert_equal 60,Time.local(2005,1,1,0,1,0).seconds_since_midnight @@ -50,37 +57,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_beginning_of_week - assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week - assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday - assert_equal Time.local(2005,11,28), Time.local(2005,11,29,0,0,0).beginning_of_week #tuesday - assert_equal Time.local(2005,11,28), Time.local(2005,11,30,0,0,0).beginning_of_week #wednesday - assert_equal Time.local(2005,11,28), Time.local(2005,12,01,0,0,0).beginning_of_week #thursday - assert_equal Time.local(2005,11,28), Time.local(2005,12,02,0,0,0).beginning_of_week #friday - assert_equal Time.local(2005,11,28), Time.local(2005,12,03,0,0,0).beginning_of_week #saturday - assert_equal Time.local(2005,11,28), Time.local(2005,12,04,0,0,0).beginning_of_week #sunday - - end - - def test_days_to_week_start - assert_equal 0, Time.local(2011,11,01,0,0,0).days_to_week_start(:tuesday) - assert_equal 1, Time.local(2011,11,02,0,0,0).days_to_week_start(:tuesday) - assert_equal 2, Time.local(2011,11,03,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 4, Time.local(2011,11,05,0,0,0).days_to_week_start(:tuesday) - assert_equal 5, Time.local(2011,11,06,0,0,0).days_to_week_start(:tuesday) - assert_equal 6, Time.local(2011,11,07,0,0,0).days_to_week_start(:tuesday) - - assert_equal 3, Time.local(2011,11,03,0,0,0).days_to_week_start(:monday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,05,0,0,0).days_to_week_start(:wednesday) - assert_equal 3, Time.local(2011,11,06,0,0,0).days_to_week_start(:thursday) - assert_equal 3, Time.local(2011,11,07,0,0,0).days_to_week_start(:friday) - assert_equal 3, Time.local(2011,11,8,0,0,0).days_to_week_start(:saturday) - assert_equal 3, Time.local(2011,11,9,0,0,0).days_to_week_start(:sunday) - end - - def test_beginning_of_day assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day with_env_tz 'US/Eastern' do @@ -97,17 +73,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour end - def test_beginning_of_month - assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,15,10,10,10).beginning_of_quarter - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,1,1,0,0,0).beginning_of_quarter - assert_equal Time.local(2005,10,1,0,0,0), Time.local(2005,12,31,10,10,10).beginning_of_quarter - assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter - end - def test_end_of_day assert_equal Time.local(2007,8,12,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,12,10,10,10).end_of_day with_env_tz 'US/Eastern' do @@ -120,100 +85,14 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_end_of_week - assert_equal Time.local(2008,1,6,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_week - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,27,0,0,0).end_of_week #monday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,28,0,0,0).end_of_week #tuesday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,29,0,0,0).end_of_week #wednesday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,30,0,0,0).end_of_week #thursday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,31,0,0,0).end_of_week #friday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,01,0,0,0).end_of_week #saturday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,02,0,0,0).end_of_week #sunday - end - def test_end_of_hour assert_equal Time.local(2005,2,4,19,59,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_hour end - def test_end_of_month - assert_equal Time.local(2005,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2005,3,20,10,10,10).end_of_month - assert_equal Time.local(2005,2,28,23,59,59,Rational(999999999, 1000)), Time.local(2005,2,20,10,10,10).end_of_month - assert_equal Time.local(2005,4,30,23,59,59,Rational(999999999, 1000)), Time.local(2005,4,20,10,10,10).end_of_month - end - - def test_end_of_quarter - assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,15,10,10,10).end_of_quarter - assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,3,31,0,0,0).end_of_quarter - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,21,10,10,10).end_of_quarter - assert_equal Time.local(2007,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2007,4,1,0,0,0).end_of_quarter - assert_equal Time.local(2008,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2008,5,31,0,0,0).end_of_quarter - end - - def test_end_of_year - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,22,10,10,10).end_of_year - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_year - end - - def test_beginning_of_year - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year - end - - def test_weeks_ago - assert_equal Time.local(2005,5,29,10), Time.local(2005,6,5,10,0,0).weeks_ago(1) - assert_equal Time.local(2005,5,1,10), Time.local(2005,6,5,10,0,0).weeks_ago(5) - assert_equal Time.local(2005,4,24,10), Time.local(2005,6,5,10,0,0).weeks_ago(6) - assert_equal Time.local(2005,2,27,10), Time.local(2005,6,5,10,0,0).weeks_ago(14) - assert_equal Time.local(2004,12,25,10), Time.local(2005,1,1,10,0,0).weeks_ago(1) - end - - def test_months_ago - assert_equal Time.local(2005,5,5,10), Time.local(2005,6,5,10,0,0).months_ago(1) - assert_equal Time.local(2004,11,5,10), Time.local(2005,6,5,10,0,0).months_ago(7) - assert_equal Time.local(2004,12,5,10), Time.local(2005,6,5,10,0,0).months_ago(6) - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(12) - assert_equal Time.local(2003,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(24) - end - - def test_months_since - assert_equal Time.local(2005,7,5,10), Time.local(2005,6,5,10,0,0).months_since(1) - assert_equal Time.local(2006,1,5,10), Time.local(2005,12,5,10,0,0).months_since(1) - assert_equal Time.local(2005,12,5,10), Time.local(2005,6,5,10,0,0).months_since(6) - assert_equal Time.local(2006,6,5,10), Time.local(2005,12,5,10,0,0).months_since(6) - assert_equal Time.local(2006,1,5,10), Time.local(2005,6,5,10,0,0).months_since(7) - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).months_since(12) - assert_equal Time.local(2007,6,5,10), Time.local(2005,6,5,10,0,0).months_since(24) - assert_equal Time.local(2005,4,30,10), Time.local(2005,3,31,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,29,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,30,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,31,10,0,0).months_since(1) - end - - def test_years_ago - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(1) - assert_equal Time.local(1998,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(7) - assert_equal Time.local(2003,2,28,10), Time.local(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).years_since(1) - assert_equal Time.local(2012,6,5,10), Time.local(2005,6,5,10,0,0).years_since(7) - assert_equal Time.local(2005,2,28,10), Time.local(2004,2,29,10,0,0).years_since(1) # 1 year since leap day - # Failure because of size limitations of numeric? - # assert_equal Time.local(2182,6,5,10), Time.local(2005,6,5,10,0,0).years_since(177) - end - - def test_prev_year - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year - end - def test_last_year assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year end - def test_next_year - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).next_year - end - def test_ago assert_equal Time.local(2005,2,22,10,10,9), Time.local(2005,2,22,10,10,10).ago(1) assert_equal Time.local(2005,2,22,9,10,10), Time.local(2005,2,22,10,10,10).ago(3600) @@ -426,16 +305,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_yesterday - assert_equal Time.local(2005,2,21,10,10,10), Time.local(2005,2,22,10,10,10).yesterday - assert_equal Time.local(2005,2,28,10,10,10), Time.local(2005,3,2,10,10,10).yesterday.yesterday - end - - def test_tomorrow - assert_equal Time.local(2005,2,23,10,10,10), Time.local(2005,2,22,10,10,10).tomorrow - assert_equal Time.local(2005,3,2,10,10,10), Time.local(2005,2,28,10,10,10).tomorrow.tomorrow - end - def test_change assert_equal Time.local(2006,2,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2006) assert_equal Time.local(2005,6,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:month => 6) @@ -539,16 +408,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal t, t.advance(:months => 0) end - def test_prev_week - with_env_tz 'US/Eastern' do - assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).prev_week - assert_equal Time.local(2005,2,22), Time.local(2005,3,1,15,15,10).prev_week(:tuesday) - assert_equal Time.local(2005,2,25), Time.local(2005,3,1,15,15,10).prev_week(:friday) - assert_equal Time.local(2006,10,30), Time.local(2006,11,6,0,0,0).prev_week - assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).prev_week(:wednesday) - end - end - def test_last_week with_env_tz 'US/Eastern' do assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week @@ -559,16 +418,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_next_week - with_env_tz 'US/Eastern' do - assert_equal Time.local(2005,2,28), Time.local(2005,2,22,15,15,10).next_week - assert_equal Time.local(2005,3,1), Time.local(2005,2,22,15,15,10).next_week(:tuesday) - assert_equal Time.local(2005,3,4), Time.local(2005,2,22,15,15,10).next_week(:friday) - assert_equal Time.local(2006,10,30), Time.local(2006,10,23,0,0,0).next_week - assert_equal Time.local(2006,11,1), Time.local(2006,10,23,0,0,0).next_week(:wednesday) - end - end - def test_next_week_near_daylight_start with_env_tz 'US/Eastern' do assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight' @@ -709,14 +558,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_next_month_on_31st - assert_equal Time.local(2005, 9, 30), Time.local(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month end @@ -938,15 +779,6 @@ class TimeExtMarshalingTest < ActiveSupport::TestCase assert_equal t, unmarshaled end - - def test_next_quarter_on_31st - assert_equal Time.local(2005, 11, 30), Time.local(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter end |