rspec-expectations 2.6.0 → 2.7.0.rc1

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 (51) hide show
  1. data/README.md +2 -2
  2. data/features/built_in_matchers/README.md +7 -5
  3. data/features/built_in_matchers/be.feature +1 -1
  4. data/features/built_in_matchers/cover.feature +1 -1
  5. data/features/built_in_matchers/expect_error.feature +59 -26
  6. data/features/built_in_matchers/have.feature +2 -2
  7. data/features/built_in_matchers/predicates.feature +1 -1
  8. data/features/step_definitions/additional_cli_steps.rb +1 -1
  9. data/features/support/env.rb +1 -1
  10. data/lib/rspec/expectations.rb +0 -2
  11. data/lib/rspec/expectations/deprecation.rb +6 -4
  12. data/lib/rspec/expectations/errors.rb +4 -7
  13. data/lib/rspec/expectations/extensions.rb +1 -0
  14. data/lib/rspec/expectations/extensions/array.rb +2 -0
  15. data/lib/rspec/expectations/extensions/kernel.rb +18 -44
  16. data/lib/rspec/expectations/{backward_compatibility.rb → extensions/object.rb} +5 -3
  17. data/lib/rspec/expectations/fail_with.rb +8 -5
  18. data/lib/rspec/expectations/version.rb +5 -4
  19. data/lib/rspec/matchers.rb +77 -73
  20. data/lib/rspec/matchers/be.rb +42 -51
  21. data/lib/rspec/matchers/be_within.rb +1 -1
  22. data/lib/rspec/matchers/change.rb +5 -13
  23. data/lib/rspec/matchers/dsl.rb +2 -1
  24. data/lib/rspec/matchers/eq.rb +3 -3
  25. data/lib/rspec/matchers/extensions/{instance_exec.rb → instance_eval_with_args.rb} +15 -7
  26. data/lib/rspec/matchers/has.rb +11 -6
  27. data/lib/rspec/matchers/have.rb +36 -19
  28. data/lib/rspec/matchers/match_array.rb +1 -1
  29. data/lib/rspec/matchers/matcher.rb +5 -5
  30. data/spec/rspec/matchers/change_spec.rb +38 -0
  31. data/spec/rspec/matchers/description_generation_spec.rb +32 -32
  32. data/spec/rspec/matchers/eq_spec.rb +2 -2
  33. data/spec/rspec/matchers/has_spec.rb +33 -1
  34. data/spec/rspec/matchers/have_spec.rb +64 -7
  35. data/spec/rspec/matchers/match_array_spec.rb +0 -3
  36. data/spec/rspec/matchers/operator_matcher_spec.rb +10 -4
  37. data/spec/rspec/matchers/raise_error_spec.rb +6 -6
  38. data/spec/support/classes.rb +21 -10
  39. metadata +51 -62
  40. data/.document +0 -5
  41. data/.gitignore +0 -10
  42. data/.travis.yml +0 -7
  43. data/Gemfile +0 -40
  44. data/Guardfile +0 -5
  45. data/License.txt +0 -22
  46. data/Rakefile +0 -81
  47. data/cucumber.yml +0 -10
  48. data/features/.nav +0 -29
  49. data/features/Changelog.md +0 -101
  50. data/rspec-expectations.gemspec +0 -27
  51. data/specs.watchr +0 -57
@@ -1,7 +1,8 @@
1
- module RSpec # :nodoc:
2
- module Expectations # :nodoc:
3
- module Version # :nodoc:
4
- STRING = '2.6.0'
1
+ module RSpec
2
+ module Expectations
3
+ # @private
4
+ module Version
5
+ STRING = '2.7.0.rc1'
5
6
  end
6
7
  end
7
8
  end
@@ -1,16 +1,15 @@
1
1
  module RSpec
2
2
  # rspec-expecations provides a number of useful Matchers we use to compose
3
- # expectations. A Matcher is any object that responds to the following
4
- # methods:
3
+ # expectations. A Matcher is any object that responds to the following:
5
4
  #
6
- # matches?(actual)
7
- # failure_message_for_should
5
+ # matches?(actual)
6
+ # failure_message_for_should
8
7
  #
