diff options
Diffstat (limited to 'activesupport/test')
26 files changed, 543 insertions, 570 deletions
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 25ed962c23..8a67b148c3 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -21,15 +21,5 @@ require 'empty_bool' ENV['NO_RELOAD'] = '1' require 'active_support' -def uses_memcached(test_name) - require 'memcache' - begin - MemCache.new('localhost:11211').stats - yield - rescue MemCache::MemCacheError - $stderr.puts "Skipping #{test_name} tests. Start memcached and try again." - end -end - # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true diff --git a/activesupport/test/autoload.rb b/activesupport/test/autoload_test.rb index 5d8026a9ca..7d02d835a8 100644 --- a/activesupport/test/autoload.rb +++ b/activesupport/test/autoload_test.rb @@ -28,15 +28,6 @@ class TestAutoloadModule < ActiveSupport::TestCase assert_nothing_raised { ::Fixtures::Autoload::SomeClass } end - test ":eager constants can be triggered via ActiveSupport::Autoload.eager_autoload!" do - module ::Fixtures::Autoload - autoload :SomeClass, "fixtures/autoload/some_class" - end - ActiveSupport::Autoload.eager_autoload! - assert $LOADED_FEATURES.include?("fixtures/autoload/some_class.rb") - assert_nothing_raised { ::Fixtures::Autoload::SomeClass } - end - test "the location of autoloaded constants defaults to :name.underscore" do module ::Fixtures::Autoload autoload :SomeClass @@ -51,8 +42,7 @@ class TestAutoloadModule < ActiveSupport::TestCase autoload :SomeClass end - ActiveSupport::Autoload.eager_autoload! - assert $LOADED_FEATURES.include?("fixtures/autoload/some_class.rb") + ::Fixtures::Autoload.eager_load! assert_nothing_raised { ::Fixtures::Autoload::SomeClass } end diff --git a/activesupport/test/autoloading_fixtures/circular1.rb b/activesupport/test/autoloading_fixtures/circular1.rb new file mode 100644 index 0000000000..a45761f066 --- /dev/null +++ b/activesupport/test/autoloading_fixtures/circular1.rb @@ -0,0 +1,6 @@ +silence_warnings do + Circular2 +end + +class Circular1 +end diff --git a/activesupport/test/autoloading_fixtures/circular2.rb b/activesupport/test/autoloading_fixtures/circular2.rb new file mode 100644 index 0000000000..c847fa5001 --- /dev/null +++ b/activesupport/test/autoloading_fixtures/circular2.rb @@ -0,0 +1,4 @@ +Circular1 + +class Circular2 +end diff --git a/activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb b/activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb index ef66ddaac7..402609c583 100644 --- a/activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb +++ b/activesupport/test/autoloading_fixtures/class_folder/class_folder_subclass.rb @@ -1,3 +1,3 @@ class ClassFolder::ClassFolderSubclass < ClassFolder - ConstantInClassFolder + ConstantInClassFolder = 'indeed' end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index a75db47be8..71cd9d81b3 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -83,20 +83,20 @@ class CacheStoreSettingTest < ActiveSupport::TestCase end def test_mem_cache_fragment_cache_store - MemCache.expects(:new).with(%w[localhost], {}) + Dalli::Client.expects(:new).with(%w[localhost], {}) store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost" assert_kind_of(ActiveSupport::Cache::MemCacheStore, store) end def test_mem_cache_fragment_cache_store_with_given_mem_cache - mem_cache = MemCache.new - MemCache.expects(:new).never + mem_cache = Dalli::Client.new + Dalli::Client.expects(:new).never store = ActiveSupport::Cache.lookup_store :mem_cache_store, mem_cache assert_kind_of(ActiveSupport::Cache::MemCacheStore, store) end def test_mem_cache_fragment_cache_store_with_given_mem_cache_like_object - MemCache.expects(:new).never + Dalli::Client.expects(:new).never memcache = Object.new def memcache.get() true end store = ActiveSupport::Cache.lookup_store :mem_cache_store, memcache @@ -104,13 +104,13 @@ class CacheStoreSettingTest < ActiveSupport::TestCase end def test_mem_cache_fragment_cache_store_with_multiple_servers - MemCache.expects(:new).with(%w[localhost 192.168.1.1], {}) + Dalli::Client.expects(:new).with(%w[localhost 192.168.1.1], {}) store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1' assert_kind_of(ActiveSupport::Cache::MemCacheStore, store) end def test_mem_cache_fragment_cache_store_with_options - MemCache.expects(:new).with(%w[localhost 192.168.1.1], { :timeout => 10 }) + Dalli::Client.expects(:new).with(%w[localhost 192.168.1.1], { :timeout => 10 }) store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo', :timeout => 10 assert_kind_of(ActiveSupport::Cache::MemCacheStore, store) assert_equal 'foo', store.options[:namespace] @@ -447,6 +447,7 @@ module CacheIncrementDecrementBehavior assert_equal 2, @cache.read('foo').to_i assert_equal 3, @cache.increment('foo') assert_equal 3, @cache.read('foo').to_i + assert_nil @cache.increment('bar') end def test_decrement @@ -456,6 +457,7 @@ module CacheIncrementDecrementBehavior assert_equal 2, @cache.read('foo').to_i assert_equal 1, @cache.decrement('foo') assert_equal 1, @cache.read('foo').to_i + assert_nil @cache.decrement('bar') end end @@ -702,53 +704,65 @@ class MemoryStoreTest < ActiveSupport::TestCase end end -uses_memcached 'memcached backed store' do - class MemCacheStoreTest < ActiveSupport::TestCase - def setup - @cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :expires_in => 60) - @peek = ActiveSupport::Cache.lookup_store(:mem_cache_store) - @data = @cache.instance_variable_get(:@data) - @cache.clear - @cache.silence! - @cache.logger = ActiveSupport::Logger.new("/dev/null") - end +class MemCacheStoreTest < ActiveSupport::TestCase + require 'dalli' + + begin + ss = Dalli::Client.new('localhost:11211').stats + raise Dalli::DalliError unless ss['localhost:11211'] + + MEMCACHE_UP = true + rescue Dalli::DalliError + $stderr.puts "Skipping memcached tests. Start memcached and try again." + MEMCACHE_UP = false + end + + def setup + skip "memcache server is not up" unless MEMCACHE_UP + + @cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :expires_in => 60) + @peek = ActiveSupport::Cache.lookup_store(:mem_cache_store) + @data = @cache.instance_variable_get(:@data) + @cache.clear + @cache.silence! + @cache.logger = ActiveSupport::Logger.new("/dev/null") + end + + include CacheStoreBehavior + include LocalCacheBehavior + include CacheIncrementDecrementBehavior + include EncodedKeyCacheBehavior + + def test_raw_values + cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true) + cache.clear + cache.write("foo", 2) + assert_equal "2", cache.read("foo") + end - include CacheStoreBehavior - include LocalCacheBehavior - include CacheIncrementDecrementBehavior - include EncodedKeyCacheBehavior + def test_raw_values_with_marshal + cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true) + cache.clear + cache.write("foo", Marshal.dump([])) + assert_equal [], cache.read("foo") + end - def test_raw_values - cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true) - cache.clear + def test_local_cache_raw_values + cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true) + cache.clear + cache.with_local_cache do cache.write("foo", 2) assert_equal "2", cache.read("foo") end + end - def test_raw_values_with_marshal - cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true) - cache.clear + def test_local_cache_raw_values_with_marshal + cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true) + cache.clear + cache.with_local_cache do cache.write("foo", Marshal.dump([])) assert_equal [], cache.read("foo") end - - def test_local_cache_raw_values - cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true) - cache.clear - cache.with_local_cache do - cache.write("foo", 2) - assert_equal "2", cache.read("foo") - end - end - - def test_local_cache_raw_values_with_marshal - cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, :raw => true) - cache.clear - cache.with_local_cache do - cache.write("foo", Marshal.dump([])) - assert_equal [], cache.read("foo") - end - end end end diff --git a/activesupport/test/core_ext/date_and_time_behavior.rb b/activesupport/test/core_ext/date_and_time_behavior.rb new file mode 100644 index 0000000000..014935b0c0 --- /dev/null +++ b/activesupport/test/core_ext/date_and_time_behavior.rb @@ -0,0 +1,186 @@ +require 'abstract_unit' + +module DateAndTimeBehavior + def test_yesterday + assert_equal date_time_init(2005,2,21,10,10,10), date_time_init(2005,2,22,10,10,10).yesterday + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,3,2,10,10,10).yesterday.yesterday + end + + def test_tomorrow + assert_equal date_time_init(2005,2,23,10,10,10), date_time_init(2005,2,22,10,10,10).tomorrow + assert_equal date_time_init(2005,3,2,10,10,10), date_time_init(2005,2,28,10,10,10).tomorrow.tomorrow + end + + def test_days_ago + assert_equal date_time_init(2005,6,4,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(1) + assert_equal date_time_init(2005,5,31,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(5) + end + + def test_days_since + assert_equal date_time_init(2005,6,6,10,10,10), date_time_init(2005,6,5,10,10,10).days_since(1) + assert_equal date_time_init(2005,1,1,10,10,10), date_time_init(2004,12,31,10,10,10).days_since(1) + end + + def test_weeks_ago + assert_equal date_time_init(2005,5,29,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(1) + assert_equal date_time_init(2005,5,1,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(5) + assert_equal date_time_init(2005,4,24,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(6) + assert_equal date_time_init(2005,2,27,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(14) + assert_equal date_time_init(2004,12,25,10,10,10), date_time_init(2005,1,1,10,10,10).weeks_ago(1) + end + + def test_weeks_since + assert_equal date_time_init(2005,7,14,10,10,10), date_time_init(2005,7,7,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,7,14,10,10,10), date_time_init(2005,7,7,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,7,4,10,10,10), date_time_init(2005,6,27,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,1,4,10,10,10), date_time_init(2004,12,28,10,10,10).weeks_since(1) + end + + def test_months_ago + assert_equal date_time_init(2005,5,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(1) + assert_equal date_time_init(2004,11,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(7) + assert_equal date_time_init(2004,12,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(6) + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(12) + assert_equal date_time_init(2003,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(24) + end + + def test_months_since + assert_equal date_time_init(2005,7,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(1) + assert_equal date_time_init(2006,1,5,10,10,10), date_time_init(2005,12,5,10,10,10).months_since(1) + assert_equal date_time_init(2005,12,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(6) + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,12,5,10,10,10).months_since(6) + assert_equal date_time_init(2006,1,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(7) + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(12) + assert_equal date_time_init(2007,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(24) + assert_equal date_time_init(2005,4,30,10,10,10), date_time_init(2005,3,31,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,29,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,30,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,31,10,10,10).months_since(1) + end + + def test_years_ago + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_ago(1) + assert_equal date_time_init(1998,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_ago(7) + assert_equal date_time_init(2003,2,28,10,10,10), date_time_init(2004,2,29,10,10,10).years_ago(1) # 1 year ago from leap day + end + + def test_years_since + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(1) + assert_equal date_time_init(2012,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(7) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2004,2,29,10,10,10).years_since(1) # 1 year since leap day + assert_equal date_time_init(2182,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(177) + end + + def test_beginning_of_month + assert_equal date_time_init(2005,2,1,0,0,0), date_time_init(2005,2,22,10,10,10).beginning_of_month + end + + def test_beginning_of_quarter + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,2,15,10,10,10).beginning_of_quarter + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,1,1,0,0,0).beginning_of_quarter + assert_equal date_time_init(2005,10,1,0,0,0), date_time_init(2005,12,31,10,10,10).beginning_of_quarter + assert_equal date_time_init(2005,4,1,0,0,0), date_time_init(2005,6,30,23,59,59).beginning_of_quarter + end + + def test_end_of_quarter + assert_equal date_time_init(2007,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,2,15,10,10,10).end_of_quarter + assert_equal date_time_init(2007,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,3,31,0,0,0).end_of_quarter + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,21,10,10,10).end_of_quarter + assert_equal date_time_init(2007,6,30,23,59,59,Rational(999999999, 1000)), date_time_init(2007,4,1,0,0,0).end_of_quarter + assert_equal date_time_init(2008,6,30,23,59,59,Rational(999999999, 1000)), date_time_init(2008,5,31,0,0,0).end_of_quarter + end + + def test_beginning_of_year + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,2,22,10,10,10).beginning_of_year + end + + def test_next_week + assert_equal date_time_init(2005,2,28,0,0,0), date_time_init(2005,2,22,15,15,10).next_week + assert_equal date_time_init(2005,3,4,0,0,0), date_time_init(2005,2,22,15,15,10).next_week(:friday) + assert_equal date_time_init(2006,10,30,0,0,0), date_time_init(2006,10,23,0,0,0).next_week + assert_equal date_time_init(2006,11,1,0,0,0), date_time_init(2006,10,23,0,0,0).next_week(:wednesday) + end + + def test_next_month_on_31st + assert_equal date_time_init(2005,9,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_month + end + + def test_next_quarter_on_31st + assert_equal date_time_init(2005,11,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_quarter + end + + def test_next_year + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).next_year + end + + def test_prev_week + assert_equal date_time_init(2005,2,21,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week + assert_equal date_time_init(2005,2,22,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week(:tuesday) + assert_equal date_time_init(2005,2,25,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week(:friday) + assert_equal date_time_init(2006,10,30,0,0,0), date_time_init(2006,11,6,0,0,0).prev_week + assert_equal date_time_init(2006,11,15,0,0,0), date_time_init(2006,11,23,0,0,0).prev_week(:wednesday) + end + + def test_prev_month_on_31st + assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,3,31,10,10,10).prev_month + end + + def test_prev_quarter_on_31st + assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,5,31,10,10,10).prev_quarter + end + + def test_prev_year + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).prev_year + end + + def test_days_to_week_start + assert_equal 0, date_time_init(2011,11,01,0,0,0).days_to_week_start(:tuesday) + assert_equal 1, date_time_init(2011,11,02,0,0,0).days_to_week_start(:tuesday) + assert_equal 2, date_time_init(2011,11,03,0,0,0).days_to_week_start(:tuesday) + assert_equal 3, date_time_init(2011,11,04,0,0,0).days_to_week_start(:tuesday) + assert_equal 4, date_time_init(2011,11,05,0,0,0).days_to_week_start(:tuesday) + assert_equal 5, date_time_init(2011,11,06,0,0,0).days_to_week_start(:tuesday) + assert_equal 6, date_time_init(2011,11,07,0,0,0).days_to_week_start(:tuesday) + + assert_equal 3, date_time_init(2011,11,03,0,0,0).days_to_week_start(:monday) + assert_equal 3, date_time_init(2011,11,04,0,0,0).days_to_week_start(:tuesday) + assert_equal 3, date_time_init(2011,11,05,0,0,0).days_to_week_start(:wednesday) + assert_equal 3, date_time_init(2011,11,06,0,0,0).days_to_week_start(:thursday) + assert_equal 3, date_time_init(2011,11,07,0,0,0).days_to_week_start(:friday) + assert_equal 3, date_time_init(2011,11,8,0,0,0).days_to_week_start(:saturday) + assert_equal 3, date_time_init(2011,11,9,0,0,0).days_to_week_start(:sunday) + end + + def test_beginning_of_week + assert_equal date_time_init(2005,1,31,0,0,0), date_time_init(2005,2,4,10,10,10).beginning_of_week + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,28,0,0,0).beginning_of_week #monday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,29,0,0,0).beginning_of_week #tuesday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,30,0,0,0).beginning_of_week #wednesday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,01,0,0,0).beginning_of_week #thursday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,02,0,0,0).beginning_of_week #friday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,03,0,0,0).beginning_of_week #saturday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,04,0,0,0).beginning_of_week #sunday + end + + def test_end_of_week + assert_equal date_time_init(2008,1,6,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,31,10,10,10).end_of_week + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,27,0,0,0).end_of_week #monday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,28,0,0,0).end_of_week #tuesday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,29,0,0,0).end_of_week #wednesday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,30,0,0,0).end_of_week #thursday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,31,0,0,0).end_of_week #friday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,9,01,0,0,0).end_of_week #saturday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,9,02,0,0,0).end_of_week #sunday + end + + def test_end_of_month + assert_equal date_time_init(2005,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2005,3,20,10,10,10).end_of_month + assert_equal date_time_init(2005,2,28,23,59,59,Rational(999999999, 1000)), date_time_init(2005,2,20,10,10,10).end_of_month + assert_equal date_time_init(2005,4,30,23,59,59,Rational(999999999, 1000)), date_time_init(2005,4,20,10,10,10).end_of_month + end + + def test_end_of_year + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,2,22,10,10,10).end_of_year + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,31,10,10,10).end_of_year + end +end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 088b74a29a..7ae1f67785 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -1,7 +1,22 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class DateExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,*args) + Date.new(year,month,day) + end + + include DateAndTimeBehavior + + def test_yesterday_in_calendar_reform + assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday + end + + def test_tomorrow_in_calendar_reform + assert_equal Date.new(1582,10,15), Date.new(1582,10,4).tomorrow + end + def test_to_s date = Date.new(2005, 2, 21) assert_equal "2005-02-21", date.to_s @@ -46,22 +61,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2005,6,22), Date.new(2005,2,22).change(:month => 6) end - def test_beginning_of_week - assert_equal Date.new(2005,1,31), Date.new(2005,2,4).beginning_of_week - assert_equal Date.new(2005,11,28), Date.new(2005,11,28).beginning_of_week #monday - assert_equal Date.new(2005,11,28), Date.new(2005,11,29).beginning_of_week #tuesday - assert_equal Date.new(2005,11,28), Date.new(2005,11,30).beginning_of_week #wednesday - assert_equal Date.new(2005,11,28), Date.new(2005,12,01).beginning_of_week #thursday - assert_equal Date.new(2005,11,28), Date.new(2005,12,02).beginning_of_week #friday - assert_equal Date.new(2005,11,28), Date.new(2005,12,03).beginning_of_week #saturday - assert_equal Date.new(2005,11,28), Date.new(2005,12,04).beginning_of_week #sunday - end - - def test_monday - assert_equal Date.new(2005,11,28), Date.new(2005,11,28).monday - assert_equal Date.new(2005,11,28), Date.new(2005,12,01).monday - end - def test_sunday assert_equal Date.new(2008,3,2), Date.new(2008,3,02).sunday assert_equal Date.new(2008,3,2), Date.new(2008,2,29).sunday @@ -71,41 +70,10 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,1), Date.new(1582,10,15).beginning_of_week #friday end - def test_beginning_of_month - assert_equal Date.new(2005,2,1), Date.new(2005,2,22).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal Date.new(2005,1,1), Date.new(2005,2,15).beginning_of_quarter - assert_equal Date.new(2005,1,1), Date.new(2005,1,1).beginning_of_quarter - assert_equal Date.new(2005,10,1), Date.new(2005,12,31).beginning_of_quarter - assert_equal Date.new(2005,4,1), Date.new(2005,6,30).beginning_of_quarter - end - - def test_end_of_week - assert_equal Date.new(2008,2,24), Date.new(2008,2,22).end_of_week - assert_equal Date.new(2008,3,2), Date.new(2008,2,25).end_of_week #monday - assert_equal Date.new(2008,3,2), Date.new(2008,2,26).end_of_week #tuesday - assert_equal Date.new(2008,3,2), Date.new(2008,2,27).end_of_week #wednesday - assert_equal Date.new(2008,3,2), Date.new(2008,2,28).end_of_week #thursday - assert_equal Date.new(2008,3,2), Date.new(2008,2,29).end_of_week #friday - assert_equal Date.new(2008,3,2), Date.new(2008,3,01).end_of_week #saturday - assert_equal Date.new(2008,3,2), Date.new(2008,3,02).end_of_week #sunday - end - def test_end_of_week_in_calendar_reform assert_equal Date.new(1582,10,17), Date.new(1582,10,4).end_of_week #thursday end - def test_end_of_quarter - assert_equal Date.new(2008,3,31), Date.new(2008,2,15).end_of_quarter - assert_equal Date.new(2008,3,31), Date.new(2008,3,31).end_of_quarter - assert_equal Date.new(2008,12,31), Date.new(2008,10,8).end_of_quarter - assert_equal Date.new(2008,6,30), Date.new(2008,4,14).end_of_quarter - assert_equal Date.new(2008,6,30), Date.new(2008,5,31).end_of_quarter - assert_equal Date.new(2008,9,30), Date.new(2008,8,21).end_of_quarter - end - def test_end_of_year assert_equal Date.new(2008,12,31).to_s, Date.new(2008,2,22).end_of_year.to_s end @@ -116,57 +84,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2005,4,30), Date.new(2005,4,20).end_of_month end - def test_beginning_of_year - assert_equal Date.new(2005,1,1).to_s, Date.new(2005,2,22).beginning_of_year.to_s - end - - def test_weeks_ago - assert_equal Date.new(2005,5,10), Date.new(2005,5,17).weeks_ago(1) - assert_equal Date.new(2005,5,10), Date.new(2005,5,24).weeks_ago(2) - assert_equal Date.new(2005,5,10), Date.new(2005,5,31).weeks_ago(3) - assert_equal Date.new(2005,5,10), Date.new(2005,6,7).weeks_ago(4) - assert_equal Date.new(2006,12,31), Date.new(2007,2,4).weeks_ago(5) - end - - def test_months_ago - assert_equal Date.new(2005,5,5), Date.new(2005,6,5).months_ago(1) - assert_equal Date.new(2004,11,5), Date.new(2005,6,5).months_ago(7) - assert_equal Date.new(2004,12,5), Date.new(2005,6,5).months_ago(6) - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).months_ago(12) - assert_equal Date.new(2003,6,5), Date.new(2005,6,5).months_ago(24) - end - - def test_months_since - assert_equal Date.new(2005,7,5), Date.new(2005,6,5).months_since(1) - assert_equal Date.new(2006,1,5), Date.new(2005,12,5).months_since(1) - assert_equal Date.new(2005,12,5), Date.new(2005,6,5).months_since(6) - assert_equal Date.new(2006,6,5), Date.new(2005,12,5).months_since(6) - assert_equal Date.new(2006,1,5), Date.new(2005,6,5).months_since(7) - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).months_since(12) - assert_equal Date.new(2007,6,5), Date.new(2005,6,5).months_since(24) - assert_equal Date.new(2005,4,30), Date.new(2005,3,31).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,29).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,30).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,31).months_since(1) - end - - def test_years_ago - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).years_ago(1) - assert_equal Date.new(1998,6,5), Date.new(2005,6,5).years_ago(7) - assert_equal Date.new(2003,2,28), Date.new(2004,2,29).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).years_since(1) - assert_equal Date.new(2012,6,5), Date.new(2005,6,5).years_since(7) - assert_equal Date.new(2182,6,5), Date.new(2005,6,5).years_since(177) - assert_equal Date.new(2005,2,28), Date.new(2004,2,29).years_since(1) # 1 year since leap day - end - - def test_prev_year - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).prev_year - end - def test_prev_year_in_leap_years assert_equal Date.new(1999,2,28), Date.new(2000,2,29).prev_year end @@ -187,10 +104,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year end - def test_next_year - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).next_year - end - def test_next_year_in_leap_years assert_equal Date.new(2001,2,28), Date.new(2000,2,29).next_year end @@ -199,24 +112,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,4), Date.new(1581,10,10).next_year end - def test_yesterday - assert_equal Date.new(2005,2,21), Date.new(2005,2,22).yesterday - assert_equal Date.new(2005,2,28), Date.new(2005,3,2).yesterday.yesterday - end - - def test_yesterday_in_calendar_reform - assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday - end - - def test_tomorrow - assert_equal Date.new(2005,2,23), Date.new(2005,2,22).tomorrow - assert_equal Date.new(2005,3,2), Date.new(2005,2,28).tomorrow.tomorrow - end - - def test_tomorrow_in_calendar_reform - assert_equal Date.new(1582,10,15), Date.new(1582,10,4).tomorrow - end - def test_advance assert_equal Date.new(2006,2,28), Date.new(2005,2,28).advance(:years => 1) assert_equal Date.new(2005,6,28), Date.new(2005,2,28).advance(:months => 4) @@ -249,14 +144,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_prev_week - assert_equal Date.new(2005,5,9), Date.new(2005,5,17).prev_week - assert_equal Date.new(2006,12,25), Date.new(2007,1,7).prev_week - assert_equal Date.new(2010,2,12), Date.new(2010,2,19).prev_week(:friday) - assert_equal Date.new(2010,2,13), Date.new(2010,2,19).prev_week(:saturday) - assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday) - end - def test_last_week assert_equal Date.new(2005,5,9), Date.new(2005,5,17).last_week assert_equal Date.new(2006,12,25), Date.new(2007,1,7).last_week @@ -265,38 +152,15 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2010,2,27), Date.new(2010,3,4).last_week(:saturday) end - def test_next_week - assert_equal Date.new(2005,2,28), Date.new(2005,2,22).next_week - assert_equal Date.new(2005,3,4), Date.new(2005,2,22).next_week(:friday) - assert_equal Date.new(2006,10,30), Date.new(2006,10,23).next_week - assert_equal Date.new(2006,11,1), Date.new(2006,10,23).next_week(:wednesday) - end - def test_next_week_in_calendar_reform assert_equal Date.new(1582,10,15), Date.new(1582,9,30).next_week(:friday) assert_equal Date.new(1582,10,18), Date.new(1582,10,4).next_week end - def test_next_month_on_31st - assert_equal Date.new(2005, 9, 30), Date.new(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month end - def test_next_quarter_on_31st - assert_equal Date.new(2005, 11, 30), Date.new(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).last_quarter end @@ -420,13 +284,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_today - Date.stubs(:current).returns(Date.new(2000, 1, 1)) - assert_equal false, Date.new(1999, 12, 31).today? - assert_equal true, Date.new(2000,1,1).today? - assert_equal false, Date.new(2000,1,2).today? - end - def test_past Date.stubs(:current).returns(Date.new(2000, 1, 1)) assert_equal true, Date.new(1999, 12, 31).past? diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 21b7efdc73..b1d1e8ecb4 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -1,7 +1,14 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class DateTimeExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,hour,minute,second,*args) + DateTime.civil(year,month,day,hour,minute,second) + end + + include DateAndTimeBehavior + def test_to_s datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0) assert_equal "2005-02-21 14:30:00", datetime.to_s(:db) @@ -54,35 +61,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal 86399,DateTime.civil(2005,1,1,23,59,59).seconds_since_midnight end - def test_days_to_week_start - assert_equal 0, Time.local(2011,11,01,0,0,0).days_to_week_start(:tuesday) - assert_equal 1, Time.local(2011,11,02,0,0,0).days_to_week_start(:tuesday) - assert_equal 2, Time.local(2011,11,03,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 4, Time.local(2011,11,05,0,0,0).days_to_week_start(:tuesday) - assert_equal 5, Time.local(2011,11,06,0,0,0).days_to_week_start(:tuesday) - assert_equal 6, Time.local(2011,11,07,0,0,0).days_to_week_start(:tuesday) - - assert_equal 3, Time.local(2011,11,03,0,0,0).days_to_week_start(:monday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,05,0,0,0).days_to_week_start(:wednesday) - assert_equal 3, Time.local(2011,11,06,0,0,0).days_to_week_start(:thursday) - assert_equal 3, Time.local(2011,11,07,0,0,0).days_to_week_start(:friday) - assert_equal 3, Time.local(2011,11,8,0,0,0).days_to_week_start(:saturday) - assert_equal 3, Time.local(2011,11,9,0,0,0).days_to_week_start(:sunday) - end - - def test_beginning_of_week - assert_equal DateTime.civil(2005,1,31), DateTime.civil(2005,2,4,10,10,10).beginning_of_week - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,28,0,0,0).beginning_of_week #monday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,29,0,0,0).beginning_of_week #tuesday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,30,0,0,0).beginning_of_week #wednesday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,01,0,0,0).beginning_of_week #thursday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,02,0,0,0).beginning_of_week #friday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,03,0,0,0).beginning_of_week #saturday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,04,0,0,0).beginning_of_week #sunday - end - def test_beginning_of_day assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day end @@ -99,82 +77,16 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2005,2,4,19,59,59), DateTime.civil(2005,2,4,19,30,10).end_of_hour end - def test_beginning_of_month - assert_equal DateTime.civil(2005,2,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,15,10,10,10).beginning_of_quarter - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,1,1,0,0,0).beginning_of_quarter - assert_equal DateTime.civil(2005,10,1,0,0,0), DateTime.civil(2005,12,31,10,10,10).beginning_of_quarter - assert_equal DateTime.civil(2005,4,1,0,0,0), DateTime.civil(2005,6,30,23,59,59).beginning_of_quarter - end - def test_end_of_month assert_equal DateTime.civil(2005,3,31,23,59,59), DateTime.civil(2005,3,20,10,10,10).end_of_month assert_equal DateTime.civil(2005,2,28,23,59,59), DateTime.civil(2005,2,20,10,10,10).end_of_month assert_equal DateTime.civil(2005,4,30,23,59,59), DateTime.civil(2005,4,20,10,10,10).end_of_month end - def test_beginning_of_year - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_year - end - - def test_weeks_ago - assert_equal DateTime.civil(2005,5,29,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(1) - assert_equal DateTime.civil(2005,5,1,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(5) - assert_equal DateTime.civil(2005,4,24,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(6) - assert_equal DateTime.civil(2005,2,27,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(14) - assert_equal DateTime.civil(2004,12,25,10), DateTime.civil(2005,1,1,10,0,0).weeks_ago(1) - end - - def test_months_ago - assert_equal DateTime.civil(2005,5,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(1) - assert_equal DateTime.civil(2004,11,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(7) - assert_equal DateTime.civil(2004,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(6) - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(12) - assert_equal DateTime.civil(2003,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(24) - end - - def test_months_since - assert_equal DateTime.civil(2005,7,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(1) - assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(6) - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(6) - assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(7) - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(12) - assert_equal DateTime.civil(2007,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(24) - assert_equal DateTime.civil(2005,4,30,10), DateTime.civil(2005,3,31,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,29,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,30,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,31,10,0,0).months_since(1) - end - - def test_years_ago - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(1) - assert_equal DateTime.civil(1998,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(7) - assert_equal DateTime.civil(2003,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(1) - assert_equal DateTime.civil(2012,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(7) - assert_equal DateTime.civil(2182,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(177) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_since(1) # 1 year since leap day - end - - def test_prev_year - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year - end - def test_last_year assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year end - def test_next_year - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).next_year - end - def test_ago assert_equal DateTime.civil(2005,2,22,10,10,9), DateTime.civil(2005,2,22,10,10,10).ago(1) assert_equal DateTime.civil(2005,2,22,9,10,10), DateTime.civil(2005,2,22,10,10,10).ago(3600) @@ -191,16 +103,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2005,2,22,10,10,12), DateTime.civil(2005,2,22,10,10,10).since(1.667) end - def test_yesterday - assert_equal DateTime.civil(2005,2,21,10,10,10), DateTime.civil(2005,2,22,10,10,10).yesterday - assert_equal DateTime.civil(2005,2,28,10,10,10), DateTime.civil(2005,3,2,10,10,10).yesterday.yesterday - end - - def test_tomorrow - assert_equal DateTime.civil(2005,2,23,10,10,10), DateTime.civil(2005,2,22,10,10,10).tomorrow - assert_equal DateTime.civil(2005,3,2,10,10,10), DateTime.civil(2005,2,28,10,10,10).tomorrow.tomorrow - end - def test_change assert_equal DateTime.civil(2006,2,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:year => 2006) assert_equal DateTime.civil(2005,6,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:month => 6) @@ -236,14 +138,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 22, 58, 59).advance(:months => 1, :hours => 1, :minutes => 1, :seconds => 1) end - def test_prev_week - assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).prev_week - assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).prev_week(:tuesday) - assert_equal DateTime.civil(2005,2,25), DateTime.civil(2005,3,1,15,15,10).prev_week(:friday) - assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,11,6,0,0,0).prev_week - assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).prev_week(:wednesday) - end - def test_last_week assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).last_week assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).last_week(:tuesday) @@ -252,33 +146,10 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).last_week(:wednesday) end - def test_next_week - assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week - assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday) - assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,10,23,0,0,0).next_week - assert_equal DateTime.civil(2006,11,1), DateTime.civil(2006,10,23,0,0,0).next_week(:wednesday) - end - - def test_next_month_on_31st - assert_equal DateTime.civil(2005, 9, 30), DateTime.civil(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month end - def test_next_quarter_on_31st - assert_equal DateTime.civil(2005, 11, 30), DateTime.civil(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).last_quarter end diff --git a/activesupport/test/core_ext/file_test.rb b/activesupport/test/core_ext/file_test.rb index 128e956a8c..2c04e9687c 100644 --- a/activesupport/test/core_ext/file_test.rb +++ b/activesupport/test/core_ext/file_test.rb @@ -51,7 +51,7 @@ class AtomicWriteTest < ActiveSupport::TestCase assert !File.exist?(file_name) end assert File.exist?(file_name) - assert_equal 0100666 & ~File.umask, file_mode + assert_equal File.probe_stat_in(Dir.pwd).mode, file_mode assert_equal contents, File.read(file_name) ensure File.unlink(file_name) rescue nil diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 4dc9f57038..37fdf1c0af 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -428,6 +428,29 @@ class HashExtTest < ActiveSupport::TestCase assert_equal 2, hash['b'] end + def test_indifferent_merging_with_block + hash = HashWithIndifferentAccess.new + hash[:a] = 1 + hash['b'] = 3 + + other = { 'a' => 4, :b => 2, 'c' => 10 } + + merged = hash.merge(other) { |key, old, new| old > new ? old : new } + + assert_equal HashWithIndifferentAccess, merged.class + assert_equal 4, merged[:a] + assert_equal 3, merged['b'] + assert_equal 10, merged[:c] + + other_indifferent = HashWithIndifferentAccess.new('a' => 9, :b => 2) + + merged = hash.merge(other_indifferent) { |key, old, new| old + new } + + assert_equal HashWithIndifferentAccess, merged.class + assert_equal 10, merged[:a] + assert_equal 5, merged[:b] + end + def test_indifferent_reverse_merging hash = HashWithIndifferentAccess.new('some' => 'value', 'other' => 'value') hash.reverse_merge!(:some => 'noclobber', :another => 'clobber') diff --git a/activesupport/test/core_ext/module/qualified_const_test.rb b/activesupport/test/core_ext/module/qualified_const_test.rb index 8af0b9a023..343a848a42 100644 --- a/activesupport/test/core_ext/module/qualified_const_test.rb +++ b/activesupport/test/core_ext/module/qualified_const_test.rb @@ -67,17 +67,24 @@ class QualifiedConstTest < ActiveSupport::TestCase end test "qualified_const_set" do - m = Module.new - assert_equal m, Object.qualified_const_set("QualifiedConstTestMod2", m) - assert_equal m, ::QualifiedConstTestMod2 - - # We are going to assign to existing constants on purpose, so silence warnings. - silence_warnings do - assert_equal true, QualifiedConstTestMod.qualified_const_set("QualifiedConstTestMod::X", true) - assert_equal true, QualifiedConstTestMod::X - - assert_equal 10, QualifiedConstTestMod::M.qualified_const_set("X", 10) - assert_equal 10, QualifiedConstTestMod::M::X + begin + m = Module.new + assert_equal m, Object.qualified_const_set("QualifiedConstTestMod2", m) + assert_equal m, ::QualifiedConstTestMod2 + + # We are going to assign to existing constants on purpose, so silence warnings. + silence_warnings do + assert_equal true, QualifiedConstTestMod.qualified_const_set("QualifiedConstTestMod::X", true) + assert_equal true, QualifiedConstTestMod::X + + assert_equal 10, QualifiedConstTestMod::M.qualified_const_set("X", 10) + assert_equal 10, QualifiedConstTestMod::M::X + end + ensure + silence_warnings do + QualifiedConstTestMod.qualified_const_set('QualifiedConstTestMod::X', false) + QualifiedConstTestMod::M.qualified_const_set('X', 1) + end end 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 98ab82609e..ec7dd6d4fb 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -99,13 +99,25 @@ class ObjectTryTest < ActiveSupport::TestCase def test_nonexisting_method method = :undefined_method assert !@string.respond_to?(method) - assert_raise(NoMethodError) { @string.try(method) } + assert_nil @string.try(method) end def test_nonexisting_method_with_arguments method = :undefined_method assert !@string.respond_to?(method) - assert_raise(NoMethodError) { @string.try(method, 'llo', 'y') } + assert_nil @string.try(method, 'llo', 'y') + end + + def test_nonexisting_method_bang + method = :undefined_method + assert !@string.respond_to?(method) + assert_raise(NoMethodError) { @string.try!(method) } + end + + def test_nonexisting_method_with_arguments_bang + method = :undefined_method + assert !@string.respond_to?(method) + assert_raise(NoMethodError) { @string.try!(method, 'llo', 'y') } end def test_valid_method @@ -139,6 +151,18 @@ class ObjectTryTest < ActiveSupport::TestCase assert_equal false, ran end + def test_try_with_private_method_bang + klass = Class.new do + private + + def private_method + 'private method' + end + end + + assert_raise(NoMethodError) { klass.new.try!(:private_method) } + end + def test_try_with_private_method klass = Class.new do private @@ -148,6 +172,6 @@ class ObjectTryTest < ActiveSupport::TestCase end end - assert_raise(NoMethodError) { klass.new.try(:private_method) } + assert_nil klass.new.try(:private_method) end end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index e5b774425e..dc5ae0eafc 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -9,6 +9,7 @@ require 'active_support/core_ext/string' require 'active_support/time' require 'active_support/core_ext/string/strip' require 'active_support/core_ext/string/output_safety' +require 'active_support/core_ext/string/indent' module Ace module Base @@ -498,8 +499,8 @@ class OutputSafetyTest < ActiveSupport::TestCase end test "ERB::Util.html_escape should escape unsafe characters" do - string = '<>&"' - expected = '<>&"' + string = '<>&"\'' + expected = '<>&"'' assert_equal expected, ERB::Util.html_escape(string) end @@ -521,3 +522,58 @@ class StringExcludeTest < ActiveSupport::TestCase assert_equal true, 'foo'.exclude?('p') end end + +class StringIndentTest < ActiveSupport::TestCase + test 'does not indent strings that only contain newlines (edge cases)' do + ['', "\n", "\n" * 7].each do |str| + assert_nil str.indent!(8) + assert_equal str, str.indent(8) + assert_equal str, str.indent(1, "\t") + end + end + + test "by default, indents with spaces if the existing indentation uses them" do + assert_equal " foo\n bar", "foo\n bar".indent(4) + end + + test "by default, indents with tabs if the existing indentation uses them" do + assert_equal "\tfoo\n\t\t\bar", "foo\n\t\bar".indent(1) + end + + test "by default, indents with spaces as a fallback if there is no indentation" do + assert_equal " foo\n bar\n baz", "foo\nbar\nbaz".indent(3) + end + + # Nothing is said about existing indentation that mixes spaces and tabs, so + # there is nothing to test. + + test 'uses the indent char if passed' do + assert_equal <<EXPECTED, <<ACTUAL.indent(4, '.') +.... def some_method(x, y) +.... some_code +.... end +EXPECTED + def some_method(x, y) + some_code + end +ACTUAL + + assert_equal <<EXPECTED, <<ACTUAL.indent(2, ' ') + def some_method(x, y) + some_code + end +EXPECTED + def some_method(x, y) + some_code + end +ACTUAL + end + + test "does not indent blank lines by default" do + assert_equal " foo\n\n bar", "foo\n\nbar".indent(1) + end + + test 'indents blank lines if told so' do + assert_equal " foo\n \n bar", "foo\n\nbar".indent(1, nil, true) + end +end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 412aef9301..6d6757a1b6 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -1,7 +1,14 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class TimeExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,hour,minute,second,usec=0) + Time.local(year,month,day,hour,minute,second,usec) + end + + include DateAndTimeBehavior + def test_seconds_since_midnight assert_equal 1,Time.local(2005,1,1,0,0,1).seconds_since_midnight assert_equal 60,Time.local(2005,1,1,0,1,0).seconds_since_midnight @@ -50,37 +57,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_beginning_of_week - assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week - assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday - assert_equal Time.local(2005,11,28), Time.local(2005,11,29,0,0,0).beginning_of_week #tuesday - assert_equal Time.local(2005,11,28), Time.local(2005,11,30,0,0,0).beginning_of_week #wednesday - assert_equal Time.local(2005,11,28), Time.local(2005,12,01,0,0,0).beginning_of_week #thursday - assert_equal Time.local(2005,11,28), Time.local(2005,12,02,0,0,0).beginning_of_week #friday - assert_equal Time.local(2005,11,28), Time.local(2005,12,03,0,0,0).beginning_of_week #saturday - assert_equal Time.local(2005,11,28), Time.local(2005,12,04,0,0,0).beginning_of_week #sunday - - end - - def test_days_to_week_start - assert_equal 0, Time.local(2011,11,01,0,0,0).days_to_week_start(:tuesday) - assert_equal 1, Time.local(2011,11,02,0,0,0).days_to_week_start(:tuesday) - assert_equal 2, Time.local(2011,11,03,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 4, Time.local(2011,11,05,0,0,0).days_to_week_start(:tuesday) - assert_equal 5, Time.local(2011,11,06,0,0,0).days_to_week_start(:tuesday) - assert_equal 6, Time.local(2011,11,07,0,0,0).days_to_week_start(:tuesday) - - assert_equal 3, Time.local(2011,11,03,0,0,0).days_to_week_start(:monday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,05,0,0,0).days_to_week_start(:wednesday) - assert_equal 3, Time.local(2011,11,06,0,0,0).days_to_week_start(:thursday) - assert_equal 3, Time.local(2011,11,07,0,0,0).days_to_week_start(:friday) - assert_equal 3, Time.local(2011,11,8,0,0,0).days_to_week_start(:saturday) - assert_equal 3, Time.local(2011,11,9,0,0,0).days_to_week_start(:sunday) - end - - def test_beginning_of_day assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day with_env_tz 'US/Eastern' do @@ -97,17 +73,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour end - def test_beginning_of_month - assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,15,10,10,10).beginning_of_quarter - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,1,1,0,0,0).beginning_of_quarter - assert_equal Time.local(2005,10,1,0,0,0), Time.local(2005,12,31,10,10,10).beginning_of_quarter - assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter - end - def test_end_of_day assert_equal Time.local(2007,8,12,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,12,10,10,10).end_of_day with_env_tz 'US/Eastern' do @@ -120,100 +85,14 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_end_of_week - assert_equal Time.local(2008,1,6,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_week - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,27,0,0,0).end_of_week #monday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,28,0,0,0).end_of_week #tuesday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,29,0,0,0).end_of_week #wednesday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,30,0,0,0).end_of_week #thursday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,31,0,0,0).end_of_week #friday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,01,0,0,0).end_of_week #saturday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,02,0,0,0).end_of_week #sunday - end - def test_end_of_hour assert_equal Time.local(2005,2,4,19,59,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_hour end - def test_end_of_month - assert_equal Time.local(2005,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2005,3,20,10,10,10).end_of_month - assert_equal Time.local(2005,2,28,23,59,59,Rational(999999999, 1000)), Time.local(2005,2,20,10,10,10).end_of_month - assert_equal Time.local(2005,4,30,23,59,59,Rational(999999999, 1000)), Time.local(2005,4,20,10,10,10).end_of_month - end - - def test_end_of_quarter - assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,15,10,10,10).end_of_quarter - assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,3,31,0,0,0).end_of_quarter - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,21,10,10,10).end_of_quarter - assert_equal Time.local(2007,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2007,4,1,0,0,0).end_of_quarter - assert_equal Time.local(2008,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2008,5,31,0,0,0).end_of_quarter - end - - def test_end_of_year - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,22,10,10,10).end_of_year - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_year - end - - def test_beginning_of_year - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year - end - - def test_weeks_ago - assert_equal Time.local(2005,5,29,10), Time.local(2005,6,5,10,0,0).weeks_ago(1) - assert_equal Time.local(2005,5,1,10), Time.local(2005,6,5,10,0,0).weeks_ago(5) - assert_equal Time.local(2005,4,24,10), Time.local(2005,6,5,10,0,0).weeks_ago(6) - assert_equal Time.local(2005,2,27,10), Time.local(2005,6,5,10,0,0).weeks_ago(14) - assert_equal Time.local(2004,12,25,10), Time.local(2005,1,1,10,0,0).weeks_ago(1) - end - - def test_months_ago - assert_equal Time.local(2005,5,5,10), Time.local(2005,6,5,10,0,0).months_ago(1) - assert_equal Time.local(2004,11,5,10), Time.local(2005,6,5,10,0,0).months_ago(7) - assert_equal Time.local(2004,12,5,10), Time.local(2005,6,5,10,0,0).months_ago(6) - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(12) - assert_equal Time.local(2003,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(24) - end - - def test_months_since - assert_equal Time.local(2005,7,5,10), Time.local(2005,6,5,10,0,0).months_since(1) - assert_equal Time.local(2006,1,5,10), Time.local(2005,12,5,10,0,0).months_since(1) - assert_equal Time.local(2005,12,5,10), Time.local(2005,6,5,10,0,0).months_since(6) - assert_equal Time.local(2006,6,5,10), Time.local(2005,12,5,10,0,0).months_since(6) - assert_equal Time.local(2006,1,5,10), Time.local(2005,6,5,10,0,0).months_since(7) - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).months_since(12) - assert_equal Time.local(2007,6,5,10), Time.local(2005,6,5,10,0,0).months_since(24) - assert_equal Time.local(2005,4,30,10), Time.local(2005,3,31,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,29,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,30,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,31,10,0,0).months_since(1) - end - - def test_years_ago - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(1) - assert_equal Time.local(1998,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(7) - assert_equal Time.local(2003,2,28,10), Time.local(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).years_since(1) - assert_equal Time.local(2012,6,5,10), Time.local(2005,6,5,10,0,0).years_since(7) - assert_equal Time.local(2005,2,28,10), Time.local(2004,2,29,10,0,0).years_since(1) # 1 year since leap day - # Failure because of size limitations of numeric? - # assert_equal Time.local(2182,6,5,10), Time.local(2005,6,5,10,0,0).years_since(177) - end - - def test_prev_year - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year - end - def test_last_year assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year end - def test_next_year - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).next_year - end - def test_ago assert_equal Time.local(2005,2,22,10,10,9), Time.local(2005,2,22,10,10,10).ago(1) assert_equal Time.local(2005,2,22,9,10,10), Time.local(2005,2,22,10,10,10).ago(3600) @@ -426,16 +305,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_yesterday - assert_equal Time.local(2005,2,21,10,10,10), Time.local(2005,2,22,10,10,10).yesterday - assert_equal Time.local(2005,2,28,10,10,10), Time.local(2005,3,2,10,10,10).yesterday.yesterday - end - - def test_tomorrow - assert_equal Time.local(2005,2,23,10,10,10), Time.local(2005,2,22,10,10,10).tomorrow - assert_equal Time.local(2005,3,2,10,10,10), Time.local(2005,2,28,10,10,10).tomorrow.tomorrow - end - def test_change assert_equal Time.local(2006,2,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2006) assert_equal Time.local(2005,6,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:month => 6) @@ -539,16 +408,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal t, t.advance(:months => 0) end - def test_prev_week - with_env_tz 'US/Eastern' do - assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).prev_week - assert_equal Time.local(2005,2,22), Time.local(2005,3,1,15,15,10).prev_week(:tuesday) - assert_equal Time.local(2005,2,25), Time.local(2005,3,1,15,15,10).prev_week(:friday) - assert_equal Time.local(2006,10,30), Time.local(2006,11,6,0,0,0).prev_week - assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).prev_week(:wednesday) - end - end - def test_last_week with_env_tz 'US/Eastern' do assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week @@ -559,16 +418,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_next_week - with_env_tz 'US/Eastern' do - assert_equal Time.local(2005,2,28), Time.local(2005,2,22,15,15,10).next_week - assert_equal Time.local(2005,3,1), Time.local(2005,2,22,15,15,10).next_week(:tuesday) - assert_equal Time.local(2005,3,4), Time.local(2005,2,22,15,15,10).next_week(:friday) - assert_equal Time.local(2006,10,30), Time.local(2006,10,23,0,0,0).next_week - assert_equal Time.local(2006,11,1), Time.local(2006,10,23,0,0,0).next_week(:wednesday) - end - end - def test_next_week_near_daylight_start with_env_tz 'US/Eastern' do assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight' @@ -709,14 +558,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_next_month_on_31st - assert_equal Time.local(2005, 9, 30), Time.local(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month end @@ -938,15 +779,6 @@ class TimeExtMarshalingTest < ActiveSupport::TestCase assert_equal t, unmarshaled end - - def test_next_quarter_on_31st - assert_equal Time.local(2005, 11, 30), Time.local(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 69829bcda5..e5bc806397 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -145,6 +145,12 @@ class DependenciesTest < ActiveSupport::TestCase end end + def test_circular_autoloading_detection + with_autoloading_fixtures do + assert_raise(RuntimeError, "Circular dependency detected while autoloading constant Circular1") { Circular1 } + end + end + def test_module_loading with_autoloading_fixtures do assert_kind_of Module, A @@ -679,6 +685,8 @@ class DependenciesTest < ActiveSupport::TestCase assert_equal true, M.unloadable assert_equal false, M.unloadable end + ensure + Object.class_eval { remove_const :M } end def test_unloadable_constants_should_receive_callback diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 1f32e4ff92..aa41e57928 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -169,11 +169,11 @@ class InflectorTest < ActiveSupport::TestCase def test_underscore_acronym_sequence ActiveSupport::Inflector.inflections do |inflect| inflect.acronym("API") - inflect.acronym("HTML5") + inflect.acronym("JSON") inflect.acronym("HTML") end - assert_equal("html5_html_api", ActiveSupport::Inflector.underscore("HTML5HTMLAPI")) + assert_equal("json_html_api", ActiveSupport::Inflector.underscore("JSONHTMLAPI")) end def test_underscore @@ -354,6 +354,35 @@ class InflectorTest < ActiveSupport::TestCase RUBY end + def test_inflector_locality + ActiveSupport::Inflector.inflections(:es) do |inflect| + inflect.plural(/$/, 's') + inflect.plural(/z$/i, 'ces') + + inflect.singular(/s$/, '') + inflect.singular(/es$/, '') + + inflect.irregular('el', 'los') + end + + assert_equal('hijos', 'hijo'.pluralize(:es)) + assert_equal('luces', 'luz'.pluralize(:es)) + assert_equal('luzs', 'luz'.pluralize) + + assert_equal('sociedad', 'sociedades'.singularize(:es)) + assert_equal('sociedade', 'sociedades'.singularize) + + assert_equal('los', 'el'.pluralize(:es)) + assert_equal('els', 'el'.pluralize) + + ActiveSupport::Inflector.inflections(:es) { |inflect| inflect.clear } + + assert ActiveSupport::Inflector.inflections(:es).plurals.empty? + assert ActiveSupport::Inflector.inflections(:es).singulars.empty? + assert !ActiveSupport::Inflector.inflections.plurals.empty? + assert !ActiveSupport::Inflector.inflections.singulars.empty? + end + def test_clear_all with_dup do ActiveSupport::Inflector.inflections do |inflect| @@ -467,7 +496,7 @@ class InflectorTest < ActiveSupport::TestCase # there are module functions that access ActiveSupport::Inflector.inflections, # so we need to replace the singleton itself. def with_dup - original = ActiveSupport::Inflector.inflections + original = ActiveSupport::Inflector::Inflections.instance_variable_get(:@__instance__) ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original.dup) ensure ActiveSupport::Inflector::Inflections.instance_variable_set(:@__instance__, original) diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index a947635f4a..7ed71f9abc 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -85,6 +85,13 @@ class TestJSONEncoding < ActiveSupport::TestCase end end + def test_json_variable + assert_deprecated do + assert_equal ActiveSupport::JSON::Variable.new('foo'), 'foo' + assert_equal ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")' + end + end + def test_hash_encoding assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(:a => :b) assert_equal %({\"a\":1}), ActiveSupport::JSON.encode('a' => 1) diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index a8d69d0ec3..ef289692bc 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -110,7 +110,7 @@ class MultibyteCharsUTF8BehaviourTest < ActiveSupport::TestCase end %w{capitalize downcase lstrip reverse rstrip swapcase upcase}.each do |method| - class_eval(<<-EOTESTS) + class_eval(<<-EOTESTS, __FILE__, __LINE__ + 1) def test_#{method}_bang_should_return_self_when_modifying_wrapped_string chars = ' él piDió Un bUen café ' assert_equal chars.object_id, chars.send("#{method}!").object_id diff --git a/activesupport/test/notifications/evented_notification_test.rb b/activesupport/test/notifications/evented_notification_test.rb index f77a0eb3fa..f690ad43fc 100644 --- a/activesupport/test/notifications/evented_notification_test.rb +++ b/activesupport/test/notifications/evented_notification_test.rb @@ -19,6 +19,12 @@ module ActiveSupport end end + class ListenerWithTimedSupport < Listener + def call(name, start, finish, id, payload) + @events << [:call, name, start, finish, id, payload] + end + end + def test_evented_listener notifier = Fanout.new listener = Listener.new @@ -62,6 +68,20 @@ module ActiveSupport [:finish, 'hello', 1, {}], ], listener.events end + + def test_evented_listener_priority + notifier = Fanout.new + listener = ListenerWithTimedSupport.new + notifier.subscribe 'hi', listener + + notifier.start 'hi', 1, {} + notifier.finish 'hi', 1, {} + + assert_equal [ + [:start, 'hi', 1, {}], + [:finish, 'hi', 1, {}] + ], listener.events + end end end end diff --git a/activesupport/test/number_helper_i18n_test.rb b/activesupport/test/number_helper_i18n_test.rb index e07198027b..65aecece71 100644 --- a/activesupport/test/number_helper_i18n_test.rb +++ b/activesupport/test/number_helper_i18n_test.rb @@ -56,6 +56,13 @@ module ActiveSupport assert_equal("-$10.00", number_to_currency(-10, :locale => 'empty')) end + def test_locale_default_format_has_precedence_over_helper_defaults + I18n.backend.store_translations 'ts', + { :number => { :format => { :separator => ";" } } } + + assert_equal("&$ - 10;00", number_to_currency(10, :locale => 'ts')) + end + def test_number_to_currency_without_currency_negative_format I18n.backend.store_translations 'no_negative_format', :number => { :currency => { :format => { :unit => '@', :format => '%n %u' } } @@ -72,11 +79,24 @@ module ActiveSupport assert_equal("1.00", number_to_rounded(1.0, :locale => 'ts')) end + def test_number_with_i18n_precision_and_empty_i18n_store + I18n.backend.store_translations 'empty', {} + + assert_equal("123456789.123", number_to_rounded(123456789.123456789, :locale => 'empty')) + assert_equal("1.000", number_to_rounded(1.0000, :locale => 'empty')) + end + def test_number_with_i18n_delimiter #Delimiter "," and separator "." assert_equal("1,000,000.234", number_to_delimited(1000000.234, :locale => 'ts')) end + def test_number_with_i18n_delimiter_and_empty_i18n_store + I18n.backend.store_translations 'empty', {} + + assert_equal("1,000,000.234", number_to_delimited(1000000.234, :locale => 'empty')) + end + def test_number_to_i18n_percentage # to see if strip_insignificant_zeros is true assert_equal("1%", number_to_percentage(1, :locale => 'ts')) @@ -86,12 +106,27 @@ module ActiveSupport assert_equal("12434%", number_to_percentage(12434, :locale => 'ts')) end + def test_number_to_i18n_percentage_and_empty_i18n_store + I18n.backend.store_translations 'empty', {} + + assert_equal("1.000%", number_to_percentage(1, :locale => 'empty')) + assert_equal("1.243%", number_to_percentage(1.2434, :locale => 'empty')) + assert_equal("12434.000%", number_to_percentage(12434, :locale => 'empty')) + end + def test_number_to_i18n_human_size #b for bytes and k for kbytes assert_equal("2 k", number_to_human_size(2048, :locale => 'ts')) assert_equal("42 b", number_to_human_size(42, :locale => 'ts')) end + def test_number_to_i18n_human_size_with_empty_i18n_store + I18n.backend.store_translations 'empty', {} + + assert_equal("2 KB", number_to_human_size(2048, :locale => 'empty')) + assert_equal("42 Bytes", number_to_human_size(42, :locale => 'empty')) + end + def test_number_to_human_with_default_translation_scope #Using t for thousand assert_equal "2 t", number_to_human(2000, :locale => 'ts') @@ -106,6 +141,13 @@ module ActiveSupport assert_equal "2 Tens", number_to_human(20, :locale => 'ts') end + def test_number_to_human_with_empty_i18n_store + I18n.backend.store_translations 'empty', {} + + assert_equal "2 Thousand", number_to_human(2000, :locale => 'empty') + assert_equal "1.23 Billion", number_to_human(1234567890, :locale => 'empty') + end + def test_number_to_human_with_custom_translation_scope #Significant was set to true with precision 2, with custom translated units assert_equal "4.3 cm", number_to_human(0.0432, :locale => 'ts', :units => :custom_units_for_number_to_human) diff --git a/activesupport/test/number_helper_test.rb b/activesupport/test/number_helper_test.rb index 9b7d7f020c..5f54587f93 100644 --- a/activesupport/test/number_helper_test.rb +++ b/activesupport/test/number_helper_test.rb @@ -4,7 +4,7 @@ require 'active_support/number_helper' module ActiveSupport module NumberHelper class NumberHelperTest < ActiveSupport::TestCase - + class TestClassWithInstanceNumberHelpers include ActiveSupport::NumberHelper end @@ -16,7 +16,7 @@ module ActiveSupport def setup @instance_with_helpers = TestClassWithInstanceNumberHelpers.new end - + def kilobytes(number) number * 1024 end @@ -362,14 +362,13 @@ module ActiveSupport assert_equal "x", number_helper.number_to_human('x') end end - + def test_extending_or_including_number_helper_correctly_hides_private_methods [@instance_with_helpers, TestClassWithClassNumberHelpers, ActiveSupport::NumberHelper].each do |number_helper| assert !number_helper.respond_to?(:valid_float?) assert number_helper.respond_to?(:valid_float?, true) end end - end end end diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index e8defd396b..ac85ba1f21 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -226,12 +226,8 @@ class OrderedHashTest < ActiveSupport::TestCase end def test_alternate_initialization_raises_exception_on_odd_length_args - begin + assert_raises ArgumentError do ActiveSupport::OrderedHash[1,2,3,4,5] - flunk "Hash::[] should have raised an exception on initialization " + - "with an odd number of parameters" - rescue ArgumentError => e - assert_equal "odd number of arguments for Hash", e.message end end diff --git a/activesupport/test/string_inquirer_test.rb b/activesupport/test/string_inquirer_test.rb index bb15916e9e..94d5fe197d 100644 --- a/activesupport/test/string_inquirer_test.rb +++ b/activesupport/test/string_inquirer_test.rb @@ -1,15 +1,23 @@ require 'abstract_unit' class StringInquirerTest < ActiveSupport::TestCase + def setup + @string_inquirer = ActiveSupport::StringInquirer.new('production') + end + def test_match - assert ActiveSupport::StringInquirer.new("production").production? + assert @string_inquirer.production? end def test_miss - assert !ActiveSupport::StringInquirer.new("production").development? + refute @string_inquirer.development? end def test_missing_question_mark - assert_raise(NoMethodError) { ActiveSupport::StringInquirer.new("production").production } + assert_raise(NoMethodError) { @string_inquirer.production } + end + + def test_respond_to + assert_respond_to @string_inquirer, :development? end end diff --git a/activesupport/test/tagged_logging_test.rb b/activesupport/test/tagged_logging_test.rb index 0751c2469e..43cf1a8e4f 100644 --- a/activesupport/test/tagged_logging_test.rb +++ b/activesupport/test/tagged_logging_test.rb @@ -29,6 +29,11 @@ class TaggedLoggingTest < ActiveSupport::TestCase assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string end + test "does not strip message content" do + @logger.info " Hello" + assert_equal " Hello\n", @output.string + end + test "provides access to the logger instance" do @logger.tagged("BCX") { |logger| logger.info "Funky time" } assert_equal "[BCX] Funky time\n", @output.string diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index b7076e9e58..b5d8142458 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -1,7 +1,6 @@ # encoding: utf-8 require 'abstract_unit' require 'active_support/inflector/transliterate' -require 'active_support/core_ext/object/inclusion' class TransliterateTest < ActiveSupport::TestCase @@ -16,7 +15,7 @@ class TransliterateTest < ActiveSupport::TestCase # create string with range of Unicode"s western characters with # diacritics, excluding the division and multiplication signs which for # some reason or other are floating in the middle of all the letters. - string = (0xC0..0x17E).to_a.reject {|c| c.in?([0xD7, 0xF7])}.pack("U*") + string = (0xC0..0x17E).to_a.reject {|c| [0xD7, 0xF7].include?(c)}.pack("U*") string.each_char do |char| assert_match %r{^[a-zA-Z']*$}, ActiveSupport::Inflector.transliterate(string) end |