Class: Caricature::MethodCallRecording

Inherits:
Object
  • Object
show all
Defined in:
lib/caricature/method_call_recorder.rb

Overview

A recording that represents a method call it contains argument variations that can be matched too

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, count = 0) ⇒ MethodCallRecording

Initializes a new instance of a method call recording every time a method gets called in an isolated object this gets stored in the method call recorder It expects a method_name at the very least.



98
99
100
101
102
# File 'lib/caricature/method_call_recorder.rb', line 98

def initialize(method_name, count=0)
  @method_name = method_name
  @count = count
  @variations = []
end

Instance Attribute Details

#argsObject

add args



89
90
91
# File 'lib/caricature/method_call_recorder.rb', line 89

def args
  @args
end

#blockObject

gets or sets the block for this method call



92
93
94
# File 'lib/caricature/method_call_recorder.rb', line 92

def block
  @block
end

#countObject

gets or sets the amount of times the method was called



86
87
88
# File 'lib/caricature/method_call_recorder.rb', line 86

def count
  @count
end

#method_nameObject

gets or sets the method name



83
84
85
# File 'lib/caricature/method_call_recorder.rb', line 83

def method_name
  @method_name
end

Instance Method Details

#add_argument_variation(args, block) ⇒ Object

add an argument variation



115
116
117
118
119
120
121
122
# File 'lib/caricature/method_call_recorder.rb', line 115

def add_argument_variation(args, block)
  variation = find_argument_variations args, nil
  if variation.empty?
    @variations << ArgumentRecording.new(args, @variations.size+1, block) if variation == []
  else
    variation.first.call_number += 1
  end
end

#find_argument_variations(args, block_args) ⇒ Object

finds an argument variation that matches the provided args



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/caricature/method_call_recorder.rb', line 125

def find_argument_variations(args, block_args)
  return @variations if args.first.is_a?(Symbol) and args.last == :any
  return match_hash(args, block_args) if args.size == 1 and args.last.is_a?(Hash)
  return @variations.select { |ar| ar.args == args } if block_args.nil? 
  return @variations.select { |ar| ar.args == args and ar.block } if block_args.last == :any
  return @variations.select do |ar|
    av = ar.args == args
    # idx = 0
    #         ags = ar.args
    #         av = args.all? do |ag| 
    #           rag = ags[idx]
    #           res = if ag.is_a?(Hash) and rag.is_a?(Hash)
    #             ag.all? { |k, v| ar.args[idx][k] == v  }
    #           else
    #             ag == rag
    #           end
    #           idx += 1
    #           res
    #         end
    av and not ar.find_block_variation(*block_args).empty?
  end
end

#has_argument_variations?Boolean

indicates if it has an argument variation

Returns:

  • (Boolean)


110
111
112
# File 'lib/caricature/method_call_recorder.rb', line 110

def has_argument_variations?
  @variations.size > 1
end

#match_hash(args, block_args) ⇒ Object



148
149
150
151
152
153
# File 'lib/caricature/method_call_recorder.rb', line 148

def match_hash(args, block_args)
  @variations.select do |ar| 
    ags = ar.args.last
    args.last.all? { |k, v| ags[k] == v }  
  end
end