Class: Pbt::Arbitrary::FixedHashArbitrary

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

Overview

Generates a hash with fixed keys and arbitrary values.

Instance Method Summary collapse

Methods inherited from Arbitrary

#filter, #map

Constructor Details

#initialize(hash) ⇒ FixedHashArbitrary

Returns a new instance of FixedHashArbitrary.

Parameters:

  • hash (Hash<Object, Arbitrary<T>>)

    Hash with any keys and arbitraries as values.



8
9
10
11
# File 'lib/pbt/arbitrary/fixed_hash_arbitrary.rb', line 8

def initialize(hash)
  @keys = hash.keys
  @arb = TupleArbitrary.new(*hash.values)
end

Instance Method Details

#generate(rng) ⇒ Object

See Also:



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

def generate(rng)
  values = @arb.generate(rng)
  @keys.zip(values).to_h
end

#shrink(current) ⇒ Object

See Also:



20
21
22
23
24
25
26
27
# File 'lib/pbt/arbitrary/fixed_hash_arbitrary.rb', line 20

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