rspec-expectations 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -2
- data/.yardopts +0 -1
- data/Changelog.md +115 -35
- data/README.md +2 -2
- data/lib/rspec/expectations.rb +13 -8
- data/lib/rspec/{matchers → expectations}/configuration.rb +38 -13
- data/lib/rspec/expectations/expectation_target.rb +72 -8
- data/lib/rspec/expectations/fail_with.rb +10 -52
- data/lib/rspec/expectations/handler.rb +9 -11
- data/lib/rspec/expectations/syntax.rb +37 -35
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +60 -9
- data/lib/rspec/matchers/aliased_matcher.rb +6 -0
- data/lib/rspec/matchers/built_in.rb +9 -1
- data/lib/rspec/matchers/built_in/all.rb +78 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +39 -1
- data/lib/rspec/matchers/built_in/be.rb +117 -42
- data/lib/rspec/matchers/built_in/be_between.rb +22 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
- data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
- data/lib/rspec/matchers/built_in/be_within.rb +26 -6
- data/lib/rspec/matchers/built_in/change.rb +89 -13
- data/lib/rspec/matchers/built_in/compound.rb +19 -3
- data/lib/rspec/matchers/built_in/contain_exactly.rb +17 -6
- data/lib/rspec/matchers/built_in/cover.rb +3 -0
- data/lib/rspec/matchers/built_in/eq.rb +20 -5
- data/lib/rspec/matchers/built_in/eql.rb +15 -3
- data/lib/rspec/matchers/built_in/equal.rb +23 -6
- data/lib/rspec/matchers/built_in/exist.rb +74 -10
- data/lib/rspec/matchers/built_in/has.rb +58 -3
- data/lib/rspec/matchers/built_in/include.rb +16 -1
- data/lib/rspec/matchers/built_in/match.rb +14 -4
- data/lib/rspec/matchers/built_in/operators.rb +16 -0
- data/lib/rspec/matchers/built_in/output.rb +47 -5
- data/lib/rspec/matchers/built_in/raise_error.rb +40 -23
- data/lib/rspec/matchers/built_in/respond_to.rb +37 -16
- data/lib/rspec/matchers/built_in/satisfy.rb +15 -0
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +29 -14
- data/lib/rspec/matchers/built_in/throw_symbol.rb +32 -3
- data/lib/rspec/matchers/built_in/yield.rb +148 -44
- data/lib/rspec/matchers/composable.rb +48 -7
- data/lib/rspec/matchers/dsl.rb +45 -17
- data/lib/rspec/matchers/generated_descriptions.rb +7 -0
- data/lib/rspec/matchers/matcher_delegator.rb +6 -2
- data/lib/rspec/matchers/pretty.rb +15 -19
- metadata +33 -236
- metadata.gz.sig +0 -0
- data/features/README.md +0 -48
- data/features/Upgrade.md +0 -53
- data/features/built_in_matchers/README.md +0 -96
- data/features/built_in_matchers/be.feature +0 -175
- data/features/built_in_matchers/be_within.feature +0 -48
- data/features/built_in_matchers/comparisons.feature +0 -97
- data/features/built_in_matchers/contain_exactly.feature +0 -46
- data/features/built_in_matchers/cover.feature +0 -47
- data/features/built_in_matchers/end_with.feature +0 -48
- data/features/built_in_matchers/equality.feature +0 -136
- data/features/built_in_matchers/exist.feature +0 -45
- data/features/built_in_matchers/expect_change.feature +0 -59
- data/features/built_in_matchers/expect_error.feature +0 -144
- data/features/built_in_matchers/include.feature +0 -126
- data/features/built_in_matchers/match.feature +0 -51
- data/features/built_in_matchers/output.feature +0 -70
- data/features/built_in_matchers/predicates.feature +0 -161
- data/features/built_in_matchers/respond_to.feature +0 -84
- data/features/built_in_matchers/satisfy.feature +0 -33
- data/features/built_in_matchers/start_with.feature +0 -48
- data/features/built_in_matchers/throw_symbol.feature +0 -91
- data/features/built_in_matchers/types.feature +0 -116
- data/features/built_in_matchers/yield.feature +0 -161
- data/features/composing_matchers.feature +0 -250
- data/features/compound_expectations.feature +0 -45
- data/features/custom_matchers/access_running_example.feature +0 -53
- data/features/custom_matchers/define_diffable_matcher.feature +0 -27
- data/features/custom_matchers/define_matcher.feature +0 -340
- data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -34
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
- data/features/customized_message.feature +0 -39
- data/features/diffing.feature +0 -85
- data/features/implicit_docstrings.feature +0 -52
- data/features/step_definitions/additional_cli_steps.rb +0 -22
- data/features/support/env.rb +0 -21
- data/features/support/rubinius.rb +0 -6
- data/features/syntax_configuration.feature +0 -71
- data/features/test_frameworks/minitest.feature +0 -44
- data/lib/rspec-expectations.rb +0 -1
- data/lib/rspec/expectations/diff_presenter.rb +0 -141
- data/lib/rspec/expectations/differ.rb +0 -44
- data/lib/rspec/expectations/encoded_string.rb +0 -56
- data/spec/rspec/expectations/diff_presenter_spec.rb +0 -249
- data/spec/rspec/expectations/encoded_string_spec.rb +0 -74
- data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
- data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
- data/spec/rspec/expectations/fail_with_spec.rb +0 -114
- data/spec/rspec/expectations/handler_spec.rb +0 -205
- data/spec/rspec/expectations/minitest_integration_spec.rb +0 -27
- data/spec/rspec/expectations/syntax_spec.rb +0 -89
- data/spec/rspec/expectations_spec.rb +0 -12
- data/spec/rspec/matchers/aliased_matcher_spec.rb +0 -48
- data/spec/rspec/matchers/aliases_spec.rb +0 -449
- data/spec/rspec/matchers/built_in/base_matcher_spec.rb +0 -83
- data/spec/rspec/matchers/built_in/be_between_spec.rb +0 -159
- data/spec/rspec/matchers/built_in/be_instance_of_spec.rb +0 -63
- data/spec/rspec/matchers/built_in/be_kind_of_spec.rb +0 -41
- data/spec/rspec/matchers/built_in/be_spec.rb +0 -592
- data/spec/rspec/matchers/built_in/be_within_spec.rb +0 -141
- data/spec/rspec/matchers/built_in/change_spec.rb +0 -808
- data/spec/rspec/matchers/built_in/compound_spec.rb +0 -292
- data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +0 -441
- data/spec/rspec/matchers/built_in/cover_spec.rb +0 -69
- data/spec/rspec/matchers/built_in/eq_spec.rb +0 -156
- data/spec/rspec/matchers/built_in/eql_spec.rb +0 -41
- data/spec/rspec/matchers/built_in/equal_spec.rb +0 -106
- data/spec/rspec/matchers/built_in/exist_spec.rb +0 -124
- data/spec/rspec/matchers/built_in/has_spec.rb +0 -161
- data/spec/rspec/matchers/built_in/include_spec.rb +0 -540
- data/spec/rspec/matchers/built_in/match_spec.rb +0 -102
- data/spec/rspec/matchers/built_in/operators_spec.rb +0 -252
- data/spec/rspec/matchers/built_in/output_spec.rb +0 -165
- data/spec/rspec/matchers/built_in/raise_error_spec.rb +0 -461
- data/spec/rspec/matchers/built_in/respond_to_spec.rb +0 -292
- data/spec/rspec/matchers/built_in/satisfy_spec.rb +0 -44
- data/spec/rspec/matchers/built_in/start_and_end_with_spec.rb +0 -253
- data/spec/rspec/matchers/built_in/throw_symbol_spec.rb +0 -135
- data/spec/rspec/matchers/built_in/yield_spec.rb +0 -627
- data/spec/rspec/matchers/configuration_spec.rb +0 -213
- data/spec/rspec/matchers/description_generation_spec.rb +0 -191
- data/spec/rspec/matchers/dsl_spec.rb +0 -895
- data/spec/rspec/matchers/legacy_spec.rb +0 -101
- data/spec/rspec/matchers_spec.rb +0 -74
- data/spec/spec_helper.rb +0 -85
- data/spec/support/matchers.rb +0 -22
- data/spec/support/shared_examples.rb +0 -35
@@ -1,18 +1,43 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Expectations
|
3
3
|
# Wraps the target of an expectation.
|
4
|
+
#
|
4
5
|
# @example
|
5
|
-
# expect(something)
|
6
|
+
# expect(something) # => ExpectationTarget wrapping something
|
7
|
+
# expect { do_something } # => ExpectationTarget wrapping the block
|
6
8
|
#
|
7
9
|
# # used with `to`
|
8
10
|
# expect(actual).to eq(3)
|
9
11
|
#
|
10
12
|
# # with `not_to`
|
11
13
|
# expect(actual).not_to eq(3)
|
14
|
+
#
|
15
|
+
# @note `ExpectationTarget` is not intended to be instantiated
|
16
|
+
# directly by users. Use `expect` instead.
|
12
17
|
class ExpectationTarget
|
18
|
+
# @private
|
19
|
+
# Used as a sentinel value to be able to tell when the user
|
20
|
+
# did not pass an argument. We can't use `nil` for that because
|
21
|
+
# `nil` is a valid value to pass.
|
22
|
+
UndefinedValue = Module.new
|
23
|
+
|
13
24
|
# @api private
|
14
|
-
def initialize(
|
15
|
-
@target =
|
25
|
+
def initialize(value)
|
26
|
+
@target = value
|
27
|
+
end
|
28
|
+
|
29
|
+
# @private
|
30
|
+
def self.for(value, block)
|
31
|
+
if UndefinedValue.equal?(value)
|
32
|
+
unless block
|
33
|
+
raise ArgumentError, "You must pass either an argument or a block to `expect`."
|
34
|
+
end
|
35
|
+
BlockExpectationTarget.new(block)
|
36
|
+
elsif block
|
37
|
+
raise ArgumentError, "You cannot pass both an argument and a block to `expect`."
|
38
|
+
else
|
39
|
+
new(value)
|
40
|
+
end
|
16
41
|
end
|
17
42
|
|
18
43
|
# Runs the given expectation, passing if `matcher` returns true.
|
@@ -25,7 +50,7 @@ module RSpec
|
|
25
50
|
# @return [Boolean] true if the expectation succeeds (else raises)
|
26
51
|
# @see RSpec::Matchers
|
27
52
|
def to(matcher=nil, message=nil, &block)
|
28
|
-
prevent_operator_matchers(:to
|
53
|
+
prevent_operator_matchers(:to) unless matcher
|
29
54
|
RSpec::Expectations::PositiveExpectationHandler.handle_matcher(@target, matcher, message, &block)
|
30
55
|
end
|
31
56
|
|
@@ -38,20 +63,59 @@ module RSpec
|
|
38
63
|
# @return [Boolean] false if the negative expectation succeeds (else raises)
|
39
64
|
# @see RSpec::Matchers
|
40
65
|
def not_to(matcher=nil, message=nil, &block)
|
41
|
-
prevent_operator_matchers(:not_to
|
66
|
+
prevent_operator_matchers(:not_to) unless matcher
|
42
67
|
RSpec::Expectations::NegativeExpectationHandler.handle_matcher(@target, matcher, message, &block)
|
43
68
|
end
|
44
69
|
alias to_not not_to
|
45
70
|
|
46
71
|
private
|
47
72
|
|
48
|
-
def prevent_operator_matchers(verb
|
49
|
-
return if matcher
|
50
|
-
|
73
|
+
def prevent_operator_matchers(verb)
|
51
74
|
raise ArgumentError, "The expect syntax does not support operator matchers, " +
|
52
75
|
"so you must pass a matcher to `##{verb}`."
|
53
76
|
end
|
54
77
|
end
|
78
|
+
|
79
|
+
# @private
|
80
|
+
# Validates the provided matcher to ensure it supports block
|
81
|
+
# expectations, in order to avoid user confusion when they
|
82
|
+
# use a block thinking the expectation will be on the return
|
83
|
+
# value of the block rather than the block itself.
|
84
|
+
class BlockExpectationTarget < ExpectationTarget
|
85
|
+
def to(matcher, message=nil, &block)
|
86
|
+
enforce_block_expectation(matcher)
|
87
|
+
super
|
88
|
+
end
|
89
|
+
|
90
|
+
def not_to(matcher, message=nil, &block)
|
91
|
+
enforce_block_expectation(matcher)
|
92
|
+
super
|
93
|
+
end
|
94
|
+
alias to_not not_to
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def enforce_block_expectation(matcher)
|
99
|
+
return if supports_block_expectations?(matcher)
|
100
|
+
|
101
|
+
raise ExpectationNotMetError,
|
102
|
+
"You must pass an argument rather than a block to use the provided " +
|
103
|
+
"matcher (#{description_of matcher}), or the matcher must implement " +
|
104
|
+
"`supports_block_expectations?`."
|
105
|
+
end
|
106
|
+
|
107
|
+
def supports_block_expectations?(matcher)
|
108
|
+
matcher.supports_block_expectations?
|
109
|
+
rescue NoMethodError
|
110
|
+
false
|
111
|
+
end
|
112
|
+
|
113
|
+
def description_of(matcher)
|
114
|
+
matcher.description
|
115
|
+
rescue NoMethodError
|
116
|
+
matcher.inspect
|
117
|
+
end
|
118
|
+
end
|
55
119
|
end
|
56
120
|
end
|
57
121
|
|
@@ -1,9 +1,14 @@
|
|
1
|
+
RSpec::Support.require_rspec_support 'differ'
|
2
|
+
|
1
3
|
module RSpec
|
2
4
|
module Expectations
|
3
5
|
class << self
|
4
6
|
# @private
|
5
7
|
def differ
|
6
|
-
|
8
|
+
RSpec::Support::Differ.new(
|
9
|
+
:object_preparer => lambda {|object| RSpec::Matchers::Composable.surface_descriptions_in(object) },
|
10
|
+
:color => RSpec::Matchers.configuration.color?
|
11
|
+
)
|
7
12
|
end
|
8
13
|
|
9
14
|
# Raises an RSpec::Expectations::ExpectationNotMetError with message.
|
@@ -19,60 +24,13 @@ module RSpec
|
|
19
24
|
"appropriate failure_message[_when_negated] method to return a string?"
|
20
25
|
end
|
21
26
|
|
22
|
-
|
23
|
-
if all_strings?(actual, expected)
|
24
|
-
if any_multiline_strings?(actual, expected)
|
25
|
-
message << "\nDiff:" << differ.diff_as_string(coerce_to_string(actual), coerce_to_string(expected))
|
26
|
-
end
|
27
|
-
elsif no_procs?(actual, expected) && no_numbers?(actual, expected)
|
28
|
-
message << "\nDiff:" << differ.diff_as_object(actual, expected)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
raise(RSpec::Expectations::ExpectationNotMetError.new(message))
|
33
|
-
end
|
34
|
-
|
35
|
-
private
|
36
|
-
|
37
|
-
def no_procs?(*args)
|
38
|
-
args.flatten.none? {|a| Proc === a}
|
39
|
-
end
|
40
|
-
|
41
|
-
def all_strings?(*args)
|
42
|
-
args.flatten.all? {|a| String === a}
|
43
|
-
end
|
44
|
-
|
45
|
-
def any_multiline_strings?(*args)
|
46
|
-
all_strings?(*args) && args.flatten.any? { |a| multiline?(a) }
|
47
|
-
end
|
27
|
+
diff = differ.diff(actual, expected)
|
48
28
|
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
def coerce_to_string(string_or_array)
|
54
|
-
return string_or_array unless Array === string_or_array
|
55
|
-
diffably_stringify(string_or_array).join("\n")
|
56
|
-
end
|
57
|
-
|
58
|
-
def diffably_stringify(array)
|
59
|
-
array.map do |entry|
|
60
|
-
if Array === entry
|
61
|
-
entry.inspect
|
62
|
-
else
|
63
|
-
entry.to_s.gsub("\n", "\\n")
|
64
|
-
end
|
29
|
+
unless diff.empty?
|
30
|
+
message = "#{message}\nDiff:#{diff}"
|
65
31
|
end
|
66
|
-
end
|
67
32
|
|
68
|
-
|
69
|
-
def multiline?(string)
|
70
|
-
string.include?("\n".encode(string.encoding))
|
71
|
-
end
|
72
|
-
else
|
73
|
-
def multiline?(string)
|
74
|
-
string.include?("\n")
|
75
|
-
end
|
33
|
+
raise RSpec::Expectations::ExpectationNotMetError.new(message)
|
76
34
|
end
|
77
35
|
end
|
78
36
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Expectations
|
3
3
|
|
4
|
-
# @
|
5
|
-
|
4
|
+
# @private
|
5
|
+
module ExpectationHelper
|
6
6
|
def self.check_message(msg)
|
7
7
|
unless msg.nil? || msg.respond_to?(:to_str) || msg.respond_to?(:call)
|
8
8
|
::Kernel.warn [
|
@@ -16,7 +16,7 @@ module RSpec
|
|
16
16
|
# Returns an RSpec-3+ compatible matcher, wrapping a legacy one
|
17
17
|
# in an adapter if necessary.
|
18
18
|
#
|
19
|
-
# @
|
19
|
+
# @private
|
20
20
|
def self.modern_matcher_from(matcher)
|
21
21
|
LegacyMacherAdapter::RSpec2.wrap(matcher) ||
|
22
22
|
LegacyMacherAdapter::RSpec1.wrap(matcher) || matcher
|
@@ -40,7 +40,7 @@ module RSpec
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
# @
|
43
|
+
# @private
|
44
44
|
class PositiveExpectationHandler
|
45
45
|
def self.handle_matcher(actual, initial_matcher, message=nil, &block)
|
46
46
|
matcher = ExpectationHelper.setup(self, initial_matcher, message)
|
@@ -62,7 +62,7 @@ module RSpec
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
# @
|
65
|
+
# @private
|
66
66
|
class NegativeExpectationHandler
|
67
67
|
def self.handle_matcher(actual, initial_matcher, message=nil, &block)
|
68
68
|
matcher = ExpectationHelper.setup(self, initial_matcher, message)
|
@@ -95,17 +95,15 @@ module RSpec
|
|
95
95
|
# Wraps a matcher written against one of the legacy protocols in
|
96
96
|
# order to present the current protocol.
|
97
97
|
#
|
98
|
-
# @
|
98
|
+
# @private
|
99
99
|
class LegacyMacherAdapter < Matchers::MatcherDelegator
|
100
100
|
def initialize(matcher)
|
101
101
|
super
|
102
|
-
::RSpec.warn_deprecation(<<-EOS.gsub(/^\s+\|/, ''))
|
103
|
-
|--------------------------------------------------------------------------
|
102
|
+
::RSpec.warn_deprecation(<<-EOS.gsub(/^\s+\|/, ''), :type => "legacy_matcher")
|
104
103
|
|#{matcher.class.name || matcher.inspect} implements a legacy RSpec matcher
|
105
104
|
|protocol. For the current protocol you should expose the failure messages
|
106
105
|
|via the `failure_message` and `failure_message_when_negated` methods.
|
107
106
|
|(Used from #{CallerFilter.first_non_rspec_line})
|
108
|
-
|--------------------------------------------------------------------------
|
109
107
|
EOS
|
110
108
|
end
|
111
109
|
|
@@ -117,7 +115,7 @@ module RSpec
|
|
117
115
|
# the failure message protocol was:
|
118
116
|
# * `failure_message_for_should`
|
119
117
|
# * `failure_message_for_should_not`
|
120
|
-
# @
|
118
|
+
# @private
|
121
119
|
class RSpec2 < self
|
122
120
|
def failure_message
|
123
121
|
base_matcher.failure_message_for_should
|
@@ -141,7 +139,7 @@ module RSpec
|
|
141
139
|
# Before RSpec 1.2, the failure message protocol was:
|
142
140
|
# * `failure_message`
|
143
141
|
# * `negative_failure_message`
|
144
|
-
# @
|
142
|
+
# @private
|
145
143
|
class RSpec1 < self
|
146
144
|
def failure_message
|
147
145
|
base_matcher.failure_message
|
@@ -6,47 +6,22 @@ module RSpec
|
|
6
6
|
module Syntax
|
7
7
|
module_function
|
8
8
|
|
9
|
-
# @method should
|
10
|
-
# Passes if `matcher` returns true. Available on every `Object`.
|
11
|
-
# @example
|
12
|
-
# actual.should eq expected
|
13
|
-
# actual.should match /expression/
|
14
|
-
# @param [Matcher]
|
15
|
-
# matcher
|
16
|
-
# @param [String] message optional message to display when the expectation fails
|
17
|
-
# @return [Boolean] true if the expectation succeeds (else raises)
|
18
|
-
# @see RSpec::Matchers
|
19
|
-
|
20
|
-
# @method should_not
|
21
|
-
# Passes if `matcher` returns false. Available on every `Object`.
|
22
|
-
# @example
|
23
|
-
# actual.should_not eq expected
|
24
|
-
# @param [Matcher]
|
25
|
-
# matcher
|
26
|
-
# @param [String] message optional message to display when the expectation fails
|
27
|
-
# @return [Boolean] false if the negative expectation succeeds (else raises)
|
28
|
-
# @see RSpec::Matchers
|
29
|
-
|
30
|
-
# @method expect
|
31
|
-
# Supports `expect(actual).to matcher` syntax by wrapping `actual` in an
|
32
|
-
# `ExpectationTarget`.
|
33
|
-
# @example
|
34
|
-
# expect(actual).to eq(expected)
|
35
|
-
# expect(actual).not_to eq(expected)
|
36
|
-
# @return [ExpectationTarget]
|
37
|
-
# @see ExpectationTarget#to
|
38
|
-
# @see ExpectationTarget#not_to
|
39
|
-
|
40
9
|
# @api private
|
41
10
|
# Determines where we add `should` and `should_not`.
|
42
11
|
def default_should_host
|
43
12
|
@default_should_host ||= ::Object.ancestors.last
|
44
13
|
end
|
45
14
|
|
15
|
+
# @api private
|
16
|
+
# Instructs rspec-expectations to warn on first usage of `should` or `should_not`.
|
17
|
+
# Enabled by default. This is largely here to facilitate testing.
|
46
18
|
def warn_about_should!
|
47
19
|
@warn_about_should = true
|
48
20
|
end
|
49
21
|
|
22
|
+
# @api private
|
23
|
+
# Generates a deprecation warning for the given method if no warning
|
24
|
+
# has already been issued.
|
50
25
|
def warn_about_should_unless_configured(method_name)
|
51
26
|
if @warn_about_should
|
52
27
|
RSpec.deprecate(
|
@@ -94,10 +69,8 @@ module RSpec
|
|
94
69
|
return if expect_enabled?(syntax_host)
|
95
70
|
|
96
71
|
syntax_host.module_exec do
|
97
|
-
def expect(
|
98
|
-
|
99
|
-
raise ArgumentError.new("You must pass an argument or a block to #expect but not both.") unless target.size == 1
|
100
|
-
::RSpec::Expectations::ExpectationTarget.new(target.first)
|
72
|
+
def expect(value=::RSpec::Expectations::ExpectationTarget::UndefinedValue, &block)
|
73
|
+
::RSpec::Expectations::ExpectationTarget.for(value, block)
|
101
74
|
end
|
102
75
|
end
|
103
76
|
end
|
@@ -126,3 +99,32 @@ module RSpec
|
|
126
99
|
end
|
127
100
|
end
|
128
101
|
end
|
102
|
+
|
103
|
+
# The legacy `:should` syntax adds the following methods directly to
|
104
|
+
# `BasicObject` so that they are available off of any object. Note, however,
|
105
|
+
# that this syntax does not always play nice with delegate/proxy objects.
|
106
|
+
# We recommend you use the non-monkeypatching `:expect` syntax instead.
|
107
|
+
class BasicObject
|
108
|
+
# @method should
|
109
|
+
# Passes if `matcher` returns true. Available on every `Object`.
|
110
|
+
# @example
|
111
|
+
# actual.should eq expected
|
112
|
+
# actual.should match /expression/
|
113
|
+
# @param [Matcher]
|
114
|
+
# matcher
|
115
|
+
# @param [String] message optional message to display when the expectation fails
|
116
|
+
# @return [Boolean] true if the expectation succeeds (else raises)
|
117
|
+
# @note This is only available when you have enabled the `:should` syntax.
|
118
|
+
# @see RSpec::Matchers
|
119
|
+
|
120
|
+
# @method should_not
|
121
|
+
# Passes if `matcher` returns false. Available on every `Object`.
|
122
|
+
# @example
|
123
|
+
# actual.should_not eq expected
|
124
|
+
# @param [Matcher]
|
125
|
+
# matcher
|
126
|
+
# @param [String] message optional message to display when the expectation fails
|
127
|
+
# @return [Boolean] false if the negative expectation succeeds (else raises)
|
128
|
+
# @note This is only available when you have enabled the `:should` syntax.
|
129
|
+
# @see RSpec::Matchers
|
130
|
+
end
|
data/lib/rspec/matchers.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
|
-
require 'rspec/
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
require 'rspec/support'
|
2
|
+
RSpec::Support.define_optimized_require_for_rspec(:matchers) { |f| require_relative(f) }
|
3
|
+
|
4
|
+
%w[
|
5
|
+
pretty
|
6
|
+
composable
|
7
|
+
built_in
|
8
|
+
generated_descriptions
|
9
|
+
dsl
|
10
|
+
matcher_delegator
|
11
|
+
aliased_matcher
|
12
|
+
].each { |file| RSpec::Support.require_rspec_matchers(file) }
|
13
|
+
|
14
|
+
# RSpec's top level namespace. All of rspec-expectations is contained
|
15
|
+
# in the `RSpec::Expectations` and `RSpec::Matchers` namespaces.
|
9
16
|
module RSpec
|
10
17
|
# RSpec::Matchers provides a number of useful matchers we use to define
|
11
18
|
# expectations. A matcher is any object that responds to the following:
|
@@ -18,6 +25,7 @@ module RSpec
|
|
18
25
|
# does_not_match?(actual)
|
19
26
|
# failure_message_when_negated
|
20
27
|
# description
|
28
|
+
# supports_block_expectations?
|
21
29
|
#
|
22
30
|
# ## Predicates
|
23
31
|
#
|
@@ -202,6 +210,16 @@ module RSpec
|
|
202
210
|
# for readable failure messages. You can alias your custom matchers in similar fashion
|
203
211
|
# using {RSpec::Matchers.alias_matcher}.
|
204
212
|
module Matchers
|
213
|
+
# @method expect
|
214
|
+
# Supports `expect(actual).to matcher` syntax by wrapping `actual` in an
|
215
|
+
# `ExpectationTarget`.
|
216
|
+
# @example
|
217
|
+
# expect(actual).to eq(expected)
|
218
|
+
# expect(actual).not_to eq(expected)
|
219
|
+
# @return [ExpectationTarget]
|
220
|
+
# @see ExpectationTarget#to
|
221
|
+
# @see ExpectationTarget#not_to
|
222
|
+
|
205
223
|
# Defines a matcher alias. The returned matcher's `description` will be overriden
|
206
224
|
# to reflect the phrasing of the new name, which will be used in failure messages
|
207
225
|
# when passed as an argument to another matcher in a composed matcher expression.
|
@@ -227,6 +245,10 @@ module RSpec
|
|
227
245
|
#
|
228
246
|
# be_sorted_by(:age).description # => "be sorted by age"
|
229
247
|
# a_list_sorted_by(:age).description # => "a list sorted by age"
|
248
|
+
#
|
249
|
+
# @!macro [attach] alias_matcher
|
250
|
+
# @!parse
|
251
|
+
# alias $1 $2
|
230
252
|
def self.alias_matcher(new_name, old_name, &description_override)
|
231
253
|
description_override ||= lambda do |old_desc|
|
232
254
|
old_desc.gsub(Pretty.split_words(old_name), Pretty.split_words(new_name))
|
@@ -559,6 +581,26 @@ module RSpec
|
|
559
581
|
alias_matcher :a_hash_including, :include
|
560
582
|
alias_matcher :including, :include
|
561
583
|
|
584
|
+
# Passes if actual all expected objects pass. This works for
|
585
|
+
# any enumerable object.
|
586
|
+
#
|
587
|
+
# @example
|
588
|
+
#
|
589
|
+
# expect([1, 3, 5]).to all be_odd
|
590
|
+
# expect([1, 3, 6]).to all be_odd # fails
|
591
|
+
#
|
592
|
+
# @note The negative form `not_to all` is not supported. Instead
|
593
|
+
# use `not_to include` or pass a negative form of a matcher
|
594
|
+
# as the argument (e.g. `all exclude(:foo)`).
|
595
|
+
#
|
596
|
+
# @note You can also use this with compound matchers as well.
|
597
|
+
#
|
598
|
+
# @example
|
599
|
+
# expect([1, 3, 5]).to all( be_odd.and be_an(Integer) )
|
600
|
+
def all(expected)
|
601
|
+
BuiltIn::All.new(expected)
|
602
|
+
end
|
603
|
+
|
562
604
|
# Given a `Regexp` or `String`, passes if `actual.match(pattern)`
|
563
605
|
# Given an arbitrary nested data structure (e.g. arrays and hashes),
|
564
606
|
# matches if `expected === actual` || `actual == expected` for each
|
@@ -675,7 +717,7 @@ module RSpec
|
|
675
717
|
#
|
676
718
|
# @example
|
677
719
|
#
|
678
|
-
#
|
720
|
+
# expect("string").to respond_to(:length)
|
679
721
|
#
|
680
722
|
def respond_to(*names)
|
681
723
|
BuiltIn::RespondTo.new(*names)
|
@@ -837,6 +879,15 @@ module RSpec
|
|
837
879
|
alias_matcher :a_block_yielding_successive_args, :yield_successive_args
|
838
880
|
alias_matcher :yielding_successive_args, :yield_successive_args
|
839
881
|
|
882
|
+
# Delegates to {RSpec::Expectations.configuration}.
|
883
|
+
# This is here because rspec-core's `expect_with` option
|
884
|
+
# looks for a `configuration` method on the mixin
|
885
|
+
# (`RSpec::Matchers`) to yield to a block.
|
886
|
+
# @return [RSpec::Expectations::Configuration] the configuration object
|
887
|
+
def self.configuration
|
888
|
+
Expectations.configuration
|
889
|
+
end
|
890
|
+
|
840
891
|
private
|
841
892
|
|
842
893
|
BE_PREDICATE_REGEX = /^(be_(?:an?_)?)(.*)/
|