rspec-expectations 2.12.1 → 2.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. data/Changelog.md +31 -0
  2. data/README.md +1 -1
  3. data/features/built_in_matchers/be.feature +6 -4
  4. data/features/built_in_matchers/be_within.feature +3 -1
  5. data/features/built_in_matchers/cover.feature +2 -0
  6. data/features/built_in_matchers/end_with.feature +2 -0
  7. data/features/built_in_matchers/equality.feature +9 -15
  8. data/features/built_in_matchers/exist.feature +2 -0
  9. data/features/built_in_matchers/expect_error.feature +14 -8
  10. data/features/built_in_matchers/have.feature +11 -5
  11. data/features/built_in_matchers/include.feature +53 -0
  12. data/features/built_in_matchers/match.feature +2 -0
  13. data/features/built_in_matchers/operators.feature +17 -11
  14. data/features/built_in_matchers/predicates.feature +21 -13
  15. data/features/built_in_matchers/respond_to.feature +7 -1
  16. data/features/built_in_matchers/satisfy.feature +2 -0
  17. data/features/built_in_matchers/start_with.feature +2 -0
  18. data/features/built_in_matchers/throw_symbol.feature +6 -0
  19. data/features/built_in_matchers/types.feature +8 -6
  20. data/lib/rspec/expectations/deprecation.rb +1 -1
  21. data/lib/rspec/expectations/differ.rb +8 -8
  22. data/lib/rspec/expectations/fail_with.rb +17 -3
  23. data/lib/rspec/expectations/syntax.rb +46 -0
  24. data/lib/rspec/expectations/version.rb +1 -1
  25. data/lib/rspec/matchers/built_in/be.rb +7 -3
  26. data/lib/rspec/matchers/built_in/be_within.rb +13 -4
  27. data/lib/rspec/matchers/built_in/change.rb +2 -2
  28. data/lib/rspec/matchers/built_in/equal.rb +5 -1
  29. data/lib/rspec/matchers/built_in/exist.rb +1 -1
  30. data/lib/rspec/matchers/built_in/have.rb +8 -8
  31. data/lib/rspec/matchers/built_in/include.rb +19 -3
  32. data/lib/rspec/matchers/built_in/respond_to.rb +1 -1
  33. data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +1 -1
  34. data/lib/rspec/matchers/matcher.rb +4 -3
  35. data/lib/rspec/matchers/operator_matcher.rb +1 -1
  36. data/lib/rspec/matchers/pretty.rb +5 -1
  37. data/spec/rspec/expectations/differ_spec.rb +8 -15
  38. data/spec/rspec/expectations/expectation_target_spec.rb +18 -8
  39. data/spec/rspec/expectations/extensions/kernel_spec.rb +15 -15
  40. data/spec/rspec/expectations/fail_with_spec.rb +41 -16
  41. data/spec/rspec/expectations/handler_spec.rb +13 -13
  42. data/spec/rspec/expectations/syntax_spec.rb +70 -8
  43. data/spec/rspec/matchers/base_matcher_spec.rb +14 -12
  44. data/spec/rspec/matchers/be_close_spec.rb +1 -1
  45. data/spec/rspec/matchers/be_instance_of_spec.rb +14 -8
  46. data/spec/rspec/matchers/be_kind_of_spec.rb +12 -8
  47. data/spec/rspec/matchers/be_spec.rb +212 -148
  48. data/spec/rspec/matchers/be_within_spec.rb +91 -42
  49. data/spec/rspec/matchers/change_spec.rb +52 -38
  50. data/spec/rspec/matchers/configuration_spec.rb +19 -15
  51. data/spec/rspec/matchers/cover_spec.rb +19 -19
  52. data/spec/rspec/matchers/description_generation_spec.rb +86 -86
  53. data/spec/rspec/matchers/dsl_spec.rb +7 -7
  54. data/spec/rspec/matchers/eq_spec.rb +17 -11
  55. data/spec/rspec/matchers/eql_spec.rb +10 -10
  56. data/spec/rspec/matchers/equal_spec.rb +27 -9
  57. data/spec/rspec/matchers/exist_spec.rb +35 -21
  58. data/spec/rspec/matchers/has_spec.rb +33 -29
  59. data/spec/rspec/matchers/have_spec.rb +165 -151
  60. data/spec/rspec/matchers/include_matcher_integration_spec.rb +30 -0
  61. data/spec/rspec/matchers/include_spec.rb +282 -124
  62. data/spec/rspec/matchers/match_array_spec.rb +90 -49
  63. data/spec/rspec/matchers/match_spec.rb +21 -21
  64. data/spec/rspec/matchers/matcher_spec.rb +85 -48
  65. data/spec/rspec/matchers/matchers_spec.rb +12 -6
  66. data/spec/rspec/matchers/method_missing_spec.rb +5 -1
  67. data/spec/rspec/matchers/operator_matcher_spec.rb +216 -237
  68. data/spec/rspec/matchers/raise_error_spec.rb +132 -132
  69. data/spec/rspec/matchers/respond_to_spec.rb +109 -112
  70. data/spec/rspec/matchers/satisfy_spec.rb +16 -16
  71. data/spec/rspec/matchers/start_with_end_with_spec.rb +36 -32
  72. data/spec/rspec/matchers/throw_symbol_spec.rb +24 -24
  73. data/spec/rspec/matchers/yield_spec.rb +7 -7
  74. data/spec/spec_helper.rb +46 -19
  75. data/spec/support/in_sub_process.rb +27 -20
  76. metadata +81 -83
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ describe "include() interaction with built-in matchers" do
6
+ it "works with be_within(delta).of(expected)" do
7
+ expect([10, 20, 30]).to include( be_within(5).of(24) )
8
+ expect([10, 20, 30]).not_to include( be_within(3).of(24) )
9
+ end
10
+
11
+ it "works with be_instance_of(klass)" do
12
+ expect(["foo", 123, {:foo => "bar"}]).to include( be_instance_of(Hash) )
13
+ expect(["foo", 123, {:foo => "bar"}]).not_to include( be_instance_of(Range) )
14
+ end
15
+
16
+ it "works with be_kind_of(klass)" do
17
+ class StringSubclass < String; end
18
+ class NotHashSubclass; end
19
+
20
+ expect([StringSubclass.new("baz")]).to include( be_kind_of(String) )
21
+ expect([NotHashSubclass.new]).not_to include( be_kind_of(Hash) )
22
+ end
23
+
24
+ it "works with be_[some predicate]" do
25
+ expect([stub("actual", :happy? => true)]).to include( be_happy )
26
+ expect([stub("actual", :happy? => false)]).not_to include( be_happy )
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,373 +1,531 @@
1
1
  require 'spec_helper'
