diff options
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/autoloading_fixtures/conflict.rb | 1 | ||||
-rw-r--r-- | activesupport/test/caching_tools_test.rb | 1 | ||||
-rw-r--r-- | activesupport/test/dependencies/conflict.rb | 1 | ||||
-rw-r--r-- | activesupport/test/dependencies_test.rb | 134 | ||||
-rw-r--r-- | activesupport/test/deprecation_test.rb | 11 | ||||
-rw-r--r-- | activesupport/test/reloadable_test.rb | 32 |
6 files changed, 170 insertions, 10 deletions
diff --git a/activesupport/test/autoloading_fixtures/conflict.rb b/activesupport/test/autoloading_fixtures/conflict.rb new file mode 100644 index 0000000000..4ac6201902 --- /dev/null +++ b/activesupport/test/autoloading_fixtures/conflict.rb @@ -0,0 +1 @@ +Conflict = 2
\ No newline at end of file diff --git a/activesupport/test/caching_tools_test.rb b/activesupport/test/caching_tools_test.rb index c9890e4d13..e1cd4e433c 100644 --- a/activesupport/test/caching_tools_test.rb +++ b/activesupport/test/caching_tools_test.rb @@ -1,4 +1,5 @@ require File.dirname(__FILE__) + '/abstract_unit' +require File.join(File.dirname(File.dirname(__FILE__)), 'lib/active_support/caching_tools.rb') class HashCachingTests < Test::Unit::TestCase def cached(&proc) diff --git a/activesupport/test/dependencies/conflict.rb b/activesupport/test/dependencies/conflict.rb new file mode 100644 index 0000000000..e888b7b54c --- /dev/null +++ b/activesupport/test/dependencies/conflict.rb @@ -0,0 +1 @@ +Conflict = 1
\ No newline at end of file diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index f19542bdb0..bf9aaac8c1 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -1,19 +1,19 @@ require File.dirname(__FILE__) + '/abstract_unit' -#require 'dependencies' class DependenciesTest < Test::Unit::TestCase + def teardown Dependencies.clear end - def with_loading(from_dir = nil) - prior_path = $LOAD_PATH.clone - $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/#{from_dir}" if from_dir + def with_loading(*from) old_mechanism, Dependencies.mechanism = Dependencies.mechanism, :load + dir = File.dirname(__FILE__) + prior_autoload_paths = Dependencies.autoload_paths + Dependencies.autoload_paths = from.collect { |f| "#{dir}/#{f}" } yield ensure - $LOAD_PATH.clear - $LOAD_PATH.concat prior_path + Dependencies.autoload_paths = prior_autoload_paths Dependencies.mechanism = old_mechanism end @@ -159,13 +159,13 @@ class DependenciesTest < Test::Unit::TestCase def test_non_existing_const_raises_name_error_with_fully_qualified_name with_loading 'autoloading_fixtures' do begin - A::DoesNotExist + A::DoesNotExist.nil? flunk "No raise!!" rescue NameError => e assert_equal "uninitialized constant A::DoesNotExist", e.message end begin - A::B::DoesNotExist + A::B::DoesNotExist.nil? flunk "No raise!!" rescue NameError => e assert_equal "uninitialized constant A::B::DoesNotExist", e.message @@ -182,4 +182,122 @@ class DependenciesTest < Test::Unit::TestCase end end + def test_autoloadable_constants_for_path_should_handle_empty_autoloads + assert_equal [], Dependencies.autoloadable_constants_for_path('hello') + end + + def test_autoloadable_constants_for_path_should_handle_relative_paths + fake_root = 'dependencies' + relative_root = File.dirname(__FILE__) + '/dependencies' + ['', '/'].each do |suffix| + with_loading fake_root + suffix do + assert_equal ["A::B"], Dependencies.autoloadable_constants_for_path(relative_root + '/a/b') + end + end + end + + def test_autoloadable_constants_for_path_should_provide_all_results + fake_root = '/usr/apps/backpack' + with_loading fake_root, fake_root + '/lib' do + root = Dependencies.autoload_paths.first + assert_equal ["Lib::A::B", "A::B"], Dependencies.autoloadable_constants_for_path(root + '/lib/a/b') + end + end + + def test_autoloadable_constants_for_path_should_uniq_results + fake_root = '/usr/apps/backpack/lib' + with_loading fake_root, fake_root + '/' do + root = Dependencies.autoload_paths.first + assert_equal ["A::B"], Dependencies.autoloadable_constants_for_path(root + '/a/b') + end + end + + def test_qualified_const_defined + assert Dependencies.qualified_const_defined?("Object") + assert Dependencies.qualified_const_defined?("::Object") + assert Dependencies.qualified_const_defined?("::Object::Kernel") + assert Dependencies.qualified_const_defined?("::Object::Dependencies") + assert Dependencies.qualified_const_defined?("::Test::Unit::TestCase") + end + + def test_autoloaded? + with_loading 'autoloading_fixtures' do + assert ! Dependencies.autoloaded?("ModuleFolder") + assert ! Dependencies.autoloaded?("ModuleFolder::NestedClass") + + assert Dependencies.autoloaded?(ModuleFolder) + + assert Dependencies.autoloaded?("ModuleFolder") + assert ! Dependencies.autoloaded?("ModuleFolder::NestedClass") + + assert Dependencies.autoloaded?(ModuleFolder::NestedClass) + + assert Dependencies.autoloaded?("ModuleFolder") + assert Dependencies.autoloaded?("ModuleFolder::NestedClass") + + assert Dependencies.autoloaded?("::ModuleFolder") + assert Dependencies.autoloaded?(:ModuleFolder) + + Object.send :remove_const, :ModuleFolder + end + end + + def test_qualified_name_for + assert_equal "A", Dependencies.qualified_name_for(Object, :A) + assert_equal "A", Dependencies.qualified_name_for(:Object, :A) + assert_equal "A", Dependencies.qualified_name_for("Object", :A) + assert_equal "A", Dependencies.qualified_name_for("::Object", :A) + assert_equal "A", Dependencies.qualified_name_for("::Kernel", :A) + + assert_equal "Dependencies::A", Dependencies.qualified_name_for(:Dependencies, :A) + assert_equal "Dependencies::A", Dependencies.qualified_name_for(Dependencies, :A) + end + + def test_file_search + with_loading 'dependencies' do + root = Dependencies.autoload_paths.first + assert_equal nil, Dependencies.search_for_autoload_file('service_three') + assert_equal nil, Dependencies.search_for_autoload_file('service_three.rb') + assert_equal root + '/service_one.rb', Dependencies.search_for_autoload_file('service_one') + assert_equal root + '/service_one.rb', Dependencies.search_for_autoload_file('service_one.rb') + end + end + + def test_file_search_uses_first_in_autoload_path + with_loading 'dependencies', 'autoloading_fixtures' do + deps, autoload = Dependencies.autoload_paths + assert_match %r/dependencies/, deps + assert_match %r/autoloading_fixtures/, autoload + + assert_equal deps + '/conflict.rb', Dependencies.search_for_autoload_file('conflict') + end + with_loading 'autoloading_fixtures', 'dependencies' do + autoload, deps = Dependencies.autoload_paths + assert_match %r/dependencies/, deps + assert_match %r/autoloading_fixtures/, autoload + + assert_equal autoload + '/conflict.rb', Dependencies.search_for_autoload_file('conflict') + end + + end + + def test_custom_const_missing_should_work + Object.module_eval <<-end_eval + module ModuleWithCustomConstMissing + def self.const_missing(name) + const_set name, name.to_s.hash + end + + module A + end + end + end_eval + + with_loading 'autoloading_fixtures' do + assert_kind_of Integer, ::ModuleWithCustomConstMissing::B + assert_kind_of Module, ::ModuleWithCustomConstMissing::A + assert_kind_of String, ::ModuleWithCustomConstMissing::A::B + end + end + end diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index 472fdecaaf..1a054335d7 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -39,7 +39,7 @@ class DeprecationTest < Test::Unit::TestCase @dtc.partially end end - + def test_undeprecated assert_not_deprecated do assert_equal 2, @dtc.not @@ -67,8 +67,15 @@ class DeprecationTest < Test::Unit::TestCase def test_deprecated_instance_variable_proxy assert_not_deprecated { @dtc.request.size } - + assert_deprecated('@request.size') { assert_equal @dtc.request.size, @dtc.old_request.size } assert_deprecated('@request.to_s') { assert_equal @dtc.request.to_s, @dtc.old_request.to_s } end + + def test_assert_deprecation_without_match + assert_deprecated do + @dtc.partially + end + end + end diff --git a/activesupport/test/reloadable_test.rb b/activesupport/test/reloadable_test.rb index c330394b29..2ab19db77e 100644 --- a/activesupport/test/reloadable_test.rb +++ b/activesupport/test/reloadable_test.rb @@ -78,4 +78,36 @@ class ReloadableTest < Test::Unit::TestCase assert ! results.include?(ReloadableTestSandbox.const_get(name)), "Expected #{name} NOT to be reloadable" end end + + def test_including_reloadable_should_warn + c = Class.new + assert_deprecated %r{Reloadable} do + c.send :include, Reloadable + end + + assert_deprecated { c.reloadable? } + end + + def test_include_subclasses_should_warn + c = Class.new + deps = collect_deprecations do + c.send :include, Reloadable::Subclasses + end + assert_equal 1, deps.size + assert_match %r{Reloadable::Subclasses}, deps.first + + assert_deprecated { c.reloadable? } + end + + def test_include_deprecated_should_not_warn + c = Class.new + deps = collect_deprecations do + c.send :include, Reloadable::Deprecated + end + assert_equal 0, deps.size + + assert c.respond_to?(:reloadable?) + assert_deprecated { c.reloadable? } + end + end |