From 50861013c22eeae152bbe8c4cd6a9c21011edc1b Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 14 Jun 2007 01:17:42 +0000 Subject: Improve Time and Date test coverage. Closes #8646. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7019 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ activesupport/test/core_ext/date_ext_test.rb | 32 ++++++++++-------- activesupport/test/core_ext/date_time_ext_test.rb | 23 ++++++++++--- activesupport/test/core_ext/time_ext_test.rb | 40 +++++++++++++---------- 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 40e811c7e0..208b276624 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Improve Time and Date test coverage. #8646 [Josh Peek] + * Add Date#since, ago, beginning_of_day, and end_of_day. Date + seconds works now. #8575 [Geoff Buesing] * String#to_time overflows to DateTime. Add String#to_datetime. #8572 [Geoff Buesing] diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index b81e18e13b..8863e6a07f 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -2,13 +2,15 @@ require File.dirname(__FILE__) + '/../abstract_unit' class DateExtCalculationsTest < Test::Unit::TestCase def test_to_s - assert_equal "21 Feb", Date.new(2005, 2, 21).to_s(:short) - assert_equal "February 21, 2005", Date.new(2005, 2, 21).to_s(:long) - assert_equal "February 21st, 2005", Date.new(2005, 2, 21).to_s(:long_ordinal) - assert_equal "2005-02-21", Date.new(2005, 2, 21).to_s(:db) - assert_equal "21 Feb 2005", Date.new(2005, 2, 21).to_s(:rfc822) + date = Date.new(2005, 2, 21) + assert_equal "2005-02-21", date.to_s + assert_equal "21 Feb", date.to_s(:short) + assert_equal "February 21, 2005", date.to_s(:long) + assert_equal "February 21st, 2005", date.to_s(:long_ordinal) + assert_equal "2005-02-21", date.to_s(:db) + assert_equal "21 Feb 2005", date.to_s(:rfc822) end - + def test_readable_inspect assert_equal "Mon, 21 Feb 2005", Date.new(2005, 2, 21).readable_inspect assert_equal Date.new(2005, 2, 21).readable_inspect, Date.new(2005, 2, 21).inspect @@ -18,7 +20,7 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2005, 2, 21), Date.new(2005, 2, 21).to_time assert_equal Time.local_time(2039, 2, 21), Date.new(2039, 2, 21).to_time end - + def test_to_datetime assert_equal DateTime.civil(2005, 2, 21), Date.new(2005, 2, 21).to_datetime end @@ -33,7 +35,7 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal Date.new(2006,2,22), Date.new(2005,2,22).change(:year => 2006) assert_equal Date.new(2005,6,22), Date.new(2005,2,22).change(:month => 6) end - + def test_begining_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 @@ -100,6 +102,10 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal Date.new(2004,6,5), Date.new(2005,6,5).last_year end + def test_next_year + assert_equal Date.new(2006,6,5), Date.new(2005,6,5).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 @@ -135,23 +141,23 @@ class DateExtCalculationsTest < Test::Unit::TestCase def test_yesterday_constructor assert_equal Date.today - 1, Date.yesterday end - + def test_tomorrow_constructor assert_equal Date.today + 1, Date.tomorrow end - + def test_since assert_equal Time.local(2005,2,21,0,0,45), Date.new(2005,2,21).since(45) end - + def test_ago assert_equal Time.local(2005,2,20,23,59,15), Date.new(2005,2,21).ago(45) end - + def test_beginning_of_day assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day end - + def test_end_of_day assert_equal Time.local(2005,2,21,23,59,59), Date.new(2005,2,21).end_of_day end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 8c663ee431..f553f8e60b 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../abstract_unit' class DateTimeExtCalculationsTest < Test::Unit::TestCase def test_to_s datetime = DateTime.new(2005, 2, 21, 14, 30, 0) - + assert_equal "2005-02-21T14:30:00+00:00", datetime.to_s assert_equal "2005-02-21 14:30:00", datetime.to_s(:db) assert_equal "14:30", datetime.to_s(:time) assert_equal "21 Feb 14:30", datetime.to_s(:short) @@ -11,13 +11,13 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.to_s(:rfc822) assert_equal "February 21st, 2005 14:30", datetime.to_s(:long_ordinal) end - + def test_readable_inspect datetime = DateTime.new(2005, 2, 21, 14, 30, 0) assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.readable_inspect assert_equal datetime.readable_inspect, datetime.inspect end - + def test_custom_date_format Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S' assert_equal '20050221143000', DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:custom) @@ -27,7 +27,11 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase def test_to_date assert_equal Date.new(2005, 2, 21), DateTime.new(2005, 2, 21).to_date end - + + def test_to_datetime + assert_equal DateTime.new(2005, 2, 21), DateTime.new(2005, 2, 21).to_datetime + end + def test_to_time assert_equal Time.utc(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0, 0).to_time assert_equal Time.local(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24), 0).to_time @@ -56,6 +60,10 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day end + def test_end_of_day + assert_equal DateTime.civil(2005,2,4,23,59,59), DateTime.civil(2005,2,4,10,10,10).end_of_day + 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 @@ -111,6 +119,9 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase 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) @@ -172,4 +183,8 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase def test_xmlschema_is_available assert_nothing_raised { DateTime.now.xmlschema } end + + def test_acts_like_time + assert DateTime.new.acts_like_time? + end end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 38a6389674..b390ae2249 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -128,6 +128,9 @@ class TimeExtCalculationsTest < Test::Unit::TestCase 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) @@ -271,27 +274,36 @@ class TimeExtCalculationsTest < Test::Unit::TestCase end def test_to_s - time = Time.local(2005, 2, 21, 17, 44, 30) - assert_equal "2005-02-21 17:44:30", time.to_s(:db) - assert_equal "21 Feb 17:44", time.to_s(:short) - assert_equal "17:44", time.to_s(:time) - assert_equal "February 21, 2005 17:44", time.to_s(:long) - assert_equal "February 21st, 2005 17:44", time.to_s(:long_ordinal) - time = Time.utc(2005, 2, 21, 17, 44, 30) + assert_equal "Mon Feb 21 17:44:30 UTC 2005", time.to_s + assert_equal "2005-02-21 17:44:30", time.to_s(:db) + assert_equal "21 Feb 17:44", time.to_s(:short) + assert_equal "17:44", time.to_s(:time) + assert_equal "February 21, 2005 17:44", time.to_s(:long) + assert_equal "February 21st, 2005 17:44", time.to_s(:long_ordinal) assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822) end - + def test_custom_date_format Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S' assert_equal '20050221143000', Time.local(2005, 2, 21, 14, 30, 0).to_s(:custom) Time::DATE_FORMATS.delete(:custom) - end + end def test_to_date assert_equal Date.new(2005, 2, 21), Time.local(2005, 2, 21, 17, 44, 30).to_date end + def test_to_datetime + assert_equal Time.utc(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, 0, 0) + with_timezone 'US/Eastern' do + assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0) + end + with_timezone 'NZ' do + assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0) + end + end + def test_to_time assert_equal Time.local(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30).to_time end @@ -360,14 +372,8 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_nothing_raised { Time.now.xmlschema } end - def test_to_datetime - assert_equal Time.utc(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, 0, 0) - with_timezone 'US/Eastern' do - assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0) - end - with_timezone 'NZ' do - assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, Rational(Time.local(2005, 2, 21, 17, 44, 30).utc_offset, 86400), 0) - end + def test_acts_like_time + assert Time.new.acts_like_time? end protected -- cgit v1.2.3