2
+ require 'uri'
2
3
 
3
4
  describe "#include matcher" do
4
5
  it "is diffable" do
5
- include("a").should be_diffable
6
+ expect(include("a")).to be_diffable
6
7
  end
7
8
 
8
- describe "should include(with_one_arg)" do
9
+ describe "expect(...).to include(with_one_arg)" do
9
10
  it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do
10
11
  let(:matcher) { include(2) }
11
12
  end
12
13
 
13
14
  context "for a string target" do
14
15
  it "passes if target includes expected" do
15
- "abc".should include("a")
16
+ expect("abc").to include("a")
16
17
  end
17
18
 
18
19
  it "fails if target does not include expected" do
19
- lambda {
20
- "abc".should include("d")
21
- }.should fail_matching("expected \"abc\" to include \"d\"")
20
+ expect {
21
+ expect("abc").to include("d")
22
+ }.to fail_matching("expected \"abc\" to include \"d\"")
22
23
  end
23
24
 
24
25
  it "includes a diff when actual is multiline" do
25
- lambda {
26
- "abc\ndef".should include("g")
27
- }.should fail_matching("expected \"abc\\ndef\" to include \"g\"\nDiff")
26
+ expect {
27
+ expect("abc\ndef").to include("g")
28
+ }.to fail_matching("expected \"abc\\ndef\" to include \"g\"\nDiff")
28
29
  end
29
30
 
30
31
  it "includes a diff when actual is multiline and there are multiple expecteds" do
31
- lambda {
32
- "abc\ndef".should include("g", "h")
33
- }.should fail_matching("expected \"abc\\ndef\" to include \"g\" and \"h\"\nDiff")
32
+ expect {
33
+ expect("abc\ndef").to include("g", "h")
34
+ }.to fail_matching("expected \"abc\\ndef\" to include \"g\" and \"h\"\nDiff")
34
35
  end
35
36
  end
36
37
 
37
38
  context "for an array target" do
38
39
  it "passes if target includes expected" do
