class Forall::Matchers::ForallMatcher

Public Class Methods

new(input) click to toggle source
# File lib/forall/matchers.rb, line 29
def initialize(input)
  @input = Input.build(input)
end

Public Instance Methods

check(property = nil, seed: nil, &block) click to toggle source
# File lib/forall/matchers.rb, line 33
def check(property = nil, seed: nil, &block)
  property ||= block
  random = Forall::Random.new(seed: seed)
  result = Forall.check(@input, random, nil, &property)

  if defined?(RSpec::Expectations)
    case result
    when Forall::Ok
    when Forall::Vacuous
      message = "gave up (after %d test%s and %d discarded)\nSeed: %d" %
        [result.counter.test,
         result.counter.test == 1 ? "" : "s",
         result.counter.skip,
         result.seed]

      error   = ::RSpec::Expectations::ExpectationNotMetError.new(message)
      source  = property.binding.source_location.join(":")
      source << " in `block in ...'"
      error.set_backtrace(source)
      raise ::RSpec::Expectations::ExpectationNotMetError, message

    else
      if Forall::No === result or RSpec::Expectations::ExpectationNotMetError === result.error
        message = "falsified (after %d test%s%s):\nInput: %s\nSeed:  %d" %
          [result.counter.ok,
           result.counter.ok == 1 ? "" : "s",
           (result.counter.shrunk.steps == 1 ? "and 1 shrink" :
            result.counter.shrunk.steps == 0 ? "" : "and #{result.counter.shrinks} shrinks"),
           result.counterexample.inspect,
           result.seed]

        error   = ::RSpec::Expectations::ExpectationNotMetError.new(message)
        source  = property.binding.source_location.join(":")
        source << " in `block in ...'"
        error.set_backtrace(source)
        raise error
      else
        message = "exception %s (after %d test%s%s):\nInput: %s\nSeed: %d" %
          [result.error,
           result.counter.ok,
           result.counter.ok == 1 ? "" : "s",
           (result.counter.shrunk.steps == 1 ? "and 1 shrink" :
            result.counter.shrunk.steps == 0 ? "" : "and #{result.counter.shrinks} shrinks"),
           result.counterexample.inspect,
           result.seed]

        error   = ::RSpec::Expectations::ExpectationNotMetError.new(message)
        source  = property.binding.source_location.join(":")
        source << " in `block in ...'"
        error.set_backtrace(source)
        raise error
      end
    end
  else
    result
  end
end