| 1 |
require 'abstract_unit' |
|---|
| 2 |
require 'pp' |
|---|
| 3 |
|
|---|
| 4 |
module ModuleWithMissing |
|---|
| 5 |
mattr_accessor :missing_count |
|---|
| 6 |
def self.const_missing(name) |
|---|
| 7 |
self.missing_count += 1 |
|---|
| 8 |
name |
|---|
| 9 |
end |
|---|
| 10 |
end |
|---|
| 11 |
|
|---|
| 12 |
module ModuleWithConstant |
|---|
| 13 |
InheritedConstant = "Hello" |
|---|
| 14 |
end |
|---|
| 15 |
|
|---|
| 16 |
class DependenciesTest < Test::Unit::TestCase |
|---|
| 17 |
def teardown |
|---|
| 18 |
Dependencies.clear |
|---|
| 19 |
end |
|---|
| 20 |
|
|---|
| 21 |
def with_loading(*from) |
|---|
| 22 |
old_mechanism, Dependencies.mechanism = Dependencies.mechanism, :load |
|---|
| 23 |
dir = File.dirname(__FILE__) |
|---|
| 24 |
prior_load_paths = Dependencies.load_paths |
|---|
| 25 |
Dependencies.load_paths = from.collect { |f| "#{dir}/#{f}" } |
|---|
| 26 |
yield |
|---|
| 27 |
ensure |
|---|
| 28 |
Dependencies.load_paths = prior_load_paths |
|---|
| 29 |
Dependencies.mechanism = old_mechanism |
|---|
| 30 |
Dependencies.explicitly_unloadable_constants = [] |
|---|
| 31 |
end |
|---|
| 32 |
|
|---|
| 33 |
def test_tracking_loaded_files |
|---|
| 34 |
require_dependency 'dependencies/service_one' |
|---|
| 35 |
require_dependency 'dependencies/service_two' |
|---|
| 36 |
assert_equal 2, Dependencies.loaded.size |
|---|
| 37 |
end |
|---|
| 38 |
|
|---|
| 39 |
def test_tracking_identical_loaded_files |
|---|
| 40 |
require_dependency 'dependencies/service_one' |
|---|
| 41 |
require_dependency 'dependencies/service_one' |
|---|
| 42 |
assert_equal 1, Dependencies.loaded.size |
|---|
| 43 |
end |
|---|
| 44 |
|
|---|
| 45 |
def test_missing_dependency_raises_missing_source_file |
|---|
| 46 |
assert_raises(MissingSourceFile) { require_dependency("missing_service") } |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
def test_missing_association_raises_nothing |
|---|
| 50 |
assert_nothing_raised { require_association("missing_model") } |
|---|
| 51 |
end |
|---|
| 52 |
|
|---|
| 53 |
def test_dependency_which_raises_exception_isnt_added_to_loaded_set |
|---|
| 54 |
with_loading do |
|---|
| 55 |
filename = 'dependencies/raises_exception' |
|---|
| 56 |
$raises_exception_load_count = 0 |
|---|
| 57 |
|
|---|
| 58 |
5.times do |count| |
|---|
| 59 |
begin |
|---|
| 60 |
require_dependency filename |
|---|
| 61 |
flunk 'should have loaded dependencies/raises_exception which raises an exception' |
|---|
| 62 |
rescue Exception => e |
|---|
| 63 |
assert_equal 'Loading me failed, so do not add to loaded or history.', e.message |
|---|
| 64 |
end |
|---|
| 65 |
assert_equal count + 1, $raises_exception_load_count |
|---|
| 66 |
|
|---|
| 67 |
assert !Dependencies.loaded.include?(filename) |
|---|
| 68 |
assert !Dependencies.history.include?(filename) |
|---|
| 69 |
end |
|---|
| 70 |
end |
|---|
| 71 |
end |
|---|
| 72 |
|
|---|
| 73 |
def test_warnings_should_be_enabled_on_first_load |
|---|
| 74 |
with_loading 'dependencies' do |
|---|
| 75 |
old_warnings, Dependencies.warnings_on_first_load = Dependencies.warnings_on_first_load, true |
|---|
| 76 |
|
|---|
| 77 |
filename = "check_warnings" |
|---|
| 78 |
expanded = File.expand_path("test/dependencies/#{filename}") |
|---|
| 79 |
$check_warnings_load_count = 0 |
|---|
| 80 |
|
|---|
| 81 |
assert !Dependencies.loaded.include?(expanded) |
|---|
| 82 |
assert !Dependencies.history.include?(expanded) |
|---|
| 83 |
|
|---|
| 84 |
silence_warnings { require_dependency filename } |
|---|
| 85 |
assert_equal 1, $check_warnings_load_count |
|---|
| 86 |
assert_equal true, $checked_verbose, 'On first load warnings should be enabled.' |
|---|
| 87 |
|
|---|
| 88 |
assert Dependencies.loaded.include?(expanded) |
|---|
| 89 |
Dependencies.clear |
|---|
| 90 |
assert !Dependencies.loaded.include?(expanded) |
|---|
| 91 |
assert Dependencies.history.include?(expanded) |
|---|
| 92 |
|
|---|
| 93 |
silence_warnings { require_dependency filename } |
|---|
| 94 |
assert_equal 2, $check_warnings_load_count |
|---|
| 95 |
assert_equal nil, $checked_verbose, 'After first load warnings should be left alone.' |
|---|
| 96 |
|
|---|
| 97 |
assert Dependencies.loaded.include?(expanded) |
|---|
| 98 |
Dependencies.clear |
|---|
| 99 |
assert !Dependencies.loaded.include?(expanded) |
|---|
| 100 |
assert Dependencies.history.include?(expanded) |
|---|
| 101 |
|
|---|
| 102 |
enable_warnings { require_dependency filename } |
|---|
| 103 |
assert_equal 3, $check_warnings_load_count |
|---|
| 104 |
assert_equal true, $checked_verbose, 'After first load warnings should be left alone.' |
|---|
| 105 |
|
|---|
| 106 |
assert Dependencies.loaded.include?(expanded) |
|---|
| 107 |
end |
|---|
| 108 |
end |
|---|
| 109 |
|
|---|
| 110 |
def test_mutual_dependencies_dont_infinite_loop |
|---|
| 111 |
with_loading 'dependencies' do |
|---|
| 112 |
$mutual_dependencies_count = 0 |
|---|
| 113 |
assert_nothing_raised { require_dependency 'mutual_one' } |
|---|
| 114 |
assert_equal 2, $mutual_dependencies_count |
|---|
| 115 |
|
|---|
| 116 |
Dependencies.clear |
|---|
| 117 |
|
|---|
| 118 |
$mutual_dependencies_count = 0 |
|---|
| 119 |
assert_nothing_raised { require_dependency 'mutual_two' } |
|---|
| 120 |
assert_equal 2, $mutual_dependencies_count |
|---|
| 121 |
end |
|---|
| 122 |
end |
|---|
| 123 |
|
|---|
| 124 |
def test_as_load_path |
|---|
| 125 |
assert_equal '', DependenciesTest.as_load_path |
|---|
| 126 |
end |
|---|
| 127 |
|
|---|
| 128 |
def test_module_loading |
|---|
| 129 |
with_loading 'autoloading_fixtures' do |
|---|
| 130 |
assert_kind_of Module, A |
|---|
| 131 |
assert_kind_of Class, A::B |
|---|
| 132 |
assert_kind_of Class, A::C::D |
|---|
| 133 |
assert_kind_of Class, A::C::E::F |
|---|
| 134 |
end |
|---|
| 135 |
end |
|---|
| 136 |
|
|---|
| 137 |
def test_non_existing_const_raises_name_error |
|---|
| 138 |
with_loading 'autoloading_fixtures' do |
|---|
| 139 |
assert_raises(NameError) { DoesNotExist } |
|---|
| 140 |
assert_raises(NameError) { NoModule::DoesNotExist } |
|---|
| 141 |
assert_raises(NameError) { A::DoesNotExist } |
|---|
| 142 |
assert_raises(NameError) { A::B::DoesNotExist } |
|---|
| 143 |
end |
|---|
| 144 |
end |
|---|
| 145 |
|
|---|
| 146 |
def test_directories_manifest_as_modules_unless_const_defined |
|---|
| 147 |
with_loading 'autoloading_fixtures' do |
|---|
| 148 |
assert_kind_of Module, ModuleFolder |
|---|
| 149 |
Object.send! :remove_const, :ModuleFolder |
|---|
| 150 |
end |
|---|
| 151 |
end |
|---|
| 152 |
|
|---|
| 153 |
def test_module_with_nested_class |
|---|
| 154 |
with_loading 'autoloading_fixtures' do |
|---|
| 155 |
assert_kind_of Class, ModuleFolder::NestedClass |
|---|
| 156 |
Object.send! :remove_const, :ModuleFolder |
|---|
| 157 |
end |
|---|
| 158 |
end |
|---|
| 159 |
|
|---|
| 160 |
def test_module_with_nested_inline_class |
|---|
| 161 |
with_loading 'autoloading_fixtures' do |
|---|
| 162 |
assert_kind_of Class, ModuleFolder::InlineClass |
|---|
| 163 |
Object.send! :remove_const, :ModuleFolder |
|---|
| 164 |
end |
|---|
| 165 |
end |
|---|
| 166 |
|
|---|
| 167 |
def test_directories_may_manifest_as_nested_classes |
|---|
| 168 |
with_loading 'autoloading_fixtures' do |
|---|
| 169 |
assert_kind_of Class, ClassFolder |
|---|
| 170 |
Object.send! :remove_const, :ClassFolder |
|---|
| 171 |
end |
|---|
| 172 |
end |
|---|
| 173 |
|
|---|
| 174 |
def test_class_with_nested_class |
|---|
| 175 |
with_loading 'autoloading_fixtures' do |
|---|
| 176 |
assert_kind_of Class, ClassFolder::NestedClass |
|---|
| 177 |
Object.send! :remove_const, :ClassFolder |
|---|
| 178 |
end |
|---|
| 179 |
end |
|---|
| 180 |
|
|---|
| 181 |
def test_class_with_nested_inline_class |
|---|
| 182 |
with_loading 'autoloading_fixtures' do |
|---|
| 183 |
assert_kind_of Class, ClassFolder::InlineClass |
|---|
| 184 |
Object.send! :remove_const, :ClassFolder |
|---|
| 185 |
end |
|---|
| 186 |
end |
|---|
| 187 |
|
|---|
| 188 |
def test_class_with_nested_inline_subclass_of_parent |
|---|
| 189 |
with_loading 'autoloading_fixtures' do |
|---|
| 190 |
assert_kind_of Class, ClassFolder::ClassFolderSubclass |
|---|
| 191 |
assert_kind_of Class, ClassFolder |
|---|
| 192 |
assert_equal 'indeed', ClassFolder::ClassFolderSubclass::ConstantInClassFolder |
|---|
| 193 |
Object.send! :remove_const, :ClassFolder |
|---|
| 194 |
end |
|---|
| 195 |
end |
|---|
| 196 |
|
|---|
| 197 |
def test_nested_class_can_access_sibling |
|---|
| 198 |
with_loading 'autoloading_fixtures' do |
|---|
| 199 |
sibling = ModuleFolder::NestedClass.class_eval "NestedSibling" |
|---|
| 200 |
assert defined?(ModuleFolder::NestedSibling) |
|---|
| 201 |
assert_equal ModuleFolder::NestedSibling, sibling |
|---|
| 202 |
Object.send! :remove_const, :ModuleFolder |
|---|
| 203 |
end |
|---|
| 204 |
end |
|---|
| 205 |
|
|---|
| 206 |
def failing_test_access_thru_and_upwards_fails |
|---|
| 207 |
with_loading 'autoloading_fixtures' do |
|---|
| 208 |
assert ! defined?(ModuleFolder) |
|---|
| 209 |
assert_raises(NameError) { ModuleFolder::Object } |
|---|
| 210 |
assert_raises(NameError) { ModuleFolder::NestedClass::Object } |
|---|
| 211 |
Object.send! :remove_const, :ModuleFolder |
|---|
| 212 |
end |
|---|
| 213 |
end |
|---|
| 214 |
|
|---|
| 215 |
def test_non_existing_const_raises_name_error_with_fully_qualified_name |
|---|
| 216 |
with_loading 'autoloading_fixtures' do |
|---|
| 217 |
begin |
|---|
| 218 |
A::DoesNotExist.nil? |
|---|
| 219 |
flunk "No raise!!" |
|---|
| 220 |
rescue NameError => e |
|---|
| 221 |
assert_equal "uninitialized constant A::DoesNotExist", e.message |
|---|
| 222 |
end |
|---|
| 223 |
begin |
|---|
| 224 |
A::B::DoesNotExist.nil? |
|---|
| 225 |
flunk "No raise!!" |
|---|
| 226 |
rescue NameError => e |
|---|
| 227 |
assert_equal "uninitialized constant A::B::DoesNotExist", e.message |
|---|
| 228 |
end |
|---|
| 229 |
end |
|---|
| 230 |
end |
|---|
| 231 |
|
|---|
| 232 |
def test_smart_name_error_strings |
|---|
| 233 |
begin |
|---|
| 234 |
Object.module_eval "ImaginaryObject" |
|---|
| 235 |
flunk "No raise!!" |
|---|
| 236 |
rescue NameError => e |
|---|
| 237 |
assert e.message.include?("uninitialized constant ImaginaryObject") |
|---|
| 238 |
end |
|---|
| 239 |
end |
|---|
| 240 |
|
|---|
| 241 |
def test_loadable_constants_for_path_should_handle_empty_autoloads |
|---|
| 242 |
assert_equal [], Dependencies.loadable_constants_for_path('hello') |
|---|
| 243 |
end |
|---|
| 244 |
|
|---|
| 245 |
def test_loadable_constants_for_path_should_handle_relative_paths |
|---|
| 246 |
fake_root = 'dependencies' |
|---|
| 247 |
relative_root = File.dirname(__FILE__) + '/dependencies' |
|---|
| 248 |
['', '/'].each do |suffix| |
|---|
| 249 |
with_loading fake_root + suffix do |
|---|
| 250 |
assert_equal ["A::B"], Dependencies.loadable_constants_for_path(relative_root + '/a/b') |
|---|
| 251 |
end |
|---|
| 252 |
end |
|---|
| 253 |
end |
|---|
| 254 |
|
|---|
| 255 |
def test_loadable_constants_for_path_should_provide_all_results |
|---|
| 256 |
fake_root = '/usr/apps/backpack' |
|---|
| 257 |
with_loading fake_root, fake_root + '/lib' do |
|---|
| 258 |
root = Dependencies.load_paths.first |
|---|
| 259 |
assert_equal ["Lib::A::B", "A::B"], Dependencies.loadable_constants_for_path(root + '/lib/a/b') |
|---|
| 260 |
end |
|---|
| 261 |
end |
|---|
| 262 |
|
|---|
| 263 |
def test_loadable_constants_for_path_should_uniq_results |
|---|
| 264 |
fake_root = '/usr/apps/backpack/lib' |
|---|
| 265 |
with_loading fake_root, fake_root + '/' do |
|---|
| 266 |
root = Dependencies.load_paths.first |
|---|
| 267 |
assert_equal ["A::B"], Dependencies.loadable_constants_for_path(root + '/a/b') |
|---|
| 268 |
end |
|---|
| 269 |
end |
|---|
| 270 |
|
|---|
| 271 |
def test_loadable_constants_with_load_path_without_trailing_slash |
|---|
| 272 |
path = File.dirname(__FILE__) + '/autoloading_fixtures/class_folder/inline_class.rb' |
|---|
| 273 |
with_loading 'autoloading_fixtures/class/' do |
|---|
| 274 |
assert_equal [], Dependencies.loadable_constants_for_path(path) |
|---|
| 275 |
end |
|---|
| 276 |
end |
|---|
| 277 |
|
|---|
| 278 |
def test_qualified_const_defined |
|---|
| 279 |
assert Dependencies.qualified_const_defined?("Object") |
|---|
| 280 |
assert Dependencies.qualified_const_defined?("::Object") |
|---|
| 281 |
assert Dependencies.qualified_const_defined?("::Object::Kernel") |
|---|
| 282 |
assert Dependencies.qualified_const_defined?("::Object::Dependencies") |
|---|
| 283 |
assert Dependencies.qualified_const_defined?("::Test::Unit::TestCase") |
|---|
| 284 |
end |
|---|
| 285 |
|
|---|
| 286 |
def test_qualified_const_defined_should_not_call_method_missing |
|---|
| 287 |
ModuleWithMissing.missing_count = 0 |
|---|
| 288 |
assert ! Dependencies.qualified_const_defined?("ModuleWithMissing::A") |
|---|
| 289 |
assert_equal 0, ModuleWithMissing.missing_count |
|---|
| 290 |
assert ! Dependencies.qualified_const_defined?("ModuleWithMissing::A::B") |
|---|
| 291 |
assert_equal 0, ModuleWithMissing.missing_count |
|---|
| 292 |
end |
|---|
| 293 |
|
|---|
| 294 |
def test_autoloaded? |
|---|
| 295 |
with_loading 'autoloading_fixtures' do |
|---|
| 296 |
assert ! Dependencies.autoloaded?("ModuleFolder") |
|---|
| 297 |
assert ! Dependencies.autoloaded?("ModuleFolder::NestedClass") |
|---|
| 298 |
|
|---|
| 299 |
assert Dependencies.autoloaded?(ModuleFolder) |
|---|
| 300 |
|
|---|
| 301 |
assert Dependencies.autoloaded?("ModuleFolder") |
|---|
| 302 |
assert ! Dependencies.autoloaded?("ModuleFolder::NestedClass") |
|---|
| 303 |
|
|---|
| 304 |
assert Dependencies.autoloaded?(ModuleFolder::NestedClass) |
|---|
| 305 |
|
|---|
| 306 |
assert Dependencies.autoloaded?("ModuleFolder") |
|---|
| 307 |
assert Dependencies.autoloaded?("ModuleFolder::NestedClass") |
|---|
| 308 |
|
|---|
| 309 |
assert Dependencies.autoloaded?("::ModuleFolder") |
|---|
| 310 |
assert Dependencies.autoloaded?(:ModuleFolder) |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
assert !Dependencies.autoloaded?(Module.new) |
|---|
| 314 |
|
|---|
| 315 |
nil_name = Module.new |
|---|
| 316 |
def nil_name.name() nil end |
|---|
| 317 |
assert !Dependencies.autoloaded?(nil_name) |
|---|
| 318 |
|
|---|
| 319 |
Object.class_eval { remove_const :ModuleFolder } |
|---|
| 320 |
end |
|---|
| 321 |
end |
|---|
| 322 |
|
|---|
| 323 |
def test_qualified_name_for |
|---|
| 324 |
assert_equal "A", Dependencies.qualified_name_for(Object, :A) |
|---|
| 325 |
assert_equal "A", Dependencies.qualified_name_for(:Object, :A) |
|---|
| 326 |
assert_equal "A", Dependencies.qualified_name_for("Object", :A) |
|---|
| 327 |
assert_equal "A", Dependencies.qualified_name_for("::Object", :A) |
|---|
| 328 |
assert_equal "A", Dependencies.qualified_name_for("::Kernel", :A) |
|---|
| 329 |
|
|---|
| 330 |
assert_equal "Dependencies::A", Dependencies.qualified_name_for(:Dependencies, :A) |
|---|
| 331 |
assert_equal "Dependencies::A", Dependencies.qualified_name_for(Dependencies, :A) |
|---|
| 332 |
end |
|---|
| 333 |
|
|---|
| 334 |
def test_file_search |
|---|
| 335 |
with_loading 'dependencies' do |
|---|
| 336 |
root = Dependencies.load_paths.first |
|---|
| 337 |
assert_equal nil, Dependencies.search_for_file('service_three') |
|---|
| 338 |
assert_equal nil, Dependencies.search_for_file('service_three.rb') |
|---|
| 339 |
assert_equal root + '/service_one.rb', Dependencies.search_for_file('service_one') |
|---|
| 340 |
assert_equal root + '/service_one.rb', Dependencies.search_for_file('service_one.rb') |
|---|
| 341 |
end |
|---|
| 342 |
end |
|---|
| 343 |
|
|---|
| 344 |
def test_file_search_uses_first_in_load_path |
|---|
| 345 |
with_loading 'dependencies', 'autoloading_fixtures' do |
|---|
| 346 |
deps, autoload = Dependencies.load_paths |
|---|
| 347 |
assert_match %r/dependencies/, deps |
|---|
| 348 |
assert_match %r/autoloading_fixtures/, autoload |
|---|
| 349 |
|
|---|
| 350 |
assert_equal deps + '/conflict.rb', Dependencies.search_for_file('conflict') |
|---|
| 351 |
end |
|---|
| 352 |
with_loading 'autoloading_fixtures', 'dependencies' do |
|---|
| 353 |
autoload, deps = Dependencies.load_paths |
|---|
| 354 |
assert_match %r/dependencies/, deps |
|---|
| 355 |
assert_match %r/autoloading_fixtures/, autoload |
|---|
| 356 |
|
|---|
| 357 |
assert_equal autoload + '/conflict.rb', Dependencies.search_for_file('conflict') |
|---|
| 358 |
end |
|---|
| 359 |
|
|---|
| 360 |
end |
|---|
| 361 |
|
|---|
| 362 |
def test_custom_const_missing_should_work |
|---|
| 363 |
Object.module_eval <<-end_eval |
|---|
| 364 |
module ModuleWithCustomConstMissing |
|---|
| 365 |
def self.const_missing(name) |
|---|
| 366 |
const_set name, name.to_s.hash |
|---|
| 367 |
end |
|---|
| 368 |
|
|---|
| 369 |
module A |
|---|
| 370 |
end |
|---|
| 371 |
end |
|---|
| 372 |
end_eval |
|---|
| 373 |
|
|---|
| 374 |
with_loading 'autoloading_fixtures' do |
|---|
| 375 |
assert_kind_of Integer, ::ModuleWithCustomConstMissing::B |
|---|
| 376 |
assert_kind_of Module, ::ModuleWithCustomConstMissing::A |
|---|
| 377 |
assert_kind_of String, ::ModuleWithCustomConstMissing::A::B |
|---|
| 378 |
end |
|---|
| 379 |
end |
|---|
| 380 |
|
|---|
| 381 |
def test_const_missing_should_not_double_load |
|---|
| 382 |
$counting_loaded_times = 0 |
|---|
| 383 |
with_loading 'autoloading_fixtures' do |
|---|
| 384 |
require_dependency '././counting_loader' |
|---|
| 385 |
assert_equal 1, $counting_loaded_times |
|---|
| 386 |
assert_raises(ArgumentError) { Dependencies.load_missing_constant Object, :CountingLoader } |
|---|
| 387 |
assert_equal 1, $counting_loaded_times |
|---|
| 388 |
end |
|---|
| 389 |
end |
|---|
| 390 |
|
|---|
| 391 |
def test_const_missing_within_anonymous_module |
|---|
| 392 |
$counting_loaded_times = 0 |
|---|
| 393 |
m = Module.new |
|---|
| 394 |
m.module_eval "def a() CountingLoader; end" |
|---|
| 395 |
extend m |
|---|
| 396 |
kls = nil |
|---|
| 397 |
with_loading 'autoloading_fixtures' do |
|---|
| 398 |
kls = nil |
|---|
| 399 |
assert_nothing_raised { kls = a } |
|---|
| 400 |
assert_equal "CountingLoader", kls.name |
|---|
| 401 |
assert_equal 1, $counting_loaded_times |
|---|
| 402 |
|
|---|
| 403 |
assert_nothing_raised { kls = a } |
|---|
| 404 |
assert_equal 1, $counting_loaded_times |
|---|
| 405 |
end |
|---|
| 406 |
end |
|---|
| 407 |
|
|---|
| 408 |
def test_removal_from_tree_should_be_detected |
|---|
| 409 |
with_loading 'dependencies' do |
|---|
| 410 |
root = Dependencies.load_paths.first |
|---|
| 411 |
c = ServiceOne |
|---|
| 412 |
Dependencies.clear |
|---|
| 413 |
assert ! defined?(ServiceOne) |
|---|
| 414 |
begin |
|---|
| 415 |
Dependencies.load_missing_constant(c, :FakeMissing) |
|---|
| 416 |
flunk "Expected exception" |
|---|
| 417 |
rescue ArgumentError => e |
|---|
| 418 |
assert_match %r{ServiceOne has been removed from the module tree}i, e.message |
|---|
| 419 |
end |
|---|
| 420 |
end |
|---|
| 421 |
end |
|---|
| 422 |
|
|---|
| 423 |
def test_nested_load_error_isnt_rescued |
|---|
| 424 |
with_loading 'dependencies' do |
|---|
| 425 |
assert_raises(MissingSourceFile) do |
|---|
| 426 |
RequiresNonexistent1 |
|---|
| 427 |
end |
|---|
| 428 |
end |
|---|
| 429 |
end |
|---|
| 430 |
|
|---|
| 431 |
def test_load_once_paths_do_not_add_to_autoloaded_constants |
|---|
| 432 |
with_loading 'autoloading_fixtures' do |
|---|
| 433 |
Dependencies.load_once_paths = Dependencies.load_paths.dup |
|---|
| 434 |
|
|---|
| 435 |
assert ! Dependencies.autoloaded?("ModuleFolder") |
|---|
| 436 |
assert ! Dependencies.autoloaded?("ModuleFolder::NestedClass") |
|---|
| 437 |
assert ! Dependencies.autoloaded?(ModuleFolder) |
|---|
| 438 |
|
|---|
| 439 |
1 if ModuleFolder::NestedClass |
|---|
| 440 |
assert ! Dependencies.autoloaded?(ModuleFolder::NestedClass) |
|---|
| 441 |
end |
|---|
| 442 |
ensure |
|---|
| 443 |
Object.class_eval { remove_const :ModuleFolder } |
|---|
| 444 |
Dependencies.load_once_paths = [] |
|---|
| 445 |
end |
|---|
| 446 |
|
|---|
| 447 |
def test_application_should_special_case_application_controller |
|---|
| 448 |
with_loading 'autoloading_fixtures' do |
|---|
| 449 |
require_dependency 'application' |
|---|
| 450 |
assert_equal 10, ApplicationController |
|---|
| 451 |
assert Dependencies.autoloaded?(:ApplicationController) |
|---|
| 452 |
end |
|---|
| 453 |
end |
|---|
| 454 |
|
|---|
| 455 |
def test_const_missing_on_kernel_should_fallback_to_object |
|---|
| 456 |
with_loading 'autoloading_fixtures' do |
|---|
| 457 |
kls = Kernel::E |
|---|
| 458 |
assert_equal "E", kls.name |
|---|
| 459 |
assert_equal kls.object_id, Kernel::E.object_id |
|---|
| 460 |
end |
|---|
| 461 |
end |
|---|
| 462 |
|
|---|
| 463 |
def test_preexisting_constants_are_not_marked_as_autoloaded |
|---|
| 464 |
with_loading 'autoloading_fixtures' do |
|---|
| 465 |
require_dependency 'e' |
|---|
| 466 |
assert Dependencies.autoloaded?(:E) |
|---|
| 467 |
Dependencies.clear |
|---|
| 468 |
end |
|---|
| 469 |
|
|---|
| 470 |
Object.const_set :E, Class.new |
|---|
| 471 |
with_loading 'autoloading_fixtures' do |
|---|
| 472 |
require_dependency 'e' |
|---|
| 473 |
assert ! Dependencies.autoloaded?(:E), "E shouldn't be marked autoloaded!" |
|---|
| 474 |
Dependencies.clear |
|---|
| 475 |
end |
|---|
| 476 |
|
|---|
| 477 |
ensure |
|---|
| 478 |
Object.class_eval { remove_const :E } |
|---|
| 479 |
end |
|---|
| 480 |
|
|---|
| 481 |
def test_unloadable |
|---|
| 482 |
with_loading 'autoloading_fixtures' do |
|---|
| 483 |
Object.const_set :M, Module.new |
|---|
| 484 |
M.unloadable |
|---|
| 485 |
|
|---|
| 486 |
Dependencies.clear |
|---|
| 487 |
assert ! defined?(M) |
|---|
| 488 |
|
|---|
| 489 |
Object.const_set :M, Module.new |
|---|
| 490 |
Dependencies.clear |
|---|
| 491 |
assert ! defined?(M), "Dependencies should unload unloadable constants each time" |
|---|
| 492 |
end |
|---|
| 493 |
end |
|---|
| 494 |
|
|---|
| 495 |
def test_unloadable_should_fail_with_anonymous_modules |
|---|
| 496 |
with_loading 'autoloading_fixtures' do |
|---|
| 497 |
m = Module.new |
|---|
| 498 |
assert_raises(ArgumentError) { m.unloadable } |
|---|
| 499 |
end |
|---|
| 500 |
end |
|---|
| 501 |
|
|---|
| 502 |
def test_unloadable_should_return_change_flag |
|---|
| 503 |
with_loading 'autoloading_fixtures' do |
|---|
| 504 |
Object.const_set :M, Module.new |
|---|
| 505 |
assert_equal true, M.unloadable |
|---|
| 506 |
assert_equal false, M.unloadable |
|---|
| 507 |
end |
|---|
| 508 |
end |
|---|
| 509 |
|
|---|
| 510 |
def test_new_contants_in_without_constants |
|---|
| 511 |
assert_equal [], (Dependencies.new_constants_in(Object) { }) |
|---|
| 512 |
assert Dependencies.constant_watch_stack.empty? |
|---|
| 513 |
end |
|---|
| 514 |
|
|---|
| 515 |
def test_new_constants_in_with_a_single_constant |
|---|
| 516 |
assert_equal ["Hello"], Dependencies.new_constants_in(Object) { |
|---|
| 517 |
Object.const_set :Hello, 10 |
|---|
| 518 |
}.map(&:to_s) |
|---|
| 519 |
assert Dependencies.constant_watch_stack.empty? |
|---|
| 520 |
ensure |
|---|
| 521 |
Object.class_eval { remove_const :Hello } |
|---|
| 522 |
end |
|---|
| 523 |
|
|---|
| 524 |
def test_new_constants_in_with_nesting |
|---|
| 525 |
outer = Dependencies.new_constants_in(Object) do |
|---|
| 526 |
Object.const_set :OuterBefore, 10 |
|---|
| 527 |
|
|---|
| 528 |
assert_equal ["Inner"], Dependencies.new_constants_in(Object) { |
|---|
| 529 |
Object.const_set :Inner, 20 |
|---|
| 530 |
}.map(&:to_s) |
|---|
| 531 |
|
|---|
| 532 |
Object.const_set :OuterAfter, 30 |
|---|
| 533 |
end |
|---|
| 534 |
|
|---|
| 535 |
assert_equal ["OuterAfter", "OuterBefore"], outer.sort.map(&:to_s) |
|---|
| 536 |
assert Dependencies.constant_watch_stack.empty? |
|---|
| 537 |
ensure |
|---|
| 538 |
%w(OuterBefore Inner OuterAfter).each do |name| |
|---|
| 539 |
Object.class_eval { remove_const name if const_defined?(name) } |
|---|
| 540 |
end |
|---|
| 541 |
end |
|---|
| 542 |
|
|---|
| 543 |
def test_new_constants_in_module |
|---|
| 544 |
Object.const_set :M, Module.new |
|---|
| 545 |
|
|---|
| 546 |
outer = Dependencies.new_constants_in(M) do |
|---|
| 547 |
M.const_set :OuterBefore, 10 |
|---|
| 548 |
|
|---|
| 549 |
inner = Dependencies.new_constants_in(M) do |
|---|
| 550 |
M.const_set :Inner, 20 |
|---|
| 551 |
end |
|---|
| 552 |
assert_equal ["M::Inner"], inner |
|---|
| 553 |
|
|---|
| 554 |
M.const_set :OuterAfter, 30 |
|---|
| 555 |
end |
|---|
| 556 |
assert_equal ["M::OuterAfter", "M::OuterBefore"], outer.sort |
|---|
| 557 |
assert Dependencies.constant_watch_stack.empty? |
|---|
| 558 |
ensure |
|---|
| 559 |
Object.class_eval { remove_const :M } |
|---|
| 560 |
end |
|---|
| 561 |
|
|---|
| 562 |
def test_new_constants_in_module_using_name |
|---|
| 563 |
outer = Dependencies.new_constants_in(:M) do |
|---|
| 564 |
Object.const_set |
|---|