Class: Caricature::MethodCallRecorder
- Defined in:
- lib/caricature/method_call_recorder.rb,
lib/caricature/clr/method_call_recorder.rb
Overview
The recorder that will collect method calls and provides an interface for finding those recordings
Instance Attribute Summary collapse
-
#method_calls ⇒ Object
readonly
gets the collection of method calls.
Instance Method Summary collapse
- #error ⇒ Object
- #event_error ⇒ Object
-
#event_raised?(event_name, mode = :instance, *args) ⇒ Boolean
returns whether the event was actually raised with the specified constraints.
- #event_raises ⇒ Object
-
#initialize ⇒ MethodCallRecorder
constructor
Initializes a new instance of a method call recorder every time a method gets called in an isolated object this gets stored in the method call recorder.
-
#record_call(method_name, mode = :instance, expectation = nil, *args, &block) ⇒ Object
records a method call or increments the count of how many times this method was called.
- #record_event_raise(event_name, mode, *args, &handler) ⇒ Object
-
#size ⇒ Object
returns the number of different methods that has been recorderd.
-
#was_called?(method_name, block_args, mode = :instance, *args) ⇒ Boolean
returns whether the method was actually called with the specified constraints.
Constructor Details
#initialize ⇒ MethodCallRecorder
Initializes a new instance of a method call recorder every time a method gets called in an isolated object this gets stored in the method call recorder
166 167 168 |
# File 'lib/caricature/method_call_recorder.rb', line 166 def initialize @method_calls = {:instance => {}, :class => {} } end |
Instance Attribute Details
#method_calls ⇒ Object (readonly)
gets the collection of method calls. This is a hash with the method name as key
160 161 162 |
# File 'lib/caricature/method_call_recorder.rb', line 160 def method_calls @method_calls end |
Instance Method Details
#error ⇒ Object
193 194 195 |
# File 'lib/caricature/method_call_recorder.rb', line 193 def error @error end |
#event_error ⇒ Object
71 72 73 |
# File 'lib/caricature/clr/method_call_recorder.rb', line 71 def event_error @event_error end |
#event_raised?(event_name, mode = :instance, *args) ⇒ Boolean
returns whether the event was actually raised with the specified constraints
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/caricature/clr/method_call_recorder.rb', line 76 def event_raised?(event_name, mode = :instance, *args) mc = event_raises[mode][event_name.to_s.to_sym] if mc vari = mc.find_argument_variations(args) result = vari.any? { |agv| agv == args } return result if result if args.size == 1 and args.last.is_a?(Hash) result = vari.any? do |agv| agv.args.last.is_a?(Hash) and args.last.all? { |k, v| agv.args.last[k] == v } end end @event_error = "Event Arguments don't match for #{event_name}.\n\nYou expected:\n#{args.join(", ")}.\n\nI did find the following variations:\n#{mc.args.collect {|ar| ar.args.join(', ') }.join(' and ')}" unless result result else @event_error = "Couldn't find an event with name #{event_name}" return !!mc end end |
#event_raises ⇒ Object
59 60 61 |
# File 'lib/caricature/clr/method_call_recorder.rb', line 59 def event_raises @event_raises ||= {} end |
#record_call(method_name, mode = :instance, expectation = nil, *args, &block) ⇒ Object
records a method call or increments the count of how many times this method was called.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/caricature/method_call_recorder.rb', line 171 def record_call(method_name, mode=:instance, expectation=nil, *args, &block) mn_sym = method_name.to_s.underscore.to_sym method_calls[mode][mn_sym] ||= MethodCallRecording.new mn_sym.to_s mc = method_calls[mode][mn_sym] mc.count += 1 agc = mc.add_argument_variation args, block b = (expectation && expectation.block) ? (expectation.block||block) : block expectation.block = lambda do |*ags| res = nil res = b.call *ags if b agc.first.add_block_variation *ags res end if expectation block.nil? ? nil : (lambda { |*ags| res = nil res = block.call *ags if block agc.first.add_block_variation *ags res }) end |
#record_event_raise(event_name, mode, *args, &handler) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/caricature/clr/method_call_recorder.rb', line 63 def record_event_raise(event_name, mode, *args, &handler) en_sym = event_name.to_sym event_raises[mode] ||= {} ev = (event_raises[mode][en_sym] ||= EventRaiseRecording.new(event_name)) ev.count += 1 ev.add_argument_variation args, handler end |
#size ⇒ Object
returns the number of different methods that has been recorderd
223 224 225 |
# File 'lib/caricature/method_call_recorder.rb', line 223 def size @method_calls.size end |
#was_called?(method_name, block_args, mode = :instance, *args) ⇒ Boolean
returns whether the method was actually called with the specified constraints
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/caricature/method_call_recorder.rb', line 198 def was_called?(method_name, block_args, mode=:instance, *args) mc = method_calls[mode][method_name.to_s.underscore.to_sym] if mc vari = mc.find_argument_variations(args, block_args) result = vari.any? { |agv| agv == args } return result if result if args.size == 1 and args.last.is_a?(Hash) result = vari.any? do |agv| agv.args.last.is_a?(Hash) and args.last.all? { |k, v| agv.args.last[k] == v } end end @error = "Arguments don't match.\nYou expected:\n#{args.join(", ")}.\nI did find the following variations:\n#{mc.args.collect {|ar| ar.args.join(', ') }.join("\nand\n")}" unless result result else @error = "Couldn't find a method with name #{method_name}" return !!mc end end |