-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_error_expectations.rb
144 lines (112 loc) · 3.8 KB
/
test_error_expectations.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require File.dirname(__FILE__) + '/test_helper.rb'
class TestErrorExpectations < Test::Unit::TestCase
def test_raises_error
lambda { raise "FAIL" }.should raise_error
end
def test_raises_error_fail
lambda {
lambda { "WIN" }.should raise_error
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_raises_error
lambda { "WIN" }.should_not raise_error
end
def test_negative_raises_error_fail
lambda {
lambda { raise "FAIL" }.should_not raise_error
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_raises_specific_error
lambda { raise TypeError }.should raise_error(TypeError)
end
def test_raises_specific_error_fail_with_no_error
lambda {
lambda { "WIN" }.should raise_error(TypeError)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_raises_specific_error_fail_with_different_error
lambda {
lambda { raise StandardError }.should raise_error(TypeError)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_throws_symbol
lambda {
throw :win
}.should throw_symbol(:win)
end
def test_throws_symbol_fails_with_different_symbol
lambda {
lambda {
throw :fail
}.should throw_symbol(:win)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_negative_throws_symbol
lambda {
"not this time!"
}.should_not throw_symbol(:win)
end
def test_negative_throws_symbol_fails_with_different_symbol
lambda{
lambda {
throw :fail
}.should_not throw_symbol(:fail)
}.should raise_error(Test::Unit::AssertionFailedError)
end
def test_error_fail_message
obj = raise_error(TypeError)
obj.matches?(lambda { raise NameError })
obj.failure_message.should =~ /Expected #<(.*)> to raise TypeError, but NameError was raised instead./
end
def test_error_fail_message_when_no_error
obj = raise_error(TypeError)
obj.matches?(lambda { "moop" })
obj.failure_message.should =~ /Expected #<(.*)> to raise TypeError, but none was raised./
end
def test_error_negative_fail_message
obj = raise_error(TypeError)
obj.matches?(lambda { raise TypeError })
obj.negative_failure_message.should =~ /Expected #<(.*)> to not raise TypeError./
end
def test_throw_fail_message
obj = throw_symbol(:fail)
obj.matches?(lambda { throw :lame })
obj.failure_message.should =~ /Expected #<(.*)> to throw :fail, but :lame was thrown instead./
end
def test_throw_fail_message_when_no_symbol
obj = throw_symbol(:fail)
obj.matches?(lambda { "moop" })
obj.failure_message.should =~ /Expected #<(.*)> to throw :fail, but no symbol was thrown./
end
def test_throw_negative_fail_message
obj = throw_symbol(:fail)
obj.matches?(lambda { throw :fail })
obj.negative_failure_message.should =~ /Expected #<(.*)> to not throw :fail./
end
def test_string_argument_message
lambda {raise "message"}.should raise_error("message")
end
def test_string_argument_message_fails_no_error
lambda do
lambda { 1 }.should raise_error("message")
end.should raise_error
end
def test_string_argument_message_fails_wrong_message
lambda do
lambda { raise "other message" }.should raise_error("message")
end.should raise_error
end
def test_regexp_argument_message
lambda {raise "message"}.should raise_error(/essa/)
end
def test_regexp_argument_message_fails_no_error
lambda do
lambda { 1 }.should raise_error(/essa/)
end.should raise_error
end
def test_regexp_argument_message_fails_wrong_message
lambda do
lambda { raise "other message" }.should raise_error(/abc/)
end.should raise_error(/matching/)
end
end