rspec-expectations 2.13.0 → 2.14.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 (64) hide show
  1. data/Changelog.md +46 -0
  2. data/README.md +43 -87
  3. data/features/README.md +8 -9
  4. data/features/built_in_matchers/README.md +41 -41
  5. data/features/built_in_matchers/be_within.feature +3 -3
  6. data/features/built_in_matchers/expect_change.feature +6 -6
  7. data/features/built_in_matchers/expect_error.feature +2 -2
  8. data/features/built_in_matchers/start_with.feature +1 -1
  9. data/features/built_in_matchers/throw_symbol.feature +11 -11
  10. data/features/built_in_matchers/yield.feature +18 -3
  11. data/features/custom_matchers/define_diffable_matcher.feature +1 -1
  12. data/features/custom_matchers/define_matcher_outside_rspec.feature +6 -6
  13. data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
  14. data/features/customized_message.feature +1 -1
  15. data/features/support/env.rb +10 -1
  16. data/features/syntax_configuration.feature +3 -0
  17. data/features/test_frameworks/test_unit.feature +15 -17
  18. data/lib/rspec/expectations/deprecation.rb +12 -33
  19. data/lib/rspec/expectations/differ.rb +25 -7
  20. data/lib/rspec/expectations/expectation_target.rb +7 -8
  21. data/lib/rspec/expectations/extensions/object.rb +2 -12
  22. data/lib/rspec/expectations/fail_with.rb +11 -1
  23. data/lib/rspec/expectations/handler.rb +11 -6
  24. data/lib/rspec/expectations/syntax.rb +2 -2
  25. data/lib/rspec/expectations/version.rb +1 -1
  26. data/lib/rspec/expectations.rb +1 -1
  27. data/lib/rspec/matchers/be_close.rb +1 -1
  28. data/lib/rspec/matchers/built_in/be.rb +2 -0
  29. data/lib/rspec/matchers/built_in/be_within.rb +1 -1
  30. data/lib/rspec/matchers/built_in/change.rb +9 -1
  31. data/lib/rspec/matchers/built_in/have.rb +20 -4
  32. data/lib/rspec/matchers/built_in/include.rb +2 -10
  33. data/lib/rspec/matchers/built_in/raise_error.rb +23 -7
  34. data/lib/rspec/matchers/built_in/yield.rb +78 -3
  35. data/lib/rspec/matchers/operator_matcher.rb +1 -1
  36. data/lib/rspec/matchers/pretty.rb +7 -1
  37. data/lib/rspec/matchers/test_unit_integration.rb +11 -0
  38. data/lib/rspec/matchers.rb +141 -143
  39. data/spec/rspec/expectations/differ_spec.rb +27 -5
  40. data/spec/rspec/expectations/expectation_target_spec.rb +10 -3
  41. data/spec/rspec/expectations/extensions/kernel_spec.rb +5 -5
  42. data/spec/rspec/expectations/fail_with_spec.rb +19 -0
  43. data/spec/rspec/expectations/handler_spec.rb +42 -21
  44. data/spec/rspec/expectations/syntax_spec.rb +45 -3
  45. data/spec/rspec/matchers/be_close_spec.rb +6 -6
  46. data/spec/rspec/matchers/be_spec.rb +36 -36
  47. data/spec/rspec/matchers/be_within_spec.rb +8 -0
  48. data/spec/rspec/matchers/change_spec.rb +17 -6
  49. data/spec/rspec/matchers/configuration_spec.rb +57 -89
  50. data/spec/rspec/matchers/description_generation_spec.rb +16 -2
  51. data/spec/rspec/matchers/exist_spec.rb +9 -9
  52. data/spec/rspec/matchers/has_spec.rb +1 -1
  53. data/spec/rspec/matchers/have_spec.rb +12 -2
  54. data/spec/rspec/matchers/include_matcher_integration_spec.rb +2 -2
  55. data/spec/rspec/matchers/include_spec.rb +4 -4
  56. data/spec/rspec/matchers/match_array_spec.rb +1 -1
  57. data/spec/rspec/matchers/match_spec.rb +1 -1
  58. data/spec/rspec/matchers/raise_error_spec.rb +189 -99
  59. data/spec/rspec/matchers/respond_to_spec.rb +4 -4
  60. data/spec/rspec/matchers/satisfy_spec.rb +1 -1
  61. data/spec/rspec/matchers/start_with_end_with_spec.rb +2 -2
  62. data/spec/rspec/matchers/yield_spec.rb +81 -4
  63. data/spec/spec_helper.rb +1 -1
  64. metadata +8 -7
