Class: Pbt::Arbitrary::OneOfArbitrary

Inherits:
Arbitrary
  • Object
show all
Defined in:
lib/pbt/arbitrary/one_of_arbitrary.rb

Overview

Generates one of the given choices.

Instance Method Summary collapse

Methods inherited from Arbitrary

#filter, #map

Constructor Details

#initialize(choices) ⇒ OneOfArbitrary

Returns a new instance of OneOfArbitrary.

Parameters:

  • choices (Array)

    List of choices.



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

#generate(rng) ⇒ Object

See Also:



14
15
16
# File 'lib/pbt/arbitrary/one_of_arbitrary.rb', line 14

def generate(rng)
  @choices[@idx_arb.generate(rng)]
end

#shrink(current) ⇒ Object

See Also:



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