Skip to content

Commit

Permalink
Extract repetitive method
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Aug 9, 2009
1 parent 635f68d commit d0bcf51
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions activesupport/test/dependencies_test.rb
Expand Up @@ -34,6 +34,10 @@ def with_loading(*from)
ActiveSupport::Dependencies.explicitly_unloadable_constants = [] ActiveSupport::Dependencies.explicitly_unloadable_constants = []
end end


def with_autoloading_fixtures(&block)
with_loading 'autoloading_fixtures', &block
end

def test_tracking_loaded_files def test_tracking_loaded_files
require_dependency 'dependencies/service_one' require_dependency 'dependencies/service_one'
require_dependency 'dependencies/service_two' require_dependency 'dependencies/service_two'
Expand Down Expand Up @@ -130,7 +134,7 @@ def test_as_load_path
end end


def test_module_loading def test_module_loading
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_kind_of Module, A assert_kind_of Module, A
assert_kind_of Class, A::B assert_kind_of Class, A::B
assert_kind_of Class, A::C::D assert_kind_of Class, A::C::D
Expand All @@ -139,7 +143,7 @@ def test_module_loading
end end


def test_non_existing_const_raises_name_error def test_non_existing_const_raises_name_error
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_raise(NameError) { DoesNotExist } assert_raise(NameError) { DoesNotExist }
assert_raise(NameError) { NoModule::DoesNotExist } assert_raise(NameError) { NoModule::DoesNotExist }
assert_raise(NameError) { A::DoesNotExist } assert_raise(NameError) { A::DoesNotExist }
Expand All @@ -148,49 +152,49 @@ def test_non_existing_const_raises_name_error
end end


def test_directories_manifest_as_modules_unless_const_defined def test_directories_manifest_as_modules_unless_const_defined
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_kind_of Module, ModuleFolder assert_kind_of Module, ModuleFolder
Object.__send__ :remove_const, :ModuleFolder Object.__send__ :remove_const, :ModuleFolder
end end
end end


def test_module_with_nested_class def test_module_with_nested_class
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_kind_of Class, ModuleFolder::NestedClass assert_kind_of Class, ModuleFolder::NestedClass
Object.__send__ :remove_const, :ModuleFolder Object.__send__ :remove_const, :ModuleFolder
end end
end end


def test_module_with_nested_inline_class def test_module_with_nested_inline_class
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_kind_of Class, ModuleFolder::InlineClass assert_kind_of Class, ModuleFolder::InlineClass
Object.__send__ :remove_const, :ModuleFolder Object.__send__ :remove_const, :ModuleFolder
end end
end end


def test_directories_may_manifest_as_nested_classes def test_directories_may_manifest_as_nested_classes
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_kind_of Class, ClassFolder assert_kind_of Class, ClassFolder
Object.__send__ :remove_const, :ClassFolder Object.__send__ :remove_const, :ClassFolder
end end
end end


def test_class_with_nested_class def test_class_with_nested_class
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_kind_of Class, ClassFolder::NestedClass assert_kind_of Class, ClassFolder::NestedClass
Object.__send__ :remove_const, :ClassFolder Object.__send__ :remove_const, :ClassFolder
end end
end end


def test_class_with_nested_inline_class def test_class_with_nested_inline_class
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_kind_of Class, ClassFolder::InlineClass assert_kind_of Class, ClassFolder::InlineClass
Object.__send__ :remove_const, :ClassFolder Object.__send__ :remove_const, :ClassFolder
end end
end end


def test_class_with_nested_inline_subclass_of_parent 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::ClassFolderSubclass
assert_kind_of Class, ClassFolder assert_kind_of Class, ClassFolder
assert_equal 'indeed', ClassFolder::ClassFolderSubclass::ConstantInClassFolder assert_equal 'indeed', ClassFolder::ClassFolderSubclass::ConstantInClassFolder
Expand All @@ -199,7 +203,7 @@ def test_class_with_nested_inline_subclass_of_parent
end end


def test_nested_class_can_access_sibling def test_nested_class_can_access_sibling
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
sibling = ModuleFolder::NestedClass.class_eval "NestedSibling" sibling = ModuleFolder::NestedClass.class_eval "NestedSibling"
assert defined?(ModuleFolder::NestedSibling) assert defined?(ModuleFolder::NestedSibling)
assert_equal ModuleFolder::NestedSibling, sibling assert_equal ModuleFolder::NestedSibling, sibling
Expand All @@ -208,7 +212,7 @@ def test_nested_class_can_access_sibling
end end


