class RSpec::Matchers::DelegateMatcher::Delegation

rubocop:disable Metrics/ClassLength

Attributes

delegate[RW]
dispatcher[RW]
expected[RW]

Public Class Methods

new(expected) click to toggle source
# File lib/delegate_matcher/delegation.rb, line 13
def initialize(expected)
  self.expected   = expected
  self.dispatcher = DelegateMatcher::Dispatcher.new(expected)
  self.delegate   = expected.to.map { |to| StubDelegate.new(expected, to) }
end

Public Instance Methods

allow_nil_failure_message(negated) click to toggle source
# File lib/delegate_matcher/delegation.rb, line 122
def allow_nil_failure_message(negated)
  case
  when expected.allow_nil.nil? || negated ^ allow_nil_ok?
    ''
  when negated
    %("#{expected.to_description}" was #{expected.allow_nil ? '' : 'not '}allowed to be nil)
  else
    %("#{expected.to_description}" was #{expected.allow_nil ? 'not ' : ''}allowed to be nil)
  end
end
allow_nil_ok?() click to toggle source
# File lib/delegate_matcher/delegation.rb, line 28
def allow_nil_ok?
  return true if expected.allow_nil.nil?

  begin
    expected.to.each { |to| NilDelegate.new(expected, to) { dispatcher.call } }
    allow_nil = true
  rescue NoMethodError
    allow_nil = false
  end

  allow_nil == expected.allow_nil
end
argument_failure_message(negated) click to toggle source
# File lib/delegate_matcher/delegation.rb, line 87
def argument_failure_message(negated)
  case
  when expected.as_args.nil? || negated ^ arguments_ok?
    ''
  else
    "was called with #{delegate[0].argument_description}"
  end
end
arguments_ok?() click to toggle source
# File lib/delegate_matcher/delegation.rb, line 41
def arguments_ok?
  args_matcher = Mocks::ArgumentListMatcher.new(*expected.as_args)
  delegate.all? { |delegate| args_matcher.args_match?(*delegate.args) }
end
block_failure_message(negated) click to toggle source
# File lib/delegate_matcher/delegation.rb, line 96
def block_failure_message(negated)
  proc_source = ProcSource.new(delegate[0].block)

  case
  when expected.block.nil? || negated ^ block_ok?
    ''
  when negated
    "a block was #{expected.block ? '' : 'not '}passed"
  when expected.block
    delegate.all? { |d| d.block.nil? } ? 'a block was not passed' : "a different block '#{proc_source}' was passed"
  else
    %(a block #{proc_source} was passed)
  end
end
block_ok?() click to toggle source
# File lib/delegate_matcher/delegation.rb, line 46
def block_ok?
  expected_block = expected.block
  return true if expected_block.nil?

  delegate.all? do |d|
    case
    when expected_block == false
      d.block.nil?
    when expected_block == true
      d.block == dispatcher.block
    else
      d.block_source == expected.block_source
    end
  end
end
delegate_return_value() click to toggle source
# File lib/delegate_matcher/delegation.rb, line 73
def delegate_return_value
  delegate.length == 1 ? delegate[0].return_value : delegate.map(&:return_value)
end
delegation_ok?() click to toggle source
# File lib/delegate_matcher/delegation.rb, line 19
def delegation_ok?
  dispatcher.call
  delegate.all?(&:received)
end
failure_message(negated) click to toggle source
# File lib/delegate_matcher/delegation.rb, line 77
def failure_message(negated)
  message = [
    argument_failure_message(negated),
    block_failure_message(negated),
    return_value_failure_message(negated),
    allow_nil_failure_message(negated)
  ].reject(&:empty?).join(' and ')
  message.empty? ? nil : message
end
ok?() click to toggle source
# File lib/delegate_matcher/delegation.rb, line 24
def ok?
  allow_nil_ok? && delegation_ok? && arguments_ok? && block_ok? && return_value_ok?
end
return_value_failure_message(negated) click to toggle source
# File lib/delegate_matcher/delegation.rb, line 111
def return_value_failure_message(negated)
  case
  when !delegate.any?(&:received) || negated ^ return_value_ok?
    ''
  when negated
    ''
  else
    "a value of \"#{dispatcher.return_value}\" was returned instead of \"#{expected.return_value || delegate_return_value}\""
  end
end
return_value_ok?() click to toggle source
# File lib/delegate_matcher/delegation.rb, line 62
def return_value_ok?
  case
  when !expected.check_return
    true
  when expected.return_value.nil?
    dispatcher.return_value == delegate_return_value
  else
    dispatcher.return_value == expected.return_value
  end
end