rspec-expectations 2.0.0.rc → 2.0.0

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.
@@ -5,3 +5,9 @@ Then /^the output should contain "([^"]*)" or "([^"]*)"$/ do |string1, string2|
5
5
  end
6
6
  end
7
7
 
8
+ Then /^the output should contain all of these:$/ do |table|
9
+ table.raw.flatten.each do |string|
10
+ assert_partial_output(string)
11
+ end
12
+ end
13
+
@@ -1,7 +1,7 @@
1
1
  module RSpec # :nodoc:
2
2
  module Expectations # :nodoc:
3
3
  module Version # :nodoc:
4
- STRING = '2.0.0.rc'
4
+ STRING = '2.0.0'
5
5
  end
6
6
  end
7
7
  end
@@ -18,10 +18,18 @@ module RSpec
18
18
  # "spread".should_not include("red")
19
19
  def include(*expected)
20
20
  Matcher.new :include, *expected do |*_expected|
21
- match do |actual|
22
- _expected.all? do |expected|
21
+ match_for_should do |actual|
22
+ perform_match(:all?, :all?, actual, _expected)
23
+ end
24
+
25
+ match_for_should_not do |actual|
26
+ perform_match(:none?, :any?, actual, _expected)
27
+ end
28
+
29
+ def perform_match(predicate, hash_predicate, actual, _expected)
30
+ _expected.send(predicate) do |expected|
23
31
  if comparing_hash_values?(actual, expected)
24
- expected.all? {|k,v| actual[k] == v}
32
+ expected.send(hash_predicate) {|k,v| actual[k] == v}
25
33
  elsif comparing_hash_keys?(actual, expected)
26
34
  actual.has_key?(expected)
27
35
  else
@@ -37,9 +45,7 @@ module RSpec
37
45
  def comparing_hash_values?(actual, expected) # :nodoc:
38
46
  actual.is_a?(Hash) && expected.is_a?(Hash)
39
47
  end
40
-
41
48
  end
42
-
43
49
  end
44
50
  end
45
51
  end
@@ -22,7 +22,7 @@ module RSpec
22
22
  end
23
23
  end
24
24
 
25
- #Used internally by objects returns by +should+ and +should_not+.
25
+ #Used internally by +should+ and +should_not+.
26
26
  def matches?(actual)
27
27
  @actual = actual
28
28
  if @expected_exception
@@ -41,10 +41,28 @@ module RSpec
41
41
  end
42
42
  end
43
43
 
44
+ # Used internally by +should_not+
45
+ def does_not_match?(actual)
46
+ @actual = actual
47
+ @match_for_should_not_block ?
48
+ instance_exec(actual, &@match_for_should_not_block) :
49
+ !matches?(actual)
50
+ end
51
+
52
+ def define_method(name, &block) # :nodoc:
53
+ singleton_class.__send__(:define_method, name, &block)
54
+ end
55
+
44
56
  # See RSpec::Matchers
45
57
  def match(&block)
46
58
  @match_block = block
47
59
  end
60
+ alias match_for_should match
61
+
62
+ # See RSpec::Matchers
63
+ def match_for_should_not(&block)
64
+ @match_for_should_not_block = block
65
+ end
48
66
 
49
67
  # See RSpec::Matchers
50
68
  def match_unless_raises(exception=Exception, &block)
@@ -110,8 +128,7 @@ module RSpec
110
128
  # cause features to fail and that will make users unhappy. So don't.
111
129
  orig_private_methods = private_methods
112
130
  yield
113
- st = (class << self; self; end)
114
- (private_methods - orig_private_methods).each {|m| st.__send__ :public, m}
131
+ (private_methods - orig_private_methods).each {|m| singleton_class.__send__ :public, m}
115
132
  end
116
133
 
117
134
  def cache_or_call_cached(key, &block)
@@ -134,6 +151,11 @@ module RSpec
134
151
  to_sentence(@expected)
135
152
  end
136
153
 
154
+ unless method_defined?(:singleton_class)
155
+ def singleton_class
156
+ class << self; self; end
157
+ end
158
+ end
137
159
  end
138
160
  end
139
161
  end
@@ -5,23 +5,22 @@ module RSpec
5
5
  def initialize(*names)
6
6
  @names = names
7
7
  @expected_arity = nil
8
- @names_not_responded_to = []
9
8
  end
10
9
 
11
10
  def matches?(actual)
12
- @actual = actual
13
- @names.each do |name|
14
- @names_not_responded_to << name unless actual.respond_to?(name) && matches_arity?(actual, name)
15
- end
16
- return @names_not_responded_to.empty?
11
+ find_failing_method_names(actual, :reject).empty?
12
+ end
13
+
14
+ def does_not_match?(actual)
15
+ find_failing_method_names(actual, :select).empty?
17
16
  end
18
17
 
19
18
  def failure_message_for_should
20
- "expected #{@actual.inspect} to respond to #{@names_not_responded_to.collect {|name| name.inspect }.join(', ')}#{with_arity}"
19
+ "expected #{@actual.inspect} to respond to #{@failing_method_names.collect {|name| name.inspect }.join(', ')}#{with_arity}"
21
20
  end
22
21
 
23
22
  def failure_message_for_should_not
24
- "expected #{@actual.inspect} not to respond to #{@names.collect {|name| name.inspect }.join(', ')}"
23
+ failure_message_for_should.sub(/to respond to/, 'not to respond to')
25
24
  end
26
25
 
27
26
  def description
@@ -39,9 +38,24 @@ module RSpec
39
38
  alias :arguments :argument
40
39
 
41
40
  private
41
+
42
+ def find_failing_method_names(actual, filter_method)
43
+ @actual = actual
44
+ @failing_method_names = @names.send(filter_method) do |name|
45
+ @actual.respond_to?(name) && matches_arity?(actual, name)
46
+ end
47
+ end
42
48
 
43
49
  def matches_arity?(actual, name)
44
- @expected_arity.nil?? true : @expected_arity == actual.method(name).arity
50
+ return true unless @expected_arity
51
+
52
+ actual_arity = actual.method(name).arity
53
+ if actual_arity < 0
54
+ # ~ inverts the one's complement and gives us the number of required args
55
+ ~actual_arity <= @expected_arity
56
+ else
57
+ actual_arity == @expected_arity
58
+ end
45
59
  end
46
60
 
47
61
  def with_arity
@@ -1,88 +1,341 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "should include(expected)" do
4
- it "passes if target includes expected" do
5
- [1,2,3].should include(3)
6
- "abc".should include("a")
7
- end
8
-
9
- it 'passes if target is a Hash and has the expected as a key' do
10
- {:key => 'value'}.should include(:key)
11
- end
12
-
13
- it "fails 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|)
4
+ context "for a string target" do
5
+ it "passes if target includes expected" do
6
+ "abc".should include("a")
7
+ end
8
+
9
+ it "fails if target does not include expected" do
10
+ lambda {
11
+ "abc".should include("d")
12
+ }.should fail_with("expected \"abc\" to include \"d\"")
13
+ end
14
+ end
15
+
16
+ context "for an array target" do
17
+ it "passes if target includes expected" do
18
+ [1,2,3].should include(3)
19
+ end
20
+
21
+ it "fails if target does not include expected" do
22
+ lambda {
23
+ [1,2,3].should include(4)
24
+ }.should fail_with("expected [1, 2, 3] to include 4")
25
+ end
26
+ end
27
+
28
+ context "for a hash target" do
29
+ it 'passes if target has the expected as a key' do
30
+ {:key => 'value'}.should include(:key)
31
+ end
32
+
33
+ it "fails if target does not include expected" do
34
+ lambda {
35
+ {:key => 'value'}.should include(:other)
36
+ }.should fail_with(%Q|expected {:key=>"value"} to include :other|)
37
+ end
23
38
  end
24
39
  end
25
40
 
26
41
  describe "should include(with, multiple, args)" do
27
- it "passes if target includes all items" do
28
- [1,2,3].should include(1,2,3)
29
- end
30
-
31
- it 'passes if target is a Hash including all items as keys' do
32
- {:key => 'value', :other => 'value'}.should include(:key, :other)
42
+ context "for a string target" do
43
+ it "passes if target includes all items" do
44
+ "a string".should include("str", "a")
45
+ end
46
+
47
+ it "fails if target does not include any one of the items" do
48
+ lambda {
49
+ "a string".should include("str", "a", "foo")
50
+ }.should fail_with(%Q{expected "a string" to include "str", "a", and "foo"})
51
+ end
33
52
  end
34
53
 
35
- it "fails 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")
54
+ context "for an array target" do
55
+ it "passes if target includes all items" do
56
+ [1,2,3].should include(1,2,3)
57
+ end
58
+
59
+ it "fails if target does not include any one of the items" do
60
+ lambda {
61
+ [1,2,3].should include(1,2,4)
62
+ }.should fail_with("expected [1, 2, 3] to include 1, 2, and 4")
63
+ end
39
64
  end
40
-
41
- it 'passes 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|)
65
+
66
+ context "for a hash target" do
67
+ it 'passes if target includes all items as keys' do
68
+ {:key => 'value', :other => 'value'}.should include(:key, :other)
69
+ end
70
+
71
+ it 'fails if target is missing any item as a key' do
72
+ lambda {
73
+ {:key => 'value'}.should include(:key, :other)
74
+ }.should fail_with(%Q|expected {:key=>"value"} to include :key and :other|)
75
+ end
45
76
  end
46
77
  end
47
78
 
48
79
  describe "should_not include(expected)" do
49
- it "passes 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 'passes 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 "fails 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|)
80
+ context "for a string target" do
81
+ it "passes if target does not include expected" do
82
+ "abc".should_not include("d")
83
+ end
84
+
85
+ it "fails if target includes expected" do
86
+ lambda {
87
+ "abc".should_not include("c")
88
+ }.should fail_with("expected \"abc\" not to include \"c\"")
89
+ end
90
+ end
91
+
92
+ context "for an array target" do
93
+ it "passes if target does not include expected" do
94
+ [1,2,3].should_not include(4)
95
+ end
96
+
97
+ it "fails if target includes expected" do
98
+ lambda {
99
+ [1,2,3].should_not include(3)
100
+ }.should fail_with("expected [1, 2, 3] not to include 3")
101
+ end
102
+ end
103
+
104
+ context "for a hash target" do
105
+ it 'passes if target does not have the expected as a key' do
106
+ {:other => 'value'}.should_not include(:key)
107
+ end
108
+
109
+ it "fails if target includes expected key" do
110
+ lambda {
111
+ {:key => 'value'}.should_not include(:key)
112
+ }.should fail_with(%Q|expected {:key=>"value"} not to include :key|)
113
+ end
114
+ end
115
+
116
+ end
117
+
118
+ describe "should_not include(with, multiple, args)" do
119
+ context "for a string target" do
120
+ it "passes if the target does not include any of the expected" do
121
+ "abc".should_not include("d", "e", "f")
122
+ end
123
+
124
+ it "fails if the target includes all of the expected" do
125
+ expect {
126
+ "abc".should_not include("c", "a")
127
+ }.to fail_with(%q{expected "abc" not to include "c" and "a"})
128
+ end
129
+
130
+ it "fails if the target includes some (but not all) of the expected" do
131
+ expect {
132
+ "abc".should_not include("d", "a")
133
+ }.to fail_with(%q{expected "abc" not to include "d" and "a"})
134
+ end
135
+ end
136
+
137
+ context "for a hash target" do
138
+ it "passes if it does not include any of the expected keys" do
139
+ { :a => 1, :b => 2 }.should_not include(:c, :d)
140
+ end
141
+
142
+ it "fails if the target includes all of the expected keys" do
143
+ expect {
144
+ { :a => 1, :b => 2 }.should_not include(:a, :b)
145
+ }.to fail_with(%q|expected {:a=>1, :b=>2} not to include :a and :b|)
146
+ end
147
+
148
+ it "fails if the target includes some (but not all) of the expected keys" do
149
+ expect {
150
+ { :a => 1, :b => 2 }.should_not include(:d, :b)
151
+ }.to fail_with(%q|expected {:a=>1, :b=>2} not to include :d and :b|)
152
+ end
153
+ end
154
+
155
+ context "for an array target" do
156
+ it "passes if the target does not include any of the expected" do
157
+ [1, 2, 3].should_not include(4, 5, 6)
158
+ end
159
+
160
+ it "fails if the target includes all of the expected" do
161
+ expect {
162
+ [1, 2, 3].should_not include(3, 1)
163
+ }.to fail_with(%q{expected [1, 2, 3] not to include 3 and 1})
164
+ end
165
+
166
+ it "fails if the target includes some (but not all) of the expected" do
167
+ expect {
168
+ [1, 2, 3].should_not include(4, 1)
169
+ }.to fail_with(%q{expected [1, 2, 3] not to include 4 and 1})
170
+ end
68
171
  end
