2016-08-06 12:03:25 -04:00
|
|
|
require "abstract_unit"
|
2008-10-04 16:48:18 -04:00
|
|
|
|
|
|
|
class WraithAttack < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
class MadRonon < StandardError
|
|
|
|
end
|
|
|
|
|
2010-07-26 09:41:53 -04:00
|
|
|
class CoolError < StandardError
|
|
|
|
end
|
|
|
|
|
2014-12-09 05:04:50 -05:00
|
|
|
module WeirdError
|
|
|
|
def self.===(other)
|
|
|
|
Exception === other && other.respond_to?(:weird?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-04 16:48:18 -04:00
|
|
|
class Stargate
|
2016-05-13 20:43:48 -04:00
|
|
|
# Nest this so the 'NuclearExplosion' handler needs a lexical const_get
|
|
|
|
# to find it.
|
|
|
|
class NuclearExplosion < StandardError; end
|
|
|
|
|
2008-10-04 16:48:18 -04:00
|
|
|
attr_accessor :result
|
|
|
|
|
|
|
|
include ActiveSupport::Rescuable
|
|
|
|
|
2016-08-06 13:38:33 -04:00
|
|
|
rescue_from WraithAttack, with: :sos_first
|
2010-07-23 15:51:46 -04:00
|
|
|
|
2016-08-06 13:38:33 -04:00
|
|
|
rescue_from WraithAttack, with: :sos
|
2008-10-04 16:48:18 -04:00
|
|
|
|
2016-08-06 12:03:25 -04:00
|
|
|
rescue_from "NuclearExplosion" do
|
|
|
|
@result = "alldead"
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-10-04 16:48:18 -04:00
|
|
|
rescue_from MadRonon do |e|
|
|
|
|
@result = e.message
|
|
|
|
end
|
|
|
|
|
2014-12-09 05:04:50 -05:00
|
|
|
rescue_from WeirdError do
|
2016-08-06 12:03:25 -04:00
|
|
|
@result = "weird"
|
2014-12-09 05:04:50 -05:00
|
|
|
end
|
|
|
|
|
2008-10-04 16:48:18 -04:00
|
|
|
def dispatch(method)
|
|
|
|
send(method)
|
|
|
|
rescue Exception => e
|
|
|
|
rescue_with_handler(e)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attack
|
|
|
|
raise WraithAttack
|
|
|
|
end
|
|
|
|
|
|
|
|
def nuke
|
|
|
|
raise NuclearExplosion
|
|
|
|
end
|
|
|
|
|
|
|
|
def ronanize
|
|
|
|
raise MadRonon.new("dex")
|
|
|
|
end
|
|
|
|
|
2016-05-13 20:43:48 -04:00
|
|
|
def fall_back_to_cause
|
|
|
|
# This exception is the cause and has a handler.
|
|
|
|
ronanize
|
|
|
|
rescue
|
|
|
|
# This is the exception we'll handle that doesn't have a cause.
|
2016-08-06 12:03:25 -04:00
|
|
|
raise "unhandled RuntimeError with a handleable cause"
|
2016-05-13 20:43:48 -04:00
|
|
|
end
|
|
|
|
|
2014-12-09 05:04:50 -05:00
|
|
|
def weird
|
|
|
|
StandardError.new.tap do |exc|
|
|
|
|
def exc.weird?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
raise exc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-10-04 16:48:18 -04:00
|
|
|
def sos
|
2016-08-06 12:03:25 -04:00
|
|
|
@result = "killed"
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|
2010-07-23 15:51:46 -04:00
|
|
|
|
|
|
|
def sos_first
|
2016-08-06 12:03:25 -04:00
|
|
|
@result = "sos_first"
|
2010-07-23 15:51:46 -04:00
|
|
|
end
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|
|
|
|
|
2010-07-26 09:41:53 -04:00
|
|
|
class CoolStargate < Stargate
|
|
|
|
attr_accessor :result
|
|
|
|
|
|
|
|
include ActiveSupport::Rescuable
|
|
|
|
|
2016-08-06 13:38:33 -04:00
|
|
|
rescue_from CoolError, with: :sos_cool_error
|
2010-07-26 09:41:53 -04:00
|
|
|
|
|
|
|
def sos_cool_error
|
2016-08-06 12:03:25 -04:00
|
|
|
@result = "sos_cool_error"
|
2010-07-26 09:41:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class RescuableTest < ActiveSupport::TestCase
|
2008-10-04 16:48:18 -04:00
|
|
|
def setup
|
|
|
|
@stargate = Stargate.new
|
2010-07-26 09:41:53 -04:00
|
|
|
@cool_stargate = CoolStargate.new
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rescue_from_with_method
|
|
|
|
@stargate.dispatch :attack
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "killed", @stargate.result
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-10-04 16:48:18 -04:00
|
|
|
def test_rescue_from_with_block
|
|
|
|
@stargate.dispatch :nuke
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "alldead", @stargate.result
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2008-10-04 16:48:18 -04:00
|
|
|
def test_rescue_from_with_block_with_args
|
|
|
|
@stargate.dispatch :ronanize
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "dex", @stargate.result
|
2010-08-14 01:13:00 -04:00
|
|
|
end
|
2010-07-23 15:51:46 -04:00
|
|
|
|
2014-12-09 05:04:50 -05:00
|
|
|
def test_rescue_from_error_dispatchers_with_case_operator
|
|
|
|
@stargate.dispatch :weird
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "weird", @stargate.result
|
2014-12-09 05:04:50 -05:00
|
|
|
end
|
|
|
|
|
2010-07-23 15:51:46 -04:00
|
|
|
def test_rescues_defined_later_are_added_at_end_of_the_rescue_handlers_array
|
2014-12-09 05:04:50 -05:00
|
|
|
expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon", "WeirdError"]
|
2014-10-27 12:28:53 -04:00
|
|
|
result = @stargate.send(:rescue_handlers).collect(&:first)
|
2010-07-23 15:51:46 -04:00
|
|
|
assert_equal expected, result
|
|
|
|
end
|
|
|
|
|
2013-05-26 06:57:05 -04:00
|
|
|
def test_children_should_inherit_rescue_definitions_from_parents_and_child_rescue_should_be_appended
|
2014-12-09 05:04:50 -05:00
|
|
|
expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon", "WeirdError", "CoolError"]
|
2014-10-27 12:28:53 -04:00
|
|
|
result = @cool_stargate.send(:rescue_handlers).collect(&:first)
|
2010-07-26 09:41:53 -04:00
|
|
|
assert_equal expected, result
|
|
|
|
end
|
2016-05-13 20:43:48 -04:00
|
|
|
|
|
|
|
def test_rescue_falls_back_to_exception_cause
|
|
|
|
@stargate.dispatch :fall_back_to_cause
|
2016-08-06 12:03:25 -04:00
|
|
|
assert_equal "unhandled RuntimeError with a handleable cause", @stargate.result
|
2016-05-13 20:43:48 -04:00
|
|
|
end
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|