lib/prop_check/property.rb in prop_check-0.14.1 vs lib/prop_check/property.rb in prop_check-0.15.0

- old
+ new

@@ -100,11 +100,11 @@ # # If no other changes need to occur before you want to check the property, # you can immediately pass a block to this method. # (so `forall(a: Generators.integer).with_config(verbose: true) do ... end` is the same as `forall(a: Generators.integer).with_config(verbose: true).check do ... end`) def with_config(**config, &block) - duplicate = self.dup + duplicate = dup duplicate.instance_variable_set(:@config, @config.merge(config)) duplicate.freeze return duplicate.check(&block) if block_given? @@ -112,11 +112,11 @@ end def with_bindings(*bindings, **kwbindings) raise ArgumentError, 'No bindings specified!' if bindings.empty? && kwbindings.empty? - duplicate = self.dup + duplicate = dup duplicate.instance_variable_set(:@gen, gen_from_bindings(bindings, kwbindings)) duplicate.freeze duplicate end @@ -127,26 +127,28 @@ # If wanted, multiple `where`-conditions can be specified on a property. # 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) - raise ArgumentError, 'No generator bindings specified! #where should be called after `#forall` or `#with_bindings`.' unless @gen + unless @gen + raise ArgumentError, + 'No generator bindings specified! #where should be called after `#forall` or `#with_bindings`.' + end - duplicate = self.dup + duplicate = dup duplicate.instance_variable_set(:@gen, @gen.where(&condition)) duplicate.freeze duplicate end - ## # Calls `hook` before each time a check is run with new data. # # This is useful to add setup logic # When called multiple times, earlier-added hooks will be called _before_ `hook` is called. def before(&hook) - duplicate = self.dup + duplicate = dup duplicate.instance_variable_set(:@hooks, @hooks.add_before(&hook)) duplicate.freeze duplicate end @@ -154,11 +156,11 @@ # Calls `hook` after each time a check is run with new data. # # This is useful to add teardown logic # When called multiple times, earlier-added hooks will be called _after_ `hook` is called. def after(&hook) - duplicate = self.dup + duplicate = dup duplicate.instance_variable_set(:@hooks, @hooks.add_after(&hook)) duplicate.freeze duplicate end @@ -174,11 +176,11 @@ # # Note that if the block passed to `hook` raises an exception, # it is possible for the code after `yield` not to be called. # So make sure that cleanup logic is wrapped with the `ensure` keyword. def around(&hook) - duplicate = self.dup + duplicate = dup duplicate.instance_variable_set(:@hooks, @hooks.add_around(&hook)) duplicate.freeze duplicate end @@ -221,19 +223,19 @@ return if n_runs >= @config.n_runs raise_generator_exhausted! end - private def raise_generator_exhausted!() - raise Errors::GeneratorExhaustedError, """ + private def raise_generator_exhausted! + raise Errors::GeneratorExhaustedError, ''" Could not perform `n_runs = #{@config.n_runs}` runs, (exhausted #{@config.max_generate_attempts} tries) because too few generator results were adhering to the `where` condition. Try refining your generators instead. - """ + "'' end private def check_attempt(generator_result, n_successful, &block) PropCheck::Helper.call_splatted(generator_result.root, &block) @@ -244,11 +246,12 @@ # We want to capture _all_ exceptions (even low-level ones) here, # so we can shrink to find their cause. # don't worry: they all get reraised rescue Exception => e - output, shrunken_result, shrunken_exception, n_shrink_steps = show_problem_output(e, generator_result, n_successful, &block) + output, shrunken_result, shrunken_exception, n_shrink_steps = show_problem_output(e, generator_result, + n_successful, &block) output_string = output.is_a?(StringIO) ? output.string : e.message e.define_singleton_method :prop_check_info do { original_input: generator_result.root, @@ -262,22 +265,29 @@ raise e, output_string, e.backtrace end private def attempts_enum(binding_generator) - @hooks + @hooks .wrap_enum(raw_attempts_enum(binding_generator)) .lazy .take(@config.n_runs) end private def raw_attempts_enum(binding_generator) rng = Random::DEFAULT size = 1 (0...@config.max_generate_attempts) .lazy - .map { binding_generator.generate(size: size, rng: rng, max_consecutive_attempts: @config.max_consecutive_attempts) } + .map do + binding_generator.generate( + size: size, + rng: rng, + max_consecutive_attempts: @config.max_consecutive_attempts, + config: @config + ) + end .map do |result| size += 1 result end @@ -285,10 +295,11 @@ private def show_problem_output(problem, generator_results, n_successful, &block) output = @config.verbose ? STDOUT : StringIO.new output = PropCheck::Property::OutputFormatter.pre_output(output, n_successful, generator_results.root, problem) shrunken_result, shrunken_exception, n_shrink_steps = shrink(generator_results, output, &block) - output = PropCheck::Property::OutputFormatter.post_output(output, n_shrink_steps, shrunken_result, shrunken_exception) + output = PropCheck::Property::OutputFormatter.post_output(output, n_shrink_steps, shrunken_result, + shrunken_exception) [output, shrunken_result, shrunken_exception, n_shrink_steps] end private def shrink(bindings_tree, io, &block)