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.
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_constraint ⇒ Stannum::Constraints::Base? (readonly)
Returns the constraint used to generate the expected error(s).
86 87 88 |
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 86 def expected_constraint @expected_constraint end |
#method_name ⇒ String, Symbol (readonly)
Returns 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_name ⇒ String, Symbol (readonly)
Returns 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_value ⇒ Object (readonly)
Returns 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 |
#parameters ⇒ Hash (readonly)
Returns 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
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
#description ⇒ String
Returns 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.
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) @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.
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 # 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#{}" 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.
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 154 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.
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.
200 201 202 203 204 205 206 207 208 |
# File 'lib/stannum/rspec/validate_parameter_matcher.rb', line 200 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.
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.
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 |