/
satisfy.rb
60 lines (53 loc) · 1.48 KB
/
satisfy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module RSpec
module Matchers
module BuiltIn
# @api private
# Provides the implementation for `satisfy`.
# Not intended to be instantiated directly.
class Satisfy < BaseMatcher
def initialize(description=nil, &block)
@description = description
@block = block
end
# @private
def matches?(actual, &block)
@block = block if block
@actual = actual
@block.call(actual)
end
# @private
def description
@description ||= "satisfy #{block_representation}"
end
# @api private
# @return [String]
def failure_message
"expected #{actual_formatted} to #{description}"
end
# @api private
# @return [String]
def failure_message_when_negated
"expected #{actual_formatted} not to #{description}"
end
private
if RSpec::Support::RubyFeatures.ripper_supported?
def block_representation
if (block_snippet = extract_block_snippet)
"expression `#{block_snippet}`"
else
'block'
end
end
def extract_block_snippet
return nil unless @block
Expectations::BlockSnippetExtractor.try_extracting_single_line_body_of(@block, matcher_name)
end
else
def block_representation
'block'
end
end
end
end
end
end