rspec-expectations 2.8.0 → 2.9.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/.document +5 -0
  2. data/.yardopts +3 -0
  3. data/Changelog.md +176 -0
  4. data/README.md +2 -13
  5. data/features/custom_matchers/access_running_example.feature +1 -1
  6. data/features/step_definitions/additional_cli_steps.rb +4 -4
  7. data/lib/rspec/expectations/fail_with.rb +3 -3
  8. data/lib/rspec/expectations/handler.rb +3 -5
  9. data/lib/rspec/expectations/version.rb +1 -1
  10. data/lib/rspec/matchers.rb +387 -21
  11. data/lib/rspec/matchers/built_in.rb +33 -0
  12. data/lib/rspec/matchers/built_in/base_matcher.rb +58 -0
  13. data/lib/rspec/matchers/built_in/be.rb +183 -0
  14. data/lib/rspec/matchers/built_in/be_instance_of.rb +13 -0
  15. data/lib/rspec/matchers/built_in/be_kind_of.rb +13 -0
  16. data/lib/rspec/matchers/built_in/be_within.rb +39 -0
  17. data/lib/rspec/matchers/built_in/change.rb +132 -0
  18. data/lib/rspec/matchers/built_in/cover.rb +22 -0
  19. data/lib/rspec/matchers/built_in/eq.rb +26 -0
  20. data/lib/rspec/matchers/built_in/eql.rb +25 -0
  21. data/lib/rspec/matchers/built_in/equal.rb +48 -0
  22. data/lib/rspec/matchers/built_in/exist.rb +28 -0
  23. data/lib/rspec/matchers/built_in/has.rb +47 -0
  24. data/lib/rspec/matchers/built_in/have.rb +107 -0
  25. data/lib/rspec/matchers/built_in/include.rb +52 -0
  26. data/lib/rspec/matchers/built_in/match.rb +13 -0
  27. data/lib/rspec/matchers/built_in/match_array.rb +52 -0
  28. data/lib/rspec/matchers/built_in/raise_error.rb +96 -0
  29. data/lib/rspec/matchers/built_in/respond_to.rb +73 -0
  30. data/lib/rspec/matchers/built_in/satisfy.rb +29 -0
  31. data/lib/rspec/matchers/built_in/throw_symbol.rb +93 -0
  32. data/lib/rspec/matchers/dsl.rb +1 -1
  33. data/lib/rspec/matchers/matcher.rb +263 -233
  34. data/lib/rspec/matchers/method_missing.rb +2 -2
  35. data/lib/rspec/matchers/operator_matcher.rb +19 -20
  36. data/spec/rspec/expectations/handler_spec.rb +1 -1
  37. data/spec/rspec/matchers/base_matcher_spec.rb +1 -2
  38. data/spec/rspec/matchers/change_spec.rb +3 -3
  39. data/spec/rspec/matchers/cover_spec.rb +46 -46
  40. data/spec/rspec/matchers/dsl_spec.rb +36 -3
  41. data/spec/rspec/matchers/have_spec.rb +2 -2
  42. data/spec/rspec/matchers/include_spec.rb +1 -1
  43. data/spec/rspec/matchers/matcher_spec.rb +319 -305
  44. data/spec/rspec/matchers/method_missing_spec.rb +1 -0
  45. data/spec/rspec/matchers/operator_matcher_spec.rb +2 -2
  46. data/spec/rspec/matchers/throw_symbol_spec.rb +103 -105
  47. metadata +93 -39
  48. data/lib/rspec/matchers/base_matcher.rb +0 -56
  49. data/lib/rspec/matchers/be.rb +0 -232
  50. data/lib/rspec/matchers/be_instance_of.rb +0 -24
  51. data/lib/rspec/matchers/be_kind_of.rb +0 -24
  52. data/lib/rspec/matchers/be_within.rb +0 -47
  53. data/lib/rspec/matchers/change.rb +0 -197
  54. data/lib/rspec/matchers/cover.rb +0 -36
  55. data/lib/rspec/matchers/eq.rb +0 -36
  56. data/lib/rspec/matchers/eql.rb +0 -35
  57. data/lib/rspec/matchers/equal.rb +0 -58
  58. data/lib/rspec/matchers/errors.rb +0 -5
  59. data/lib/rspec/matchers/exist.rb +0 -34
  60. data/lib/rspec/matchers/has.rb +0 -44
  61. data/lib/rspec/matchers/have.rb +0 -162
  62. data/lib/rspec/matchers/include.rb +0 -66
  63. data/lib/rspec/matchers/match.rb +0 -21
  64. data/lib/rspec/matchers/match_array.rb +0 -65
  65. data/lib/rspec/matchers/raise_error.rb +0 -116
  66. data/lib/rspec/matchers/respond_to.rb +0 -80
  67. data/lib/rspec/matchers/satisfy.rb +0 -46
  68. data/lib/rspec/matchers/throw_symbol.rb +0 -112
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ -
3
+ README.md
4
+ License.txt
5
+ Changelog.md
@@ -0,0 +1,3 @@
1
+ --no-private
2
+ --exclude features
3
+ --markup markdown
@@ -0,0 +1,176 @@
1
+ ### 2.9.0.rc2 / 2012-03-12
2
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0...v2.9.0.rc2)
3
+
4
+ Enhancements
5
+
6
+ * Move built-in matcher classes to RSpec::Matchers::BuiltIn to reduce pollution
7
+ of RSpec::Matchers (which is included in every example).
8
+ * Autoload files with matcher classes to improve load time.
9
+
10
+ Bug fixes
11
+
12
+ * Align respond_to? and method_missing in DSL-defined matchers.
13
+ * Clear out user-defined instance variables between invocations of DSL-defined
14
+ matchers.
15
+ * Dup the instance of a DSL generated matcher so its state is not changed by
16
+ subsequent invocations.
17
+ * Treat expected args consistently across positive and negative expectations
18
+ (thanks to Ralf Kistner for the heads up)
19
+
20
+ ### 2.8.0 / 2012-01-04
21
+
22
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0.rc2...v2.8.0)
23
+
24
+ Enhancements
25
+
26
+ * Better diff output for Hash (Philippe Creux)
27
+ * Eliminate Ruby warnings (Olek Janiszewski)
28
+
29
+ ### 2.8.0.rc2 / 2011-12-19
30
+
31
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.8.0.rc1...v2.8.0.rc2)
32
+
33
+ No changes for this release. Just releasing with the other rspec gems.
34
+
35
+ ### 2.8.0.rc1 / 2011-11-06
36
+
37
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.7.0...v2.8.0.rc1)
38
+
39
+ Enhancements
40
+
41
+ * Use classes for the built-in matchers (they're faster).
42
+ * Eliminate Ruby warnings (Matijs van Zuijlen)
43
+
44
+ ### 2.7.0 / 2011-10-16
45
+
46
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.6.0...v2.7.0)
47
+
48
+ Enhancements
49
+
50
+ * `HaveMatcher` converts argument using `to_i` (Alex Bepple & Pat Maddox)
51
+ * Improved failure message for the `have_xxx` matcher (Myron Marston)
52
+ * `HaveMatcher` supports `count` (Matthew Bellantoni)
53
+ * Change matcher dups `Enumerable` before the action, supporting custom
54
+ `Enumerable` types like `CollectionProxy` in Rails (David Chelimsky)
55
+
56
+ Bug fixes
57
+
58
+ * Fix typo in `have(n).xyz` documentation (Jean Boussier)
59
+ * fix `safe_sort` for ruby 1.9.2 (`Kernel` now defines `<=>` for Object) (Peter
60
+ van Hardenberg)
61
+
62
+ ### 2.6.0 / 2011-05-12
63
+
64
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.5.0...v2.6.0)
65
+
66
+ Enhancements
67
+
68
+ * `change` matcher accepts regexps (Robert Davis)
69
+ * better descriptions for `have_xxx` matchers (Magnus Bergmark)
70
+ * `range.should cover(*values)` (Anders Furseth)
71
+
72
+ Bug fixes
73
+
74
+ * Removed non-ascii characters that were choking rcov (Geoffrey Byers)
75
+ * change matcher dups arrays and hashes so their before/after states can be
76
+ compared correctly.
77
+ * Fix the order of inclusion of RSpec::Matchers in Test::Unit::TestCase and
78
+ MiniTest::Unit::TestCase to prevent a SystemStackError (Myron Marston)
79
+
80
+ ### 2.5.0 / 2011-02-05
81
+
82
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.4.0...v2.5.0)
83
+
84
+ Enhancements
85
+
86
+ * `should exist` works with `exist?` or `exists?` (Myron Marston)
87
+ * `expect { ... }.not_to do_something` (in addition to `to_not`)
88
+
89
+ Documentation
90
+
91
+ * improved docs for raise_error matcher (James Almond)
92
+
93
+ ### 2.4.0 / 2011-01-02
94
+
95
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.3.0...v2.4.0)
96
+
97
+ No functional changes in this release, which was made to align with the
98
+ rspec-core-2.4.0 release.
99
+
100
+ Enhancements
101
+
102
+ * improved RDoc for change matcher (Jo Liss)
103
+
104
+ ### 2.3.0 / 2010-12-12
105
+
106
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.2.1...v2.3.0)
107
+
108
+ Enhancements
109
+
110
+ * diff strings when include matcher fails (Mike Sassak)
111
+
112
+ ### 2.2.0 / 2010-11-28
113
+
114
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.1.0...v2.2.0)
115
+
116
+ ### 2.1.0 / 2010-11-07
117
+
118
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.1...v2.1.0)
119
+
120
+ Enhancements
121
+
122
+ * `be_within(delta).of(expected)` matcher (Myron Marston)
123
+ * Lots of new Cucumber features (Myron Marston)
124
+ * Raise error if you try `should != expected` on Ruby-1.9 (Myron Marston)
125
+ * Improved failure messages from `throw_symbol` (Myron Marston)
126
+
127
+ Bug fixes
128
+
129
+ * Eliminate hard dependency on `RSpec::Core` (Myron Marston)
130
+ * `have_matcher` - use pluralize only when ActiveSupport inflections are indeed
131
+ defined (Josep M Bach)
132
+ * throw_symbol matcher no longer swallows exceptions (Myron Marston)
133
+ * fix matcher chaining to avoid name collisions (Myron Marston)
134
+
135
+ ### 2.0.0 / 2010-10-10
136
+
137
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0.rc...v2.0.0)
138
+
139
+ Enhancements
140
+
141
+ * Add match_for_should_not method to matcher DSL (Myron Marston)
142
+
143
+ Bug fixes
144
+
145
+ * `respond_to` matcher works correctly with `should_not` with multiple methods
146
+ (Myron Marston)
147
+ * `include` matcher works correctly with `should_not` with multiple values
148
+ (Myron Marston)
149
+
150
+ ### 2.0.0.rc / 2010-10-05
151
+
152
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0.beta.22...v2.0.0.rc)
153
+
154
+ Enhancements
155
+
156
+ * `require 'rspec/expectations'` in a T::U or MiniUnit suite (Josep M. Bach)
157
+
158
+ Bug fixes
159
+
160
+ * change by 0 passes/fails correctly (Len Smith)
161
+ * Add description to satisfy matcher
162
+
163
+ ### 2.0.0.beta.22 / 2010-09-12
164
+
165
+ [full changelog](http://github.com/rspec/rspec-expectations/compare/v2.0.0.beta.20...v2.0.0.beta.22)
166
+
167
+ Enhancements
168
+
169
+ * diffing improvements
170
+ * diff multiline strings
171
+ * don't diff single line strings
172
+ * don't diff numbers (silly)
173
+ * diff regexp + multiline string
174
+
175
+ Bug fixes
176
+ * `should[_not]` change now handles boolean values correctly
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # RSpec Expectations
2
2
 
3
- [RSpec::Expectations](../RSpec/Expectations) lets you express expected outcomes
4
- on an object in an example.
3
+ RSpec::Expectations lets you express expected outcomes on an object in an
4
+ example.
5
5
 
6
6
  account.balance.should eq(Money.new(37.42, :USD))
7
7
 
@@ -119,8 +119,6 @@ actual.should be_xxx # passes if actual.xxx?
119
119
  actual.should have_xxx(:arg) # passes if actual.has_xxx?(:arg)
120
120
  ```
121
121
 
122
- See [RSpec::Matchers](../RSpec/Matchers) for more about predicate matchers.
123
-
124
122
  ### Ranges (Ruby >= 1.9 only)
125
123
 
126
124
  ```ruby
@@ -142,15 +140,6 @@ actual.should include(expected)
142
140
  "this string".should include("is str")
143
141
  ```
144
142
 
145
- ## Learn more
146
-
147
- See [RSpec::Expectations](../RSpec/Expectations) for more information about
148
- `should` and `should_not` and how they work.
149
-
150
- See [RSpec::Matchers](../RSpec/Matchers) for more information about the
151
- built-in matchers that ship with rspec-expectations, and how to write your own
152
- custom matchers.
153
-
154
143
  ## Also see
155
144
 
156
145
  * [http://github.com/rspec/rspec](http://github.com/rspec/rspec)
@@ -49,5 +49,5 @@ Feature: access running example
49
49
  When I run `rspec ./example_spec.rb`
50
50
  Then the output should contain "1 example, 1 failure"
51
51
  And the output should contain "undefined local variable"
52
- And the output should contain "RSpec::Matchers::Matcher"
52
+ And the output should contain "RSpec::Matchers::DSL::Matcher"
53
53
  And the output should not contain "ExampleGroup"
@@ -12,11 +12,11 @@ Then /^the output should contain all of these:$/ do |table|
12
12
  end
13
13
 
14
14
  Then /^the example(?:s)? should(?: all)? pass$/ do
15
- Then %q{the output should contain "0 failures"}
16
- Then %q{the exit status should be 0}
15
+ step %q{the output should contain "0 failures"}
16
+ step %q{the exit status should be 0}
17
17
  end
18
18
 
19
19
  Then /^the example should fail$/ do
20
- Then %q{the output should contain "1 failure"}
21
- Then %q{the exit status should not be 0}
20
+ step %q{the output should contain "1 failure"}
21
+ step %q{the exit status should not be 0}
22
22
  end
@@ -35,11 +35,11 @@ module RSpec
35
35
  private
36
36
 
37
37
  def no_procs?(*args)
38
- args.none? {|a| Proc === a}
38
+ args.flatten.none? {|a| Proc === a}
39
39
  end
40
40
 
41
41
  def all_strings?(*args)
42
- args.all? {|a| String === a}
42
+ args.flatten.all? {|a| String === a}
43
43
  end
44
44
 
45
45
  def any_multiline_strings?(*args)
@@ -47,7 +47,7 @@ module RSpec
47
47
  end
48
48
 
49
49
  def no_numbers?(*args)
50
- args.none? {|a| Numeric === a}
50
+ args.flatten.none? {|a| Numeric === a}
51
51
  end
52
52
  end
53
53
  end
@@ -1,12 +1,10 @@
1
1
  module RSpec
2
2
  module Expectations
3
- class InvalidMatcherError < ArgumentError; end
4
-
5
3
  class PositiveExpectationHandler
6
4
  def self.handle_matcher(actual, matcher, message=nil, &block)
7
5
  ::RSpec::Matchers.last_should = :should
8
6
  ::RSpec::Matchers.last_matcher = matcher
9
- return ::RSpec::Matchers::PositiveOperatorMatcher.new(actual) if matcher.nil?
7
+ return ::RSpec::Matchers::BuiltIn::PositiveOperatorMatcher.new(actual) if matcher.nil?
10
8
 
11
9
  match = matcher.matches?(actual, &block)
12
10
  return match if match
@@ -27,7 +25,7 @@ module RSpec
27
25
  def self.handle_matcher(actual, matcher, message=nil, &block)
28
26
  ::RSpec::Matchers.last_should = :should_not
29
27
  ::RSpec::Matchers.last_matcher = matcher
30
- return ::RSpec::Matchers::NegativeOperatorMatcher.new(actual) if matcher.nil?
28
+ return ::RSpec::Matchers::BuiltIn::NegativeOperatorMatcher.new(actual) if matcher.nil?
31
29
 
32
30
  match = matcher.respond_to?(:does_not_match?) ?
33
31
  !matcher.does_not_match?(actual, &block) :
@@ -39,7 +37,7 @@ module RSpec
39
37
  matcher.negative_failure_message
40
38
 
41
39
  if matcher.respond_to?(:diffable?) && matcher.diffable?
42
- ::RSpec::Expectations.fail_with message, matcher.expected.first, matcher.actual
40
+ ::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual
43
41
  else
44
42
  ::RSpec::Expectations.fail_with message
45
43
  end
@@ -2,7 +2,7 @@ module RSpec
2
2
  module Expectations
3
3
  # @private
4
4
  module Version
5
- STRING = '2.8.0'
5
+ STRING = '2.9.0.rc2'
6
6
  end
7
7
  end
8
8
  end
@@ -175,32 +175,398 @@ end
175
175
 
176
176
  require 'rspec/matchers/extensions/instance_eval_with_args'
177
177
  require 'rspec/matchers/pretty'
178
- require 'rspec/matchers/base_matcher'
178
+
179
+ require 'rspec/matchers/built_in'
179
180
  require 'rspec/matchers/matcher'
180
181
  require 'rspec/matchers/operator_matcher'
181
- require 'rspec/matchers/be'
182
182
  require 'rspec/matchers/be_close'
183
- require 'rspec/matchers/be_instance_of'
184
- require 'rspec/matchers/be_kind_of'
185
- require 'rspec/matchers/be_within'
183
+
186
184
  require 'rspec/matchers/block_aliases'
187
- require 'rspec/matchers/change'
188
- require 'rspec/matchers/cover' if (1..2).respond_to? :cover?
189
- require 'rspec/matchers/eq'
190
- require 'rspec/matchers/eql'
191
- require 'rspec/matchers/equal'
192
- require 'rspec/matchers/errors'
193
- require 'rspec/matchers/exist'
194
185
  require 'rspec/matchers/generated_descriptions'
195
- require 'rspec/matchers/has'
196
- require 'rspec/matchers/have'
197
- require 'rspec/matchers/include'
198
- require 'rspec/matchers/match'
199
- require 'rspec/matchers/match_array'
200
186
  require 'rspec/matchers/method_missing'
201
- require 'rspec/matchers/raise_error'
202
- require 'rspec/matchers/respond_to'
203
- require 'rspec/matchers/satisfy'
204
- require 'rspec/matchers/throw_symbol'
205
187
  require 'rspec/matchers/compatibility'
206
188
  require 'rspec/matchers/dsl'
189
+
190
+ module RSpec
191
+ module Matchers
192
+
193
+ # Passes if actual is truthy (anything but false or nil)
194
+ def be_true
195
+ BuiltIn::BeTrue.new
196
+ end
197
+
198
+ # Passes if actual is falsy (false or nil)
199
+ def be_false
200
+ BuiltIn::BeFalse.new
201
+ end
202
+
203
+ # Passes if actual is nil
204
+ def be_nil
205
+ BuiltIn::BeNil.new
206
+ end
207
+
208
+ # @example
209
+ # actual.should be_true
210
+ # actual.should be_false
211
+ # actual.should be_nil
212
+ # actual.should be_[arbitrary_predicate](*args)
213
+ # actual.should_not be_nil
214
+ # actual.should_not be_[arbitrary_predicate](*args)
215
+ #
216
+ # Given true, false, or nil, will pass if actual value is true, false or
217
+ # nil (respectively). Given no args means the caller should satisfy an if
218
+ # condition (to be or not to be).
219
+ #
220
+ # Predicates are any Ruby method that ends in a "?" and returns true or
221
+ # false. Given be_ followed by arbitrary_predicate (without the "?"),
222
+ # RSpec will match convert that into a query against the target object.
223
+ #
224
+ # The arbitrary_predicate feature will handle any predicate prefixed with
225
+ # "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) or "be_"
226
+ # (e.g. be_empty), letting you choose the prefix that best suits the
227
+ # predicate.
228
+ def be(*args)
229
+ args.empty? ?
230
+ Matchers::BuiltIn::Be.new : equal(*args)
231
+ end
232
+
233
+ # passes if target.kind_of?(klass)
234
+ def be_a(klass)
235
+ be_a_kind_of(klass)
236
+ end
237
+
238
+ alias_method :be_an, :be_a
239
+
240
+ # Passes if actual.instance_of?(expected)
241
+ #
242
+ # @example
243
+ #
244
+ # 5.should be_instance_of(Fixnum)
245
+ # 5.should_not be_instance_of(Numeric)
246
+ # 5.should_not be_instance_of(Float)
247
+ def be_an_instance_of(expected)
248
+ BuiltIn::BeAnInstanceOf.new(expected)
249
+ end
250
+
251
+ alias_method :be_instance_of, :be_an_instance_of
252
+
253
+ # Passes if actual.kind_of?(expected)
254
+ #
255
+ # @example
256
+ #
257
+ # 5.should be_kind_of(Fixnum)
258
+ # 5.should be_kind_of(Numeric)
259
+ # 5.should_not be_kind_of(Float)
260
+ def be_a_kind_of(expected)
261
+ BuiltIn::BeAKindOf.new(expected)
262
+ end
263
+
264
+ alias_method :be_kind_of, :be_a_kind_of
265
+
266
+ # Passes if actual == expected +/- delta
267
+ #
268
+ # @example
269
+ #
270
+ # result.should be_within(0.5).of(3.0)
271
+ # result.should_not be_within(0.5).of(3.0)
272
+ def be_within(delta)
273
+ BuiltIn::BeWithin.new(delta)
274
+ end
275
+
276
+ # Applied to a proc, specifies that its execution will cause some value to
277
+ # change.
278
+ #
279
+ # @param [Object] receiver
280
+ # @param [Symbol] message the message to send the receiver
281
+ #
282
+ # You can either pass <tt>receiver</tt> and <tt>message</tt>, or a block,
283
+ # but not both.
284
+ #
285
+ # When passing a block, it must use the <tt>{ ... }</tt> format, not
286
+ # do/end, as <tt>{ ... }</tt> binds to the +change+ method, whereas do/end
287
+ # would errantly bind to the +should+ or +should_not+ method.
288
+ #
289
+ # @example
290
+ #
291
+ # lambda {
292
+ # team.add_player(player)
293
+ # }.should change(roster, :count)
294
+ #
295
+ # lambda {
296
+ # team.add_player(player)
297
+ # }.should change(roster, :count).by(1)
298
+ #
299
+ # lambda {
300
+ # team.add_player(player)
301
+ # }.should change(roster, :count).by_at_least(1)
302
+ #
303
+ # lambda {
304
+ # team.add_player(player)
305
+ # }.should change(roster, :count).by_at_most(1)
306
+ #
307
+ # string = "string"
308
+ # lambda {
309
+ # string.reverse!
310
+ # }.should change { string }.from("string").to("gnirts")
311
+ #
312
+ # lambda {
313
+ # person.happy_birthday
314
+ # }.should change(person, :birthday).from(32).to(33)
315
+ #
316
+ # lambda {
317
+ # employee.develop_great_new_social_networking_app
318
+ # }.should change(employee, :title).from("Mail Clerk").to("CEO")
319
+ #
320
+ # lambda {
321
+ # doctor.leave_office
322
+ # }.should change(doctor, :sign).from(/is in/).to(/is out/)
323
+ #
324
+ # user = User.new(:type => "admin")
325
+ # lambda {
326
+ # user.symbolize_type
327
+ # }.should change(user, :type).from(String).to(Symbol)
328
+ #
329
+ # == Notes
330
+ #
331
+ # Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after it
332
+ # evaluates the proc object (generated by the lambdas in the examples
333
+ # above).
334
+ #
335
+ # <tt>should_not change</tt> only supports the form with no subsequent
336
+ # calls to <tt>by</tt>, <tt>by_at_least</tt>, <tt>by_at_most</tt>,
337
+ # <tt>to</tt> or <tt>from</tt>.
338
+ def change(receiver=nil, message=nil, &block)
339
+ BuiltIn::Change.new(receiver, message, &block)
340
+ end
341
+
342
+ # Passes if actual covers expected. This works for
343
+ # Ranges. You can also pass in multiple args
344
+ # and it will only pass if all args are found in Range.
345
+ #
346
+ # @example
347
+ # (1..10).should cover(5)
348
+ # (1..10).should cover(4, 6)
349
+ # (1..10).should cover(4, 6, 11) # will fail
350
+ # (1..10).should_not cover(11)
351
+ # (1..10).should_not cover(5) # will fail
352
+ #
353
+ # ### Warning:: Ruby >= 1.9 only
354
+ def cover(*values)
355
+ BuiltIn::Cover.new(*values)
356
+ end if (1..2).respond_to?(:cover?)
357
+
358
+ # Passes if <tt>actual == expected</tt>.
359
+ #
360
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
361
+ #
362
+ # @example
363
+ #
364
+ # 5.should eq(5)
365
+ # 5.should_not eq(3)
366
+ def eq(expected)
367
+ BuiltIn::Eq.new(expected)
368
+ end
369
+
370
+ # Passes if +actual.eql?(expected)+
371
+ #
372
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
373
+ #
374
+ # @example
375
+ #
376
+ # 5.should eql(5)
377
+ # 5.should_not eql(3)
378
+ def eql(expected)
379
+ BuiltIn::Eql.new(expected)
380
+ end
381
+
382
+ # Passes if <tt>actual.equal?(expected)</tt> (object identity).
383
+ #
384
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
385
+ #
386
+ # @example
387
+ #
388
+ # 5.should equal(5) # Fixnums are equal
389
+ # "5".should_not equal("5") # Strings that look the same are not the same object
390
+ def equal(expected)
391
+ BuiltIn::Equal.new(expected)
392
+ end
393
+
394
+ # Passes if `actual.exist?` or `actual.exists?`
395
+ #
396
+ # @example
397
+ # File.should exist("path/to/file")
398
+ def exist(*args)
399
+ BuiltIn::Exist.new(*args)
400
+ end
401
+
402
+ # Passes if receiver is a collection with the submitted number of items OR
403
+ # if the receiver OWNS a collection with the submitted number of items.
404
+ #
405
+ # If the receiver OWNS the collection, you must use the name of the
406
+ # collection. So if a `Team` instance has a collection named `#players`,
407
+ # you must use that name to set the expectation.
408
+ #
409
+ # If the receiver IS the collection, you can use any name you like for
410
+ # `named_collection`. We'd recommend using either "elements", "members", or
411
+ # "items" as these are all standard ways of describing the things IN a
412
+ # collection.
413
+ #
414
+ # This also works for Strings, letting you set expectations about their
415
+ # lengths.
416
+ #
417
+ # @example
418
+ #
419
+ # # Passes if team.players.size == 11
420
+ # team.should have(11).players
421
+ #
422
+ # # Passes if [1,2,3].length == 3
423
+ # [1,2,3].should have(3).items #"items" is pure sugar
424
+ #
425
+ # # Passes if ['a', 'b', 'c'].count == 3
426
+ # [1,2,3].should have(3).items #"items" is pure sugar
427
+ #
428
+ # # Passes if "this string".length == 11
429
+ # "this string".should have(11).characters #"characters" is pure sugar
430
+ def have(n)
431
+ BuiltIn::Have.new(n)
432
+ end
433
+ alias :have_exactly :have
434
+
435
+ # Exactly like have() with >=.
436
+ #
437
+ # @example
438
+ # "this".should have_at_least(3).letters
439
+ #
440
+ # ### Warning:
441
+ #
442
+ # `should_not have_at_least` is not supported
443
+ def have_at_least(n)
444
+ BuiltIn::Have.new(n, :at_least)
445
+ end
446
+
447
+ # Exactly like have() with <=.
448
+ #
449
+ # @example
450
+ # should have_at_most(number).items
451
+ #
452
+ # ### Warning:
453
+ #
454
+ # `should_not have_at_most` is not supported
455
+ def have_at_most(n)
456
+ BuiltIn::Have.new(n, :at_most)
457
+ end
458
+
459
+ # Passes if actual includes expected. This works for
460
+ # collections and Strings. You can also pass in multiple args
461
+ # and it will only pass if all args are found in collection.
462
+ #
463
+ # @example
464
+ #
465
+ # [1,2,3].should include(3)
466
+ # [1,2,3].should include(2,3) #would pass
467
+ # [1,2,3].should include(2,3,4) #would fail
468
+ # [1,2,3].should_not include(4)
469
+ # "spread".should include("read")
470
+ # "spread".should_not include("red")
471
+ def include(*expected)
472
+ BuiltIn::Include.new(*expected)
473
+ end
474
+
475
+ # Given a Regexp or String, passes if actual.match(pattern)
476
+ #
477
+ # @example
478
+ #
479
+ # email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
480
+ # email.should match("@example.com")
481
+ def match(expected)
482
+ BuiltIn::Match.new(expected)
483
+ end
484
+
485
+ # With no args, matches if any error is raised.
486
+ # With a named error, matches only if that specific error is raised.
487
+ # With a named error and messsage specified as a String, matches only if both match.
488
+ # With a named error and messsage specified as a Regexp, matches only if both match.
489
+ # Pass an optional block to perform extra verifications on the exception matched
490
+ #
491
+ # @example
492
+ #
493
+ # lambda { do_something_risky }.should raise_error
494
+ # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
495
+ # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 }
496
+ # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
497
+ # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)
498
+ #
499
+ # lambda { do_something_risky }.should_not raise_error
500
+ # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
501
+ # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
502
+ # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/)
503
+ def raise_error(error=Exception, message=nil, &block)
504
+ BuiltIn::RaiseError.new(error, message, &block)
505
+ end
506
+
507
+ alias_method :raise_exception, :raise_error
508
+
509
+ # Matches if the target object responds to all of the names
510
+ # provided. Names can be Strings or Symbols.
511
+ #
512
+ # @example
513
+ #
514
+ def respond_to(*names)
515
+ BuiltIn::RespondTo.new(*names)
516
+ end
517
+
518
+ # Passes if the submitted block returns true. Yields target to the
519
+ # block.
520
+ #
521
+ # Generally speaking, this should be thought of as a last resort when
522
+ # you can't find any other way to specify the behaviour you wish to
523
+ # specify.
524
+ #
525
+ # If you do find yourself in such a situation, you could always write
526
+ # a custom matcher, which would likely make your specs more expressive.
527
+ #
528
+ # @example
529
+ #
530
+ # 5.should satisfy { |n|
531
+ # n > 3
532
+ # }
533
+ def satisfy(&block)
534
+ BuiltIn::Satisfy.new(&block)
535
+ end
536
+
537
+ # Given no argument, matches if a proc throws any Symbol.
538
+ #
539
+ # Given a Symbol, matches if the given proc throws the specified Symbol.
540
+ #
541
+ # Given a Symbol and an arg, matches if the given proc throws the
542
+ # specified Symbol with the specified arg.
543
+ #
544
+ # @example
545
+ #
546
+ # lambda { do_something_risky }.should throw_symbol
547
+ # lambda { do_something_risky }.should throw_symbol(:that_was_risky)
548
+ # lambda { do_something_risky }.should throw_symbol(:that_was_risky, culprit)
549
+ #
550
+ # lambda { do_something_risky }.should_not throw_symbol
551
+ # lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
552
+ # lambda { do_something_risky }.should_not throw_symbol(:that_was_risky, culprit)
553
+ def throw_symbol(expected_symbol=nil, expected_arg=nil)
554
+ BuiltIn::ThrowSymbol.new(expected_symbol, expected_arg)
555
+ end
556
+
557
+ # Passes if actual contains all of the expected regardless of order.
558
+ # This works for collections. Pass in multiple args and it will only
559
+ # pass if all args are found in collection.
560
+ #
561
+ # NOTE: there is no should_not version of array.should =~ other_array
562
+ #
563
+ # @example
564
+ #
565
+ # [1,2,3].should =~ [1,2,3] # => would pass
566
+ # [1,2,3].should =~ [2,3,1] # => would pass
567
+ # [1,2,3,4].should =~ [1,2,3] # => would fail
568
+ # [1,2,2,3].should =~ [1,2,3] # => would fail
569
+ # [1,2,3].should =~ [1,2,3,4] # => would fail
570
+ OperatorMatcher.register(Array, '=~', BuiltIn::MatchArray)
571
+ end
572
+ end