diff options
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/caching_test.rb | 6 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_ext_test.rb | 13 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 20 | ||||
-rw-r--r-- | activesupport/test/core_ext/duration_test.rb | 14 | ||||
-rw-r--r-- | activesupport/test/core_ext/hash_ext_test.rb | 30 | ||||
-rw-r--r-- | activesupport/test/core_ext/numeric_ext_test.rb | 74 | ||||
-rw-r--r-- | activesupport/test/core_ext/range_ext_test.rb | 31 | ||||
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 54 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 13 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_with_zone_test.rb | 16 | ||||
-rw-r--r-- | activesupport/test/dependencies_test.rb | 10 | ||||
-rw-r--r-- | activesupport/test/deprecation_test.rb | 7 | ||||
-rw-r--r-- | activesupport/test/i18n_test.rb | 2 | ||||
-rw-r--r-- | activesupport/test/inflector_test.rb | 2 | ||||
-rw-r--r-- | activesupport/test/message_encryptor_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/notifications/instrumenter_test.rb | 50 | ||||
-rw-r--r-- | activesupport/test/test_test.rb | 36 | ||||
-rw-r--r-- | activesupport/test/testing/performance_test.rb | 68 |
18 files changed, 310 insertions, 140 deletions
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index a2e2c51283..5158bbc196 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -675,7 +675,7 @@ class FileStoreTest < ActiveSupport::TestCase def test_log_exception_when_cache_read_fails File.expects(:exist?).raises(StandardError, "failed") @cache.send(:read_entry, "winston", {}) - assert_present @buffer.string + assert @buffer.string.present? end end @@ -897,12 +897,12 @@ class CacheStoreLoggerTest < ActiveSupport::TestCase def test_logging @cache.fetch('foo') { 'bar' } - assert_present @buffer.string + assert @buffer.string.present? end def test_mute_logging @cache.mute { @cache.fetch('foo') { 'bar' } } - assert_blank @buffer.string + assert @buffer.string.blank? end end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 5e89a6e00b..f3fa96ec6f 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -33,8 +33,12 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end def test_to_time - assert_equal Time.local(2005, 2, 21), Date.new(2005, 2, 21).to_time - assert_equal Time.local(2039, 2, 21), Date.new(2039, 2, 21).to_time + with_env_tz 'US/Eastern' do + assert_equal Time, Date.new(2005, 2, 21).to_time.class + assert_equal Time.local(2005, 2, 21), Date.new(2005, 2, 21).to_time + assert_equal Time.local(2005, 2, 21).utc_offset, Date.new(2005, 2, 21).to_time.utc_offset + end + silence_warnings do 0.upto(138) do |year| [:utc, :local].each do |format| @@ -353,6 +357,11 @@ class DateExtBehaviorTest < ActiveSupport::TestCase Date.today.freeze.freeze end end + + def test_compare_with_infinity + assert_equal(-1, Date.today <=> Float::INFINITY) + assert_equal(1, Date.today <=> -Float::INFINITY) + end end class DateExtConversionsTest < ActiveSupport::TestCase diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index dfad3a90f8..f74b7b9917 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -41,11 +41,14 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase end def test_to_time - assert_equal Time.utc(2005, 2, 21, 10, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time - assert_equal Time.utc(2039, 2, 21, 10, 11, 12), DateTime.new(2039, 2, 21, 10, 11, 12, 0).to_time - # DateTimes with offsets other than 0 are returned unaltered - assert_equal DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)), DateTime.new(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).to_time - # Fractional seconds are preserved + with_env_tz 'US/Eastern' do + assert_equal Time, DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time.class + assert_equal Time.local(2005, 2, 21, 5, 11, 12), DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time + assert_equal Time.local(2005, 2, 21, 5, 11, 12).utc_offset, DateTime.new(2005, 2, 21, 10, 11, 12, 0).to_time.utc_offset + end + end + + def test_to_time_preserves_fractional_seconds assert_equal Time.utc(2005, 2, 21, 10, 11, 12, 256), DateTime.new(2005, 2, 21, 10, 11, 12 + Rational(256, 1000000), 0).to_time end @@ -317,3 +320,10 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') end end + +class DateTimeExtBehaviorTest < ActiveSupport::TestCase + def test_compare_with_infinity + assert_equal(-1, DateTime.now <=> Float::INFINITY) + assert_equal(1, DateTime.now <=> -Float::INFINITY) + end +end diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 2826f51f2d..5e3987265b 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -50,14 +50,12 @@ class DurationTest < ActiveSupport::TestCase end def test_argument_error - begin - 1.second.ago('') - flunk("no exception was raised") - rescue ArgumentError => e - assert_equal 'expected a time or date, got ""', e.message, "ensure ArgumentError is not being raised by dependencies.rb" - rescue Exception => e - flunk("ArgumentError should be raised, but we got #{e.class} instead") - end + 1.second.ago('') + flunk("no exception was raised") + rescue ArgumentError => e + assert_equal 'expected a time or date, got ""', e.message, "ensure ArgumentError is not being raised by dependencies.rb" + rescue Exception => e + flunk("ArgumentError should be raised, but we got #{e.class} instead") end def test_fractional_weeks diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 5fc81ba6fc..30d95b75bc 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -8,7 +8,7 @@ require 'active_support/core_ext/object/deep_dup' require 'active_support/inflections' class HashExtTest < ActiveSupport::TestCase - class IndifferentHash < HashWithIndifferentAccess + class IndifferentHash < ActiveSupport::HashWithIndifferentAccess end class SubclassingArray < Array @@ -1015,12 +1015,10 @@ class HashToXmlTest < ActiveSupport::TestCase <replies-close-in type="integer">2592000000</replies-close-in> <written-on type="date">2003-07-16</written-on> <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at> - <content type="yaml">--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n</content> <author-email-address>david@loudthinking.com</author-email-address> <parent-id></parent-id> <ad-revenue type="decimal">1.5</ad-revenue> <optimum-viewing-angle type="float">135</optimum-viewing-angle> - <resident type="symbol">yes</resident> </topic> EOT @@ -1033,12 +1031,10 @@ class HashToXmlTest < ActiveSupport::TestCase :replies_close_in => 2592000000, :written_on => Date.new(2003, 7, 16), :viewed_at => Time.utc(2003, 7, 16, 9, 28), - :content => { :message => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] }, :author_email_address => "david@loudthinking.com", :parent_id => nil, :ad_revenue => BigDecimal("1.50"), :optimum_viewing_angle => 135.0, - :resident => :yes }.stringify_keys assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["topic"] @@ -1052,7 +1048,6 @@ class HashToXmlTest < ActiveSupport::TestCase <approved type="boolean"></approved> <written-on type="date"></written-on> <viewed-at type="datetime"></viewed-at> - <content type="yaml"></content> <parent-id></parent-id> </topic> EOT @@ -1063,7 +1058,6 @@ class HashToXmlTest < ActiveSupport::TestCase :approved => nil, :written_on => nil, :viewed_at => nil, - :content => nil, :parent_id => nil }.stringify_keys @@ -1290,6 +1284,28 @@ class HashToXmlTest < ActiveSupport::TestCase assert_equal expected_product_hash, Hash.from_xml(product_xml)["product"] end + def test_from_xml_raises_on_disallowed_type_attributes + assert_raise ActiveSupport::XMLConverter::DisallowedType do + Hash.from_xml '<product><name type="foo">value</name></product>', %w(foo) + end + end + + def test_from_xml_disallows_symbol_and_yaml_types_by_default + assert_raise ActiveSupport::XMLConverter::DisallowedType do + Hash.from_xml '<product><name type="symbol">value</name></product>' + end + + assert_raise ActiveSupport::XMLConverter::DisallowedType do + Hash.from_xml '<product><name type="yaml">value</name></product>' + end + end + + def test_from_trusted_xml_allows_symbol_and_yaml_types + expected = { 'product' => { 'name' => :value }} + assert_equal expected, Hash.from_trusted_xml('<product><name type="symbol">value</name></product>') + assert_equal expected, Hash.from_trusted_xml('<product><name type="yaml">:value</name></product>') + end + def test_should_use_default_value_for_unknown_key hash_wia = HashWithIndifferentAccess.new(3) assert_equal 3, hash_wia[:new_key] diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index 435f4aa5a1..8c7d00cae1 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -203,7 +203,7 @@ class NumericExtFormattingTest < ActiveSupport::TestCase def terabytes(number) gigabytes(number) * 1024 end - + def test_to_s__phone assert_equal("555-1234", 5551234.to_s(:phone)) assert_equal("800-555-1212", 8005551212.to_s(:phone)) @@ -217,7 +217,7 @@ class NumericExtFormattingTest < ActiveSupport::TestCase assert_equal("22-555-1212", 225551212.to_s(:phone)) assert_equal("+45-22-555-1212", 225551212.to_s(:phone, :country_code => 45)) end - + def test_to_s__currency assert_equal("$1,234,567,890.50", 1234567890.50.to_s(:currency)) assert_equal("$1,234,567,890.51", 1234567890.506.to_s(:currency)) @@ -228,8 +228,8 @@ class NumericExtFormattingTest < ActiveSupport::TestCase assert_equal("$1,234,567,890.5", 1234567890.50.to_s(:currency, :precision => 1)) assert_equal("£1234567890,50", 1234567890.50.to_s(:currency, :unit => "£", :separator => ",", :delimiter => "")) end - - + + def test_to_s__rounded assert_equal("-111.235", -111.2346.to_s(:rounded)) assert_equal("111.235", 111.2346.to_s(:rounded)) @@ -246,7 +246,7 @@ class NumericExtFormattingTest < ActiveSupport::TestCase assert_equal("11.00", 10.995.to_s(:rounded, :precision => 2)) assert_equal("0.00", -0.001.to_s(:rounded, :precision => 2)) end - + def test_to_s__percentage assert_equal("100.000%", 100.to_s(:percentage)) assert_equal("100%", 100.to_s(:percentage, :precision => 0)) @@ -274,7 +274,7 @@ class NumericExtFormattingTest < ActiveSupport::TestCase assert_equal '12.345.678,05', 12345678.05.to_s(:delimited, :separator => ',', :delimiter => '.') assert_equal '12.345.678,05', 12345678.05.to_s(:delimited, :delimiter => '.', :separator => ',') end - + def test_to_s__rounded_with_custom_delimiter_and_separator assert_equal '31,83', 31.825.to_s(:rounded, :precision => 2, :separator => ',') @@ -350,7 +350,7 @@ class NumericExtFormattingTest < ActiveSupport::TestCase assert_equal '1.23 GB', 1234567890.to_s(:human_size, :prefix => :si) assert_equal '1.23 TB', 1234567890123.to_s(:human_size, :prefix => :si) end - + def test_to_s__human_size_with_options_hash assert_equal '1.2 MB', 1234567.to_s(:human_size, :precision => 2) assert_equal '3 Bytes', 3.14159265.to_s(:human_size, :precision => 4) @@ -366,13 +366,13 @@ class NumericExtFormattingTest < ActiveSupport::TestCase assert_equal '1.012 KB', kilobytes(1.0123).to_s(:human_size, :precision => 3, :significant => false) assert_equal '1 KB', kilobytes(1.0123).to_s(:human_size, :precision => 0, :significant => true) #ignores significant it precision is 0 end - + def test_to_s__human_size_with_custom_delimiter_and_separator assert_equal '1,01 KB', kilobytes(1.0123).to_s(:human_size, :precision => 3, :separator => ',') assert_equal '1,01 KB', kilobytes(1.0100).to_s(:human_size, :precision => 4, :separator => ',') assert_equal '1.000,1 TB', terabytes(1000.1).to_s(:human_size, :precision => 5, :delimiter => '.', :separator => ',') end - + def test_number_to_human assert_equal '-123', -123.to_s(:human) assert_equal '-0.5', -0.5.to_s(:human) @@ -436,7 +436,7 @@ class NumericExtFormattingTest < ActiveSupport::TestCase def test_to_s__injected_on_proper_types assert_equal Fixnum, 1230.class assert_equal '1.23 Thousand', 1230.to_s(:human) - + assert_equal Float, Float(1230).class assert_equal '1.23 Thousand', Float(1230).to_s(:human) @@ -447,3 +447,57 @@ class NumericExtFormattingTest < ActiveSupport::TestCase assert_equal '1 Million', BigDecimal("1000010").to_s(:human) end end + +class NumericExtBehaviorTest < ActiveSupport::TestCase + def setup + @inf = BigDecimal.new('Infinity') + end + + def test_compare_infinity_with_date + assert_equal(-1, -Float::INFINITY <=> Date.today) + assert_equal(1, Float::INFINITY <=> Date.today) + assert_equal(-1, -@inf <=> Date.today) + assert_equal(1, @inf <=> Date.today) + end + + def test_compare_infinty_with_infinty + assert_equal(-1, -Float::INFINITY <=> Float::INFINITY) + assert_equal(1, Float::INFINITY <=> -Float::INFINITY) + assert_equal(0, Float::INFINITY <=> Float::INFINITY) + assert_equal(0, -Float::INFINITY <=> -Float::INFINITY) + + assert_equal(-1, -Float::INFINITY <=> BigDecimal::INFINITY) + assert_equal(1, Float::INFINITY <=> -BigDecimal::INFINITY) + assert_equal(0, Float::INFINITY <=> BigDecimal::INFINITY) + assert_equal(0, -Float::INFINITY <=> -BigDecimal::INFINITY) + + assert_equal(-1, -BigDecimal::INFINITY <=> Float::INFINITY) + assert_equal(1, BigDecimal::INFINITY <=> -Float::INFINITY) + assert_equal(0, BigDecimal::INFINITY <=> Float::INFINITY) + assert_equal(0, -BigDecimal::INFINITY <=> -Float::INFINITY) + end + + def test_compare_infinity_with_time + assert_equal(-1, -Float::INFINITY <=> Time.now) + assert_equal(1, Float::INFINITY <=> Time.now) + assert_equal(-1, -@inf <=> Time.now) + assert_equal(1, @inf <=> Time.now) + end + + def test_compare_infinity_with_datetime + assert_equal(-1, -Float::INFINITY <=> DateTime.now) + assert_equal(1, Float::INFINITY <=> DateTime.now) + assert_equal(-1, -@inf <=> DateTime.now) + assert_equal(1, @inf <=> DateTime.now) + end + + def test_compare_infinity_with_twz + time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + twz = ActiveSupport::TimeWithZone.new(Time.now, time_zone) + + assert_equal(-1, -Float::INFINITY <=> twz) + assert_equal(1, Float::INFINITY <=> twz) + assert_equal(-1, -@inf <=> twz) + assert_equal(1, @inf <=> twz) + end +end diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb index f0cdc0bfd4..6e94d5e10d 100644 --- a/activesupport/test/core_ext/range_ext_test.rb +++ b/activesupport/test/core_ext/range_ext_test.rb @@ -1,6 +1,7 @@ require 'abstract_unit' require 'active_support/time' require 'active_support/core_ext/range' +require 'active_support/core_ext/numeric' class RangeTest < ActiveSupport::TestCase def test_to_s_from_dates @@ -12,6 +13,12 @@ class RangeTest < ActiveSupport::TestCase date_range = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30) assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_s(:db) end + + def test_date_range + assert_instance_of Range, DateTime.new..DateTime.new + assert_instance_of Range, DateTime::Infinity.new..DateTime::Infinity.new + assert_instance_of Range, DateTime.new..DateTime::Infinity.new + end def test_overlaps_last_inclusive assert((1..5).overlaps?(5..10)) @@ -85,4 +92,28 @@ class RangeTest < ActiveSupport::TestCase time_range_2 = Time.utc(2005, 12, 10, 17, 31)..Time.utc(2005, 12, 10, 18, 00) assert !time_range_1.overlaps?(time_range_2) end + + def test_infinite_bounds + time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + + time = Time.now + date = Date.today + datetime = DateTime.now + twz = ActiveSupport::TimeWithZone.new(time, time_zone) + + infinity1 = Float::INFINITY + infinity2 = BigDecimal.new('Infinity') + + [infinity1, infinity2].each do |infinity| + [time, date, datetime, twz].each do |bound| + [time, date, datetime, twz].each do |value| + assert Range.new(bound, infinity).include?(value + 10.years) + assert Range.new(-infinity, bound).include?(value - 10.years) + + assert !Range.new(bound, infinity).include?(value - 10.years) + assert !Range.new(-infinity, bound).include?(value + 10.years) + end + end + end + end end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index fa8839bcb3..db1cf14abf 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -286,14 +286,37 @@ end class StringConversionsTest < ActiveSupport::TestCase def test_string_to_time - assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time - assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:local) - assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time - assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:local) - assert_equal DateTime.civil(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time - assert_equal Time.local(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:local) - assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time - assert_nil "".to_time + with_env_tz "US/Eastern" do + assert_equal Time.utc(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time(:utc) + assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50".to_time + assert_equal Time.utc(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time(:utc) + assert_equal Time.local(2005, 2, 27, 23, 50, 19, 275038), "2005-02-27T23:50:19.275038".to_time + assert_equal Time.utc(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time(:utc) + assert_equal Time.local(2039, 2, 27, 23, 50), "2039-02-27 23:50".to_time + assert_equal Time.local(2011, 2, 27, 18, 50), "2011-02-27 22:50 -0100".to_time + assert_equal Time.utc(2011, 2, 27, 23, 50), "2011-02-27 22:50 -0100".to_time(:utc) + assert_equal Time.local(2005, 2, 27, 23, 50), "2005-02-27 23:50 -0500".to_time + assert_nil "".to_time + end + end + + def test_string_to_time_utc_offset + with_env_tz "US/Eastern" do + assert_equal 0, "2005-02-27 23:50".to_time(:utc).utc_offset + assert_equal(-18000, "2005-02-27 23:50".to_time.utc_offset) + assert_equal 0, "2005-02-27 22:50 -0100".to_time(:utc).utc_offset + assert_equal(-18000, "2005-02-27 22:50 -0100".to_time.utc_offset) + end + end + + def test_partial_string_to_time + with_env_tz "US/Eastern" do + now = Time.now + assert_equal Time.local(now.year, now.month, now.day, 23, 50), "23:50".to_time + assert_equal Time.utc(now.year, now.month, now.day, 23, 50), "23:50".to_time(:utc) + assert_equal Time.local(now.year, now.month, now.day, 18, 50), "22:50 -0100".to_time + assert_equal Time.utc(now.year, now.month, now.day, 23, 50), "22:50 -0100".to_time(:utc) + end end def test_string_to_datetime @@ -304,10 +327,25 @@ class StringConversionsTest < ActiveSupport::TestCase assert_nil "".to_datetime end + def test_partial_string_to_datetime + now = DateTime.now + assert_equal DateTime.civil(now.year, now.month, now.day, 23, 50), "23:50".to_datetime + assert_equal DateTime.civil(now.year, now.month, now.day, 23, 50, 0, "-04:00"), "23:50 -0400".to_datetime + end + def test_string_to_date assert_equal Date.new(2005, 2, 27), "2005-02-27".to_date assert_nil "".to_date + assert_equal Date.new(Date.today.year, 2, 3), "Feb 3rd".to_date end + + protected + def with_env_tz(new_tz = 'US/Eastern') + old_tz, ENV['TZ'] = ENV['TZ'], new_tz + yield + ensure + old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') + end end class StringBehaviourTest < ActiveSupport::TestCase diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 70e07cefd1..43c92003dc 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -526,7 +526,11 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase 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 + with_env_tz 'US/Eastern' do + assert_equal Time, Time.local(2005, 2, 21, 17, 44, 30).to_time.class + assert_equal Time.local(2005, 2, 21, 17, 44, 30), Time.local(2005, 2, 21, 17, 44, 30).to_time + assert_equal Time.local(2005, 2, 21, 17, 44, 30).utc_offset, Time.local(2005, 2, 21, 17, 44, 30).to_time.utc_offset + end end # NOTE: this test seems to fail (changeset 1958) only on certain platforms, @@ -843,3 +847,10 @@ class TimeExtMarshalingTest < ActiveSupport::TestCase assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter end end + +class TimeExtBehaviorTest < ActiveSupport::TestCase + def test_compare_with_infinity + assert_equal(-1, Time.now <=> Float::INFINITY) + assert_equal(1, Time.now <=> -Float::INFINITY) + end +end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 56020da035..18eca4cd8b 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -327,7 +327,11 @@ class TimeWithZoneTest < ActiveSupport::TestCase end def test_to_time - assert_equal @twz, @twz.to_time + with_env_tz 'US/Eastern' do + assert_equal Time, @twz.to_time.class + assert_equal Time.local(1999, 12, 31, 19), @twz.to_time + assert_equal Time.local(1999, 12, 31, 19).utc_offset, @twz.to_time.utc_offset + end end def test_to_date @@ -1081,3 +1085,13 @@ class TimeWithZoneMethodsForString < ActiveSupport::TestCase Time.zone = old_tz end end + +class TimeWithZoneExtBehaviorTest < ActiveSupport::TestCase + def test_compare_with_infinity + time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + twz = ActiveSupport::TimeWithZone.new(Time.now, time_zone) + + assert_equal(-1, twz <=> Float::INFINITY) + assert_equal(1, twz <=> -Float::INFINITY) + end +end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 9d913c27b0..615808090d 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -363,12 +363,10 @@ class DependenciesTest < ActiveSupport::TestCase end def test_smart_name_error_strings - begin - Object.module_eval "ImaginaryObject" - flunk "No raise!!" - rescue NameError => e - assert e.message.include?("uninitialized constant ImaginaryObject") - end + Object.module_eval "ImaginaryObject" + flunk "No raise!!" + rescue NameError => e + assert e.message.include?("uninitialized constant ImaginaryObject") end def test_loadable_constants_for_path_should_handle_empty_autoloads diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 332100f5a1..c1a468ec86 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -119,9 +119,10 @@ class DeprecationTest < ActiveSupport::TestCase ActiveSupport::Deprecation.behavior = :silence behavior = ActiveSupport::Deprecation.behavior.first - assert_blank capture(:stderr) { + stderr_output = capture(:stderr) { assert_nil behavior.call('Some error!', ['call stack!']) } + assert stderr_output.blank? end def test_deprecated_instance_variable_proxy @@ -254,10 +255,10 @@ class DeprecationTest < ActiveSupport::TestCase klass::OLD.to_s end end - + def test_deprecated_instance_variable_with_instance_deprecator deprecator = deprecator_with_messages - + klass = Class.new() do def initialize(deprecator) @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator) diff --git a/activesupport/test/i18n_test.rb b/activesupport/test/i18n_test.rb index ddbba444cf..5ef59b6e6b 100644 --- a/activesupport/test/i18n_test.rb +++ b/activesupport/test/i18n_test.rb @@ -62,7 +62,7 @@ class I18nTest < ActiveSupport::TestCase end def test_date_order - assert_equal [:year, :month, :day], I18n.translate(:'date.order') + assert_equal %w(year month day), I18n.translate(:'date.order') end def test_time_am diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index aa41e57928..a1e5db6a2e 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -361,7 +361,7 @@ class InflectorTest < ActiveSupport::TestCase inflect.singular(/s$/, '') inflect.singular(/es$/, '') - + inflect.irregular('el', 'los') end diff --git a/activesupport/test/message_encryptor_test.rb b/activesupport/test/message_encryptor_test.rb index d6c31396b6..06c7e8a1a7 100644 --- a/activesupport/test/message_encryptor_test.rb +++ b/activesupport/test/message_encryptor_test.rb @@ -29,9 +29,9 @@ class MessageEncryptorTest < ActiveSupport::TestCase end def test_encrypting_twice_yields_differing_cipher_text - first_messqage = @encryptor.encrypt_and_sign(@data).split("--").first + first_message = @encryptor.encrypt_and_sign(@data).split("--").first second_message = @encryptor.encrypt_and_sign(@data).split("--").first - assert_not_equal first_messqage, second_message + assert_not_equal first_message, second_message end def test_messing_with_either_encrypted_values_causes_failure diff --git a/activesupport/test/notifications/instrumenter_test.rb b/activesupport/test/notifications/instrumenter_test.rb new file mode 100644 index 0000000000..62a9b61464 --- /dev/null +++ b/activesupport/test/notifications/instrumenter_test.rb @@ -0,0 +1,50 @@ +require 'abstract_unit' +require 'active_support/notifications/instrumenter' + +module ActiveSupport + module Notifications + class InstrumenterTest < ActiveSupport::TestCase + class TestNotifier + attr_reader :starts, :finishes + + def initialize + @starts = [] + @finishes = [] + end + + def start(*args); @starts << args; end + def finish(*args); @finishes << args; end + end + + attr_reader :instrumenter, :notifier, :payload + + def setup + super + @notifier = TestNotifier.new + @instrumenter = Instrumenter.new @notifier + @payload = { :foo => Object.new } + end + + def test_instrument + called = false + instrumenter.instrument("foo", payload) { + called = true + } + + assert called + end + + def test_start + instrumenter.start("foo", payload) + assert_equal [["foo", instrumenter.id, payload]], notifier.starts + assert_predicate notifier.finishes, :empty? + end + + def test_finish + instrumenter.finish("foo", payload) + assert_equal [["foo", instrumenter.id, payload]], notifier.finishes + assert_predicate notifier.starts, :empty? + end + end + end +end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 0f93c8b59e..3e6ac811a4 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -92,17 +92,21 @@ class AssertBlankTest < ActiveSupport::TestCase NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ] def test_assert_blank_true - BLANK.each { |v| assert_blank v } + BLANK.each { |value| + assert_deprecated { assert_blank value } + } end def test_assert_blank_false NOT_BLANK.each { |v| - begin - assert_blank v - fail 'should not get to here' - rescue Exception => e - assert_match(/is not blank/, e.message) - end + assert_deprecated { + begin + assert_blank v + fail 'should not get to here' + rescue Exception => e + assert_match(/is not blank/, e.message) + end + } } end end @@ -112,17 +116,21 @@ class AssertPresentTest < ActiveSupport::TestCase NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ] def test_assert_present_true - NOT_BLANK.each { |v| assert_present v } + NOT_BLANK.each { |v| + assert_deprecated { assert_present v } + } end def test_assert_present_false BLANK.each { |v| - begin - assert_present v - fail 'should not get to here' - rescue Exception => e - assert_match(/is blank/, e.message) - end + assert_deprecated { + begin + assert_present v + fail 'should not get to here' + rescue Exception => e + assert_match(/is blank/, e.message) + end + } } end end diff --git a/activesupport/test/testing/performance_test.rb b/activesupport/test/testing/performance_test.rb deleted file mode 100644 index 6918110cce..0000000000 --- a/activesupport/test/testing/performance_test.rb +++ /dev/null @@ -1,68 +0,0 @@ -require 'abstract_unit' - -module ActiveSupport - module Testing - class PerformanceTest < ActiveSupport::TestCase - begin - require 'active_support/testing/performance' - HAVE_RUBYPROF = true - rescue LoadError - HAVE_RUBYPROF = false - end - - def setup - skip "no rubyprof" unless HAVE_RUBYPROF - end - - def test_amount_format - amount_metric = ActiveSupport::Testing::Performance::Metrics[:amount].new - assert_equal "0", amount_metric.format(0) - assert_equal "1", amount_metric.format(1.23) - assert_equal "40,000,000", amount_metric.format(40000000) - end - - def test_time_format - time_metric = ActiveSupport::Testing::Performance::Metrics[:time].new - assert_equal "0 ms", time_metric.format(0) - assert_equal "40 ms", time_metric.format(0.04) - assert_equal "41 ms", time_metric.format(0.0415) - assert_equal "1.23 sec", time_metric.format(1.23) - assert_equal "40000.00 sec", time_metric.format(40000) - assert_equal "-5000 ms", time_metric.format(-5) - end - - def test_space_format - space_metric = ActiveSupport::Testing::Performance::Metrics[:digital_information_unit].new - assert_equal "0 Bytes", space_metric.format(0) - assert_equal "0 Bytes", space_metric.format(0.4) - assert_equal "1 Byte", space_metric.format(1.23) - assert_equal "123 Bytes", space_metric.format(123) - assert_equal "123 Bytes", space_metric.format(123.45) - assert_equal "12 KB", space_metric.format(12345) - assert_equal "1.2 MB", space_metric.format(1234567) - assert_equal "9.3 GB", space_metric.format(10**10) - assert_equal "91 TB", space_metric.format(10**14) - assert_equal "910000 TB", space_metric.format(10**18) - end - - def test_environment_format_without_rails - metric = ActiveSupport::Testing::Performance::Metrics[:time].new - benchmarker = ActiveSupport::Testing::Performance::Benchmarker.new(self, metric) - assert_equal "#{RUBY_ENGINE}-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL},#{RUBY_PLATFORM}", benchmarker.environment - end - - def test_environment_format_with_rails - rails, version = Module.new, Module.new - version.const_set :STRING, "4.0.0" - rails.const_set :VERSION, version - Object.const_set :Rails, rails - - metric = ActiveSupport::Testing::Performance::Metrics[:time].new - benchmarker = ActiveSupport::Testing::Performance::Benchmarker.new(self, metric) - assert_equal "rails-4.0.0,#{RUBY_ENGINE}-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL},#{RUBY_PLATFORM}", benchmarker.environment - ensure - Object.send :remove_const, :Rails - end - end - end -end |