@@ -35,7 +35,7 @@ module RSpec
35
35
  end
36
36
 
37
37
  def to_word(item)
38
- item.respond_to?(:description) ? item.description : item.inspect
38
+ is_matcher_with_description?(item) ? item.description : item.inspect
39
39
  end
40
40
 
41
41
  def name_to_sentence
@@ -59,6 +59,12 @@ module RSpec
59
59
  word.downcase!
60
60
  word
61
61
  end
62
+
63
+ private
64
+
65
+ def is_matcher_with_description?(object)
66
+ RSpec::Matchers.is_a_matcher?(object) && object.respond_to?(:description)
67
+ end
62
68
  end
63
69
  end
64
70
  end
@@ -0,0 +1,11 @@
1
+ # Include Matchers for other test frameworks. Note that MiniTest _must_
2
+ # come before TU because on ruby 1.9, T::U::TC is a subclass of MT::U::TC
3
+ # and a 1.9 bug can lead to infinite recursion from the `super` call in our
4
+ # method_missing hook. See this gist for more info:
5
+ # https://gist.github.com/845896
6
+ if defined?(MiniTest::Unit::TestCase)
7
+ MiniTest::Unit::TestCase.send(:include, RSpec::Matchers)
8
+ end
9
+ if defined?(Test::Unit::TestCase)
10
+ Test::Unit::TestCase.send(:include, RSpec::Matchers)
11
+ end
@@ -1,5 +1,19 @@
1
+ require 'rspec/matchers/extensions/instance_eval_with_args'
2
+ require 'rspec/matchers/pretty'
3
+
4
+ require 'rspec/matchers/built_in'
5
+ require 'rspec/matchers/matcher'
6
+ require 'rspec/matchers/operator_matcher'
7
+ require 'rspec/matchers/be_close'
8
+
9
+ require 'rspec/matchers/generated_descriptions'
10
+ require 'rspec/matchers/method_missing'
11
+ require 'rspec/matchers/compatibility'
12
+ require 'rspec/matchers/dsl'
13
+ require 'rspec/matchers/test_unit_integration'
14
+
1
15
  module RSpec
2
- # RSpec::Matchers provides a number of useful matchers we use to compose
16
+ # RSpec::Matchers provides a number of useful matchers we use to define
3
17
  # expectations. A matcher is any object that responds to the following:
4
18
  #
5
19
  # matches?(actual)
@@ -20,28 +34,29 @@ module RSpec
20
34
  # A Ruby predicate is a method that ends with a "?" and returns true or false.
21
35
  # Common examples are `empty?`, `nil?`, and `instance_of?`.
22
36
  #
23
- # All you need to do is write `should be_` followed by the predicate without
24
- # the question mark, and RSpec will figure it out from there. For example:
37
+ # All you need to do is write `expect(..).to be_` followed by the predicate
38
+ # without the question mark, and RSpec will figure it out from there.
39
+ # For example:
25
40
  #
26
- # [].should be_empty # => [].empty?() | passes
27
- # [].should_not be_empty # => [].empty?() | fails
41
+ # expect([]).to be_empty # => [].empty?() | passes
42
+ # expect([]).not_to be_empty # => [].empty?() | fails
28
43
  #
29
44
  # In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_"
30
45
  # and "be_an_", making your specs read much more naturally:
31
46
  #
32
- # "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes
47
+ # expect("a string").to be_an_instance_of(String) # =>"a string".instance_of?(String) # passes
33
48
  #
34
- # 3.should be_a_kind_of(Fixnum) # => 3.kind_of?(Numeric) | passes
35
- # 3.should be_a_kind_of(Numeric) # => 3.kind_of?(Numeric) | passes
36
- # 3.should be_an_instance_of(Fixnum) # => 3.instance_of?(Fixnum) | passes
37
- # 3.should_not be_instance_of(Numeric) # => 3.instance_of?(Numeric) | fails
49
+ # expect(3).to be_a_kind_of(Fixnum) # => 3.kind_of?(Numeric) | passes
50
+ # expect(3).to be_a_kind_of(Numeric) # => 3.kind_of?(Numeric) | passes
51
+ # expect(3).to be_an_instance_of(Fixnum) # => 3.instance_of?(Fixnum) | passes
52
+ # expect(3).not_to be_an_instance_of(Numeric) # => 3.instance_of?(Numeric) | fails
38
53
  #
