class RSpecial::Expect

Wraps the target of an expectation.

@example

expect(something) # => Expect

# used with `to`
expect(actual).to eq(3)

# with `to_not`
expect(actual).to_not eq(3)

Public Class Methods

new(target) click to toggle source

@api private

# File lib/rspecial/expect.rb, line 17
def initialize(target)
  @target = target
end

Public Instance Methods

not_to(matcher=nil, message=nil, &block)
Alias for: to_not
to(matcher=nil, message=nil, &block) click to toggle source

Runs the given expectation, passing if ‘matcher` returns true.

@example

expect(value).to eq(5)
expect { perform }.to raise_error

@param [Matcher]

matcher

@param [String] message optional message to display when the expectation fails

@return [Boolean] true if the expectation succeeds (else raises)

@see RSpec::Matchers

# File lib/rspecial/expect.rb, line 36
def to(matcher=nil, message=nil, &block)
  #prevent_operator_matchers(:to, matcher)
  handle_positive_matcher(@target, matcher, message, &block)
end
to_not(matcher=nil, message=nil, &block) click to toggle source

Runs the given expectation, passing if ‘matcher` returns false.

@example

expect(value).to_not eq(5)
expect(value).not_to eq(5)

@param [Matcher]

matcher

@param [String] message optional message to display when the expectation fails

@return [Boolean] false if the negative expectation succeeds (else raises)

@see RSpec::Matchers

# File lib/rspecial/expect.rb, line 56
def to_not(matcher=nil, message=nil, &block)
  #prevent_operator_matchers(:to_not, matcher)
  handle_negative_matcher(@target, matcher, message, &block)
end
Also aliased as: not_to

Private Instance Methods

check_message(msg) click to toggle source
# File lib/rspecial/expect.rb, line 145
def check_message(msg)
  ::Kernel.warn message_must_be_string(msg) unless msg.nil? || msg.is_a?(String)
end
handle_negative_matcher(actual, matcher, message=nil, &block) click to toggle source

@todo how to customize the message?

# File lib/rspecial/expect.rb, line 108
def handle_negative_matcher(actual, matcher, message=nil, &block)
  check_message(message)

  #::RSpec::Matchers.last_should = :should_not
  #::RSpec::Matchers.last_matcher = matcher

  if block
    actual = block.call(actual)
  end

  #return ::RSpec::Matchers::BuiltIn::NegativeOperatorMatcher.new(actual) if matcher.nil?

  return Operatics.new(actual, true) if matcher.nil?

  matcher !~ actual

  #match = matcher.respond_to?(:does_not_match?) ?
  #        !matcher.does_not_match?(actual, &block) :
  #        matcher.matches?(actual, &block)
  #return match unless match

  #message ||= matcher.respond_to?(:failure_message_for_should_not) ?
  #            matcher.failure_message_for_should_not :
  #            matcher.negative_failure_message

  #if matcher.respond_to?(:diffable?) && matcher.diffable?
  #  ::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual
  #else
  #  ::RSpec::Expectations.fail_with message
  #end
end
handle_positive_matcher(actual, matcher, message=nil, &block) click to toggle source

@todo how to customize the message?

# File lib/rspecial/expect.rb, line 75
def handle_positive_matcher(actual, matcher, message=nil, &block)
  check_message(message)

  #::RSpec::Matchers.last_should = :should
  #::RSpec::Matchers.last_matcher = matcher

  if block
    actual = block.call(actual)
  end

  #return ::RSpec::Matchers::BuiltIn::PositiveOperatorMatcher.new(actual) if matcher.nil?

  return Operatics.new(actual) if matcher.nil?

  matcher =~ actual

  #match = matcher.matches?(actual, &block)
  #return match if match

  #message ||= matcher.respond_to?(:failure_message_for_should) ?
  #            matcher.failure_message_for_should :
  #            matcher.failure_message

  #if matcher.respond_to?(:diffable?) && matcher.diffable?
  #  ::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual
  #else
  #  ::RSpec::Expectations.fail_with message
  #end
end
message_must_be_string(msg) click to toggle source
# File lib/rspecial/expect.rb, line 140
def message_must_be_string(msg)
  "WARNING: ignoring the provided expectation message argument " +
  "(#{msg.inspect}) since it is not a string."
end