/
testException2.rb
111 lines (98 loc) · 2.75 KB
/
testException2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'test/minirunit'
test_check "Test Exception (2)"
def raise_test(thing_to_raise, exception_to_rescue)
e = nil
begin
raise thing_to_raise
rescue exception_to_rescue
e = $!
end
test_equal(exception_to_rescue, e.class)
end
test_ok(ArgumentError < Exception)
raise_test(ArgumentError.new("hello"), ArgumentError)
raise_test("hello", RuntimeError)
class SomeOtherException < StandardError
end
e = Exception.new
test_ok(!e.kind_of?(SomeOtherException))
test_ok(!e.kind_of?(StandardError))
test_ok(e.kind_of?(Exception))
begin
raise "whoah!"
rescue SomeOtherException
test_fail()
rescue Exception
test_ok(true)
end
class Foo
end
class Har
def exception(message)
Bar.new(message)
end
end
class Bar < Exception
def exception(message)
1
end
end
class Gar < Exception
def exception(message)
Bar.new(message)
end
end
test_exception(TypeError) { raise nil }
test_exception(TypeError) { raise Foo }
test_exception(TypeError) { raise Foo, "HEH" }
test_exception(TypeError) { raise Foo, "HEH", caller }
test_exception(TypeError) { raise Har }
test_exception(TypeError) { raise Har, "HEH" }
test_exception(TypeError) { raise Har, "HEH", caller }
test_exception(Bar) { raise Bar }
test_exception(Bar) { raise Bar, "HEH" }
test_exception(Bar) { raise Bar, "HEH", caller }
test_exception(Bar) { raise Gar.new, "HEH" }
test_exception(TypeError) { raise Bar.new, "HEH" }
test_exception(TypeError) { raise Bar.new, "HEH", caller }
# empty rescue block should cause method to return nil
def do_except
raise Exception.new
rescue Exception
end
test_equal(nil, do_except)
# Check exception hierarchy structure
test_ok(NoMemoryError < Exception)
test_ok(ScriptError < Exception)
test_ok(LoadError < ScriptError)
test_ok(NotImplementedError < ScriptError)
test_ok(SyntaxError < ScriptError)
# we don't implement SignalError or descendants
test_ok(StandardError < Exception)
test_ok(ArgumentError < StandardError)
test_ok(IOError < StandardError)
test_ok(EOFError < IOError)
test_ok(IndexError < StandardError)
test_ok(LocalJumpError < StandardError)
test_ok(NameError < StandardError)
test_ok(NoMethodError < NameError)
test_ok(RangeError < StandardError)
test_ok(FloatDomainError < RangeError)
test_ok(RegexpError < StandardError)
test_ok(RuntimeError < StandardError)
test_ok(SecurityError < StandardError)
# we don't implement SystemCallError
test_ok(ThreadError < StandardError)
test_ok(TypeError < StandardError)
test_ok(ZeroDivisionError < StandardError)
test_ok(SystemExit < Exception)
test_ok(SystemStackError < Exception)
n = NameError.new
test_equal("NameError", n.message)
test_equal(nil, n.name)
n = NameError.new("foo")
test_equal("foo", n.message)
test_equal(nil, n.name)
n = NameError.new("foo", "bar")
test_equal("foo", n.message)
test_equal("bar", n.name)