Class: Crspec::Matchers::YieldWithArgsMatcher

Inherits:
BaseMatcher
  • Object
show all
Defined in:
lib/crspec/matchers.rb

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected

Instance Method Summary collapse

Methods inherited from BaseMatcher

#and, #failure_message_when_negated, #or

Constructor Details

#initialize(*expected_args) ⇒ YieldWithArgsMatcher

Returns a new instance of YieldWithArgsMatcher.



662
663
664
665
# File 'lib/crspec/matchers.rb', line 662

def initialize(*expected_args)
  super(expected_args)
  @expected_args = expected_args
end

Instance Method Details

#failure_messageObject



678
679
680
681
682
683
684
# File 'lib/crspec/matchers.rb', line 678

def failure_message
  if @yielded_args.nil?
    "Expected block to yield with args #{@expected_args.inspect}, but it did not yield"
  else
    "Expected block to yield with args #{@expected_args.inspect}, got #{@yielded_args.inspect}"
  end
end

#matches?(block) ⇒ Boolean

Returns:

  • (Boolean)


667
668
669
670
671
672
673
674
675
676
# File 'lib/crspec/matchers.rb', line 667

def matches?(block)
  @yielded_args = nil
  probe = ->(*args) { @yielded_args = args }
  block.call(probe)
  return false if @yielded_args.nil?
  return true if @expected_args.empty?
  return false unless @yielded_args.size == @expected_args.size

  @expected_args.zip(@yielded_args).all? { |e, a| e == a || e === a }
end