39
54
  # RSpec will also create custom matchers for predicates like `has_key?`. To
40
55
  # use this feature, just state that the object should have_key(:key) and RSpec will
41
56
  # call has_key?(:key) on the target. For example:
42
57
  #
43
- # {:a => "A"}.should have_key(:a) # => {:a => "A"}.has_key?(:a) | passes
44
- # {:a => "A"}.should have_key(:b) # => {:a => "A"}.has_key?(:b) | fails
58
+ # expect(:a => "A").to have_key(:a)
59
+ # expect(:a => "A").to have_key(:b) # fails
45
60
  #
46
61
  # You can use this feature to invoke any predicate that begins with "has_", whether it is
47
62
  # part of the Ruby libraries (like `Hash#has_key?`) or a method you wrote on your own class.
@@ -58,15 +73,15 @@ module RSpec
58
73
  # zones on a virtual board. To specify that bob should be in zone 4, you
59
74
  # could say:
60
75
  #
61
- # bob.current_zone.should eql(Zone.new("4"))
76
+ # expect(bob.current_zone).to eql(Zone.new("4"))
62
77
  #
63
78
  # But you might find it more expressive to say:
64
79
  #
65
- # bob.should be_in_zone("4")
80
+ # expect(bob).to be_in_zone("4")
66
81
  #
67
82
  # and/or
68
83
  #
69
- # bob.should_not be_in_zone("3")
84
+ # expect(bob).not_to be_in_zone("3")
70
85
  #
71
86
  # You can create such a matcher like so:
72
87
  #
@@ -102,7 +117,7 @@ module RSpec
102
117
  # passed to the <tt>create</tt> method (in this case, <tt>zone</tt>). The
103
118
  # failure message methods (<tt>failure_message_for_should</tt> and
104
119
  # <tt>failure_message_for_should_not</tt>) are passed the actual value (the
105
- # receiver of <tt>should</tt> or <tt>should_not</tt>).
120
+ # receiver of <tt>expect(..)</tt> or <tt>expect(..).not_to</tt>).
106
121
  #
107
122
  # ### Custom Matcher from scratch
108
123
  #
@@ -159,35 +174,14 @@ module RSpec
159
174
  # config.include(CustomGameMatchers)
160
175
  # end
161
176
  module Matchers
162
- # Include Matchers for other test frameworks. Note that MiniTest _must_
163
- # come before TU because on ruby 1.9, T::U::TC is a subclass of MT::U::TC
164
- # and a 1.9 bug can lead to infinite recursion from the `super` call in our
165
- # method_missing hook. See this gist for more info:
166
- # https://gist.github.com/845896
167
- if defined?(MiniTest::Unit::TestCase)
168
- MiniTest::Unit::TestCase.send(:include, self)
169
- end
170
- if defined?(Test::Unit::TestCase)
171
- Test::Unit::TestCase.send(:include, self)
172
- end
173
- end
174
- end
177
+ # @api private
178
+ def self.is_a_matcher?(obj)
179
+ return true if ::RSpec::Matchers::BuiltIn::BaseMatcher === obj
180
+ return false if obj.respond_to?(:i_respond_to_everything_so_im_not_really_a_matcher)
181
+ return false unless obj.respond_to?(:matches?)
175
182
 
176
- require 'rspec/matchers/extensions/instance_eval_with_args'
177
- require 'rspec/matchers/pretty'
178
-
179
- require 'rspec/matchers/built_in'
180
- require 'rspec/matchers/matcher'
181
- require 'rspec/matchers/operator_matcher'
182
- require 'rspec/matchers/be_close'
183
-
184
- require 'rspec/matchers/generated_descriptions'
185
- require 'rspec/matchers/method_missing'
186
- require 'rspec/matchers/compatibility'
187
- require 'rspec/matchers/dsl'
188
-
189
- module RSpec
190
- module Matchers
183
+ obj.respond_to?(:failure_message_for_should) || obj.respond_to?(:failure_message)
184
+ end
191
185
 
192
186
  # Passes if actual is truthy (anything but false or nil)
