diff options
Diffstat (limited to 'test/ruby/test_method.rb')
| -rw-r--r-- | test/ruby/test_method.rb | 105 |
1 files changed, 52 insertions, 53 deletions
diff --git a/test/ruby/test_method.rb b/test/ruby/test_method.rb index 9f34164a77..ac50a9d0b0 100644 --- a/test/ruby/test_method.rb +++ b/test/ruby/test_method.rb @@ -318,17 +318,6 @@ class TestMethod < Test::Unit::TestCase assert_equal(:foo, o.foo) end - PUBLIC_SINGLETON_TEST = Object.new - class << PUBLIC_SINGLETON_TEST - private - PUBLIC_SINGLETON_TEST.define_singleton_method(:dsm){} - def PUBLIC_SINGLETON_TEST.def; end - end - def test_define_singleton_method_public - assert_nil(PUBLIC_SINGLETON_TEST.dsm) - assert_nil(PUBLIC_SINGLETON_TEST.def) - end - def test_define_singleton_method_no_proc o = Object.new assert_raise(ArgumentError) { @@ -577,9 +566,9 @@ class TestMethod < Test::Unit::TestCase assert_equal([[:req, :a], [:rest, :b], [:req, :c]], method(:mo5).parameters) assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:mo6).parameters) assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:mo7).parameters) - assert_equal([[:req, :a], [:opt, :b], [:rest, :*], [:req, :d], [:block, :e]], method(:mo8).parameters) + assert_equal([[:req, :a], [:opt, :b], [:rest], [:req, :d], [:block, :e]], method(:mo8).parameters) assert_equal([[:req], [:block, :b]], method(:ma1).parameters) - assert_equal([[:keyrest, :**]], method(:mk1).parameters) + assert_equal([[:keyrest]], method(:mk1).parameters) assert_equal([[:keyrest, :o]], method(:mk2).parameters) assert_equal([[:req, :a], [:keyrest, :o]], method(:mk3).parameters) assert_equal([[:opt, :a], [:keyrest, :o]], method(:mk4).parameters) @@ -603,9 +592,9 @@ class TestMethod < Test::Unit::TestCase assert_equal([[:req, :a], [:rest, :b], [:req, :c]], self.class.instance_method(:mo5).parameters) assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], self.class.instance_method(:mo6).parameters) assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], self.class.instance_method(:mo7).parameters) - assert_equal([[:req, :a], [:opt, :b], [:rest, :*], [:req, :d], [:block, :e]], self.class.instance_method(:mo8).parameters) + assert_equal([[:req, :a], [:opt, :b], [:rest], [:req, :d], [:block, :e]], self.class.instance_method(:mo8).parameters) assert_equal([[:req], [:block, :b]], self.class.instance_method(:ma1).parameters) - assert_equal([[:keyrest, :**]], self.class.instance_method(:mk1).parameters) + assert_equal([[:keyrest]], self.class.instance_method(:mk1).parameters) assert_equal([[:keyrest, :o]], self.class.instance_method(:mk2).parameters) assert_equal([[:req, :a], [:keyrest, :o]], self.class.instance_method(:mk3).parameters) assert_equal([[:opt, :a], [:keyrest, :o]], self.class.instance_method(:mk4).parameters) @@ -630,7 +619,7 @@ class TestMethod < Test::Unit::TestCase assert_equal([[:req, :a], [:rest, :b], [:req, :c], [:block, :d]], method(:pmo6).parameters) assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], method(:pmo7).parameters) assert_equal([[:req], [:block, :b]], method(:pma1).parameters) - assert_equal([[:keyrest, :**]], method(:pmk1).parameters) + assert_equal([[:keyrest]], method(:pmk1).parameters) assert_equal([[:keyrest, :o]], method(:pmk2).parameters) assert_equal([[:req, :a], [:keyrest, :o]], method(:pmk3).parameters) assert_equal([[:opt, :a], [:keyrest, :o]], method(:pmk4).parameters) @@ -654,7 +643,7 @@ class TestMethod < Test::Unit::TestCase assert_equal([[:req, :a], [:opt, :b], [:rest, :c], [:req, :d], [:block, :e]], self.class.instance_method(:pmo7).parameters) assert_equal([[:req], [:block, :b]], self.class.instance_method(:pma1).parameters) assert_equal([[:req], [:block, :b]], self.class.instance_method(:pma1).parameters) - assert_equal([[:keyrest, :**]], self.class.instance_method(:pmk1).parameters) + assert_equal([[:keyrest]], self.class.instance_method(:pmk1).parameters) assert_equal([[:keyrest, :o]], self.class.instance_method(:pmk2).parameters) assert_equal([[:req, :a], [:keyrest, :o]], self.class.instance_method(:pmk3).parameters) assert_equal([[:opt, :a], [:keyrest, :o]], self.class.instance_method(:pmk4).parameters) @@ -771,14 +760,6 @@ class TestMethod < Test::Unit::TestCase assert_raise(NoMethodError) { (self).mv2 } assert_nothing_raised { self.mv3 } - class << (obj = Object.new) - private def [](x) x end - def mv1(x) self[x] end - def mv2(x) (self)[x] end - end - assert_nothing_raised { obj.mv1(0) } - assert_raise(NoMethodError) { obj.mv2(0) } - v = Visibility.new assert_equal('method', defined?(v.mv1)) @@ -1215,6 +1196,25 @@ class TestMethod < Test::Unit::TestCase assert_nil(super_method) end + def test_method_visibility_predicates + v = Visibility.new + assert_equal(true, v.method(:mv1).public?) + assert_equal(true, v.method(:mv2).private?) + assert_equal(true, v.method(:mv3).protected?) + assert_equal(false, v.method(:mv2).public?) + assert_equal(false, v.method(:mv3).private?) + assert_equal(false, v.method(:mv1).protected?) + end + + def test_unbound_method_visibility_predicates + assert_equal(true, Visibility.instance_method(:mv1).public?) + assert_equal(true, Visibility.instance_method(:mv2).private?) + assert_equal(true, Visibility.instance_method(:mv3).protected?) + assert_equal(false, Visibility.instance_method(:mv2).public?) + assert_equal(false, Visibility.instance_method(:mv3).private?) + assert_equal(false, Visibility.instance_method(:mv1).protected?) + end + # Bug 18435 def test_instance_methods_owner_consistency a = Module.new { def method1; end } @@ -1244,12 +1244,12 @@ class TestMethod < Test::Unit::TestCase unbound = b.instance_method(:foo) assert_equal unbound, b.public_instance_method(:foo) - assert_equal "#<UnboundMethod: A#foo(arg=...) #{__FILE__}:#{line}>", unbound.inspect + assert_equal "#<UnboundMethod: B(A)#foo(arg=...) #{__FILE__}:#{line}>", unbound.inspect assert_equal [[:opt, :arg]], unbound.parameters a.remove_method(:foo) - assert_equal "#<UnboundMethod: A#foo(arg=...) #{__FILE__}:#{line}>", unbound.inspect + assert_equal "#<UnboundMethod: B(A)#foo(arg=...) #{__FILE__}:#{line}>", unbound.inspect assert_equal [[:opt, :arg]], unbound.parameters obj = b.new @@ -1289,7 +1289,7 @@ class TestMethod < Test::Unit::TestCase a.remove_method(:foo) - assert_equal "#<UnboundMethod: A#foo(arg=...) #{__FILE__}:#{line}>", unbound.inspect + assert_equal "#<UnboundMethod: B(A)#foo(arg=...) #{__FILE__}:#{line}>", unbound.inspect assert_equal [[:opt, :arg]], unbound.parameters assert_equal a0_foo, unbound.super_method @@ -1297,7 +1297,7 @@ class TestMethod < Test::Unit::TestCase assert_equal 1, unbound.bind_call(obj) assert_include b.instance_methods(false), :foo - assert_equal "#<UnboundMethod: A0#foo(arg1=..., arg2=...) #{__FILE__}:#{line0}>", b.instance_method(:foo).inspect + assert_equal "#<UnboundMethod: B(A0)#foo(arg1=..., arg2=...) #{__FILE__}:#{line0}>", b.instance_method(:foo).inspect end def test_zsuper_method_redefined_bind_call @@ -1322,7 +1322,6 @@ class TestMethod < Test::Unit::TestCase m2 = c2.instance_method(:foo) c1.class_exec do - remove_method :foo def foo [:bar2] end @@ -1363,7 +1362,7 @@ class TestMethod < Test::Unit::TestCase def test_splat_long_array if File.exist?('/etc/os-release') && File.read('/etc/os-release').include?('openSUSE Leap') # For RubyCI's openSUSE machine http://rubyci.s3.amazonaws.com/opensuseleap/ruby-trunk/recent.html, which tends to die with NoMemoryError here. - omit 'do not exhaust memory on RubyCI openSUSE Leap machine' + skip 'do not exhaust memory on RubyCI openSUSE Leap machine' end n = 10_000_000 assert_equal n , rest_parameter(*(1..n)).size, '[Feature #10440]' @@ -1431,25 +1430,25 @@ class TestMethod < Test::Unit::TestCase end def test_argument_error_location - body = <<~'END_OF_BODY' - eval <<~'EOS', nil, "main.rb" - $line_lambda = __LINE__; $f = lambda do - _x = 1 - end - $line_method = __LINE__; def foo - _x = 1 - end - begin - $f.call(1) - rescue ArgumentError => e - assert_equal "main.rb:#{$line_lambda}:in `block in <main>'", e.backtrace.first - end - begin - foo(1) - rescue ArgumentError => e - assert_equal "main.rb:#{$line_method}:in `foo'", e.backtrace.first - end - EOS + body = <<-'END_OF_BODY' + eval <<-'EOS' + $line_lambda = __LINE__; $f = lambda do + _x = 1 + end + $line_method = __LINE__; def foo + _x = 1 + end + begin + $f.call(1) + rescue ArgumentError => e + assert_equal "(eval):#{$line_lambda.to_s}:in `block in <main>'", e.backtrace.first + end + begin + foo(1) + rescue ArgumentError => e + assert_equal "(eval):#{$line_method}:in `foo'", e.backtrace.first + end + EOS END_OF_BODY assert_separately [], body @@ -1458,7 +1457,7 @@ class TestMethod < Test::Unit::TestCase end def test_zsuper_private_override_instance_method - assert_separately([], <<-'end;', timeout: 30) + assert_separately(%w(--disable-gems), <<-'end;', timeout: 30) # Bug #16942 [ruby-core:98691] module M def x @@ -1479,7 +1478,7 @@ class TestMethod < Test::Unit::TestCase end def test_override_optimized_method_on_class_using_prepend - assert_separately([], <<-'end;', timeout: 30) + assert_separately(%w(--disable-gems), <<-'end;', timeout: 30) # Bug #17725 [ruby-core:102884] $VERBOSE = nil String.prepend(Module.new) @@ -1565,7 +1564,7 @@ class TestMethod < Test::Unit::TestCase # use_symbol = Object.instance_methods[0].is_a?(Symbol) nummodule = nummethod = 0 mods = [] - ObjectSpace.each_object(Module) {|m| mods << m if String === m.name } + ObjectSpace.each_object(Module) {|m| mods << m if m.name } mods = mods.sort_by {|m| m.name } mods.each {|mod| nummodule += 1 |
