/usr/lib/ruby/vendor_ruby/rspec/matchers/built_in/yield.rb is in ruby-rspec-expectations 3.4.0c3e0m1s1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | RSpec::Support.require_rspec_support 'method_signature_verifier'
module RSpec
module Matchers
module BuiltIn
# @private
# Object that is yielded to `expect` when one of the
# yield matchers is used. Provides information about
# the yield behavior of the object-under-test.
class YieldProbe
def self.probe(block)
probe = new(block)
return probe unless probe.has_block?
probe.assert_valid_expect_block!
block.call(probe)
probe.assert_used!
probe
end
attr_accessor :num_yields, :yielded_args
def initialize(block)
@block = block
@used = false
self.num_yields = 0
self.yielded_args = []
end
def has_block?
Proc === @block
end
def to_proc
@used = true
probe = self
Proc.new do |*args|
probe.num_yields += 1
probe.yielded_args << args
nil # to indicate the block does not return a meaningful value
end
end
def single_yield_args
yielded_args.first
end
def yielded_once?(matcher_name)
case num_yields
when 1 then true
when 0 then false
else
raise "The #{matcher_name} matcher is not designed to be used with a " \
'method that yields multiple times. Use the yield_successive_args ' \
'matcher for that case.'
end
end
def successive_yield_args
yielded_args.map do |arg_array|
arg_array.size == 1 ? arg_array.first : arg_array
end
end
def assert_used!
return if @used
raise 'You must pass the argument yielded to your expect block on ' \
'to the method-under-test as a block. It acts as a probe that ' \
'allows the matcher to detect whether or not the method-under-test ' \
'yields, and, if so, how many times, and what the yielded arguments ' \
'are.'
end
if RUBY_VERSION.to_f > 1.8
def assert_valid_expect_block!
block_signature = RSpec::Support::BlockSignature.new(@block)
return if RSpec::Support::StrictSignatureVerifier.new(block_signature, [self]).valid?
raise 'Your expect block must accept an argument to be used with this ' \
'matcher. Pass the argument as a block on to the method you are testing.'
end
else
# :nocov:
# On 1.8.7, `lambda { }.arity` and `lambda { |*a| }.arity` both return -1,
# so we can't distinguish between accepting no args and an arg splat.
# It's OK to skip, this, though; it just provides a nice error message
# when the user forgets to accept an arg in their block. They'll still get
# the `assert_used!` error message from above, which is sufficient.
def assert_valid_expect_block!
# nothing to do
end
# :nocov:
end
end
# @api private
# Provides the implementation for `yield_control`.
# Not intended to be instantiated directly.
class YieldControl < BaseMatcher
def initialize
at_least(:once)
end
# @api public
# Specifies that the method is expected to yield once.
def once
exactly(1)
self
end
# @api public
# Specifies that the method is expected to yield twice.
def twice
exactly(2)
self
end
# @api public
# Specifies that the method is expected to yield thrice.
def thrice
exactly(3)
self
end
# @api public
# Specifies that the method is expected to yield the given number of times.
def exactly(number)
set_expected_yields_count(:==, number)
self
end
# @api public
# Specifies the maximum number of times the method is expected to yield
def at_most(number)
set_expected_yields_count(:<=, number)
self
end
# @api public
# Specifies the minimum number of times the method is expected to yield
def at_least(number)
set_expected_yields_count(:>=, number)
self
end
# @api public
# No-op. Provides syntactic sugar.
def times
self
end
# @private
def matches?(block)
@probe = YieldProbe.probe(block)
return false unless @probe.has_block?
@probe.num_yields.__send__(@expectation_type, @expected_yields_count)
end
# @private
def does_not_match?(block)
!matches?(block) && @probe.has_block?
end
# @api private
# @return [String]
def failure_message
'expected given block to yield control' + failure_reason
end
# @api private
# @return [String]
def failure_message_when_negated
'expected given block not to yield control' + failure_reason
end
# @private
def supports_block_expectations?
true
end
private
def set_expected_yields_count(relativity, n)
@expectation_type = relativity
@expected_yields_count = case n
when Numeric then n
when :once then 1
when :twice then 2
when :thrice then 3
end
end
def failure_reason
return ' but was not a block' unless @probe.has_block?
return '' unless @expected_yields_count
" #{human_readable_expectation_type}#{human_readable_count(@expected_yields_count)}" \
" but yielded #{human_readable_count(@probe.num_yields)}"
end
def human_readable_expectation_type
case @expectation_type
when :<= then 'at most '
when :>= then 'at least '
else ''
end
end
def human_readable_count(count)
case count
when 1 then 'once'
when 2 then 'twice'
else "#{count} times"
end
end
end
# @api private
# Provides the implementation for `yield_with_no_args`.
# Not intended to be instantiated directly.
class YieldWithNoArgs < BaseMatcher
# @private
def matches?(block)
@probe = YieldProbe.probe(block)
return false unless @probe.has_block?
@probe.yielded_once?(:yield_with_no_args) && @probe.single_yield_args.empty?
end
# @private
def does_not_match?(block)
!matches?(block) && @probe.has_block?
end
# @private
def failure_message
"expected given block to yield with no arguments, but #{positive_failure_reason}"
end
# @private
def failure_message_when_negated
"expected given block not to yield with no arguments, but #{negative_failure_reason}"
end
# @private
def supports_block_expectations?
true
end
private
def positive_failure_reason
return 'was not a block' unless @probe.has_block?
return 'did not yield' if @probe.num_yields.zero?
"yielded with arguments: #{description_of @probe.single_yield_args}"
end
def negative_failure_reason
return 'was not a block' unless @probe.has_block?
'did'
end
end
# @api private
# Provides the implementation for `yield_with_args`.
# Not intended to be instantiated directly.
class YieldWithArgs < BaseMatcher
def initialize(*args)
@expected = args
end
# @private
def matches?(block)
@probe = YieldProbe.probe(block)
return false unless @probe.has_block?
@actual = @probe.single_yield_args
@probe.yielded_once?(:yield_with_args) && args_match?
end
# @private
def does_not_match?(block)
!matches?(block) && @probe.has_block?
end
# @private
def failure_message
"expected given block to yield with arguments, but #{positive_failure_reason}"
end
# @private
def failure_message_when_negated
"expected given block not to yield with arguments, but #{negative_failure_reason}"
end
# @private
def description
desc = 'yield with args'
desc << "(#{expected_arg_description})" unless @expected.empty?
desc
end
# @private
def supports_block_expectations?
true
end
private
def positive_failure_reason
return 'was not a block' unless @probe.has_block?
return 'did not yield' if @probe.num_yields.zero?
@positive_args_failure
end
def expected_arg_description
@expected.map { |e| description_of e }.join(', ')
end
def negative_failure_reason
if !@probe.has_block?
'was not a block'
elsif all_args_match?
'yielded with expected arguments' \
"\nexpected not: #{surface_descriptions_in(@expected).inspect}" \
"\n got: #{actual_formatted}"
else
'did'
end
end
def args_match?
if @expected.empty? # expect {...}.to yield_with_args
@positive_args_failure = 'yielded with no arguments' if @actual.empty?
return !@actual.empty?
end
unless (match = all_args_match?)
@positive_args_failure = 'yielded with unexpected arguments' \
"\nexpected: #{surface_descriptions_in(@expected).inspect}" \
"\n got: #{actual_formatted}"
end
match
end
def all_args_match?
values_match?(@expected, @actual)
end
end
# @api private
# Provides the implementation for `yield_successive_args`.
# Not intended to be instantiated directly.
class YieldSuccessiveArgs < BaseMatcher
def initialize(*args)
@expected = args
end
# @private
def matches?(block)
@probe = YieldProbe.probe(block)
return false unless @probe.has_block?
@actual = @probe.successive_yield_args
args_match?
end
def does_not_match?(block)
!matches?(block) && @probe.has_block?
end
# @private
def failure_message
'expected given block to yield successively with arguments, ' \
"but #{positive_failure_reason}"
end
# @private
def failure_message_when_negated
'expected given block not to yield successively with arguments, ' \
"but #{negative_failure_reason}"
end
# @private
def description
"yield successive args(#{expected_arg_description})"
end
# @private
def supports_block_expectations?
true
end
private
def args_match?
values_match?(@expected, @actual)
end
def expected_arg_description
@expected.map { |e| description_of e }.join(', ')
end
def positive_failure_reason
return 'was not a block' unless @probe.has_block?
'yielded with unexpected arguments' \
"\nexpected: #{surface_descriptions_in(@expected).inspect}" \
"\n got: #{actual_formatted}"
end
def negative_failure_reason
return 'was not a block' unless @probe.has_block?
'yielded with expected arguments' \
"\nexpected not: #{surface_descriptions_in(@expected).inspect}" \
"\n got: #{actual_formatted}"
end
end
end
end
end
|