193
187
  def be_true
@@ -205,12 +199,12 @@ module RSpec
205
199
  end
206
200
 
207
201
  # @example
208
- # actual.should be_true
209
- # actual.should be_false
210
- # actual.should be_nil
211
- # actual.should be_[arbitrary_predicate](*args)
212
- # actual.should_not be_nil
213
- # actual.should_not be_[arbitrary_predicate](*args)
202
+ # expect(actual).to be_true
203
+ # expect(actual).to be_false
204
+ # expect(actual).to be_nil
205
+ # expect(actual).to be_[arbitrary_predicate](*args)
206
+ # expect(actual).not_to be_nil
207
+ # expect(actual).not_to be_[arbitrary_predicate](*args)
214
208
  #
215
209
  # Given true, false, or nil, will pass if actual value is true, false or
216
210
  # nil (respectively). Given no args means the caller should satisfy an if
@@ -240,9 +234,9 @@ module RSpec
240
234
  #
241
235
  # @example
242
236
  #
243
- # 5.should be_instance_of(Fixnum)
244
- # 5.should_not be_instance_of(Numeric)
245
- # 5.should_not be_instance_of(Float)
237
+ # expect(5).to be_an_instance_of(Fixnum)
238
+ # expect(5).not_to be_an_instance_of(Numeric)
239
+ # expect(5).not_to be_an_instance_of(Float)
246
240
  def be_an_instance_of(expected)
247
241
  BuiltIn::BeAnInstanceOf.new(expected)
248
242
  end
@@ -253,9 +247,9 @@ module RSpec
253
247
  #
254
248
  # @example
255
249
  #
256
- # 5.should be_kind_of(Fixnum)
257
- # 5.should be_kind_of(Numeric)
258
- # 5.should_not be_kind_of(Float)
250
+ # expect(5).to be_a_kind_of(Fixnum)
251
+ # expect(5).to be_a_kind_of(Numeric)
252
+ # expect(5).not_to be_a_kind_of(Float)
259
253
  def be_a_kind_of(expected)
260
254
  BuiltIn::BeAKindOf.new(expected)
261
255
  end
@@ -266,8 +260,8 @@ module RSpec
266
260
  #
267
261
  # @example
268
262
  #
269
- # result.should be_within(0.5).of(3.0)
270
- # result.should_not be_within(0.5).of(3.0)
263
+ # expect(result).to be_within(0.5).of(3.0)
264
+ # expect(result).not_to be_within(0.5).of(3.0)
271
265
  def be_within(delta)
272
266
  BuiltIn::BeWithin.new(delta)
273
267
  end
@@ -283,55 +277,59 @@ module RSpec
283
277
  #
284
278
  # When passing a block, it must use the <tt>{ ... }</tt> format, not
285
279
  # do/end, as <tt>{ ... }</tt> binds to the +change+ method, whereas do/end
286
- # would errantly bind to the +should+ or +should_not+ method.
280
+ # would errantly bind to the +expect(..)+ or +expect(..).not_to+ method.
287
281
  #
288
282
  # @example
289
283
  #
290
- # lambda {
284
+ # expect {
291
285
  # team.add_player(player)
292
- # }.should change(roster, :count)
286
+ # }.to change(roster, :count)
293
287
  #
294
- # lambda {
288
+ # expect {
295
289
  # team.add_player(player)
296
- # }.should change(roster, :count).by(1)
290
+ # }.to change(roster, :count).by(1)
297
291
  #
298
- # lambda {
292
+ # expect {
299
293
  # team.add_player(player)
300
- # }.should change(roster, :count).by_at_least(1)
294
+ # }.to change(roster, :count).by_at_least(1)
301
295
  #
302
- # lambda {
296
+ # expect {
303
297
  # team.add_player(player)
304
- # }.should change(roster, :count).by_at_most(1)
298
+ # }.to change(roster, :count).by_at_most(1)
305
299
  #
306
300
  # string = "string"
307
- # lambda {
301
+ # expect {
308
302
  # string.reverse!
309
- # }.should change { string }.from("string").to("gnirts")
303
+ # }.to change { string }.from("string").to("gnirts")
310
304
  #
311
- # lambda {
305
+ # string = "string"
306
+ # expect {
307
+ # string
308
+ # }.not_to change { string }
309
+ #
310
+ # expect {
312
311
  # person.happy_birthday
