rspec-expectations 2.0.0.a1 → 2.0.0.a2

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.
Files changed (48) hide show
  1. data/.document +3 -3
  2. data/.gitignore +1 -0
  3. data/License.txt +2 -2
  4. data/Rakefile +17 -10
  5. data/Upgrade.markdown +38 -0
  6. data/lib/rspec/expectations.rb +1 -0
  7. data/lib/rspec/expectations/extensions.rb +1 -0
  8. data/lib/rspec/expectations/extensions/rspec/core/example_group.rb +19 -0
  9. data/lib/rspec/expectations/version.rb +16 -0
  10. data/lib/rspec/matchers.rb +0 -2
  11. data/lib/rspec/matchers/exist.rb +2 -2
  12. data/lib/rspec/matchers/extensions/instance_exec.rb +27 -19
  13. data/lib/rspec/matchers/include.rb +17 -16
  14. data/lib/rspec/matchers/matcher.rb +71 -25
  15. data/rspec-expectations.gemspec +66 -19
  16. data/spec/rspec/matchers/be_close_spec.rb +50 -0
  17. data/spec/rspec/matchers/be_instance_of_spec.rb +36 -0
  18. data/spec/rspec/matchers/be_kind_of_spec.rb +33 -0
  19. data/spec/rspec/matchers/be_spec.rb +311 -0
  20. data/spec/rspec/matchers/change_spec.rb +349 -0
  21. data/spec/rspec/matchers/compatibility_spec.rb +28 -0
  22. data/spec/rspec/matchers/description_generation_spec.rb +160 -0
  23. data/spec/rspec/matchers/dsl_spec.rb +25 -0
  24. data/spec/rspec/matchers/eql_spec.rb +33 -0
  25. data/spec/rspec/matchers/equal_spec.rb +57 -0
  26. data/spec/rspec/matchers/exist_spec.rb +65 -0
  27. data/spec/rspec/matchers/has_spec.rb +81 -0
  28. data/spec/rspec/matchers/have_spec.rb +407 -0
  29. data/spec/rspec/matchers/include_spec.rb +88 -0
  30. data/spec/rspec/matchers/match_array_spec.rb +108 -0
  31. data/spec/rspec/matchers/match_spec.rb +57 -0
  32. data/spec/rspec/matchers/matcher_methods_spec.rb +63 -0
  33. data/spec/rspec/matchers/matcher_spec.rb +289 -0
  34. data/spec/rspec/matchers/matchers_spec.rb +2 -0
  35. data/spec/rspec/matchers/operator_matcher_spec.rb +191 -0
  36. data/spec/rspec/matchers/raise_error_spec.rb +333 -0
  37. data/spec/rspec/matchers/respond_to_spec.rb +116 -0
  38. data/spec/rspec/matchers/satisfy_spec.rb +36 -0
  39. data/spec/rspec/matchers/throw_symbol_spec.rb +96 -0
  40. data/spec/spec_helper.rb +13 -1
  41. data/spec/support/classes.rb +51 -0
  42. metadata +60 -15
  43. data/VERSION +0 -1
  44. data/VERSION.yml +0 -5
  45. data/lib/rspec/matchers/simple_matcher.rb +0 -133
  46. data/lib/rspec/matchers/wrap_expectation.rb +0 -55
  47. data/spec/rspec/expectations/wrap_expectation_spec.rb +0 -30
  48. data/spec/support/macros.rb +0 -29
