Class: RR::Expectations::ArgumentEqualityExpectation

Inherits:
Object
  • Object
show all
Defined in:
lib/rr/expectations/argument_equality_expectation.rb

Overview

:nodoc:

Direct Known Subclasses

AnyArgumentExpectation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected_arguments, expected_keyword_arguments) ⇒ ArgumentEqualityExpectation

Returns a new instance of ArgumentEqualityExpectation.



15
16
17
18
19
# File 'lib/rr/expectations/argument_equality_expectation.rb', line 15

def initialize(expected_arguments,
               expected_keyword_arguments)
  @expected_arguments = expected_arguments
  @expected_keyword_arguments = expected_keyword_arguments
end

Instance Attribute Details

#expected_argumentsObject (readonly)

Returns the value of attribute expected_arguments.



12
13
14
# File 'lib/rr/expectations/argument_equality_expectation.rb', line 12

def expected_arguments
  @expected_arguments
end

#expected_keyword_argumentsObject (readonly)

Returns the value of attribute expected_keyword_arguments.



13
14
15
# File 'lib/rr/expectations/argument_equality_expectation.rb', line 13

def expected_keyword_arguments
  @expected_keyword_arguments
end

Class Method Details

.recursive_safe_eq(arg1, arg2) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/rr/expectations/argument_equality_expectation.rb', line 4

def self.recursive_safe_eq(arg1, arg2)
  if arg1.respond_to?(:'__rr__original_==')
    arg1.__send__(:'__rr__original_==', arg2)
  else
    arg1 == arg2
  end
end

Instance Method Details

#==(other) ⇒ Object



75
76
77
78
79
# File 'lib/rr/expectations/argument_equality_expectation.rb', line 75

def ==(other)
  other.is_a?(self.class) and
    expected_arguments == other.expected_arguments and
    expected_keyword_arguments == other.expected_keyword_arguments
end

#exact_match?(arguments, keyword_arguments) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rr/expectations/argument_equality_expectation.rb', line 21

def exact_match?(arguments, keyword_arguments)
  return false unless arguments.length == expected_arguments.length
  arguments.each_with_index do |arg, index|
    expected_arg = expected_arguments[index]
    return false unless self.class.recursive_safe_eq(expected_arg, arg)
  end
  keywords = keyword_arguments.keys
  expected_keywords = expected_keyword_arguments.keys
  unless keywords.length == expected_keywords.length
    return false
  end
  keywords.each do |keyword|
    keyword_argument = keyword_arguments[keyword]
    expected_keyword_argument = expected_keyword_arguments[keyword]
    unless self.class.recursive_safe_eq(expected_keyword_argument,
                                        keyword_argument)
      return false
    end
  end
  true
end

#wildcard_match?(arguments, keyword_arguments) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rr/expectations/argument_equality_expectation.rb', line 43

def wildcard_match?(arguments, keyword_arguments)
  return false unless arguments.length == expected_arguments.length
  arguments.each_with_index do |arg, index|
    expected_argument = expected_arguments[index]
    if expected_argument.respond_to?(:wildcard_match?)
      return false unless expected_argument.wildcard_match?(arg)
    else
      return false unless self.class.recursive_safe_eq(expected_argument, arg)
    end
  end
  keywords = keyword_arguments.keys
  expected_keywords = expected_keyword_arguments.keys
  unless keywords.length == expected_keywords.length
    return false
  end
  keywords.each do |keyword|
    keyword_argument = keyword_arguments[keyword]
    expected_keyword_argument = expected_keyword_arguments[keyword]
    if expected_keyword_argument.respond_to?(:wildcard_match?)
      unless expected_keyword_argument.wildcard_match?(keyword_argument)
        return false
      end
    else
      unless self.class.recursive_safe_eq(expected_keyword_argument,
                                          keyword_argument)
        return false
      end
    end
  end
  true
end