313
- # }.should change(person, :birthday).from(32).to(33)
312
+ # }.to change(person, :birthday).from(32).to(33)
314
313
  #
315
- # lambda {
314
+ # expect {
316
315
  # employee.develop_great_new_social_networking_app
317
- # }.should change(employee, :title).from("Mail Clerk").to("CEO")
316
+ # }.to change(employee, :title).from("Mail Clerk").to("CEO")
318
317
  #
319
- # lambda {
318
+ # expect {
320
319
  # doctor.leave_office
321
- # }.should change(doctor, :sign).from(/is in/).to(/is out/)
320
+ # }.to change(doctor, :sign).from(/is in/).to(/is out/)
322
321
  #
323
322
  # user = User.new(:type => "admin")
324
- # lambda {
323
+ # expect {
325
324
  # user.symbolize_type
326
- # }.should change(user, :type).from(String).to(Symbol)
325
+ # }.to change(user, :type).from(String).to(Symbol)
327
326
  #
328
327
  # == Notes
329
328
  #
330
329
  # Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after it
331
- # evaluates the proc object (generated by the lambdas in the examples
332
- # above).
330
+ # evaluates the block passed to <tt>expect</tt>.
333
331
  #
334
- # <tt>should_not change</tt> only supports the form with no subsequent
332
+ # <tt>expect( ... ).not_to change</tt> only supports the form with no subsequent
335
333
  # calls to <tt>by</tt>, <tt>by_at_least</tt>, <tt>by_at_most</tt>,
336
334
  # <tt>to</tt> or <tt>from</tt>.
337
335
  def change(receiver=nil, message=nil, &block)
@@ -343,11 +341,11 @@ module RSpec
343
341
  # and it will only pass if all args are found in Range.
344
342
  #
345
343
  # @example
346
- # (1..10).should cover(5)
347
- # (1..10).should cover(4, 6)
348
- # (1..10).should cover(4, 6, 11) # will fail
349
- # (1..10).should_not cover(11)
350
- # (1..10).should_not cover(5) # will fail
344
+ # expect(1..10).to cover(5)
345
+ # expect(1..10).to cover(4, 6)
346
+ # expect(1..10).to cover(4, 6, 11) # fails
347
+ # expect(1..10).not_to cover(11)
348
+ # expect(1..10).not_to cover(5) # fails
351
349
  #
352
350
  # ### Warning:: Ruby >= 1.9 only
353
351
  def cover(*values)
@@ -361,45 +359,48 @@ module RSpec
361
359
  #
362
360
  # @example
363
361
  #
364
- # "this string".should end_with "string"
365
- # [0, 1, 2, 3, 4].should end_with 4
366
- # [0, 2, 3, 4, 4].should end_with 3, 4
362
+ # expect("this string").to end_with "string"
363
+ # expect([0, 1, 2, 3, 4]).to end_with 4
364
+ # expect([0, 2, 3, 4, 4]).to end_with 3, 4
367
365
  def end_with(*expected)
368
366
  BuiltIn::EndWith.new(*expected)
369
367
  end
370
368
 
371
369
  # Passes if <tt>actual == expected</tt>.
372
370
  #
373
- # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
371
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
372
+ # information about equality in Ruby.
374
373
  #
375
374
  # @example
376
375
  #
377
- # 5.should eq(5)
378
- # 5.should_not eq(3)
376
+ # expect(5).to eq(5)
377
+ # expect(5).not_to eq(3)
379
378
  def eq(expected)
380
379
  BuiltIn::Eq.new(expected)
381
380
  end
382
381
 
383
382
  # Passes if +actual.eql?(expected)+
384
383
  #
385
- # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
384
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
385
+ # information about equality in Ruby.
386
386
  #
387
387
  # @example
388
388
  #
389
- # 5.should eql(5)
390
- # 5.should_not eql(3)
389
+ # expect(5).to eql(5)
390
+ # expect(5).not_to eql(3)
391
391
  def eql(expected)
392
392
  BuiltIn::Eql.new(expected)
393
393
  end
394
394
 
395
395
  # Passes if <tt>actual.equal?(expected)</tt> (object identity).
396
396
  #
397
- # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more information about equality in Ruby.
397
+ # See http://www.ruby-doc.org/core/classes/Object.html#M001057 for more
398
+ # information about equality in Ruby.
398
399
  #
