Module: Crspec::Mock::ArgumentMatchers

Included in:
DSL
Defined in:
lib/crspec/mock/argument_matchers.rb

Overview

RSpec-style argument matchers usable in with(...) and have_received constraints. Each responds to ===(actual).

Defined Under Namespace

Classes: Anything, ArrayIncluding, Duck, HashExcluding, HashIncluding, InstanceOf, KindOf

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.args_match?(expected_args, actual_args, actual_kwargs = nil) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/crspec/mock/argument_matchers.rb', line 122

def self.args_match?(expected_args, actual_args, actual_kwargs = nil)
  return true if expected_args.nil?

  # A trailing hash matcher (hash_including etc.) may target the
  # call's keyword arguments.
  if actual_kwargs && !actual_kwargs.empty? &&
     expected_args.size == actual_args.size + 1 &&
     (expected_args.last.is_a?(HashIncluding) || expected_args.last.is_a?(HashExcluding) || expected_args.last.is_a?(Hash))
    positional = expected_args[0...-1]
    return args_match?(positional, actual_args) &&
           (expected_args.last == actual_kwargs || expected_args.last === actual_kwargs)
  end

  return false unless expected_args.size == actual_args.size

  expected_args.zip(actual_args).all? do |expected, actual|
    expected == actual || expected === actual
  end
end

.kwargs_match?(expected_kwargs, actual_kwargs) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/crspec/mock/argument_matchers.rb', line 142

def self.kwargs_match?(expected_kwargs, actual_kwargs)
  return true if expected_kwargs.nil? || expected_kwargs.empty?

  if expected_kwargs.size == 1 && expected_kwargs.values.first.is_a?(HashIncluding)
    return expected_kwargs.values.first === actual_kwargs
  end

  return false unless expected_kwargs.size == actual_kwargs.size

  expected_kwargs.all? do |key, expected|
    actual_kwargs.key?(key) && (expected == actual_kwargs[key] || expected === actual_kwargs[key])
  end
end

Instance Method Details

#an_instance_of(klass) ⇒ Object Also known as: instance_of



108
109
110
# File 'lib/crspec/mock/argument_matchers.rb', line 108

def an_instance_of(klass)
  InstanceOf.new(klass)
end

#anythingObject



92
93
94
# File 'lib/crspec/mock/argument_matchers.rb', line 92

def anything
  Anything.new
end

#array_including(*items) ⇒ Object



104
105
106
# File 'lib/crspec/mock/argument_matchers.rb', line 104

def array_including(*items)
  ArrayIncluding.new(items.flatten(1))
end

#duck_type(*methods) ⇒ Object



118
119
120
# File 'lib/crspec/mock/argument_matchers.rb', line 118

def duck_type(*methods)
  Duck.new(*methods)
end

#hash_excluding(expected = {}, **kwargs) ⇒ Object



100
101
102
# File 'lib/crspec/mock/argument_matchers.rb', line 100

def hash_excluding(expected = {}, **kwargs)
  HashExcluding.new(expected.merge(kwargs))
end

#hash_including(expected = {}, **kwargs) ⇒ Object



96
97
98
# File 'lib/crspec/mock/argument_matchers.rb', line 96

def hash_including(expected = {}, **kwargs)
  HashIncluding.new(expected.merge(kwargs))
end

#kind_of(klass) ⇒ Object Also known as: a_kind_of



113
114
115
# File 'lib/crspec/mock/argument_matchers.rb', line 113

def kind_of(klass)
  KindOf.new(klass)
end