rspec 1.2.9 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.rdoc +51 -1
- data/License.txt +1 -1
- data/Manifest.txt +11 -4
- data/README.rdoc +10 -24
- data/Rakefile +17 -13
- data/Upgrade.rdoc +63 -2
- data/examples/passing/shared_example_group_example.rb +0 -36
- data/examples/passing/simple_matcher_example.rb +3 -3
- data/features/formatters/nested_formatter.feature +32 -0
- data/features/interop/cucumber_stubs_dont_leak.feature +11 -0
- data/features/interop/test_but_not_test_unit.feature +1 -1
- data/features/interop/test_case_with_should_methods.feature +1 -1
- data/features/matchers/define_matcher_with_fluent_interface.feature +21 -0
- data/features/matchers/define_wrapped_matcher.feature +28 -1
- data/features/matchers/match_unless_raises.feature +60 -0
- data/features/matchers/match_unless_raises_unexpected_error.feature +39 -0
- data/features/mocks/block_local_expectations.feature +62 -0
- data/features/step_definitions/running_rspec_steps.rb +9 -0
- data/features/step_definitions/stubbing_steps.rb +16 -0
- data/features/support/env.rb +1 -0
- data/features/support/matchers/smart_match.rb +23 -4
- data/geminstaller.yml +29 -0
- data/lib/autotest/rspec.rb +14 -7
- data/lib/spec/deprecation.rb +2 -1
- data/lib/spec/dsl/main.rb +2 -1
- data/lib/spec/example/example_group_methods.rb +6 -1
- data/lib/spec/example/example_methods.rb +4 -0
- data/lib/spec/example/subject.rb +9 -3
- data/lib/spec/interop/test/unit/testsuite_adapter.rb +2 -0
- data/lib/spec/interop/test.rb +1 -1
- data/lib/spec/matchers/be.rb +167 -128
- data/lib/spec/matchers/has.rb +3 -3
- data/lib/spec/matchers/have.rb +1 -0
- data/lib/spec/matchers/matcher.rb +55 -10
- data/lib/spec/matchers/method_missing.rb +2 -2
- data/lib/spec/matchers/operator_matcher.rb +6 -1
- data/lib/spec/matchers/pretty.rb +2 -2
- data/lib/spec/matchers/raise_exception.rb +131 -0
- data/lib/spec/matchers/simple_matcher.rb +2 -1
- data/lib/spec/matchers/throw_symbol.rb +18 -22
- data/lib/spec/matchers.rb +21 -2
- data/lib/spec/mocks/message_expectation.rb +63 -48
- data/lib/spec/mocks/methods.rb +13 -8
- data/lib/spec/mocks/proxy.rb +45 -22
- data/lib/spec/runner/backtrace_tweaker.rb +3 -2
- data/lib/spec/runner/configuration.rb +8 -0
- data/lib/spec/runner/differs/default.rb +1 -1
- data/lib/spec/runner/drb_command_line.rb +8 -2
- data/lib/spec/runner/example_group_runner.rb +1 -2
- data/lib/spec/runner/formatter/nested_text_formatter.rb +6 -3
- data/lib/spec/runner/option_parser.rb +2 -0
- data/lib/spec/runner/options.rb +8 -2
- data/lib/spec/stubs/cucumber.rb +2 -2
- data/lib/spec/version.rb +2 -2
- data/spec/autotest/autotest_helper.rb +1 -1
- data/spec/autotest/discover_spec.rb +2 -2
- data/spec/autotest/failed_results_re_spec.rb +2 -2
- data/spec/autotest/rspec_spec.rb +21 -6
- data/spec/spec/dsl/main_spec.rb +10 -2
- data/spec/spec/example/example_group_methods_spec.rb +21 -1
- data/spec/spec/example/example_group_proxy_spec.rb +1 -1
- data/spec/spec/example/example_group_spec.rb +1 -12
- data/spec/spec/example/example_matcher_spec.rb +3 -4
- data/spec/spec/example/subject_spec.rb +7 -0
- data/spec/spec/expectations/wrap_expectation_spec.rb +2 -1
- data/spec/spec/interop/test/unit/spec_spec.rb +7 -7
- data/spec/spec/interop/test/unit/testcase_spec.rb +7 -7
- data/spec/spec/interop/test/unit/testsuite_adapter_spec.rb +1 -1
- data/spec/spec/matchers/be_spec.rb +159 -10
- data/spec/spec/matchers/has_spec.rb +109 -0
- data/spec/spec/matchers/have_spec.rb +278 -293
- data/spec/spec/matchers/match_array_spec.rb +8 -1
- data/spec/spec/matchers/matcher_spec.rb +70 -9
- data/spec/spec/matchers/raise_exception_spec.rb +345 -0
- data/spec/spec/matchers/simple_matcher_spec.rb +51 -44
- data/spec/spec/matchers/throw_symbol_spec.rb +83 -58
- data/spec/spec/mocks/and_yield_spec.rb +117 -0
- data/spec/spec/mocks/bug_report_496_spec.rb +2 -4
- data/spec/spec/mocks/mock_spec.rb +2 -2
- data/spec/spec/runner/command_line_spec.rb +26 -5
- data/spec/spec/runner/drb_command_line_spec.rb +39 -0
- data/spec/spec/runner/formatter/nested_text_formatter_spec.rb +35 -11
- data/spec/spec/runner/option_parser_spec.rb +12 -6
- data/spec/spec/runner/options_spec.rb +7 -0
- data/spec/spec/runner/quiet_backtrace_tweaker_spec.rb +23 -5
- data/spec/spec_helper.rb +2 -1
- metadata +107 -38
- data/lib/spec/matchers/raise_error.rb +0 -129
- data/spec/spec/matchers/matcher_methods_spec.rb +0 -63
- data/spec/spec/matchers/raise_error_spec.rb +0 -333
- /data/lib/spec/{matchers/extensions → extensions}/instance_exec.rb +0 -0
|
@@ -66,7 +66,8 @@ the extra elements were: [5, 6]
|
|
|
66
66
|
MESSAGE
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
it "should not sort items in the error message if they don't all respond to <=>" do
|
|
70
|
+
with_ruby 1.8 do
|
|
70
71
|
lambda {
|
|
71
72
|
[UnsortableObject.new(2), UnsortableObject.new(1)].should =~ [UnsortableObject.new(4), UnsortableObject.new(3)]
|
|
72
73
|
}.should fail_with(<<-MESSAGE)
|
|
@@ -76,6 +77,7 @@ the missing elements were: [4, 3]
|
|
|
76
77
|
the extra elements were: [2, 1]
|
|
77
78
|
MESSAGE
|
|
78
79
|
end
|
|
80
|
+
end
|
|
79
81
|
|
|
80
82
|
it "should accurately report extra elements when there are duplicates" do
|
|
81
83
|
lambda {
|
|
@@ -97,6 +99,11 @@ the missing elements were: [1]
|
|
|
97
99
|
MESSAGE
|
|
98
100
|
end
|
|
99
101
|
|
|
102
|
+
it "should work with subclasses of Array" do
|
|
103
|
+
class SuperArray < Array; end
|
|
104
|
+
SuperArray.new([1,2,3]).should =~ SuperArray.new([3,2,1])
|
|
105
|
+
end
|
|
106
|
+
|
|
100
107
|
end
|
|
101
108
|
|
|
102
109
|
describe "should_not =~ [:with, :multiple, :args]" do
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
|
+
class UnexpectedError < StandardError; end
|
|
4
|
+
|
|
3
5
|
module Spec
|
|
4
6
|
module Matchers
|
|
5
7
|
describe Matcher do
|
|
@@ -52,6 +54,26 @@ module Spec
|
|
|
52
54
|
matcher.actual.should == 'actual string'
|
|
53
55
|
end
|
|
54
56
|
|
|
57
|
+
context "wrapping another expectation (should == ...)" do
|
|
58
|
+
it "returns true if the wrapped expectation passes" do
|
|
59
|
+
matcher = Spec::Matchers::Matcher.new(:name, 'value') do |expected|
|
|
60
|
+
match do |actual|
|
|
61
|
+
actual.should == expected
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
matcher.matches?('value').should be_true
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "returns false if the wrapped expectation fails" do
|
|
68
|
+
matcher = Spec::Matchers::Matcher.new(:name, 'value') do |expected|
|
|
69
|
+
match do |actual|
|
|
70
|
+
actual.should == expected
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
matcher.matches?('other value').should be_false
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
55
77
|
context "with overrides" do
|
|
56
78
|
before(:each) do
|
|
57
79
|
@matcher = Spec::Matchers::Matcher.new(:be_boolean, true) do |boolean|
|
|
@@ -207,22 +229,61 @@ module Spec
|
|
|
207
229
|
matcher.matches?(8).should be_true
|
|
208
230
|
end
|
|
209
231
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
232
|
+
describe "#match_unless_raises" do
|
|
233
|
+
context "with a passing assertion" do
|
|
234
|
+
let(:mod) do
|
|
235
|
+
Module.new do
|
|
236
|
+
def assert_equal(a,b)
|
|
237
|
+
a == b ? nil : (raise UnexpectedError.new("#{a} does not equal #{b}"))
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
let(:matcher) do
|
|
242
|
+
m = mod
|
|
243
|
+
Spec::Matchers::Matcher.new :equal, 4 do |expected|
|
|
244
|
+
extend m
|
|
245
|
+
match_unless_raises UnexpectedError do
|
|
246
|
+
assert_equal expected, actual
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
it "passes as you would expect" do
|
|
251
|
+
matcher.matches?(4).should be_true
|
|
215
252
|
end
|
|
253
|
+
it "fails as you would expect" do
|
|
254
|
+
matcher.matches?(5).should be_false
|
|
255
|
+
end
|
|
256
|
+
end
|
|
216
257
|
|
|
217
|
-
|
|
218
|
-
|
|
258
|
+
context "with an unexpected error" do
|
|
259
|
+
let(:matcher) do
|
|
260
|
+
Spec::Matchers::Matcher.new :foo, :bar do |expected|
|
|
261
|
+
match_unless_raises SyntaxError do |actual|
|
|
262
|
+
raise "unexpected exception"
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
it "raises the error" do
|
|
268
|
+
expect do
|
|
269
|
+
matcher.matches?(:bar)
|
|
270
|
+
end.to raise_error("unexpected exception")
|
|
219
271
|
end
|
|
220
272
|
end
|
|
221
273
|
|
|
222
|
-
matcher.matches?("foo")
|
|
223
|
-
matcher.failure_message_for_should.should =~ /replaced/
|
|
224
274
|
end
|
|
225
275
|
|
|
276
|
+
it "can define chainable methods" do
|
|
277
|
+
matcher = Spec::Matchers::Matcher.new(:name) do
|
|
278
|
+
chain(:expecting) do |expected_value|
|
|
279
|
+
@expected_value = expected_value
|
|
280
|
+
end
|
|
281
|
+
match { |actual| actual == @expected_value }
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
matcher.expecting('value').matches?('value').should be_true
|
|
285
|
+
matcher.expecting('value').matches?('other value').should be_false
|
|
286
|
+
end
|
|
226
287
|
end
|
|
227
288
|
end
|
|
228
289
|
end
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "should raise_exception" do
|
|
4
|
+
it "should pass if anything is raised" do
|
|
5
|
+
lambda {raise}.should raise_exception
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should fail if nothing is raised" do
|
|
9
|
+
lambda {
|
|
10
|
+
lambda {}.should raise_exception
|
|
11
|
+
}.should fail_with("expected Exception but nothing was raised")
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
describe "should raise_error" do
|
|
16
|
+
it "should pass if anything is raised" do
|
|
17
|
+
lambda {raise}.should raise_error
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "should fail if nothing is raised" do
|
|
21
|
+
lambda {
|
|
22
|
+
lambda {}.should raise_error
|
|
23
|
+
}.should fail_with("expected Exception but nothing was raised")
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe "should raise_exception {|e| ... }" do
|
|
28
|
+
it "passes if there is an exception" do
|
|
29
|
+
ran = false
|
|
30
|
+
lambda { non_existent_method }.should raise_exception {|e|
|
|
31
|
+
ran = true
|
|
32
|
+
}
|
|
33
|
+
ran.should be_true
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "passes the exception to the block" do
|
|
37
|
+
exception = nil
|
|
38
|
+
lambda { non_existent_method }.should raise_exception {|e|
|
|
39
|
+
exception = e
|
|
40
|
+
}
|
|
41
|
+
exception.should be_kind_of(NameError)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
describe "should_not raise_exception" do
|
|
46
|
+
it "should pass if nothing is raised" do
|
|
47
|
+
lambda {}.should_not raise_exception
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "should fail if anything is raised" do
|
|
51
|
+
lambda {
|
|
52
|
+
lambda {raise}.should_not raise_exception
|
|
53
|
+
}.should fail_with("expected no Exception, got RuntimeError")
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe "should raise_exception(message)" do
|
|
58
|
+
it "should pass if RuntimeError is raised with the right message" do
|
|
59
|
+
lambda {raise 'blah'}.should raise_exception('blah')
|
|
60
|
+
end
|
|
61
|
+
it "should pass if RuntimeError is raised with a matching message" do
|
|
62
|
+
lambda {raise 'blah'}.should raise_exception(/blah/)
|
|
63
|
+
end
|
|
64
|
+
it "should pass if any other exception is raised with the right message" do
|
|
65
|
+
lambda {raise NameError.new('blah')}.should raise_exception('blah')
|
|
66
|
+
end
|
|
67
|
+
it "should fail if RuntimeError exception is raised with the wrong message" do
|
|
68
|
+
lambda do
|
|
69
|
+
lambda {raise 'blarg'}.should raise_exception('blah')
|
|
70
|
+
end.should fail_with("expected Exception with \"blah\", got #<RuntimeError: blarg>")
|
|
71
|
+
end
|
|
72
|
+
it "should fail if any other exception is raised with the wrong message" do
|
|
73
|
+
lambda do
|
|
74
|
+
lambda {raise NameError.new('blarg')}.should raise_exception('blah')
|
|
75
|
+
end.should fail_with("expected Exception with \"blah\", got #<NameError: blarg>")
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
describe "should_not raise_exception(message)" do
|
|
80
|
+
it "should pass if RuntimeError exception is raised with the different message" do
|
|
81
|
+
lambda {raise 'blarg'}.should_not raise_exception('blah')
|
|
82
|
+
end
|
|
83
|
+
it "should pass if any other exception is raised with the wrong message" do
|
|
84
|
+
lambda {raise NameError.new('blarg')}.should_not raise_exception('blah')
|
|
85
|
+
end
|
|
86
|
+
it "should fail if RuntimeError is raised with message" do
|
|
87
|
+
lambda do
|
|
88
|
+
lambda {raise 'blah'}.should_not raise_exception('blah')
|
|
89
|
+
end.should fail_with(%Q|expected no Exception with "blah", got #<RuntimeError: blah>|)
|
|
90
|
+
end
|
|
91
|
+
it "should fail if any other exception is raised with message" do
|
|
92
|
+
lambda do
|
|
93
|
+
lambda {raise NameError.new('blah')}.should_not raise_exception('blah')
|
|
94
|
+
end.should fail_with(%Q|expected no Exception with "blah", got #<NameError: blah>|)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
describe "should raise_exception(NamedError)" do
|
|
99
|
+
it "should pass if named exception is raised" do
|
|
100
|
+
lambda { non_existent_method }.should raise_exception(NameError)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it "should fail if nothing is raised" do
|
|
104
|
+
lambda {
|
|
105
|
+
lambda { }.should raise_exception(NameError)
|
|
106
|
+
}.should fail_with("expected NameError but nothing was raised")
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "should fail if another exception is raised (NameError)" do
|
|
110
|
+
lambda {
|
|
111
|
+
lambda { raise }.should raise_exception(NameError)
|
|
112
|
+
}.should fail_with("expected NameError, got RuntimeError")
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it "should fail if another exception is raised (NameError)" do
|
|
116
|
+
lambda {
|
|
117
|
+
lambda { load "non/existent/file" }.should raise_exception(NameError)
|
|
118
|
+
}.should fail_with(/expected NameError, got #<LoadError/)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
describe "should_not raise_exception(NamedError)" do
|
|
123
|
+
it "should pass if nothing is raised" do
|
|
124
|
+
lambda { }.should_not raise_exception(NameError)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
it "should pass if another exception is raised" do
|
|
128
|
+
lambda { raise }.should_not raise_exception(NameError)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
it "should fail if named exception is raised" do
|
|
132
|
+
lambda {
|
|
133
|
+
lambda { 1 + 'b' }.should_not raise_exception(TypeError)
|
|
134
|
+
}.should fail_with(/expected no TypeError, got #<TypeError: String can't be/)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
describe "should raise_exception(NamedError, exception_message) with String" do
|
|
139
|
+
it "should pass if named exception is raised with same message" do
|
|
140
|
+
lambda { raise "example message" }.should raise_exception(RuntimeError, "example message")
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it "should fail if nothing is raised" do
|
|
144
|
+
lambda {
|
|
145
|
+
lambda {}.should raise_exception(RuntimeError, "example message")
|
|
146
|
+
}.should fail_with("expected RuntimeError with \"example message\" but nothing was raised")
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it "should fail if incorrect exception is raised" do
|
|
150
|
+
lambda {
|
|
151
|
+
lambda { raise }.should raise_exception(NameError, "example message")
|
|
152
|
+
}.should fail_with("expected NameError with \"example message\", got RuntimeError")
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
it "should fail if correct exception is raised with incorrect message" do
|
|
156
|
+
lambda {
|
|
157
|
+
lambda { raise RuntimeError.new("not the example message") }.should raise_exception(RuntimeError, "example message")
|
|
158
|
+
}.should fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
describe "should raise_exception(NamedError, exception_message) { |err| ... }" do
|
|
163
|
+
it "should yield exception if named exception is raised with same message" do
|
|
164
|
+
ran = false
|
|
165
|
+
|
|
166
|
+
lambda {
|
|
167
|
+
raise "example message"
|
|
168
|
+
}.should raise_exception(RuntimeError, "example message") { |err|
|
|
169
|
+
ran = true
|
|
170
|
+
err.class.should == RuntimeError
|
|
171
|
+
err.message.should == "example message"
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
ran.should == true
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
it "yielded block should be able to fail on it's own right" do
|
|
178
|
+
ran, passed = false, false
|
|
179
|
+
|
|
180
|
+
lambda {
|
|
181
|
+
lambda {
|
|
182
|
+
raise "example message"
|
|
183
|
+
}.should raise_exception(RuntimeError, "example message") { |err|
|
|
184
|
+
ran = true
|
|
185
|
+
5.should == 4
|
|
186
|
+
passed = true
|
|
187
|
+
}
|
|
188
|
+
}.should fail_with(/expected: 4/m)
|
|
189
|
+
|
|
190
|
+
ran.should == true
|
|
191
|
+
passed.should == false
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it "should NOT yield exception if no exception was thrown" do
|
|
195
|
+
ran = false
|
|
196
|
+
|
|
197
|
+
lambda {
|
|
198
|
+
lambda {}.should raise_exception(RuntimeError, "example message") { |err|
|
|
199
|
+
ran = true
|
|
200
|
+
}
|
|
201
|
+
}.should fail_with("expected RuntimeError with \"example message\" but nothing was raised")
|
|
202
|
+
|
|
203
|
+
ran.should == false
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
it "should not yield exception if exception class is not matched" do
|
|
207
|
+
ran = false
|
|
208
|
+
|
|
209
|
+
lambda {
|
|
210
|
+
lambda {
|
|
211
|
+
raise "example message"
|
|
212
|
+
}.should raise_exception(SyntaxError, "example message") { |err|
|
|
213
|
+
ran = true
|
|
214
|
+
}
|
|
215
|
+
}.should fail_with("expected SyntaxError with \"example message\", got #<RuntimeError: example message>")
|
|
216
|
+
|
|
217
|
+
ran.should == false
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
it "should NOT yield exception if exception message is not matched" do
|
|
221
|
+
ran = false
|
|
222
|
+
|
|
223
|
+
lambda {
|
|
224
|
+
lambda {
|
|
225
|
+
raise "example message"
|
|
226
|
+
}.should raise_exception(RuntimeError, "different message") { |err|
|
|
227
|
+
ran = true
|
|
228
|
+
}
|
|
229
|
+
}.should fail_with("expected RuntimeError with \"different message\", got #<RuntimeError: example message>")
|
|
230
|
+
|
|
231
|
+
ran.should == false
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
describe "should_not raise_exception(NamedError, exception_message) { |err| ... }" do
|
|
236
|
+
it "should pass if nothing is raised" do
|
|
237
|
+
ran = false
|
|
238
|
+
|
|
239
|
+
lambda {}.should_not raise_exception(RuntimeError, "example message") { |err|
|
|
240
|
+
ran = true
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
ran.should == false
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
it "should pass if a different exception is raised" do
|
|
247
|
+
ran = false
|
|
248
|
+
|
|
249
|
+
lambda { raise }.should_not raise_exception(NameError, "example message") { |err|
|
|
250
|
+
ran = true
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
ran.should == false
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
it "should pass if same exception is raised with different message" do
|
|
257
|
+
ran = false
|
|
258
|
+
|
|
259
|
+
lambda {
|
|
260
|
+
raise RuntimeError.new("not the example message")
|
|
261
|
+
}.should_not raise_exception(RuntimeError, "example message") { |err|
|
|
262
|
+
ran = true
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
ran.should == false
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
it "should fail if named exception is raised with same message" do
|
|
269
|
+
ran = false
|
|
270
|
+
|
|
271
|
+
lambda {
|
|
272
|
+
lambda {
|
|
273
|
+
raise "example message"
|
|
274
|
+
}.should_not raise_exception(RuntimeError, "example message") { |err|
|
|
275
|
+
ran = true
|
|
276
|
+
}
|
|
277
|
+
}.should fail_with("expected no RuntimeError with \"example message\", got #<RuntimeError: example message>")
|
|
278
|
+
|
|
279
|
+
ran.should == false
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
describe "should_not raise_exception(NamedError, exception_message) with String" do
|
|
284
|
+
it "should pass if nothing is raised" do
|
|
285
|
+
lambda {}.should_not raise_exception(RuntimeError, "example message")
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
it "should pass if a different exception is raised" do
|
|
289
|
+
lambda { raise }.should_not raise_exception(NameError, "example message")
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
it "should pass if same exception is raised with different message" do
|
|
293
|
+
lambda { raise RuntimeError.new("not the example message") }.should_not raise_exception(RuntimeError, "example message")
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
it "should fail if named exception is raised with same message" do
|
|
297
|
+
lambda {
|
|
298
|
+
lambda { raise "example message" }.should_not raise_exception(RuntimeError, "example message")
|
|
299
|
+
}.should fail_with("expected no RuntimeError with \"example message\", got #<RuntimeError: example message>")
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
describe "should raise_exception(NamedError, exception_message) with Regexp" do
|
|
304
|
+
it "should pass if named exception is raised with matching message" do
|
|
305
|
+
lambda { raise "example message" }.should raise_exception(RuntimeError, /ample mess/)
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
it "should fail if nothing is raised" do
|
|
309
|
+
lambda {
|
|
310
|
+
lambda {}.should raise_exception(RuntimeError, /ample mess/)
|
|
311
|
+
}.should fail_with("expected RuntimeError with message matching /ample mess/ but nothing was raised")
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
it "should fail if incorrect exception is raised" do
|
|
315
|
+
lambda {
|
|
316
|
+
lambda { raise }.should raise_exception(NameError, /ample mess/)
|
|
317
|
+
}.should fail_with("expected NameError with message matching /ample mess/, got RuntimeError")
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
it "should fail if correct exception is raised with incorrect message" do
|
|
321
|
+
lambda {
|
|
322
|
+
lambda { raise RuntimeError.new("not the example message") }.should raise_exception(RuntimeError, /less than ample mess/)
|
|
323
|
+
}.should fail_with("expected RuntimeError with message matching /less than ample mess/, got #<RuntimeError: not the example message>")
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
describe "should_not raise_exception(NamedError, exception_message) with Regexp" do
|
|
328
|
+
it "should pass if nothing is raised" do
|
|
329
|
+
lambda {}.should_not raise_exception(RuntimeError, /ample mess/)
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
it "should pass if a different exception is raised" do
|
|
333
|
+
lambda { raise }.should_not raise_exception(NameError, /ample mess/)
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
it "should pass if same exception is raised with non-matching message" do
|
|
337
|
+
lambda { raise RuntimeError.new("non matching message") }.should_not raise_exception(RuntimeError, /ample mess/)
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
it "should fail if named exception is raised with matching message" do
|
|
341
|
+
lambda {
|
|
342
|
+
lambda { raise "example message" }.should_not raise_exception(RuntimeError, /ample mess/)
|
|
343
|
+
}.should fail_with("expected no RuntimeError with message matching /ample mess/, got #<RuntimeError: example message>")
|
|
344
|
+
end
|
|
345
|
+
end
|
|
@@ -3,6 +3,13 @@ require 'spec_helper'
|
|
|
3
3
|
module Spec
|
|
4
4
|
module Matchers
|
|
5
5
|
describe SimpleMatcher do
|
|
6
|
+
before { Spec.stub(:deprecate) }
|
|
7
|
+
|
|
8
|
+
it "is deprecated" do
|
|
9
|
+
Spec.should_receive(:deprecate)
|
|
10
|
+
simple_matcher("foo") {}
|
|
11
|
+
end
|
|
12
|
+
|
|
6
13
|
it "should pass match arg to block" do
|
|
7
14
|
actual = nil
|
|
8
15
|
matcher = simple_matcher("message") do |given| actual = given end
|
|
@@ -35,59 +42,59 @@ module Spec
|
|
|
35
42
|
matcher.matches?("anything").should be_true
|
|
36
43
|
end.should fail_with(/expected: 3/)
|
|
37
44
|
end
|
|
38
|
-
end
|
|
39
45
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
describe "with arity of 2" do
|
|
47
|
+
it "should provide the matcher so you can access its messages" do
|
|
48
|
+
provided_matcher = nil
|
|
49
|
+
matcher = simple_matcher("thing") do |given, matcher|
|
|
50
|
+
provided_matcher = matcher
|
|
51
|
+
end
|
|
52
|
+
matcher.matches?("anything")
|
|
53
|
+
provided_matcher.should equal(matcher)
|
|
45
54
|
end
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
matcher.failure_message
|
|
55
|
+
|
|
56
|
+
it "should support a custom failure message" do
|
|
57
|
+
matcher = simple_matcher("thing") do |given, matcher|
|
|
58
|
+
matcher.failure_message = "custom message"
|
|
59
|
+
end
|
|
60
|
+
matcher.matches?("other")
|
|
61
|
+
matcher.failure_message.should == "custom message"
|
|
53
62
|
end
|
|
54
|
-
matcher.matches?("other")
|
|
55
|
-
matcher.failure_message.should == "custom message"
|
|
56
|
-
end
|
|
57
63
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
it "should complain when asked for a failure message if you don't give it a description or a message" do
|
|
65
|
+
matcher = simple_matcher do |given, matcher| end
|
|
66
|
+
matcher.matches?("other")
|
|
67
|
+
matcher.failure_message.should =~ /No description provided/
|
|
68
|
+
end
|
|
63
69
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
it "should support a custom negative failure message" do
|
|
71
|
+
matcher = simple_matcher("thing") do |given, matcher|
|
|
72
|
+
matcher.negative_failure_message = "custom message"
|
|
73
|
+
end
|
|
74
|
+
matcher.matches?("other")
|
|
75
|
+
matcher.negative_failure_message.should == "custom message"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "should complain when asked for a negative failure message if you don't give it a description or a message" do
|
|
79
|
+
matcher = simple_matcher do |given, matcher| end
|
|
80
|
+
matcher.matches?("other")
|
|
81
|
+
matcher.negative_failure_message.should =~ /No description provided/
|
|
67
82
|
end
|
|
68
|
-
matcher.matches?("other")
|
|
69
|
-
matcher.negative_failure_message.should == "custom message"
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
it "should complain when asked for a negative failure message if you don't give it a description or a message" do
|
|
73
|
-
matcher = simple_matcher do |given, matcher| end
|
|
74
|
-
matcher.matches?("other")
|
|
75
|
-
matcher.negative_failure_message.should =~ /No description provided/
|
|
76
|
-
end
|
|
77
83
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
it "should support a custom description" do
|
|
85
|
+
matcher = simple_matcher("thing") do |given, matcher|
|
|
86
|
+
matcher.description = "custom message"
|
|
87
|
+
end
|
|
88
|
+
matcher.matches?("description")
|
|
89
|
+
matcher.description.should == "custom message"
|
|
81
90
|
end
|
|
82
|
-
matcher.matches?("description")
|
|
83
|
-
matcher.description.should == "custom message"
|
|
84
|
-
end
|
|
85
91
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
92
|
+
it "should tell you no description was provided when it doesn't receive one" do
|
|
93
|
+
matcher = simple_matcher do end
|
|
94
|
+
matcher.description.should =~ /No description provided/
|
|
95
|
+
end
|
|
89
96
|
end
|
|
97
|
+
|
|
90
98
|
end
|
|
91
|
-
|
|
92
99
|
end
|
|
93
|
-
end
|
|
100
|
+
end
|