39
- [1,2,3].should include(3)
40
+ expect([1,2,3]).to include(3)
40
41
  end
41
42
 
42
43
  it "fails if target does not include expected" do
43
- lambda {
44
- [1,2,3].should include(4)
45
- }.should fail_matching("expected [1, 2, 3] to include 4")
44
+ expect {
45
+ expect([1,2,3]).to include(4)
46
+ }.to fail_matching("expected [1, 2, 3] to include 4")
47
+ end
48
+
49
+ it 'fails when given differing null doubles' do
50
+ dbl_1 = double.as_null_object
51
+ dbl_2 = double.as_null_object
52
+
53
+ expect {
54
+ expect([dbl_1]).to include(dbl_2)
55
+ }.to fail_matching("expected [#{dbl_1.inspect}] to include")
56
+ end
57
+
58
+ it 'passes when given the same null double' do
59
+ dbl = double.as_null_object
60
+ expect([dbl]).to include(dbl)
46
61
  end
47
62
  end
48
63
 
49
64
  context "for a hash target" do
50
65
  it 'passes if target has the expected as a key' do
51
- {:key => 'value'}.should include(:key)
66
+ expect({:key => 'value'}).to include(:key)
52
67
  end
53
68
 
54
69
  it "fails if target does not include expected" do
55
- lambda {
56
- {:key => 'value'}.should include(:other)
57
- }.should fail_matching(%Q|expected {:key=>"value"} to include :other|)
70
+ expect {
71
+ expect({:key => 'value'}).to include(:other)
72
+ }.to fail_matching(%Q|expected {:key=>"value"} to include :other|)
58
73
  end
59
74
 
60
75
  it "fails if target doesn't have a key and we expect nil" do
61
- lambda {
62
- {}.should include(:something => nil)
63
- }.should fail_matching(%Q|expected {} to include {:something=>nil}|)
76
+ expect {
77
+ expect({}).to include(:something => nil)
78
+ }.to fail_matching(%Q|expected {} to include {:something=>nil}|)
79
+ end
80
+
81
+ it 'works even when an entry in the hash overrides #send' do
82
+ hash = { :key => 'value' }
83
+ def hash.send; :sent; end
84
+ expect(hash).to include(hash)
85
+ end
86
+
87
+ context 'that overrides #send' do
88
+ it 'still works' do
89
+ array = [1, 2]
90
+ def array.send; :sent; end
91
+
92
+ expect(array).to include(*array)
93
+ end
64
94
  end
65
95
  end
66
96
  end
67
97
 
68
- describe "should include(with, multiple, args)" do
98
+ describe "expect(...).to include(with, multiple, args)" do
69
99
  it "has a description" do
70
100
  matcher = include("a")
71
- matcher.description.should eq("include \"a\"")
101
+ expect(matcher.description).to eq("include \"a\"")
72
102
  end
73
103
  context "for a string target" do
74
104
  it "passes if target includes all items" do
75
- "a string".should include("str", "a")
105
+ expect("a string").to include("str", "a")
76
106
  end
77
107
 
78
108
  it "fails if target does not include any one of the items" do
79
- lambda {
80
- "a string".should include("str", "a", "foo")
81
- }.should fail_matching(%Q{expected "a string" to include "str", "a", and "foo"})
109
+ expect {
110
+ expect("a string").to include("str", "a", "foo")
111
+ }.to fail_matching(%Q{expected "a string" to include "str", "a", and "foo"})
82
112
  end
83
113
  end
84
114
 
85
115
  context "for an array target" do
86
116
  it "passes if target includes all items" do
87
- [1,2,3].should include(1,2,3)
117
+ expect([1,2,3]).to include(1,2,3)
88
118
  end
89
119
 
90
120
  it "fails if target does not include any one of the items" do
91
- lambda {
92
- [1,2,3].should include(1,2,4)
93
- }.should fail_matching("expected [1, 2, 3] to include 1, 2, and 4")
121
+ expect {
122
+ expect([1,2,3]).to include(1,2,4)
123
+ }.to fail_matching("expected [1, 2, 3] to include 1, 2, and 4")
94
124
  end
95
125
  end
96
126
 
97
127
  context "for a hash target" do
98
128
  it 'passes if target includes all items as keys' do
99
- {:key => 'value', :other => 'value'}.should include(:key, :other)
129
+ expect({:key => 'value', :other => 'value'}).to include(:key, :other)
100
130
  end
