Sha256: be66155f0ab1d4d3378074d251d793a41f5c96001a9036e3af8015d60ed9ea01

Contents?: true

Size: 866 Bytes

Versions: 6

Compression:

Stored size: 866 Bytes

Contents

# frozen_string_literal: true

module Pbt
  module Arbitrary
    # Generates a mapped value from another arbitrary.
    class MapArbitrary < Arbitrary
      # @param arb [Arbitrary] Arbitrary to generate values to be mapped.
      # @param mapper [Proc] Proc to map generated values. Mainly used for generation.
      # @param unmapper [Proc] Proc to unmap generated values. Used for shrinking.
      def initialize(arb, mapper, unmapper)
        @arb = arb
        @mapper = mapper
        @unmapper = unmapper
      end

      # @see Arbitrary#generate
      def generate(rng)
        @mapper.call(@arb.generate(rng))
      end

      # @see Arbitrary#shrink
      def shrink(current)
        Enumerator.new do |y|
          @arb.shrink(@unmapper.call(current)).each do |v|
            y.yield @mapper.call(v)
          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/map_arbitrary.rb
pbt-0.4.0 lib/pbt/arbitrary/map_arbitrary.rb
pbt-0.3.0 lib/pbt/arbitrary/map_arbitrary.rb
pbt-0.2.0 lib/pbt/arbitrary/map_arbitrary.rb
pbt-0.1.1 lib/pbt/arbitrary/map_arbitrary.rb
pbt-0.1.0 lib/pbt/arbitrary/map_arbitrary.rb