@@ -0,0 +1,2 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
@@ -0,0 +1,191 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ require 'rspec/expectations/differs/default'
4
+
5
+ describe "should ==" do
6
+
7
+ it "should delegate message to target" do
8
+ subject = "apple"
9
+ subject.should_receive(:==).with("apple").and_return(true)
10
+ subject.should == "apple"
11
+ end
12
+
13
+ it "should return true on success" do
14
+ subject = "apple"
15
+ (subject.should == "apple").should be_true
16
+ end
17
+
18
+ it "should fail when target.==(actual) returns false" do
19
+ subject = "apple"
20
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected: "orange",\n got: "apple" (using ==)], "orange", "apple")
21
+ subject.should == "orange"
22
+ end
23
+
24
+ end
25
+
26
+ describe "should_not ==" do
27
+
28
+ it "should delegate message to target" do
29
+ subject = "orange"
30
+ subject.should_receive(:==).with("apple").and_return(false)
31
+ subject.should_not == "apple"
32
+ end
33
+
34
+ it "should return true on success" do
35
+ subject = "apple"
36
+ (subject.should_not == "orange").should be_false
37
+ end
38
+
39
+ it "should fail when target.==(actual) returns false" do
40
+ subject = "apple"
41
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected not: == "apple",\n got: "apple"], "apple", "apple")
42
+ subject.should_not == "apple"
43
+ end
44
+
45
+ end
46
+
47
+ describe "should ===" do
48
+
49
+ it "should delegate message to target" do
50
+ subject = "apple"
51
+ subject.should_receive(:===).with("apple").and_return(true)
52
+ subject.should === "apple"
53
+ end
54
+
55
+ it "should fail when target.===(actual) returns false" do
56
+ subject = "apple"
57
+ subject.should_receive(:===).with("orange").and_return(false)
58
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected: "orange",\n got: "apple" (using ===)], "orange", "apple")
59
+ subject.should === "orange"
60
+ end
61
+
62
+ end
63
+
64
+ describe "should_not ===" do
65
+
66
+ it "should delegate message to target" do
67
+ subject = "orange"
68
+ subject.should_receive(:===).with("apple").and_return(false)
69
+ subject.should_not === "apple"
70
+ end
71
+
72
+ it "should fail when target.===(actual) returns false" do
73
+ subject = "apple"
74
+ subject.should_receive(:===).with("apple").and_return(true)
75
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected not: === "apple",\n got: "apple"], "apple", "apple")
76
+ subject.should_not === "apple"
77
+ end
78
+
79
+ end
80
+
81
+ describe "should =~" do
82
+
83
+ it "should delegate message to target" do
84
+ subject = "foo"
85
+ subject.should_receive(:=~).with(/oo/).and_return(true)
86
+ subject.should =~ /oo/
87
+ end
88
+
89
+ it "should fail when target.=~(actual) returns false" do
90
+ subject = "fu"
91
+ subject.should_receive(:=~).with(/oo/).and_return(false)
92
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected: /oo/,\n got: "fu" (using =~)], /oo/, "fu")
93
+ subject.should =~ /oo/
94
+ end
95
+
96
+ end
97
+
98
+ describe "should_not =~" do
99
+
100
+ it "should delegate message to target" do
101
+ subject = "fu"
102
+ subject.should_receive(:=~).with(/oo/).and_return(false)
103
+ subject.should_not =~ /oo/
104
+ end
105
+
106
+ it "should fail when target.=~(actual) returns false" do
107
+ subject = "foo"
108
+ subject.should_receive(:=~).with(/oo/).and_return(true)
109
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected not: =~ /oo/,\n got: "foo"], /oo/, "foo")
110
+ subject.should_not =~ /oo/
111
+ end
112
+
113
+ end
114
+
115
+ describe "should >" do
116
+
117
+ it "should pass if > passes" do
118
+ 4.should > 3
119
+ end
120
+
121
+ it "should fail if > fails" do
122
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected: > 5,\n got: 4], 5, 4)
123
+ 4.should > 5
124
+ end
125
+
126
+ end
127
+
128
+ describe "should >=" do
129
+
130
+ it "should pass if >= passes" do
131
+ 4.should > 3
132
+ 4.should >= 4
133
+ end
134
+
135
+ it "should fail if > fails" do
136
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected: >= 5,\n got: 4], 5, 4)
137
+ 4.should >= 5
138
+ end
139
+
140
+ end
141
+
142
+ describe "should <" do
143
+
144
+ it "should pass if < passes" do
145
+ 4.should < 5
146
+ end
147
+
148
+ it "should fail if > fails" do
149
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected: < 3,\n got: 4], 3, 4)
150
+ 4.should < 3
151
+ end
152
+
153
+ end
154
+
155
+ describe "should <=" do
156
+
157
+ it "should pass if <= passes" do
158
+ 4.should <= 5
159
+ 4.should <= 4
160
+ end
161
+
162
+ it "should fail if > fails" do
163
+ Rspec::Expectations.should_receive(:fail_with).with(%[expected: <= 3,\n got: 4], 3, 4)
164
+ 4.should <= 3
165
+ end
166
+
167
+ end
168
+
169
+ describe Rspec::Matchers::PositiveOperatorMatcher do
170
+
171
+ it "should work when the target has implemented #send" do
172
+ o = Object.new
173
+ def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
174
+ lambda {
175
+ o.should == o
176
+ }.should_not raise_error
177
+ end
178
+
179
+ end
180
+
181
+ describe Rspec::Matchers::NegativeOperatorMatcher do
182
+
183
+ it "should work when the target has implemented #send" do
184
+ o = Object.new
185
+ def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
186
+ lambda {
187
+ o.should_not == :foo
188
+ }.should_not raise_error
189
+ end
190
+
191
+ end
@@ -0,0 +1,333 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "should raise_error" do
4
+ it "should pass if anything is raised" do
5
+ lambda {raise}.should raise_error
6
+ end
7
+
8
+ it "should fail if nothing is raised" do
9
+ lambda {
10
+ lambda {}.should raise_error
11
+ }.should fail_with("expected Exception but nothing was raised")
12
+ end
13
+ end
14
+
15
+ describe "should raise_error {|err| ... }" do
16
+ it "passes if there is an error" do
17
+ ran = false
18
+ lambda { non_existent_method }.should raise_error {|e|
19
+ ran = true
20
+ }
21
+ ran.should be_true
22
+ end
23
+
24
+ it "passes the error to the block" do
25
+ error = nil
26
+ lambda { non_existent_method }.should raise_error {|e|
27
+ error = e
28
+ }
29
+ error.should be_kind_of(NameError)
30
+ end
31
+ end
32
+
33
+ describe "should_not raise_error" do
34
+ it "should pass if nothing is raised" do
35
+ lambda {}.should_not raise_error
36
+ end
37
+
38
+ it "should fail if anything is raised" do
39
+ lambda {
40
+ lambda {raise}.should_not raise_error
41
+ }.should fail_with("expected no Exception, got RuntimeError")
42
+ end
43
+ end
44
+
45
+ describe "should raise_error(message)" do
46
+ it "should pass if RuntimeError is raised with the right message" do
47
+ lambda {raise 'blah'}.should raise_error('blah')
48
+ end
49
+ it "should pass if RuntimeError is raised with a matching message" do
50
+ lambda {raise 'blah'}.should raise_error(/blah/)
51
+ end
52
+ it "should pass if any other error is raised with the right message" do
53
+ lambda {raise NameError.new('blah')}.should raise_error('blah')
54
+ end
55
+ it "should fail if RuntimeError error is raised with the wrong message" do
56
+ lambda do
57
+ lambda {raise 'blarg'}.should raise_error('blah')
58
+ end.should fail_with("expected Exception with \"blah\", got #<RuntimeError: blarg>")
59
+ end
60
+ it "should fail if any other error is raised with the wrong message" do
61
+ lambda do
62
+ lambda {raise NameError.new('blarg')}.should raise_error('blah')
63
+ end.should fail_with("expected Exception with \"blah\", got #<NameError: blarg>")
64
+ end
65
+ end
66
+
67
+ describe "should_not raise_error(message)" do
68
+ it "should pass if RuntimeError error is raised with the different message" do
69
+ lambda {raise 'blarg'}.should_not raise_error('blah')
70
+ end
71
+ it "should pass if any other error is raised with the wrong message" do
72
+ lambda {raise NameError.new('blarg')}.should_not raise_error('blah')
73
+ end
74
+ it "should fail if RuntimeError is raised with message" do
75
+ lambda do
76
+ lambda {raise 'blah'}.should_not raise_error('blah')
77
+ end.should fail_with(%Q|expected no Exception with "blah", got #<RuntimeError: blah>|)
78
+ end
79
+ it "should fail if any other error is raised with message" do
80
+ lambda do
81
+ lambda {raise NameError.new('blah')}.should_not raise_error('blah')
82
+ end.should fail_with(%Q|expected no Exception with "blah", got #<NameError: blah>|)
83
+ end
84
+ end
85
+
86
+ describe "should raise_error(NamedError)" do
87
+ it "should pass if named error is raised" do
88
+ lambda { non_existent_method }.should raise_error(NameError)
89
+ end
90
+
91
+ it "should fail if nothing is raised" do
92
+ lambda {
93
+ lambda { }.should raise_error(NameError)
94
+ }.should fail_with("expected NameError but nothing was raised")
95
+ end
96
+
97
+ it "should fail if another error is raised (NameError)" do
98
+ lambda {
99
+ lambda { raise }.should raise_error(NameError)
100
+ }.should fail_with("expected NameError, got RuntimeError")
101
+ end
102
+
103
+ it "should fail if another error is raised (NameError)" do
104
+ lambda {
105
+ lambda { load "non/existent/file" }.should raise_error(NameError)
106
+ }.should fail_with(/expected NameError, got #<LoadError/)
107
+ end
108
+ end
109
+
110
+ describe "should_not raise_error(NamedError)" do
111
+ it "should pass if nothing is raised" do
112
+ lambda { }.should_not raise_error(NameError)
113
+ end
114
+
115
+ it "should pass if another error is raised" do
116
+ lambda { raise }.should_not raise_error(NameError)
117
+ end
118
+
119
+ it "should fail if named error is raised" do
120
+ lambda {
121
+ lambda { 1 + 'b' }.should_not raise_error(TypeError)
122
+ }.should fail_with(/expected no TypeError, got #<TypeError: String can't be/)
123
+ end
124
+ end
125
+
126
+ describe "should raise_error(NamedError, error_message) with String" do
127
+ it "should pass if named error is raised with same message" do
128
+ lambda { raise "example message" }.should raise_error(RuntimeError, "example message")
129
+ end
130
+
131
+ it "should fail if nothing is raised" do
132
+ lambda {
133
+ lambda {}.should raise_error(RuntimeError, "example message")
134
+ }.should fail_with("expected RuntimeError with \"example message\" but nothing was raised")
135
+ end
136
+
137
+ it "should fail if incorrect error is raised" do
138
+ lambda {
139
+ lambda { raise }.should raise_error(NameError, "example message")
140
+ }.should fail_with("expected NameError with \"example message\", got RuntimeError")
141
+ end
142
+
143
+ it "should fail if correct error is raised with incorrect message" do
144
+ lambda {
145
+ lambda { raise RuntimeError.new("not the example message") }.should raise_error(RuntimeError, "example message")
146
+ }.should fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
147
+ end
148
+ end
149
+
150
+ describe "should raise_error(NamedError, error_message) { |err| ... }" do
151
+ it "should yield exception if named error is raised with same message" do
152
+ ran = false
153
+
154
+ lambda {
155
+ raise "example message"
156
+ }.should raise_error(RuntimeError, "example message") { |err|
157
+ ran = true
158
+ err.class.should == RuntimeError
159
+ err.message.should == "example message"
160
+ }
161
+
162
+ ran.should == true
163
+ end
164
+
165
+ it "yielded block should be able to fail on it's own right" do
166
+ ran, passed = false, false
167
+
168
+ lambda {
169
+ lambda {
170
+ raise "example message"
171
+ }.should raise_error(RuntimeError, "example message") { |err|
172
+ ran = true
173
+ 5.should == 4
174
+ passed = true
175
+ }
176
+ }.should fail_with(/expected: 4/m)
177
+
178
+ ran.should == true
179
+ passed.should == false
180
+ end
181
+
182
+ it "should NOT yield exception if no error was thrown" do
183
+ ran = false
184
+
185
+ lambda {
186
+ lambda {}.should raise_error(RuntimeError, "example message") { |err|
187
+ ran = true
188
+ }
189
+ }.should fail_with("expected RuntimeError with \"example message\" but nothing was raised")
190
+
191
+ ran.should == false
192
+ end
193
+
194
+ it "should not yield exception if error class is not matched" do
195
+ ran = false
196
+
197
+ lambda {
198
+ lambda {
199
+ raise "example message"
200
+ }.should raise_error(SyntaxError, "example message") { |err|
201
+ ran = true
202
+ }
203
+ }.should fail_with("expected SyntaxError with \"example message\", got #<RuntimeError: example message>")
204
+
205
+ ran.should == false
206
+ end
207
+
208
+ it "should NOT yield exception if error message is not matched" do
209
+ ran = false
210
+
211
+ lambda {
212
+ lambda {
213
+ raise "example message"
214
+ }.should raise_error(RuntimeError, "different message") { |err|
215
+ ran = true
216
+ }
217
+ }.should fail_with("expected RuntimeError with \"different message\", got #<RuntimeError: example message>")
218
+
219
+ ran.should == false
220
+ end
221
+ end
222
+
223
+ describe "should_not raise_error(NamedError, error_message) { |err| ... }" do
224
+ it "should pass if nothing is raised" do
225
+ ran = false
226
+
227
+ lambda {}.should_not raise_error(RuntimeError, "example message") { |err|
228
+ ran = true
229
+ }
230
+
231
+ ran.should == false
232
+ end
233
+
234
+ it "should pass if a different error is raised" do
235
+ ran = false
236
+
237
+ lambda { raise }.should_not raise_error(NameError, "example message") { |err|
238
+ ran = true
239
+ }
240
+
241
+ ran.should == false
242
+ end
243
+
244
+ it "should pass if same error is raised with different message" do
245
+ ran = false
246
+
247
+ lambda {
248
+ raise RuntimeError.new("not the example message")
249
+ }.should_not raise_error(RuntimeError, "example message") { |err|
250
+ ran = true
251
+ }
252
+
253
+ ran.should == false
254
+ end
255
+
256
+ it "should fail if named error is raised with same message" do
257
+ ran = false
258
+
259
+ lambda {
260
+ lambda {
261
+ raise "example message"
262
+ }.should_not raise_error(RuntimeError, "example message") { |err|
263
+ ran = true
264
+ }
265
+ }.should fail_with("expected no RuntimeError with \"example message\", got #<RuntimeError: example message>")
266
+
267
+ ran.should == false
268
+ end
269
+ end
270
+
271
+ describe "should_not raise_error(NamedError, error_message) with String" do
272
+ it "should pass if nothing is raised" do
273
+ lambda {}.should_not raise_error(RuntimeError, "example message")
274
+ end
275
+
276
+ it "should pass if a different error is raised" do
277
+ lambda { raise }.should_not raise_error(NameError, "example message")
278
+ end
279
+
280
+ it "should pass if same error is raised with different message" do
281
+ lambda { raise RuntimeError.new("not the example message") }.should_not raise_error(RuntimeError, "example message")
282
+ end
283
+
284
+ it "should fail if named error is raised with same message" do
285
+ lambda {
286
+ lambda { raise "example message" }.should_not raise_error(RuntimeError, "example message")
287
+ }.should fail_with("expected no RuntimeError with \"example message\", got #<RuntimeError: example message>")
288
+ end
289
+ end
290
+
291
+ describe "should raise_error(NamedError, error_message) with Regexp" do
292
+ it "should pass if named error is raised with matching message" do
293
+ lambda { raise "example message" }.should raise_error(RuntimeError, /ample mess/)
294
+ end
295
+
296
+ it "should fail if nothing is raised" do
297
+ lambda {
298
+ lambda {}.should raise_error(RuntimeError, /ample mess/)
299
+ }.should fail_with("expected RuntimeError with message matching /ample mess/ but nothing was raised")
300
+ end
301
+
302
+ it "should fail if incorrect error is raised" do
303
+ lambda {
304
+ lambda { raise }.should raise_error(NameError, /ample mess/)
305
+ }.should fail_with("expected NameError with message matching /ample mess/, got RuntimeError")
306
+ end
307
+
308
+ it "should fail if correct error is raised with incorrect message" do
309
+ lambda {
310
+ lambda { raise RuntimeError.new("not the example message") }.should raise_error(RuntimeError, /less than ample mess/)
311
+ }.should fail_with("expected RuntimeError with message matching /less than ample mess/, got #<RuntimeError: not the example message>")
312
+ end
313
+ end
314
+
315
+ describe "should_not raise_error(NamedError, error_message) with Regexp" do
316
+ it "should pass if nothing is raised" do
317
+ lambda {}.should_not raise_error(RuntimeError, /ample mess/)
318
+ end
319
+
320
+ it "should pass if a different error is raised" do
321
+ lambda { raise }.should_not raise_error(NameError, /ample mess/)
322
+ end
323
+
324
+ it "should pass if same error is raised with non-matching message" do
325
+ lambda { raise RuntimeError.new("non matching message") }.should_not raise_error(RuntimeError, /ample mess/)
326
+ end
327
+
328
+ it "should fail if named error is raised with matching message" do
329
+ lambda {
330
+ lambda { raise "example message" }.should_not raise_error(RuntimeError, /ample mess/)
331
+ }.should fail_with("expected no RuntimeError with message matching /ample mess/, got #<RuntimeError: example message>")
332
+ end
333
+ end