Sha256: c02c1292b8d6db541819489997701a0a85bc3a24b4349b232d1b6564807b7cc4

Contents?: true

Size: 850 Bytes

Versions: 6

Compression:

Stored size: 850 Bytes

Contents

# frozen_string_literal: true

module Pbt
  module Arbitrary
    # Generates a hash with fixed keys and arbitrary values.
    class FixedHashArbitrary < Arbitrary
      # @param hash [Hash<Object, Arbitrary<T>>] Hash with any keys and arbitraries as values.
      def initialize(hash)
        @keys = hash.keys
        @arb = TupleArbitrary.new(*hash.values)
      end

      # @see Arbitrary#generate
      def generate(rng)
        values = @arb.generate(rng)
        @keys.zip(values).to_h
      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|
          @arb.shrink(current.values).each do |next_values|
            y << @keys.zip(next_values).to_h
          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/fixed_hash_arbitrary.rb
pbt-0.4.0 lib/pbt/arbitrary/fixed_hash_arbitrary.rb
pbt-0.3.0 lib/pbt/arbitrary/fixed_hash_arbitrary.rb
pbt-0.2.0 lib/pbt/arbitrary/fixed_hash_arbitrary.rb
pbt-0.1.1 lib/pbt/arbitrary/fixed_hash_arbitrary.rb
pbt-0.1.0 lib/pbt/arbitrary/fixed_hash_arbitrary.rb