diff options
author | Geoff Buesing <gbuesing@gmail.com> | 2008-01-21 03:55:54 +0000 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2008-01-21 03:55:54 +0000 |
commit | 1d4f4cdfe22cbe4962ae8953c96bc5c70d8d4e6a (patch) | |
tree | dea694c566e1a13c2e63a5a617370542517b7627 /activesupport/test | |
parent | 9c4beb5e982aae09a0813f82b8634baa8f8b4d08 (diff) | |
download | rails-1d4f4cdfe22cbe4962ae8953c96bc5c70d8d4e6a.tar.gz rails-1d4f4cdfe22cbe4962ae8953c96bc5c70d8d4e6a.tar.bz2 rails-1d4f4cdfe22cbe4962ae8953c96bc5c70d8d4e6a.zip |
Replace non-dst-aware TimeZone class with dst-aware class from tzinfo_timezone plugin. TimeZone#adjust and #unadjust are no longer available; tzinfo gem must now be present in order to perform time zone calculations, via #local_to_utc and #utc_to_local methods.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8679 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/abstract_unit.rb | 25 | ||||
-rw-r--r-- | activesupport/test/time_zone_test.rb | 139 |
2 files changed, 96 insertions, 68 deletions
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 2cfa245bc8..c2fd943ca0 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -4,15 +4,26 @@ $:.unshift "#{File.dirname(__FILE__)}/../lib" $:.unshift File.dirname(__FILE__) require 'active_support' +def uses_gem(gem_name, test_name, version = '> 0') + require 'rubygems' + gem gem_name.to_s, version + require gem_name.to_s + yield +rescue LoadError + $stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again." +end + # Wrap tests that use Mocha and skip if unavailable. unless defined? uses_mocha - def uses_mocha(test_name) - require 'rubygems' - gem 'mocha', '>= 0.5.5' - require 'mocha' - yield - rescue LoadError - $stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again." + def uses_mocha(test_name, &block) + uses_gem('mocha', test_name, '>= 0.5.5', &block) + end +end + +# Wrap tests that use TZInfo and skip if unavailable. +unless defined? uses_tzinfo + def uses_tzinfo(test_name, &block) + uses_gem('tzinfo', test_name, &block) end end diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index a974d85dd7..939d928b3e 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -1,94 +1,110 @@ require 'abstract_unit' class TimeZoneTest < Test::Unit::TestCase - class MockTime - def self.now - Time.utc( 2004, 7, 25, 14, 49, 00 ) + + uses_tzinfo 'TestTimeZoneCalculations' do + + def test_utc_to_local + silence_warnings do # silence warnings raised by tzinfo gem + zone = TimeZone['Eastern Time (US & Canada)'] + assert_equal Time.utc(1999, 12, 31, 19), zone.utc_to_local(Time.utc(2000, 1)) # standard offset -0500 + assert_equal Time.utc(2000, 6, 30, 20), zone.utc_to_local(Time.utc(2000, 7)) # dst offset -0400 + end end - - def self.local(*args) - Time.utc(*args) + + def test_local_to_utc + silence_warnings do # silence warnings raised by tzinfo gem + zone = TimeZone['Eastern Time (US & Canada)'] + assert_equal Time.utc(2000, 1, 1, 5), zone.local_to_utc(Time.utc(2000, 1)) # standard offset -0500 + assert_equal Time.utc(2000, 7, 1, 4), zone.local_to_utc(Time.utc(2000, 7)) # dst offset -0400 + end + end + + def test_period_for_local + silence_warnings do # silence warnings raised by tzinfo gem + zone = TimeZone['Eastern Time (US & Canada)'] + assert_instance_of TZInfo::TimezonePeriod, zone.period_for_local(Time.utc(2000)) + end + end + + TimeZone::MAPPING.keys.each do |name| + define_method("test_map_#{name.downcase.gsub(/[^a-z]/, '_')}_to_tzinfo") do + silence_warnings do # silence warnings raised by tzinfo gem + zone = TimeZone[name] + assert zone.tzinfo.respond_to?(:period_for_local) + end + end end - end - TimeZone::Time = MockTime + TimeZone.all.each do |zone| + name = zone.name.downcase.gsub(/[^a-z]/, '_') + define_method("test_from_#{name}_to_map") do + silence_warnings do # silence warnings raised by tzinfo gem + assert_instance_of TimeZone, TimeZone[zone.name] + end + end + + define_method("test_utc_offset_for_#{name}") do + silence_warnings do # silence warnings raised by tzinfo gem + period = zone.tzinfo.period_for_utc(Time.utc(2006,1,1,0,0,0)) + assert_equal period.utc_offset, zone.utc_offset + end + end + end + uses_mocha 'TestTimeZoneNowAndToday' do + def test_now + TZInfo::DataTimezone.any_instance.stubs(:now).returns(Time.utc(2000)) + assert_equal Time.utc(2000), TimeZone['Eastern Time (US & Canada)'].now + end + + def test_today + TZInfo::DataTimezone.any_instance.stubs(:now).returns(Time.utc(2000)) + assert_equal Date.new(2000), TimeZone['Eastern Time (US & Canada)'].today + end + end + end + def test_formatted_offset_positive - zone = TimeZone.create( "Test", 4200 ) - assert_equal "+01:10", zone.formatted_offset + zone = TimeZone['Moscow'] + assert_equal "+03:00", zone.formatted_offset + assert_equal "+0300", zone.formatted_offset(false) end - + def test_formatted_offset_negative - zone = TimeZone.create( "Test", -4200 ) - assert_equal "-01:10", zone.formatted_offset - end - - def test_now - zone = TimeZone.create( "Test", 4200 ) - assert_equal Time.local(2004,7,25,15,59,00).to_a[0,6], zone.now.to_a[0,6] - end - - def test_today - zone = TimeZone.create( "Test", 43200 ) - assert_equal Date.new(2004,7,26), zone.today - end - - def test_adjust_negative - zone = TimeZone.create( "Test", -4200 ) # 4200s == 70 mins - assert_equal Time.utc(2004,7,24,23,55,0), zone.adjust(Time.utc(2004,7,25,1,5,0)) + zone = TimeZone['Eastern Time (US & Canada)'] + assert_equal "-05:00", zone.formatted_offset + assert_equal "-0500", zone.formatted_offset(false) end - - def test_adjust_positive - zone = TimeZone.create( "Test", 4200 ) - assert_equal Time.utc(2004,7,26,1,5,0), zone.adjust(Time.utc(2004,7,25,23,55,0)) - end - - def test_unadjust - zone = TimeZone.create( "Test", 4200 ) - expect = Time.utc(2004,7,24,23,55,0).to_a[0,6] - actual = zone.unadjust(Time.utc(2004,7,25,1,5,0)).to_a[0,6] - assert_equal expect, actual - end - + def test_zone_compare - zone1 = TimeZone.create( "Test1", 4200 ) - zone2 = TimeZone.create( "Test1", 5600 ) - assert zone1 < zone2 - assert zone2 > zone1 - - zone1 = TimeZone.create( "Able", 10000 ) - zone2 = TimeZone.create( "Zone", 10000 ) + zone1 = TimeZone['Central Time (US & Canada)'] # offset -0600 + zone2 = TimeZone['Eastern Time (US & Canada)'] # offset -0500 assert zone1 < zone2 assert zone2 > zone1 - - zone1 = TimeZone.create( "Able", 10000 ) assert zone1 == zone1 end - + def test_to_s - zone = TimeZone.create( "Test", 4200 ) - assert_equal "(UTC+01:10) Test", zone.to_s + assert_equal "(GMT+03:00) Moscow", TimeZone['Moscow'].to_s end - + def test_all_sorted all = TimeZone.all 1.upto( all.length-1 ) do |i| assert all[i-1] < all[i] end end - + def test_index assert_nil TimeZone["bogus"] - assert_not_nil TimeZone["Central Time (US & Canada)"] - assert_not_nil TimeZone[8] + assert_instance_of TimeZone, TimeZone["Central Time (US & Canada)"] + assert_instance_of TimeZone, TimeZone[8] assert_raises(ArgumentError) { TimeZone[false] } end def test_new - a = TimeZone.new("Berlin") - b = TimeZone.new("Berlin") - assert_same a, b - assert_nil TimeZone.new("bogus") + assert_equal TimeZone["Central Time (US & Canada)"], TimeZone.new("Central Time (US & Canada)") end def test_us_zones @@ -96,3 +112,4 @@ class TimeZoneTest < Test::Unit::TestCase assert !TimeZone.us_zones.include?(TimeZone["Kuala Lumpur"]) end end + |