def failing_test_access_thru_and_upwards_fails def failing_test_access_thru_and_upwards_fails
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert ! defined?(ModuleFolder) assert ! defined?(ModuleFolder)
assert_raise(NameError) { ModuleFolder::Object } assert_raise(NameError) { ModuleFolder::Object }
assert_raise(NameError) { ModuleFolder::NestedClass::Object } assert_raise(NameError) { ModuleFolder::NestedClass::Object }
Expand All @@ -217,7 +221,7 @@ def failing_test_access_thru_and_upwards_fails
end end


def test_non_existing_const_raises_name_error_with_fully_qualified_name def test_non_existing_const_raises_name_error_with_fully_qualified_name
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
begin begin
A::DoesNotExist.nil? A::DoesNotExist.nil?
flunk "No raise!!" flunk "No raise!!"
Expand Down Expand Up @@ -295,7 +299,7 @@ def test_qualified_const_defined_should_not_call_method_missing
end end


def test_autoloaded? def test_autoloaded?
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder")
assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass") assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass")


Expand Down Expand Up @@ -374,7 +378,7 @@ module A
end end
end_eval end_eval


with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert_kind_of Integer, ::ModuleWithCustomConstMissing::B assert_kind_of Integer, ::ModuleWithCustomConstMissing::B
assert_kind_of Module, ::ModuleWithCustomConstMissing::A assert_kind_of Module, ::ModuleWithCustomConstMissing::A
assert_kind_of String, ::ModuleWithCustomConstMissing::A::B assert_kind_of String, ::ModuleWithCustomConstMissing::A::B
Expand All @@ -383,7 +387,7 @@ module A


def test_const_missing_should_not_double_load def test_const_missing_should_not_double_load
$counting_loaded_times = 0 $counting_loaded_times = 0
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
require_dependency '././counting_loader' require_dependency '././counting_loader'
assert_equal 1, $counting_loaded_times assert_equal 1, $counting_loaded_times
assert_raise(ArgumentError) { ActiveSupport::Dependencies.load_missing_constant Object, :CountingLoader } assert_raise(ArgumentError) { ActiveSupport::Dependencies.load_missing_constant Object, :CountingLoader }
Expand All @@ -397,7 +401,7 @@ def test_const_missing_within_anonymous_module
m.module_eval "def a() CountingLoader; end" m.module_eval "def a() CountingLoader; end"
extend m extend m
kls = nil kls = nil
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
kls = nil kls = nil
assert_nothing_raised { kls = a } assert_nothing_raised { kls = a }
assert_equal "CountingLoader", kls.name assert_equal "CountingLoader", kls.name
Expand Down Expand Up @@ -432,7 +436,7 @@ def test_nested_load_error_isnt_rescued
end end


def test_load_once_paths_do_not_add_to_autoloaded_constants 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 ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths.dup


assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder")
Expand All @@ -448,30 +452,30 @@ def test_load_once_paths_do_not_add_to_autoloaded_constants
end end


def test_application_should_special_case_application_controller def test_application_should_special_case_application_controller
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
require_dependency 'application' require_dependency 'application'
assert_equal 10, ApplicationController assert_equal 10, ApplicationController
assert ActiveSupport::Dependencies.autoloaded?(:ApplicationController) assert ActiveSupport::Dependencies.autoloaded?(:ApplicationController)
end end
end end


def test_const_missing_on_kernel_should_fallback_to_object def test_const_missing_on_kernel_should_fallback_to_object
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
kls = Kernel::E kls = Kernel::E
assert_equal "E", kls.name assert_equal "E", kls.name
assert_equal kls.object_id, Kernel::E.object_id assert_equal kls.object_id, Kernel::E.object_id
end end
end end


def test_preexisting_constants_are_not_marked_as_autoloaded def test_preexisting_constants_are_not_marked_as_autoloaded
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
require_dependency 'e' require_dependency 'e'
assert ActiveSupport::Dependencies.autoloaded?(:E) assert ActiveSupport::Dependencies.autoloaded?(:E)
ActiveSupport::Dependencies.clear ActiveSupport::Dependencies.clear
end end


