Sha256: 5d708b3cd5a926a1c82daca41117bfd3cefcf3b4e035de95caa26bd9a84c17c1

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module RSpec
    module Matchers
      module Custom
        ##
        # 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 BeDirectDescendantOf
          def initialize(base_klass)
            @base_klass = base_klass
          end

          def matches?(klass)
            @klass = klass

            klass.superclass == base_klass
          end

          def description
            "be a direct 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 direct 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 direct descendant of `#{base_klass}`"
          end

          private

          attr_reader :klass, :base_klass
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
convenient_service-0.14.0 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb
convenient_service-0.13.0 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb
convenient_service-0.12.0 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb
convenient_service-0.11.0 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb
convenient_service-0.10.1 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb
convenient_service-0.10.0 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb
convenient_service-0.9.0 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb
convenient_service-0.8.0 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb
convenient_service-0.7.0 lib/convenient_service/rspec/matchers/custom/be_direct_descendant_of.rb