Class: Spec::Mocks::ArgumentExpectation

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/mocks/argument_expectation.rb

Constant Summary collapse

@@constraint_classes =
Hash.new { |hash, key| LiteralArgConstraint}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ArgumentExpectation

Returns a new instance of ArgumentExpectation.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/spec/mocks/argument_expectation.rb', line 120

def initialize(args)
  @args = args
  if [:any_args] == args
    @expected_params = nil
    warn_deprecated(:any_args.inspect, "any_args()")
  elsif args.length == 1 && args[0].is_a?(AnyArgsConstraint) then @expected_params = nil
  elsif [:no_args] == args
    @expected_params = []
    warn_deprecated(:no_args.inspect, "no_args()")
  elsif args.length == 1 && args[0].is_a?(NoArgsConstraint) then @expected_params = []
  else @expected_params = process_arg_constraints(args)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



113
114
115
# File 'lib/spec/mocks/argument_expectation.rb', line 113

def args
  @args
end

Instance Method Details

#check_args(args) ⇒ Object



168
169
170
171
172
# File 'lib/spec/mocks/argument_expectation.rb', line 168

def check_args(args)
  return true if @expected_params.nil?
  return true if @expected_params == args
  return constraints_match?(args)
end

#constraints_match?(args) ⇒ Boolean

Returns:

  • (Boolean)


174
175
176
177
178
# File 'lib/spec/mocks/argument_expectation.rb', line 174

def constraints_match?(args)
  return false if args.length != @expected_params.length
  @expected_params.each_index { |i| return false unless @expected_params[i].matches?(args[i]) }
  return true
end

#convert_constraint(constraint) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/spec/mocks/argument_expectation.rb', line 144

def convert_constraint(constraint)
  if [:anything, :numeric, :boolean, :string].include?(constraint)
    case constraint
    when :anything
      instead = "anything()"
    when :boolean
      instead = "boolean()"
    when :numeric
      instead = "an_instance_of(Numeric)"
    when :string
      instead = "an_instance_of(String)"
    end
    warn_deprecated(constraint.inspect, instead)
    return @@constraint_classes[constraint].new(constraint)
  end
  return MatcherConstraint.new(constraint) if is_matcher?(constraint)
  return RegexpArgConstraint.new(constraint) if constraint.is_a?(Regexp)
  return LiteralArgConstraint.new(constraint)
end

#is_matcher?(obj) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/spec/mocks/argument_expectation.rb', line 164

def is_matcher?(obj)
  return obj.respond_to?(:matches?) && obj.respond_to?(:description)
end

#process_arg_constraints(constraints) ⇒ Object



134
135
136
137
138
# File 'lib/spec/mocks/argument_expectation.rb', line 134

def process_arg_constraints(constraints)
  constraints.collect do |constraint| 
    convert_constraint(constraint)
  end
end

#warn_deprecated(deprecated_method, instead) ⇒ Object



140
141
142
# File 'lib/spec/mocks/argument_expectation.rb', line 140

def warn_deprecated(deprecated_method, instead)
  STDERR.puts "The #{deprecated_method} constraint is deprecated. Use #{instead} instead."
end