Object.const_set :E, Class.new Object.const_set :E, Class.new
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
require_dependency 'e' require_dependency 'e'
assert ! ActiveSupport::Dependencies.autoloaded?(:E), "E shouldn't be marked autoloaded!" assert ! ActiveSupport::Dependencies.autoloaded?(:E), "E shouldn't be marked autoloaded!"
ActiveSupport::Dependencies.clear ActiveSupport::Dependencies.clear
Expand All @@ -482,7 +486,7 @@ def test_preexisting_constants_are_not_marked_as_autoloaded
end end


def test_unloadable def test_unloadable
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
Object.const_set :M, Module.new Object.const_set :M, Module.new
M.unloadable M.unloadable


Expand All @@ -496,14 +500,14 @@ def test_unloadable
end end


def test_unloadable_should_fail_with_anonymous_modules def test_unloadable_should_fail_with_anonymous_modules
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
m = Module.new m = Module.new
assert_raise(ArgumentError) { m.unloadable } assert_raise(ArgumentError) { m.unloadable }
end end
end end


def test_unloadable_should_return_change_flag def test_unloadable_should_return_change_flag
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
Object.const_set :M, Module.new Object.const_set :M, Module.new
assert_equal true, M.unloadable assert_equal true, M.unloadable
assert_equal false, M.unloadable assert_equal false, M.unloadable
Expand Down Expand Up @@ -594,7 +598,7 @@ def test_new_constants_in_with_illegal_module_name_raises_correct_error
end end


def test_file_with_multiple_constants_and_require_dependency def test_file_with_multiple_constants_and_require_dependency
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert ! defined?(MultipleConstantFile) assert ! defined?(MultipleConstantFile)
assert ! defined?(SiblingConstant) assert ! defined?(SiblingConstant)


Expand All @@ -612,7 +616,7 @@ def test_file_with_multiple_constants_and_require_dependency
end end


def test_file_with_multiple_constants_and_auto_loading def test_file_with_multiple_constants_and_auto_loading
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
assert ! defined?(MultipleConstantFile) assert ! defined?(MultipleConstantFile)
assert ! defined?(SiblingConstant) assert ! defined?(SiblingConstant)


Expand All @@ -631,7 +635,7 @@ def test_file_with_multiple_constants_and_auto_loading
end end


def test_nested_file_with_multiple_constants_and_require_dependency 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::NestedClass)
assert ! defined?(ClassFolder::SiblingClass) assert ! defined?(ClassFolder::SiblingClass)


Expand All @@ -650,7 +654,7 @@ def test_nested_file_with_multiple_constants_and_require_dependency
end end


def test_nested_file_with_multiple_constants_and_auto_loading 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::NestedClass)
assert ! defined?(ClassFolder::SiblingClass) assert ! defined?(ClassFolder::SiblingClass)


Expand All @@ -669,7 +673,7 @@ def test_nested_file_with_multiple_constants_and_auto_loading
end end


def test_autoload_doesnt_shadow_no_method_error_with_relative_constant 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!" assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it hasn't been referenced yet!"
2.times do 2.times do
assert_raise(NoMethodError) { RaisesNoMethodError } assert_raise(NoMethodError) { RaisesNoMethodError }
Expand All @@ -682,7 +686,7 @@ def test_autoload_doesnt_shadow_no_method_error_with_relative_constant
end end


def test_autoload_doesnt_shadow_no_method_error_with_absolute_constant 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!" assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it hasn't been referenced yet!"
2.times do 2.times do
assert_raise(NoMethodError) { ::RaisesNoMethodError } assert_raise(NoMethodError) { ::RaisesNoMethodError }
Expand All @@ -695,7 +699,7 @@ def test_autoload_doesnt_shadow_no_method_error_with_absolute_constant
end end


def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load 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 ActiveSupport::Dependencies.mechanism = :require
2.times do 2.times do
assert_raise(NameError) { assert_equal 123, ::RaisesNameError::FooBarBaz } assert_raise(NameError) { assert_equal 123, ::RaisesNameError::FooBarBaz }
Expand All @@ -704,7 +708,7 @@ def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load
end end


def test_autoload_doesnt_shadow_name_error def test_autoload_doesnt_shadow_name_error
with_loading 'autoloading_fixtures' do with_autoloading_fixtures do
Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError) Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError)
2.times do 2.times do
begin begin
Expand Down Expand Up @@ -738,7 +742,7 @@ def test_remove_constant_handles_double_colon_at_start
end end


def test_load_once_constants_should_not_be_unloaded 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 ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths
::A.to_s ::A.to_s
assert defined?(A) assert defined?(A)
Expand Down

0 comments on commit d0bcf51

Please sign in to comment.