Class: Matchi::RaiseException

Inherits:
Object
  • Object
show all
Defined in:
lib/matchi/raise_exception.rb

Overview

Exception matcher: passes when evaluating the block raises an exception of the expected class or of one of its descendants (Module#===, so the exception cannot lie about its class), optionally with an expected message: a String the message must be eql? to, or a Regexp it must match.

The matcher rescues an exception of the expected class, and any StandardError, ScriptError, SystemStackError or SystemExit, which it then compares with the expected class. Anything else, an Interrupt, a signal, a NoMemoryError, the exception with which an enclosing Timeout ends its block, goes through as it would with any other matcher, unless that is the class expected. The expected class can be given directly, or by name; see Constant.

Examples:

Matchi::RaiseException.new(ZeroDivisionError).match? { 1 / 0 }              # => true
Matchi::RaiseException.new(:StandardError).match? { 1 / 0 }                 # => true
Matchi::RaiseException.new(ZeroDivisionError, /by 0/).match? { 1 / 0 }      # => true
Matchi::RaiseException.new(ZeroDivisionError, "nope").match? { 1 / 0 }      # => false
Matchi::RaiseException.new(IOError).match? { 1 / 0 }                        # => false

Instance Method Summary collapse

Constructor Details

#initialize(expected, message = nil) ⇒ RaiseException

Returns a new instance of RaiseException.

Parameters:

  • expected (Class, String, Symbol) —

    the expected exception class, or its name

  • message (String, Regexp, nil) (defaults to: nil) —

    the expected message, exact or as a pattern

Raises:

  • (ArgumentError) —

    when expected is neither an Exception class nor a constant name, or message is neither a String nor a Regexp



31
32
33
34
35
36
# File 'lib/matchi/raise_exception.rb', line 31

def initialize(expected, message = nil)
  @expected = Constant.normalize(expected, ::Class)
  @message = message
  validate_class!
  validate_message!
end

Instance Method Details

#match? ⇒ Boolean

Yield Returns:

  • (Object) —

    ignored, the block is evaluated for its exception

Returns:

  • (Boolean)

Raises:

  • (ArgumentError) —

    when no block is given

  • (NameError) —

    when the name is not defined

  • (TypeError) —

    when the name does not resolve to an Exception class



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/matchi/raise_exception.rb', line 43

def match?
  raise ::ArgumentError, "a block must be provided" unless block_given?

  # Resolved before the block, so that a NameError from the name is not
  # mistaken for an exception raised by the block.
  klass = expected_class

  # The expected class first, whatever it is; then what a block raises
  # on its own, the exceptions Defi keeps too.
  begin
    yield
    false
  rescue klass => e
    message?(e)
  rescue ::StandardError, ::ScriptError, ::SystemStackError, ::SystemExit
    false
  end
end

#to_s ⇒ String

Returns:

  • (String)


63
64
65
66
67
# File 'lib/matchi/raise_exception.rb', line 63

def to_s
  return "raise exception #{Constant.label(@expected)}" if @message.nil?

  "raise exception #{Constant.label(@expected)} with message #{@message.inspect}"
end