Class: Stannum::RSpec::ValidateParameterMatcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Mocks::ExampleMethods
Defined in:
lib/stannum/rspec/validate_parameter_matcher.rb

Overview

Asserts that the command validates the given method parameter.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name:, parameter_name:) ⇒ ValidateParameterMatcher

Returns a new instance of ValidateParameterMatcher.

Parameters:

  • method_name (String, Symbol)

    The name of the method with validated parameters.

  • parameter_name (String, Symbol)

    The name of the validated method parameter.



79
80
81
82
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 79

def initialize(method_name:, parameter_name:)
  @method_name    = method_name.intern
  @parameter_name = parameter_name.intern
end

Instance Attribute Details

#expected_constraintStannum::Constraints::Base? (readonly)

Returns the constraint used to generate the expected error(s).

Returns:



86
87
88
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 86

def expected_constraint
  @expected_constraint
end

#method_nameString, Symbol (readonly)

Returns the name of the method with validated parameters.

Returns:

  • (String, Symbol)

    the name of the method with validated parameters.



89
90
91
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 89

def method_name
  @method_name
end

#parameter_nameString, Symbol (readonly)

Returns the name of the validated method parameter.

Returns:

  • (String, Symbol)

    the name of the validated method parameter.



95
96
97
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 95

def parameter_name
  @parameter_name
end

#parameter_valueObject (readonly)

Returns the invalid value for the validated parameter.

Returns:

  • (Object)

    the invalid value for the validated parameter.



98
99
100
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 98

def parameter_value
  @parameter_value
end

#parametersHash (readonly)

Returns the configured parameters to match.

Returns:

  • (Hash)

    the configured parameters to match.



92
93
94
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 92

def parameters
  @parameters
end

Class Method Details

.add_parameter_mapping(map:, match:) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 23

def add_parameter_mapping(map:, match:)
  raise ArgumentError, 'map must be a Proc'   unless map.is_a?(Proc)
  raise ArgumentError, 'match must be a Proc' unless match.is_a?(Proc)

  parameter_mappings << { match: match, map: map }
end

.map_parameters(actual:, method_name:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 31

def map_parameters(actual:, method_name:)
  parameter_mappings.each do |keywords|
    match = keywords.fetch(:match)
    map   = keywords.fetch(:map)

    next unless match.call(actual: actual, method_name: method_name)

    return map.call(actual: actual, method_name: method_name)
  end

  unwrapped_method(actual: actual, method_name: method_name).parameters
end

Instance Method Details

#descriptionString

Returns a short description of the matcher and expected properties.

Returns:

  • (String)

    a short description of the matcher and expected properties.



102
103
104
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 102

def description
  "validate the #{parameter_name.inspect} #{parameter_type || 'parameter'}"
end

#does_not_match?(actual) ⇒ true, false

Asserts that the object does not validate the specified method parameter.

Parameters:

  • actual (Object)

    The object to match.

Returns:

  • (true, false)

    false if the object validates the parameter, otherwise true.



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 112

def does_not_match?(actual)
  disallow_fluent_options!

  @actual         = actual
  @failure_reason = nil

  return true  unless supports_parameter_validation?
  return false unless responds_to_method?
  return true  unless validates_method?
  return false unless method_has_parameter?

  !validates_method_parameter?
end

#failure_messageString

Returns a summary message describing a failed expectation.

Returns:

  • (String)

    a summary message describing a failed expectation.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 127

def failure_message # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
  message = "expected ##{method_name} to #{description}"
  reason  =
    case @failure_reason
    when :does_not_respond_to_method
      "the object does not respond to ##{method_name}"
    when :does_not_support_parameter_validation
      'the object does not implement parameter validation'
    when :does_not_validate_method
      "the object does not validate the parameters of ##{method_name}"
    when :errors_do_not_match
      "the errors do not match:\n\n#{equality_matcher_failure_message}"
    when :method_does_not_have_parameter
      "##{method_name} does not have a #{parameter_name.inspect} parameter"
    when :parameter_not_validated
      "##{method_name} does not expect a #{parameter_name.inspect} " \
      "#{parameter_type}"
    when :valid_parameter_value
      "#{valid_value.inspect} is a valid value for the " \
      "#{parameter_name.inspect} #{parameter_type}"
    end

  [message, reason].compact.join(', but ')
end

#failure_message_when_negatedString

Returns a summary message describing a failed negated expectation.

Returns:

  • (String)

    a summary message describing a failed negated expectation.



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 154

def failure_message_when_negated
  message = "expected ##{method_name} not to #{description}"
  reason  =
    case @failure_reason
    when :does_not_respond_to_method
      "the object does not respond to ##{method_name}"
    when :method_does_not_have_parameter
      "##{method_name} does not have a #{parameter_name.inspect} parameter"
    end

  [message, reason].compact.join(', but ')
end

#matches?(actual) ⇒ true, false

Asserts that the object validates the specified method parameter.

Parameters:

  • actual (Object)

    The object to match.

Returns:

  • (true, false)

    true if the object validates the parameter, otherwise false.



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 173

def matches?(actual) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  @actual         = actual
  @failure_reason = nil

  return false unless supports_parameter_validation?
  return false unless responds_to_method?
  return false unless validates_method?
  return false unless method_has_parameter?

  if @expected_constraint.nil? && @parameters.nil? && @parameter_value.nil?
    return validates_method_parameter?
  end

  call_validated_method

  return false if extra_parameter?
  return false if valid_parameter?

  matches_expected_error?
end

#using_constraint(constraint, **options) ⇒ Stannum::RSpec::ValidateParameterMatcher

Specifies a constraint or type used to validate the parameter.

Parameters:

Returns:



200
201
202
203
204
205
206
207
208
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 200

def using_constraint(constraint, **options)
  @expected_constraint = Stannum::Support::Coercion.type_constraint(
    constraint,
    as: 'constraint',
    **options
  )

  self
end

#with_parameters(*arguments, **keywords, &block) ⇒ Stannum::RSpec::ValidateParameterMatcher

Specifies custom parameters to test.

The matcher will pass if and only if the method fails validation with the specified parameters.

Parameters:

  • arguments (Array)

    A list of arguments to test.

  • keywords (Hash)

    A hash of keywords to test.

Returns:



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 219

def with_parameters(*arguments, **keywords, &block)
  if @parameter_value
    raise 'cannot use both #with_parameters and #with_value'
  end

  @parameters = [
    arguments,
    keywords,
    block
  ]

  self
end

#with_value(parameter_value) ⇒ Stannum::RSpec::ValidateParameterMatcher

Specifies an invalid value for the parameter.

Parameters:

  • parameter_value (Object)

    The invalid value for the validated parameter.

Returns:



239
240
241
242
243
244
245
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 239

def with_value(parameter_value)
  raise 'cannot use both #with_parameters and #with_value' if @parameters

  @parameter_value = parameter_value

  self
end