Class: Pbt::Arbitrary::OneOfArbitrary
- Defined in:
- lib/pbt/arbitrary/one_of_arbitrary.rb
Overview
Generates one of the given choices.
Instance Method Summary collapse
- #generate(rng) ⇒ Object
-
#initialize(choices) ⇒ OneOfArbitrary
constructor
A new instance of OneOfArbitrary.
- #shrink(current) ⇒ Object
Methods inherited from Arbitrary
Constructor Details
permalink #initialize(choices) ⇒ OneOfArbitrary
Returns a new instance of OneOfArbitrary.
8 9 10 11 |
# File 'lib/pbt/arbitrary/one_of_arbitrary.rb', line 8 def initialize(choices) @choices = choices @idx_arb = IntegerArbitrary.new(0, choices.size - 1) end |
Instance Method Details
permalink #generate(rng) ⇒ Object
14 15 16 |
# File 'lib/pbt/arbitrary/one_of_arbitrary.rb', line 14 def generate(rng) @choices[@idx_arb.generate(rng)] end |
permalink #shrink(current) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/pbt/arbitrary/one_of_arbitrary.rb', line 19 def shrink(current) # Shrinks to values earlier in the list of `choices`. Enumerator.new do |y| @idx_arb.shrink(@choices.index(current)).map do |idx| y << @choices[idx] end end end |