PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/watchdog/test/multi-exception-tests.rb

https://bitbucket.org/redricko/pragprog-scripting
Ruby | 75 lines | 57 code | 11 blank | 7 comment | 0 complexity | 8b38af74beea72af9535d812d38f42d7 MD5 | raw file
  1. #---
  2. # Excerpted from "Everyday Scripting in Ruby"
  3. # We make no guarantees that this code is fit for any purpose.
  4. # Visit http://www.pragmaticprogrammer.com/titles/bmsft for more book information.
  5. #---
  6. require "set-standalone-test-paths.rb" unless $started_from_rakefile
  7. require 'test/unit'
  8. require 's4t-utils'
  9. include S4tUtils
  10. require 'watchdog/multi-exceptions'
  11. class QueueExtensionTests < Test::Unit::TestCase
  12. def test_to_a
  13. queue = Queue.new
  14. queue << 1
  15. assert_equal([1], queue.to_a)
  16. end
  17. end
  18. class MultiExceptionTests < Test::Unit::TestCase
  19. include Watchdog
  20. def two_exceptions
  21. retval = []
  22. [IndexError.new("index error"),
  23. RuntimeError.new("runtime error")].each do | ex |
  24. begin
  25. raise ex # You have to raise to get a backtrace.
  26. rescue Exception => caught
  27. retval << caught
  28. end
  29. end
  30. retval
  31. end
  32. def setup
  33. @ex = MultiException.new(two_exceptions)
  34. end
  35. def test_multi_exception_aggregates_messages
  36. messages = @ex.message.split("\n")
  37. assert_match(/index error/, messages[0])
  38. assert_match(/runtime error/, messages[1])
  39. end
  40. def test_multi_exception_includes_exception_classes_in_messages
  41. messages = @ex.message.split("\n")
  42. assert_match(/IndexError/, messages[0])
  43. assert_match(/RuntimeError/, messages[1])
  44. end
  45. def test_multi_exception_aggregates_backtraces_in_traces_field
  46. assert_equal(2, @ex.traces.length)
  47. assert_equal(1, @ex.traces[0].grep(/setup/).length)
  48. assert_equal(1, @ex.traces[1].grep(/setup/).length)
  49. end
  50. def test_combined_trace
  51. begin
  52. MultiException.reraise_with_combined_backtrace do
  53. raise @ex
  54. end
  55. rescue MultiException => ex
  56. assert_equal(1, ex.backtrace.grep(/Trace number 0:/).length)
  57. assert_equal(1, ex.backtrace.grep(/Trace number 1:/).length)
  58. # The raise in this method is wiped out.
  59. assert_equal(0, ex.backtrace.grep(/test_combined_trace/).length)
  60. # The raises in setup are preserved.
  61. assert_equal(2, ex.backtrace.grep(/setup/).length)
  62. end
  63. end
  64. end