Class: RBS::Test::Spy::SingletonSpy
- Inherits:
-
Object
- Object
- RBS::Test::Spy::SingletonSpy
- Defined in:
- lib/rbs/test/spy.rb
Instance Attribute Summary collapse
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Instance Method Summary collapse
-
#initialize(object:, method_name:) ⇒ SingletonSpy
constructor
A new instance of SingletonSpy.
- #reset ⇒ Object
- #setup ⇒ Object
- #spy ⇒ Object
Constructor Details
#initialize(object:, method_name:) ⇒ SingletonSpy
Returns a new instance of SingletonSpy.
51 52 53 54 55 |
# File 'lib/rbs/test/spy.rb', line 51 def initialize(object:, method_name:) @object = object @method_name = method_name @callback = -> (_) { } end |
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback.
47 48 49 |
# File 'lib/rbs/test/spy.rb', line 47 def callback @callback end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
48 49 50 |
# File 'lib/rbs/test/spy.rb', line 48 def method_name @method_name end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
49 50 51 |
# File 'lib/rbs/test/spy.rb', line 49 def object @object end |
Instance Method Details
#reset ⇒ Object
133 134 135 136 137 |
# File 'lib/rbs/test/spy.rb', line 133 def reset if object.singleton_class.methods.include?(method_name) object.singleton_class.remove_method method_name end end |
#setup ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/rbs/test/spy.rb', line 57 def setup spy = self object.singleton_class.class_eval do remove_method spy.method_name define_method spy.method_name, spy.spy() end end |
#spy ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rbs/test/spy.rb', line 66 def spy() spy = self -> (*args, &block) do 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 = super(*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 raise exception else return_value end end.ruby2_keywords end |