rspec-expectations 2.0.0.beta.22 → 2.6.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.
Files changed (93) hide show
  1. data/.gitignore +1 -1
  2. data/.travis.yml +7 -0
  3. data/Gemfile +35 -13
  4. data/Guardfile +5 -0
  5. data/License.txt +1 -1
  6. data/README.md +43 -0
  7. data/Rakefile +51 -9
  8. data/cucumber.yml +9 -2
  9. data/features/.nav +29 -0
  10. data/features/Changelog.md +101 -0
  11. data/features/README.markdown +45 -8
  12. data/features/built_in_matchers/README.md +71 -0
  13. data/features/built_in_matchers/be.feature +135 -0
  14. data/features/built_in_matchers/be_within.feature +43 -0
  15. data/features/built_in_matchers/cover.feature +45 -0
  16. data/features/{matchers → built_in_matchers}/equality.feature +16 -13
  17. data/features/built_in_matchers/exist.feature +43 -0
  18. data/features/built_in_matchers/expect_change.feature +59 -0
  19. data/features/built_in_matchers/expect_error.feature +105 -0
  20. data/features/built_in_matchers/have.feature +103 -0
  21. data/features/built_in_matchers/include.feature +121 -0
  22. data/features/built_in_matchers/match.feature +50 -0
  23. data/features/built_in_matchers/operators.feature +221 -0
  24. data/features/built_in_matchers/predicates.feature +128 -0
  25. data/features/built_in_matchers/respond_to.feature +78 -0
  26. data/features/built_in_matchers/satisfy.feature +31 -0
  27. data/features/built_in_matchers/throw_symbol.feature +85 -0
  28. data/features/built_in_matchers/types.feature +114 -0
  29. data/features/{matchers → custom_matchers}/access_running_example.feature +2 -2
  30. data/features/{matchers → custom_matchers}/define_diffable_matcher.feature +1 -1
  31. data/features/{matchers → custom_matchers}/define_matcher.feature +91 -9
  32. data/features/{matchers → custom_matchers}/define_matcher_outside_rspec.feature +1 -1
  33. data/features/{matchers → custom_matchers}/define_matcher_with_fluent_interface.feature +8 -11
  34. data/features/customized_message.feature +22 -0
  35. data/features/{expectations/diffing.feature → diffing.feature} +16 -16
  36. data/features/{expectations/implicit_docstrings.feature → implicit_docstrings.feature} +4 -4
  37. data/features/step_definitions/additional_cli_steps.rb +22 -0
  38. data/features/support/env.rb +5 -1
  39. data/features/test_frameworks/test_unit.feature +46 -0
  40. data/lib/rspec/expectations/backward_compatibility.rb +22 -1
  41. data/lib/rspec/expectations/deprecation.rb +36 -0
  42. data/lib/rspec/expectations/extensions.rb +0 -1
  43. data/lib/rspec/expectations/version.rb +1 -1
  44. data/lib/rspec/expectations.rb +2 -1
  45. data/lib/rspec/matchers/be.rb +4 -4
  46. data/lib/rspec/matchers/be_close.rb +2 -17
  47. data/lib/rspec/matchers/be_within.rb +40 -0
  48. data/lib/rspec/matchers/block_aliases.rb +19 -0
  49. data/lib/rspec/matchers/change.rb +74 -49
  50. data/lib/rspec/matchers/cover.rb +35 -0
  51. data/lib/rspec/matchers/exist.rb +13 -3
  52. data/lib/rspec/matchers/has.rb +15 -11
  53. data/lib/rspec/matchers/have.rb +5 -5
  54. data/lib/rspec/matchers/include.rb +14 -5
  55. data/lib/rspec/matchers/matcher.rb +39 -13
  56. data/lib/rspec/matchers/method_missing.rb +6 -3
  57. data/lib/rspec/matchers/operator_matcher.rb +12 -3
  58. data/lib/rspec/matchers/respond_to.rb +23 -9
  59. data/lib/rspec/matchers/satisfy.rb +4 -0
  60. data/lib/rspec/matchers/throw_symbol.rb +43 -26
  61. data/lib/rspec/matchers.rb +15 -3
  62. data/rspec-expectations.gemspec +2 -6
  63. data/spec/rspec/matchers/be_close_spec.rb +11 -39
  64. data/spec/rspec/matchers/be_spec.rb +8 -8
  65. data/spec/rspec/matchers/be_within_spec.rb +64 -0
  66. data/spec/rspec/matchers/change_spec.rb +88 -9
  67. data/spec/rspec/matchers/cover_spec.rb +65 -0
  68. data/spec/rspec/matchers/description_generation_spec.rb +41 -25
  69. data/spec/rspec/matchers/exist_spec.rb +90 -51
  70. data/spec/rspec/matchers/has_spec.rb +2 -2
  71. data/spec/rspec/matchers/have_spec.rb +291 -291
  72. data/spec/rspec/matchers/include_spec.rb +318 -65
  73. data/spec/rspec/matchers/matcher_spec.rb +91 -0
  74. data/spec/rspec/matchers/matchers_spec.rb +29 -0
  75. data/spec/rspec/matchers/method_missing_spec.rb +23 -0
  76. data/spec/rspec/matchers/operator_matcher_spec.rb +36 -10
  77. data/spec/rspec/matchers/respond_to_spec.rb +177 -1
  78. data/spec/rspec/matchers/satisfy_spec.rb +4 -0
  79. data/spec/rspec/matchers/throw_symbol_spec.rb +27 -10
  80. data/spec/spec_helper.rb +10 -35
  81. data/spec/support/classes.rb +1 -1
  82. data/spec/support/matchers.rb +22 -0
  83. data/spec/support/ruby_version.rb +10 -0
  84. metadata +95 -114
  85. data/History.md +0 -16
  86. data/README.markdown +0 -23
  87. data/features/expectations/attribute_of_subject.feature +0 -19
  88. data/features/expectations/customized_message.feature +0 -54
  89. data/features/matchers/expect_change.feature +0 -65
  90. data/features/matchers/expect_error.feature +0 -44
  91. data/lib/rspec/expectations/extensions/rspec/core/example_group.rb +0 -19
  92. data/spec/suite.rb +0 -1
  93. /data/{Upgrade.markdown → features/Upgrade.md} +0 -0