101
131
 
102
132
  it 'fails if target is missing any item as a key' do
103
- lambda {
104
- {:key => 'value'}.should include(:key, :other)
105
- }.should fail_matching(%Q|expected {:key=>"value"} to include :key and :other|)
133
+ expect {
134
+ expect({:key => 'value'}).to include(:key, :other)
135
+ }.to fail_matching(%Q|expected {:key=>"value"} to include :key and :other|)
106
136
  end
107
137
  end
108
138
  end
109
139
 
110
- describe "should_not include(expected)" do
140
+ describe "expect(...).to_not include(expected)" do
111
141
  context "for a string target" do
112
142
  it "passes if target does not include expected" do
113
- "abc".should_not include("d")
143
+ expect("abc").not_to include("d")
114
144
  end
115
145
 
116
146
  it "fails if target includes expected" do
117
- lambda {
118
- "abc".should_not include("c")
119
- }.should fail_with("expected \"abc\" not to include \"c\"")
147
+ expect {
148
+ expect("abc").not_to include("c")
149
+ }.to fail_with("expected \"abc\" not to include \"c\"")
120
150
  end
121
151
  end
122
152
 
123
153
  context "for an array target" do
124
154
  it "passes if target does not include expected" do
125
- [1,2,3].should_not include(4)
155
+ expect([1,2,3]).not_to include(4)
126
156
  end
127
157
 
128
158
  it "fails if target includes expected" do
129
- lambda {
130
- [1,2,3].should_not include(3)
131
- }.should fail_with("expected [1, 2, 3] not to include 3")
159
+ expect {
160
+ expect([1,2,3]).not_to include(3)
161
+ }.to fail_with("expected [1, 2, 3] not to include 3")
162
+ end
163
+
164
+ it 'passes when given differing null doubles' do
165
+ expect([double.as_null_object]).not_to include(double.as_null_object)
166
+ end
167
+
168
+ it 'fails when given the same null double' do
169
+ dbl = double.as_null_object
170
+
171
+ expect {
172
+ expect([dbl]).not_to include(dbl)
173
+ }.to fail_matching("expected [#{dbl.inspect}] not to include")
132
174
  end
133
175
  end
134
176
 
135
177
  context "for a hash target" do
136
178
  it 'passes if target does not have the expected as a key' do
137
- {:other => 'value'}.should_not include(:key)
179
+ expect({:other => 'value'}).not_to include(:key)
138
180
  end
139
181
 
140
182
  it "fails if target includes expected key" do
141
- lambda {
142
- {:key => 'value'}.should_not include(:key)
143
- }.should fail_matching(%Q|expected {:key=>"value"} not to include :key|)
183
+ expect {
184
+ expect({:key => 'value'}).not_to include(:key)
185
+ }.to fail_matching(%Q|expected {:key=>"value"} not to include :key|)
144
186
  end
145
187
  end
146
188
 
147
189
  end
148
190
 
149
- describe "should_not include(with, multiple, args)" do
191
+ describe "expect(...).to_not include(with, multiple, args)" do
150
192
  context "for a string target" do
151
193
  it "passes if the target does not include any of the expected" do
152
- "abc".should_not include("d", "e", "f")
194
+ expect("abc").not_to include("d", "e", "f")
153
195
  end
154
196
 
155
197
  it "fails if the target includes all of the expected" do
156
198
  expect {
157
- "abc".should_not include("c", "a")
199
+ expect("abc").not_to include("c", "a")
158
200
  }.to fail_with('expected "abc" not to include "c" and "a"')
159
201
  end
160
202
 
161
203
  it "fails if the target includes some (but not all) of the expected" do
162
204
  expect {
163
- "abc".should_not include("d", "a")
205
+ expect("abc").not_to include("d", "a")
164
206
  }.to fail_with(%q{expected "abc" not to include "d" and "a"})
165
207
  end
166
208
  end
167
209
 
168
210
  context "for a hash target" do
169
211
  it "passes if it does not include any of the expected keys" do
170
- { :a => 1, :b => 2 }.should_not include(:c, :d)
212
+ expect({ :a => 1, :b => 2 }).not_to include(:c, :d)
171
213
  end
172
214
 
173
215
  it "fails if the target includes all of the expected keys" do
