lib/prop_check/property.rb in prop_check-0.9.0 vs lib/prop_check/property.rb in prop_check-0.10.0
- old
+ new
@@ -1,9 +1,10 @@
require 'stringio'
require "awesome_print"
require 'prop_check/property/configuration'
+require 'prop_check/hooks'
module PropCheck
##
# Run properties
class Property
@@ -65,10 +66,11 @@
@bindings = bindings
@kwbindings = kwbindings
@condition = proc { true }
@config = self.class.configuration
+ @hooks = PropCheck::Hooks.new
end
##
# Returns the configuration of this property
# for introspection.
@@ -101,17 +103,32 @@
# Be aware that if you filter away too much generated inputs,
# you might encounter a GeneratorExhaustedError.
# Only filter if you have few inputs to reject. Otherwise, improve your generators.
def where(&condition)
original_condition = @condition.dup
- @condition = proc do |**kwargs|
- original_condition.call(**kwargs) && condition.call(**kwargs)
+ @condition = proc do |*args|
+ original_condition.call(*args) && condition.call(*args)
end
self
end
+ def before(&hook)
+ @hooks.add_before(&hook)
+ self
+ end
+
+ def after(&hook)
+ @hooks.add_after(&hook)
+ self
+ end
+
+ def around(&hook)
+ @hooks.add_around(&hook)
+ self
+ end
+
##
# Checks the property (after settings have been altered using the other instance methods in this class.)
def check(&block)
gens =
if @kwbindings != {}
@@ -125,11 +142,11 @@
n_runs = 0
n_successful = 0
# Loop stops at first exception
- attempts_enumerator(binding_generator).each do |generator_result|
+ attempts_enum(binding_generator).each do |generator_result|
n_runs += 1
check_attempt(generator_result, n_successful, &block)
n_successful += 1
end
@@ -180,27 +197,30 @@
end
raise e, output_string, e.backtrace
end
- private def attempts_enumerator(binding_generator)
+ private def attempts_enum(binding_generator)
+ @hooks
+ .wrap_enum(raw_attempts_enum(binding_generator))
+ .lazy
+ .take(@config.n_runs)
+ end
+ private def raw_attempts_enum(binding_generator)
rng = Random::DEFAULT
- n_runs = 0
size = 1
(0...@config.max_generate_attempts)
.lazy
.map { binding_generator.generate(size, rng) }
.reject { |val| val.root.any? { |elem| elem == :"_PropCheck.filter_me" }}
.select { |val| @condition.call(*val.root) }
.map do |result|
- n_runs += 1
size += 1
result
end
- .take_while { n_runs <= @config.n_runs }
end
private def show_problem_output(problem, generator_results, n_successful, &block)
output = @config.verbose ? STDOUT : StringIO.new
output = pre_output(output, n_successful, generator_results.root, problem)
@@ -251,10 +271,10 @@
problem_child = bindings_tree
siblings = problem_child.children.lazy
parent_siblings = nil
problem_exception = nil
shrink_steps = 0
- (0..@config.max_shrink_steps).each do
+ @hooks.wrap_enum(0..@config.max_shrink_steps).lazy.each do
begin
sibling = siblings.next
rescue StopIteration
break if parent_siblings.nil?