summaryrefslogtreecommitdiff
path: root/test/ruby/test_module.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_module.rb')
-rw-r--r--test/ruby/test_module.rb335
1 files changed, 60 insertions, 275 deletions
diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb
index 5cfda018f3..076ea0901f 100644
--- a/test/ruby/test_module.rb
+++ b/test/ruby/test_module.rb
@@ -28,13 +28,10 @@ class TestModule < Test::Unit::TestCase
def setup
@verbose = $VERBOSE
$VERBOSE = nil
- @deprecated = Warning[:deprecated]
- Warning[:deprecated] = true
end
def teardown
$VERBOSE = @verbose
- Warning[:deprecated] = @deprecated
end
def test_LT_0
@@ -297,11 +294,8 @@ class TestModule < Test::Unit::TestCase
end
def test_nested_get
- assert_equal Other, Object.const_get([self.class, 'Other'].join('::'))
+ assert_equal Other, Object.const_get([self.class, Other].join('::'))
assert_equal User::USER, self.class.const_get([User, 'USER'].join('::'))
- assert_raise(NameError) {
- Object.const_get([self.class.name, 'String'].join('::'))
- }
end
def test_nested_get_symbol
@@ -334,7 +328,6 @@ class TestModule < Test::Unit::TestCase
assert_send([Object, :const_defined?, [self.class.name, 'Other'].join('::')])
assert_send([self.class, :const_defined?, 'User::USER'])
assert_not_send([self.class, :const_defined?, 'User::Foo'])
- assert_not_send([Object, :const_defined?, [self.class.name, 'String'].join('::')])
end
def test_nested_defined_symbol
@@ -345,17 +338,6 @@ class TestModule < Test::Unit::TestCase
assert_raise(NameError) {self.class.const_defined?(const)}
end
- def test_nested_defined_inheritance
- assert_send([Object, :const_defined?, [self.class.name, 'User', 'MIXIN'].join('::')])
- assert_send([self.class, :const_defined?, 'User::MIXIN'])
- assert_send([Object, :const_defined?, 'File::SEEK_SET'])
-
- # const_defined? with `false`
- assert_not_send([Object, :const_defined?, [self.class.name, 'User', 'MIXIN'].join('::'), false])
- assert_not_send([self.class, :const_defined?, 'User::MIXIN', false])
- assert_not_send([Object, :const_defined?, 'File::SEEK_SET', false])
- end
-
def test_nested_defined_bad_class
assert_raise(TypeError) do
self.class.const_defined?('User::USER::Foo')
@@ -480,48 +462,26 @@ class TestModule < Test::Unit::TestCase
assert_equal([:cClass], (class << CClass; self; end).instance_methods(false))
assert_equal([], (class << BClass; self; end).instance_methods(false))
assert_equal([:cm2], (class << AClass; self; end).instance_methods(false))
+ # Ruby 1.8 feature change:
+ # #instance_methods includes protected methods.
+ #assert_equal([:aClass], AClass.instance_methods(false))
assert_equal([:aClass, :aClass2], AClass.instance_methods(false).sort)
assert_equal([:aClass, :aClass2],
(AClass.instance_methods(true) - Object.instance_methods(true)).sort)
end
def test_method_defined?
- [User, Class.new{include User}, Class.new{prepend User}].each do |klass|
- [[], [true]].each do |args|
- assert !klass.method_defined?(:wombat, *args)
- assert klass.method_defined?(:mixin, *args)
- assert klass.method_defined?(:user, *args)
- assert klass.method_defined?(:user2, *args)
- assert !klass.method_defined?(:user3, *args)
-
- assert !klass.method_defined?("wombat", *args)
- assert klass.method_defined?("mixin", *args)
- assert klass.method_defined?("user", *args)
- assert klass.method_defined?("user2", *args)
- assert !klass.method_defined?("user3", *args)
- end
- end
- end
-
- def test_method_defined_without_include_super
- assert User.method_defined?(:user, false)
- assert !User.method_defined?(:mixin, false)
- assert Mixin.method_defined?(:mixin, false)
-
- User.const_set(:FOO, c = Class.new)
-
- c.prepend(User)
- assert !c.method_defined?(:user, false)
- c.define_method(:user){}
- assert c.method_defined?(:user, false)
+ assert !User.method_defined?(:wombat)
+ assert User.method_defined?(:mixin)
+ assert User.method_defined?(:user)
+ assert User.method_defined?(:user2)
+ assert !User.method_defined?(:user3)
- assert !c.method_defined?(:mixin, false)
- c.define_method(:mixin){}
- assert c.method_defined?(:mixin, false)
-
- assert !c.method_defined?(:userx, false)
- c.define_method(:userx){}
- assert c.method_defined?(:userx, false)
+ assert !User.method_defined?("wombat")
+ assert User.method_defined?("mixin")
+ assert User.method_defined?("user")
+ assert User.method_defined?("user2")
+ assert !User.method_defined?("user3")
end
def module_exec_aux
@@ -566,28 +526,6 @@ class TestModule < Test::Unit::TestCase
assert_equal("Integer", Integer.name)
assert_equal("TestModule::Mixin", Mixin.name)
assert_equal("TestModule::User", User.name)
-
- assert_predicate Integer.name, :frozen?
- assert_predicate Mixin.name, :frozen?
- assert_predicate User.name, :frozen?
- end
-
- def test_accidental_singleton_naming_with_module
- o = Object.new
- assert_nil(o.singleton_class.name)
- class << o
- module Hi; end
- end
- assert_nil(o.singleton_class.name)
- end
-
- def test_accidental_singleton_naming_with_class
- o = Object.new
- assert_nil(o.singleton_class.name)
- class << o
- class Hi; end
- end
- assert_nil(o.singleton_class.name)
end
def test_classpath
@@ -609,8 +547,6 @@ class TestModule < Test::Unit::TestCase
assert_equal(prefix+"N", m.const_get(:N).name)
assert_equal(prefix+"O", m.const_get(:O).name)
assert_equal(prefix+"C", m.const_get(:C).name)
- c = m.class_eval("Bug15891 = Class.new.freeze")
- assert_equal(prefix+"Bug15891", c.name)
end
def test_private_class_method
@@ -730,32 +666,6 @@ class TestModule < Test::Unit::TestCase
assert_equal(false, o.respond_to?(:bar=))
end
- def test_attr_public_at_toplevel
- s = Object.new
- TOPLEVEL_BINDING.eval(<<-END).call(s.singleton_class)
- proc do |c|
- c.send(:attr_accessor, :x)
- c.send(:attr, :y)
- c.send(:attr_reader, :z)
- c.send(:attr_writer, :w)
- end
- END
- assert_nil s.x
- s.x = 1
- assert_equal 1, s.x
-
- assert_nil s.y
- s.instance_variable_set(:@y, 2)
- assert_equal 2, s.y
-
- assert_nil s.z
- s.instance_variable_set(:@z, 3)
- assert_equal 3, s.z
-
- s.w = 4
- assert_equal 4, s.instance_variable_get(:@w)
- end
-
def test_const_get_evaled
c1 = Class.new
c2 = Class.new(c1)
@@ -813,6 +723,10 @@ class TestModule < Test::Unit::TestCase
assert_raise(NameError) { c1.const_get(:foo) }
bug5084 = '[ruby-dev:44200]'
assert_raise(TypeError, bug5084) { c1.const_get(1) }
+ bug7574 = '[ruby-dev:46749]'
+ assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) {
+ Object.const_get("String\0")
+ }
end
def test_const_defined_invalid_name
@@ -820,6 +734,10 @@ class TestModule < Test::Unit::TestCase
assert_raise(NameError) { c1.const_defined?(:foo) }
bug5084 = '[ruby-dev:44200]'
assert_raise(TypeError, bug5084) { c1.const_defined?(1) }
+ bug7574 = '[ruby-dev:46749]'
+ assert_raise_with_message(NameError, "wrong constant name \"String\\u0000\"", bug7574) {
+ Object.const_defined?("String\0")
+ }
end
def test_const_get_no_inherited
@@ -1059,8 +977,8 @@ class TestModule < Test::Unit::TestCase
end
def test_method_defined
- cl = Class.new
- def_methods = proc do
+ c = Class.new
+ c.class_eval do
def foo; end
def bar; end
def baz; end
@@ -1068,47 +986,33 @@ class TestModule < Test::Unit::TestCase
protected :bar
private :baz
end
- cl.class_eval(&def_methods)
- sc = Class.new(cl)
- mod = Module.new(&def_methods)
- only_prepend = Class.new{prepend(mod)}
- empty_prepend = cl.clone
- empty_prepend.prepend(Module.new)
- overlap_prepend = cl.clone
- overlap_prepend.prepend(mod)
- [[], [true], [false]].each do |args|
- [cl, sc, only_prepend, empty_prepend, overlap_prepend].each do |c|
- always_false = [sc, only_prepend].include?(c) && args == [false]
+ assert_equal(true, c.public_method_defined?(:foo))
+ assert_equal(false, c.public_method_defined?(:bar))
+ assert_equal(false, c.public_method_defined?(:baz))
- assert_equal(always_false ? false : true, c.public_method_defined?(:foo, *args))
- assert_equal(always_false ? false : false, c.public_method_defined?(:bar, *args))
- assert_equal(always_false ? false : false, c.public_method_defined?(:baz, *args))
+ # Test if string arguments are converted to symbols
+ assert_equal(true, c.public_method_defined?("foo"))
+ assert_equal(false, c.public_method_defined?("bar"))
+ assert_equal(false, c.public_method_defined?("baz"))
- # Test if string arguments are converted to symbols
- assert_equal(always_false ? false : true, c.public_method_defined?("foo", *args))
- assert_equal(always_false ? false : false, c.public_method_defined?("bar", *args))
- assert_equal(always_false ? false : false, c.public_method_defined?("baz", *args))
+ assert_equal(false, c.protected_method_defined?(:foo))
+ assert_equal(true, c.protected_method_defined?(:bar))
+ assert_equal(false, c.protected_method_defined?(:baz))
- assert_equal(always_false ? false : false, c.protected_method_defined?(:foo, *args))
- assert_equal(always_false ? false : true, c.protected_method_defined?(:bar, *args))
- assert_equal(always_false ? false : false, c.protected_method_defined?(:baz, *args))
+ # Test if string arguments are converted to symbols
+ assert_equal(false, c.protected_method_defined?("foo"))
+ assert_equal(true, c.protected_method_defined?("bar"))
+ assert_equal(false, c.protected_method_defined?("baz"))
- # Test if string arguments are converted to symbols
- assert_equal(always_false ? false : false, c.protected_method_defined?("foo", *args))
- assert_equal(always_false ? false : true, c.protected_method_defined?("bar", *args))
- assert_equal(always_false ? false : false, c.protected_method_defined?("baz", *args))
+ assert_equal(false, c.private_method_defined?(:foo))
+ assert_equal(false, c.private_method_defined?(:bar))
+ assert_equal(true, c.private_method_defined?(:baz))
- assert_equal(always_false ? false : false, c.private_method_defined?(:foo, *args))
- assert_equal(always_false ? false : false, c.private_method_defined?(:bar, *args))
- assert_equal(always_false ? false : true, c.private_method_defined?(:baz, *args))
-
- # Test if string arguments are converted to symbols
- assert_equal(always_false ? false : false, c.private_method_defined?("foo", *args))
- assert_equal(always_false ? false : false, c.private_method_defined?("bar", *args))
- assert_equal(always_false ? false : true, c.private_method_defined?("baz", *args))
- end
- end
+ # Test if string arguments are converted to symbols
+ assert_equal(false, c.private_method_defined?("foo"))
+ assert_equal(false, c.private_method_defined?("bar"))
+ assert_equal(true, c.private_method_defined?("baz"))
end
def test_top_public_private
@@ -1397,17 +1301,6 @@ class TestModule < Test::Unit::TestCase
assert_match(/: warning: previous definition of foo/, stderr)
end
- def test_module_function_inside_method
- assert_warn(/calling module_function without arguments inside a method may not have the intended effect/, '[ruby-core:79751]') do
- Module.new do
- def self.foo
- module_function
- end
- foo
- end
- end
- end
-
def test_protected_singleton_method
klass = Class.new
x = klass.new
@@ -1532,21 +1425,6 @@ class TestModule < Test::Unit::TestCase
RUBY
end
- def test_private_constant_const_missing
- c = Class.new
- c.const_set(:FOO, "foo")
- c.private_constant(:FOO)
- class << c
- attr_reader :const_missing_arg
- def const_missing(name)
- @const_missing_arg = name
- name == :FOO ? const_get(:FOO) : super
- end
- end
- assert_equal("foo", c::FOO)
- assert_equal(:FOO, c.const_missing_arg)
- end
-
class PrivateClass
end
private_constant :PrivateClass
@@ -1579,31 +1457,10 @@ class TestModule < Test::Unit::TestCase
c = Class.new
c.const_set(:FOO, "foo")
c.deprecate_constant(:FOO)
- assert_warn(/deprecated/) do
- Warning[:deprecated] = true
- c::FOO
- end
- assert_warn(/#{c}::FOO is deprecated/) do
- Warning[:deprecated] = true
- Class.new(c)::FOO
- end
+ assert_warn(/deprecated/) {c::FOO}
+ assert_warn(/#{c}::FOO is deprecated/) {Class.new(c)::FOO}
bug12382 = '[ruby-core:75505] [Bug #12382]'
- assert_warn(/deprecated/, bug12382) do
- Warning[:deprecated] = true
- c.class_eval "FOO"
- end
- assert_warn('') do
- Warning[:deprecated] = false
- c::FOO
- end
- assert_warn('') do
- Warning[:deprecated] = false
- Class.new(c)::FOO
- end
- assert_warn('') do
- Warning[:deprecated] = false
- c.class_eval "FOO"
- end
+ assert_warn(/deprecated/, bug12382) {c.class_eval "FOO"}
end
def test_constants_with_private_constant
@@ -1924,29 +1781,6 @@ class TestModule < Test::Unit::TestCase
assert_equal(0, 1 / 2)
end
- def test_visibility_after_refine_and_visibility_change
- m = Module.new
- c = Class.new do
- def x; :x end
- end
- c.prepend(m)
- Module.new do
- refine c do
- def x; :y end
- end
- end
-
- o1 = c.new
- o2 = c.new
- assert_equal(:x, o1.public_send(:x))
- assert_equal(:x, o2.public_send(:x))
- o1.singleton_class.send(:private, :x)
- o2.singleton_class.send(:public, :x)
-
- assert_raise(NoMethodError) { o1.public_send(:x) }
- assert_equal(:x, o2.public_send(:x))
- end
-
def test_prepend_visibility
bug8005 = '[ruby-core:53106] [Bug #8005]'
c = Class.new do
@@ -2173,13 +2007,17 @@ class TestModule < Test::Unit::TestCase
$foo
\u3042$
].each do |name|
- e = assert_raise(NameError) do
+ assert_raise_with_message(NameError, /#{Regexp.quote(quote(name))}/) do
Module.new { attr_accessor name.to_sym }
end
- assert_equal(name, e.name.to_s)
end
end
+ private def quote(name)
+ encoding = Encoding.default_internal || Encoding.default_external
+ (name.encoding == encoding || name.ascii_only?) ? name : name.inspect
+ end
+
class AttrTest
class << self
attr_accessor :cattr
@@ -2408,7 +2246,7 @@ class TestModule < Test::Unit::TestCase
A.prepend InspectIsShallow
- expect = "#<Method: A(ShallowInspect)#inspect(shallow_inspect)() -:7>"
+ expect = "#<Method: A(ShallowInspect)#inspect(shallow_inspect)>"
assert_equal expect, A.new.method(:inspect).inspect, "#{bug_10282}"
RUBY
end
@@ -2434,17 +2272,15 @@ class TestModule < Test::Unit::TestCase
def test_redefinition_mismatch
m = Module.new
- m.module_eval "A = 1", __FILE__, line = __LINE__
- e = assert_raise_with_message(TypeError, /is not a module/) {
+ m.module_eval "A = 1"
+ assert_raise_with_message(TypeError, /is not a module/) {
m.module_eval "module A; end"
}
- assert_include(e.message, "#{__FILE__}:#{line}: previous definition")
n = "M\u{1f5ff}"
- m.module_eval "#{n} = 42", __FILE__, line = __LINE__
- e = assert_raise_with_message(TypeError, /#{n} is not a module/) {
+ m.module_eval "#{n} = 42"
+ assert_raise_with_message(TypeError, "#{n} is not a module") {
m.module_eval "module #{n}; end"
}
- assert_include(e.message, "#{__FILE__}:#{line}: previous definition")
assert_separately([], <<-"end;")
Etc = (class C\u{1f5ff}; self; end).new
@@ -2472,56 +2308,6 @@ class TestModule < Test::Unit::TestCase
}
end
- ConstLocation = [__FILE__, __LINE__]
-
- def test_const_source_location
- assert_equal(ConstLocation, self.class.const_source_location(:ConstLocation))
- assert_equal(ConstLocation, self.class.const_source_location("ConstLocation"))
- assert_equal(ConstLocation, Object.const_source_location("#{self.class.name}::ConstLocation"))
- assert_raise(TypeError) {
- self.class.const_source_location(nil)
- }
- assert_raise_with_message(NameError, /wrong constant name/) {
- self.class.const_source_location("xxx")
- }
- assert_raise_with_message(TypeError, %r'does not refer to class/module') {
- self.class.const_source_location("ConstLocation::FILE")
- }
- end
-
- module CloneTestM_simple
- C = 1
- def self.m; C; end
- end
-
- module CloneTestM0
- def foo; TEST; end
- end
-
- CloneTestM1 = CloneTestM0.clone
- CloneTestM2 = CloneTestM0.clone
- module CloneTestM1
- TEST = :M1
- end
- module CloneTestM2
- TEST = :M2
- end
- class CloneTestC1
- include CloneTestM1
- end
- class CloneTestC2
- include CloneTestM2
- end
-
- def test_constant_access_from_method_in_cloned_module
- m = CloneTestM_simple.dup
- assert_equal 1, m::C, '[ruby-core:47834]'
- assert_equal 1, m.m, '[ruby-core:47834]'
-
- assert_equal :M1, CloneTestC1.new.foo, '[Bug #15877]'
- assert_equal :M2, CloneTestC2.new.foo, '[Bug #15877]'
- end
-
private
def assert_top_method_is_private(method)
@@ -2530,8 +2316,7 @@ class TestModule < Test::Unit::TestCase
assert_include(methods, :#{method}, ":#{method} should be private")
assert_raise_with_message(NoMethodError, "private method `#{method}' called for main:Object") {
- recv = self
- recv.#{method}
+ self.#{method}
}
}
end