Class: RBS::Test::Spy::WrapSpy

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/test/spy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object:, method_name:) ⇒ WrapSpy

Returns a new instance of WrapSpy.



244
245
246
247
248
# File 'lib/rbs/test/spy.rb', line 244

def initialize(object:, method_name:)
  @callback = -> (_) { }
  @object = object
  @method_name = method_name
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



240
241
242
# File 'lib/rbs/test/spy.rb', line 240

def callback
  @callback
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



242
243
244
# File 'lib/rbs/test/spy.rb', line 242

def method_name
  @method_name
end

#objectObject (readonly)

Returns the value of attribute object.



241
242
243
# File 'lib/rbs/test/spy.rb', line 241

def object
  @object
end

Instance Method Details

#wrapped_objectObject



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/rbs/test/spy.rb', line 250

def wrapped_object
  spy = self

  Class.new(BasicObject) do
    define_method(:method_missing) do |name, *args, &block|
      spy.object.__send__(name, *args, &block)
    end

    define_method(spy.method_name, -> (*args, &block) {
      return_value = nil
      exception = nil
      block_calls = []

      spy_block = if block
                    Object.new.instance_eval do |fresh|
                      -> (*block_args) do
                        block_exn = nil
                        block_return = nil

                        begin
                          block_return = if self.equal?(fresh)
                                           # no instance eval
                                           block.call(*block_args)
                                         else
                                           self.instance_exec(*block_args, &block)
                                         end
                        rescue Exception => exn
                          block_exn = exn
                        end

                        block_calls << ArgumentsReturn.new(
                          arguments: block_args,
                          return_value: block_return,
                          exception: block_exn
                        )

                        if block_exn
                          raise block_exn
                        else
                          block_return
                        end
                      end.ruby2_keywords
                    end
                  end

      begin
        return_value = spy.object.__send__(spy.method_name, *args, &spy_block)
      rescue ::Exception => exn
        exception = exn
      end

      trace = CallTrace.new(
        method_name: spy.method_name,
        method_call: ArgumentsReturn.new(
          arguments: args,
          return_value: return_value,
          exception: exception,
          ),
        block_calls: block_calls,
        block_given: block != nil
      )

      spy.callback.call(trace)

      if exception
        spy.object.__send__(:raise, exception)
      else
        return_value
      end
    }.ruby2_keywords)
  end.new()
end