Class: Given::Failure
- Inherits:
- BasicObject
- Defined in:
- lib/given/failure.rb,
lib/given/minitest/failure_must_raise.rb
Overview
Failure objects will raise the given exception whenever you try to send it any message.
Class Method Summary collapse
-
.capture(*exceptions) ⇒ Object
Evaluate a block.
Instance Method Summary collapse
-
#!=(other) ⇒ Object
Failure objects may be compared for in-equality.
-
#==(other) ⇒ Object
Failure objects may be compared for equality.
-
#initialize(exception) ⇒ Failure
constructor
Create a failure object that will rethrow the given exception whenever an undefined method is called.
-
#is_a?(klass) ⇒ Boolean
Failure objects will respond to #is_a?.
-
#method_missing(sym, *args, &block) ⇒ Object
Most methods will just re-raise the captured exception.
-
#must_raise(*args) ⇒ Object
Minitest expectation method.
-
#respond_to?(method_symbol) ⇒ Boolean
Report that we respond to a limited number of methods.
Constructor Details
#initialize(exception) ⇒ Failure
Create a failure object that will rethrow the given exception whenever an undefined method is called.
24 25 26 |
# File 'lib/given/failure.rb', line 24 def initialize(exception) @exception = exception end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Most methods will just re-raise the captured exception.
54 55 56 |
# File 'lib/given/failure.rb', line 54 def method_missing(sym, *args, &block) die end |
Class Method Details
.capture(*exceptions) ⇒ Object
Evaluate a block. If an exception is raised, capture it in a Failure object. Explicitly listed exceptions are passed thru without capture.
12 13 14 15 16 17 18 19 20 |
# File 'lib/given/failure.rb', line 12 def self.capture(*exceptions) begin yield rescue *exceptions => ex raise rescue ::Exception => ex new(ex) end end |
Instance Method Details
#!=(other) ⇒ Object
Failure objects may be compared for in-equality. If the comparison object is not a matcher, then the exception is re-raised.
45 46 47 48 49 50 51 |
# File 'lib/given/failure.rb', line 45 def !=(other) if failure_matcher?(other) other.does_not_match?(self) else die end end |
#==(other) ⇒ Object
Failure objects may be compared for equality. If the comparison object is not a matcher, then the exception is re-raised.
35 36 37 38 39 40 41 |
# File 'lib/given/failure.rb', line 35 def ==(other) if failure_matcher?(other) other.matches?(self) else die end end |
#is_a?(klass) ⇒ Boolean
Failure objects will respond to #is_a?.
29 30 31 |
# File 'lib/given/failure.rb', line 29 def is_a?(klass) klass == Failure end |
#must_raise(*args) ⇒ Object
Minitest expectation method. Since Failure inherits from BasicObject, we need to add this method explicitly.
6 7 8 9 10 |
# File 'lib/given/minitest/failure_must_raise.rb', line 6 def must_raise(*args) ::Minitest::Spec.current.assert_raises(*args) do die end end |
#respond_to?(method_symbol) ⇒ Boolean
Report that we respond to a limited number of methods.
59 60 61 62 63 64 65 |
# File 'lib/given/failure.rb', line 59 def respond_to?(method_symbol) method_symbol == :call || method_symbol == :== || method_symbol == :!= || method_symbol == :is_a? || method_symbol == :to_bool end |