11require "ruby/signature"
2+ require "pp"
23
34module Ruby
45 module Signature
56 module Test
67 class Hook
7- IS_AP = Object . instance_method ( :is_a? )
8+ IS_AP = Kernel . instance_method ( :is_a? )
89 DEFINE_METHOD = Module . instance_method ( :define_method )
910 INSTANCE_EVAL = BasicObject . instance_method ( :instance_eval )
1011 INSTANCE_EXEC = BasicObject . instance_method ( :instance_exec )
1112 METHOD = Kernel . instance_method ( :method )
13+ CLASS = Kernel . instance_method ( :class )
14+ SINGLETON_CLASS = Kernel . instance_method ( :singleton_class )
15+ PP = Kernel . instance_method ( :pp )
16+ INSPECT = Kernel . instance_method ( :inspect )
1217
1318 module Errors
1419 ArgumentTypeError =
@@ -37,29 +42,33 @@ def self.format_param(param)
3742 end
3843 end
3944
45+ def self . inspect_ ( obj )
46+ Hook . inspect_ ( obj )
47+ end
48+
4049 def self . to_string ( error )
4150 method = "#{ error . klass . name } #{ error . method_name } "
4251 case error
4352 when ArgumentTypeError
44- "[#{ method } ] ArgumentTypeError: expected #{ format_param error . param } but given `#{ error . value . inspect } `"
53+ "[#{ method } ] ArgumentTypeError: expected #{ format_param error . param } but given `#{ inspect_ ( error . value ) } `"
4554 when BlockArgumentTypeError
46- "[#{ method } ] BlockArgumentTypeError: expected #{ format_param error . param } but given `#{ error . value . inspect } `"
55+ "[#{ method } ] BlockArgumentTypeError: expected #{ format_param error . param } but given `#{ inspect_ ( error . value ) } `"
4756 when ArgumentError
4857 "[#{ method } ] ArgumentError: expected method type #{ error . method_type } "
4958 when BlockArgumentError
5059 "[#{ method } ] BlockArgumentError: expected method type #{ error . method_type } "
5160 when ReturnTypeError
52- "[#{ method } ] ReturnTypeError: expected `#{ error . type } ` but returns `#{ error . value . inspect } `"
61+ "[#{ method } ] ReturnTypeError: expected `#{ error . type } ` but returns `#{ inspect_ ( error . value ) } `"
5362 when BlockReturnTypeError
54- "[#{ method } ] BlockReturnTypeError: expected `#{ error . type } ` but returns `#{ error . value . inspect } `"
63+ "[#{ method } ] BlockReturnTypeError: expected `#{ error . type } ` but returns `#{ inspect_ ( error . value ) } `"
5564 when UnexpectedBlockError
5665 "[#{ method } ] UnexpectedBlockError: unexpected block is given for `#{ error . method_type } `"
5766 when MissingBlockError
5867 "[#{ method } ] MissingBlockError: required block is missing for `#{ error . method_type } `"
5968 when UnresolvedOverloadingError
6069 "[#{ method } ] UnresolvedOverloadingError: couldn't find a suitable overloading"
6170 else
62- raise "Unexpected error: #{ error . inspect } "
71+ raise "Unexpected error: #{ inspect_ ( error ) } "
6372 end
6473 end
6574 end
@@ -161,7 +170,7 @@ def delegation(name, method_types, method_name)
161170 hook = self
162171
163172 proc do |*args , &block |
164- hook . logger . debug { "#{ method_name } receives arguments: #{ args . inspect } " }
173+ hook . logger . debug { "#{ method_name } receives arguments: #{ hook . inspect_ ( args ) } " }
165174
166175 block_call = nil
167176
@@ -170,7 +179,7 @@ def delegation(name, method_types, method_name)
170179
171180 block = hook . call ( Object . new , INSTANCE_EVAL ) do |fresh_obj |
172181 proc do |*as |
173- hook . logger . debug { "#{ method_name } receives block arguments: #{ as . inspect } " }
182+ hook . logger . debug { "#{ method_name } receives block arguments: #{ hook . inspect_ ( as ) } " }
174183
175184 ret = if self . equal? ( fresh_obj )
176185 original_block [ *as ]
@@ -180,23 +189,23 @@ def delegation(name, method_types, method_name)
180189
181190 block_call = ArgsReturn . new ( arguments : as , return_value : ret )
182191
183- hook . logger . debug { "#{ method_name } returns from block: #{ ret . inspect } " }
192+ hook . logger . debug { "#{ method_name } returns from block: #{ hook . inspect_ ( ret ) } " }
184193
185194 ret
186195 end
187196 end
188197 end
189198
190- method = self . method ( name )
191- prepended = self . class . ancestors . include? ( hook . instance_module ) || self . singleton_class . ancestors . include? ( hook . singleton_module )
199+ method = hook . call ( self , METHOD , name )
200+ prepended = hook . call ( self , CLASS ) . ancestors . include? ( hook . instance_module ) || hook . call ( self , SINGLETON_CLASS ) . ancestors . include? ( hook . singleton_module )
192201 result = if prepended
193202 method . super_method . call ( *args , &block )
194203 else
195204 # Using refinement
196205 method . call ( *args , &block )
197206 end
198207
199- hook . logger . debug { "#{ method_name } returns: #{ result . inspect } " }
208+ hook . logger . debug { "#{ method_name } returns: #{ hook . inspect_ ( result ) } " }
200209
201210 call = Call . new ( method_call : ArgsReturn . new ( arguments : args , return_value : result ) ,
202211 block_call : block_call ,
@@ -310,6 +319,14 @@ def call(receiver, method, *args, &block)
310319 method . bind ( receiver ) . call ( *args , &block )
311320 end
312321
322+ def inspect_ ( obj )
323+ Hook . inspect_ ( obj )
324+ end
325+
326+ def self . inspect_ ( obj )
327+ INSPECT . bind ( obj ) . call ( )
328+ end
329+
313330 def disable
314331 self . instance_module . remove_method ( *instance_methods )
315332 self . singleton_module . remove_method ( *singleton_methods )
0 commit comments