Method: Speculation.float_in

Defined in:
lib/speculation.rb

.float_in(min: nil, max: nil, infinite: true, nan: true) ⇒ Spec

Returns that validates floats.

Parameters:

  • infinite (Boolean) (defaults to: true)

    whether +/- infinity allowed (default true)

  • nan (Boolean) (defaults to: true)

    whether Flaot::NAN allowed (default true)

  • min (Boolean) (defaults to: nil)

    minimum value (inclusive, default none)

  • max (Boolean) (defaults to: nil)

    maximum value (inclusive, default none)

Returns:

  • (Spec)

    that validates floats



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/speculation.rb', line 94

def self.float_in(min: nil, max: nil, infinite: true, nan: true)
  preds = [Float]

  preds.push(->(x) { !x.nan? })      unless nan
  preds.push(->(x) { !x.infinite? }) unless infinite
  preds.push(->(x) { x <= max })     if max
  preds.push(->(x) { x >= min })     if min

  min ||= Float::MIN
  max ||= Float::MAX

  gens = [[20, ->(_) { rand(min.to_f..max.to_f) }]]
  gens << [1, ->(r) { r.choose(Float::INFINITY, -Float::INFINITY) }] if infinite
  gens << [1, ->(_) { Float::NAN }] if nan

  spec(self.and(*preds), :gen => ->(rantly) { rantly.freq(*gens) })
end