lib/convenient_service/rspec/matchers/custom/results/base.rb in convenient_service-0.6.0 vs lib/convenient_service/rspec/matchers/custom/results/base.rb in convenient_service-0.7.0

- old
+ new

@@ -1,7 +1,10 @@ # frozen_string_literal: true +require_relative "base/commands" +require_relative "base/errors" + module ConvenientService module RSpec module Matchers module Custom module Results @@ -22,11 +25,12 @@ rules = [] rules << ->(result) { result.class.include?(Service::Plugins::HasResult::Entities::Result::Concern) } rules << ->(result) { result.status.in?(statuses) } - rules << ->(result) { result.service.instance_of?(service_class) } if used_of? + rules << ->(result) { result.service.instance_of?(service_class) } if used_of_service? + rules << ->(result) { Commands::MatchResultStep.call(result: result, step: step) } if used_of_step? rules << ->(result) { result.unsafe_data == data } if used_data? rules << ->(result) { result.unsafe_message == message } if used_message? rules << ->(result) { result.unsafe_code == code } if used_code? condition = Utils::Proc.conjunct(rules) @@ -36,28 +40,28 @@ ## # @return [String] # def description - default_text + expected_parts end ## # @return [String] # def failure_message - "expected that `#{result}` would #{default_text}" + "expected that `#{result.service.class}` result would #{default_text}" end ## # @return [String] # # @internal # https://relishapp.com/rspec/rspec-expectations/v/3-11/docs/custom-matchers/define-a-custom-matcher#overriding-the-failure-message-when-negated # def failure_message_when_negated - "expected that #{result} would NOT #{default_text}" + "expected that `#{result.service.class}` result would NOT #{default_text}" end ## # @param data [Hash] # @return [ConvenientService::RSpec::Matchers::Custom::Results::Base] @@ -129,16 +133,35 @@ ## # @param service_class [Class] # @return [ConvenientService::RSpec::Matchers::Custom::Results::Base] # - def of(service_class) + def of_service(service_class) chain[:service_class] = service_class self end + ## + # @param step [Class, Symbol] + # @return [ConvenientService::RSpec::Matchers::Custom::Results::Base] + # + def of_step(step) + chain[:step] = step + + self + end + + ## + # @return [ConvenientService::RSpec::Matchers::Custom::Results::Base] + # + def without_step + chain[:step] = nil + + self + end + private ## # @!attribute [r] result # @return [ConvenientService::Service::Plugins::HasResult::Entities::Result] @@ -147,25 +170,49 @@ ## # @return [String] # def default_text + expected_parts << "\n\n" << got_parts + end + + ## + # @return [String] + # + # @internal + # TODO: Align for easier visual comparison. + # TODO: New line for each attribute. + # + def expected_parts parts = [] parts << "be #{printable_statuses}" - parts << "of `#{service_class}`" if used_of? + parts << "of service `#{service_class}`" if used_of_service? + parts << Commands::GenerateExpectedStepPart.call(step: step) if used_of_step? parts << "with data `#{data}`" if used_data? parts << "with message `#{message}`" if used_message? parts << "with code `#{code}`" if used_code? - parts << "\n\n" + parts.join(" ") + end + ## + # @return [String] + # + # @internal + # TODO: Align for easier visual comparison. + # TODO: New line for each attribute. + # + def got_parts + parts = [] + parts << "got `#{result.status}`" - parts << "of `#{result.service.class}`" if used_of? - parts << "with data `#{result.data}`" if used_data? - parts << "with message `#{result.message}`" if used_message? - parts << "with code `#{result.code}`" if used_code? + parts << "of service `#{result.service.class}`" if used_of_service? + parts << Commands::GenerateGotStepPart.call(result: result) if used_of_step? + parts << "with data `#{result.unsafe_data}`" if used_data? + parts << "with message `#{result.unsafe_message}`" if used_message? + parts << "with code `#{result.unsafe_code}`" if used_code? parts.join(" ") end ## @@ -190,15 +237,22 @@ end ## # @return [Boolean] # - def used_of? + def used_of_service? chain.key?(:service_class) end ## + # @return [Boolean] + # + def used_of_step? + chain.key?(:step) + end + + ## # @return [Hash] # def data @data ||= chain[:data] || {} end @@ -220,9 +274,16 @@ ## # @return [Class] # def service_class Utils::Object.instance_variable_fetch(self, :@service_class) { chain[:service_class] } + end + + ## + # @return [Class] + # + def step + Utils::Object.instance_variable_fetch(self, :@step) { chain[:step] } end ## # @return [Hash] #