rspec-expectations 3.8.6
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 +5 -0
- data/.document +5 -0
- data/.yardopts +6 -0
- data/Changelog.md +1156 -0
- data/LICENSE.md +25 -0
- data/README.md +305 -0
- data/lib/rspec/expectations.rb +82 -0
- data/lib/rspec/expectations/block_snippet_extractor.rb +253 -0
- data/lib/rspec/expectations/configuration.rb +215 -0
- data/lib/rspec/expectations/expectation_target.rb +127 -0
- data/lib/rspec/expectations/fail_with.rb +39 -0
- data/lib/rspec/expectations/failure_aggregator.rb +194 -0
- data/lib/rspec/expectations/handler.rb +170 -0
- data/lib/rspec/expectations/minitest_integration.rb +58 -0
- data/lib/rspec/expectations/syntax.rb +132 -0
- data/lib/rspec/expectations/version.rb +8 -0
- data/lib/rspec/matchers.rb +1034 -0
- data/lib/rspec/matchers/aliased_matcher.rb +116 -0
- data/lib/rspec/matchers/built_in.rb +52 -0
- data/lib/rspec/matchers/built_in/all.rb +86 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +193 -0
- data/lib/rspec/matchers/built_in/be.rb +288 -0
- data/lib/rspec/matchers/built_in/be_between.rb +77 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +26 -0
- data/lib/rspec/matchers/built_in/be_kind_of.rb +20 -0
- data/lib/rspec/matchers/built_in/be_within.rb +72 -0
- data/lib/rspec/matchers/built_in/change.rb +428 -0
- data/lib/rspec/matchers/built_in/compound.rb +271 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +302 -0
- data/lib/rspec/matchers/built_in/cover.rb +24 -0
- data/lib/rspec/matchers/built_in/eq.rb +40 -0
- data/lib/rspec/matchers/built_in/eql.rb +34 -0
- data/lib/rspec/matchers/built_in/equal.rb +81 -0
- data/lib/rspec/matchers/built_in/exist.rb +90 -0
- data/lib/rspec/matchers/built_in/has.rb +103 -0
- data/lib/rspec/matchers/built_in/have_attributes.rb +114 -0
- data/lib/rspec/matchers/built_in/include.rb +149 -0
- data/lib/rspec/matchers/built_in/match.rb +106 -0
- data/lib/rspec/matchers/built_in/operators.rb +128 -0
- data/lib/rspec/matchers/built_in/output.rb +200 -0
- data/lib/rspec/matchers/built_in/raise_error.rb +230 -0
- data/lib/rspec/matchers/built_in/respond_to.rb +165 -0
- data/lib/rspec/matchers/built_in/satisfy.rb +60 -0
- data/lib/rspec/matchers/built_in/start_or_end_with.rb +94 -0
- data/lib/rspec/matchers/built_in/throw_symbol.rb +132 -0
- data/lib/rspec/matchers/built_in/yield.rb +432 -0
- data/lib/rspec/matchers/composable.rb +171 -0
- data/lib/rspec/matchers/dsl.rb +527 -0
- data/lib/rspec/matchers/english_phrasing.rb +58 -0
- data/lib/rspec/matchers/expecteds_for_multiple_diffs.rb +73 -0
- data/lib/rspec/matchers/fail_matchers.rb +42 -0
- data/lib/rspec/matchers/generated_descriptions.rb +41 -0
- data/lib/rspec/matchers/matcher_delegator.rb +35 -0
- data/lib/rspec/matchers/matcher_protocol.rb +99 -0
- metadata +215 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,106 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
# @api private
|
5
|
+
# Provides the implementation for `match`.
|
6
|
+
# Not intended to be instantiated directly.
|
7
|
+
class Match < BaseMatcher
|
8
|
+
def initialize(expected)
|
9
|
+
super(expected)
|
10
|
+
|
11
|
+
@expected_captures = nil
|
12
|
+
end
|
13
|
+
# @api private
|
14
|
+
# @return [String]
|
15
|
+
def description
|
16
|
+
if @expected_captures && @expected.match(actual)
|
17
|
+
"match #{surface_descriptions_in(expected).inspect} with captures #{surface_descriptions_in(@expected_captures).inspect}"
|
18
|
+
else
|
19
|
+
"match #{surface_descriptions_in(expected).inspect}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# @api private
|
24
|
+
# @return [Boolean]
|
25
|
+
def diffable?
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
# Used to specify the captures we match against
|
30
|
+
# @return [self]
|
31
|
+
def with_captures(*captures)
|
32
|
+
@expected_captures = captures
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def match(expected, actual)
|
39
|
+
return match_captures(expected, actual) if @expected_captures
|
40
|
+
return true if values_match?(expected, actual)
|
41
|
+
return false unless can_safely_call_match?(expected, actual)
|
42
|
+
actual.match(expected)
|
43
|
+
end
|
44
|
+
|
45
|
+
def can_safely_call_match?(expected, actual)
|
46
|
+
return false unless actual.respond_to?(:match)
|
47
|
+
|
48
|
+
!(RSpec::Matchers.is_a_matcher?(expected) &&
|
49
|
+
(String === actual || Regexp === actual))
|
50
|
+
end
|
51
|
+
|
52
|
+
def match_captures(expected, actual)
|
53
|
+
match = actual.match(expected)
|
54
|
+
if match
|
55
|
+
match = ReliableMatchData.new(match)
|
56
|
+
if match.names.empty?
|
57
|
+
values_match?(@expected_captures, match.captures)
|
58
|
+
else
|
59
|
+
expected_matcher = @expected_captures.last
|
60
|
+
values_match?(expected_matcher, Hash[match.names.zip(match.captures)]) ||
|
61
|
+
values_match?(expected_matcher, Hash[match.names.map(&:to_sym).zip(match.captures)]) ||
|
62
|
+
values_match?(@expected_captures, match.captures)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
false
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# @api private
|
71
|
+
# Used to wrap match data and make it reliable for 1.8.7
|
72
|
+
class ReliableMatchData
|
73
|
+
def initialize(match_data)
|
74
|
+
@match_data = match_data
|
75
|
+
end
|
76
|
+
|
77
|
+
if RUBY_VERSION == "1.8.7"
|
78
|
+
# @api private
|
79
|
+
# Returns match data names for named captures
|
80
|
+
# @return Array
|
81
|
+
def names
|
82
|
+
[]
|
83
|
+
end
|
84
|
+
else
|
85
|
+
# @api private
|
86
|
+
# Returns match data names for named captures
|
87
|
+
# @return Array
|
88
|
+
def names
|
89
|
+
match_data.names
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# @api private
|
94
|
+
# returns an array of captures from the match data
|
95
|
+
# @return Array
|
96
|
+
def captures
|
97
|
+
match_data.captures
|
98
|
+
end
|
99
|
+
|
100
|
+
protected
|
101
|
+
|
102
|
+
attr_reader :match_data
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'rspec/support'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Matchers
|
5
|
+
module BuiltIn
|
6
|
+
# @api private
|
7
|
+
# Provides the implementation for operator matchers.
|
8
|
+
# Not intended to be instantiated directly.
|
9
|
+
# Only available for use with `should`.
|
10
|
+
class OperatorMatcher
|
11
|
+
class << self
|
12
|
+
# @private
|
13
|
+
def registry
|
14
|
+
@registry ||= {}
|
15
|
+
end
|
16
|
+
|
17
|
+
# @private
|
18
|
+
def register(klass, operator, matcher)
|
19
|
+
registry[klass] ||= {}
|
20
|
+
registry[klass][operator] = matcher
|
21
|
+
end
|
22
|
+
|
23
|
+
# @private
|
24
|
+
def unregister(klass, operator)
|
25
|
+
registry[klass] && registry[klass].delete(operator)
|
26
|
+
end
|
27
|
+
|
28
|
+
# @private
|
29
|
+
def get(klass, operator)
|
30
|
+
klass.ancestors.each do |ancestor|
|
31
|
+
matcher = registry[ancestor] && registry[ancestor][operator]
|
32
|
+
return matcher if matcher
|
33
|
+
end
|
34
|
+
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
register Enumerable, '=~', BuiltIn::ContainExactly
|
40
|
+
|
41
|
+
def initialize(actual)
|
42
|
+
@actual = actual
|
43
|
+
end
|
44
|
+
|
45
|
+
# @private
|
46
|
+
def self.use_custom_matcher_or_delegate(operator)
|
47
|
+
define_method(operator) do |expected|
|
48
|
+
if !has_non_generic_implementation_of?(operator) && (matcher = OperatorMatcher.get(@actual.class, operator))
|
49
|
+
@actual.__send__(::RSpec::Matchers.last_expectation_handler.should_method, matcher.new(expected))
|
50
|
+
else
|
51
|
+
eval_match(@actual, operator, expected)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
negative_operator = operator.sub(/^=/, '!')
|
56
|
+
if negative_operator != operator && respond_to?(negative_operator)
|
57
|
+
define_method(negative_operator) do |_expected|
|
58
|
+
opposite_should = ::RSpec::Matchers.last_expectation_handler.opposite_should_method
|
59
|
+
raise "RSpec does not support `#{::RSpec::Matchers.last_expectation_handler.should_method} #{negative_operator} expected`. " \
|
60
|
+
"Use `#{opposite_should} #{operator} expected` instead."
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
['==', '===', '=~', '>', '>=', '<', '<='].each do |operator|
|
66
|
+
use_custom_matcher_or_delegate operator
|
67
|
+
end
|
68
|
+
|
69
|
+
# @private
|
70
|
+
def fail_with_message(message)
|
71
|
+
RSpec::Expectations.fail_with(message, @expected, @actual)
|
72
|
+
end
|
73
|
+
|
74
|
+
# @api private
|
75
|
+
# @return [String]
|
76
|
+
def description
|
77
|
+
"#{@operator} #{RSpec::Support::ObjectFormatter.format(@expected)}"
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def has_non_generic_implementation_of?(op)
|
83
|
+
Support.method_handle_for(@actual, op).owner != ::Kernel
|
84
|
+
rescue NameError
|
85
|
+
false
|
86
|
+
end
|
87
|
+
|
88
|
+
def eval_match(actual, operator, expected)
|
89
|
+
::RSpec::Matchers.last_matcher = self
|
90
|
+
@operator, @expected = operator, expected
|
91
|
+
__delegate_operator(actual, operator, expected)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# @private
|
96
|
+
# Handles operator matcher for `should`.
|
97
|
+
class PositiveOperatorMatcher < OperatorMatcher
|
98
|
+
def __delegate_operator(actual, operator, expected)
|
99
|
+
if actual.__send__(operator, expected)
|
100
|
+
true
|
101
|
+
else
|
102
|
+
expected_formatted = RSpec::Support::ObjectFormatter.format(expected)
|
103
|
+
actual_formatted = RSpec::Support::ObjectFormatter.format(actual)
|
104
|
+
|
105
|
+
if ['==', '===', '=~'].include?(operator)
|
106
|
+
fail_with_message("expected: #{expected_formatted}\n got: #{actual_formatted} (using #{operator})")
|
107
|
+
else
|
108
|
+
fail_with_message("expected: #{operator} #{expected_formatted}\n got: #{operator.gsub(/./, ' ')} #{actual_formatted}")
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# @private
|
115
|
+
# Handles operator matcher for `should_not`.
|
116
|
+
class NegativeOperatorMatcher < OperatorMatcher
|
117
|
+
def __delegate_operator(actual, operator, expected)
|
118
|
+
return false unless actual.__send__(operator, expected)
|
119
|
+
|
120
|
+
expected_formatted = RSpec::Support::ObjectFormatter.format(expected)
|
121
|
+
actual_formatted = RSpec::Support::ObjectFormatter.format(actual)
|
122
|
+
|
123
|
+
fail_with_message("expected not: #{operator} #{expected_formatted}\n got: #{operator.gsub(/./, ' ')} #{actual_formatted}")
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,200 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Matchers
|
5
|
+
module BuiltIn
|
6
|
+
# @api private
|
7
|
+
# Provides the implementation for `output`.
|
8
|
+
# Not intended to be instantiated directly.
|
9
|
+
class Output < BaseMatcher
|
10
|
+
def initialize(expected)
|
11
|
+
@expected = expected
|
12
|
+
@actual = ""
|
13
|
+
@block = nil
|
14
|
+
@stream_capturer = NullCapture
|
15
|
+
end
|
16
|
+
|
17
|
+
def matches?(block)
|
18
|
+
@block = block
|
19
|
+
return false unless Proc === block
|
20
|
+
@actual = @stream_capturer.capture(block)
|
21
|
+
@expected ? values_match?(@expected, @actual) : captured?
|
22
|
+
end
|
23
|
+
|
24
|
+
def does_not_match?(block)
|
25
|
+
!matches?(block) && Proc === block
|
26
|
+
end
|
27
|
+
|
28
|
+
# @api public
|
29
|
+
# Tells the matcher to match against stdout.
|
30
|
+
# Works only when the main Ruby process prints to stdout
|
31
|
+
def to_stdout
|
32
|
+
@stream_capturer = CaptureStdout
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
# @api public
|
37
|
+
# Tells the matcher to match against stderr.
|
38
|
+
# Works only when the main Ruby process prints to stderr
|
39
|
+
def to_stderr
|
40
|
+
@stream_capturer = CaptureStderr
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
# @api public
|
45
|
+
# Tells the matcher to match against stdout.
|
46
|
+
# Works when subprocesses print to stdout as well.
|
47
|
+
# This is significantly (~30x) slower than `to_stdout`
|
48
|
+
def to_stdout_from_any_process
|
49
|
+
@stream_capturer = CaptureStreamToTempfile.new("stdout", $stdout)
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
# @api public
|
54
|
+
# Tells the matcher to match against stderr.
|
55
|
+
# Works when subprocesses print to stderr as well.
|
56
|
+
# This is significantly (~30x) slower than `to_stderr`
|
57
|
+
def to_stderr_from_any_process
|
58
|
+
@stream_capturer = CaptureStreamToTempfile.new("stderr", $stderr)
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
# @api private
|
63
|
+
# @return [String]
|
64
|
+
def failure_message
|
65
|
+
"expected block to #{description}, but #{positive_failure_reason}"
|
66
|
+
end
|
67
|
+
|
68
|
+
# @api private
|
69
|
+
# @return [String]
|
70
|
+
def failure_message_when_negated
|
71
|
+
"expected block to not #{description}, but #{negative_failure_reason}"
|
72
|
+
end
|
73
|
+
|
74
|
+
# @api private
|
75
|
+
# @return [String]
|
76
|
+
def description
|
77
|
+
if @expected
|
78
|
+
"output #{description_of @expected} to #{@stream_capturer.name}"
|
79
|
+
else
|
80
|
+
"output to #{@stream_capturer.name}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# @api private
|
85
|
+
# @return [Boolean]
|
86
|
+
def diffable?
|
87
|
+
true
|
88
|
+
end
|
89
|
+
|
90
|
+
# @api private
|
91
|
+
# Indicates this matcher matches against a block.
|
92
|
+
# @return [True]
|
93
|
+
def supports_block_expectations?
|
94
|
+
true
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
|
99
|
+
def captured?
|
100
|
+
@actual.length > 0
|
101
|
+
end
|
102
|
+
|
103
|
+
def positive_failure_reason
|
104
|
+
return "was not a block" unless Proc === @block
|
105
|
+
return "output #{actual_output_description}" if @expected
|
106
|
+
"did not"
|
107
|
+
end
|
108
|
+
|
109
|
+
def negative_failure_reason
|
110
|
+
return "was not a block" unless Proc === @block
|
111
|
+
"output #{actual_output_description}"
|
112
|
+
end
|
113
|
+
|
114
|
+
def actual_output_description
|
115
|
+
return "nothing" unless captured?
|
116
|
+
actual_formatted
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# @private
|
121
|
+
module NullCapture
|
122
|
+
def self.name
|
123
|
+
"some stream"
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.capture(_block)
|
127
|
+
raise "You must chain `to_stdout` or `to_stderr` off of the `output(...)` matcher."
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# @private
|
132
|
+
module CaptureStdout
|
133
|
+
def self.name
|
134
|
+
'stdout'
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.capture(block)
|
138
|
+
captured_stream = StringIO.new
|
139
|
+
|
140
|
+
original_stream = $stdout
|
141
|
+
$stdout = captured_stream
|
142
|
+
|
143
|
+
block.call
|
144
|
+
|
145
|
+
captured_stream.string
|
146
|
+
ensure
|
147
|
+
$stdout = original_stream
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# @private
|
152
|
+
module CaptureStderr
|
153
|
+
def self.name
|
154
|
+
'stderr'
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.capture(block)
|
158
|
+
captured_stream = StringIO.new
|
159
|
+
|
160
|
+
original_stream = $stderr
|
161
|
+
$stderr = captured_stream
|
162
|
+
|
163
|
+
block.call
|
164
|
+
|
165
|
+
captured_stream.string
|
166
|
+
ensure
|
167
|
+
$stderr = original_stream
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# @private
|
172
|
+
class CaptureStreamToTempfile < Struct.new(:name, :stream)
|
173
|
+
def capture(block)
|
174
|
+
# We delay loading tempfile until it is actually needed because
|
175
|
+
# we want to minimize stdlibs loaded so that users who use a
|
176
|
+
# portion of the stdlib can't have passing specs while forgetting
|
177
|
+
# to load it themselves. `CaptureStreamToTempfile` is rarely used
|
178
|
+
# and `tempfile` pulls in a bunch of things (delegate, tmpdir,
|
179
|
+
# thread, fileutils, etc), so it's worth delaying it until this point.
|
180
|
+
require 'tempfile'
|
181
|
+
|
182
|
+
original_stream = stream.clone
|
183
|
+
captured_stream = Tempfile.new(name)
|
184
|
+
|
185
|
+
begin
|
186
|
+
captured_stream.sync = true
|
187
|
+
stream.reopen(captured_stream)
|
188
|
+
block.call
|
189
|
+
captured_stream.rewind
|
190
|
+
captured_stream.read
|
191
|
+
ensure
|
192
|
+
stream.reopen(original_stream)
|
193
|
+
captured_stream.close
|
194
|
+
captured_stream.unlink
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
@@ -0,0 +1,230 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
# @api private
|
5
|
+
# Provides the implementation for `raise_error`.
|
6
|
+
# Not intended to be instantiated directly.
|
7
|
+
# rubocop:disable ClassLength
|
8
|
+
# rubocop:disable RescueException
|
9
|
+
class RaiseError
|
10
|
+
include Composable
|
11
|
+
|
12
|
+
def initialize(expected_error_or_message=nil, expected_message=nil, &block)
|
13
|
+
@block = block
|
14
|
+
@actual_error = nil
|
15
|
+
@warn_about_bare_error = expected_error_or_message.nil?
|
16
|
+
|
17
|
+
case expected_error_or_message
|
18
|
+
when nil
|
19
|
+
@expected_error = Exception
|
20
|
+
@expected_message = expected_message
|
21
|
+
when String
|
22
|
+
@expected_error = Exception
|
23
|
+
@expected_message = expected_error_or_message
|
24
|
+
else
|
25
|
+
@expected_error = expected_error_or_message
|
26
|
+
@expected_message = expected_message
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# @api public
|
31
|
+
# Specifies the expected error message.
|
32
|
+
def with_message(expected_message)
|
33
|
+
raise_message_already_set if @expected_message
|
34
|
+
@warn_about_bare_error = false
|
35
|
+
@expected_message = expected_message
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
# rubocop:disable MethodLength
|
40
|
+
# @private
|
41
|
+
def matches?(given_proc, negative_expectation=false, &block)
|
42
|
+
@given_proc = given_proc
|
43
|
+
@block ||= block
|
44
|
+
@raised_expected_error = false
|
45
|
+
@with_expected_message = false
|
46
|
+
@eval_block = false
|
47
|
+
@eval_block_passed = false
|
48
|
+
|
49
|
+
return false unless Proc === given_proc
|
50
|
+
|
51
|
+
begin
|
52
|
+
given_proc.call
|
53
|
+
rescue Exception => @actual_error
|
54
|
+
if values_match?(@expected_error, @actual_error) ||
|
55
|
+
values_match?(@expected_error, @actual_error.message)
|
56
|
+
@raised_expected_error = true
|
57
|
+
@with_expected_message = verify_message
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
warn_about_bare_error if warning_about_bare_error && !negative_expectation
|
62
|
+
eval_block if !negative_expectation && ready_to_eval_block?
|
63
|
+
|
64
|
+
expectation_matched?
|
65
|
+
end
|
66
|
+
# rubocop:enable MethodLength
|
67
|
+
|
68
|
+
# @private
|
69
|
+
def does_not_match?(given_proc)
|
70
|
+
warn_for_false_positives
|
71
|
+
!matches?(given_proc, :negative_expectation) && Proc === given_proc
|
72
|
+
end
|
73
|
+
|
74
|
+
# @private
|
75
|
+
def supports_block_expectations?
|
76
|
+
true
|
77
|
+
end
|
78
|
+
|
79
|
+
def expects_call_stack_jump?
|
80
|
+
true
|
81
|
+
end
|
82
|
+
|
83
|
+
# @api private
|
84
|
+
# @return [String]
|
85
|
+
def failure_message
|
86
|
+
@eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
|
87
|
+
end
|
88
|
+
|
89
|
+
# @api private
|
90
|
+
# @return [String]
|
91
|
+
def failure_message_when_negated
|
92
|
+
"expected no #{expected_error}#{given_error}"
|
93
|
+
end
|
94
|
+
|
95
|
+
# @api private
|
96
|
+
# @return [String]
|
97
|
+
def description
|
98
|
+
"raise #{expected_error}"
|
99
|
+
end
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
def expectation_matched?
|
104
|
+
error_and_message_match? && block_matches?
|
105
|
+
end
|
106
|
+
|
107
|
+
def error_and_message_match?
|
108
|
+
@raised_expected_error && @with_expected_message
|
109
|
+
end
|
110
|
+
|
111
|
+
def block_matches?
|
112
|
+
@eval_block ? @eval_block_passed : true
|
113
|
+
end
|
114
|
+
|
115
|
+
def ready_to_eval_block?
|
116
|
+
@raised_expected_error && @with_expected_message && @block
|
117
|
+
end
|
118
|
+
|
119
|
+
def eval_block
|
120
|
+
@eval_block = true
|
121
|
+
begin
|
122
|
+
@block[@actual_error]
|
123
|
+
@eval_block_passed = true
|
124
|
+
rescue Exception => err
|
125
|
+
@actual_error = err
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def verify_message
|
130
|
+
return true if @expected_message.nil?
|
131
|
+
values_match?(@expected_message, @actual_error.message.to_s)
|
132
|
+
end
|
133
|
+
|
134
|
+
def warn_for_false_positives
|
135
|
+
expression = if expecting_specific_exception? && @expected_message
|
136
|
+
"`expect { }.not_to raise_error(SpecificErrorClass, message)`"
|
137
|
+
elsif expecting_specific_exception?
|
138
|
+
"`expect { }.not_to raise_error(SpecificErrorClass)`"
|
139
|
+
elsif @expected_message
|
140
|
+
"`expect { }.not_to raise_error(message)`"
|
141
|
+
end
|
142
|
+
|
143
|
+
return unless expression
|
144
|
+
|
145
|
+
warn_about_negative_false_positive expression
|
146
|
+
end
|
147
|
+
|
148
|
+
def handle_warning(message)
|
149
|
+
RSpec::Expectations.configuration.false_positives_handler.call(message)
|
150
|
+
end
|
151
|
+
|
152
|
+
def warning_about_bare_error
|
153
|
+
@warn_about_bare_error && @block.nil?
|
154
|
+
end
|
155
|
+
|
156
|
+
def warn_about_bare_error
|
157
|
+
handle_warning("Using the `raise_error` matcher without providing a specific " \
|
158
|
+
"error or message risks false positives, since `raise_error` " \
|
159
|
+
"will match when Ruby raises a `NoMethodError`, `NameError` or " \
|
160
|
+
"`ArgumentError`, potentially allowing the expectation to pass " \
|
161
|
+
"without even executing the method you are intending to call. " \
|
162
|
+
"#{warning}"\
|
163
|
+
"Instead consider providing a specific error class or message. " \
|
164
|
+
"This message can be suppressed by setting: " \
|
165
|
+
"`RSpec::Expectations.configuration.on_potential_false" \
|
166
|
+
"_positives = :nothing`")
|
167
|
+
end
|
168
|
+
|
169
|
+
def warn_about_negative_false_positive(expression)
|
170
|
+
handle_warning("Using #{expression} risks false positives, since literally " \
|
171
|
+
"any other error would cause the expectation to pass, " \
|
172
|
+
"including those raised by Ruby (e.g. NoMethodError, NameError " \
|
173
|
+
"and ArgumentError), meaning the code you are intending to test " \
|
174
|
+
"may not even get reached. Instead consider using " \
|
175
|
+
"`expect { }.not_to raise_error` or `expect { }.to raise_error" \
|
176
|
+
"(DifferentSpecificErrorClass)`. This message can be suppressed by " \
|
177
|
+
"setting: `RSpec::Expectations.configuration.on_potential_false" \
|
178
|
+
"_positives = :nothing`")
|
179
|
+
end
|
180
|
+
|
181
|
+
def expected_error
|
182
|
+
case @expected_message
|
183
|
+
when nil
|
184
|
+
if RSpec::Support.is_a_matcher?(@expected_error)
|
185
|
+
"Exception with #{description_of(@expected_error)}"
|
186
|
+
else
|
187
|
+
description_of(@expected_error)
|
188
|
+
end
|
189
|
+
when Regexp
|
190
|
+
"#{@expected_error} with message matching #{description_of(@expected_message)}"
|
191
|
+
else
|
192
|
+
"#{@expected_error} with #{description_of(@expected_message)}"
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def format_backtrace(backtrace)
|
197
|
+
formatter = Matchers.configuration.backtrace_formatter
|
198
|
+
formatter.format_backtrace(backtrace)
|
199
|
+
end
|
200
|
+
|
201
|
+
def given_error
|
202
|
+
return " but was not given a block" unless Proc === @given_proc
|
203
|
+
return " but nothing was raised" unless @actual_error
|
204
|
+
|
205
|
+
backtrace = format_backtrace(@actual_error.backtrace)
|
206
|
+
[
|
207
|
+
", got #{description_of(@actual_error)} with backtrace:",
|
208
|
+
*backtrace
|
209
|
+
].join("\n # ")
|
210
|
+
end
|
211
|
+
|
212
|
+
def expecting_specific_exception?
|
213
|
+
@expected_error != Exception
|
214
|
+
end
|
215
|
+
|
216
|
+
def raise_message_already_set
|
217
|
+
raise "`expect { }.to raise_error(message).with_message(message)` is not valid. " \
|
218
|
+
'The matcher only allows the expected message to be specified once'
|
219
|
+
end
|
220
|
+
|
221
|
+
def warning
|
222
|
+
warning = "Actual error raised was #{description_of(@actual_error)}. "
|
223
|
+
warning if @actual_error
|
224
|
+
end
|
225
|
+
end
|
226
|
+
# rubocop:enable RescueException
|
227
|
+
# rubocop:enable ClassLength
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|