9
8
  # These methods are also part of the matcher protocol, but are optional:
10
9
  #
11
- # does_not_match?(actual)
12
- # failure_message_for_should_not
13
- # description #optional
10
+ # does_not_match?(actual)
11
+ # failure_message_for_should_not
12
+ # description #optional
14
13
  #
15
14
  # == Predicates
16
15
  #
@@ -24,25 +23,25 @@ module RSpec
24
23
  # All you need to do is write +should be_+ followed by the predicate without
25
24
  # the question mark, and RSpec will figure it out from there. For example:
26
25
  #
27
- # [].should be_empty => [].empty? #passes
28
- # [].should_not be_empty => [].empty? #fails
26
+ # [].should be_empty # => [].empty?() | passes
27
+ # [].should_not be_empty # => [].empty?() | fails
29
28
  #
30
29
  # In addtion to prefixing the predicate matchers with "be_", you can also use "be_a_"
31
30
  # and "be_an_", making your specs read much more naturally:
32
31
  #
33
- # "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes
32
+ # "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes
34
33
  #
35
- # 3.should be_a_kind_of(Fixnum) => 3.kind_of?(Numeric) #passes
36
- # 3.should be_a_kind_of(Numeric) => 3.kind_of?(Numeric) #passes
37
- # 3.should be_an_instance_of(Fixnum) => 3.instance_of?(Fixnum) #passes
38
- # 3.should_not be_instance_of(Numeric) => 3.instance_of?(Numeric) #fails
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
39
38
  #
40
39
  # RSpec will also create custom matchers for predicates like +has_key?+. To
41
40
  # use this feature, just state that the object should have_key(:key) and RSpec will
42
41
  # call has_key?(:key) on the target. For example:
43
42
  #
44
- # {:a => "A"}.should have_key(:a) => {:a => "A"}.has_key?(:a) #passes
45
- # {:a => "A"}.should have_key(:b) => {:a => "A"}.has_key?(:b) #fails
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
46
45
  #
47
46
  # You can use this feature to invoke any predicate that begins with "has_", whether it is
48
47
  # part of the Ruby libraries (like +Hash#has_key?+) or a method you wrote on your own class.
@@ -59,42 +58,45 @@ module RSpec
59
58
  # zones on a virtual board. To specify that bob should be in zone 4, you
60
59
  # could say:
61
60
  #
62
- # bob.current_zone.should eql(Zone.new("4"))
61
+ # bob.current_zone.should eql(Zone.new("4"))
63
62
  #
64
63
  # But you might find it more expressive to say:
65
64
  #
66
- # bob.should be_in_zone("4")
65
+ # bob.should be_in_zone("4")
67
66
  #
68
67
  # and/or
69
68
  #
70
- # bob.should_not be_in_zone("3")
69
+ # bob.should_not be_in_zone("3")
71
70
  #
72
71
  # You can create such a matcher like so:
73
72
  #
74
- # RSpec::Matchers.define :be_in_zone do |zone|
75
- # match do |player|
76
- # player.in_zone?(zone)
77
- # end
78
- # end
73
+ # RSpec::Matchers.define :be_in_zone do |zone|
74
+ # match do |player|
75
+ # player.in_zone?(zone)
76
+ # end
77
+ # end
79
78
  #
80
79
  # This will generate a <tt>be_in_zone</tt> method that returns a matcher
81
80
  # with logical default messages for failures. You can override the failure
82
81
  # messages and the generated description as follows:
83
82
  #
84
- # RSpec::Matchers.define :be_in_zone do |zone|
85
- # match do |player|
86
- # player.in_zone?(zone)
87
- # end
88
- # failure_message_for_should do |player|
89
- # # generate and return the appropriate string.
90
- # end
91
- # failure_message_for_should_not do |player|
92
- # # generate and return the appropriate string.
93
- # end
94
- # description do
95
- # # generate and return the appropriate string.
96
- # end
97
- # end
83
+ # RSpec::Matchers.define :be_in_zone do |zone|
84
+ # match do |player|
85
+ # player.in_zone?(zone)
86
+ # end
87
+ #
88
+ # failure_message_for_should do |player|
89
+ # # generate and return the appropriate string.
90
+ # end
91
+ #
92
+ # failure_message_for_should_not do |player|
93
+ # # generate and return the appropriate string.
94
+ # end
95
+ #
96
+ # description do
97
+ # # generate and return the appropriate string.
98
+ # end
99
+ # end
98
100
  #
