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,108 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ class UnsortableObject
4
+ def initialize(id)
5
+ @id = id
6
+ end
7
+
8
+ def inspect
9
+ @id.to_s
10
+ end
11
+
12
+ def ==(other)
13
+ false
14
+ end
15
+ end
16
+
17
+ describe "array.should =~ other_array" do
18
+ it "should pass if target contains all items" do
19
+ [1,2,3].should =~ [1,2,3]
20
+ end
21
+
22
+ it "should pass if target contains all items out of order" do
23
+ [1,3,2].should =~ [1,2,3]
24
+ end
25
+
26
+ it "should fail if target includes extra items" do
27
+ lambda {
28
+ [1,2,3,4].should =~ [1,2,3]
29
+ }.should fail_with(<<-MESSAGE)
30
+ expected collection contained: [1, 2, 3]
31
+ actual collection contained: [1, 2, 3, 4]
32
+ the extra elements were: [4]
33
+ MESSAGE
34
+ end
35
+
36
+ it "should fail if target is missing items" do
37
+ lambda {
38
+ [1,2].should =~ [1,2,3]
39
+ }.should fail_with(<<-MESSAGE)
40
+ expected collection contained: [1, 2, 3]
41
+ actual collection contained: [1, 2]
42
+ the missing elements were: [3]
43
+ MESSAGE
44
+ end
45
+
46
+ it "should fail if target is missing items and has extra items" do
47
+
48
+ lambda {
49
+ [1,2,4].should =~ [1,2,3]
50
+ }.should fail_with(<<-MESSAGE)
51
+ expected collection contained: [1, 2, 3]
52
+ actual collection contained: [1, 2, 4]
53
+ the missing elements were: [3]
54
+ the extra elements were: [4]
55
+ MESSAGE
56
+ end
57
+
58
+ it "should sort items in the error message if they all respond to <=>" do
59
+ lambda {
60
+ [6,2,1,5].should =~ [4,1,2,3]
61
+ }.should fail_with(<<-MESSAGE)
62
+ expected collection contained: [1, 2, 3, 4]
63
+ actual collection contained: [1, 2, 5, 6]
64
+ the missing elements were: [3, 4]
65
+ the extra elements were: [5, 6]
66
+ MESSAGE
67
+ end
68
+
69
+ it "should not sort items in the error message if they don't all respond to <=>" do
70
+ lambda {
71
+ [UnsortableObject.new(2), UnsortableObject.new(1)].should =~ [UnsortableObject.new(4), UnsortableObject.new(3)]
72
+ }.should fail_with(<<-MESSAGE)
73
+ expected collection contained: [4, 3]
74
+ actual collection contained: [2, 1]
75
+ the missing elements were: [4, 3]
76
+ the extra elements were: [2, 1]
77
+ MESSAGE
78
+ end
79
+
80
+ it "should accurately report extra elements when there are duplicates" do
81
+ lambda {
82
+ [1,1,1,5].should =~ [1,5]
83
+ }.should fail_with(<<-MESSAGE)
84
+ expected collection contained: [1, 5]
85
+ actual collection contained: [1, 1, 1, 5]
86
+ the extra elements were: [1, 1]
87
+ MESSAGE
88
+ end
89
+
90
+ it "should accurately report missing elements when there are duplicates" do
91
+ lambda {
92
+ [1,5].should =~ [1,1,5]
93
+ }.should fail_with(<<-MESSAGE)
94
+ expected collection contained: [1, 1, 5]
95
+ actual collection contained: [1, 5]
96
+ the missing elements were: [1]
97
+ MESSAGE
98
+ end
99
+
100
+ end
101
+
102
+ describe "should_not =~ [:with, :multiple, :args]" do
103
+ it "should not be supported" do
104
+ lambda {
105
+ [1,2,3].should_not =~ [1,2,3]
106
+ }.should fail_with(/Matcher does not support should_not/)
107
+ end
108
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "should match(expected)" do
4
+ it "should pass when target (String) matches expected (Regexp)" do
5
+ "string".should match(/tri/)
6
+ end
7
+
8
+ it "should pass when target (String) matches expected (String)" do
9
+ "string".should match("tri")
10
+ end
11
+
12
+ it "should fail when target (String) does not match expected (Regexp)" do
13
+ lambda {
14
+ "string".should match(/rings/)
15
+ }.should fail
16
+ end
17
+
18
+ it "should fail when target (String) does not match expected (String)" do
19
+ lambda {
20
+ "string".should match("rings")
21
+ }.should fail
22
+ end
23
+
24
+ it "should provide message, expected and actual on failure" do
25
+ matcher = match(/rings/)
26
+ matcher.matches?("string")
27
+ matcher.failure_message_for_should.should == "expected \"string\" to match /rings/"
28
+ end
29
+ end
30
+
31
+ describe "should_not match(expected)" do
32
+ it "should pass when target (String) matches does not match (Regexp)" do
33
+ "string".should_not match(/rings/)
34
+ end
35
+
36
+ it "should pass when target (String) matches does not match (String)" do
37
+ "string".should_not match("rings")
38
+ end
39
+
40
+ it "should fail when target (String) matches expected (Regexp)" do
41
+ lambda {
42
+ "string".should_not match(/tri/)
43
+ }.should fail
44
+ end
45
+
46
+ it "should fail when target (String) matches expected (String)" do
47
+ lambda {
48
+ "string".should_not match("tri")
49
+ }.should fail
50
+ end
51
+
52
+ it "should provide message, expected and actual on failure" do
53
+ matcher = match(/tri/)
54
+ matcher.matches?("string")
55
+ matcher.failure_message_for_should_not.should == "expected \"string\" not to match /tri/"
56
+ end
57
+ end
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Matchers
5
+ context %Q{The Rspec::Matchers module gets included in the execution context of every spec.
6
+ This module should provide the following methods, each of which returns a Matcher object.} do
7
+ it "be_true" do
8
+ be_true.should be_an_instance_of(Be)
9
+ end
10
+ it "be_false" do
11
+ be_false.should be_an_instance_of(Be)
12
+ end
13
+ it "be_nil" do
14
+ be_nil.should be_an_instance_of(Be)
15
+ end
16
+ it "be_arbitrary_predicate" do
17
+ be_arbitrary_predicate.should be_an_instance_of(Be)
18
+ end
19
+ it "change" do
20
+ change("target", :message).should be_an_instance_of(Change)
21
+ end
22
+ it "have" do
23
+ have(0).should be_an_instance_of(Have)
24
+ end
25
+ it "have_exactly" do
26
+ have_exactly(0).should be_an_instance_of(Have)
27
+ end
28
+ it "have_at_least" do
29
+ have_at_least(0).should be_an_instance_of(Have)
30
+ end
31
+ it "have_at_most" do
32
+ have_at_most(0).should be_an_instance_of(Have)
33
+ end
34
+ it "raise_error" do
35
+ raise_error.should be_an_instance_of(RaiseError)
36
+ raise_error(NoMethodError).should be_an_instance_of(RaiseError)
37
+ raise_error(NoMethodError, "message").should be_an_instance_of(RaiseError)
38
+ end
39
+ it "satisfy" do
40
+ satisfy{}.should be_an_instance_of(Satisfy)
41
+ end
42
+ it "throw_symbol" do
43
+ throw_symbol.should be_an_instance_of(ThrowSymbol)
44
+ throw_symbol(:sym).should be_an_instance_of(ThrowSymbol)
45
+ end
46
+ it "respond_to" do
47
+ respond_to(:sym).should be_an_instance_of(RespondTo)
48
+ end
49
+ end
50
+
51
+ describe "Rspec::Matchers#method_missing" do
52
+ it "should convert be_xyz to Be(:be_xyz)" do
53
+ Be.should_receive(:new).with(:be_whatever)
54
+ be_whatever
55
+ end
56
+
57
+ it "should convert have_xyz to Has(:have_xyz)" do
58
+ Has.should_receive(:new).with(:have_whatever)
59
+ have_whatever
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,289 @@
1
+ require 'spec_helper'
2
+
3
+ class UnexpectedError < StandardError; end
4
+
5
+ module Rspec
6
+ module Matchers
7
+ describe Matcher do
8
+ context "without overrides" do
9
+ before(:each) do
10
+ @matcher = Rspec::Matchers::Matcher.new(:be_a_multiple_of, 3) do |multiple|
11
+ match do |actual|
12
+ actual % multiple == 0
13
+ end
14
+ end
15
+ end
16
+
17
+ it "provides a default description" do
18
+ @matcher.description.should == "be a multiple of 3"
19
+ end
20
+
21
+ it "provides a default failure message for #should" do
22
+ @matcher.matches?(8)
23
+ @matcher.failure_message_for_should.should == "expected 8 to be a multiple of 3"
24
+ end
25
+
26
+ it "provides a default failure message for #should_not" do
27
+ @matcher.matches?(9)
28
+ @matcher.failure_message_for_should_not.should == "expected 9 not to be a multiple of 3"
29
+ end
30
+ end
31
+
32
+ it "is not diffable by default" do
33
+ matcher = Rspec::Matchers::Matcher.new(:name) {}
34
+ matcher.should_not be_diffable
35
+ end
36
+
37
+ it "is diffable when told to be" do
38
+ matcher = Rspec::Matchers::Matcher.new(:name) { diffable }
39
+ matcher.should be_diffable
40
+ end
41
+
42
+ it "provides expected" do
43
+ matcher = Rspec::Matchers::Matcher.new(:name, 'expected string') {}
44
+ matcher.expected.should == ['expected string']
45
+ end
46
+
47
+ it "provides actual" do
48
+ matcher = Rspec::Matchers::Matcher.new(:name, 'expected string') do
49
+ match {|actual|}
50
+ end
51
+
52
+ matcher.matches?('actual string')
53
+
54
+ matcher.actual.should == 'actual string'
55
+ end
56
+
57
+ context "wrapping another expectation (should == ...)" do
58
+ it "returns true if the wrapped expectation passes" do
59
+ matcher = Rspec::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 = Rspec::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
+
77
+ context "with overrides" do
78
+ before(:each) do
79
+ @matcher = Rspec::Matchers::Matcher.new(:be_boolean, true) do |boolean|
80
+ match do |actual|
81
+ actual
82
+ end
83
+ description do
84
+ "be the boolean #{boolean}"
85
+ end
86
+ failure_message_for_should do |actual|
87
+ "expected #{actual} to be the boolean #{boolean}"
88
+ end
89
+ failure_message_for_should_not do |actual|
90
+ "expected #{actual} not to be the boolean #{boolean}"
91
+ end
92
+ end
93
+ end
94
+
95
+ it "does not hide result of match block when true" do
96
+ @matcher.matches?(true).should be_true
97
+ end
98
+
99
+ it "does not hide result of match block when false" do
100
+ @matcher.matches?(false).should be_false
101
+ end
102
+
103
+ it "overrides the description" do
104
+ @matcher.description.should == "be the boolean true"
105
+ end
106
+
107
+ it "overrides the failure message for #should" do
108
+ @matcher.matches?(false)
109
+ @matcher.failure_message_for_should.should == "expected false to be the boolean true"
110
+ end
111
+
112
+ it "overrides the failure message for #should_not" do
113
+ @matcher.matches?(true)
114
+ @matcher.failure_message_for_should_not.should == "expected true not to be the boolean true"
115
+ end
116
+ end
117
+
118
+ context "#new" do
119
+ it "passes matches? arg to match block" do
120
+ matcher = Rspec::Matchers::Matcher.new(:ignore) do
121
+ match do |actual|
122
+ actual == 5
123
+ end
124
+ end
125
+ matcher.matches?(5).should be_true
126
+ end
127
+
128
+ it "exposes arg submitted through #new to matcher block" do
129
+ matcher = Rspec::Matchers::Matcher.new(:ignore, 4) do |expected|
130
+ match do |actual|
131
+ actual > expected
132
+ end
133
+ end
134
+ matcher.matches?(5).should be_true
135
+ end
136
+ end
137
+
138
+ context "with no args" do
139
+ before(:each) do
140
+ @matcher = Rspec::Matchers::Matcher.new(:matcher_name) do
141
+ match do |actual|
142
+ actual == 5
143
+ end
144
+ end
145
+ end
146
+
147
+ it "matches" do
148
+ @matcher.matches?(5).should be_true
149
+ end
150
+
151
+ it "describes" do
152
+ @matcher.description.should == "matcher name"
153
+ end
154
+ end
155
+
156
+ context "with 1 arg" do
157
+ before(:each) do
158
+ @matcher = Rspec::Matchers::Matcher.new(:matcher_name, 1) do |expected|
159
+ match do |actual|
160
+ actual == 5 && expected == 1
161
+ end
162
+ end
163
+ end
164
+
165
+ it "matches" do
166
+ @matcher.matches?(5).should be_true
167
+ end
168
+
169
+ it "describes" do
170
+ @matcher.description.should == "matcher name 1"
171
+ end
172
+ end
173
+
174
+ context "with multiple args" do
175
+ before(:each) do
176
+ @matcher = Rspec::Matchers::Matcher.new(:matcher_name, 1, 2, 3, 4) do |a,b,c,d|
177
+ match do |sum|
178
+ a + b + c + d == sum
179
+ end
180
+ end
181
+ end
182
+
183
+ it "matches" do
184
+ @matcher.matches?(10).should be_true
185
+ end
186
+
187
+ it "describes" do
188
+ @matcher.description.should == "matcher name 1, 2, 3, and 4"
189
+ end
190
+ end
191
+
192
+ it "supports helper methods" do
193
+ matcher = Rspec::Matchers::Matcher.new(:be_similar_to, [1,2,3]) do |sample|
194
+ match do |actual|
195
+ similar?(sample, actual)
196
+ end
197
+
198
+ def similar?(a, b)
199
+ a.sort == b.sort
200
+ end
201
+ end
202
+
203
+ matcher.matches?([2,3,1]).should be_true
204
+ end
205
+
206
+ it "supports fluent interface" do
207
+ matcher = Rspec::Matchers::Matcher.new(:first_word) do
208
+ def second_word
209
+ self
210
+ end
211
+ end
212
+
213
+ matcher.second_word.should == matcher
214
+ end
215
+
216
+ it "treats method missing normally for undeclared methods" do
217
+ matcher = Rspec::Matchers::Matcher.new(:ignore) { }
218
+ expect { matcher.non_existent_method }.to raise_error(NoMethodError)
219
+ end
220
+
221
+ it "has access to other matchers" do
222
+ matcher = Rspec::Matchers::Matcher.new(:ignore, 3) do |expected|
223
+ match do |actual|
224
+ extend Rspec::Matchers
225
+ actual.should eql(5 + expected)
226
+ end
227
+ end
228
+
229
+ matcher.matches?(8).should be_true
230
+ end
231
+
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
+ Rspec::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
252
+ end
253
+ it "fails as you would expect" do
254
+ matcher.matches?(5).should be_false
255
+ end
256
+ end
257
+
258
+ context "with an unexpected error" do
259
+ let(:matcher) do
260
+ Rspec::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")
271
+ end
272
+ end
273
+
274
+ end
275
+
276
+ it "can define chainable methods" do
277
+ matcher = Rspec::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
287
+ end
288
+ end
289
+ end