1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/test/constantize_test_cases.rb
Keenan Brock a930f5ba06 Fix safe_constantize to not raise a LoadError.
### Summary

There was an issues when using `safe_constantize` on a string that has
the wrong case.

File `em.rb` defines `EM`.
`"Em".safe_constantize` causes a little confusion with the autoloader.
The autoloader finds file "em.rb",
expecting it to define `Em`, but `Em` is not defined.
The autoloader raises a `LoadError`, which is good,
But `safe_constantize` is defined to return `nil` when a class is not found.

### Before

```
"Em".safe_constantize
LoadError: Unable to autoload constant Em, \
expected rails/activesupport/test/autoloading_fixtures/em.rb to define it
```

### After

```
"Em".safe_constantize
# => nil
```
2019-01-09 14:52:46 -05:00

137 lines
4.4 KiB
Ruby

# frozen_string_literal: true
require "dependencies_test_helpers"
module Ace
module Base
class Case
class Dice
end
end
class Fase < Case
end
end
class Gas
include Base
end
end
class Object
module AddtlGlobalConstants
class Case
class Dice
end
end
end
include AddtlGlobalConstants
end
module ConstantizeTestCases
include DependenciesTestHelpers
def run_constantize_tests_on
assert_equal Ace::Base::Case, yield("Ace::Base::Case")
assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice")
assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Fase::Dice")
assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice")
assert_equal Ace::Gas::Case, yield("Ace::Gas::Case")
assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice")
assert_equal Ace::Base::Case::Dice, yield("Ace::Gas::Case::Dice")
assert_equal Case::Dice, yield("Case::Dice")
assert_equal AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
assert_equal Object::AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
assert_equal Case::Dice, yield("Object::Case::Dice")
assert_equal AddtlGlobalConstants::Case::Dice, yield("Object::Case::Dice")
assert_equal Object::AddtlGlobalConstants::Case::Dice, yield("Case::Dice")
assert_equal ConstantizeTestCases, yield("ConstantizeTestCases")
assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases")
assert_raises(NameError) { yield("UnknownClass") }
assert_raises(NameError) { yield("UnknownClass::Ace") }
assert_raises(NameError) { yield("UnknownClass::Ace::Base") }
assert_raises(NameError) { yield("An invalid string") }
assert_raises(NameError) { yield("InvalidClass\n") }
assert_raises(NameError) { yield("Ace::ConstantizeTestCases") }
assert_raises(NameError) { yield("Ace::Base::ConstantizeTestCases") }
assert_raises(NameError) { yield("Ace::Gas::Base") }
assert_raises(NameError) { yield("Ace::Gas::ConstantizeTestCases") }
assert_raises(NameError) { yield("") }
assert_raises(NameError) { yield("::") }
assert_raises(NameError) { yield("Ace::gas") }
assert_raises(NameError) do
with_autoloading_fixtures do
yield("RaisesNameError")
end
end
assert_raises(NoMethodError) do
with_autoloading_fixtures do
yield("RaisesNoMethodError")
end
end
with_autoloading_fixtures do
yield("Prepend::SubClassConflict")
assert_equal "constant", defined?(Prepend::SubClassConflict)
end
end
def run_safe_constantize_tests_on
assert_equal Ace::Base::Case, yield("Ace::Base::Case")
assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice")
assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice")
assert_equal Ace::Gas::Case, yield("Ace::Gas::Case")
assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice")
assert_equal Case::Dice, yield("Case::Dice")
assert_equal Case::Dice, yield("Object::Case::Dice")
assert_equal ConstantizeTestCases, yield("ConstantizeTestCases")
assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases")
assert_nil yield("")
assert_nil yield("::")
assert_nil yield("UnknownClass")
assert_nil yield("UnknownClass::Ace")
assert_nil yield("UnknownClass::Ace::Base")
assert_nil yield("An invalid string")
assert_nil yield("InvalidClass\n")
assert_nil yield("blargle")
assert_nil yield("Ace::ConstantizeTestCases")
assert_nil yield("Ace::Base::ConstantizeTestCases")
assert_nil yield("Ace::Gas::Base")
assert_nil yield("Ace::Gas::ConstantizeTestCases")
assert_nil yield("#<Class:0x7b8b718b>::Nested_1")
assert_nil yield("Ace::gas")
assert_nil yield("Object::ABC")
assert_nil yield("Object::Object::Object::ABC")
assert_nil yield("A::Object::B")
assert_nil yield("A::Object::Object::Object::B")
with_autoloading_fixtures do
assert_nil yield("Em")
end
assert_raises(LoadError) do
with_autoloading_fixtures do
yield("RaisesLoadError")
end
end
assert_raises(NameError) do
with_autoloading_fixtures do
yield("RaisesNameError")
end
end
assert_raises(NoMethodError) do
with_autoloading_fixtures do
yield("RaisesNoMethodError")
end
end
end
end