Class: RBS::Test::Spy::InstanceSpy
- Inherits:
-
Object
- Object
- RBS::Test::Spy::InstanceSpy
- 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.
-
#mod ⇒ Object
readonly
Returns the value of attribute mod.
-
#original_method ⇒ Object
readonly
Returns the value of attribute original_method.
Instance Method Summary collapse
-
#initialize(mod:, method_name:) ⇒ InstanceSpy
constructor
A new instance of InstanceSpy.
- #reset ⇒ Object
- #setup ⇒ Object
- #spy ⇒ Object
Constructor Details
#initialize(mod:, method_name:) ⇒ InstanceSpy
Returns a new instance of InstanceSpy.
146 147 148 149 150 151 |
# File 'lib/rbs/test/spy.rb', line 146 def initialize(mod:, method_name:) @mod = mod @method_name = method_name @original_method = mod.instance_method(method_name) @callback = -> (_) { } end |
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback.
141 142 143 |
# File 'lib/rbs/test/spy.rb', line 141 def callback @callback end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
143 144 145 |
# File 'lib/rbs/test/spy.rb', line 143 def method_name @method_name end |
#mod ⇒ Object (readonly)
Returns the value of attribute mod.
142 143 144 |
# File 'lib/rbs/test/spy.rb', line 142 def mod @mod end |
#original_method ⇒ Object (readonly)
Returns the value of attribute original_method.
144 145 146 |
# File 'lib/rbs/test/spy.rb', line 144 def original_method @original_method end |
Instance Method Details
#reset ⇒ Object
162 163 164 165 166 167 168 169 |
# File 'lib/rbs/test/spy.rb', line 162 def reset spy = self mod.class_eval do remove_method spy.method_name define_method spy.method_name, spy.original_method end end |
#setup ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/rbs/test/spy.rb', line 153 def setup spy = self mod.class_eval do remove_method spy.method_name define_method spy.method_name, spy.spy() end end |
#spy ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/rbs/test/spy.rb', line 171 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 = spy.original_method.bind_call(self, *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 |