Sha256: be0b94699bcd63d0eeab27a50d27900a5ea03c3040121c49771079ee185f9b1a

Contents?: true

Size: 722 Bytes

Versions: 6

Compression:

Stored size: 722 Bytes

Contents

# frozen_string_literal: true

module Pbt
  module Arbitrary
    # Generates one of the given choices.
    class OneOfArbitrary < Arbitrary
      # @param choices [Array] List of choices.
      def initialize(choices)
        @choices = choices
        @idx_arb = IntegerArbitrary.new(0, choices.size - 1)
      end

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

      # @see Arbitrary#shrink
      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
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
pbt-0.5.0 lib/pbt/arbitrary/one_of_arbitrary.rb
pbt-0.4.0 lib/pbt/arbitrary/one_of_arbitrary.rb
pbt-0.3.0 lib/pbt/arbitrary/one_of_arbitrary.rb
pbt-0.2.0 lib/pbt/arbitrary/one_of_arbitrary.rb
pbt-0.1.1 lib/pbt/arbitrary/one_of_arbitrary.rb
pbt-0.1.0 lib/pbt/arbitrary/one_of_arbitrary.rb