Class: Stannum::RSpec::ValidateParameterMatcher
- Inherits:
-
Object
- Object
- Stannum::RSpec::ValidateParameterMatcher
- 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
-
#expected_constraint ⇒ Stannum::Constraints::Base?
readonly
The constraint used to generate the expected error(s).
-
#method_name ⇒ String, Symbol
readonly
The name of the method with validated parameters.
-
#parameter_name ⇒ String, Symbol
readonly
The name of the validated method parameter.
-
#parameter_value ⇒ Object
readonly
The invalid value for the validated parameter.
-
#parameters ⇒ Hash
readonly
The configured parameters to match.
Class Method Summary collapse
Instance Method Summary collapse
-
#description ⇒ String
A short description of the matcher and expected properties.
-
#does_not_match?(actual) ⇒ true, false
Asserts that the object does not validate the specified method parameter.
-
#failure_message ⇒ String
A summary message describing a failed expectation.
-
#failure_message_when_negated ⇒ String
A summary message describing a failed negated expectation.
-
#initialize(method_name:, parameter_name:) ⇒ ValidateParameterMatcher
constructor
A new instance of ValidateParameterMatcher.
-
#matches?(actual) ⇒ true, false
Asserts that the object validates the specified method parameter.
-
#using_constraint(constraint, **options) ⇒ Stannum::RSpec::ValidateParameterMatcher
Specifies a constraint or type used to validate the parameter.
-
#with_parameters(*arguments, **keywords, &block) ⇒ Stannum::RSpec::ValidateParameterMatcher
Specifies custom parameters to test.
-
#with_value(parameter_value) ⇒ Stannum::RSpec::ValidateParameterMatcher
Specifies an invalid value for the parameter.
Constructor Details
#initialize(method_name:, parameter_name:) ⇒ ValidateParameterMatcher
Returns a new instance of ValidateParameterMatcher.
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_constraint ⇒ Stannum::Constraints::Base? (readonly)
Returns the constraint used to generate the expected error(s).
94 95 96 |
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 94 def expected_constraint @expected_constraint end |
#method_name ⇒ String, Symbol (readonly)
Returns 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_name ⇒ String, Symbol (readonly)
Returns 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_value ⇒ Object (readonly)
Returns 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 |
#parameters ⇒ Hash (readonly)
Returns 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
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
#description ⇒ String
Returns 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.
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 @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_message ⇒ String
Returns 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 # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength = "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 [, reason].compact.join(', but ') end |
#failure_message_when_negated ⇒ String
Returns 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 = "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 [, reason].compact.join(', but ') end |
#matches?(actual) ⇒ true, false
Asserts that the object validates the specified method parameter.
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.
208 209 210 211 212 213 214 215 216 |
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 208 def using_constraint(constraint, **) @expected_constraint = Stannum::Support::Coercion.type_constraint( constraint, as: 'constraint', ** ) 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.
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.
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 |