174
216
  expect {
175
- { :a => 1, :b => 2 }.should_not include(:a, :b)
217
+ expect({ :a => 1, :b => 2 }).not_to include(:a, :b)
176
218
  }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include :a and :b|)
177
219
  end
178
220
 
179
221
  it "fails if the target includes some (but not all) of the expected keys" do
180
222
  expect {
181
- { :a => 1, :b => 2 }.should_not include(:d, :b)
223
+ expect({ :a => 1, :b => 2 }).not_to include(:d, :b)
182
224
  }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include :d and :b|)
183
225
  end
184
226
  end
185
227
 
186
228
  context "for an array target" do
187
229
  it "passes if the target does not include any of the expected" do
188
- [1, 2, 3].should_not include(4, 5, 6)
230
+ expect([1, 2, 3]).not_to include(4, 5, 6)
189
231
  end
190
232
 
191
233
  it "fails if the target includes all of the expected" do
192
234
  expect {
193
- [1, 2, 3].should_not include(3, 1)
235
+ expect([1, 2, 3]).not_to include(3, 1)
194
236
  }.to fail_with(%q{expected [1, 2, 3] not to include 3 and 1})
195
237
  end
196
238
 
197
239
  it "fails if the target includes some (but not all) of the expected" do
198
240
  expect {
199
- [1, 2, 3].should_not include(4, 1)
241
+ expect([1, 2, 3]).not_to include(4, 1)
200
242
  }.to fail_with(%q{expected [1, 2, 3] not to include 4 and 1})
201
243
  end
202
244
  end
203
245
  end
204
246
 
205
- describe "should include(:key => value)" do
247
+ describe "expect(...).to include(:key => value)" do
206
248
  context 'for a hash target' do
207
249
  it "passes if target includes the key/value pair" do
208
- {:key => 'value'}.should include(:key => 'value')
250
+ expect({:key => 'value'}).to include(:key => 'value')
209
251
  end
210
252
 
211
253
  it "passes if target includes the key/value pair among others" do
212
- {:key => 'value', :other => 'different'}.should include(:key => 'value')
254
+ expect({:key => 'value', :other => 'different'}).to include(:key => 'value')
213
255
  end
214
256
 
215
257
  it "fails if target has a different value for key" do
216
- lambda {
217
- {:key => 'different'}.should include(:key => 'value')
218
- }.should fail_matching(%Q|expected {:key=>"different"} to include {:key=>"value"}|)
258
+ expect {
259
+ expect({:key => 'different'}).to include(:key => 'value')
260
+ }.to fail_matching(%Q|expected {:key=>"different"} to include {:key=>"value"}|)
219
261
  end
220
262
 
221
263
  it "fails if target has a different key" do
222
- lambda {
223
- {:other => 'value'}.should include(:key => 'value')
224
- }.should fail_matching(%Q|expected {:other=>"value"} to include {:key=>"value"}|)
264
+ expect {
265
+ expect({:other => 'value'}).to include(:key => 'value')
266
+ }.to fail_matching(%Q|expected {:other=>"value"} to include {:key=>"value"}|)
225
267
  end
226
268
  end
227
269
 
228
270
  context 'for a non-hash target' do
229
271
  it "fails if the target does not contain the given hash" do
230
- lambda {
231
- ['a', 'b'].should include(:key => 'value')
232
- }.should fail_matching(%q|expected ["a", "b"] to include {:key=>"value"}|)
272
+ expect {
273
+ expect(['a', 'b']).to include(:key => 'value')
274
+ }.to fail_matching(%q|expected ["a", "b"] to include {:key=>"value"}|)
233
275
  end
234
276
 
235
277
  it "passes if the target contains the given hash" do
236
- ['a', { :key => 'value' } ].should include(:key => 'value')
278
+ expect(['a', { :key => 'value' } ]).to include(:key => 'value')
237
279
  end
238
280
  end
239
281
  end
240
282
 
241
- describe "should_not include(:key => value)" do
283
+ describe "expect(...).to_not include(:key => value)" do
242
284
  context 'for a hash target' do
243
285
  it "fails if target includes the key/value pair" do
244
- lambda {
245
- {:key => 'value'}.should_not include(:key => 'value')
246
- }.should fail_matching(%Q|expected {:key=>"value"} not to include {:key=>"value"}|)
286
+ expect {
287
+ expect({:key => 'value'}).not_to include(:key => 'value')
288
+ }.to fail_matching(%Q|expected {:key=>"value"} not to include {:key=>"value"}|)
247
289
  end
