2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "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
|
2017-05-10 13:30:41 -04:00
|
|
|
unless rescue_with_handler(e)
|
|
|
|
@result = "unhandled"
|
|
|
|
end
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def attack
|
|
|
|
raise WraithAttack
|
|
|
|
end
|
|
|
|
|
|
|
|
def nuke
|
|
|
|
raise NuclearExplosion
|
|
|
|
end
|
|
|
|
|
|
|
|
def ronanize
|
|
|
|
raise MadRonon.new("dex")
|
|
|
|
end
|
|
|
|
|
2017-05-10 13:30:41 -04:00
|
|
|
def crash
|
|
|
|
raise "unhandled RuntimeError"
|
|
|
|
end
|
|
|
|
|
|
|
|
def looped_crash
|
|
|
|
ex1 = StandardError.new("error 1")
|
|
|
|
ex2 = StandardError.new("error 2")
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
raise ex1
|
|
|
|
rescue
|
|
|
|
# sets the cause on ex2 to be ex1
|
|
|
|
raise ex2
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
# sets the cause on ex1 to be ex2
|
|
|
|
raise ex1
|
|
|
|
end
|
|
|
|
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-11-17 14:49:07 -05:00
|
|
|
assert_equal "dex", @stargate.result
|
2016-05-13 20:43:48 -04:00
|
|
|
end
|
2017-05-10 13:30:41 -04:00
|
|
|
|
|
|
|
def test_unhandled_exceptions
|
|
|
|
@stargate.dispatch(:crash)
|
|
|
|
assert_equal "unhandled", @stargate.result
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rescue_handles_loops_in_exception_cause_chain
|
|
|
|
@stargate.dispatch :looped_crash
|
|
|
|
assert_equal "unhandled", @stargate.result
|
|
|
|
end
|
2008-10-04 16:48:18 -04:00
|
|
|
end
|