diff options
Diffstat (limited to 'activesupport')
23 files changed, 305 insertions, 124 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 1b8bcf649c..373236ce9a 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,14 @@ *Rails 3.1.0 (unreleased)* +* LocalCache strategy is now a real middleware class, not an anonymous class +posing for pictures. + +* ActiveSupport::Dependencies::ClassCache class has been introduced for +holding references to reloadable classes. + +* ActiveSupport::Dependencies::Reference has been refactored to take direct +advantage of the new ClassCache. + * Backports Range#cover? as an alias for Range#include? in Ruby 1.8 [Diego Carrion, fxn] * Added weeks_ago and prev_week to Date/DateTime/Time. [Rob Zolkos, fxn] diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb index 4dce35f1c9..0649a058aa 100644 --- a/activesupport/lib/active_support/cache/strategy/local_cache.rb +++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb @@ -50,34 +50,39 @@ module ActiveSupport end end - # Middleware class can be inserted as a Rack handler to be local cache for the - # duration of request. - def middleware - @middleware ||= begin - klass = Class.new - klass.class_eval(<<-EOS, __FILE__, __LINE__ + 1) - class << self - def name - "ActiveSupport::Cache::Strategy::LocalCache" - end - alias :to_s :name - end + #-- + # This class wraps up local storage for middlewares. Only the middleware method should + # construct them. + class Middleware # :nodoc: + attr_reader :name, :thread_local_key - def initialize(app) - @app = app - end + def initialize(name, thread_local_key) + @name = name + @thread_local_key = thread_local_key + @app = nil + end - def call(env) - Thread.current[:#{thread_local_key}] = LocalStore.new - @app.call(env) - ensure - Thread.current[:#{thread_local_key}] = nil - end - EOS - klass + def new(app) + @app = app + self + end + + def call(env) + Thread.current[thread_local_key] = LocalStore.new + @app.call(env) + ensure + Thread.current[thread_local_key] = nil end end + # Middleware class can be inserted as a Rack handler to be local cache for the + # duration of request. + def middleware + @middleware ||= Middleware.new( + "ActiveSupport::Cache::Strategy::LocalCache", + thread_local_key) + end + def clear(options = nil) # :nodoc: local_cache.clear(options) if local_cache super diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb index 644db0b205..be19189c04 100644 --- a/activesupport/lib/active_support/configurable.rb +++ b/activesupport/lib/active_support/configurable.rb @@ -26,7 +26,7 @@ module ActiveSupport module ClassMethods def config - @_config ||= if superclass.respond_to?(:config) + @_config ||= if respond_to?(:superclass) && superclass.respond_to?(:config) superclass.config.inheritable_copy else # create a new "anonymous" class that will host the compiled reader methods diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index f34185f22c..724e076407 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -36,9 +36,9 @@ class Date ::Date.current.tomorrow end - # Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today. + # Returns Time.zone.today when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise just returns Date.today. def current - ::Time.zone_default ? ::Time.zone.today : ::Date.today + ::Time.zone ? ::Time.zone.today : ::Date.today end end diff --git a/activesupport/lib/active_support/core_ext/date/zones.rb b/activesupport/lib/active_support/core_ext/date/zones.rb index 3a83af6be2..a70b47b7bc 100644 --- a/activesupport/lib/active_support/core_ext/date/zones.rb +++ b/activesupport/lib/active_support/core_ext/date/zones.rb @@ -2,10 +2,10 @@ require 'date' require 'active_support/core_ext/time/zones' class Date - # Converts Date to a TimeWithZone in the current zone if Time.zone_default is set, - # otherwise converts Date to a Time via Date#to_time + # Converts Date to a TimeWithZone in the current zone if Time.zone or Time.zone_default + # is set, otherwise converts Date to a Time via Date#to_time def to_time_in_current_zone - if ::Time.zone_default + if ::Time.zone ::Time.zone.local(year, month, day) else to_time diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index 1dc3933e12..8d924ad420 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -1,6 +1,4 @@ require 'rational' unless RUBY_VERSION >= '1.9.2' -require 'active_support/core_ext/object/acts_like' -require 'active_support/core_ext/time/zones' class DateTime class << self @@ -9,8 +7,9 @@ class DateTime ::Time.local(2007).utc_offset.to_r / 86400 end + # Returns <tt>Time.zone.now.to_datetime</tt> when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise returns <tt>Time.now.to_datetime</tt>. def current - ::Time.zone_default ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime + ::Time.zone ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime end end @@ -104,11 +103,7 @@ class DateTime end # Layers additional behavior on DateTime#<=> so that Time and ActiveSupport::TimeWithZone instances can be compared with a DateTime - def compare_with_coercion(other) - other = other.comparable_time if other.respond_to?(:comparable_time) - other = other.to_datetime unless other.acts_like?(:date) - compare_without_coercion(other) + def <=>(other) + super other.to_datetime end - alias_method :compare_without_coercion, :<=> - alias_method :<=>, :compare_with_coercion end diff --git a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb index 87a7bebd7b..63b4ba49e9 100644 --- a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb @@ -1,11 +1,9 @@ class Hash - # Allows for reverse merging two hashes where the keys in the calling hash take precedence over those - # in the <tt>other_hash</tt>. This is particularly useful for initializing an option hash with default values: + # Merges the caller into +other_hash+. For example, # - # def setup(options = {}) - # options.reverse_merge! :size => 25, :velocity => 10 - # end + # options = options.reverse_merge(:size => 25, :velocity => 10) # +<<<<<<< HEAD # The default <tt>:size</tt> and <tt>:velocity</tt> are only set if the +options+ hash passed in doesn't already # have the respective key. # @@ -14,12 +12,19 @@ class Hash # def setup(options = {}) # options = { :size => 25, :velocity => 10 }.merge(options) # end +======= + # is equivalent to + # + # options = {:size => 25, :velocity => 10}.merge(options) + # + # This is particularly useful for initializing an options hash + # with default values. +>>>>>>> 20768176292cbcb883ab152b4aa9ed8c664771cd def reverse_merge(other_hash) other_hash.merge(self) end - # Performs the opposite of <tt>merge</tt>, with the keys and values from the first hash taking precedence over the second. - # Modifies the receiver in place. + # Destructive +reverse_merge+. def reverse_merge!(other_hash) # right wins if there is no left merge!( other_hash ){|key,left,right| left } diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index f70853073a..7b5832b51a 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -13,7 +13,11 @@ class Object respond_to?(:empty?) ? empty? : !self end +<<<<<<< HEAD # An object is present if it's not #blank?. +======= + # An object is present if it's not <tt>blank?</tt>. +>>>>>>> 20768176292cbcb883ab152b4aa9ed8c664771cd def present? !blank? end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index fa052fa86b..7e134db118 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -1,7 +1,4 @@ require 'active_support/duration' -require 'active_support/core_ext/date/acts_like' -require 'active_support/core_ext/date/calculations' -require 'active_support/core_ext/date_time/conversions' class Time COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] @@ -41,9 +38,9 @@ class Time time_with_datetime_fallback(:local, *args) end - # Returns <tt>Time.zone.now</tt> when <tt>config.time_zone</tt> is set, otherwise just returns <tt>Time.now</tt>. + # Returns <tt>Time.zone.now</tt> when <tt>Time.zone</tt> or <tt>config.time_zone</tt> are set, otherwise just returns <tt>Time.now</tt>. def current - ::Time.zone_default ? ::Time.zone.now : ::Time.now + ::Time.zone ? ::Time.zone.now : ::Time.now end end @@ -283,14 +280,8 @@ class Time # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances # can be chronologically compared with a Time def compare_with_coercion(other) - # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do <=> comparison - other = other.comparable_time if other.respond_to?(:comparable_time) - if other.acts_like?(:date) - # other is a Date/DateTime, so coerce self #to_datetime and hand off to DateTime#<=> - to_datetime.compare_without_coercion(other) - else - compare_without_coercion(other) - end + # we're avoiding Time#to_datetime cause it's expensive + other.is_a?(Time) ? compare_without_coercion(other.to_time) : to_datetime <=> other end alias_method :compare_without_coercion, :<=> alias_method :<=>, :compare_with_coercion diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index daa98162d0..dc10f78104 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -5,6 +5,7 @@ require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/attribute_accessors' require 'active_support/core_ext/module/introspection' require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/module/deprecation' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/load_error' require 'active_support/core_ext/name_error' @@ -47,9 +48,6 @@ module ActiveSupport #:nodoc: mattr_accessor :autoloaded_constants self.autoloaded_constants = [] - mattr_accessor :references - self.references = {} - # An array of constant names that need to be unloaded on every request. Used # to allow arbitrary constants to be marked for unloading. mattr_accessor :explicitly_unloadable_constants @@ -524,31 +522,76 @@ module ActiveSupport #:nodoc: explicitly_unloadable_constants.each { |const| remove_constant const } end - class Reference - @@constants = Hash.new { |h, k| h[k] = Inflector.constantize(k) } + class ClassCache + def initialize + @store = Hash.new { |h, k| h[k] = Inflector.constantize(k) } + end + + def empty? + @store.empty? + end + + def key?(key) + @store.key?(key) + end + + def []=(key, value) + return unless key.respond_to?(:name) + + raise(ArgumentError, 'anonymous classes cannot be cached') if key.name.blank? + + @store[key.name] = value + end + + def [](key) + key = key.name if key.respond_to?(:name) + + @store[key] + end + alias :get :[] - attr_reader :name + class Getter # :nodoc: + def initialize(name) + @name = name + end - def initialize(name) - @name = name.to_s - @@constants[@name] = name if name.respond_to?(:name) + def get + Reference.get @name + end + deprecate :get end - def get - @@constants[@name] + def new(name) + self[name] = name + Getter.new(name) end + deprecate :new - def self.clear! - @@constants.clear + def store(name) + self[name] = name + self + end + + def clear! + @store.clear end end + Reference = ClassCache.new + def ref(name) - references[name] ||= Reference.new(name) + Reference.new(name) + end + deprecate :ref + + # Store a reference to a class +klass+. + def reference(klass) + Reference.store klass end + # Get the reference for class named +name+. def constantize(name) - ref(name).get + Reference.get(name) end # Determine if the given constant has been automatically loaded. diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb index f4c27ac935..8d8e6ebc58 100644 --- a/activesupport/lib/active_support/ordered_options.rb +++ b/activesupport/lib/active_support/ordered_options.rb @@ -31,7 +31,7 @@ module ActiveSupport #:nodoc: def method_missing(name, *args) if name.to_s =~ /(.*)=$/ - self[$1.to_sym] = args.first + self[$1] = args.first else self[name] end diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 3da216ac78..c66aa78ce8 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -281,7 +281,7 @@ module ActiveSupport # A TimeWithZone acts like a Time, so just return +self+. def to_time - self + utc end def to_datetime diff --git a/activesupport/test/class_cache_test.rb b/activesupport/test/class_cache_test.rb new file mode 100644 index 0000000000..8445af8d25 --- /dev/null +++ b/activesupport/test/class_cache_test.rb @@ -0,0 +1,108 @@ +require 'abstract_unit' +require 'active_support/dependencies' + +module ActiveSupport + module Dependencies + class ClassCacheTest < ActiveSupport::TestCase + def setup + @cache = ClassCache.new + end + + def test_empty? + assert @cache.empty? + @cache[ClassCacheTest] = ClassCacheTest + assert !@cache.empty? + end + + def test_clear! + assert @cache.empty? + @cache[ClassCacheTest] = ClassCacheTest + assert !@cache.empty? + @cache.clear! + assert @cache.empty? + end + + def test_set_key + @cache[ClassCacheTest] = ClassCacheTest + assert @cache.key?(ClassCacheTest.name) + end + + def test_set_rejects_strings + @cache[ClassCacheTest.name] = ClassCacheTest + assert @cache.empty? + end + + def test_get_with_class + @cache[ClassCacheTest] = ClassCacheTest + assert_equal ClassCacheTest, @cache[ClassCacheTest] + end + + def test_get_with_name + @cache[ClassCacheTest] = ClassCacheTest + assert_equal ClassCacheTest, @cache[ClassCacheTest.name] + end + + def test_get_constantizes + assert @cache.empty? + assert_equal ClassCacheTest, @cache[ClassCacheTest.name] + end + + def test_get_is_an_alias + assert_equal @cache[ClassCacheTest], @cache.get(ClassCacheTest.name) + end + + def test_new + assert_deprecated do + @cache.new ClassCacheTest + end + assert @cache.key?(ClassCacheTest.name) + end + + def test_new_rejects_strings + assert_deprecated do + @cache.new ClassCacheTest.name + end + assert !@cache.key?(ClassCacheTest.name) + end + + def test_new_rejects_strings + @cache.store ClassCacheTest.name + assert !@cache.key?(ClassCacheTest.name) + end + + def test_store_returns_self + x = @cache.store ClassCacheTest + assert_equal @cache, x + end + + def test_new_returns_proxy + v = nil + assert_deprecated do + v = @cache.new ClassCacheTest.name + end + + assert_deprecated do + assert_equal ClassCacheTest, v.get + end + end + + def test_anonymous_class_fail + assert_raises(ArgumentError) do + assert_deprecated do + @cache.new Class.new + end + end + + assert_raises(ArgumentError) do + x = Class.new + @cache[x] = x + end + + assert_raises(ArgumentError) do + x = Class.new + @cache.store x + end + end + end + end +end diff --git a/activesupport/test/configurable_test.rb b/activesupport/test/configurable_test.rb index 9c773c1944..2b28e61815 100644 --- a/activesupport/test/configurable_test.rb +++ b/activesupport/test/configurable_test.rb @@ -21,6 +21,12 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase assert_equal({ :foo => :bar }, Parent.config) end + test "adds a configuration hash to a module as well" do + mixin = Module.new { include ActiveSupport::Configurable } + mixin.config.foo = :bar + assert_equal({ :foo => :bar }, mixin.config) + end + test "configuration hash is inheritable" do assert_equal :bar, Child.config.foo assert_equal :bar, Parent.config.foo @@ -57,4 +63,4 @@ class ConfigurableActiveSupport < ActiveSupport::TestCase assert_respond_to child.config, :bar assert_respond_to child.new.config, :bar end -end
\ No newline at end of file +end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index b4d7633e5f..03b84ae2e5 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -259,7 +259,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.current - 1, Date.yesterday end - def test_yesterday_constructor_when_zone_default_is_not_set + def test_yesterday_constructor_when_zone_is_not_set with_env_tz 'UTC' do with_tz_default do Time.stubs(:now).returns Time.local(2000, 1, 1) @@ -268,7 +268,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_yesterday_constructor_when_zone_default_is_set + def test_yesterday_constructor_when_zone_is_set with_env_tz 'UTC' do with_tz_default ActiveSupport::TimeZone['Eastern Time (US & Canada)'] do # UTC -5 Time.stubs(:now).returns Time.local(2000, 1, 1) @@ -281,7 +281,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.current + 1, Date.tomorrow end - def test_tomorrow_constructor_when_zone_default_is_not_set + def test_tomorrow_constructor_when_zone_is_not_set with_env_tz 'UTC' do with_tz_default do Time.stubs(:now).returns Time.local(1999, 12, 31) @@ -290,7 +290,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_tomorrow_constructor_when_zone_default_is_set + def test_tomorrow_constructor_when_zone_is_set with_env_tz 'UTC' do with_tz_default ActiveSupport::TimeZone['Europe/Paris'] do # UTC +1 Time.stubs(:now).returns Time.local(1999, 12, 31, 23) @@ -303,7 +303,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2005,2,21,0,0,45), Date.new(2005,2,21).since(45) end - def test_since_when_zone_default_is_set + def test_since_when_zone_is_set zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'UTC' do with_tz_default zone do @@ -317,7 +317,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2005,2,20,23,59,15), Date.new(2005,2,21).ago(45) end - def test_ago_when_zone_default_is_set + def test_ago_when_zone_is_set zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'UTC' do with_tz_default zone do @@ -331,7 +331,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day end - def test_beginning_of_day_when_zone_default_is_set + def test_beginning_of_day_when_zone_is_set zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'UTC' do with_tz_default zone do @@ -345,7 +345,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2005,2,21,23,59,59,999999.999), Date.new(2005,2,21).end_of_day end - def test_end_of_day_when_zone_default_is_set + def test_end_of_day_when_zone_is_set zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'UTC' do with_tz_default zone do @@ -367,7 +367,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_xmlschema_when_zone_default_is_set + def test_xmlschema_when_zone_is_set with_env_tz 'UTC' do with_tz_default ActiveSupport::TimeZone['Eastern Time (US & Canada)'] do # UTC -5 assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema) @@ -407,7 +407,7 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal true, Date.new(2000,1,2).future? end - def test_current_returns_date_today_when_zone_default_not_set + def test_current_returns_date_today_when_zone_not_set 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 @@ -415,15 +415,15 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_current_returns_time_zone_today_when_zone_default_set - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + def test_current_returns_time_zone_today_when_zone_is_set + Time.zone = 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 + Time.zone = nil end def test_date_advance_should_not_change_passed_options_hash @@ -441,11 +441,11 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end def with_tz_default(tz = nil) - old_tz = Time.zone_default - Time.zone_default = tz + old_tz = Time.zone + Time.zone = tz yield ensure - Time.zone_default = old_tz + Time.zone = old_tz end end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 8edb95b63a..456736cbad 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -282,21 +282,21 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal true, DateTime.civil(2005,2,10,20,30,46).future? end - def test_current_returns_date_today_when_zone_default_not_set + def test_current_returns_date_today_when_zone_is_not_set with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59) assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current end end - def test_current_returns_time_zone_today_when_zone_default_set - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + def test_current_returns_time_zone_today_when_zone_is_set + Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(1999, 12, 31, 23, 59, 59) assert_equal DateTime.new(1999, 12, 31, 23, 59, 59, Rational(-18000, 86400)), DateTime.current end ensure - Time.zone_default = nil + Time.zone = nil end def test_current_without_time_zone diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 6a01eeed6b..c0b529d9f8 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -89,8 +89,8 @@ class DurationTest < ActiveSupport::TestCase assert_in_delta((7 * 24 * 1.7).hours.ago(t), 1.7.weeks.ago(t), 1) end - def test_since_and_ago_anchored_to_time_now_when_time_zone_default_not_set - Time.zone_default = nil + def test_since_and_ago_anchored_to_time_now_when_time_zone_is_not_set + Time.zone = nil with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) # since @@ -102,8 +102,8 @@ class DurationTest < ActiveSupport::TestCase end end - def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_is_set + Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) # since @@ -116,7 +116,7 @@ class DurationTest < ActiveSupport::TestCase assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name end ensure - Time.zone_default = nil + Time.zone = nil end def test_adding_hours_across_dst_boundary diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index 6ef4e37b26..3a2452b4b0 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -89,8 +89,8 @@ class NumericExtTimeAndDateTimeTest < Test::Unit::TestCase assert_equal DateTime.civil(2005,2,28,15,15,10), DateTime.civil(2004,2,29,15,15,10) + 1.year end - def test_since_and_ago_anchored_to_time_now_when_time_zone_default_not_set - Time.zone_default = nil + def test_since_and_ago_anchored_to_time_now_when_time_zone_is_not_set + Time.zone = nil with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) # since @@ -102,8 +102,8 @@ class NumericExtTimeAndDateTimeTest < Test::Unit::TestCase end end - def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_is_set + Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) # since @@ -116,7 +116,7 @@ class NumericExtTimeAndDateTimeTest < Test::Unit::TestCase assert_equal 'Eastern Time (US & Canada)', 5.ago.time_zone.name end ensure - Time.zone_default = nil + Time.zone = nil end protected diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 891a6badac..53d497013a 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -533,9 +533,19 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase Time::DATE_FORMATS.delete(:custom) end - def test_conversion_methods_are_publicized - assert Time.public_instance_methods.include?(:to_date) || Time.public_instance_methods.include?('to_date') - assert Time.public_instance_methods.include?(:to_datetime) || Time.public_instance_methods.include?('to_datetime') + 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_env_tz '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_env_tz '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 + assert_equal ::Date::ITALY, Time.utc(2005, 2, 21, 17, 44, 30).to_datetime.start # use Ruby's default start value end def test_to_time diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 5c226c2d09..bafa335a09 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -768,10 +768,10 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end def test_localtime - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + Time.zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_equal @dt.in_time_zone.localtime, @dt.in_time_zone.utc.to_time.getlocal ensure - Time.zone_default = nil + Time.zone = nil end def test_use_zone @@ -801,7 +801,7 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase assert_equal nil, Time.zone end - def test_time_zone_getter_and_setter_with_zone_default + def test_time_zone_getter_and_setter_with_zone_default_set Time.zone_default = ActiveSupport::TimeZone['Alaska'] assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone Time.zone = ActiveSupport::TimeZone['Hawaii'] @@ -809,6 +809,7 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase Time.zone = nil assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone ensure + Time.zone = nil Time.zone_default = nil end @@ -849,7 +850,7 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase assert_nil Time.zone end - def test_current_returns_time_now_when_zone_default_not_set + def test_current_returns_time_now_when_zone_not_set with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone) @@ -857,8 +858,8 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end end - def test_current_returns_time_zone_now_when_zone_default_set - Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + def test_current_returns_time_zone_now_when_zone_set + Time.zone = 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) @@ -866,7 +867,7 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase assert_equal Time.utc(2000), Time.current.time end ensure - Time.zone_default = nil + Time.zone = nil end protected diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index bc7f597f1d..ef017d7436 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -477,15 +477,15 @@ class DependenciesTest < Test::Unit::TestCase def test_references_should_work with_loading 'dependencies' do - c = ActiveSupport::Dependencies.ref("ServiceOne") + c = ActiveSupport::Dependencies.reference("ServiceOne") service_one_first = ServiceOne - assert_equal service_one_first, c.get + assert_equal service_one_first, c.get("ServiceOne") ActiveSupport::Dependencies.clear assert ! defined?(ServiceOne) service_one_second = ServiceOne - assert_not_equal service_one_first, c.get - assert_equal service_one_second, c.get + assert_not_equal service_one_first, c.get("ServiceOne") + assert_equal service_one_second, c.get("ServiceOne") end end diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index f55116dfab..1670d9ee7d 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -51,21 +51,21 @@ class InflectorTest < Test::Unit::TestCase end SingularToPlural.each do |singular, plural| - define_method "test_pluralize_#{singular}" do + define_method "test_pluralize_singular_#{singular}" do assert_equal(plural, ActiveSupport::Inflector.pluralize(singular)) assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(singular.capitalize)) end end SingularToPlural.each do |singular, plural| - define_method "test_singularize_#{plural}" do + define_method "test_singularize_plural_#{plural}" do assert_equal(singular, ActiveSupport::Inflector.singularize(plural)) assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(plural.capitalize)) end end - + SingularToPlural.each do |singular, plural| - define_method "test_pluralize_#{plural}" do + define_method "test_pluralize_plural_#{plural}" do assert_equal(plural, ActiveSupport::Inflector.pluralize(plural)) assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(plural.capitalize)) end diff --git a/activesupport/test/test_case_test.rb b/activesupport/test/test_case_test.rb index 7e65c63062..756d21b3e4 100644 --- a/activesupport/test/test_case_test.rb +++ b/activesupport/test/test_case_test.rb @@ -12,6 +12,10 @@ module ActiveSupport def puke(klass, name, e) @puked << [klass, name, e] end + + def options + nil + end end if defined?(MiniTest::Assertions) && TestCase < MiniTest::Assertions |