99
101
  # Each of the message-generation methods has access to the block arguments
100
102
  # passed to the <tt>create</tt> method (in this case, <tt>zone</tt>). The
@@ -106,54 +108,56 @@ module RSpec
106
108
  #
107
109
  # You could also write a custom matcher from scratch, as follows:
108
110
  #
109
- # class BeInZone
110
- # def initialize(expected)
111
- # @expected = expected
112
- # end
113
- # def matches?(target)
114
- # @target = target
115
- # @target.current_zone.eql?(Zone.new(@expected))
116
- # end
117
- # def failure_message_for_should
118
- # "expected #{@target.inspect} to be in Zone #{@expected}"
119
- # end
120
- # def failure_message_for_should_not
121
- # "expected #{@target.inspect} not to be in Zone #{@expected}"
122
- # end
123
- # end
111
+ # class BeInZone
112
+ # def initialize(expected)
113
+ # @expected = expected
114
+ # end
115
+ #
116
+ # def matches?(target)
117
+ # @target = target
118
+ # @target.current_zone.eql?(Zone.new(@expected))
119
+ # end
120
+ #
121
+ # def failure_message_for_should
122
+ # "expected #{@target.inspect} to be in Zone #{@expected}"
123
+ # end
124
+ #
125
+ # def failure_message_for_should_not
126
+ # "expected #{@target.inspect} not to be in Zone #{@expected}"
127
+ # end
128
+ # end
124
129
  #
125
130
  # ... and a method like this:
126
131
  #
127
- # def be_in_zone(expected)
128
- # BeInZone.new(expected)
129
- # end
132
+ # def be_in_zone(expected)
133
+ # BeInZone.new(expected)
134
+ # end
130
135
  #
131
136
  # And then expose the method to your specs. This is normally done
132
137
  # by including the method and the class in a module, which is then
133
138
  # included in your spec:
134
139
  #
135
- # module CustomGameMatchers
136
- # class BeInZone
137
- # ...
138
- # end
140
+ # module CustomGameMatchers
141
+ # class BeInZone
142
+ # # ...
143
+ # end
139
144
  #
140
- # def be_in_zone(expected)
141
- # ...
142
- # end
143
- # end
145
+ # def be_in_zone(expected)
146
+ # # ...
147
+ # end
148
+ # end
144
149
  #
145
- # describe "Player behaviour" do
146
- # include CustomGameMatchers
147
- # ...
148
- # end
150
+ # describe "Player behaviour" do
151
+ # include CustomGameMatchers
152
+ # # ...
153
+ # end
149
154
  #
150
155
  # or you can include in globally in a spec_helper.rb file <tt>require</tt>d
151
156
  # from your spec file(s):
152
157
  #
153
- # RSpec::Runner.configure do |config|
154
- # config.include(CustomGameMatchers)
155
- # end
156
- #
158
+ # RSpec::configure do |config|
159
+ # config.include(CustomGameMatchers)
160
+ # end
157
161
  module Matchers
158
162
  # Include Matchers for other test frameworks.
159
163
  # Note that MiniTest _must_ come before TU because on ruby 1.9,
@@ -170,7 +174,7 @@ module RSpec
170
174
  end
171
175
  end
172
176
 
173
- require 'rspec/matchers/extensions/instance_exec'
177
+ require 'rspec/matchers/extensions/instance_eval_with_args'
174
178
  require 'rspec/matchers/pretty'
175
179
  require 'rspec/matchers/matcher'
176
180
  require 'rspec/matchers/operator_matcher'
@@ -1,35 +1,36 @@
1
1
  require 'rspec/matchers/dsl'
2
2
 