248
290
 
249
291
  it "fails if target includes the key/value pair among others" do
250
- lambda {
251
- {:key => 'value', :other => 'different'}.should_not include(:key => 'value')
252
- }.should fail_matching(%Q|expected #{{:key=>"value", :other=>"different"}.inspect} not to include {:key=>"value"}|)
292
+ expect {
293
+ expect({:key => 'value', :other => 'different'}).not_to include(:key => 'value')
294
+ }.to fail_matching(%Q|expected #{{:key=>"value", :other=>"different"}.inspect} not to include {:key=>"value"}|)
253
295
  end
254
296
 
255
297
  it "passes if target has a different value for key" do
256
- {:key => 'different'}.should_not include(:key => 'value')
298
+ expect({:key => 'different'}).not_to include(:key => 'value')
257
299
  end
258
300
 
259
301
  it "passes if target has a different key" do
260
- {:other => 'value'}.should_not include(:key => 'value')
302
+ expect({:other => 'value'}).not_to include(:key => 'value')
261
303
  end
262
304
  end
263
305
 
264
306
  context "for a non-hash target" do
265
307
  it "passes if the target does not contain the given hash" do
266
- ['a', 'b'].should_not include(:key => 'value')
308
+ expect(['a', 'b']).not_to include(:key => 'value')
267
309
  end
268
310
 
269
311
  it "fails if the target contains the given hash" do
270
- lambda {
271
- ['a', { :key => 'value' } ].should_not include(:key => 'value')
272
- }.should fail_matching(%Q|expected ["a", {:key=>"value"}] not to include {:key=>"value"}|)
312
+ expect {
313
+ expect(['a', { :key => 'value' } ]).not_to include(:key => 'value')
314
+ }.to fail_matching(%Q|expected ["a", {:key=>"value"}] not to include {:key=>"value"}|)
273
315
  end
274
316
  end
275
317
  end
276
318
 
277
- describe "should include(:key1 => value1, :key2 => value2)" do
319
+ describe "expect(...).to include(:key1 => value1, :key2 => value2)" do
278
320
  context 'for a hash target' do
279
321
  it "passes if target includes the key/value pairs" do
280
- {:a => 1, :b => 2}.should include(:b => 2, :a => 1)
322
+ expect({:a => 1, :b => 2}).to include(:b => 2, :a => 1)
281
323
  end
282
324
 
283
325
  it "passes if target includes the key/value pairs among others" do
284
- {:a => 1, :c => 3, :b => 2}.should include(:b => 2, :a => 1)
326
+ expect({:a => 1, :c => 3, :b => 2}).to include(:b => 2, :a => 1)
285
327
  end
286
328
 
287
329
  it "fails if target has a different value for one of the keys" do
288
- lambda {
289
- {:a => 1, :b => 2}.should include(:a => 2, :b => 2)
290
- }.should fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} to include #{{:a=>2, :b=>2}.inspect}|)
330
+ expect {
331
+ expect({:a => 1, :b => 2}).to include(:a => 2, :b => 2)
332
+ }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} to include #{{:a=>2, :b=>2}.inspect}|)
291
333
  end
292
334
 
293
335
  it "fails if target has a different value for both of the keys" do
294
- lambda {
295
- {:a => 1, :b => 1}.should include(:a => 2, :b => 2)
296
- }.should fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:a=>2, :b=>2}.inspect}|)
336
+ expect {
337
+ expect({:a => 1, :b => 1}).to include(:a => 2, :b => 2)
338
+ }.to fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:a=>2, :b=>2}.inspect}|)
297
339
  end
298
340
 
299
341
  it "fails if target lacks one of the keys" do
300
- lambda {
301
- {:a => 1, :b => 1}.should include(:a => 1, :c => 1)
302
- }.should fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:a=>1, :c=>1}.inspect}|)
342
+ expect {
343
+ expect({:a => 1, :b => 1}).to include(:a => 1, :c => 1)
344
+ }.to fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:a=>1, :c=>1}.inspect}|)
303
345
  end
304
346
 
305
347
  it "fails if target lacks both of the keys" do
306
- lambda {
307
- {:a => 1, :b => 1}.should include(:c => 1, :d => 1)
308
- }.should fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:c=>1, :d=>1}.inspect}|)
348
+ expect {
349
+ expect({:a => 1, :b => 1}).to include(:c => 1, :d => 1)
350
+ }.to fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:c=>1, :d=>1}.inspect}|)
309
351
  end
