Sha256: 092b53cdbb821e419a6406b0607f0f108a3887fd01aa5a739615b11ec60c41f2
Contents?: true
Size: 967 Bytes
Versions: 2
Compression:
Stored size: 967 Bytes
Contents
require 'bigdecimal' module Pact module Generator # RandomDecimal provides the random decimal generator which will generate a decimal value of digits length class RandomDecimal def can_generate?(hash) hash.key?('type') && hash['type'] == 'RandomDecimal' end def call(hash, _params = nil, _example_value = nil) digits = hash['digits'] || 6 raise 'RandomDecimalGenerator digits must be > 0, got $digits' if digits < 1 return rand(0..9) if digits == 1 return rand(0..9) + rand(1..9) / 10 if digits == 2 pos = rand(1..digits - 1) precision = digits - pos integers = '' decimals = '' while pos.positive? integers += String(rand(1..9)) pos -= 1 end while precision.positive? decimals += String(rand(1..9)) precision -= 1 end Float("#{integers}.#{decimals}") end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
pact-support-1.21.1 | lib/pact/generator/random_decimal.rb |
pact-support-1.21.0 | lib/pact/generator/random_decimal.rb |