399
400
  # @example
400
401
  #
401
- # 5.should equal(5) # Fixnums are equal
402
- # "5".should_not equal("5") # Strings that look the same are not the same object
402
+ # expect(5).to equal(5) # Fixnums are equal
403
+ # expect("5").not_to equal("5") # Strings that look the same are not the same object
403
404
  def equal(expected)
404
405
  BuiltIn::Equal.new(expected)
405
406
  end
@@ -407,7 +408,7 @@ module RSpec
407
408
  # Passes if `actual.exist?` or `actual.exists?`
408
409
  #
409
410
  # @example
410
- # File.should exist("path/to/file")
411
+ # expect(File).to exist("path/to/file")
411
412
  def exist(*args)
412
413
  BuiltIn::Exist.new(*args)
413
414
  end
@@ -430,16 +431,16 @@ module RSpec
430
431
  # @example
431
432
  #
432
433
  # # Passes if team.players.size == 11
433
- # team.should have(11).players
434
+ # expect(team).to have(11).players
434
435
  #
435
436
  # # Passes if [1,2,3].length == 3
436
- # [1,2,3].should have(3).items #"items" is pure sugar
437
+ # expect([1,2,3]).to have(3).items #"items" is pure sugar
437
438
  #
438
439
  # # Passes if ['a', 'b', 'c'].count == 3
439
- # [1,2,3].should have(3).items #"items" is pure sugar
440
+ # expect([1,2,3]).to have(3).items #"items" is pure sugar
440
441
  #
441
442
  # # Passes if "this string".length == 11
442
- # "this string".should have(11).characters #"characters" is pure sugar
443
+ # expect("this string").to have(11).characters #"characters" is pure sugar
443
444
  def have(n)
444
445
  BuiltIn::Have.new(n)
445
446
  end
@@ -448,11 +449,11 @@ module RSpec
448
449
  # Exactly like have() with >=.
449
450
  #
450
451
  # @example
451
- # "this".should have_at_least(3).letters
452
+ # expect("this").to have_at_least(3).letters
452
453
  #
453
454
  # ### Warning:
454
455
  #
455
- # `should_not have_at_least` is not supported
456
+ # `expect(..).not_to have_at_least` is not supported
456
457
  def have_at_least(n)
457
458
  BuiltIn::Have.new(n, :at_least)
458
459
  end
@@ -460,11 +461,11 @@ module RSpec
460
461
  # Exactly like have() with <=.
461
462
  #
462
463
  # @example
463
- # should have_at_most(number).items
464
+ # expect("this").to have_at_most(4).letters
464
465
  #
465
466
  # ### Warning:
466
467
  #
467
- # `should_not have_at_most` is not supported
468
+ # `expect(..).not_to have_at_most` is not supported
468
469
  def have_at_most(n)
469
470
  BuiltIn::Have.new(n, :at_most)
470
471
  end
@@ -475,12 +476,12 @@ module RSpec
475
476
  #
476
477
  # @example
477
478
  #
478
- # [1,2,3].should include(3)
479
- # [1,2,3].should include(2,3) #would pass
480
- # [1,2,3].should include(2,3,4) #would fail
481
- # [1,2,3].should_not include(4)
482
- # "spread".should include("read")
483
- # "spread".should_not include("red")
479
+ # expect([1,2,3]).to include(3)
480
+ # expect([1,2,3]).to include(2,3)
481
+ # expect([1,2,3]).to include(2,3,4) # fails
482
+ # expect([1,2,3]).not_to include(4)
483
+ # expect("spread").to include("read")
484
+ # expect("spread").not_to include("red")
484
485
  def include(*expected)
485
486
  BuiltIn::Include.new(*expected)
486
487
  end
@@ -489,10 +490,10 @@ module RSpec
489
490
  #
490
491
  # @example
491
492
  #
492
- # email.should match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
493
- # email.should match("@example.com")
494
- # zipcode.should match_regex(/\A\d{5}(-\d{4})?\z/)
495
- # zipcode.should match_regex("90210")
493
+ # expect(email).to match(/^([^\s]+)((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)
494
+ # expect(email).to match("@example.com")
495
+ # expect(zipcode).to match_regex(/\A\d{5}(-\d{4})?\z/)
496
+ # expect(zipcode).to match_regex("90210")
496
497
  #
