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.



91
92
93
94
95
96
97
# File 'lib/spec/mocks/argument_expectation.rb', line 91

def initialize(args)
  @args = args
  if [:any_args] == args then @expected_params = nil
  elsif [:no_args] == args then @expected_params = []
  else @expected_params = process_arg_constraints(args)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



84
85
86
# File 'lib/spec/mocks/argument_expectation.rb', line 84

def args
  @args
end

Instance Method Details

#check_args(args) ⇒ Object



117
118
119
120
121
# File 'lib/spec/mocks/argument_expectation.rb', line 117

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)


123
124
125
126
127
# File 'lib/spec/mocks/argument_expectation.rb', line 123

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



105
106
107
108
109
110
111
# File 'lib/spec/mocks/argument_expectation.rb', line 105

def convert_constraint(constraint)
  return @@constraint_classes[constraint].new(constraint) if constraint.is_a?(Symbol)
  return constraint if constraint.is_a?(DuckTypeArgConstraint)
  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)


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

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

#process_arg_constraints(constraints) ⇒ Object



99
100
101
102
103
# File 'lib/spec/mocks/argument_expectation.rb', line 99

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