Sha256: 4e0283ba152e52995e6dfbd8f0b232673a435a5042905f6810f49a15300a8309

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Matchi
  # *Expecting errors* matcher.
  class RaiseException
    # Initialize the matcher with a descendant of class Exception.
    #
    # @example
    #   require "matchi/raise_exception"
    #
    #   Matchi::RaiseException.new(NameError)
    #
    # @param expected [Exception] The class of the expected exception.
    def initialize(expected)
      @expected = expected
    end

    # Boolean comparison between the actual value and the expected value.
    #
    # @example
    #   require "matchi/raise_exception"
    #
    #   matcher = Matchi::RaiseException.new(NameError)
    #   matcher.matches? { Boom } # => true
    #
    # @yieldreturn [#object_id] The actual value to compare to the expected
    #   one.
    #
    # @return [Boolean] Comparison between actual and expected values.
    def matches?(*, **)
      yield
    rescue @expected => _e
      true
    else
      false
    end

    # A string containing a human-readable representation of the matcher.
    def inspect
      "#{self.class}(#{@expected.inspect})"
    end

    # Returns a string representing the matcher.
    def to_s
      "raise exception #{@expected.inspect}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
matchi-3.0.0 lib/matchi/raise_exception.rb