Module: Properb

Defined in:
lib/properb.rb,
lib/properb/generator.rb,
lib/properb/generators.rb,
lib/properb/shrink_tree.rb,
lib/properb/rspec/example_methods.rb,
lib/properb/generators/array_generator.rb,
lib/properb/generators/sized_generator.rb,
lib/properb/generators/tuple_generator.rb,
lib/properb/generators/choice_generator.rb,
lib/properb/generators/mapped_generator.rb,
lib/properb/rspec/example_group_methods.rb,
lib/properb/generators/integer_generator.rb,
lib/properb/generators/constant_generator.rb,
lib/properb/generators/deferred_generator.rb,
lib/properb/generators/selected_generator.rb,
lib/properb/generators/recursive_generator.rb,
lib/properb/generators/frequencies_generator.rb

Defined Under Namespace

Modules: AllExceptionsExceptOnesWeMustNotRescue, Generators, RSpec Classes: Generator, ShrinkTree, TestRunner

Class Method Summary collapse

Class Method Details

.for_all(generators, size: 0..10, num_tests: 100, assumption_limit: 10, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/properb.rb', line 51

def self.for_all(generators, size: 0..10, num_tests: 100, assumption_limit: 10, &block)
  test_runner = TestRunner.new(block)

  random = Random.new
  generated = nil
  passed = Set.new
  generator = Generators.hashmap(generators)
  current_size = if size.is_a?(Range)
                   size.min
                 else
                   size
                 end
  num_tests.times do
    result = false
    attempt_number = 0
    until result
      if attempt_number >= assumption_limit
        raise StandardError,
              "Could not satisfy assumptions after #{assumption_limit} attempts"
      end

      current_size += 1 if size.is_a?(Range) && size.include?(current_size + 1)
      generated = generator.generate_value(random, current_size)
      result = test_runner.run(generated.value)
      attempt_number += 1
    end
    passed << generated.value
  end
rescue AllExceptionsExceptOnesWeMustNotRescue => e
  exception, = shrink(generated, e, passed, test_runner)
  raise exception
end

.rspec_install(config) ⇒ Object



22
23
24
25
26
# File 'lib/properb.rb', line 22

def self.rspec_install(config)
  config.extend Properb::RSpec::ExampleGroupMethods
  config.include Properb::RSpec::ExampleMethods
  config.extend Properb::Generators
end

.shrink(generated, exception, passed, test_runner) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/properb.rb', line 84

def self.shrink(generated, exception, passed, test_runner)
  smallest = generated
  while smallest
    begin
      current = nil
      smallest.children.each do |child|
        current = child
        next if passed.include?(current.value)

        if test_runner.run(current.value)
          passed << current.value
        else
          # If the assumptions have failed then we don't learn anything
          # useful. We process this node's children before continuing,
          # in the hopes that we get some information from them instead.
          e, current = shrink(current, exception, passed, test_runner)
          raise e unless e == exception
        end
      end
      break
    rescue AllExceptionsExceptOnesWeMustNotRescue => e
      smallest = current
      exception = e
    end
  end
  [exception, smallest]
end