Module: Speculation::Gen

Defined in:
lib/speculation/gen.rb

Class Method Summary collapse

Class Method Details

.gen_for_pred(pred) ⇒ Proc

Given a predicate, returns a built-in generator if one exists.

See Also:


55
56
57
58
59
60
61
# File 'lib/speculation/gen.rb', line 55

def self.gen_for_pred(pred)
  if pred.is_a?(Set)
    ->(r) { r.choose(*pred) }
  else
    GEN_BUILTINS[pred]
  end
end

.generate(gen, limit = 100) ⇒ Object

Returns single value using gen

See Also:


37
38
39
# File 'lib/speculation/gen.rb', line 37

def self.generate(gen, limit = 100)
  Rantly.value(limit, &gen)
end

.sample(gen, n = 10, limit = 100) ⇒ Array

Generate `n` values using `gen`

See Also:


46
47
48
# File 'lib/speculation/gen.rb', line 46

def self.sample(gen, n = 10, limit = 100)
  Rantly.map(n, limit, &gen)
end

.such_that(gen) { ... } ⇒ Proc

Adds pred block as a Rantly `guard` to generator `gen`.

Yields:

  • generated value

Raises:

  • (ArgumentError)

See Also:


25
26
27
28
29
30
31
# File 'lib/speculation/gen.rb', line 25

def self.such_that(gen)
  raise ArgumentError, "block required" unless block_given?

  ->(rantly) do
    gen.call(rantly).tap { |val| rantly.guard(yield(val)) }
  end
end