Class: RSpec::Matchers::BuiltIn::YieldProbe

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, &callback) ⇒ YieldProbe

Returns a new instance of YieldProbe.



21
22
23
24
25
26
27
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 21

def initialize(block, &callback)
  @block = block
  @callback = callback || Proc.new {}
  @used = false
  self.num_yields = 0
  self.yielded_args = []
end

Instance Attribute Details

#num_yieldsObject

Returns the value of attribute num_yields.



19
20
21
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 19

def num_yields
  @num_yields
end

#yielded_argsObject

Returns the value of attribute yielded_args.



19
20
21
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 19

def yielded_args
  @yielded_args
end

Class Method Details

.probe(block, &callback) ⇒ Object



13
14
15
16
17
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 13

def self.probe(block, &callback)
  probe = new(block, &callback)
  return probe unless probe.has_block?
  probe.probe
end

Instance Method Details

#assert_used!Object



68
69
70
71
72
73
74
75
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 68

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

#assert_valid_expect_block!Object

: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.



91
92
93
94
95
96
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 91

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

#has_block?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 29

def has_block?
  Proc === @block
end

#probeObject



33
34
35
36
37
38
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 33

def probe
  assert_valid_expect_block!
  @block.call(self)
  assert_used!
  self
end

#single_yield_argsObject



53
54
55
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 53

def single_yield_args
  yielded_args.first
end

#to_procObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 40

def to_proc
  @used = true

  probe = self
  callback = @callback
  Proc.new do |*args|
    probe.num_yields += 1
    probe.yielded_args << args
    callback.call(*args)
    nil # to indicate the block does not return a meaningful value
  end
end

#yielded_once?(matcher_name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-expectations-3.12.2/lib/rspec/matchers/built_in/yield.rb', line 57

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