@@ -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_matching(%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_matching(%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_matching(%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_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} 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_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} 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_matching(%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_matching(%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_matching(%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_matching(%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_matching(%Q|expected #{{:key=>"value", :other=>"different"}.inspect} 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_matching(%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_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} to include #{{:a=>2, :b=>2}.inspect}|)
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_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:a=>2, :b=>2}.inspect}|)
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_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:a=>1, :c=>1}.inspect}|)
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_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} to include #{{:c=>1, :d=>1}.inspect}|)
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_matching(%Q|expected ["a", "b"] to include #{{:a=>1, :b=>1}.inspect}|)
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_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
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_matching(%Q|expected #{hash.inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
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_matching(%Q|expected #{{:a=>1, :b=>2}.inspect} not to include #{{:a=>2, :b=>2}.inspect}|)
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_matching(%Q|expected #{{:a=>1, :b=>1}.inspect} not to include #{{:a=>1, :c=>1}.inspect}|)
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_matching(%Q|expected #{["a", {:a=>1, :b=>2}].inspect} not to include #{{:a=>1, :b=>2}.inspect}|)
339
+ end
87
340
  end
88
341
  end
@@ -1,10 +1,56 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class UnexpectedError < StandardError; end
4
+ module MatcherHelperModule
5
+ def self.included(base)
6
+ base.module_eval do
7
+ def included_method; end
8
+ end
9
+ end
10
+
11
+ def self.extended(base)
12
+ base.instance_eval do
13
+ def extended_method; end
14
+ end
15
+ end
16
+
17
+ def greeting
18
+ "Hello, World"
19
+ end
20
+ end
4
21
 
5
22
  module RSpec
6
23
  module Matchers
7
24
  describe Matcher do
25
+ context "with an included module" do
26
+ let(:matcher) do
27
+ RSpec::Matchers::Matcher.new(:be_a_greeting) do
28
+ include MatcherHelperModule
29
+ match { |actual| actual == greeting }
30
+ end
31
+ end
32
+
33
+ it "has access to the module's methods" do
34
+ matcher.matches?("Hello, World")
35
+ end
36
+
37
+ it "runs the module's included hook" do
38
+ matcher.should respond_to(:included_method)
39
+ end
40
+
41
+ it "does not run the module's extended hook" do
42
+ matcher.should_not respond_to(:extended_method)
43
+ end
44
+
45
+ it 'allows multiple modules to be included at once' do
46
+ m = RSpec::Matchers::Matcher.new(:multiple_modules) do
47
+ include Enumerable, Comparable
48
+ end
49
+ m.should be_a(Enumerable)
50
+ m.should be_a(Comparable)
51
+ end
52
+ end
53
+
8
54
  context "without overrides" do
9
55
  before(:each) do
10
56
  @matcher = RSpec::Matchers::Matcher.new(:be_a_multiple_of, 3) do |multiple|
@@ -29,6 +75,43 @@ module RSpec
29
75
  end
30
76
  end
31
77
 
78
+ context "with separate match logic for should and should not" do
79
+ let(:matcher) do
80
+ RSpec::Matchers::Matcher.new(:to_be_composed_of, 7, 11) do |a, b|
81
+ match_for_should do |actual|
82
+ actual == a * b
83
+ end
84
+
85
+ match_for_should_not do |actual|
86
+ actual == a + b
87
+ end
88
+ end
89
+ end
90
+
91
+ it "invokes the match_for_should block for #matches?" do
92
+ matcher.matches?(77).should be_true
93
+ matcher.matches?(18).should be_false
94
+ end
95
+
96
+ it "invokes the match_for_should_not block for #does_not_match?" do
97
+ matcher.does_not_match?(77).should be_false
98
+ matcher.does_not_match?(18).should be_true
99
+ end
100
+
101
+ it "provides a default failure message for #should_not" do
102
+ matcher.does_not_match?(77)
103
+ matcher.failure_message_for_should_not.should == "expected 77 not to to be composed of 7 and 11"
104
+ end
105
+ end
106
+
107
+ it "allows helper methods to be defined with #define_method to have access to matcher parameters" do
108
+ matcher = RSpec::Matchers::Matcher.new(:name, 3, 4) do |a, b|
109
+ define_method(:sum) { a + b }
110
+ end
111
+
112
+ matcher.sum.should == 7
113
+ end
114
+
32
115
  it "is not diffable by default" do
33
116
  matcher = RSpec::Matchers::Matcher.new(:name) {}
34
117
  matcher.should_not be_diffable
@@ -297,6 +380,14 @@ module RSpec
297
380
  matcher.expecting('value').matches?('other value').should be_false
298
381
  end
299
382
 
383
+ it "prevents name collisions on chainable methods from different matchers" do
384
+ m1 = RSpec::Matchers::Matcher.new(:m1) { chain(:foo) { raise "foo in m1" } }
385
+ m2 = RSpec::Matchers::Matcher.new(:m2) { chain(:foo) { raise "foo in m2" } }
386
+
387
+ expect { m1.foo }.to raise_error("foo in m1")
388
+ expect { m2.foo }.to raise_error("foo in m2")
389
+ end
390
+
300
391
  context "defined using the dsl" do
301
392
  def a_method_in_the_example
302
393
  "method defined in the example"
@@ -1,2 +1,31 @@
1
1
  require 'spec_helper'
2
2
 
3
+ module RSpec
4
+ describe Matchers do
5
+
6
+ let(:sample_matchers) do
7
+ [:be,
8
+ :be_close,
9
+ :be_instance_of,
10
+ :be_kind_of]
11
+ end
12
+
13
+ context "once required" do
14
+ it "includes itself in Test::Unit::TestCase" do
15
+ test_unit_case = Test::Unit::TestCase.allocate
16
+ sample_matchers.each do |sample_matcher|
17
+ test_unit_case.should respond_to(sample_matcher)
18
+ end
19
+ end
20
+
21
+ it "includes itself in MiniTest::Unit::TestCase", :if => defined?(MiniTest) do
22
+ minitest_case = MiniTest::Unit::TestCase.allocate
23
+ sample_matchers.each do |sample_matcher|
24
+ minitest_case.should respond_to(sample_matcher)
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ shared_examples_for "a well-behaved method_missing hook" do
4
+ it "raises a NoMethodError (and not SystemStackError) for an undefined method" do
5
+ expect { subject.some_undefined_method }.to raise_error(NoMethodError)
6
+ end
7
+ end
8
+
9
+ describe "RSpec::Matchers method_missing hook" do
10
+ subject { self }
11
+ it_behaves_like "a well-behaved method_missing hook"
12
+
13
+ context 'when invoked in a Test::Unit::TestCase' do
14
+ subject { Test::Unit::TestCase.allocate }
15
+ it_behaves_like "a well-behaved method_missing hook"
16
+ end
17
+
18
+ context 'when invoked in a MiniTest::Unit::TestCase', :if => defined?(MiniTest) do
19
+ subject { MiniTest::Unit::TestCase.allocate }
20
+ it_behaves_like "a well-behaved method_missing hook"
21
+ end
22
+ end
23
+