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.



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

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:



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

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.



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

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.



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

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.



106
107
108
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 106

def parameter_value
  @parameter_value
end

#parametersHash (readonly)

Returns the configured parameters to match.

Returns:

  • (Hash)

    the configured parameters to match.



100
101
102
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 100

def parameters
  @parameters
end

Class Method Details

.add_parameter_mapping(map:, match:) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 31

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:, map: }
end

.map_parameters(actual:, method_name:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 39

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

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

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

  unwrapped_method(actual:, 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.



110
111
112
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 110

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.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 120

def does_not_match?(actual) # rubocop:disable Naming/PredicatePrefix
  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.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 135

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.



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 162

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 181

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:



208
209
210
211
212
213
214
215
216
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 208

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:



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 227

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:



247
248
249
250
251
252
253
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 247

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

  @parameter_value = parameter_value

  self
end