Class: Hegel::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/hegel/generator.rb,
sig/hegel.rbs

Overview

Base class for every value generator (Hegel::Generators.integers, .booleans, .arrays, and so on) and the two combinators, #map and #filter, that build a new generator out of an existing one. Mirrors hegel-rust's Generator trait: #do_draw is the one method a concrete generator implements, and #map/#filter come for free from this class rather than from each generator repeating them.

Defined Under Namespace

Classes: Filtered, Mapped

Instance Method Summary collapse

Instance Method Details

#do_draw(_tc) ⇒ Object

Draws one value using tc (a Hegel::TestCase). Every concrete generator implements this; the base class only exists to raise a clear error for a subclass that forgot to.

Parameters:

  • tc (Object)

Returns:

  • (Object)

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/hegel/generator.rb', line 17

def do_draw(_tc)
  raise NotImplementedError, "#{self.class} must implement #do_draw"
end

#filter {|arg0| ... } ⇒ Generator

Returns a new Generator whose #do_draw retries this generator until block returns true for the drawn value, or discards the test case if it never does. See Filtered for the retry budget.

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (boolish)

Returns:



32
33
34
# File 'lib/hegel/generator.rb', line 32

def filter(&block)
  Filtered.new(self, block)
end

#map {|arg0| ... } ⇒ Generator

Returns a new Generator whose #do_draw runs block on the value this generator drew, spanned with HEGEL_LABEL_MAPPED so the shrinker treats the source draw and the transform as one unit (see hegel-rust's Mapped, src/generators/generators.rs).

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (Object)

Returns:



25
26
27
# File 'lib/hegel/generator.rb', line 25

def map(&block)
  Mapped.new(self, block)
end