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
Jean Boussier 11e05defec Fix constantize edge case involving prepend, autoloading and name conflicts
In the following situation:

```ruby
class Bar
end

module Baz
end

class Foo
  prepend Baz
end

class Foo::Bar
end
```

Running `Inflector.constantize('Foo::Bar')` would blow up with a NameError.

What is happening is that `constatize` was written before the introduction
of prepend, and wrongly assume that `klass.ancestors.first == klass`.

So it uses `klass.ancestors.inject` without arguments, as a result
a prepended module is used in place of the actual class.
2016-12-14 14:25:43 +01:00

125 lines
4.2 KiB
Ruby

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")
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