Sha256: 1a554efe8eb1dbc204f27f54310bd62f5b9a6ee96f9da8a0975b110e4a3cabc2

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 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 BeDescendantOf
          def initialize(base_klass)
            @base_klass = base_klass
          end

          def matches?(klass)
            @klass = klass

            Utils::Bool.to_bool(klass < base_klass)
          end

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

          ##
          # NOTE: `failure_message` is only called when `mathces?` 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 `mathces?` 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

8 entries across 8 versions & 1 rubygems

Version Path
convenient_service-0.6.0 lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb
convenient_service-0.5.0 lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb
convenient_service-0.4.0 lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb
convenient_service-0.3.1 lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb
convenient_service-0.3.0 lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb
convenient_service-0.2.1 lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb
convenient_service-0.2.0 lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb
convenient_service-0.1.0 lib/convenient_service/rspec/matchers/custom/be_descendant_of.rb