Created
October 4, 2008 20:42
-
-
Save lifo/14803 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'abstract_unit' | |
class WraithAttack < StandardError | |
end | |
class NuclearExplosion < StandardError | |
end | |
class MadRonon < StandardError | |
attr_accessor :message | |
def initialize(message) | |
@message = message | |
super() | |
end | |
end | |
class Stargate | |
attr_accessor :result | |
include ActiveSupport::Rescuable | |
rescue_from WraithAttack, :with => :sos | |
rescue_from WraithAttack, :with => :run_to_earth, :scope => :process | |
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 | |
# Rescue with process scope first. If that fails, go back to the default scope. | |
def process(method) | |
send(method) | |
rescue Exception => e | |
rescue_with_handler(e, :process) || 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 | |
def run_to_earth | |
@result = 'earth' | |
end | |
end | |
class RescueableTest < Test::Unit::TestCase | |
def setup | |
@stargate = Stargate.new | |
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 | |
def test_rescue_from_scope_with_method | |
@stargate.process :attack | |
assert_equal 'earth', @stargate.result | |
end | |
def test_rescue_from_scope_falling_back_to_default | |
@stargate.process :nuke | |
assert_equal 'alldead', @stargate.result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment