Guest User

Untitled

a guest
Feb 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. require 'abstract_unit'
  2.  
  3. class WraithAttack < StandardError
  4. end
  5.  
  6. class NuclearExplosion < StandardError
  7. end
  8.  
  9. class MadRonon < StandardError
  10. attr_accessor :message
  11.  
  12. def initialize(message)
  13. @message = message
  14. super()
  15. end
  16. end
  17.  
  18. class Stargate
  19. attr_accessor :result
  20.  
  21. include ActiveSupport::Rescuable
  22.  
  23. rescue_from WraithAttack, :with => :sos
  24. rescue_from WraithAttack, :with => :run_to_earth, :scope => :process
  25.  
  26. rescue_from NuclearExplosion do
  27. @result = 'alldead'
  28. end
  29.  
  30. rescue_from MadRonon do |e|
  31. @result = e.message
  32. end
  33.  
  34. def dispatch(method)
  35. send(method)
  36. rescue Exception => e
  37. rescue_with_handler(e)
  38. end
  39.  
  40. # Rescue with process scope first. If that fails, go back to the default scope.
  41. def process(method)
  42. send(method)
  43. rescue Exception => e
  44. rescue_with_handler(e, :process) || rescue_with_handler(e)
  45. end
  46.  
  47. def attack
  48. raise WraithAttack
  49. end
  50.  
  51. def nuke
  52. raise NuclearExplosion
  53. end
  54.  
  55. def ronanize
  56. raise MadRonon.new("dex")
  57. end
  58.  
  59. def sos
  60. @result = 'killed'
  61. end
  62.  
  63. def run_to_earth
  64. @result = 'earth'
  65. end
  66. end
  67.  
  68. class RescueableTest < Test::Unit::TestCase
  69. def setup
  70. @stargate = Stargate.new
  71. end
  72.  
  73. def test_rescue_from_with_method
  74. @stargate.dispatch :attack
  75. assert_equal 'killed', @stargate.result
  76. end
  77.  
  78. def test_rescue_from_with_block
  79. @stargate.dispatch :nuke
  80. assert_equal 'alldead', @stargate.result
  81. end
  82.  
  83. def test_rescue_from_with_block_with_args
  84. @stargate.dispatch :ronanize
  85. assert_equal 'dex', @stargate.result
  86. end
  87.  
  88. def test_rescue_from_scope_with_method
  89. @stargate.process :attack
  90. assert_equal 'earth', @stargate.result
  91. end
  92.  
  93. def test_rescue_from_scope_falling_back_to_default
  94. @stargate.process :nuke
  95. assert_equal 'alldead', @stargate.result
  96. end
  97. end
Add Comment
Please, Sign In to add comment