class RSpec::Maybes::MaybeTarget

Wraps the target of a maybe.

@example

maybe(something)       # => MaybeTarget wrapping something
maybe { do_something } # => MaybeTarget wrapping the block

# used with `will`
maybe(actual).will eq(3)

# with `will_not`
maybe(actual).will_not eq(3)

@note `MaybeTarget` is not intended to be instantiated

directly by users. Use `maybe` instead.

Constants

UndefinedValue

@private Used as a sentinel value to be able to tell when the user did not pass an argument. We can't use `nil` for that because `nil` is a valid value to pass.

Public Class Methods

for(value, block) click to toggle source

@private

# File lib/rspec/maybes/maybe_target.rb, line 32
def self.for(value, block)
  if UndefinedValue.equal?(value)
    unless block
      raise ArgumentError, "You must pass either an argument or a block to `maybe`."
    end
    BlockMaybeTarget.new(block)
  elsif block
    raise ArgumentError, "You cannot pass both an argument and a block to `maybe`."
  else
    new(value)
  end
end
new(value) click to toggle source

@api private

# File lib/rspec/maybes/maybe_target.rb, line 27
def initialize(value)
  @target = value
end

Public Instance Methods

will(matcher = nil, message = nil, &block) click to toggle source

Runs the given maybe, passing randomly. @example

maybe(value).will eq(5)
maybe { perform }.will_not raise_error

@param [Matcher]

matcher

@param [String or Proc] message optional message to display when the expectation fails @return [Boolean] true if the maybe succeeds (else raises) @see RSpec::Matchers

# File lib/rspec/maybes/maybe_target.rb, line 54
def will(matcher = nil, message = nil, &block)
  prevent_operator_matchers(:will) unless matcher
  RSpec::Maybes::PositiveMaybeHandler.handle_matcher(@target, matcher, message, &block)
end
will_not(matcher = nil, message = nil, &block) click to toggle source

Runs the given maybe, failing randomly. @example

maybe(value).will_not eq(5)

@param [Matcher]

matcher

@param [String or Proc] message optional message to display when the maybe fails @return [Boolean] false if the negative maybe succeeds (else raises) @see RSpec::Matchers

# File lib/rspec/maybes/maybe_target.rb, line 67
def will_not(matcher = nil, message = nil, &block)
  prevent_operator_matchers(:will_not) unless matcher
  RSpec::Maybes::NegativeMaybeHandler.handle_matcher(@target, matcher, message, &block)
end

Private Instance Methods

prevent_operator_matchers(verb) click to toggle source
# File lib/rspec/maybes/maybe_target.rb, line 74
def prevent_operator_matchers(verb)
  raise ArgumentError, "The maybe syntax does not support operator matchers, " \
                       "so you must pass a matcher to `##{verb}`."
end