lib/prop_check/generator.rb in prop_check-0.10.2 vs lib/prop_check/generator.rb in prop_check-0.10.3

- old
+ new

@@ -8,21 +8,33 @@ # and the children are 'smaller' values related to the root, # to be used during the shrinking phase. class Generator @@default_size = 10 @@default_rng = Random.new + @@max_consecutive_attempts = 100 ## # Being a special kind of Proc, a Generator wraps a block. def initialize(&block) @block = block end ## # Given a `size` (integer) and a random number generator state `rng`, # generate a LazyTree. - def generate(size = @@default_size, rng = @@default_rng) - @block.call(size, rng) + def generate(size = @@default_size, rng = @@default_rng, max_consecutive_attempts = @@max_consecutive_attempts) + (0..max_consecutive_attempts).each do + res = @block.call(size, rng) + next if res == :"PropCheck.filter_me" + + return res + end + + raise Errors::GeneratorExhaustedError, """ + Exhausted #{max_consecutive_attempts} consecutive generation attempts. + + Probably too few generator results were adhering to a `where` condition. + """ end ## # Generates a value, and only return this value # (drop information for shrinking)