diff options
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 52 | ||||
-rw-r--r-- | activesupport/test/core_ext/class_test.rb | 6 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_ext_test.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/duration_test.rb | 36 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 26 | ||||
-rw-r--r-- | activesupport/test/core_ext/load_error_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/module/synchronization_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/module_test.rb | 10 | ||||
-rw-r--r-- | activesupport/test/core_ext/object_and_class_ext_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 18 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 77 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_with_zone_test.rb | 422 | ||||
-rw-r--r-- | activesupport/test/core_ext/uri_ext_test.rb | 12 |
13 files changed, 379 insertions, 304 deletions
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 93f4482307..b70ec475ad 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -48,6 +48,8 @@ class ArrayExtToParamTests < Test::Unit::TestCase end class ArrayExtToSentenceTests < Test::Unit::TestCase + include ActiveSupport::Testing::Deprecation + def test_plain_array_to_sentence assert_equal "", [].to_sentence assert_equal "one", ['one'].to_sentence @@ -56,12 +58,28 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase end def test_to_sentence_with_words_connector + assert_deprecated(":connector has been deprecated. Use :words_connector instead") do + assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '') + end + + assert_deprecated(":connector has been deprecated. Use :words_connector instead") do + assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ') + end + assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ') assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ') assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil) end def test_to_sentence_with_last_word_connector + assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do + assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => true) + end + + assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do + assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false) + end + assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ') assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil) assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ') @@ -302,3 +320,37 @@ class ArrayExtRandomTests < Test::Unit::TestCase assert_equal 2, [1, 2, 3].rand end end + +class ArrayWrapperTests < Test::Unit::TestCase + class FakeCollection + def to_ary + ["foo", "bar"] + end + end + + def test_array + ary = %w(foo bar) + assert_same ary, Array.wrap(ary) + end + + def test_nil + assert_equal [], Array.wrap(nil) + end + + def test_object + o = Object.new + assert_equal [o], Array.wrap(o) + end + + def test_string + assert_equal ["foo"], Array.wrap("foo") + end + + def test_string_with_newline + assert_equal ["foo\nbar"], Array.wrap("foo\nbar") + end + + def test_object_with_to_ary + assert_equal ["foo", "bar"], Array.wrap(FakeCollection.new) + end +end diff --git a/activesupport/test/core_ext/class_test.rb b/activesupport/test/core_ext/class_test.rb index 9c6071d478..48d8a78acf 100644 --- a/activesupport/test/core_ext/class_test.rb +++ b/activesupport/test/core_ext/class_test.rb @@ -19,19 +19,19 @@ class ClassTest < Test::Unit::TestCase def test_removing_class_in_root_namespace assert A.is_a?(Class) Class.remove_class(A) - assert_raises(NameError) { A.is_a?(Class) } + assert_raise(NameError) { A.is_a?(Class) } end def test_removing_class_in_one_level_namespace assert X::B.is_a?(Class) Class.remove_class(X::B) - assert_raises(NameError) { X::B.is_a?(Class) } + assert_raise(NameError) { X::B.is_a?(Class) } end def test_removing_class_in_two_level_namespace assert Y::Z::C.is_a?(Class) Class.remove_class(Y::Z::C) - assert_raises(NameError) { Y::Z::C.is_a?(Class) } + assert_raise(NameError) { Y::Z::C.is_a?(Class) } end def test_retrieving_subclasses diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 2cedf65153..1001868c77 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -240,13 +240,11 @@ class DateExtCalculationsTest < Test::Unit::TestCase end def test_current_returns_time_zone_today_when_zone_default_set - silence_warnings do # silence warnings raised by tzinfo gem - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] - with_env_tz 'US/Central' do - Time.stubs(:now).returns Time.local(1999, 12, 31, 23) - assert_equal Date.new(1999, 12, 31), Date.today - assert_equal Date.new(2000, 1, 1), Date.current - end + Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + with_env_tz 'US/Central' do + Time.stubs(:now).returns Time.local(1999, 12, 31, 23) + assert_equal Date.new(1999, 12, 31), Date.today + assert_equal Date.new(2000, 1, 1), Date.current end ensure Time.zone_default = nil diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 3948006b42..8954295d10 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -94,23 +94,33 @@ class DurationTest < ActiveSupport::TestCase end def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set - silence_warnings do # silence warnings raised by tzinfo gem - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] - with_env_tz 'US/Eastern' do - Time.stubs(:now).returns Time.local(2000) - # since - assert_equal true, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone) - assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time - assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name - # ago - assert_equal true, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone) - assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time - assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name - end + Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + with_env_tz 'US/Eastern' do + Time.stubs(:now).returns Time.local(2000) + # since + assert_equal true, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time + assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name + # ago + assert_equal true, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time + assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name end ensure Time.zone_default = nil end + + def test_adding_hours_across_dst_boundary + with_env_tz 'CET' do + assert_equal Time.local(2009,3,29,0,0,0) + 24.hours, Time.local(2009,3,30,1,0,0) + end + end + + def test_adding_day_across_dst_boundary + with_env_tz 'CET' do + assert_equal Time.local(2009,3,29,0,0,0) + 1.day, Time.local(2009,3,30,0,0,0) + end + end protected def with_env_tz(new_tz = 'US/Eastern') diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index b63ab30965..0edac72fe7 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -174,6 +174,13 @@ class HashExtTest < Test::Unit::TestCase assert_equal 2, hash['b'] end + def test_indifferent_reverse_merging + hash = HashWithIndifferentAccess.new('some' => 'value', 'other' => 'value') + hash.reverse_merge!(:some => 'noclobber', :another => 'clobber') + assert_equal 'value', hash[:some] + assert_equal 'clobber', hash[:another] + end + def test_indifferent_deleting get_hash = proc{ { :a => 'foo' }.with_indifferent_access } hash = get_hash.call @@ -234,7 +241,7 @@ class HashExtTest < Test::Unit::TestCase { :failure => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny) end - assert_raises(ArgumentError, "Unknown key(s): failore") do + assert_raise(ArgumentError, "Unknown key(s): failore") do { :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ]) { :failore => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny) end @@ -490,6 +497,15 @@ class HashToXmlTest < Test::Unit::TestCase assert xml.include?(%(<addresses type="array"><address><streets type="array"><street><name>)) end + def test_timezoned_attributes + xml = { + :created_at => Time.utc(1999,2,2), + :local_created_at => Time.utc(1999,2,2).in_time_zone('Eastern Time (US & Canada)') + }.to_xml(@xml_options) + assert_match %r{<created-at type=\"datetime\">1999-02-02T00:00:00Z</created-at>}, xml + assert_match %r{<local-created-at type=\"datetime\">1999-02-01T19:00:00-05:00</local-created-at>}, xml + end + def test_single_record_from_xml topic_xml = <<-EOT <topic> @@ -884,7 +900,13 @@ class QueryTest < Test::Unit::TestCase end def test_expansion_count_is_limited - assert_raises RuntimeError do + expected = { + 'ActiveSupport::XmlMini_REXML' => 'RuntimeError', + 'ActiveSupport::XmlMini_Nokogiri' => 'Nokogiri::XML::SyntaxError', + 'ActiveSupport::XmlMini_LibXML' => 'LibXML::XML::Error', + }[ActiveSupport::XmlMini.backend.name].constantize + + assert_raise expected do attack_xml = <<-EOT <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE member [ diff --git a/activesupport/test/core_ext/load_error_test.rb b/activesupport/test/core_ext/load_error_test.rb index 5bb5b4d1b8..cfa8f978af 100644 --- a/activesupport/test/core_ext/load_error_test.rb +++ b/activesupport/test/core_ext/load_error_test.rb @@ -2,10 +2,10 @@ require 'abstract_unit' class TestMissingSourceFile < Test::Unit::TestCase def test_with_require - assert_raises(MissingSourceFile) { require 'no_this_file_don\'t_exist' } + assert_raise(MissingSourceFile) { require 'no_this_file_don\'t_exist' } end def test_with_load - assert_raises(MissingSourceFile) { load 'nor_does_this_one' } + assert_raise(MissingSourceFile) { load 'nor_does_this_one' } end def test_path begin load 'nor/this/one.rb' diff --git a/activesupport/test/core_ext/module/synchronization_test.rb b/activesupport/test/core_ext/module/synchronization_test.rb index b1d4bc5e06..c28bc9b073 100644 --- a/activesupport/test/core_ext/module/synchronization_test.rb +++ b/activesupport/test/core_ext/module/synchronization_test.rb @@ -28,14 +28,14 @@ class SynchronizationTest < Test::Unit::TestCase end def test_synchronize_with_no_mutex_raises_an_argument_error - assert_raises(ArgumentError) do + assert_raise(ArgumentError) do @target.synchronize :to_s end end def test_double_synchronize_raises_an_argument_error @target.synchronize :to_s, :with => :mutex - assert_raises(ArgumentError) do + assert_raise(ArgumentError) do @target.synchronize :to_s, :with => :mutex end end diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index a5d98507ba..0d3d10f333 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -92,8 +92,8 @@ class ModuleTest < Test::Unit::TestCase end def test_missing_delegation_target - assert_raises(ArgumentError) { eval($nowhere) } - assert_raises(ArgumentError) { eval($noplace) } + assert_raise(ArgumentError) { eval($nowhere) } + assert_raise(ArgumentError) { eval($noplace) } end def test_delegation_prefix @@ -141,7 +141,7 @@ class ModuleTest < Test::Unit::TestCase def test_delegation_without_allow_nil_and_nil_value david = Someone.new("David") - assert_raises(NoMethodError) { david.street } + assert_raise(NoMethodError) { david.street } end def test_parent @@ -314,7 +314,7 @@ class MethodAliasingTest < Test::Unit::TestCase alias_method_chain :duck, :orange end - assert_raises NoMethodError do + assert_raise NoMethodError do @instance.duck end @@ -330,7 +330,7 @@ class MethodAliasingTest < Test::Unit::TestCase alias_method_chain :duck, :orange end - assert_raises NoMethodError do + assert_raise NoMethodError do @instance.duck end diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index 0bdbd14f33..b6515e05a0 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -109,7 +109,7 @@ end class ObjectTests < Test::Unit::TestCase def test_suppress_re_raises - assert_raises(LoadError) { suppress(ArgumentError) {raise LoadError} } + assert_raise(LoadError) { suppress(ArgumentError) {raise LoadError} } end def test_suppress_supresses suppress(ArgumentError) { raise ArgumentError } @@ -256,7 +256,7 @@ class ObjectTryTest < Test::Unit::TestCase def test_nonexisting_method method = :undefined_method assert !@string.respond_to?(method) - assert_raises(NoMethodError) { @string.try(method) } + assert_raise(NoMethodError) { @string.try(method) } end def test_valid_method diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index e232bf8384..6c9b7e7236 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -77,6 +77,24 @@ class StringInflectionsTest < Test::Unit::TestCase end end + def test_string_parameterized_normal + StringToParameterized.each do |normal, slugged| + assert_equal(normal.parameterize, slugged) + end + end + + def test_string_parameterized_no_separator + StringToParameterizeWithNoSeparator.each do |normal, slugged| + assert_equal(normal.parameterize(''), slugged) + end + end + + def test_string_parameterized_underscore + StringToParameterizeWithUnderscore.each do |normal, slugged| + assert_equal(normal.parameterize('_'), slugged) + end + end + def test_humanize UnderscoreToHuman.each do |underscore, human| assert_equal(human, underscore.humanize) diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 52d6c18dce..8ee4904036 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -183,26 +183,46 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_backward_start with_env_tz 'US/Eastern' do # dt: US: 2005 April 3rd 4:18am - assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-1.day=>st' - assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-1.day=>st' + assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(24.hours), 'dt-24.hours=>st' + assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-86400=>st' + assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(86400.seconds), 'dt-86400.seconds=>st' + + assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(24.hours), 'st-24.hours=>st' + assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-86400=>st' + assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400.seconds), 'st-86400.seconds=>st' end with_env_tz 'NZ' do # dt: New Zealand: 2006 October 1st 4:18am - assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-1.day=>st' - assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-1.day=>st' + assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(24.hours), 'dt-24.hours=>st' + assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-86400=>st' + assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400.seconds), 'dt-86400.seconds=>st' + + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(24.hours), 'st-24.hours=>st' + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-86400=>st' + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400.seconds), 'st-86400.seconds=>st' end end def test_daylight_savings_time_crossings_backward_end with_env_tz 'US/Eastern' do # st: US: 2005 October 30th 4:03am - assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-1.day=>dt' - assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-1.day=>dt' + assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(24.hours), 'st-24.hours=>dt' + assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-86400=>dt' + assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(86400.seconds), 'st-86400.seconds=>dt' + + assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(24.hours), 'dt-24.hours=>dt' + assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-86400=>dt' + assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400.seconds), 'dt-86400.seconds=>dt' end with_env_tz 'NZ' do # st: New Zealand: 2006 March 19th 4:03am - assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(86400), 'st-1.day=>dt' - assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400), 'dt-1.day=>dt' + assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(24.hours), 'st-24.hours=>dt' + assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(86400), 'st-86400=>dt' + assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(86400.seconds), 'st-86400.seconds=>dt' + + assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(24.hours), 'dt-24.hours=>dt' + assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400), 'dt-86400=>dt' + assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400.seconds), 'dt-86400.seconds=>dt' end end @@ -231,6 +251,7 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(1.day), 'dt-1.day=>dt' end end + def test_since assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1) assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600) @@ -243,13 +264,23 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_forward_start with_env_tz 'US/Eastern' do # st: US: 2005 April 2nd 7:27pm - assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+1.day=>dt' - assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+1.day=>dt' + assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(24.hours), 'st+24.hours=>dt' + assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+86400=>dt' + assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(86400.seconds), 'st+86400.seconds=>dt' + + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(24.hours), 'dt+24.hours=>dt' + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+86400=>dt' + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400.seconds), 'dt+86400.seconds=>dt' end with_env_tz 'NZ' do # st: New Zealand: 2006 September 30th 7:27pm - assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(86400), 'st+1.day=>dt' - assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400), 'dt+1.day=>dt' + assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(24.hours), 'st+24.hours=>dt' + assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(86400), 'st+86400=>dt' + assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(86400.seconds), 'st+86400.seconds=>dt' + + assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(24.hours), 'dt+24.hours=>dt' + assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400), 'dt+86400=>dt' + assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400.seconds), 'dt+86400.seconds=>dt' end end @@ -295,13 +326,23 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_forward_end with_env_tz 'US/Eastern' do # dt: US: 2005 October 30th 12:45am - assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(86400), 'dt+1.day=>st' - assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400), 'st+1.day=>st' + assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(24.hours), 'dt+24.hours=>st' + assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(86400), 'dt+86400=>st' + assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(86400.seconds), 'dt+86400.seconds=>st' + + assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(24.hours), 'st+24.hours=>st' + assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400), 'st+86400=>st' + assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400.seconds), 'st+86400.seconds=>st' end with_env_tz 'NZ' do # dt: New Zealand: 2006 March 19th 1:45am - assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(86400), 'dt+1.day=>st' - assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400), 'st+1.day=>st' + assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(24.hours), 'dt+24.hours=>st' + assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(86400), 'dt+86400=>st' + assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(86400.seconds), 'dt+86400.seconds=>st' + + assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(24.hours), 'st+24.hours=>st' + assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400), 'st+86400=>st' + assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400.seconds), 'st+86400.seconds=>st' end end @@ -461,6 +502,10 @@ class TimeExtCalculationsTest < Test::Unit::TestCase with_env_tz "UTC" do assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822) end + with_env_tz "US/Central" do + assert_equal "Thu, 05 Feb 2009 14:30:05 -0600", Time.local(2009, 2, 5, 14, 30, 5).to_s(:rfc822) + assert_equal "Mon, 09 Jun 2008 04:05:01 -0500", Time.local(2008, 6, 9, 4, 5, 1).to_s(:rfc822) + end end def test_custom_date_format diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 4dc1fec559..7be19e7900 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -13,9 +13,7 @@ class TimeWithZoneTest < Test::Unit::TestCase end def test_time - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal Time.utc(1999, 12, 31, 19), @twz.time - end + assert_equal Time.utc(1999, 12, 31, 19), @twz.time end def test_time_zone @@ -42,30 +40,22 @@ class TimeWithZoneTest < Test::Unit::TestCase end def test_formatted_offset - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal '-05:00', @twz.formatted_offset - assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst - end + assert_equal '-05:00', @twz.formatted_offset + assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst end def test_dst? - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal false, @twz.dst? - assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).dst? - end + assert_equal false, @twz.dst? + assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).dst? end def test_zone - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal 'EST', @twz.zone - assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst - end + assert_equal 'EST', @twz.zone + assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst end def test_to_json - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json - end + assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json end def test_to_json_with_use_standard_json_time_format_config_set_to_true @@ -76,66 +66,50 @@ class TimeWithZoneTest < Test::Unit::TestCase end def test_strftime - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') - end + assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') end def test_inspect - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect - end + assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect end def test_to_s - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s - end + assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s + end + + def test_to_formatted_s + assert_equal '1999-12-31 19:00:00 -0500', @twz.to_formatted_s end def test_to_s_db - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal '2000-01-01 00:00:00', @twz.to_s(:db) - end + assert_equal '2000-01-01 00:00:00', @twz.to_s(:db) end def test_xmlschema - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema - end + assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema end def test_xmlschema_with_fractional_seconds - silence_warnings do # silence warnings raised by tzinfo gem - @twz += 0.123456 # advance the time by a fraction of a second - assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3) - assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6) - assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12) - end + @twz += 0.123456 # advance the time by a fraction of a second + assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3) + assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6) + assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12) end def test_to_yaml - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml - end + assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml end def test_ruby_to_yaml - silence_warnings do - assert_equal "--- \n:twz: 2000-01-01 00:00:00 Z\n", {:twz => @twz}.to_yaml - end + assert_equal "--- \n:twz: 2000-01-01 00:00:00 Z\n", {:twz => @twz}.to_yaml end def test_httpdate - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate - end + assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate end def test_rfc2822 - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 - end + assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 end def test_compare_with_time @@ -209,50 +183,36 @@ class TimeWithZoneTest < Test::Unit::TestCase end def test_plus_with_integer - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time - end + assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time end def test_plus_with_integer_when_self_wraps_datetime - silence_warnings do # silence warnings raised by tzinfo gem - datetime = DateTime.civil(2000, 1, 1, 0) - twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) - assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time - end + datetime = DateTime.civil(2000, 1, 1, 0) + twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) + assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time end def test_plus_when_crossing_time_class_limit - silence_warnings do # silence warnings raised by tzinfo gem - twz = ActiveSupport::TimeWithZone.new(Time.utc(2038, 1, 19), @time_zone) - assert_equal [0, 0, 19, 19, 1, 2038], (twz + 86_400).to_a[0,6] - end + twz = ActiveSupport::TimeWithZone.new(Time.utc(2038, 1, 19), @time_zone) + assert_equal [0, 0, 19, 19, 1, 2038], (twz + 86_400).to_a[0,6] end def test_plus_with_duration - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time - end + assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time end def test_minus_with_integer - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time - end + assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time end def test_minus_with_integer_when_self_wraps_datetime - silence_warnings do # silence warnings raised by tzinfo gem - datetime = DateTime.civil(2000, 1, 1, 0) - twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) - assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time - end + datetime = DateTime.civil(2000, 1, 1, 0) + twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) + assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time end def test_minus_with_duration - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time - end + assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time end def test_minus_with_time @@ -265,56 +225,50 @@ class TimeWithZoneTest < Test::Unit::TestCase twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) assert_equal 86_400.0, twz2 - twz1 end - + def test_minus_with_datetime assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1) end - + def test_minus_with_wrapped_datetime assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1) assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1) end def test_plus_and_minus_enforce_spring_dst_rules - silence_warnings do # silence warnings raised by tzinfo gem - utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start - twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) - assert_equal Time.utc(2006,4,2,1,59,59), twz.time - assert_equal false, twz.dst? - assert_equal 'EST', twz.zone - twz = twz + 1 - assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT - assert_equal true, twz.dst? - assert_equal 'EDT', twz.zone - twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST - assert_equal Time.utc(2006,4,2,1,59,59), twz.time - assert_equal false, twz.dst? - assert_equal 'EST', twz.zone - end + utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start + twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) + assert_equal Time.utc(2006,4,2,1,59,59), twz.time + assert_equal false, twz.dst? + assert_equal 'EST', twz.zone + twz = twz + 1 + assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT + assert_equal true, twz.dst? + assert_equal 'EDT', twz.zone + twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST + assert_equal Time.utc(2006,4,2,1,59,59), twz.time + assert_equal false, twz.dst? + assert_equal 'EST', twz.zone end def test_plus_and_minus_enforce_fall_dst_rules - silence_warnings do # silence warnings raised by tzinfo gem - utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end - twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) - assert_equal Time.utc(2006,10,29,1,59,59), twz.time - assert_equal true, twz.dst? - assert_equal 'EDT', twz.zone - twz = twz + 1 - assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST - assert_equal false, twz.dst? - assert_equal 'EST', twz.zone - twz = twz - 1 - assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT - assert_equal true, twz.dst? - assert_equal 'EDT', twz.zone - end + utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end + twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) + assert_equal Time.utc(2006,10,29,1,59,59), twz.time + assert_equal true, twz.dst? + assert_equal 'EDT', twz.zone + twz = twz + 1 + assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST + assert_equal false, twz.dst? + assert_equal 'EST', twz.zone + twz = twz - 1 + assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT + assert_equal true, twz.dst? + assert_equal 'EDT', twz.zone end def test_to_a - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), ActiveSupport::TimeZone['Hawaii'] ).to_a - end + assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), ActiveSupport::TimeZone['Hawaii'] ).to_a end def test_to_f @@ -334,22 +288,18 @@ class TimeWithZoneTest < Test::Unit::TestCase end def test_to_date - silence_warnings do # silence warnings raised by tzinfo gem - # 1 sec before midnight Jan 1 EST - assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date - # midnight Jan 1 EST - assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date - # 1 sec before midnight Jan 2 EST - assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date - # midnight Jan 2 EST - assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date - end + # 1 sec before midnight Jan 1 EST + assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date + # midnight Jan 1 EST + assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date + # 1 sec before midnight Jan 2 EST + assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date + # midnight Jan 2 EST + assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date end def test_to_datetime - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime - end + assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime end def test_acts_like_time @@ -367,46 +317,44 @@ class TimeWithZoneTest < Test::Unit::TestCase assert @twz.kind_of?(Time) assert @twz.is_a?(ActiveSupport::TimeWithZone) end + + def test_class_name + assert_equal 'Time', ActiveSupport::TimeWithZone.name + end def test_method_missing_with_time_return_value - silence_warnings do # silence warnings raised by tzinfo gem - assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1) - assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time - end + assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1) + assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time end def test_marshal_dump_and_load - silence_warnings do # silence warnings raised by tzinfo gem - marshal_str = Marshal.dump(@twz) - mtime = Marshal.load(marshal_str) - assert_equal Time.utc(2000, 1, 1, 0), mtime.utc - assert mtime.utc.utc? - assert_equal ActiveSupport::TimeZone['Eastern Time (US & Canada)'], mtime.time_zone - assert_equal Time.utc(1999, 12, 31, 19), mtime.time - assert mtime.time.utc? - assert_equal @twz.inspect, mtime.inspect - end + marshal_str = Marshal.dump(@twz) + mtime = Marshal.load(marshal_str) + assert_equal Time.utc(2000, 1, 1, 0), mtime.utc + assert mtime.utc.utc? + assert_equal ActiveSupport::TimeZone['Eastern Time (US & Canada)'], mtime.time_zone + assert_equal Time.utc(1999, 12, 31, 19), mtime.time + assert mtime.time.utc? + assert_equal @twz.inspect, mtime.inspect end def test_marshal_dump_and_load_with_tzinfo_identifier - silence_warnings do # silence warnings raised by tzinfo gem - twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York')) - marshal_str = Marshal.dump(twz) - mtime = Marshal.load(marshal_str) - assert_equal Time.utc(2000, 1, 1, 0), mtime.utc - assert mtime.utc.utc? - assert_equal 'America/New_York', mtime.time_zone.name - assert_equal Time.utc(1999, 12, 31, 19), mtime.time - assert mtime.time.utc? - assert_equal @twz.inspect, mtime.inspect - end + twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York')) + marshal_str = Marshal.dump(twz) + mtime = Marshal.load(marshal_str) + assert_equal Time.utc(2000, 1, 1, 0), mtime.utc + assert mtime.utc.utc? + assert_equal 'America/New_York', mtime.time_zone.name + assert_equal Time.utc(1999, 12, 31, 19), mtime.time + assert mtime.time.utc? + assert_equal @twz.inspect, mtime.inspect end - + def test_freeze @twz.freeze assert @twz.frozen? end - + def test_freeze_preloads_instance_variables @twz.freeze assert_nothing_raised do @@ -416,66 +364,52 @@ class TimeWithZoneTest < Test::Unit::TestCase end def test_method_missing_with_non_time_return_value - silence_warnings do # silence warnings raised by tzinfo gem - @twz.time.expects(:foo).returns('bar') - assert_equal 'bar', @twz.foo - end + @twz.time.expects(:foo).returns('bar') + assert_equal 'bar', @twz.foo end def test_date_part_value_methods - silence_warnings do # silence warnings raised by tzinfo gem - twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) - twz.expects(:method_missing).never - assert_equal 1999, twz.year - assert_equal 12, twz.month - assert_equal 31, twz.day - assert_equal 14, twz.hour - assert_equal 18, twz.min - assert_equal 17, twz.sec - assert_equal 500, twz.usec - assert_equal 5, twz.wday - assert_equal 365, twz.yday - end + twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) + twz.expects(:method_missing).never + assert_equal 1999, twz.year + assert_equal 12, twz.month + assert_equal 31, twz.day + assert_equal 14, twz.hour + assert_equal 18, twz.min + assert_equal 17, twz.sec + assert_equal 500, twz.usec + assert_equal 5, twz.wday + assert_equal 365, twz.yday end def test_usec_returns_0_when_datetime_is_wrapped - silence_warnings do # silence warnings raised by tzinfo gem - twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone) - assert_equal 0, twz.usec - end + twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone) + assert_equal 0, twz.usec end def test_utc_to_local_conversion_saves_period_in_instance_variable - silence_warnings do # silence warnings raised by tzinfo gem - assert_nil @twz.instance_variable_get('@period') - @twz.time - assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period') - end + assert_nil @twz.instance_variable_get('@period') + @twz.time + assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period') end def test_instance_created_with_local_time_returns_correct_utc_time - silence_warnings do # silence warnings raised by tzinfo gem - twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19)) - assert_equal Time.utc(2000), twz.utc - end + twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19)) + assert_equal Time.utc(2000), twz.utc end def test_instance_created_with_local_time_enforces_spring_dst_rules - silence_warnings do # silence warnings raised by tzinfo gem - twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST - assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM - assert_equal true, twz.dst? - assert_equal 'EDT', twz.zone - end + twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST + assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM + assert_equal true, twz.dst? + assert_equal 'EDT', twz.zone end def test_instance_created_with_local_time_enforces_fall_dst_rules - silence_warnings do # silence warnings raised by tzinfo gem - twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST - assert_equal Time.utc(2006,10,29,1), twz.time - assert_equal true, twz.dst? - assert_equal 'EDT', twz.zone - end + twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST + assert_equal Time.utc(2006,10,29,1), twz.time + assert_equal true, twz.dst? + assert_equal 'EDT', twz.zone end def test_ruby_19_weekday_name_query_methods @@ -486,15 +420,11 @@ class TimeWithZoneTest < Test::Unit::TestCase end def test_utc_to_local_conversion_with_far_future_datetime - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6] - end + assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6] end def test_local_to_utc_conversion_with_far_future_datetime - silence_warnings do # silence warnings raised by tzinfo gem - assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f - end + assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f end def test_change @@ -782,42 +712,36 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end def test_in_time_zone - silence_warnings do # silence warnings raised by tzinfo gem - Time.use_zone 'Alaska' do - assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect - assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect - end - Time.use_zone 'Hawaii' do - assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect - assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect - end - Time.use_zone nil do - assert_equal @t, @t.in_time_zone - assert_equal @dt, @dt.in_time_zone - end + Time.use_zone 'Alaska' do + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect + end + Time.use_zone 'Hawaii' do + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect + end + Time.use_zone nil do + assert_equal @t, @t.in_time_zone + assert_equal @dt, @dt.in_time_zone end end def test_in_time_zone_with_argument - silence_warnings do # silence warnings raised by tzinfo gem - Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) - assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect - assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect - assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect - assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect - assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect - assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect - assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone(-9.hours).inspect - end + Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect + assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect + assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone(-9.hours).inspect end end def test_in_time_zone_with_time_local_instance - silence_warnings do # silence warnings raised by tzinfo gem - with_env_tz 'US/Eastern' do - time = Time.local(1999, 12, 31, 19) # == Time.utc(2000) - assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', time.in_time_zone('Alaska').inspect - end + with_env_tz 'US/Eastern' do + time = Time.local(1999, 12, 31, 19) # == Time.utc(2000) + assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', time.in_time_zone('Alaska').inspect end end @@ -831,7 +755,7 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase def test_use_zone_with_exception_raised Time.zone = 'Alaska' - assert_raises RuntimeError do + assert_raise RuntimeError do Time.use_zone('Hawaii') { raise RuntimeError } end assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone @@ -872,24 +796,20 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end def test_time_zone_setter_with_tzinfo_timezone_object_wraps_in_rails_time_zone - silence_warnings do # silence warnings raised by tzinfo gem - tzinfo = TZInfo::Timezone.get('America/New_York') - Time.zone = tzinfo - assert_kind_of ActiveSupport::TimeZone, Time.zone - assert_equal tzinfo, Time.zone.tzinfo - assert_equal 'America/New_York', Time.zone.name - assert_equal(-18_000, Time.zone.utc_offset) - end + tzinfo = TZInfo::Timezone.get('America/New_York') + Time.zone = tzinfo + assert_kind_of ActiveSupport::TimeZone, Time.zone + assert_equal tzinfo, Time.zone.tzinfo + assert_equal 'America/New_York', Time.zone.name + assert_equal(-18_000, Time.zone.utc_offset) end def test_time_zone_setter_with_tzinfo_timezone_identifier_does_lookup_and_wraps_in_rails_time_zone - silence_warnings do # silence warnings raised by tzinfo gem - Time.zone = 'America/New_York' - assert_kind_of ActiveSupport::TimeZone, Time.zone - assert_equal 'America/New_York', Time.zone.tzinfo.name - assert_equal 'America/New_York', Time.zone.name - assert_equal(-18_000, Time.zone.utc_offset) - end + Time.zone = 'America/New_York' + assert_kind_of ActiveSupport::TimeZone, Time.zone + assert_equal 'America/New_York', Time.zone.tzinfo.name + assert_equal 'America/New_York', Time.zone.name + assert_equal(-18_000, Time.zone.utc_offset) end def test_time_zone_setter_with_non_identifying_argument_returns_nil @@ -908,14 +828,12 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end def test_current_returns_time_zone_now_when_zone_default_set - silence_warnings do # silence warnings raised by tzinfo gem - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] - with_env_tz 'US/Eastern' do - Time.stubs(:now).returns Time.local(2000) - assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone) - assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name - assert_equal Time.utc(2000), Time.current.time - end + Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + with_env_tz 'US/Eastern' do + Time.stubs(:now).returns Time.local(2000) + assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone) + assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name + assert_equal Time.utc(2000), Time.current.time end ensure Time.zone_default = nil diff --git a/activesupport/test/core_ext/uri_ext_test.rb b/activesupport/test/core_ext/uri_ext_test.rb new file mode 100644 index 0000000000..0837d3cb2d --- /dev/null +++ b/activesupport/test/core_ext/uri_ext_test.rb @@ -0,0 +1,12 @@ +require 'abstract_unit' +require 'uri' + +class URIExtTest < Test::Unit::TestCase + def test_uri_decode_handle_multibyte + str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese. + str.force_encoding(Encoding::UTF_8) if str.respond_to?(:force_encoding) + + assert_equal str, URI.unescape(URI.escape(str)) + assert_equal str, URI.decode(URI.escape(str)) + end +end |