497
498
  # @note Due to Ruby's method dispatch mechanism, using the `#match` matcher
498
499
  # within a custom matcher defined via the matcher DSL
@@ -513,16 +514,16 @@ module RSpec
513
514
  #
514
515
  # @example
515
516
  #
516
- # lambda { do_something_risky }.should raise_error
517
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)
518
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 }
519
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky")
520
- # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/)
517
+ # expect { do_something_risky }.to raise_error
518
+ # expect { do_something_risky }.to raise_error(PoorRiskDecisionError)
519
+ # expect { do_something_risky }.to raise_error(PoorRiskDecisionError) { |error| expect(error.data).to eq 42 }
520
+ # expect { do_something_risky }.to raise_error(PoorRiskDecisionError, "that was too risky")
521
+ # expect { do_something_risky }.to raise_error(PoorRiskDecisionError, /oo ri/)
521
522
  #
522
- # lambda { do_something_risky }.should_not raise_error
523
- # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError)
524
- # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky")
525
- # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/)
523
+ # expect { do_something_risky }.not_to raise_error
524
+ # expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError)
525
+ # expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError, "that was too risky")
526
+ # expect { do_something_risky }.not_to raise_error(PoorRiskDecisionError, /oo ri/)
526
527
  def raise_error(error=Exception, message=nil, &block)
527
528
  BuiltIn::RaiseError.new(error, message, &block)
528
529
  end
@@ -534,6 +535,8 @@ module RSpec
534
535
  #
535
536
  # @example
536
537
  #
538
+ # expect("string").to respond_to(:length)
539
+ #
537
540
  def respond_to(*names)
538
541
  BuiltIn::RespondTo.new(*names)
539
542
  end
@@ -550,9 +553,7 @@ module RSpec
550
553
  #
551
554
  # @example
552
555
  #
553
- # 5.should satisfy { |n|
554
- # n > 3
555
- # }
556
+ # expect(5).to satisfy { |n| n > 3 }
556
557
  def satisfy(&block)
557
558
  BuiltIn::Satisfy.new(&block)
558
559
  end
@@ -564,9 +565,9 @@ module RSpec
564
565
  #
565
566
  # @example
566
567
  #
567
- # "this string".should start_with "this s"
568
- # [0, 1, 2, 3, 4].should start_with 0
569
- # [0, 2, 3, 4, 4].should start_with 0, 1
568
+ # expect("this string").to start_with "this s"
569
+ # expect([0, 1, 2, 3, 4]).to start_with 0
570
+ # expect([0, 2, 3, 4, 4]).to start_with 0, 1
570
571
  def start_with(*expected)
571
572
  BuiltIn::StartWith.new(*expected)
572
573
  end
@@ -580,13 +581,13 @@ module RSpec
580
581
  #
581
582
  # @example
582
583
  #
583
- # lambda { do_something_risky }.should throw_symbol
584
- # lambda { do_something_risky }.should throw_symbol(:that_was_risky)
585
- # lambda { do_something_risky }.should throw_symbol(:that_was_risky, culprit)
584
+ # expect { do_something_risky }.to throw_symbol
585
+ # expect { do_something_risky }.to throw_symbol(:that_was_risky)
586
+ # expect { do_something_risky }.to throw_symbol(:that_was_risky, 'culprit')
586
587
  #
587
- # lambda { do_something_risky }.should_not throw_symbol
588
- # lambda { do_something_risky }.should_not throw_symbol(:that_was_risky)
589
- # lambda { do_something_risky }.should_not throw_symbol(:that_was_risky, culprit)
588
+ # expect { do_something_risky }.not_to throw_symbol
589
+ # expect { do_something_risky }.not_to throw_symbol(:that_was_risky)
590
+ # expect { do_something_risky }.not_to throw_symbol(:that_was_risky, 'culprit')
590
591
  def throw_symbol(expected_symbol=nil, expected_arg=nil)
591
592
  BuiltIn::ThrowSymbol.new(expected_symbol, expected_arg)
592
593
  end
@@ -679,17 +680,14 @@ module RSpec
679
680
  #
680
681
  # @note This is also available using the `=~` operator with `should`,
681
682
  # but `=~` is not supported with `expect`.
