class RSpec::Clone::ExpectationTarget::Base

Abstract expectation target base class.

@note `RSpec::Clone::ExpectationTarget::Base` is not intended to be

instantiated directly by users. Use `expect` instead.

Public Class Methods

new(input) click to toggle source

Instantiate a new expectation target.

@param input [#object_id, Proc] The code to evaluate.

# File lib/r_spec/clone/expectation_target/base.rb, line 19
def initialize(input)
  @input = input
end

Public Instance Methods

not_to(matcher) click to toggle source

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

@example _Absolute prohibition_ definition

expect { "foo".size }.not_to be(4)

@param (see to)

@raise (see result) @return (see result)

@api public

# File lib/r_spec/clone/expectation_target/base.rb, line 49
def not_to(matcher)
  absolute_requirement(matcher: matcher, negate: true)
end
to(matcher) click to toggle source

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

@example _Absolute requirement_ definition

expect { "foo".upcase }.to eq("foo")

@param matcher [#matches?] The matcher.

@raise (see result) @return (see result)

@api public

# File lib/r_spec/clone/expectation_target/base.rb, line 34
def to(matcher)
  absolute_requirement(matcher: matcher, negate: false)
end

Protected Instance Methods

absolute_requirement(test, matcher:, negate:) click to toggle source

@param test [::TestTube::Base] The state of the experiment. @param matcher [#matches?] The matcher. @param negate [Boolean] The assertion is positive or negative.

@return [nil] Write a message to STDOUT.

@raise [SystemExit] Terminate execution immediately by calling

`Kernel.exit(false)` with a failure message written to STDERR.
# File lib/r_spec/clone/expectation_target/base.rb, line 63
def absolute_requirement(test, matcher:, negate:)
  result(
    passed?(test),
    actual:  test.actual,
    error:   test.error,
    got:     test.got,
    matcher: matcher,
    negate:  negate
  )
end
passed?(test) click to toggle source

Code experiment result.

@param test [::TestTube::Base] The state of the experiment.

@see github.com/fixrb/test_tube

@return [Boolean] The result of the test (passed or failed).

# File lib/r_spec/clone/expectation_target/base.rb, line 81
def passed?(test)
  test.got.equal?(true)
end
result(passed, actual:, error:, got:, matcher:, negate:) click to toggle source

@param passed [Boolean] The high expectation passed or failed. @param actual [#object_id] The actual value. @param error [Exception, nil] Any raised exception. @param got [Boolean, nil] Any returned value. @param matcher [#matches?] The matcher. @param negate [Boolean] The assertion is positive or negative.

@return [nil] Write a message to STDOUT.

@raise [SystemExit] Terminate execution immediately by calling

`Kernel.exit(false)` with a failure message written to STDERR.
# File lib/r_spec/clone/expectation_target/base.rb, line 96
def result(passed, actual:, error:, got:, matcher:, negate:)
  Console.passed_spec ::Expresenter.call(passed).with(
    actual:     actual,
    definition: matcher.to_s,
    error:      error,
    expected:   matcher.expected,
    got:        got,
    negate:     negate,
    level:      :MUST
  )
rescue ::Expresenter::Fail => e
  Console.failed_spec(e)
end