From d0bcf51191d71edfddf38ade6ff7a3099ba23a54 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 9 Aug 2009 12:14:25 -0700 Subject: [PATCH] Extract repetitive method --- activesupport/test/dependencies_test.rb | 70 +++++++++++++------------ 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 9026126044..fe9f86fd8c 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -34,6 +34,10 @@ class DependenciesTest < Test::Unit::TestCase ActiveSupport::Dependencies.explicitly_unloadable_constants = [] end + def with_autoloading_fixtures(&block) + with_loading 'autoloading_fixtures', &block + end + def test_tracking_loaded_files require_dependency 'dependencies/service_one' require_dependency 'dependencies/service_two' @@ -130,7 +134,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_module_loading - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Module, A assert_kind_of Class, A::B assert_kind_of Class, A::C::D @@ -139,7 +143,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_non_existing_const_raises_name_error - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_raise(NameError) { DoesNotExist } assert_raise(NameError) { NoModule::DoesNotExist } assert_raise(NameError) { A::DoesNotExist } @@ -148,49 +152,49 @@ class DependenciesTest < Test::Unit::TestCase end def test_directories_manifest_as_modules_unless_const_defined - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Module, ModuleFolder Object.__send__ :remove_const, :ModuleFolder end end def test_module_with_nested_class - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Class, ModuleFolder::NestedClass Object.__send__ :remove_const, :ModuleFolder end end def test_module_with_nested_inline_class - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Class, ModuleFolder::InlineClass Object.__send__ :remove_const, :ModuleFolder end end def test_directories_may_manifest_as_nested_classes - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Class, ClassFolder Object.__send__ :remove_const, :ClassFolder end end def test_class_with_nested_class - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Class, ClassFolder::NestedClass Object.__send__ :remove_const, :ClassFolder end end def test_class_with_nested_inline_class - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Class, ClassFolder::InlineClass Object.__send__ :remove_const, :ClassFolder end end def test_class_with_nested_inline_subclass_of_parent - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Class, ClassFolder::ClassFolderSubclass assert_kind_of Class, ClassFolder assert_equal 'indeed', ClassFolder::ClassFolderSubclass::ConstantInClassFolder @@ -199,7 +203,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_nested_class_can_access_sibling - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do sibling = ModuleFolder::NestedClass.class_eval "NestedSibling" assert defined?(ModuleFolder::NestedSibling) assert_equal ModuleFolder::NestedSibling, sibling @@ -208,7 +212,7 @@ class DependenciesTest < Test::Unit::TestCase end def failing_test_access_thru_and_upwards_fails - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert ! defined?(ModuleFolder) assert_raise(NameError) { ModuleFolder::Object } assert_raise(NameError) { ModuleFolder::NestedClass::Object } @@ -217,7 +221,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_non_existing_const_raises_name_error_with_fully_qualified_name - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do begin A::DoesNotExist.nil? flunk "No raise!!" @@ -295,7 +299,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_autoloaded? - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass") @@ -374,7 +378,7 @@ class DependenciesTest < Test::Unit::TestCase end end_eval - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert_kind_of Integer, ::ModuleWithCustomConstMissing::B assert_kind_of Module, ::ModuleWithCustomConstMissing::A assert_kind_of String, ::ModuleWithCustomConstMissing::A::B @@ -383,7 +387,7 @@ class DependenciesTest < Test::Unit::TestCase def test_const_missing_should_not_double_load $counting_loaded_times = 0 - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do require_dependency '././counting_loader' assert_equal 1, $counting_loaded_times assert_raise(ArgumentError) { ActiveSupport::Dependencies.load_missing_constant Object, :CountingLoader } @@ -397,7 +401,7 @@ class DependenciesTest < Test::Unit::TestCase m.module_eval "def a() CountingLoader; end" extend m kls = nil - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do kls = nil assert_nothing_raised { kls = a } assert_equal "CountingLoader", kls.name @@ -432,7 +436,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_load_once_paths_do_not_add_to_autoloaded_constants - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths.dup assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") @@ -448,7 +452,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_application_should_special_case_application_controller - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do require_dependency 'application' assert_equal 10, ApplicationController assert ActiveSupport::Dependencies.autoloaded?(:ApplicationController) @@ -456,7 +460,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_const_missing_on_kernel_should_fallback_to_object - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do kls = Kernel::E assert_equal "E", kls.name assert_equal kls.object_id, Kernel::E.object_id @@ -464,14 +468,14 @@ class DependenciesTest < Test::Unit::TestCase end def test_preexisting_constants_are_not_marked_as_autoloaded - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do require_dependency 'e' assert ActiveSupport::Dependencies.autoloaded?(:E) ActiveSupport::Dependencies.clear end Object.const_set :E, Class.new - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do require_dependency 'e' assert ! ActiveSupport::Dependencies.autoloaded?(:E), "E shouldn't be marked autoloaded!" ActiveSupport::Dependencies.clear @@ -482,7 +486,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_unloadable - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do Object.const_set :M, Module.new M.unloadable @@ -496,14 +500,14 @@ class DependenciesTest < Test::Unit::TestCase end def test_unloadable_should_fail_with_anonymous_modules - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do m = Module.new assert_raise(ArgumentError) { m.unloadable } end end def test_unloadable_should_return_change_flag - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do Object.const_set :M, Module.new assert_equal true, M.unloadable assert_equal false, M.unloadable @@ -594,7 +598,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_file_with_multiple_constants_and_require_dependency - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert ! defined?(MultipleConstantFile) assert ! defined?(SiblingConstant) @@ -612,7 +616,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_file_with_multiple_constants_and_auto_loading - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert ! defined?(MultipleConstantFile) assert ! defined?(SiblingConstant) @@ -631,7 +635,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_nested_file_with_multiple_constants_and_require_dependency - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert ! defined?(ClassFolder::NestedClass) assert ! defined?(ClassFolder::SiblingClass) @@ -650,7 +654,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_nested_file_with_multiple_constants_and_auto_loading - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert ! defined?(ClassFolder::NestedClass) assert ! defined?(ClassFolder::SiblingClass) @@ -669,7 +673,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_autoload_doesnt_shadow_no_method_error_with_relative_constant - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it hasn't been referenced yet!" 2.times do assert_raise(NoMethodError) { RaisesNoMethodError } @@ -682,7 +686,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_autoload_doesnt_shadow_no_method_error_with_absolute_constant - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it hasn't been referenced yet!" 2.times do assert_raise(NoMethodError) { ::RaisesNoMethodError } @@ -695,7 +699,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do ActiveSupport::Dependencies.mechanism = :require 2.times do assert_raise(NameError) { assert_equal 123, ::RaisesNameError::FooBarBaz } @@ -704,7 +708,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_autoload_doesnt_shadow_name_error - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError) 2.times do begin @@ -738,7 +742,7 @@ class DependenciesTest < Test::Unit::TestCase end def test_load_once_constants_should_not_be_unloaded - with_loading 'autoloading_fixtures' do + with_autoloading_fixtures do ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths ::A.to_s assert defined?(A)