310
352
  end
311
353
 
312
354
  context 'for a non-hash target' do
313
355
  it "fails if the target does not contain the given hash" do
314
- lambda {
315
- ['a', 'b'].should include(:a => 1, :b => 1)
316
- }.should fail_matching(%Q|expected ["a", "b"] to include #{{:a=>1, :b=>1}.inspect}|)
356
+ expect {
357
+ expect(['a', 'b']).to include(:a => 1, :b => 1)
358
+ }.to fail_matching(%Q|expected ["a", "b"] to include #{{:a=>1, :b=>1}.inspect}|)
317
359
  end
318
360
 
319
361
  it "passes if the target contains the given hash" do
320
- ['a', { :a => 1, :b => 2 } ].should include(:a => 1, :b => 2)
362
+ expect(['a', { :a => 1, :b => 2 } ]).to include(:a => 1, :b => 2)
321
363
  end
322
364
  end
323
365
  end
324
366
 
325
- describe "should_not include(:key1 => value1, :key2 => value2)" do
367
+ describe "expect(...).to_not include(:key1 => value1, :key2 => value2)" do
326
368
  context 'for a hash target' do
327
369
  it "fails if target includes the key/value pairs" do
328
- lambda {
329
- {:a => 1, :b => 2}.should_not include(:a => 1, :b => 2)
330
- }.should fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
370
+ expect {
371
+ expect({:a => 1, :b => 2}).not_to include(:a => 1, :b => 2)
372
+ }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
331
373
  end
332
374
 
333
375
  it "fails if target includes the key/value pairs among others" do
334
376
  hash = {:a => 1, :b => 2, :c => 3}
335
- lambda {
336
- hash.should_not include(:a => 1, :b => 2)
337
- }.should fail_matching(%Q|expected #{hash.inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
377
+ expect {
378
+ expect(hash).not_to include(:a => 1, :b => 2)
379
+ }.to fail_matching(%Q|expected #{hash.inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
338
380
  end
339
381
 
340
382
  it "fails if target has a different value for one of the keys" do
341
- lambda {
342
- {:a => 1, :b => 2}.should_not include(:a => 2, :b => 2)
343
- }.should fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include #{{:a=>2, :b=>2}.inspect}|)
383
+ expect {
384
+ expect({:a => 1, :b => 2}).not_to include(:a => 2, :b => 2)
385
+ }.to fail_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include #{{:a=>2, :b=>2}.inspect}|)
344
386
  end
345
387
 
346
388
  it "passes if target has a different value for both of the keys" do
347
- {:a => 1, :b => 1}.should_not include(:a => 2, :b => 2)
389
+ expect({:a => 1, :b => 1}).not_to include(:a => 2, :b => 2)
348
390
  end
349
391
 
350
392
  it "fails if target lacks one of the keys" do
351
- lambda {
352
- {:a => 1, :b => 1}.should_not include(:a => 1, :c => 1)
353
- }.should fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} not to include #{{:a=>1, :c=>1}.inspect}|)
393
+ expect {
394
+ expect({:a => 1, :b => 1}).not_to include(:a => 1, :c => 1)
395
+ }.to fail_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} not to include #{{:a=>1, :c=>1}.inspect}|)
354
396
  end
355
397
 
356
398
  it "passes if target lacks both of the keys" do
357
- {:a => 1, :b => 1}.should_not include(:c => 1, :d => 1)
399
+ expect({:a => 1, :b => 1}).not_to include(:c => 1, :d => 1)
358
400
  end
359
401
  end
360
402
 
361
403
  context 'for a non-hash target' do
362
404
  it "passes if the target does not contain the given hash" do
363
- ['a', 'b'].should_not include(:a => 1, :b => 1)
405
+ expect(['a', 'b']).not_to include(:a => 1, :b => 1)
364
406
  end
365
407
 
366
408
  it "fails if the target contains the given hash" do
367
- lambda {
368
- ['a', { :a => 1, :b => 2 } ].should_not include(:a => 1, :b => 2)
369
- }.should fail_matching(%Q|expected #{["a", {:a=>1, :b=>2}].inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
409
+ expect {
410
+ expect(['a', { :a => 1, :b => 2 } ]).not_to include(:a => 1, :b => 2)
411
+ }.to fail_matching(%Q|expected #{["a", {:a=>1, :b=>2}].inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
412
+ end
413
+ end
414
+ end
415
+ end
416
+
417
+ RSpec::Matchers.define :a_string_containing do |expected|
418
+ match do |actual|
419
+ actual.include?(expected)
420
+ end
421
+
422
+ description do
423
+ "a string containing '#{expected}'"
424
+ end
425
+ end
426
+
427
+ describe "expect(...).to include(matcher)" do
428
+ context 'for an array target' do
429
+ it "passes if target includes an object that satisfies the matcher" do
430
+ expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"))
431
+ end
432
+
433
+ it "fails if target doesn't include object that satisfies the matcher" do
434
+ expect {
435
+ expect(['foo', 'bar', 'baz']).to include(a_string_containing("abc"))
436
+ }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} to include a string containing 'abc'|)
437
+ end
438
+
439
+ it 'does not include a diff when the match fails' do
440
+ expect {
441
+ expect(['foo', 'bar', 'baz']).to include(a_string_containing("abc"))
442
+ }.to raise_error { |e|
443
+ expect(e.message).not_to match(/diff/i)
444
+ }
445
+ end
446
+
447
+ it 'does not treat an object that only implements #matches? as a matcher' do
448
+ domain = Struct.new(:domain) do
449
+ def matches?(url)
450
+ URI(url).host == self.domain
451
+ end
452
+ end
453
+
454
+ expect([domain.new("rspec.info")]).to include(domain.new("rspec.info"))
455
+
456
+ expect {
457
+ expect([domain.new("rspec.info")]).to include(domain.new("foo.com"))
458
+ }.to fail_matching("expected [#{domain.new("rspec.info").inspect}] to include")
459
+ end
460
+
461
+ it 'works with an old-style matcher that implements failure_message rather than failure_message_for_should' do
462
+ a_multiple_of = Class.new do
463
+ def initialize(expected)
464
+ @expected = expected
465
+ end
466
+
467
+ def matches?(actual)
468
+ (actual % @expected).zero?
469
+ end
470
+
471
+ def failure_message
472
+ "expected a multiple of #{@expected}"
473
+ end
370
474
  end
475
+
476
+ # Verify the matcher works normally
477
+ expect(10).to a_multiple_of.new(5)
478
+
479
+ expect {
480
+ expect(10).to a_multiple_of.new(7)
481
+ }.to fail_with("expected a multiple of 7")
482
+
483
+ expect([12, 13, 14]).to include(a_multiple_of.new(6))
484
+
485
+ expect {
486
+ expect([12, 13, 14]).to include(a_multiple_of.new(10))
487
+ }.to fail_matching("expected [12, 13, 14] to include")
488
+ end
489
+ end
490
+ end
491
+
492
+ describe "expect(...).to include(multiple, matcher, arguments)" do
493
+ context 'for an array target' do
494
+ it "passes if target includes items satisfying all matchers" do
495
+ expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing('oo'))
496
+ end
497
+
498
+ it "fails if target does not include an item satisfying any one of the items" do
499
+ expect {
500
+ expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing("abc"))
501
+ }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} to include a string containing 'ar' and a string containing 'abc'|)
502
+ end
503
+
504
+ it 'does not include a diff when the match fails' do
505
+ expect {
506
+ expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing("abc"))
507
+ }.to raise_error { |e|
508
+ expect(e.message).not_to match(/diff/i)
509
+ }
371
510
  end
372
511
  end
373
512
  end
513
+
514
+ describe "expect(...).not_to include(multiple, matcher, arguments)" do
515
+ it "passes if none of the target values satisfies any of the matchers" do
516
+ expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("gh"), a_string_containing('de'))
517
+ end
518
+
519
+ it 'fails if all of the matchers are satisfied by one of the target values' do
520
+ expect {
521
+ expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('az'))
522
+ }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} not to include a string containing 'ar' and a string containing 'az'|)
523
+ end
524
+
525
+ it 'fails if the some (but not all) of the matchers are satisifed' do
526
+ expect {
527
+ expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('bz'))
528
+ }.to fail_matching(%Q|expected #{['foo', 'bar', 'baz'].inspect} not to include a string containing 'ar' and a string containing 'bz'|)
529
+ end
530
+ end
531
+