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 __(RuntimeError), MySpecialError.ancestors[1]
10 assert_equal __(StandardError), MySpecialError.ancestors[2]
11 assert_equal __(Exception), MySpecialError.ancestors[3]
12 assert_equal __(Object), MySpecialError.ancestors[4]
15 def test_rescue_clause
19 rescue StandardError => ex
20 result = :exception_handled
23 assert_equal __(:exception_handled), result
25 assert_equal __(true), ex.is_a?(StandardError), "Should be a Standard Error"
26 assert_equal __(true), 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 __("Oops"), 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 __(:exception_handled), result
44 assert_equal __("My Message"), ex.message
47 def test_ensure_clause
51 rescue StandardError => ex
57 assert_equal __(:always_run), 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
63 assert_raise(___(MySpecialError)) do
64 raise MySpecialError.new("New instances can be raised directly.")