Class: Spec::Api::ArgumentExpectation

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ArgumentExpectation

Returns a new instance of ArgumentExpectation.



70
71
72
73
74
75
# File 'lib/spec/api/mocks/argument_expectation.rb', line 70

def initialize(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 Method Details

#check_args(args) ⇒ Object



89
90
91
92
93
# File 'lib/spec/api/mocks/argument_expectation.rb', line 89

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)


95
96
97
98
99
# File 'lib/spec/api/mocks/argument_expectation.rb', line 95

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



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

def convert_constraint(constraint)
  return @@constraint_classes[constraint].new(constraint) if constraint.is_a?(Symbol)
  return constraint if constraint.is_a?(DuckTypeArgConstraint)
  return LiteralArgConstraint.new(constraint)
end

#process_arg_constraints(constraints) ⇒ Object



77
78
79
80
81
# File 'lib/spec/api/mocks/argument_expectation.rb', line 77

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