[前][次][番号順一覧][スレッド一覧]

ruby-changes:64257

From: Nobuyoshi <ko1@a...>
Date: Fri, 18 Dec 2020 13:03:54 +0900 (JST)
Subject: [ruby-changes:64257] e042e8460b (master): Added missing tests for public, private, protected and alias_method

https://git.ruby-lang.org/ruby.git/commit/?id=e042e8460b

From e042e8460bb9a63c05f938d51e8c7c5345a6f3a4 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Fri, 18 Dec 2020 12:59:01 +0900
Subject: Added missing tests for public, private, protected and alias_method


diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb
index 55755a6..037715b 100644
--- a/test/ruby/test_module.rb
+++ b/test/ruby/test_module.rb
@@ -828,6 +828,67 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L828
     assert_equal([:bClass1], BClass.public_instance_methods(false))
   end
 
+  def test_s_public
+    o = (c = Class.new(AClass)).new
+    assert_raise(NoMethodError, /private method/) {o.aClass1}
+    assert_raise(NoMethodError, /protected method/) {o.aClass2}
+    c.class_eval {public :aClass1}
+    assert_equal(:aClass1, o.aClass1)
+
+    o = (c = Class.new(AClass)).new
+    c.class_eval {public :aClass1, :aClass2}
+    assert_equal(:aClass1, o.aClass1)
+    assert_equal(:aClass2, o.aClass2)
+
+    o = AClass.new
+    assert_equal(:aClass, o.aClass)
+    assert_raise(NoMethodError, /private method/) {o.aClass1}
+    assert_raise(NoMethodError, /protected method/) {o.aClass2}
+  end
+
+  def test_s_private
+    o = (c = Class.new(AClass)).new
+    assert_equal(:aClass, o.aClass)
+    c.class_eval {private :aClass}
+    assert_raise(NoMethodError, /private method/) {o.aClass}
+
+    o = (c = Class.new(AClass)).new
+    c.class_eval {private :aClass, :aClass2}
+    assert_raise(NoMethodError, /private method/) {o.aClass}
+    assert_raise(NoMethodError, /private method/) {o.aClass2}
+
+    o = AClass.new
+    assert_equal(:aClass, o.aClass)
+    assert_raise(NoMethodError, /private method/) {o.aClass1}
+    assert_raise(NoMethodError, /protected method/) {o.aClass2}
+  end
+
+  def test_s_protected
+    aclass = Class.new(AClass) do
+      def _aClass(o) o.aClass; end
+      def _aClass1(o) o.aClass1; end
+      def _aClass2(o) o.aClass2; end
+    end
+
+    o = (c = Class.new(aclass)).new
+    assert_equal(:aClass, o.aClass)
+    c.class_eval {protected :aClass}
+    assert_raise(NoMethodError, /protected method/) {o.aClass}
+    assert_equal(:aClass, c.new._aClass(o))
+
+    o = (c = Class.new(aclass)).new
+    c.class_eval {protected :aClass, :aClass1}
+    assert_raise(NoMethodError, /protected method/) {o.aClass}
+    assert_raise(NoMethodError, /protected method/) {o.aClass1}
+    assert_equal(:aClass, c.new._aClass(o))
+    assert_equal(:aClass1, c.new._aClass1(o))
+
+    o = AClass.new
+    assert_equal(:aClass, o.aClass)
+    assert_raise(NoMethodError, /private method/) {o.aClass1}
+    assert_raise(NoMethodError, /protected method/) {o.aClass2}
+  end
+
   def test_s_constants
     c1 = Module.constants
     Object.module_eval "WALTER = 99"
@@ -1156,6 +1217,18 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L1217
     end
   end
 
+  def test_alias_method
+    c = Class.new do
+      def foo; :foo end
+    end
+    o = c.new
+    assert_respond_to(o, :foo)
+    assert_not_respond_to(o, :bar)
+    c.class_eval {alias_method :bar, :foo}
+    assert_respond_to(o, :bar)
+    assert_equal(:foo, o.bar)
+  end
+
   def test_undef
     c = Class.new
     assert_raise(NameError) do
diff --git a/vm_method.c b/vm_method.c
index e2c5bfb..f0f6580 100644
--- a/vm_method.c
+++ b/vm_method.c
@@ -2004,7 +2004,8 @@ set_method_visibility(VALUE self, int argc, const VALUE *argv, rb_method_visibil https://github.com/ruby/ruby/blob/trunk/vm_method.c#L2004
 	for (j = 0; j < RARRAY_LEN(v); j++) {
 	    check_and_export_method(self, RARRAY_AREF(v, j), visi);
 	}
-    } else {
+    }
+    else {
         for (i = 0; i < argc; i++) {
             check_and_export_method(self, argv[i], visi);
         }
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]