Browse files

Extract repetitive method

  • Loading branch information...
1 parent 635f68d commit d0bcf51191d71edfddf38ade6ff7a3099ba23a54 @jeremy jeremy committed Aug 9, 2009
Showing with 37 additions and 33 deletions.
  1. +37 −33 activesupport/test/dependencies_test.rb
View
70 activesupport/test/dependencies_test.rb
@@ -34,6 +34,10 @@ def with_loading(*from)
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 @@ def test_as_load_path
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 @@ def test_module_loading
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 @@ def test_non_existing_const_raises_name_error
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 @@ def test_class_with_nested_inline_subclass_of_parent
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 @@ def test_nested_class_can_access_sibling
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 @@ def failing_test_access_thru_and_upwards_fails
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 @@ def test_qualified_const_defined_should_not_call_method_missing
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 @@ module A
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 @@ module A
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 @@ def test_const_missing_within_anonymous_module
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 @@ def test_nested_load_error_isnt_rescued
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,30 +452,30 @@ def test_load_once_paths_do_not_add_to_autoloaded_constants
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)
end
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
end
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 @@ def test_preexisting_constants_are_not_marked_as_autoloaded
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 @@ def test_unloadable
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 @@ def test_new_constants_in_with_illegal_module_name_raises_correct_error
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 @@ def test_file_with_multiple_constants_and_require_dependency
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 @@ def test_file_with_multiple_constants_and_auto_loading
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 @@ def test_nested_file_with_multiple_constants_and_require_dependency
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 @@ def test_nested_file_with_multiple_constants_and_auto_loading
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 @@ def test_autoload_doesnt_shadow_no_method_error_with_relative_constant
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 @@ def test_autoload_doesnt_shadow_no_method_error_with_absolute_constant
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 @@ def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load
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 @@ def test_remove_constant_handles_double_colon_at_start
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)

0 comments on commit d0bcf51

Please sign in to comment.