Class: Matchi::RaiseException
- Inherits:
-
Object
- Object
- Matchi::RaiseException
- 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.
Instance Method Summary collapse
-
#initialize(expected, message = nil) ⇒ RaiseException
constructor
A new instance of RaiseException.
- #match? ⇒ Boolean
- #to_s ⇒ String
Constructor Details
#initialize(expected, message = nil) ⇒ RaiseException
Returns a new instance of RaiseException.
31 32 33 34 35 36 |
# File 'lib/matchi/raise_exception.rb', line 31 def initialize(expected, = nil) @expected = Constant.normalize(expected, ::Class) @message = validate_class! end |
Instance Method Details
#match? ⇒ Boolean
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 (e) rescue ::StandardError, ::ScriptError, ::SystemStackError, ::SystemExit false end end |