682
- # @note There is no should_not version of array.should =~ other_array
683
+ #
684
+ # @note This matcher only supports positive expectations.
685
+ # expect(..).not_to match_array(other_array) is not supported.
683
686
  #
684
687
  # @example
685
688
  #
686
689
  # expect([1,2,3]).to match_array([1,2,3])
687
690
  # expect([1,2,3]).to match_array([1,3,2])
688
- # [1,2,3].should =~ [1,2,3] # => would pass
689
- # [1,2,3].should =~ [2,3,1] # => would pass
690
- # [1,2,3,4].should =~ [1,2,3] # => would fail
691
- # [1,2,2,3].should =~ [1,2,3] # => would fail
692
- # [1,2,3].should =~ [1,2,3,4] # => would fail
693
691
  def match_array(array)
694
692
  BuiltIn::MatchArray.new(array)
695
693
  end
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  require 'spec_helper'
2
3
  require 'ostruct'
3
4
 
@@ -13,10 +14,11 @@ module RSpec
13
14
  # color disabled context
14
15
 
15
16
  describe '#diff_as_string' do
17
+ subject { differ.diff_as_string(@expected, @actual) }
16
18
  it "outputs unified diff of two strings" do
17
- expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
18
- actual="foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
19
- expected_diff= <<'EOD'
19
+ @expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
20
+ @actual="foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
21
+ expect(subject).to eql(<<-'EOD')
20
22
 
21
23
 
22
24
  @@ -1,6 +1,6 @@
@@ -35,8 +37,28 @@ module RSpec
35
37
  line
36
38
  EOD
37
39
 
38
- diff = differ.diff_as_string(expected, actual)
39
- expect(diff).to eql(expected_diff)
40
+ end
41
+ if RUBY_VERSION.to_f > 1.9
42
+ it 'copes with encoded strings', :pending => (Diff::LCS::VERSION < '1.2.2') do
43
+ @expected="Tu avec carté {count} itém has".encode('UTF-16LE')
44
+ @actual="Tu avec carte {count} item has".encode('UTF-16LE')
45
+ expect(subject).to eql(<<-EOD.encode('UTF-16LE'))
46
+
47
+ @@ -1,2 +1,2 @@
48
+ -Tu avec carte {count} item has
49
+ +Tu avec carté {count} itém has
50
+ EOD
51
+ end
52
+ it 'copes with encoded strings', :pending => (Diff::LCS::VERSION >= '1.2.2') do
53
+ @expected="Tu avec carté {count} itém has".encode('UTF-16LE')
54
+ @actual="Tu avec carte {count} item has".encode('UTF-16LE')
55
+ expect(subject).to eql 'Could not produce a diff because of the encoding of the string (UTF-16LE)'
56
+ end
57
+ it 'ouputs a message when encountering differently encoded strings' do
58
+ @expected="Tu avec carté {count} itém has".encode('UTF-16LE')
59
+ @actual="Tu avec carte {count} item has"
60
+ expect(subject).to eql 'Could not produce a diff because the encoding of the actual string (UTF-8) differs from the encoding of the expected string (UTF-16LE)'
61
+ end
40
62
  end
41
63
  end
42
64
 
@@ -37,10 +37,13 @@ module RSpec
37
37
  end
38
38
 
39
39
  it 'passes a valid negative expectation' do
40
- expect(5).to_not eq(4)
41
40
  expect(5).not_to eq(4)
42
41
  end
43
42
 
43
+ it 'passes a valid negative expectation with a split infinitive' do
44
+ expect(5).to_not eq(4)
45
+ end
46
+
44
47
  it 'fails an invalid positive expectation' do
45
48
  expect {
46
49
  expect(5).to eq(4)
@@ -50,10 +53,14 @@ module RSpec
50
53
  it 'fails an invalid negative expectation' do
51
54
  message = /expected 5 not to be a kind of Fixnum/
52
55
  expect {
53
- expect(5).to_not be_a(Fixnum)
56
+ expect(5).not_to be_a(Fixnum)
54
57
  }.to fail_with(message)
58
+ end
59
+
60
+ it 'fails an invalid negative expectation with a split infinitive' do
61
+ message = /expected 5 not to be a kind of Fixnum/
55
62
  expect {
56
- expect(5).not_to be_a(Fixnum)
63
+ expect(5).to_not be_a(Fixnum)
57
64
  }.to fail_with(message)
58
65
  end
59
66