mirror of
https://github.com/ruby/ruby.git
synced 2025-09-19 10:33:58 +02:00
* compile.c (NODE_CLASS, NODE_MODULE), insns.def (defineclass): raise
an exception when "class Foo::Bar" is evaluated and Foo::Bar is private. To implement this, define_type of "defineclass" is added so that the instruction can distinguish whether the class definition is scoped (class Foo::Bar) or not (class Bar). * test/ruby/test_class.rb (test_redefine_private_class), test/ruby/test_module.rb (test_define_module_under_private_constant): add tests for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30714 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
f0483c494f
commit
a2ec8666cf
5 changed files with 58 additions and 8 deletions
|
@ -240,4 +240,22 @@ class TestClass < Test::Unit::TestCase
|
|||
def test_nested_class_removal
|
||||
assert_normal_exit('File.__send__(:remove_const, :Stat); at_exit{File.stat(".")}; GC.start')
|
||||
end
|
||||
|
||||
class PrivateClass
|
||||
end
|
||||
private_constant :PrivateClass
|
||||
|
||||
def test_redefine_private_class
|
||||
assert_raise(NameError) do
|
||||
eval("class ::TestClass::PrivateClass; end")
|
||||
end
|
||||
eval <<-END
|
||||
class ::TestClass
|
||||
class PrivateClass
|
||||
def foo; 42; end
|
||||
end
|
||||
end
|
||||
END
|
||||
assert_equal(42, PrivateClass.new.foo)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -947,6 +947,24 @@ class TestModule < Test::Unit::TestCase
|
|||
c.private_constant(:FOO)
|
||||
assert_raise(NameError) { c::FOO }
|
||||
assert_equal("foo", c.class_eval("FOO"))
|
||||
assert_equal("foo", c.const_get("FOO"))
|
||||
end
|
||||
|
||||
class PrivateClass
|
||||
end
|
||||
private_constant :PrivateClass
|
||||
|
||||
def test_define_module_under_private_constant
|
||||
assert_raise(NameError) do
|
||||
eval %q{class TestModule::PrivateClass; end}
|
||||
end
|
||||
assert_raise(NameError) do
|
||||
eval %q{module TestModule::PrivateClass::TestModule; end}
|
||||
end
|
||||
eval %q{class PrivateClass; end}
|
||||
eval %q{module PrivateClass::TestModule; end}
|
||||
assert_instance_of(Module, PrivateClass::TestModule)
|
||||
PrivateClass.class_eval { remove_const(:TestModule) }
|
||||
end
|
||||
|
||||
def test_public_constant
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue