Sha256: b85e202d1a6a35971c33c90d7f1ad9b251cdba2f694bde23270ddf8416a948e9

Contents?: true

Size: 618 Bytes

Versions: 6

Compression:

Stored size: 618 Bytes

Contents

# frozen_string_literal: true

module Pbt
  module Arbitrary
    # Generates a random value from a range.
    class ChooseArbitrary < Arbitrary
      # @param range [Range<Integer>]
      def initialize(range)
        @range = range
      end

      # @see Arbitrary#generate
      def generate(rng)
        rng.rand(@range)
      end

      # @see Arbitrary#shrink
      def shrink(current)
        # Range is ordered from min to max, so we can just shrink towards min.
        min, max = [@range.begin, @range.end].sort
        IntegerArbitrary.new(min, max).shrink(current, target: min)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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