1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 class AboutExceptions < EdgeCase::Koan
5 class MySpecialError < RuntimeError
8 def test_exceptions_inherit_from_Exception
9 assert_equal __, MySpecialError.ancestors[1]
10 assert_equal __, MySpecialError.ancestors[2]
11 assert_equal __, MySpecialError.ancestors[3]
12 assert_equal __, MySpecialError.ancestors[4]
15 def test_rescue_clause
19 rescue StandardError => ex
20 result = :exception_handled
23 assert_equal __, result
25 assert_equal __, ex.is_a?(StandardError), "Should be a Standard Error"
26 assert_equal __, ex.is_a?(RuntimeError), "Should be a Runtime Error"
28 assert RuntimeError.ancestors.include?(StandardError),
29 "RuntimeError is a subclass of StandardError"
31 assert_equal __, ex.message
34 def test_raising_a_particular_error
37 # 'raise' and 'fail' are synonyms
38 raise MySpecialError, "My Message"
39 rescue MySpecialError => ex
40 result = :exception_handled
43 assert_equal __, result
44 assert_equal __, ex.message
47 def test_ensure_clause
51 rescue StandardError => ex
57 assert_equal __, result
60 # Sometimes, we must know about the unknown
61 def test_asserting_an_error_is_raised
62 # A do-end is a block, a topic to explore more later
64 raise MySpecialError.new("New instances can be raised directly.")