mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* test/-ext-/symbol/test_inadvertent_creation.rb: moved tests from
test_module.rb and test_symbol.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33425 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e50c05f7f6
commit
881891fb2f
3 changed files with 125 additions and 91 deletions
|
@ -3,10 +3,10 @@ require "-test-/symbol/symbol"
|
|||
|
||||
module Test_Symbol
|
||||
class TestInadvertent < Test::Unit::TestCase
|
||||
def self.noninterned_name
|
||||
th = Thread.current.object_id.to_s(36)
|
||||
def noninterned_name(prefix = "_")
|
||||
prefix += Thread.current.object_id.to_s(36)
|
||||
begin
|
||||
name = "#{th}.#{rand(0x1000).to_s(16)}.#{Time.now.usec}"
|
||||
name = "#{prefix}_#{rand(0x1000).to_s(16)}_#{Time.now.usec}"
|
||||
end while Bug::Symbol.interned?(name)
|
||||
name
|
||||
end
|
||||
|
@ -15,10 +15,129 @@ module Test_Symbol
|
|||
@obj = Object.new
|
||||
end
|
||||
|
||||
def assert_not_interned(name, msg = nil)
|
||||
assert_not_send([Bug::Symbol, :interned?, name], msg)
|
||||
end
|
||||
|
||||
def assert_not_interned_error(obj, meth, name, msg = nil)
|
||||
e = assert_raise(NameError, msg) {obj.__send__(meth, name)}
|
||||
assert_not_interned(name, msg)
|
||||
e
|
||||
end
|
||||
|
||||
def assert_not_interned_false(obj, meth, name, msg = nil)
|
||||
assert_not_send([obj, meth, name], msg)
|
||||
assert_not_interned(name, msg)
|
||||
end
|
||||
|
||||
Feature5072 = '[ruby-core:38367]'
|
||||
|
||||
def test_module_const_get
|
||||
cl = Class.new
|
||||
name = noninterned_name("A")
|
||||
|
||||
assert_not_interned_false(cl, :const_defined?, name, Feature5072)
|
||||
end
|
||||
|
||||
def test_respond_to_missing
|
||||
feature5072 = Feature5072
|
||||
c = Class.new do
|
||||
def self.respond_to_missing?(*)
|
||||
super
|
||||
end
|
||||
end
|
||||
s = noninterned_name
|
||||
|
||||
# assert_not_interned_false(c, :respond_to?, s, feature5072)
|
||||
assert_not_interned_false(c, :method_defined?, s, feature5072)
|
||||
assert_not_interned_false(c, :public_method_defined?, s, feature5072)
|
||||
assert_not_interned_false(c, :private_method_defined?, s, feature5072)
|
||||
assert_not_interned_false(c, :protected_method_defined?, s, feature5072)
|
||||
assert_not_interned_false(c, :const_defined?, noninterned_name("A"), feature5072)
|
||||
assert_not_interned_false(c, :instance_variable_defined?, noninterned_name("@"), feature5072)
|
||||
assert_not_interned_false(c, :class_variable_defined?, noninterned_name("@@"), feature5072)
|
||||
end
|
||||
|
||||
Feature5079 = '[ruby-core:38404]'
|
||||
|
||||
def test_undefined_instance_variable
|
||||
feature5079 = feature5079
|
||||
c = Class.new
|
||||
iv = noninterned_name("@")
|
||||
|
||||
assert_not_interned_false(c, :instance_variable_get, iv, feature5079)
|
||||
assert_not_interned_error(c, :remove_instance_variable, iv, feature5079)
|
||||
end
|
||||
|
||||
def test_undefined_class_variable
|
||||
feature5079 = feature5079
|
||||
c = Class.new
|
||||
cv = noninterned_name("@@")
|
||||
|
||||
assert_not_interned_error(c, :class_variable_get, cv, feature5079)
|
||||
assert_not_interned_error(c, :remove_class_variable, cv, feature5079)
|
||||
end
|
||||
|
||||
|
||||
def test_undefined_const
|
||||
feature5079 = feature5079
|
||||
c = Class.new
|
||||
s = noninterned_name("A")
|
||||
|
||||
assert_not_interned_error(c, :remove_const, s, feature5079)
|
||||
end
|
||||
|
||||
def test_undefined_method
|
||||
feature5079 = feature5079
|
||||
c = Class.new
|
||||
s = noninterned_name
|
||||
|
||||
assert_not_interned_error(c, :method, s, feature5079)
|
||||
assert_not_interned_error(c, :public_method, s, feature5079)
|
||||
assert_not_interned_error(c, :instance_method, s, feature5079)
|
||||
assert_not_interned_error(c, :public_instance_method, s, feature5079)
|
||||
end
|
||||
|
||||
Feature5089 = '[ruby-core:38447]'
|
||||
def test_const_missing
|
||||
feature5089 = Feature5089
|
||||
c = Class.new do
|
||||
def self.const_missing(const_name)
|
||||
raise NameError, const_name.to_s
|
||||
end
|
||||
end
|
||||
s = noninterned_name("A")
|
||||
|
||||
# assert_not_interned_error(c, :const_get, s, feature5089)
|
||||
assert_not_interned_false(c, :autoload?, s, feature5089)
|
||||
end
|
||||
|
||||
def test_aliased_method
|
||||
feature5089 = Feature5089
|
||||
c = Class.new do
|
||||
def self.alias_method(str)
|
||||
super(:puts, str)
|
||||
end
|
||||
end
|
||||
s = noninterned_name
|
||||
|
||||
assert_not_interned_error(c, :alias_method, s, feature5089)
|
||||
assert_not_interned_error(c, :private_class_method, s, feature5089)
|
||||
assert_not_interned_error(c, :private_constant, s, feature5089)
|
||||
assert_not_interned_error(c, :private, s, feature5089)
|
||||
assert_not_interned_error(c, :protected, s, feature5089)
|
||||
assert_not_interned_error(c, :public, s, feature5089)
|
||||
assert_not_interned_error(c, :public_class_method, s, feature5089)
|
||||
assert_not_interned_error(c, :public_constant, s, feature5089)
|
||||
assert_not_interned_error(c, :remove_method, s, feature5089)
|
||||
assert_not_interned_error(c, :undef_method, s, feature5089)
|
||||
assert_not_interned_error(c, :untrace_var, s, feature5089)
|
||||
end
|
||||
|
||||
Feature5112 = '[ruby-core:38576]'
|
||||
|
||||
def test_public_send
|
||||
name = self.class.noninterned_name
|
||||
name = noninterned_name
|
||||
e = assert_raise(NoMethodError) {@obj.public_send(name, Feature5112)}
|
||||
assert_not_send([Bug::Symbol, :interned?, name])
|
||||
assert_equal(name, e.name)
|
||||
|
@ -26,7 +145,7 @@ module Test_Symbol
|
|||
end
|
||||
|
||||
def test_send
|
||||
name = self.class.noninterned_name
|
||||
name = noninterned_name
|
||||
e = assert_raise(NoMethodError) {@obj.send(name, Feature5112)}
|
||||
assert_not_send([Bug::Symbol, :interned?, name])
|
||||
assert_equal(name, e.name)
|
||||
|
@ -34,7 +153,7 @@ module Test_Symbol
|
|||
end
|
||||
|
||||
def test___send__
|
||||
name = self.class.noninterned_name
|
||||
name = noninterned_name
|
||||
e = assert_raise(NoMethodError) {@obj.__send__(name, Feature5112)}
|
||||
assert_not_send([Bug::Symbol, :interned?, name])
|
||||
assert_equal(name, e.name)
|
||||
|
|
|
@ -507,9 +507,6 @@ class TestModule < Test::Unit::TestCase
|
|||
def test_const_get_invalid_name
|
||||
c1 = Class.new
|
||||
assert_raise(NameError) { c1.const_defined?(:foo) }
|
||||
name = "gadzooks"
|
||||
assert !Symbol.all_symbols.any? {|sym| sym.to_s == name}
|
||||
assert_raise(NameError) { c1.const_defined?(name) }
|
||||
bug5084 = '[ruby-dev:44200]'
|
||||
assert_raise(TypeError, bug5084) { c1.const_defined?(1) }
|
||||
end
|
||||
|
|
|
@ -161,86 +161,4 @@ class TestSymbol < Test::Unit::TestCase
|
|||
assert_equal(':"\\u3042\\u3044\\u3046"', "\u3042\u3044\u3046".encode(e).to_sym.inspect)
|
||||
end
|
||||
end
|
||||
|
||||
def test_no_inadvertent_symbol_creation
|
||||
feature5072 = '[ruby-core:38367]'
|
||||
c = Class.new do
|
||||
def self.respond_to_missing?(*)
|
||||
super
|
||||
end
|
||||
end
|
||||
s = "gadzooks"
|
||||
{
|
||||
#:respond_to? => "#{s}1",
|
||||
:method_defined? => "#{s}2",
|
||||
:public_method_defined? => "#{s}3", :private_method_defined? => "#{s}4",
|
||||
:protected_method_defined? => "#{s}5", :const_defined? => "A#{s}",
|
||||
:instance_variable_defined? => "@#{s}", :class_variable_defined? => "@@#{s}"
|
||||
}.each do |meth, str|
|
||||
msg = "#{meth}(#{str}) #{feature5072}"
|
||||
assert !c.send(meth, str), msg
|
||||
assert !Symbol.all_symbols.any? {|sym| sym.to_s == str}, msg
|
||||
end
|
||||
end
|
||||
|
||||
def test_no_inadvertent_symbol_creation2
|
||||
feature5079 = '[ruby-core:38404]'
|
||||
c = Class.new
|
||||
s = "gadzoooks"
|
||||
{
|
||||
:instance_variable_get => ["@#{s}1", nil],
|
||||
:class_variable_get => ["@@#{s}1", NameError],
|
||||
:remove_instance_variable => ["@#{s}2", NameError],
|
||||
:remove_class_variable => ["@@#{s}2", NameError],
|
||||
:remove_const => ["A#{s}", NameError],
|
||||
:method => ["#{s}1", NameError],
|
||||
:public_method => ["#{s}2", NameError],
|
||||
:instance_method => ["#{s}3", NameError],
|
||||
:public_instance_method => ["#{s}4", NameError],
|
||||
}.each do |meth, (str, ret)|
|
||||
msg = "#{meth}(#{str}) #{feature5079}"
|
||||
if ret.is_a?(Class) && (ret < Exception)
|
||||
assert_raise(ret, msg) {c.send(meth, str)}
|
||||
else
|
||||
assert_equal(ret, c.send(meth, str), msg)
|
||||
end
|
||||
assert !Symbol.all_symbols.any? {|sym| sym.to_s == str}, msg
|
||||
end
|
||||
end
|
||||
|
||||
def test_no_inadvertent_symbol_creation3
|
||||
feature5089 = '[ruby-core:38447]'
|
||||
c = Class.new do
|
||||
def self.const_missing(const_name)
|
||||
raise NameError, const_name.to_s
|
||||
end
|
||||
def self.alias_method(str)
|
||||
super(:puts, str)
|
||||
end
|
||||
end
|
||||
s = "gadzoooks"
|
||||
{
|
||||
:alias_method => ["#{s}1", NameError],
|
||||
:autoload? => ["#{s}2", nil],
|
||||
# :const_get => ["A#{s}3", NameError],
|
||||
:private_class_method => ["#{s}4", NameError],
|
||||
:private_constant => ["#{s}5", NameError],
|
||||
:private => ["#{s}6", NameError],
|
||||
:protected => ["#{s}7", NameError],
|
||||
:public => ["#{s}8", NameError],
|
||||
:public_class_method => ["#{s}9", NameError],
|
||||
:public_constant => ["#{s}10", NameError],
|
||||
:remove_method => ["#{s}11", NameError],
|
||||
:undef_method => ["#{s}12", NameError],
|
||||
:untrace_var => ["#{s}13", NameError],
|
||||
}.each do |meth, (str, ret)|
|
||||
msg = "#{meth}(#{str}) #{feature5089}"
|
||||
if ret.is_a?(Class) && (ret < Exception)
|
||||
assert_raise(ret, msg) {c.send(meth, str)}
|
||||
else
|
||||
assert_equal(ret, c.send(meth, str), msg)
|
||||
end
|
||||
assert(!Symbol.all_symbols.any? {|sym| sym.to_s == str}, msg)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue