Sha256: 46d0c77d305fac9b8158ba2348a30f3b67a4c1792894b4d72151ce8d608de750

Contents?: true

Size: 868 Bytes

Versions: 6

Compression:

Stored size: 868 Bytes

Contents

# frozen_string_literal: true

module Pbt
  module Arbitrary
    # Generates a tuple of arbitrary values.
    class TupleArbitrary < Arbitrary
      # @param arbs [Array<Arbitrary>] Arbitraries used to generate the values of the tuple.
      def initialize(*arbs)
        @arbs = arbs
      end

      # @see Arbitrary#generate
      def generate(rng)
        @arbs.map { |arb| arb.generate(rng) }
      end

      # @see Arbitrary#shrink
      def shrink(current)
        # This is not the most comprehensive but allows a reasonable number of entries in the shrink.
        Enumerator.new do |y|
          @arbs.each_with_index do |arb, idx|
            arb.shrink(current[idx]).each do |v|
              next_values = current.dup
              next_values[idx] = v
              y << next_values
            end
          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/tuple_arbitrary.rb
pbt-0.4.0 lib/pbt/arbitrary/tuple_arbitrary.rb
pbt-0.3.0 lib/pbt/arbitrary/tuple_arbitrary.rb
pbt-0.2.0 lib/pbt/arbitrary/tuple_arbitrary.rb
pbt-0.1.1 lib/pbt/arbitrary/tuple_arbitrary.rb
pbt-0.1.0 lib/pbt/arbitrary/tuple_arbitrary.rb