Sha256: 67a6847b6c927f0c08778f98f96ac5f218387957235bbd0dfdbc2a295290d5a7

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require_relative './induce_error'
require_relative './verify_key_in_hash'

module MiniTest
  # Adding custom assertions to make specs easier to read
  module Assertions
    # Base assertion class to verify error raised when parameter omitted.
    class BaseAssertRequiredParameter
      def initialize(klass, params, param_key, message)
        message ||= default_message_for(param_key)
        @params = Internals.hash_without_key(params, param_key)
        @klass = klass
        @param_key = param_key
        @message = message
        self
      end

      def call(assert)
        assert.call(correct_error_message?, message)
      end

      private

      attr_reader :params, :klass, :message, :param_key

      # Recent versions of Reek complain about a :reek:NilCheck. Yup.
      def actual_error_message
        errors[:expected]&.message
      end

      def correct_error_message?
        actual_error_message.match message
      end

      def errors
        InduceError.call error_class: error_class, inducer: error_inducer
      end

      # Methods that neither affect nor are affected by instance state.
      module Internals
        def self.hash_without_key(hash, key)
          VerifyKeyInHash.call hash, key
          hash.reject { |source_key, _| source_key == key }
        end
      end
      private_constant :Internals
    end # class MiniTest::Assertions::BaseAssertRequiredParameter
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
prolog_minitest_matchers-0.5.4 lib/prolog_minitest_matchers/matchers/asserters/base_assert_required_parameter.rb
prolog_minitest_matchers-0.5.3 lib/prolog_minitest_matchers/matchers/asserters/base_assert_required_parameter.rb
prolog_minitest_matchers-0.5.2 lib/prolog_minitest_matchers/matchers/asserters/base_assert_required_parameter.rb
prolog_minitest_matchers-0.5.1 lib/prolog_minitest_matchers/matchers/asserters/base_assert_required_parameter.rb
prolog_minitest_matchers-0.5.0 lib/prolog_minitest_matchers/matchers/asserters/base_assert_required_parameter.rb