3
- RSpec::Matchers.define :be_true do
4
- match do |actual|
5
- actual
6
- end
7
- end
3
+ module RSpec
4
+ module Matchers
8
5
 
9
- RSpec::Matchers.define :be_false do
10
- match do |actual|
11
- !actual
12
- end
13
- end
6
+ # @method be_true
7
+ RSpec::Matchers.define :be_true do
8
+ match do |actual|
9
+ actual
10
+ end
11
+ end
14
12
 
15
- RSpec::Matchers.define :be_nil do
16
- match do |actual|
17
- actual.nil?
18
- end
13
+ RSpec::Matchers.define :be_false do
14
+ match do |actual|
15
+ !actual
16
+ end
17
+ end
19
18
 
20
- failure_message_for_should do |actual|
21
- "expected: nil\n got: #{actual.inspect}"
22
- end
19
+ RSpec::Matchers.define :be_nil do
20
+ match do |actual|
21
+ actual.nil?
22
+ end
23
23
 
24
- failure_message_for_should_not do
25
- "expected: not nil\n got: nil"
26
- end
27
- end
24
+ failure_message_for_should do |actual|
25
+ "expected: nil\n got: #{actual.inspect}"
26
+ end
28
27
 
29
- module RSpec
30
- module Matchers
28
+ failure_message_for_should_not do
29
+ "expected: not nil\n got: nil"
30
+ end
31
+ end
31
32
 
32
- class Be #:nodoc:
33
+ class Be
33
34
  include RSpec::Matchers::Pretty
34
35
 
35
36
  def initialize(*args, &block)
@@ -177,36 +178,26 @@ it is a bit confusing.
177
178
 
178
179
  end
179
180
 
180
- # :call-seq:
181
- # should be_true
182
- # should be_false
183
- # should be_nil
184
- # should be_[arbitrary_predicate](*args)
185
- # should_not be_nil
186
- # should_not be_[arbitrary_predicate](*args)
187
- #
188
- # Given true, false, or nil, will pass if actual value is
189
- # true, false or nil (respectively). Given no args means
190
- # the caller should satisfy an if condition (to be or not to be).
191
- #
192
- # Predicates are any Ruby method that ends in a "?" and returns true or false.
193
- # Given be_ followed by arbitrary_predicate (without the "?"), RSpec will match
194
- # convert that into a query against the target object.
195
- #
196
- # The arbitrary_predicate feature will handle any predicate
197
- # prefixed with "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of)
198
- # or "be_" (e.g. be_empty), letting you choose the prefix that best suits the predicate.
181
+ # @example
182
+ # actual.should be_true
183
+ # actual.should be_false
184
+ # actual.should be_nil
185
+ # actual.should be_[arbitrary_predicate](*args)
186
+ # actual.should_not be_nil
187
+ # actual.should_not be_[arbitrary_predicate](*args)
199
188
  #
200
- # == Examples
189
+ # Given true, false, or nil, will pass if actual value is true, false or
190
+ # nil (respectively). Given no args means the caller should satisfy an if
191
+ # condition (to be or not to be).
201
192
  #
202
- # target.should be_true
203
- # target.should be_false
204
- # target.should be_nil
205
- # target.should_not be_nil
193
+ # Predicates are any Ruby method that ends in a "?" and returns true or
194
+ # false. Given be_ followed by arbitrary_predicate (without the "?"),
195
+ # RSpec will match convert that into a query against the target object.
206
196
  #
207
- # collection.should be_empty #passes if target.empty?
208
- # target.should_not be_empty #passes unless target.empty?
209
- # target.should_not be_old_enough(16) #passes unless target.old_enough?(16)
197
+ # The arbitrary_predicate feature will handle any predicate prefixed with
198
+ # "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) or "be_"
199
+ # (e.g. be_empty), letting you choose the prefix that best suits the
200
+ # predicate.
210
201
  def be(*args)
211
202
  args.empty? ?
212
203
  Matchers::Be.new : equal(*args)
@@ -16,7 +16,7 @@ module RSpec
16
16
  end
17
17
 
18
18
  match do |actual|
