lib/prop_check/generators.rb in prop_check-0.11.0 vs lib/prop_check/generators.rb in prop_check-0.11.1

- old
+ new

@@ -8,10 +8,11 @@ # Contains common generators. # Use this module by including it in the class (e.g. in your test suite) # where you want to use them. module Generators extend self + ## # Always returns the same value, regardless of `size` or `rng` (random number generator state) # # No shrinking (only considers the current single value `val`). # @@ -57,11 +58,11 @@ # >> r = Random.new(42); Generators.choose(0..5).sample(size: 10, rng: r) # => [3, 4, 2, 4, 4, 1, 2, 2, 2, 4] # >> r = Random.new(42); Generators.choose(0..5).sample(size: 20000, rng: r) # => [3, 4, 2, 4, 4, 1, 2, 2, 2, 4] def choose(range) - Generator.new do |_size, rng| + Generator.new do |rng:, **| val = rng.rand(range) LazyTree.new(val, integer_shrink(val)) end end @@ -71,18 +72,18 @@ # and become more extreme (both higher and lower, negative) when `size` increases. # # # Shrinks to integers closer to zero. # - # >> Generators.integer.call(2, Random.new(42)) + # >> Generators.integer.call(size: 2, rng: Random.new(42)) # => 1 - # >> Generators.integer.call(10000, Random.new(42)) + # >> Generators.integer.call(size: 10000, rng: Random.new(42)) # => 5795 # >> r = Random.new(42); Generators.integer.sample(size: 20000, rng: r) # => [-4205, -19140, 18158, -8716, -13735, -3150, 17194, 1962, -3977, -18315] def integer - Generator.new do |size, rng| + Generator.new do |size:, rng:, **| val = rng.rand(-size..size) LazyTree.new(val, integer_shrink(val)) end end @@ -191,16 +192,16 @@ # Generates an array containing always exactly one value from each of the passed generators, # in the same order as specified: # # Shrinks element generators, one at a time (trying last one first). # - # >> Generators.tuple(Generators.integer, Generators.real_float).call(10, Random.new(42)) + # >> Generators.tuple(Generators.integer, Generators.real_float).call(size: 10, rng: Random.new(42)) # => [-4, 13.0] def tuple(*generators) - Generator.new do |size, rng| + Generator.new do |**kwargs| LazyTree.zip(generators.map do |generator| - generator.generate(size, rng) + generator.generate(**kwargs) end) end end ## @@ -208,11 +209,11 @@ # creates a generator that returns hashes # with the same keys, and their corresponding values from their corresponding generators. # # Shrinks element generators. # - # >> Generators.fixed_hash(a: Generators.integer(), b: Generators.real_float(), c: Generators.integer()).call(10, Random.new(42)) + # >> Generators.fixed_hash(a: Generators.integer(), b: Generators.real_float(), c: Generators.integer()).call(size: 10, rng: Random.new(42)) # => {:a=>-4, :b=>13.0, :c=>-3} def fixed_hash(hash) keypair_generators = hash.map do |key, generator| generator.map { |val| [key, val] } @@ -263,24 +264,34 @@ else proc.call(max) end end - ## # Generates a hash of key->values, # where each of the keys is made using the `key_generator` # and each of the values using the `value_generator`. # # Shrinks to hashes with less key/value pairs. # # >> Generators.hash(Generators.printable_ascii_string, Generators.positive_integer).sample(5, size: 3, rng: Random.new(42)) # => [{""=>2, "g\\4"=>4, "rv"=>2}, {"7"=>2}, {"!"=>1, "E!"=>1}, {"kY5"=>2}, {}] - def hash(key_generator, value_generator, **kwargs) + def hash(*args, **kwargs) + if args.length == 2 + hash_of(*args, **kwargs) + else + super + end + end + + ## + # + # Alias for `#hash` that does not conflict with a possibly overriden `Object#hash`. + # + def hash_of(key_generator, value_generator, **kwargs) array(tuple(key_generator, value_generator), **kwargs) .map(&:to_h) end - @alphanumeric_chars = [('a'..'z'), ('A'..'Z'), ('0'..'9')].flat_map(&:to_a).freeze ## # Generates a single-character string # containing one of a..z, A..Z, 0..9