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.
- data/README.md +2 -2
- data/features/built_in_matchers/README.md +7 -5
- data/features/built_in_matchers/be.feature +1 -1
- data/features/built_in_matchers/cover.feature +1 -1
- data/features/built_in_matchers/expect_error.feature +59 -26
- data/features/built_in_matchers/have.feature +2 -2
- data/features/built_in_matchers/predicates.feature +1 -1
- data/features/step_definitions/additional_cli_steps.rb +1 -1
- data/features/support/env.rb +1 -1
- data/lib/rspec/expectations.rb +0 -2
- data/lib/rspec/expectations/deprecation.rb +6 -4
- data/lib/rspec/expectations/errors.rb +4 -7
- data/lib/rspec/expectations/extensions.rb +1 -0
- data/lib/rspec/expectations/extensions/array.rb +2 -0
- data/lib/rspec/expectations/extensions/kernel.rb +18 -44
- data/lib/rspec/expectations/{backward_compatibility.rb → extensions/object.rb} +5 -3
- data/lib/rspec/expectations/fail_with.rb +8 -5
- data/lib/rspec/expectations/version.rb +5 -4
- data/lib/rspec/matchers.rb +77 -73
- data/lib/rspec/matchers/be.rb +42 -51
- data/lib/rspec/matchers/be_within.rb +1 -1
- data/lib/rspec/matchers/change.rb +5 -13
- data/lib/rspec/matchers/dsl.rb +2 -1
- data/lib/rspec/matchers/eq.rb +3 -3
- data/lib/rspec/matchers/extensions/{instance_exec.rb → instance_eval_with_args.rb} +15 -7
- data/lib/rspec/matchers/has.rb +11 -6
- data/lib/rspec/matchers/have.rb +36 -19
- data/lib/rspec/matchers/match_array.rb +1 -1
- data/lib/rspec/matchers/matcher.rb +5 -5
- data/spec/rspec/matchers/change_spec.rb +38 -0
- data/spec/rspec/matchers/description_generation_spec.rb +32 -32
- data/spec/rspec/matchers/eq_spec.rb +2 -2
- data/spec/rspec/matchers/has_spec.rb +33 -1
- data/spec/rspec/matchers/have_spec.rb +64 -7
- data/spec/rspec/matchers/match_array_spec.rb +0 -3
- data/spec/rspec/matchers/operator_matcher_spec.rb +10 -4
- data/spec/rspec/matchers/raise_error_spec.rb +6 -6
- data/spec/support/classes.rb +21 -10
- metadata +51 -62
- data/.document +0 -5
- data/.gitignore +0 -10
- data/.travis.yml +0 -7
- data/Gemfile +0 -40
- data/Guardfile +0 -5
- data/License.txt +0 -22
- data/Rakefile +0 -81
- data/cucumber.yml +0 -10
- data/features/.nav +0 -29
- data/features/Changelog.md +0 -101
- data/rspec-expectations.gemspec +0 -27
- data/specs.watchr +0 -57
data/lib/rspec/matchers.rb
CHANGED
@@ -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
|
-
#
|
7
|
-
#
|
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
|
-
#
|
12
|
-
#
|
13
|
-
#
|
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
|
-
#
|
28
|
-
#
|
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
|
-
#
|
32
|
+
# "a string".should be_an_instance_of(String) =>"a string".instance_of?(String) #passes
|
34
33
|
#
|
35
|
-
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
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
|
-
#
|
45
|
-
#
|
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
|
-
#
|
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
|
-
#
|
65
|
+
# bob.should be_in_zone("4")
|
67
66
|
#
|
68
67
|
# and/or
|
69
68
|
#
|
70
|
-
#
|
69
|
+
# bob.should_not be_in_zone("3")
|
71
70
|
#
|
72
71
|
# You can create such a matcher like so:
|
73
72
|
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
#
|
77
|
-
#
|
78
|
-
#
|
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
|
-
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
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
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
#
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
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
|
-
#
|
128
|
-
#
|
129
|
-
#
|
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
|
-
#
|
136
|
-
#
|
137
|
-
#
|
138
|
-
#
|
140
|
+
# module CustomGameMatchers
|
141
|
+
# class BeInZone
|
142
|
+
# # ...
|
143
|
+
# end
|
139
144
|
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
145
|
+
# def be_in_zone(expected)
|
146
|
+
# # ...
|
147
|
+
# end
|
148
|
+
# end
|
144
149
|
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
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
|
-
#
|
154
|
-
#
|
155
|
-
#
|
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/
|
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'
|
data/lib/rspec/matchers/be.rb
CHANGED
@@ -1,35 +1,36 @@
|
|
1
1
|
require 'rspec/matchers/dsl'
|
2
2
|
|
3
|
-
RSpec
|
4
|
-
|
5
|
-
actual
|
6
|
-
end
|
7
|
-
end
|
3
|
+
module RSpec
|
4
|
+
module Matchers
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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 :
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
RSpec::Matchers.define :be_false do
|
14
|
+
match do |actual|
|
15
|
+
!actual
|
16
|
+
end
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
RSpec::Matchers.define :be_nil do
|
20
|
+
match do |actual|
|
21
|
+
actual.nil?
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
24
|
+
failure_message_for_should do |actual|
|
25
|
+
"expected: nil\n got: #{actual.inspect}"
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
28
|
+
failure_message_for_should_not do
|
29
|
+
"expected: not nil\n got: nil"
|
30
|
+
end
|
31
|
+
end
|
31
32
|
|
32
|
-
class Be
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
203
|
-
#
|
204
|
-
#
|
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
|
-
#
|
208
|
-
#
|
209
|
-
#
|
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)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Matchers
|
3
|
-
class Change
|
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
|
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
|
#
|
data/lib/rspec/matchers/dsl.rb
CHANGED
data/lib/rspec/matchers/eq.rb
CHANGED
@@ -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
|
-
"
|
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
|
4
|
-
|
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
|
8
|
-
#
|
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
|
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
|