Method: PropCheck::Generators.frequency

Defined in:
lib/prop_check/generators.rb

.frequency(frequencies) ⇒ Object

Picks one of the choices given in frequencies at random every time. frequencies expects keys to be numbers (representing the relative frequency of this generator) and values to be generators.

Side note: If you want to use the same frequency number for multiple generators, Ruby syntax requires you to send an array of two-element arrays instead of a hash.

Shrinks to arbitrary elements (since hashes are not ordered).

>> Generators.frequency(5 => Generators.integer, 1 => Generators.printable_ascii_char).sample(size: 10, rng: Random.new(42))
=> [4, -3, 10, 8, 0, -7, 10, 1, "E", 10]


302
303
304
305
306
307
308
# File 'lib/prop_check/generators.rb', line 302

def frequency(frequencies)
  choices = frequencies.reduce([]) do |acc, elem|
    freq, val = elem
    acc + ([val] * freq)
  end
  one_of(*choices)
end