19
- unless @_expected
19
+ unless defined?(@_expected)
20
20
  raise ArgumentError.new("You must set an expected value using #of: be_within(#{_delta_}).of(expected_value)")
21
21
  end
22
22
  (actual - @_expected).abs < _delta_
@@ -1,6 +1,6 @@
1
1
  module RSpec
2
2
  module Matchers
3
- class Change #:nodoc:
3
+ class Change
4
4
  def initialize(receiver=nil, message=nil, &block)
5
5
  @message = message
6
6
  @value_proc = block || lambda {receiver.__send__(message)}
@@ -27,7 +27,7 @@ MESSAGE
27
27
 
28
28
  def evaluate_value_proc
29
29
  case val = @value_proc.call
30
- when Array, Hash
30
+ when Enumerable
31
31
  val.dup
32
32
  else
33
33
  val
@@ -128,20 +128,12 @@ MESSAGE
128
128
  end
129
129
  end
130
130
 
131
- # :call-seq:
132
- # should change(receiver, message)
133
- # should change(receiver, message).by(value)
134
- # should change(receiver, message).from(old).to(new)
135
- # should_not change(receiver, message)
136
- #
137
- # should change {...}
138
- # should change {...}.by(value)
139
- # should change {...}.from(old).to(new)
140
- # should_not change {...}
141
- #
142
131
  # Applied to a proc, specifies that its execution will cause some value to
143
132
  # change.
144
133
  #
134
+ # @param [Object] receiver
135
+ # @param [Symbol] message the message to send the receiver
136
+ #
145
137
  # You can either pass <tt>receiver</tt> and <tt>message</tt>, or a block,
146
138
  # but not both.
147
139
  #
@@ -1,7 +1,8 @@
1
1
  module RSpec
2
2
  module Matchers
3
3
  module DSL
4
- # See RSpec::Matchers
4
+ # Defines a custom matcher.
5
+ # @see RSpec::Matchers
5
6
  def define(name, &declarations)
6
7
  define_method name do |*expected|
7
8
  $matcher_execution_context = self
@@ -24,8 +24,8 @@ module RSpec
24
24
  failure_message_for_should do |actual|
25
25
  <<-MESSAGE
26
26
 
27
- expected #{_expected_.inspect}
28
- got #{actual.inspect}
27
+ expected: #{_expected_.inspect}
28
+ got: #{actual.inspect}
29
29
 
30
30
  (compared using ==)
31
31
  MESSAGE
@@ -41,7 +41,7 @@ MESSAGE
41
41
  end
42
42
 
43
43
  description do
44
- "== #{_expected_}"
44
+ "eq #{_expected_}"
45
45
  end
46
46
  end
47
47
  end
@@ -1,15 +1,23 @@
1
1
  module RSpec
2
2
  module Matchers
3
- module InstanceExec
4
- unless respond_to?(:instance_exec)
3
+ module Extensions
4
+ module InstanceEvalWithArgs
5
5
  # based on Bounded Spec InstanceExec (Mauricio Fernandez)
6
6
  # http://eigenclass.org/hiki/bounded+space+instance_exec
7
- # - uses singleton_class of matcher instead of global
8
- # InstanceExecHelper module
9
- # - this keeps it scoped to this class only, which is the
10
- # only place we need it
7
+ # - uses singleton_class instead of global InstanceExecHelper module
8
+ # - this keeps it scoped to classes/modules that include this module
11
9
  # - only necessary for ruby 1.8.6
12
- def instance_exec(*args, &block)
10
+ def instance_eval_with_args(*args, &block)
11
+ return instance_exec(*args, &block) if respond_to?(:instance_exec)
12
+
13
+ # If there are no args and the block doesn't expect any, there's no
14
+ # need to fake instance_exec with our hack below.
15
+ # Notes:
16
+ # * lambda { }.arity # => -1
17
+ # * lambda { || }.arity # => 0
18
+ # * lambda { |*a| }.arity # -1
19
+ return instance_eval(&block) if block.arity < 1 && args.empty?
20
+
13
21
  singleton_class = (class << self; self; end)
14
22
  begin
15
23
  orig_critical, Thread.critical = Thread.critical, true