69
172
  end
70
173
 
71
174
  describe "should include(:key => value)" do
72
- it "passes if target is a Hash and includes the key/value pair" do
73
- {:key => 'value'}.should include(:key => 'value')
175
+ context 'for a hash target' do
176
+ it "passes if target includes the key/value pair" do
177
+ {:key => 'value'}.should include(:key => 'value')
178
+ end
179
+
180
+ it "passes if target includes the key/value pair among others" do
181
+ {:key => 'value', :other => 'different'}.should include(:key => 'value')
182
+ end
183
+
184
+ it "fails if target has a different value for key" do
185
+ lambda {
186
+ {:key => 'different'}.should include(:key => 'value')
187
+ }.should fail_with(%Q|expected {:key=>"different"} to include {:key=>"value"}|)
188
+ end
189
+
190
+ it "fails if target has a different key" do
191
+ lambda {
192
+ {:other => 'value'}.should include(:key => 'value')
193
+ }.should fail_with(%Q|expected {:other=>"value"} to include {:key=>"value"}|)
194
+ end
74
195
  end
75
- it "passes if target is a Hash and includes the key/value pair among others" do
76
- {:key => 'value', :other => 'different'}.should include(:key => 'value')
196
+
197
+ context 'for a non-hash target' do
198
+ it "fails if the target does not contain the given hash" do
199
+ lambda {
200
+ ['a', 'b'].should include(:key => 'value')
201
+ }.should fail_with(%q|expected ["a", "b"] to include {:key=>"value"}|)
202
+ end
203
+
204
+ it "passes if the target contains the given hash" do
205
+ ['a', { :key => 'value' } ].should include(:key => 'value')
206
+ end
77
207
  end
