rspec-expectations 2.14.5 → 2.99.0.beta1
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.
- checksums.yaml +15 -7
- data/Changelog.md +22 -4
- data/features/built_in_matchers/be.feature +40 -40
- data/lib/rspec/expectations/caller_filter.rb +55 -0
- data/lib/rspec/expectations/deprecation.rb +3 -15
- data/lib/rspec/expectations/version.rb +1 -2
- data/lib/rspec/matchers.rb +24 -6
- data/lib/rspec/matchers/built_in.rb +2 -2
- data/lib/rspec/matchers/built_in/base_matcher.rb +5 -7
- data/lib/rspec/matchers/built_in/be.rb +24 -6
- data/lib/rspec/matchers/built_in/have.rb +97 -1
- data/lib/rspec/matchers/differentiate_block_method_types.rb +55 -0
- data/lib/rspec/matchers/matcher.rb +85 -2
- data/spec/rspec/expectations/handler_spec.rb +1 -1
- data/spec/rspec/expectations/syntax_spec.rb +6 -6
- data/spec/rspec/expectations_spec.rb +14 -0
- data/spec/rspec/matchers/base_matcher_spec.rb +8 -29
- data/spec/rspec/matchers/be_spec.rb +84 -10
- data/spec/rspec/matchers/description_generation_spec.rb +21 -17
- data/spec/rspec/matchers/differentiate_block_method_types_spec.rb +39 -0
- data/spec/rspec/matchers/eq_spec.rb +1 -1
- data/spec/rspec/matchers/equal_spec.rb +0 -26
- data/spec/rspec/matchers/have_spec.rb +301 -1
- data/spec/rspec/matchers/matcher_spec.rb +148 -24
- data/spec/rspec/matchers/operator_matcher_spec.rb +3 -3
- data/spec/rspec/matchers/raise_error_spec.rb +3 -3
- data/spec/rspec/matchers/throw_symbol_spec.rb +14 -14
- data/spec/spec_helper.rb +23 -0
- data/spec/support/helper_methods.rb +11 -0
- metadata +79 -59
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
|
-
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZWNiMjczZmU0MjNkMzFhOTQxYzdkNWJjZDc1MTc4MWJlZmJmODhhZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NjMyZWVhYjFmYzRkNjM5YWRiMDFhMGRmMDhmNTNlYWJhMzg3NDlmNQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NzYyZDgzZjM5ODk5MjliNWI3YWE2MzY1ZmQ4YzQxNDQzOGU0ZDRiYzczZWMx
|
10
|
+
NGI5NzdjOTIwZmMyMDRkMGE2YzY2YTk2MjBiNGZlM2M5NjU4MmMzZjVhODFj
|
11
|
+
MDcxMDJhYTIwNTI4MTFlNmI4NjBkMjAxYTE1NzA4MDgwODJiODM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YzA0Mzg2MzBmYzc1ZmFjNDI3YjQzOTVkMzYwNzQ1OWU1NDYwZTg3YjFhMzQy
|
14
|
+
Y2JjYWZkMzNiZDJjMDJkYmMyZDZjYmJlMmU0MTlkNmZiNDVkY2E0OTUwMmJj
|
15
|
+
MGM0ODI1ZWZmMjUxYmQzMDAzYzM1M2I3MzM3YWVjMDQ4YmIzMmM=
|
data/Changelog.md
CHANGED
@@ -1,9 +1,27 @@
|
|
1
|
-
### 2.
|
2
|
-
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.4...v2.
|
1
|
+
### 2.99.0.beta1 / 2013-11-07
|
2
|
+
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.4...v2.99.0.beta1)
|
3
3
|
|
4
|
-
|
4
|
+
Deprecations
|
5
5
|
|
6
|
-
*
|
6
|
+
* Deprecate `have`, `have_at_least` and `have_at_most`. You can continue using those
|
7
|
+
matchers through https://github.com/rspec/rspec-collection_matchers, or
|
8
|
+
you can rewrite your expectations with something like
|
9
|
+
`expect(your_object.size).to eq(num)`. (Hugo Baraúna)
|
10
|
+
* Deprecate `be_xyz` predicate matcher when `xyz?` is a private method.
|
11
|
+
(Jon Rowe)
|
12
|
+
* Deprecate `be_true`/`be_false` in favour of `be_truthy`/`be_falsey`
|
13
|
+
(for Ruby's conditional semantics) or `be true`/`be false`
|
14
|
+
(for exact equality). (Sam Phippen)
|
15
|
+
* Deprecate calling helper methods from a custom matcher with the wrong
|
16
|
+
scope. (Myron Marston)
|
17
|
+
* `def self.foo` / `extend Helper` can be used to add macro methods
|
18
|
+
(e.g. methods that call the custom matcher DSL methods), but should
|
19
|
+
not be used to define helper methods called from within the DSL
|
20
|
+
blocks.
|
21
|
+
* `def foo` / `include Helper` is the opposite: it's for helper methods
|
22
|
+
callable from within a DSL block, but not for defining macros.
|
23
|
+
* RSpec 2.x allowed helper methods defined either way to be used for
|
24
|
+
either purpose, but RSpec 3.0 will not.
|
7
25
|
|
8
26
|
### 2.14.4 / 2013-11-06
|
9
27
|
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.3...v2.14.4)
|
@@ -3,101 +3,101 @@ Feature: "be" matchers
|
|
3
3
|
There are several related "be" matchers:
|
4
4
|
|
5
5
|
```ruby
|
6
|
-
obj.should
|
7
|
-
obj.should
|
6
|
+
obj.should be_truthy # passes if obj is truthy (not nil or false)
|
7
|
+
obj.should be_falsey # passes if obj is falsy (nil or false)
|
8
8
|
obj.should be_nil # passes if obj is nil
|
9
9
|
obj.should be # passes if obj is truthy (not nil or false)
|
10
10
|
```
|
11
11
|
|
12
|
-
Scenario:
|
13
|
-
Given a file named "
|
12
|
+
Scenario: be_truthy matcher
|
13
|
+
Given a file named "be_truthy_spec.rb" with:
|
14
14
|
"""ruby
|
15
|
-
describe "
|
16
|
-
specify { true.should
|
17
|
-
specify { 7.should
|
18
|
-
specify { "foo".should
|
19
|
-
specify { nil.should_not
|
20
|
-
specify { false.should_not
|
15
|
+
describe "be_truthy matcher" do
|
16
|
+
specify { true.should be_truthy }
|
17
|
+
specify { 7.should be_truthy }
|
18
|
+
specify { "foo".should be_truthy }
|
19
|
+
specify { nil.should_not be_truthy }
|
20
|
+
specify { false.should_not be_truthy }
|
21
21
|
|
22
22
|
# deliberate failures
|
23
|
-
specify { true.should_not
|
24
|
-
specify { 7.should_not
|
25
|
-
specify { "foo".should_not
|
26
|
-
specify { nil.should
|
27
|
-
specify { false.should
|
23
|
+
specify { true.should_not be_truthy }
|
24
|
+
specify { 7.should_not be_truthy }
|
25
|
+
specify { "foo".should_not be_truthy }
|
26
|
+
specify { nil.should be_truthy }
|
27
|
+
specify { false.should be_truthy }
|
28
28
|
end
|
29
29
|
"""
|
30
|
-
When I run `rspec
|
30
|
+
When I run `rspec be_truthy_spec.rb`
|
31
31
|
Then the output should contain "10 examples, 5 failures"
|
32
32
|
And the output should contain:
|
33
33
|
"""
|
34
|
-
expected:
|
34
|
+
expected: falsey value
|
35
35
|
got: true
|
36
36
|
"""
|
37
37
|
And the output should contain:
|
38
38
|
"""
|
39
|
-
expected:
|
39
|
+
expected: falsey value
|
40
40
|
got: 7
|
41
41
|
"""
|
42
42
|
And the output should contain:
|
43
43
|
"""
|
44
|
-
expected:
|
44
|
+
expected: falsey value
|
45
45
|
got: "foo"
|
46
46
|
"""
|
47
47
|
And the output should contain:
|
48
48
|
"""
|
49
|
-
expected:
|
49
|
+
expected: truthy value
|
50
50
|
got: nil
|
51
51
|
"""
|
52
52
|
And the output should contain:
|
53
53
|
"""
|
54
|
-
expected:
|
54
|
+
expected: truthy value
|
55
55
|
got: false
|
56
56
|
"""
|
57
57
|
|
58
|
-
Scenario:
|
59
|
-
Given a file named "
|
58
|
+
Scenario: be_falsey matcher
|
59
|
+
Given a file named "be_falsey_spec.rb" with:
|
60
60
|
"""ruby
|
61
|
-
describe "
|
62
|
-
specify { nil.should
|
63
|
-
specify { false.should
|
64
|
-
specify { true.should_not
|
65
|
-
specify { 7.should_not
|
66
|
-
specify { "foo".should_not
|
61
|
+
describe "be_falsey matcher" do
|
62
|
+
specify { nil.should be_falsey }
|
63
|
+
specify { false.should be_falsey }
|
64
|
+
specify { true.should_not be_falsey }
|
65
|
+
specify { 7.should_not be_falsey }
|
66
|
+
specify { "foo".should_not be_falsey }
|
67
67
|
|
68
68
|
# deliberate failures
|
69
|
-
specify { nil.should_not
|
70
|
-
specify { false.should_not
|
71
|
-
specify { true.should
|
72
|
-
specify { 7.should
|
73
|
-
specify { "foo".should
|
69
|
+
specify { nil.should_not be_falsey }
|
70
|
+
specify { false.should_not be_falsey }
|
71
|
+
specify { true.should be_falsey }
|
72
|
+
specify { 7.should be_falsey }
|
73
|
+
specify { "foo".should be_falsey }
|
74
74
|
end
|
75
75
|
"""
|
76
|
-
When I run `rspec
|
76
|
+
When I run `rspec be_falsey_spec.rb`
|
77
77
|
Then the output should contain "10 examples, 5 failures"
|
78
78
|
And the output should contain:
|
79
79
|
"""
|
80
|
-
expected:
|
80
|
+
expected: truthy value
|
81
81
|
got: nil
|
82
82
|
"""
|
83
83
|
And the output should contain:
|
84
84
|
"""
|
85
|
-
expected:
|
85
|
+
expected: truthy value
|
86
86
|
got: false
|
87
87
|
"""
|
88
88
|
And the output should contain:
|
89
89
|
"""
|
90
|
-
expected:
|
90
|
+
expected: falsey value
|
91
91
|
got: true
|
92
92
|
"""
|
93
93
|
And the output should contain:
|
94
94
|
"""
|
95
|
-
expected:
|
95
|
+
expected: falsey value
|
96
96
|
got: 7
|
97
97
|
"""
|
98
98
|
And the output should contain:
|
99
99
|
"""
|
100
|
-
expected:
|
100
|
+
expected: falsey value
|
101
101
|
got: "foo"
|
102
102
|
"""
|
103
103
|
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module RSpec
|
2
|
+
# Consistent implementation for "cleaning" the caller method to strip out
|
3
|
+
# non-rspec lines. This enables errors to be reported at the call site in
|
4
|
+
# the code using the library, which is far more useful than the particular
|
5
|
+
# internal method that raised an error.
|
6
|
+
class CallerFilter
|
7
|
+
|
8
|
+
RSPEC_LIBS = %w[
|
9
|
+
core
|
10
|
+
mocks
|
11
|
+
expectations
|
12
|
+
matchers
|
13
|
+
rails
|
14
|
+
]
|
15
|
+
|
16
|
+
ADDITIONAL_TOP_LEVEL_FILES = %w[ autorun ]
|
17
|
+
|
18
|
+
LIB_REGEX = %r{/lib/rspec/(#{(RSPEC_LIBS + ADDITIONAL_TOP_LEVEL_FILES).join('|')})(\.rb|/)}
|
19
|
+
|
20
|
+
if RUBY_VERSION >= '2.0.0'
|
21
|
+
def self.first_non_rspec_line
|
22
|
+
# `caller` is an expensive method that scales linearly with the size of
|
23
|
+
# the stack. The performance hit for fetching it in chunks is small,
|
24
|
+
# and since the target line is probably near the top of the stack, the
|
25
|
+
# overall improvement of a chunked search like this is significant.
|
26
|
+
#
|
27
|
+
# See benchmarks/caller.rb for measurements.
|
28
|
+
|
29
|
+
# Initial value here is mostly arbitrary, but is chosen to give good
|
30
|
+
# performance on the common case of creating a double.
|
31
|
+
increment = 5
|
32
|
+
i = 1
|
33
|
+
line = nil
|
34
|
+
|
35
|
+
while !line
|
36
|
+
stack = caller(i, increment)
|
37
|
+
raise "No non-lib lines in stack" unless stack
|
38
|
+
|
39
|
+
line = stack.find { |l| l !~ LIB_REGEX }
|
40
|
+
|
41
|
+
i += increment
|
42
|
+
increment *= 2 # The choice of two here is arbitrary.
|
43
|
+
end
|
44
|
+
|
45
|
+
line
|
46
|
+
end
|
47
|
+
else
|
48
|
+
# Earlier rubies do not support the two argument form of `caller`. This
|
49
|
+
# fallback is logically the same, but slower.
|
50
|
+
def self.first_non_rspec_line
|
51
|
+
caller.find { |line| line !~ LIB_REGEX }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -1,27 +1,15 @@
|
|
1
|
+
require 'rspec/expectations/caller_filter' unless defined?(::RSpec::CallerFilter)
|
2
|
+
|
1
3
|
module RSpec
|
2
4
|
module Expectations
|
3
5
|
module Deprecation
|
4
|
-
RSPEC_LIBS = %w[
|
5
|
-
core
|
6
|
-
mocks
|
7
|
-
expectations
|
8
|
-
matchers
|
9
|
-
rails
|
10
|
-
]
|
11
|
-
|
12
|
-
ADDITIONAL_TOP_LEVEL_FILES = %w[ autorun ]
|
13
|
-
|
14
|
-
LIB_REGEX = %r{/lib/rspec/(#{(RSPEC_LIBS + ADDITIONAL_TOP_LEVEL_FILES).join('|')})(\.rb|/)}
|
15
|
-
|
16
6
|
# @private
|
17
7
|
#
|
18
8
|
# Used internally to print deprecation warnings
|
19
9
|
def deprecate(deprecated, options={})
|
20
|
-
call_site = caller.find { |line| line !~ LIB_REGEX }
|
21
|
-
|
22
10
|
message = "DEPRECATION: #{deprecated} is deprecated."
|
23
11
|
message << " Use #{options[:replacement]} instead." if options[:replacement]
|
24
|
-
message << " Called from #{
|
12
|
+
message << " Called from #{CallerFilter.first_non_rspec_line}."
|
25
13
|
warn message
|
26
14
|
end
|
27
15
|
end
|
data/lib/rspec/matchers.rb
CHANGED
@@ -183,24 +183,42 @@ module RSpec
|
|
183
183
|
obj.respond_to?(:failure_message_for_should) || obj.respond_to?(:failure_message)
|
184
184
|
end
|
185
185
|
|
186
|
-
# Passes if actual is truthy (anything but false or nil)
|
187
186
|
def be_true
|
188
|
-
|
187
|
+
RSpec.deprecate("`be_true`", :replacement =>
|
188
|
+
"`be_truthy` (for Ruby's conditional semantics) or " +
|
189
|
+
"`be true` (for exact `== true` equality)"
|
190
|
+
)
|
191
|
+
BuiltIn::BeTruthy.new
|
189
192
|
end
|
190
193
|
|
191
|
-
# Passes if actual is falsy (false or nil)
|
192
194
|
def be_false
|
193
|
-
|
195
|
+
RSpec.deprecate("`be_false`", :replacement =>
|
196
|
+
"`be_falsey` (for Ruby's conditional semantics) or " +
|
197
|
+
"`be false` (for exact `== false` equality)"
|
198
|
+
)
|
199
|
+
BuiltIn::BeFalsey.new
|
200
|
+
end
|
201
|
+
|
202
|
+
# Passes if actual is truthy (anything but false or nil)
|
203
|
+
def be_truthy
|
204
|
+
BuiltIn::BeTruthy.new
|
194
205
|
end
|
195
206
|
|
207
|
+
# Passes if actual is falsy (false or nil)
|
208
|
+
def be_falsey
|
209
|
+
BuiltIn::BeFalsey.new
|
210
|
+
end
|
211
|
+
|
212
|
+
alias_method :be_falsy, :be_falsey
|
213
|
+
|
196
214
|
# Passes if actual is nil
|
197
215
|
def be_nil
|
198
216
|
BuiltIn::BeNil.new
|
199
217
|
end
|
200
218
|
|
201
219
|
# @example
|
202
|
-
# expect(actual).to
|
203
|
-
# expect(actual).to
|
220
|
+
# expect(actual).to be_truthy
|
221
|
+
# expect(actual).to be_falsey
|
204
222
|
# expect(actual).to be_nil
|
205
223
|
# expect(actual).to be_[arbitrary_predicate](*args)
|
206
224
|
# expect(actual).not_to be_nil
|
@@ -4,8 +4,8 @@ module RSpec
|
|
4
4
|
require 'rspec/matchers/built_in/base_matcher'
|
5
5
|
autoload :BeAnInstanceOf, 'rspec/matchers/built_in/be_instance_of'
|
6
6
|
autoload :Be, 'rspec/matchers/built_in/be'
|
7
|
-
autoload :
|
8
|
-
autoload :
|
7
|
+
autoload :BeTruthy, 'rspec/matchers/built_in/be'
|
8
|
+
autoload :BeFalsey, 'rspec/matchers/built_in/be'
|
9
9
|
autoload :BeNil, 'rspec/matchers/built_in/be'
|
10
10
|
autoload :BeComparedTo, 'rspec/matchers/built_in/be'
|
11
11
|
autoload :BePredicate, 'rspec/matchers/built_in/be'
|
@@ -14,12 +14,10 @@ module RSpec
|
|
14
14
|
class BaseMatcher
|
15
15
|
include RSpec::Matchers::Pretty
|
16
16
|
|
17
|
-
UNDEFINED = Object.new.freeze
|
18
|
-
|
19
17
|
attr_reader :actual, :expected, :rescued_exception
|
20
18
|
|
21
|
-
def initialize(expected =
|
22
|
-
@expected = expected
|
19
|
+
def initialize(expected = nil)
|
20
|
+
@expected = expected
|
23
21
|
end
|
24
22
|
|
25
23
|
def matches?(actual)
|
@@ -38,17 +36,17 @@ module RSpec
|
|
38
36
|
end
|
39
37
|
|
40
38
|
def failure_message_for_should
|
41
|
-
assert_ivars :@actual
|
39
|
+
assert_ivars :@actual, :@expected
|
42
40
|
"expected #{@actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
|
43
41
|
end
|
44
42
|
|
45
43
|
def failure_message_for_should_not
|
46
|
-
assert_ivars :@actual
|
44
|
+
assert_ivars :@actual, :@expected
|
47
45
|
"expected #{@actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"
|
48
46
|
end
|
49
47
|
|
50
48
|
def description
|
51
|
-
|
49
|
+
expected ? "#{name_to_sentence} #{@expected.inspect}" : name_to_sentence
|
52
50
|
end
|
53
51
|
|
54
52
|
def diffable?
|
@@ -3,31 +3,31 @@ require 'rspec/matchers/dsl'
|
|
3
3
|
module RSpec
|
4
4
|
module Matchers
|
5
5
|
module BuiltIn
|
6
|
-
class
|
6
|
+
class BeTruthy < BaseMatcher
|
7
7
|
def match(_, actual)
|
8
8
|
!!actual
|
9
9
|
end
|
10
10
|
|
11
11
|
def failure_message_for_should
|
12
|
-
"expected:
|
12
|
+
"expected: truthy value\n got: #{actual.inspect}"
|
13
13
|
end
|
14
14
|
|
15
15
|
def failure_message_for_should_not
|
16
|
-
"expected:
|
16
|
+
"expected: falsey value\n got: #{actual.inspect}"
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
class
|
20
|
+
class BeFalsey < BaseMatcher
|
21
21
|
def match(_, actual)
|
22
22
|
!actual
|
23
23
|
end
|
24
24
|
|
25
25
|
def failure_message_for_should
|
26
|
-
"expected:
|
26
|
+
"expected: falsey value\n got: #{actual.inspect}"
|
27
27
|
end
|
28
28
|
|
29
29
|
def failure_message_for_should_not
|
30
|
-
"expected:
|
30
|
+
"expected: truthy value\n got: #{actual.inspect}"
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -141,6 +141,13 @@ it is a bit confusing.
|
|
141
141
|
|
142
142
|
def matches?(actual)
|
143
143
|
@actual = actual
|
144
|
+
|
145
|
+
if is_private_on?( @actual )
|
146
|
+
RSpec.deprecate "matching with be_#{predicate.to_s.gsub(/\?$/,'')} on private method #{predicate}",
|
147
|
+
:replacement => "`expect(object.send(#{predicate.inspect})).to be_true` or change the method's visibility to public",
|
148
|
+
:call_site => caller(0)[3]
|
149
|
+
end
|
150
|
+
|
144
151
|
begin
|
145
152
|
return @result = actual.__send__(predicate, *@args, &@block)
|
146
153
|
rescue NameError => predicate_missing_error
|
@@ -170,6 +177,17 @@ it is a bit confusing.
|
|
170
177
|
|
171
178
|
private
|
172
179
|
|
180
|
+
# support 1.8.7
|
181
|
+
if methods.first.is_a? String
|
182
|
+
def is_private_on? actual
|
183
|
+
actual.private_methods.include? predicate.to_s
|
184
|
+
end
|
185
|
+
else
|
186
|
+
def is_private_on? actual
|
187
|
+
actual.private_methods.include? predicate
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
173
191
|
def predicate
|
174
192
|
"#{@expected}?".to_sym
|
175
193
|
end
|