class Cuprum::RSpec::BeAResultMatcher
Custom matcher that asserts the actual object is a Cuprum
result object with the specified properties.
Constants
- DEFAULT_VALUE
- RSPEC_MATCHER_METHODS
Attributes
Public Class Methods
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 16 def initialize @expected_error = DEFAULT_VALUE @expected_value = DEFAULT_VALUE end
Public Instance Methods
@return [String] a short description of the matcher and expected
properties.
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 23 def description message = 'be a Cuprum result' return message unless expected_properties? "#{message} #{properties_description}" end
Checks that the given actual object is not a Cuprum
result.
@param actual [Object] The actual object to match.
@return [Boolean] false if the actual object is a result; otherwise true.
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 36 def does_not_match?(actual) @actual = actual raise ArgumentError, negated_matcher_warning if expected_properties? !actual_is_result? end
@return [String] a summary message describing a failed expectation.
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 45 def failure_message message = "expected #{actual.inspect} to #{description}" if !actual_is_result? "#{message}, but the object is not a result" elsif actual_is_uncalled_operation? "#{message}, but the object is an uncalled operation" elsif !properties_match? message + properties_failure_message else # :nocov: message # :nocov: end end
@return [String] a summary message describing a failed negated
expectation.
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 63 def failure_message_when_negated "expected #{actual.inspect} not to #{description}" end
Checks that the given actual object is a Cuprum
result or compatible object and has the specified properties.
@param actual [Object] The actual object to match.
@return [Boolean] true if the actual object is a result with the expected
properties; otherwise false.
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 74 def matches?(actual) @actual = actual actual_is_result? && !actual_is_uncalled_operation? && properties_match? end
Sets an error expectation on the matcher. Calls to matches?
will fail unless the actual object has the specified error.
@param error [Cuprum::Error, Object] The expected error.
@return [BeAResultMatcher] the updated matcher.
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 86 def with_error(error) @expected_error = error self end
Sets a status expectation on the matcher. Calls to matches?
will fail unless the actual object has the specified status.
@param status [Symbol] The expected status.
@return [BeAResultMatcher] the updated matcher.
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 99 def with_status(status) @expected_status = status self end
Sets a value expectation on the matcher. Calls to matches?
will fail unless the actual object has the specified value.
@param value [Object] The expected value.
@return [BeAResultMatcher] the updated matcher.
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 112 def with_value(value) @expected_value = value self end
Private Instance Methods
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 127 def actual_is_result? actual.respond_to?(:to_cuprum_result) end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 131 def actual_is_uncalled_operation? result.error.is_a?(Cuprum::Errors::OperationNotCalled) end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 135 def compare_items(expected, actual) return expected.matches?(actual) if expected.respond_to?(:matches?) expected == actual end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 141 def error_failure_message return '' if error_matches? "\n expected error: #{inspect_expected(expected_error)}" \ "\n actual error: #{result.error.inspect}" end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 148 def error_matches? return @error_matches unless @error_matches.nil? return @error_matches = true unless expected_error? @error_matches = compare_items(expected_error, result.error) end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 162 def expected_error? expected_error != DEFAULT_VALUE end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 156 def expected_properties? (expected_error? && !expected_error.nil?) || expected_status? || expected_value? end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 166 def expected_status? !!expected_status end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 170 def expected_value? expected_value != DEFAULT_VALUE end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 174 def inspect_expected(expected) return expected.description if rspec_matcher?(expected) expected.inspect end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 180 def negated_matcher_warning "Using `expect().not_to be_a_result#{properties_warning}` risks false" \ ' positives, since any other result will match.' end
rubocop:disable Metrics/AbcSize
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 186 def properties_description msg = '' ary = [] ary << 'value' if expected_value? ary << 'error' if expected_error? && !expected_error.nil? unless ary.empty? msg = "with the expected #{tools.array_tools.humanize_list(ary)}" end return msg unless expected_status? return "with status: #{expected_status.inspect}" if msg.empty? msg + " and status: #{expected_status.inspect}" end
rubocop:enable Metrics/AbcSize
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 204 def properties_failure_message properties_short_message + status_failure_message + value_failure_message + error_failure_message end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 211 def properties_match? error_matches? && status_matches? && value_matches? end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 215 def properties_short_message ary = [] ary << 'status' unless status_matches? ary << 'value' unless value_matches? ary << 'error' unless error_matches? ", but the #{tools.array_tools.humanize_list(ary)}" \ " #{tools.integer_tools.pluralize(ary.size, 'does', 'do')} not match:" end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 225 def properties_warning ary = [] ary << 'value' if expected_value? ary << 'status' if expected_status? ary << 'error' if expected_error? return '' if ary.empty? message = ".with_#{ary.first}()" return message if ary.size == 1 message + ary[1..-1].map { |str| ".and_#{str}()" }.join end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 240 def result @result ||= actual.to_cuprum_result end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 244 def rspec_matcher?(value) RSPEC_MATCHER_METHODS.all? do |method_name| value.respond_to?(method_name) end end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 250 def status_failure_message return '' if status_matches? "\n expected status: #{expected_status.inspect}" \ "\n actual status: #{result.status.inspect}" end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 257 def status_matches? return @status_matches unless @status_matches.nil? return @status_matches = true unless expected_status? @status_matches = result.status == expected_status end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 265 def tools SleepingKingStudios::Tools::Toolbelt.instance end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 269 def value_failure_message return '' if value_matches? "\n expected value: #{inspect_expected(expected_value)}" \ "\n actual value: #{result.value.inspect}" end
# File lib/cuprum/rspec/be_a_result_matcher.rb, line 276 def value_matches? return @value_matches unless @value_matches.nil? return @value_matches = true unless expected_value? @value_matches = compare_items(expected_value, result.value) end