78
- it "fails 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"}|)
208
+ end
209
+
210
+ describe "should_not include(:key => value)" do
211
+ context 'for a hash target' do
212
+ it "fails if target includes the key/value pair" do
213
+ lambda {
214
+ {:key => 'value'}.should_not include(:key => 'value')
215
+ }.should fail_with(%Q|expected {:key=>"value"} not to include {:key=>"value"}|)
216
+ end
217
+
218
+ it "fails if target includes the key/value pair among others" do
219
+ lambda {
220
+ {:key => 'value', :other => 'different'}.should_not include(:key => 'value')
221
+ }.should fail_with(%Q|expected {:key=>"value", :other=>"different"} not to include {:key=>"value"}|)
222
+ end
223
+
224
+ it "passes if target has a different value for key" do
225
+ {:key => 'different'}.should_not include(:key => 'value')
226
+ end
227
+
228
+ it "passes if target has a different key" do
229
+ {:other => 'value'}.should_not include(:key => 'value')
230
+ end
82
231
  end
83
- it "fails 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"}|)
232
+
233
+ context "for a non-hash target" do
234
+ it "passes if the target does not contain the given hash" do
235
+ ['a', 'b'].should_not include(:key => 'value')
236
+ end
237
+
238
+ it "fails if the target contains the given hash" do
239
+ lambda {
240
+ ['a', { :key => 'value' } ].should_not include(:key => 'value')
241
+ }.should fail_with(%Q|expected ["a", {:key=>"value"}] not to include {:key=>"value"}|)
242
+ end
243
+ end
244
+ end
245
+
246
+ describe "should include(:key1 => value1, :key2 => value2)" do
247
+ context 'for a hash target' do
248
+ it "passes if target includes the key/value pairs" do
249
+ {:a => 1, :b => 2}.should include(:b => 2, :a => 1)
250
+ end
251
+
252
+ it "passes if target includes the key/value pairs among others" do
253
+ {:a => 1, :c => 3, :b => 2}.should include(:b => 2, :a => 1)
254
+ end
255
+
256
+ it "fails if target has a different value for one of the keys" do
257
+ lambda {
258
+ {:a => 1, :b => 2}.should include(:a => 2, :b => 2)
259
+ }.should fail_with(%Q|expected {:a=>1, :b=>2} to include {:a=>2, :b=>2}|)
260
+ end
261
+
262
+ it "fails if target has a different value for both of the keys" do
263
+ lambda {
264
+ {:a => 1, :b => 1}.should include(:a => 2, :b => 2)
265
+ }.should fail_with(%Q|expected {:a=>1, :b=>1} to include {:a=>2, :b=>2}|)
266
+ end
267
+
268
+ it "fails if target lacks one of the keys" do
269
+ lambda {
270
+ {:a => 1, :b => 1}.should include(:a => 1, :c => 1)
271
+ }.should fail_with(%Q|expected {:a=>1, :b=>1} to include {:a=>1, :c=>1}|)
272
+ end
273
+
274
+ it "fails if target lacks both of the keys" do
275
+ lambda {
276
+ {:a => 1, :b => 1}.should include(:c => 1, :d => 1)
277
+ }.should fail_with(%Q|expected {:a=>1, :b=>1} to include {:c=>1, :d=>1}|)
278
+ end
279
+ end
280
+
281
+ context 'for a non-hash target' do
282
+ it "fails if the target does not contain the given hash" do
283
+ lambda {
284
+ ['a', 'b'].should include(:a => 1, :b => 1)
285
+ }.should fail_with(%q|expected ["a", "b"] to include {:a=>1, :b=>1}|)
286
+ end
287
+
288
+ it "passes if the target contains the given hash" do
289
+ ['a', { :a => 1, :b => 2 } ].should include(:a => 1, :b => 2)
290
+ end
291
+ end
292
+ end
293
+
294
+ describe "should_not include(:key1 => value1, :key2 => value2)" do
295
+ context 'for a hash target' do
296
+ it "fails if target includes the key/value pairs" do
297
+ lambda {
298
+ {:a => 1, :b => 2}.should_not include(:a => 1, :b => 2)
299
+ }.should fail_with(%Q|expected {:a=>1, :b=>2} not to include {:a=>1, :b=>2}|)
300
+ end
301
+
302
+ it "fails if target includes the key/value pairs among others" do
303
+ hash = {:a => 1, :b => 2, :c => 3}
304
+ lambda {
305
+ hash.should_not include(:a => 1, :b => 2)
306
+ }.should fail_with(%Q|expected #{hash.inspect} not to include {:a=>1, :b=>2}|)
307
+ end
308
+
309
+ it "fails if target has a different value for one of the keys" do
310
+ lambda {
311
+ {:a => 1, :b => 2}.should_not include(:a => 2, :b => 2)
312
+ }.should fail_with(%Q|expected {:a=>1, :b=>2} not to include {:a=>2, :b=>2}|)
313
+ end
314
+
315
+ it "passes if target has a different value for both of the keys" do
316
+ {:a => 1, :b => 1}.should_not include(:a => 2, :b => 2)
317
+ end
318
+
319
+ it "fails if target lacks one of the keys" do
320
+ lambda {
321
+ {:a => 1, :b => 1}.should_not include(:a => 1, :c => 1)
322
+ }.should fail_with(%Q|expected {:a=>1, :b=>1} not to include {:a=>1, :c=>1}|)
323
+ end
324
+
325
+ it "passes if target lacks both of the keys" do
326
+ {:a => 1, :b => 1}.should_not include(:c => 1, :d => 1)
327
+ end
328
+ end
329
+
330
+ context 'for a non-hash target' do
331
+ it "passes if the target does not contain the given hash" do
332
+ ['a', 'b'].should_not include(:a => 1, :b => 1)
333
+ end
334
+
335
+ it "fails if the target contains the given hash" do
336
+ lambda {
337
+ ['a', { :a => 1, :b => 2 } ].should_not include(:a => 1, :b => 2)
338
+ }.should fail_with(%Q|expected ["a", {:a=>1, :b=>2}] not to include {:a=>1, :b=>2}|)
339
+ end
87
340
  end
88
341
  end