Sha256: 3edc602e038c2de799cd04b2b6538819e63a45826999a9f4b35e43016d737c8f

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module RSpec
    module PrimitiveMatchers
      module Classes
        ##
        # NOTE: Navigate to the link below and `Ctrl + F` for "Custom Matcher from scratch"
        # for a guide of how to define a complex custom matcher.
        # https://rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers
        #
        # More info:
        # - https://relishapp.com/rspec/rspec-expectations/v/3-11/docs/custom-matchers/define-a-custom-matcher
        # - http://rspec.info/documentation/3.9/rspec-expectations/RSpec/Matchers/MatcherProtocol.html
        # - https://makandracards.com/makandra/662-write-custom-rspec-matchers
        #
        class BeDescendantOf
          def initialize(base_klass)
            @base_klass = base_klass
          end

          def matches?(klass)
            @klass = klass

            Utils.to_bool(klass < base_klass)
          end

          def description
            "be a descendant of `#{base_klass}`"
          end

          ##
          # NOTE: `failure_message` is only called when `matches?` returns `false`.
          # https://rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers/MatcherProtocol#failure_message-instance_method
          #
          def failure_message
            "expected #{klass} to be a descendant of `#{base_klass}`"
          end

          ##
          # NOTE: `failure_message_when_negated` is only called when `matches?` returns `false`.
          # https://rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers/MatcherProtocol#failure_message-instance_method
          #
          def failure_message_when_negated
            "expected #{klass} NOT to be a descendant of `#{base_klass}`"
          end

          private

          attr_reader :klass, :base_klass
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/rspec/primitive_matchers/classes/be_descendant_of.rb
convenient_service-0.19.0 lib/convenient_service/rspec/primitive_matchers/classes/be_descendant_of.rb
convenient_service-0.18.0 lib/convenient_service/rspec/primitive_matchers/classes/be_descendant_of.rb
convenient_service-0.17.0 lib/convenient_service/rspec/primitive_matchers/classes/be_descendant_of.rb
convenient_service-0.16.0 lib/convenient_service/rspec/primitive_matchers/classes/be_descendant_of.rb
convenient_service-0.15.0 lib/convenient_service/rspec/primitive_matchers/classes/be_descendant_of.rb