Class: Forall

Inherits:
Object
  • Object
show all
Defined in:
lib/forall.rb,
lib/forall/input.rb,
lib/forall/random.rb,
lib/forall/shrink.rb,
lib/forall/counter.rb,
lib/forall/matchers.rb

Defined Under Namespace

Modules: Matchers Classes: C, Counter, Fail, Input, No, Ok, Options, Random, Shrink, Vacuous

Class Method Summary collapse

Class Method Details

.check(input, random, options = nil, &prop) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/forall.rb', line 30

def check(input, random, options = nil, &prop)
  options            ||= Options.new
  options.max_shrink ||= 100

  if input.exhaustive?
    options.max_ok   ||= input.size * 0.90
    options.max_skip ||= input.size * 0.10
  else
    options.max_ok   ||= 100
    options.max_skip ||= options.max_ok * 0.10
  end

  if prop.arity == 1
    _prop = prop
    prop  = lambda{|x,_| _prop.call(x) }
  end

  raise ArgumentError, "property must take one or two arguments" \
    unless prop.arity == 2

  counter = Counter.new

  input.each(random) do |example|
    return Ok.new(random.seed, counter)      if counter.ok   >= options.max_ok
    return Vacuous.new(random.seed, counter) if counter.skip >= options.max_skip

    catch(:skip) do
      if prop.call(example, counter)
        counter.ok += 1
      else
        return no(random, counter, input.shrink, example, options, prop)
      end
    end
  rescue Exception => error
    counter.fail += 1
    return fail(random, counter, input.shrink, example, options, prop, error)
  end

  # Didn't meet ok_max because input was exhausted
  Ok.new(random.seed, counter)
end