Project

General

Profile

Feature #14223 » refinemetns_to_proc.patch

osyo (manga osyo), 12/29/2017 06:45 AM

View differences:

test/ruby/test_refinement.rb
2010 2010
    assert_equal(:foo, ToSymbol.new("foo").symbol)
2011 2011
  end
2012 2012

  
2013
  module ToProc
2014
    def self.call &block
2015
      block.call
2016
    end
2017

  
2018
    class ReturnProc
2019
      c = self
2020
      using Module.new {
2021
        refine c do
2022
          def to_proc
2023
            proc { "to_proc" }
2024
          end
2025
        end
2026
      }
2027
      def call
2028
        ToProc.call &self
2029
      end
2030
    end
2031

  
2032
    class ReturnNoProc
2033
      c = self
2034
      using Module.new {
2035
        refine c do
2036
          def to_proc
2037
            true
2038
          end
2039
        end
2040
      }
2041

  
2042
      def call
2043
        ToProc.call &self
2044
      end
2045
    end
2046

  
2047
    class PrivateToProc
2048
      c = self
2049
      using Module.new {
2050
        refine c do
2051
          private
2052
          def to_proc
2053
            proc { "private_to_proc" }
2054
          end
2055
        end
2056
      }
2057

  
2058
      def call
2059
        ToProc.call &self
2060
      end
2061
    end
2062

  
2063

  
2064
    class NonProc
2065
      def call
2066
        ToProc.call &self
2067
      end
2068
    end
2069

  
2070
    class MethodMissing
2071
      def method_missing *args
2072
        proc { "method_missing" }
2073
      end
2074

  
2075
      def call
2076
        ToProc.call &self
2077
      end
2078
      end
2079

  
2080
    class ToProcAndMethodMissing
2081
      def method_missing *args
2082
        proc { "method_missing" }
2083
      end
2084

  
2085
      c = self
2086
      using Module.new {
2087
        refine c do
2088
          def to_proc
2089
            proc { "to_proc" }
2090
          end
2091
        end
2092
      }
2093

  
2094
      def call
2095
        ToProc.call &self
2096
      end
2097
    end
2098

  
2099
    class ToProcAndRefinements
2100
      def to_proc
2101
        proc { "to_proc" }
2102
      end
2103

  
2104
      c = self
2105
      using Module.new {
2106
        refine c do
2107
          def to_proc
2108
            proc { "refinements_to_proc" }
2109
          end
2110
        end
2111
      }
2112

  
2113
      def call
2114
        ToProc.call &self
2115
      end
2116
    end
2117
  end
2118

  
2119
  def test_to_proc
2120
    assert_equal("to_proc", ToProc::ReturnProc.new.call)
2121
    assert_equal("private_to_proc", ToProc::PrivateToProc.new.call)
2122
    assert_raise(TypeError){ ToProc::ReturnNoProc.new.call }
2123
    assert_raise(TypeError){ ToProc::NonProc.new.call }
2124
    assert_equal("method_missing", ToProc::MethodMissing.new.call)
2125
    assert_equal("to_proc", ToProc::ToProcAndMethodMissing.new.call)
2126
    assert_equal("refinements_to_proc", ToProc::ToProcAndRefinements.new.call)
2127
  end
2128

  
2013 2129
  def test_unused_refinement_for_module
2014 2130
    bug14068 = '[ruby-core:83613] [Bug #14068]'
2015 2131
    assert_in_out_err([], <<-INPUT, ["M1#foo"], [], bug14068)
vm_args.c
790 790
{
791 791
    if (UNLIKELY(!rb_obj_is_proc(proc))) {
792 792
	VALUE b;
793
	b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc);
793
	const rb_callable_method_entry_t *me =
794
	    rb_callable_method_entry_with_refinements(CLASS_OF(proc), idTo_proc, NULL);
795

  
796
	if (me) {
797
	    b = vm_call0(GET_EC(), proc, idTo_proc, 0, NULL, me);
798
	}
799
	else {
800
	    /* NOTE: calling method_missing */
801
	    b = rb_check_convert_type_with_id(proc, T_DATA, "Proc", idTo_proc);
802
	}
794 803

  
795 804
	if (NIL_P(b) || !rb_obj_is_proc(b)) {
796 805
	    rb_raise(rb_eTypeError,