/
testException.rb
102 lines (90 loc) · 1.68 KB
/
testException.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
require 'test/minirunit'
#test backtrace
if !(defined? $recurse)
test_check "Test Exception:"
$recurse = false
end
begin
if $recurse
raise Exception, 'test'
else
$recurse=true
load('test/testException.rb')
end
rescue Exception => boom
result = boom.backtrace.collect {|trace|
trace =~ /:(\d+):?/ && $1.to_i
}
test_equal([10,13,13] , result.slice(0..2))
end
test_no_exception {
begin
raise "X"
rescue NoMethodError,RuntimeError =>e
end
}
test_no_exception {
begin
begin
raise "X"
rescue NoMethodError
test_ok(false)
end
rescue
test_ok(true)
end
}
begin
e = StandardError.new
e.set_backtrace("abc")
rescue TypeError => e
test_ok(true)
end
begin
e = StandardError.new
e.set_backtrace(123)
rescue TypeError => e
test_ok(true)
end
begin
e = StandardError.new
e.set_backtrace(["abc", 123])
rescue TypeError => e
test_ok(true)
end
test_no_exception {
e = StandardError.new
e.set_backtrace(["abc", "123"])
}
test_no_exception {
e = StandardError.new
e.set_backtrace(nil)
}
begin
raise 'Something bad'
rescue
begin
test_ok(true)
rescue
test_ok(false)
end
test_ok($!, 'Global exception ($!) should not be nil')
test_ok($!.to_s.eql?('Something bad'), "Global exceptions should be 'Something bad', but is '#{$!}'")
end
begin
raise 'Something bad'
rescue
begin
raise 'Something else bad'
rescue
test_ok(true)
end
test_ok($!, 'Global exception ($!) should not be nil')
test_ok($!.to_s.eql?('Something bad'), "Global exceptions should be 'Something bad', but is '#{$!}'")
end
class TypeError
def new(*args)
StandardError.new
end
end
test_exception(StandardError) { 5 + "A" }