rspec-expectations 2.0.0.a1 → 2.0.0.a2

Sign up to get free protection for your applications and to get access to all the features.
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,407 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ share_as :HaveSpecHelper do
4
+ def create_collection_owner_with(n)
5
+ owner = Rspec::Expectations::Helper::CollectionOwner.new
6
+ (1..n).each do |number|
7
+ owner.add_to_collection_with_length_method(number)
8
+ owner.add_to_collection_with_size_method(number)
9
+ end
10
+ owner
11
+ end
12
+ before(:each) do
13
+ if defined?(::ActiveSupport::Inflector)
14
+ @active_support_was_defined = true
15
+ else
16
+ @active_support_was_defined = false
17
+ module ::ActiveSupport
18
+ class Inflector
19
+ def self.pluralize(string)
20
+ string.to_s + 's'
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "should have(n).items" do
29
+ include HaveSpecHelper
30
+
31
+ it "should pass if target has a collection of items with n members" do
32
+ owner = create_collection_owner_with(3)
33
+ owner.should have(3).items_in_collection_with_length_method
34
+ owner.should have(3).items_in_collection_with_size_method
35
+ end
36
+
37
+ it "should convert :no to 0" do
38
+ owner = create_collection_owner_with(0)
39
+ owner.should have(:no).items_in_collection_with_length_method
40
+ owner.should have(:no).items_in_collection_with_size_method
41
+ end
42
+
43
+ it "should fail if target has a collection of items with < n members" do
44
+ owner = create_collection_owner_with(3)
45
+ lambda {
46
+ owner.should have(4).items_in_collection_with_length_method
47
+ }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
48
+ lambda {
49
+ owner.should have(4).items_in_collection_with_size_method
50
+ }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
51
+ end
52
+
53
+ it "should fail if target has a collection of items with > n members" do
54
+ owner = create_collection_owner_with(3)
55
+ lambda {
56
+ owner.should have(2).items_in_collection_with_length_method
57
+ }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
58
+ lambda {
59
+ owner.should have(2).items_in_collection_with_size_method
60
+ }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
61
+ end
62
+ end
63
+
64
+ describe 'should have(1).item when ActiveSupport::Inflector is defined' do
65
+ include HaveSpecHelper
66
+
67
+ it 'should pluralize the collection name' do
68
+ owner = create_collection_owner_with(1)
69
+ owner.should have(1).item
70
+ end
71
+
72
+ after(:each) do
73
+ unless @active_support_was_defined
74
+ Object.__send__ :remove_const, :ActiveSupport
75
+ end
76
+ end
77
+ end
78
+
79
+ describe 'should have(1).item when Inflector is defined' do
80
+ include HaveSpecHelper
81
+
82
+ before(:each) do
83
+ if defined?(Inflector)
84
+ @inflector_was_defined = true
85
+ else
86
+ @inflector_was_defined = false
87
+ class ::Inflector
88
+ def self.pluralize(string)
89
+ string.to_s + 's'
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ it 'should pluralize the collection name' do
96
+ owner = create_collection_owner_with(1)
97
+ owner.should have(1).item
98
+ end
99
+
100
+ after(:each) do
101
+ unless @inflector_was_defined
102
+ Object.__send__ :remove_const, :Inflector
103
+ end
104
+ end
105
+ end
106
+
107
+ describe "should have(n).items where result responds to items but returns something other than a collection" do
108
+ it "should provide a meaningful error" do
109
+ owner = Class.new do
110
+ def items
111
+ Object.new
112
+ end
113
+ end.new
114
+ lambda do
115
+ owner.should have(3).items
116
+ end.should raise_error("expected items to be a collection but it does not respond to #length or #size")
117
+ end
118
+ end
119
+
120
+ describe "should_not have(n).items" do
121
+ include HaveSpecHelper
122
+
123
+ it "should pass if target has a collection of items with < n members" do
124
+ owner = create_collection_owner_with(3)
125
+ owner.should_not have(4).items_in_collection_with_length_method
126
+ owner.should_not have(4).items_in_collection_with_size_method
127
+ end
128
+
129
+ it "should pass if target has a collection of items with > n members" do
130
+ owner = create_collection_owner_with(3)
131
+ owner.should_not have(2).items_in_collection_with_length_method
132
+ owner.should_not have(2).items_in_collection_with_size_method
133
+ end
134
+
135
+ it "should fail if target has a collection of items with n members" do
136
+ owner = create_collection_owner_with(3)
137
+ lambda {
138
+ owner.should_not have(3).items_in_collection_with_length_method
139
+ }.should fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3")
140
+ lambda {
141
+ owner.should_not have(3).items_in_collection_with_size_method
142
+ }.should fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3")
143
+ end
144
+ end
145
+
146
+ describe "should have_exactly(n).items" do
147
+ include HaveSpecHelper
148
+
149
+ it "should pass if target has a collection of items with n members" do
150
+ owner = create_collection_owner_with(3)
151
+ owner.should have_exactly(3).items_in_collection_with_length_method
152
+ owner.should have_exactly(3).items_in_collection_with_size_method
153
+ end
154
+
155
+ it "should convert :no to 0" do
156
+ owner = create_collection_owner_with(0)
157
+ owner.should have_exactly(:no).items_in_collection_with_length_method
158
+ owner.should have_exactly(:no).items_in_collection_with_size_method
159
+ end
160
+
161
+ it "should fail if target has a collection of items with < n members" do
162
+ owner = create_collection_owner_with(3)
163
+ lambda {
164
+ owner.should have_exactly(4).items_in_collection_with_length_method
165
+ }.should fail_with("expected 4 items_in_collection_with_length_method, got 3")
166
+ lambda {
167
+ owner.should have_exactly(4).items_in_collection_with_size_method
168
+ }.should fail_with("expected 4 items_in_collection_with_size_method, got 3")
169
+ end
170
+
171
+ it "should fail if target has a collection of items with > n members" do
172
+ owner = create_collection_owner_with(3)
173
+ lambda {
174
+ owner.should have_exactly(2).items_in_collection_with_length_method
175
+ }.should fail_with("expected 2 items_in_collection_with_length_method, got 3")
176
+ lambda {
177
+ owner.should have_exactly(2).items_in_collection_with_size_method
178
+ }.should fail_with("expected 2 items_in_collection_with_size_method, got 3")
179
+ end
180
+ end
181
+
182
+ describe "should have_at_least(n).items" do
183
+ include HaveSpecHelper
184
+
185
+ it "should pass if target has a collection of items with n members" do
186
+ owner = create_collection_owner_with(3)
187
+ owner.should have_at_least(3).items_in_collection_with_length_method
188
+ owner.should have_at_least(3).items_in_collection_with_size_method
189
+ end
190
+
191
+ it "should pass if target has a collection of items with > n members" do
192
+ owner = create_collection_owner_with(3)
193
+ owner.should have_at_least(2).items_in_collection_with_length_method
194
+ owner.should have_at_least(2).items_in_collection_with_size_method
195
+ end
196
+
197
+ it "should fail if target has a collection of items with < n members" do
198
+ owner = create_collection_owner_with(3)
199
+ lambda {
200
+ owner.should have_at_least(4).items_in_collection_with_length_method
201
+ }.should fail_with("expected at least 4 items_in_collection_with_length_method, got 3")
202
+ lambda {
203
+ owner.should have_at_least(4).items_in_collection_with_size_method
204
+ }.should fail_with("expected at least 4 items_in_collection_with_size_method, got 3")
205
+ end
206
+
207
+ it "should provide educational negative failure messages" do
208
+ #given
209
+ owner = create_collection_owner_with(3)
210
+ length_matcher = have_at_least(3).items_in_collection_with_length_method
211
+ size_matcher = have_at_least(3).items_in_collection_with_size_method
212
+
213
+ #when
214
+ length_matcher.matches?(owner)
215
+ size_matcher.matches?(owner)
216
+
217
+ #then
218
+ length_matcher.failure_message_for_should_not.should == <<-EOF
219
+ Isn't life confusing enough?
220
+ Instead of having to figure out the meaning of this:
221
+ should_not have_at_least(3).items_in_collection_with_length_method
222
+ We recommend that you use this instead:
223
+ should have_at_most(2).items_in_collection_with_length_method
224
+ EOF
225
+
226
+ size_matcher.failure_message_for_should_not.should == <<-EOF
227
+ Isn't life confusing enough?
228
+ Instead of having to figure out the meaning of this:
229
+ should_not have_at_least(3).items_in_collection_with_size_method
230
+ We recommend that you use this instead:
231
+ should have_at_most(2).items_in_collection_with_size_method
232
+ EOF
233
+ end
234
+ end
235
+
236
+ describe "should have_at_most(n).items" do
237
+ include HaveSpecHelper
238
+
239
+ it "should pass if target has a collection of items with n members" do
240
+ owner = create_collection_owner_with(3)
241
+ owner.should have_at_most(3).items_in_collection_with_length_method
242
+ owner.should have_at_most(3).items_in_collection_with_size_method
243
+ end
244
+
245
+ it "should fail if target has a collection of items with > n members" do
246
+ owner = create_collection_owner_with(3)
247
+ lambda {
248
+ owner.should have_at_most(2).items_in_collection_with_length_method
249
+ }.should fail_with("expected at most 2 items_in_collection_with_length_method, got 3")
250
+ lambda {
251
+ owner.should have_at_most(2).items_in_collection_with_size_method
252
+ }.should fail_with("expected at most 2 items_in_collection_with_size_method, got 3")
253
+ end
254
+
255
+ it "should pass if target has a collection of items with < n members" do
256
+ owner = create_collection_owner_with(3)
257
+ owner.should have_at_most(4).items_in_collection_with_length_method
258
+ owner.should have_at_most(4).items_in_collection_with_size_method
259
+ end
260
+
261
+ it "should provide educational negative failure messages" do
262
+ #given
263
+ owner = create_collection_owner_with(3)
264
+ length_matcher = have_at_most(3).items_in_collection_with_length_method
265
+ size_matcher = have_at_most(3).items_in_collection_with_size_method
266
+
267
+ #when
268
+ length_matcher.matches?(owner)
269
+ size_matcher.matches?(owner)
270
+
271
+ #then
272
+ length_matcher.failure_message_for_should_not.should == <<-EOF
273
+ Isn't life confusing enough?
274
+ Instead of having to figure out the meaning of this:
275
+ should_not have_at_most(3).items_in_collection_with_length_method
276
+ We recommend that you use this instead:
277
+ should have_at_least(4).items_in_collection_with_length_method
278
+ EOF
279
+
280
+ size_matcher.failure_message_for_should_not.should == <<-EOF
281
+ Isn't life confusing enough?
282
+ Instead of having to figure out the meaning of this:
283
+ should_not have_at_most(3).items_in_collection_with_size_method
284
+ We recommend that you use this instead:
285
+ should have_at_least(4).items_in_collection_with_size_method
286
+ EOF
287
+ end
288
+ end
289
+
290
+ describe "have(n).items(args, block)" do
291
+ it "should pass args to target" do
292
+ target = mock("target")
293
+ target.should_receive(:items).with("arg1","arg2").and_return([1,2,3])
294
+ target.should have(3).items("arg1","arg2")
295
+ end
296
+
297
+ it "should pass block to target" do
298
+ target = mock("target")
299
+ block = lambda { 5 }
300
+ target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
301
+ target.should have(3).items("arg1","arg2", block)
302
+ end
303
+ end
304
+
305
+ describe "have(n).items where target IS a collection" do
306
+ it "should reference the number of items IN the collection" do
307
+ [1,2,3].should have(3).items
308
+ end
309
+
310
+ it "should fail when the number of items IN the collection is not as expected" do
311
+ lambda { [1,2,3].should have(7).items }.should fail_with("expected 7 items, got 3")
312
+ end
313
+ end
314
+
315
+ describe "have(n).characters where target IS a String" do
316
+ it "should pass if the length is correct" do
317
+ "this string".should have(11).characters
318
+ end
319
+
320
+ it "should fail if the length is incorrect" do
321
+ lambda { "this string".should have(12).characters }.should fail_with("expected 12 characters, got 11")
322
+ end
323
+ end
324
+
325
+ describe "have(n).things on an object which is not a collection nor contains one" do
326
+ it "should fail" do
327
+ lambda { Object.new.should have(2).things }.should raise_error(NoMethodError, /undefined method `things' for #<Object:/)
328
+ end
329
+ end
330
+
331
+ describe Rspec::Matchers::Have, "for a collection owner that implements #send" do
332
+ include HaveSpecHelper
333
+
334
+ before(:each) do
335
+ @collection = Object.new
336
+ def @collection.floozles; [1,2] end
337
+ def @collection.send(*args); raise "DOH! Library developers shouldn't use #send!" end
338
+ end
339
+
340
+ it "should work in the straightforward case" do
341
+ lambda {
342
+ @collection.should have(2).floozles
343
+ }.should_not raise_error
344
+ end
345
+
346
+ it "should work when doing automatic pluralization" do
347
+ lambda {
348
+ @collection.should have_at_least(1).floozle
349
+ }.should_not raise_error
350
+ end
351
+
352
+ it "should blow up when the owner doesn't respond to that method" do
353
+ lambda {
354
+ @collection.should have(99).problems
355
+ }.should raise_error(NoMethodError, /problems/)
356
+ end
357
+ end
358
+
359
+ module Rspec
360
+ module Matchers
361
+ describe Have do
362
+ it "should have method_missing as private" do
363
+ with_ruby 1.8 do
364
+ described_class.private_instance_methods.should include("method_missing")
365
+ end
366
+ with_ruby 1.9 do
367
+ described_class.private_instance_methods.should include(:method_missing)
368
+ end
369
+ end
370
+
371
+ it "should not respond_to? method_missing (because it's private)" do
372
+ formatter = self.class.described_class.new({ }, StringIO.new)
373
+ formatter.should_not respond_to(:method_missing)
374
+ end
375
+
376
+ describe "respond_to?" do
377
+ before :each do
378
+ @have = Have.new(:foo)
379
+ @a_method_which_have_defines = Have.instance_methods.first
380
+ @a_method_which_object_defines = Object.instance_methods.first
381
+ end
382
+
383
+ it "should be true for a method which Have defines" do
384
+ @have.should respond_to(@a_method_which_have_defines)
385
+ end
386
+
387
+ it "should be true for a method that it's superclass (Object) defines" do
388
+ @have.should respond_to(@a_method_which_object_defines)
389
+ end
390
+
391
+ it "should be false for a method which neither Object nor nor Have defines" do
392
+ @have.should_not respond_to(:foo_bar_baz)
393
+ end
394
+
395
+ it "should be false if the owner doesn't respond to the method" do
396
+ have = Have.new(99)
397
+ have.should_not respond_to(:problems)
398
+ end
399
+
400
+ it "should be true if the owner responds to the method" do
401
+ have = Have.new(:a_symbol)
402
+ have.should respond_to(:to_sym)
403
+ end
404
+ end
405
+ end
406
+ end
407
+ end
@@ -0,0 +1,88 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "should include(expected)" do
4
+ it "should pass if target includes expected" do
5
+ [1,2,3].should include(3)
6
+ "abc".should include("a")
7
+ end
8
+
9
+ it 'should pass if target is a Hash and has the expected as a key' do
10
+ {:key => 'value'}.should include(:key)
11
+ end
12
+
13
+ it "should fail if target does not include expected" do
14
+ lambda {
15
+ [1,2,3].should include(4)
16
+ }.should fail_with("expected [1, 2, 3] to include 4")
17
+ lambda {
18
+ "abc".should include("d")
19
+ }.should fail_with("expected \"abc\" to include \"d\"")
20
+ lambda {
21
+ {:key => 'value'}.should include(:other)
22
+ }.should fail_with(%Q|expected {:key=>"value"} to include :other|)
23
+ end
24
+ end
25
+
26
+ describe "should include(with, multiple, args)" do
27
+ it "should pass if target includes all items" do
28
+ [1,2,3].should include(1,2,3)
29
+ end
30
+
31
+ it 'should pass if target is a Hash including all items as keys' do
32
+ {:key => 'value', :other => 'value'}.should include(:key, :other)
33
+ end
34
+
35
+ it "should fail if target does not include any one of the items" do
36
+ lambda {
37
+ [1,2,3].should include(1,2,4)
38
+ }.should fail_with("expected [1, 2, 3] to include 1, 2, and 4")
39
+ end
40
+
41
+ it 'should pass if target is a Hash missing any item as a key' do
42
+ lambda {
43
+ {:key => 'value'}.should include(:key, :other)
44
+ }.should fail_with(%Q|expected {:key=>"value"} to include :key and :other|)
45
+ end
46
+ end
47
+
48
+ describe "should_not include(expected)" do
49
+ it "should pass if target does not include expected" do
50
+ [1,2,3].should_not include(4)
51
+ "abc".should_not include("d")
52
+ end
53
+
54
+ it 'should pass if target is a Hash and does not have the expected as a key' do
55
+ {:other => 'value'}.should_not include(:key)
56
+ end
57
+
58
+ it "should fail if target includes expected" do
59
+ lambda {
60
+ [1,2,3].should_not include(3)
61
+ }.should fail_with("expected [1, 2, 3] not to include 3")
62
+ lambda {
63
+ "abc".should_not include("c")
64
+ }.should fail_with("expected \"abc\" not to include \"c\"")
65
+ lambda {
66
+ {:key => 'value'}.should_not include(:key)
67
+ }.should fail_with(%Q|expected {:key=>"value"} not to include :key|)
68
+ end
69
+ end
70
+
71
+ describe "should include(:key => value)" do
72
+ it "should pass if target is a Hash and includes the key/value pair" do
73
+ {:key => 'value'}.should include(:key => 'value')
74
+ end
75
+ it "should pass if target is a Hash and includes the key/value pair among others" do
76
+ {:key => 'value', :other => 'different'}.should include(:key => 'value')
77
+ end
78
+ it "should fail if target is a Hash and has a different value for key" do
79
+ lambda {
80
+ {:key => 'different'}.should include(:key => 'value')
81
+ }.should fail_with(%Q|expected {:key=>"different"} to include {:key=>"value"}|)
82
+ end
83
+ it "should fail if target is a Hash and has a different key" do
84
+ lambda {
85
+ {:other => 'value'}.should include(:key => 'value')
86
+ }.should fail_with(%Q|expected {:other=>"value"} to include {:key=>"value"}|)
87
+ end
88
+ end