2008-10-04 20:48:18 +00:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
class WraithAttack < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
class NuclearExplosion < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
class MadRonon < StandardError
|
|
|
|
end
|
|
|
|
|
2010-07-26 13:41:53 +00:00
|
|
|
class CoolError < StandardError
|
|
|
|
end
|
|
|
|
|
2008-10-04 20:48:18 +00:00
|
|
|
class Stargate
|
|
|
|
attr_accessor :result
|
|
|
|
|
|
|
|
include ActiveSupport::Rescuable
|
|
|
|
|
2010-07-23 19:51:46 +00:00
|
|
|
rescue_from WraithAttack, :with => :sos_first
|
|
|
|
|
2008-10-04 20:48:18 +00:00
|
|
|
rescue_from WraithAttack, :with => :sos
|
|
|
|
|
|
|
|
rescue_from NuclearExplosion do
|
|
|
|
@result = 'alldead'
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue_from MadRonon do |e|
|
|
|
|
@result = e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def sos
|
|
|
|
@result = 'killed'
|
|
|
|
end
|
2010-07-23 19:51:46 +00:00
|
|
|
|
|
|
|
def sos_first
|
|
|
|
@result = 'sos_first'
|
|
|
|
end
|
|
|
|
|
2008-10-04 20:48:18 +00:00
|
|
|
end
|
|
|
|
|
2010-07-26 13:41:53 +00:00
|
|
|
class CoolStargate < Stargate
|
|
|
|
attr_accessor :result
|
|
|
|
|
|
|
|
include ActiveSupport::Rescuable
|
|
|
|
|
|
|
|
rescue_from CoolError, :with => :sos_cool_error
|
|
|
|
|
|
|
|
def sos_cool_error
|
|
|
|
@result = 'sos_cool_error'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-10-04 20:48:18 +00:00
|
|
|
class RescueableTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@stargate = Stargate.new
|
2010-07-26 13:41:53 +00:00
|
|
|
@cool_stargate = CoolStargate.new
|
2008-10-04 20:48:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rescue_from_with_method
|
|
|
|
@stargate.dispatch :attack
|
|
|
|
assert_equal 'killed', @stargate.result
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rescue_from_with_block
|
|
|
|
@stargate.dispatch :nuke
|
|
|
|
assert_equal 'alldead', @stargate.result
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rescue_from_with_block_with_args
|
|
|
|
@stargate.dispatch :ronanize
|
|
|
|
assert_equal 'dex', @stargate.result
|
|
|
|
end
|
2010-07-23 19:51:46 +00:00
|
|
|
|
|
|
|
def test_rescues_defined_later_are_added_at_end_of_the_rescue_handlers_array
|
|
|
|
expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon"]
|
|
|
|
result = @stargate.send(:rescue_handlers).collect {|e| e.first}
|
|
|
|
assert_equal expected, result
|
|
|
|
end
|
|
|
|
|
2010-07-26 13:41:53 +00:00
|
|
|
def test_children_should_inherit_rescue_defintions_from_parents_and_child_rescue_should_be_appended
|
|
|
|
expected = ["WraithAttack", "WraithAttack", "NuclearExplosion", "MadRonon", "CoolError"]
|
|
|
|
result = @cool_stargate.send(:rescue_handlers).collect {|e| e.first}
|
|
|
|
assert_equal expected, result
|
|
|
|
end
|
|
|
|
|
2008-10-04 20:48:18 +00:00
|
|
|
end
|